use std::io::Write; use clap::Parser; use aho_corasick::AhoCorasick; /// Utility command to output information #[derive(Parser, Debug)] #[command(version, about, long_about = None)] struct Args { /// Removes the \n from the end of the output string #[arg(short, long)] noendl: bool, /// Parse escapes in the given string #[arg(short, long)] escape: bool, /// String to be output #[arg()] data: Option, } fn main() { let args = Args::parse(); let mut data = match args.data { Some(s) => s, None => String::new() }; let mut noendl = args.noendl; let patterns = &["\\\\", "\\a", "\\b", "\\e", "\\f", "\\n", "\\r", "\\t", "\\v"]; let replacements = &["\\", "\x07", "\x08", "\x1B", "\x0C", "\n", "\r", "\t", "\x0B"]; if args.escape { let ac = AhoCorasick::new(patterns).unwrap(); data = ac.replace_all(data.as_str(), replacements); let idx = data.find("\\c"); if let Some(n) = idx { data = (&data[0..n]).to_string(); noendl = true; } } if !noendl { data.push('\n'); } let mut out = std::io::stdout(); match out.write_all(data.as_bytes()) { Err(e) => { eprintln!("Failed to write to stdout: {e}"); std::process::exit(1); }, Ok(()) => () }; }