From e6ed9d617b42df6ce2a509670a99a27508ea8de4 Mon Sep 17 00:00:00 2001 From: iff Date: Fri, 6 Dec 2024 21:36:08 +0100 Subject: [PATCH] fix: alias expansion with arguments --- src/modes.rs | 24 +++++++++++++++++++----- src/shell.rs | 26 ++++++++++++++++---------- src/suggestions.rs | 2 ++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/modes.rs b/src/modes.rs index eaca182..da483e3 100644 --- a/src/modes.rs +++ b/src/modes.rs @@ -8,9 +8,10 @@ use inquire::*; pub fn suggestion(data: &mut Data) { let shell = data.shell.clone(); - let last_command = data.command.clone(); + let mut last_command ; loop { + last_command = data.command.clone(); let suggestion = { let command = suggest_command(data); if command.is_none() { @@ -71,13 +72,26 @@ pub fn cnf(data: &mut Data) { if best_match.is_some() { let best_match = best_match.unwrap(); split_command[0] = best_match; - let suggestion = split_command.join(" "); - data.update_suggest(&suggestion); + let suggest = split_command.join(" "); + data.update_suggest(&suggest); data.expand_suggest(); let highlighted_suggestion = - highlight_difference(&shell, &suggestion, &last_command).unwrap(); - let _ = suggestions::confirm_suggestion(data, &highlighted_suggestion); + highlight_difference(&shell, &suggest, &last_command).unwrap(); + let status = suggestions::confirm_suggestion(data, &highlighted_suggestion); + if status.is_err() { + data.update_command(&suggest); + let msg = Some( + status + .err() + .unwrap() + .split_whitespace() + .collect::>() + .join(" "), + ); + data.update_error(msg); + } + suggestion(data); } else { let package_manager = match system::get_package_manager(&shell) { Some(package_manager) => package_manager, diff --git a/src/shell.rs b/src/shell.rs index 9b42232..a9a29f0 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -61,6 +61,8 @@ impl Data { } let alias = self.alias.as_ref().unwrap(); if let Some(command) = expand_alias_multiline(alias, &self.command) { + #[cfg(debug_assertions)] + eprintln!("expand_command: {}", command); self.update_command(&command); } } @@ -71,6 +73,8 @@ impl Data { } let alias = self.alias.as_ref().unwrap(); if let Some(suggest) = expand_alias_multiline(alias, self.suggest.as_ref().unwrap()) { + #[cfg(debug_assertions)] + eprintln!("expand_suggest: {}", suggest); self.update_suggest(&suggest); } } @@ -109,9 +113,8 @@ impl Data { } pub fn split_command(command: &str) -> Vec { - if cfg!(debug_assertions) { - eprintln!("command: {command}") - } + #[cfg(debug_assertions)] + eprintln!("command: {command}"); // this regex splits the command separated by spaces, except when the space // is escaped by a backslash or surrounded by quotes let regex = r#"([^\s"'\\]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\\ )+|\\|\n"#; @@ -120,6 +123,8 @@ pub fn split_command(command: &str) -> Vec { .find_iter(command) .map(|cap| cap.as_str().to_owned()) .collect::>(); + #[cfg(debug_assertions)] + eprintln!("split_command: {:?}", split_command); split_command } @@ -247,15 +252,16 @@ pub fn alias_map(shell: &str) -> Option> { } pub fn expand_alias(map: &HashMap, command: &str) -> Option { - #[cfg(debug_assertions)] - eprintln!("expand_alias: command: {}", command); - let command = if let Some(split) = command.split_once(' ') { - split.0 + let (command, args) = if let Some(split) = command.split_once(' ') { + (split.0, split.1) } else { - command + (command, "") }; - map.get(command) - .map(|expand| command.replacen(command, expand, 1)) + if let Some(expand) = map.get(command) { + Some(format!("{} {}", expand, args)) + } else { + None + } } pub fn expand_alias_multiline(map: &HashMap, command: &str) -> Option { diff --git a/src/suggestions.rs b/src/suggestions.rs index caf10b7..f7c44bd 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -252,6 +252,8 @@ pub fn confirm_suggestion(data: &Data, highlighted: &str) -> Result<(), String> let shell = &data.shell; let command = &data.suggest.clone().unwrap(); + #[cfg(debug_assertions)] + eprintln!("running command: {command}"); let now = Instant::now(); let process = run_suggestion(data, command);