about summary refs log tree commit diff stats
path: root/src/main.rs
blob: a6b047b3a2bbea693c3de364438ea2145ed457dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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(())
}