diff options
author | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-05-03 15:21:22 -0400 |
---|---|---|
committer | Jeff Zhao <jeff.no.zhao@gmail.com> | 2022-05-03 15:26:44 -0400 |
commit | 7ad3eb1823556107cd412bb25d4f8e95931354e0 (patch) | |
tree | f2ae137a9b1aae2777d4d38181b36581993c6aca /src/commands | |
parent | 9cc4f10fdb3e31723289892be5929ccd0a1c48d7 (diff) |
add initial support for zoxide
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/mod.rs | 1 | ||||
-rw-r--r-- | src/commands/zoxide.rs | 58 |
2 files changed, 59 insertions, 0 deletions
diff --git a/src/commands/mod.rs b/src/commands/mod.rs index d01c3e3..f585bbc 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -27,3 +27,4 @@ pub mod sub_process; pub mod subdir_fzf; pub mod tab_ops; pub mod touch_file; +pub mod zoxide; diff --git a/src/commands/zoxide.rs b/src/commands/zoxide.rs new file mode 100644 index 0000000..78709b0 --- /dev/null +++ b/src/commands/zoxide.rs @@ -0,0 +1,58 @@ +use std::path::Path; +use std::process::{Command, Stdio}; + +use crate::commands::change_directory; +use crate::context::AppContext; +use crate::error::JoshutoResult; +use crate::ui::TuiBackend; + +pub fn zoxide(context: &mut AppContext, args: &str) -> JoshutoResult { + let cwd = std::env::current_dir()?; + + let zoxide_output = Command::new("zoxide") + .arg("query") + .arg("--exclude") + .arg(&cwd) + .arg("--") + .arg(args) + .output()?; + + if zoxide_output.status.success() { + if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) { + let zoxide_path = zoxide_str.trim(); + let path = Path::new(zoxide_path); + context + .message_queue_mut() + .push_info(format!("z {}", zoxide_path)); + change_directory::change_directory(context, &path)?; + } + } + Ok(()) +} + +pub fn zoxide_interactive(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult { + backend.terminal_drop(); + + let zoxide_process = Command::new("zoxide") + .arg("query") + .arg("-i") + .arg("--") + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn()?; + let zoxide_output = zoxide_process.wait_with_output()?; + + backend.terminal_restore()?; + + if zoxide_output.status.success() { + if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) { + let zoxide_path = zoxide_str.trim(); + let path = Path::new(zoxide_path); + context + .message_queue_mut() + .push_info(format!("z {}", zoxide_path)); + change_directory::change_directory(context, &path)?; + } + } + Ok(()) +} |