aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs39
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(())
+}