diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-05-04 12:54:23 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-05-04 12:54:23 -0400 |
commit | 8e7005ac347d0ef06af83e87874c824aa432da42 (patch) | |
tree | 08d9441a6a0bf3d2645e4d44f34799103e669bbd /src | |
parent | 99d8dcfb0cb3043e43473eeb8619968cd0da2aaf (diff) |
add 'zoxide add' and parsing zoxide errors
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/zoxide.rs | 38 | ||||
-rw-r--r-- | src/key_command/impl_appexecute.rs | 4 |
2 files changed, 34 insertions, 8 deletions
diff --git a/src/commands/zoxide.rs b/src/commands/zoxide.rs index 78709b0..dc7cec6 100644 --- a/src/commands/zoxide.rs +++ b/src/commands/zoxide.rs @@ -1,3 +1,4 @@ +use std::io; use std::path::Path; use std::process::{Command, Stdio}; @@ -6,7 +7,7 @@ use crate::context::AppContext; use crate::error::JoshutoResult; use crate::ui::TuiBackend; -pub fn zoxide(context: &mut AppContext, args: &str) -> JoshutoResult { +pub fn zoxide_query(context: &mut AppContext, args: &str) -> JoshutoResult { let cwd = std::env::current_dir()?; let zoxide_output = Command::new("zoxide") @@ -19,18 +20,26 @@ pub fn zoxide(context: &mut AppContext, args: &str) -> JoshutoResult { if zoxide_output.status.success() { if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) { - let zoxide_path = zoxide_str.trim(); + let zoxide_path = &zoxide_str[..zoxide_str.len() - 1]; + zoxide_add(zoxide_path)?; + let path = Path::new(zoxide_path); context .message_queue_mut() - .push_info(format!("z {}", zoxide_path)); + .push_info(format!("z {:?}", zoxide_path)); change_directory::change_directory(context, &path)?; } + } else { + if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stderr) { + context + .message_queue_mut() + .push_error(zoxide_str.to_string()); + } } Ok(()) } -pub fn zoxide_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { +pub fn zoxide_query_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { backend.terminal_drop(); let zoxide_process = Command::new("zoxide") @@ -38,6 +47,7 @@ pub fn zoxide_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> .arg("-i") .arg("--") .stdin(Stdio::piped()) + .stderr(Stdio::piped()) .stdout(Stdio::piped()) .spawn()?; let zoxide_output = zoxide_process.wait_with_output()?; @@ -46,13 +56,29 @@ pub fn zoxide_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> if zoxide_output.status.success() { if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) { - let zoxide_path = zoxide_str.trim(); + let zoxide_path = &zoxide_str[..zoxide_str.len() - 1]; + zoxide_add(zoxide_path)?; + let path = Path::new(zoxide_path); context .message_queue_mut() - .push_info(format!("z {}", zoxide_path)); + .push_info(format!("z {:?}", zoxide_path)); change_directory::change_directory(context, &path)?; } + } else { + if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stderr) { + context + .message_queue_mut() + .push_error(zoxide_str.to_string()); + } } Ok(()) } + +fn zoxide_add(s: &str) -> io::Result<()> { + Command::new("zoxide") + .arg("add") + .arg(s) + .output()?; + Ok(()) +} diff --git a/src/key_command/impl_appexecute.rs b/src/key_command/impl_appexecute.rs index e381770..0a9725c 100644 --- a/src/key_command/impl_appexecute.rs +++ b/src/key_command/impl_appexecute.rs @@ -106,8 +106,8 @@ impl AppExecute for Command { Self::SearchFzf => search_fzf::search_fzf(context, backend), Self::SubdirFzf => subdir_fzf::subdir_fzf(context, backend), - Self::Zoxide(arg) => zoxide::zoxide(context, &arg), - Self::ZoxideInteractive => zoxide::zoxide_interactive(context, backend), + Self::Zoxide(arg) => zoxide::zoxide_query(context, &arg), + Self::ZoxideInteractive => zoxide::zoxide_query_interactive(context, backend), } } } |