diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-25 15:47:15 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-25 15:48:39 -0500 |
commit | 1ffe773d50eee1a6466d7c4f060b9c0fbde78755 (patch) | |
tree | 3e9cfcf2d9ac318597680673c27aa8a415251a55 | |
parent | a875058500cc93f4d18bd5481d94987232bfbc2a (diff) | |
download | nbsh-1ffe773d50eee1a6466d7c4f060b9c0fbde78755.tar.gz nbsh-1ffe773d50eee1a6466d7c4f060b9c0fbde78755.zip |
more refactoring
-rw-r--r-- | src/state/history/builtins.rs | 75 | ||||
-rw-r--r-- | src/state/history/mod.rs | 5 |
2 files changed, 40 insertions, 40 deletions
diff --git a/src/state/history/builtins.rs b/src/state/history/builtins.rs index e8f87da..2b2084b 100644 --- a/src/state/history/builtins.rs +++ b/src/state/history/builtins.rs @@ -1,47 +1,48 @@ -pub fn is(exe: &str) -> bool { - matches!(exe, "cd") -} +use std::os::unix::process::ExitStatusExt as _; -pub fn run<'a>(exe: &str, args: impl IntoIterator<Item = &'a str>) -> u8 { - match exe { - "cd" => impls::cd( - args.into_iter() - .map(std::convert::AsRef::as_ref) - .next() - .unwrap_or(""), - ), - _ => unreachable!(), +pub fn run( + exe: &crate::parse::Exe, +) -> Option<async_std::process::ExitStatus> { + match exe.exe() { + "cd" => Some(cd(exe)), + _ => None, } } -mod impls { - pub fn cd(dir: &str) -> u8 { - let dir = if dir.is_empty() { - home() - } else if dir.starts_with('~') { - let path: std::path::PathBuf = dir.into(); - if let std::path::Component::Normal(prefix) = - path.components().next().unwrap() - { - if prefix.to_str() == Some("~") { - home().join(path.strip_prefix(prefix).unwrap()) - } else { - // TODO - return 1; - } +fn cd(exe: &crate::parse::Exe) -> async_std::process::ExitStatus { + let dir = exe + .args() + .into_iter() + .map(std::convert::AsRef::as_ref) + .next() + .unwrap_or(""); + + let dir = if dir.is_empty() { + home() + } else if dir.starts_with('~') { + let path: std::path::PathBuf = dir.into(); + if let std::path::Component::Normal(prefix) = + path.components().next().unwrap() + { + if prefix.to_str() == Some("~") { + home().join(path.strip_prefix(prefix).unwrap()) } else { - unreachable!() + // TODO + return async_std::process::ExitStatus::from_raw(1 << 8); } } else { - dir.into() - }; - match std::env::set_current_dir(dir) { - Ok(()) => 0, - Err(_) => 1, + unreachable!() } - } + } else { + dir.into() + }; + let code = match std::env::set_current_dir(dir) { + Ok(()) => 0, + Err(_) => 1, + }; + async_std::process::ExitStatus::from_raw(code << 8) +} - fn home() -> std::path::PathBuf { - std::env::var_os("HOME").unwrap().into() - } +fn home() -> std::path::PathBuf { + std::env::var_os("HOME").unwrap().into() } diff --git a/src/state/history/mod.rs b/src/state/history/mod.rs index acdbb44..6ab8bbd 100644 --- a/src/state/history/mod.rs +++ b/src/state/history/mod.rs @@ -604,9 +604,8 @@ async fn run_exe( resize_r: async_std::channel::Receiver<(u16, u16)>, event_w: async_std::channel::Sender<crate::event::Event>, ) -> async_std::process::ExitStatus { - if builtins::is(exe.exe()) { - let code: i32 = builtins::run(exe.exe(), exe.args()).into(); - return async_std::process::ExitStatus::from_raw(code << 8); + if let Some(status) = builtins::run(exe) { + return status; } let mut process = async_std::process::Command::new(exe.exe()); |