diff options
Diffstat (limited to 'src/poem/anthology/alias.rs')
-rw-r--r-- | src/poem/anthology/alias.rs | 56 |
1 files changed, 43 insertions, 13 deletions
diff --git a/src/poem/anthology/alias.rs b/src/poem/anthology/alias.rs index 45ee7f2..0746b59 100644 --- a/src/poem/anthology/alias.rs +++ b/src/poem/anthology/alias.rs @@ -1,6 +1,7 @@ -use crate::poem::Verse; use std::collections::BTreeMap; use std::collections::HashMap; +use std::os::unix::process::ExitStatusExt; +use std::process::{ExitStatus, Output}; /// alias /// @@ -11,8 +12,15 @@ use std::collections::HashMap; /// ```sh /// alias vim=nvim /// ``` -pub fn incant(verse: &Verse, out: &mut Vec<u8>, aliases: &mut HashMap<String, String>) -> i32 { - match verse.clause() { +pub fn incant( + clause: &Option<Vec<String>>, + uout: bool, + aliases: &mut HashMap<String, String>, +) -> Output { + let status = 0; + let mut out: Vec<u8> = Vec::new(); + let err: Vec<u8> = Vec::new(); + match clause { Some(clause) => { for stanza in clause { let (key, val) = match stanza.split_once("=") { @@ -24,7 +32,7 @@ pub fn incant(verse: &Verse, out: &mut Vec<u8>, aliases: &mut HashMap<String, St } None => { let mut lines = Vec::new(); - let sorted: BTreeMap<_, _> = aliases.into_iter().collect(); + let sorted: BTreeMap<_, _> = aliases.iter().collect(); for (key, val) in sorted { let line = if key.contains(' ') && val.contains(' ') { format!("'{}'='{}'", key, val) @@ -40,14 +48,19 @@ pub fn incant(verse: &Verse, out: &mut Vec<u8>, aliases: &mut HashMap<String, St lines.push(line); } - if verse.couplet > 0 { - *out = format!("{}\n", lines.join("\n")).as_bytes().to_vec(); + if uout { + out.append(&mut format!("{}\n", lines.join("\n")).as_bytes().to_vec()); } else { println!("{}", lines.join("\n")); } } } - 0 + + Output { + status: ExitStatus::from_raw(status), + stdout: out, + stderr: err, + } } /// unalias @@ -59,17 +72,34 @@ pub fn incant(verse: &Verse, out: &mut Vec<u8>, aliases: &mut HashMap<String, St /// ```sh /// unalias vim /// ``` -pub fn unincant(verse: &Verse, aliases: &mut HashMap<String, String>) -> i32 { - match verse.clause() { +pub fn unincant( + clause: &Option<Vec<String>>, + uerr: bool, + aliases: &mut HashMap<String, String>, +) -> Output { + let out: Vec<u8> = Vec::new(); + let mut err: Vec<u8> = Vec::new(); + + let status = match clause { Some(clause) => { for stanza in clause { - aliases.remove(&stanza); + aliases.remove(stanza); } + 0 } None => { - eprintln!("unalias: not enough arguments"); - return 1; + 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, } - 0 } |