diff options
author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-10-04 17:11:19 +0200 |
---|---|---|
committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2024-10-04 17:11:19 +0200 |
commit | bd2b435e7cd782cacfc4c74af3573de7a1791c8e (patch) | |
tree | f631881faa69f94b3de3754459f50b1033b3e830 /src/main.rs | |
download | quotify-bd2b435e7cd782cacfc4c74af3573de7a1791c8e.tar.gz quotify-bd2b435e7cd782cacfc4c74af3573de7a1791c8e.zip |
chore: Initial Commit
Diffstat (limited to '')
-rw-r--r-- | src/main.rs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a6b047b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,39 @@ +// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de> +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// This file is part of Quotify - A simple CLI utility to shell quote the text +// inputted into it. +// +// You should have received a copy of the License along with this program. +// If not, see <https://www.gnu.org/licenses/agpl.txt>. + +use std::{ + env::args, + io::{stdin, Read}, +}; + +use anyhow::{Context, Result}; + +fn main() -> Result<()> { + let text: String = { + if args().count() != 1 { + args().skip(1).collect() + } else { + let mut stdin = stdin(); + let mut buf = vec![]; + stdin + .read_to_end(&mut buf) + .context("Failed to read stdin")?; + + let output = + String::from_utf8(buf).context("Failed to decode stdin as a utf8 string")?; + output + } + }; + + let quoted_text = text.replace('\'', "'\\''"); + + print!("'{}'", quoted_text); + + Ok(()) +} |