diff options
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/change_directory.rs | 6 | ||||
-rw-r--r-- | src/commands/command_line.rs | 10 | ||||
-rw-r--r-- | src/commands/zoxide.rs | 33 |
3 files changed, 42 insertions, 7 deletions
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index ebf471f..84d8fdb 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -1,6 +1,6 @@ use std::path; -use crate::commands::reload; +use crate::commands::{reload, zoxide}; use crate::context::AppContext; use crate::error::AppResult; use crate::history::{generate_entries_to_root, DirectoryHistory}; @@ -10,6 +10,10 @@ use crate::util::cwd; pub fn cd(path: &path::Path, context: &mut AppContext) -> std::io::Result<()> { cwd::set_current_dir(path)?; context.tab_context_mut().curr_tab_mut().set_cwd(path); + if context.config_ref().zoxide_update { + debug_assert!(path.is_absolute()); + zoxide::zoxide_add(path.to_str().expect("cannot convert path to string"))?; + } Ok(()) } diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs index d423835..5f9d0de 100644 --- a/src/commands/command_line.rs +++ b/src/commands/command_line.rs @@ -22,12 +22,20 @@ pub fn read_and_execute( .suffix(suffix) .get_input(backend, context, &mut listener); - if let Some(s) = user_input { + if let Some(mut s) = user_input { let mut trimmed = s.trim_start(); let _ = context.commandline_context_mut().history_mut().add(trimmed); + let (command, arg) = match trimmed.find(' ') { + Some(i) => (&trimmed[..i], &trimmed[i..]), + None => (trimmed, ""), + }; + if let Some(alias) = context.config_ref().cmd_aliases.get(trimmed) { trimmed = alias; + } else if let Some(alias) = context.config_ref().cmd_aliases.get(command) { + s.replace_range(..s.len() - arg.len(), alias); + trimmed = &s; } let command = Command::from_str(trimmed)?; diff --git a/src/commands/zoxide.rs b/src/commands/zoxide.rs index 222bb9d..4ebddff 100644 --- a/src/commands/zoxide.rs +++ b/src/commands/zoxide.rs @@ -10,18 +10,34 @@ use crate::ui::AppBackend; pub fn zoxide_query(context: &mut AppContext, args: &str) -> AppResult { let cwd = std::env::current_dir()?; + let path = Path::new(args); + if change_directory::change_directory(context, path).is_ok() { + if !context.config_ref().zoxide_update { + let cwd = context + .tab_context_ref() + .curr_tab_ref() + .cwd() + .to_str() + .expect("path cannot be converted to string"); + zoxide_add(cwd)?; + } + return Ok(()); + } + let zoxide_output = Command::new("zoxide") .arg("query") .arg("--exclude") .arg(&cwd) .arg("--") - .args(args.split(' ').collect::<Vec<&str>>()) + .args(args.split(' ')) .output()?; if zoxide_output.status.success() { if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) { let zoxide_path = &zoxide_str[..zoxide_str.len() - 1]; - zoxide_add(zoxide_path)?; + if !context.config_ref().zoxide_update { + zoxide_add(zoxide_path)?; + } let path = Path::new(zoxide_path); context @@ -37,13 +53,18 @@ pub fn zoxide_query(context: &mut AppContext, args: &str) -> AppResult { Ok(()) } -pub fn zoxide_query_interactive(context: &mut AppContext, backend: &mut AppBackend) -> AppResult { +pub fn zoxide_query_interactive( + context: &mut AppContext, + backend: &mut AppBackend, + args: &str, +) -> AppResult { backend.terminal_drop(); let zoxide_process = Command::new("zoxide") .arg("query") .arg("-i") .arg("--") + .args(args.split(' ')) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn()?; @@ -54,7 +75,9 @@ pub fn zoxide_query_interactive(context: &mut AppContext, backend: &mut AppBacke if zoxide_output.status.success() { if let Ok(zoxide_str) = std::str::from_utf8(&zoxide_output.stdout) { let zoxide_path = &zoxide_str[..zoxide_str.len() - 1]; - zoxide_add(zoxide_path)?; + if !context.config_ref().zoxide_update { + zoxide_add(zoxide_path)?; + } let path = Path::new(zoxide_path); context @@ -70,7 +93,7 @@ pub fn zoxide_query_interactive(context: &mut AppContext, backend: &mut AppBacke Ok(()) } -fn zoxide_add(s: &str) -> io::Result<()> { +pub fn zoxide_add(s: &str) -> io::Result<()> { Command::new("zoxide").arg("add").arg(s).output()?; Ok(()) } |