use std::collections::BTreeMap; use std::collections::HashMap; use std::os::unix::process::ExitStatusExt; use std::process::{ExitStatus, Output}; /// alias /// /// The builtin `alias` command. Used to monikers for other verbs, or /// entire verses. /// /// # Shell Example /// ```sh /// alias vim=nvim /// ``` pub fn incant( clause: &Option>, uout: bool, aliases: &mut HashMap, ) -> Output { let status = 0; let mut out: Vec = Vec::new(); let err: Vec = Vec::new(); match clause { Some(clause) => { for stanza in clause { let (key, val) = match stanza.split_once("=") { Some((key, val)) => (key, val), None => continue, }; aliases.insert(String::from(key), String::from(val)); } } None => { let mut lines = Vec::new(); let sorted: BTreeMap<_, _> = aliases.iter().collect(); for (key, val) in sorted { let line = if key.contains(' ') && val.contains(' ') { format!("'{}'='{}'", key, val) } else if key.contains(' ') { format!("'{}'={}", key, val) } else if val.contains(' ') { format!("{}='{}'", key, val) } else if val.is_empty() { format!("{}=''", key) } else { format!("{}={}", key, val) }; lines.push(line); } if uout { out.append(&mut format!("{}\n", lines.join("\n")).as_bytes().to_vec()); } else { println!("{}", lines.join("\n")); } } } Output { status: ExitStatus::from_raw(status), stdout: out, stderr: err, } } /// unalias /// /// The builtin `unalias` command. Used to remove shell monikers, since `alias` /// may be called with an empty string as the value. /// /// # Shell Example /// ```sh /// unalias vim /// ``` pub fn unincant( clause: &Option>, uerr: bool, aliases: &mut HashMap, ) -> Output { let out: Vec = Vec::new(); let mut err: Vec = Vec::new(); let status = match clause { Some(clause) => { for stanza in clause { aliases.remove(stanza); } 0 } None => { if uerr { err.append(&mut "unalias: not enough arguments".as_bytes().to_vec()); } else { eprintln!("unalias: not enough arguments"); } 1 } }; Output { status: ExitStatus::from_raw(status), stdout: out, stderr: err, } }