fix: alias expansion with arguments

This commit is contained in:
iff 2024-12-06 21:36:08 +01:00
parent 5840e748e6
commit e6ed9d617b
3 changed files with 37 additions and 15 deletions

View file

@ -8,9 +8,10 @@ use inquire::*;
pub fn suggestion(data: &mut Data) { pub fn suggestion(data: &mut Data) {
let shell = data.shell.clone(); let shell = data.shell.clone();
let last_command = data.command.clone(); let mut last_command ;
loop { loop {
last_command = data.command.clone();
let suggestion = { let suggestion = {
let command = suggest_command(data); let command = suggest_command(data);
if command.is_none() { if command.is_none() {
@ -71,13 +72,26 @@ pub fn cnf(data: &mut Data) {
if best_match.is_some() { if best_match.is_some() {
let best_match = best_match.unwrap(); let best_match = best_match.unwrap();
split_command[0] = best_match; split_command[0] = best_match;
let suggestion = split_command.join(" "); let suggest = split_command.join(" ");
data.update_suggest(&suggestion); data.update_suggest(&suggest);
data.expand_suggest(); data.expand_suggest();
let highlighted_suggestion = let highlighted_suggestion =
highlight_difference(&shell, &suggestion, &last_command).unwrap(); highlight_difference(&shell, &suggest, &last_command).unwrap();
let _ = suggestions::confirm_suggestion(data, &highlighted_suggestion); 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::<Vec<&str>>()
.join(" "),
);
data.update_error(msg);
}
suggestion(data);
} else { } else {
let package_manager = match system::get_package_manager(&shell) { let package_manager = match system::get_package_manager(&shell) {
Some(package_manager) => package_manager, Some(package_manager) => package_manager,

View file

@ -61,6 +61,8 @@ impl Data {
} }
let alias = self.alias.as_ref().unwrap(); let alias = self.alias.as_ref().unwrap();
if let Some(command) = expand_alias_multiline(alias, &self.command) { if let Some(command) = expand_alias_multiline(alias, &self.command) {
#[cfg(debug_assertions)]
eprintln!("expand_command: {}", command);
self.update_command(&command); self.update_command(&command);
} }
} }
@ -71,6 +73,8 @@ impl Data {
} }
let alias = self.alias.as_ref().unwrap(); let alias = self.alias.as_ref().unwrap();
if let Some(suggest) = expand_alias_multiline(alias, self.suggest.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); self.update_suggest(&suggest);
} }
} }
@ -109,9 +113,8 @@ impl Data {
} }
pub fn split_command(command: &str) -> Vec<String> { pub fn split_command(command: &str) -> Vec<String> {
if cfg!(debug_assertions) { #[cfg(debug_assertions)]
eprintln!("command: {command}") eprintln!("command: {command}");
}
// this regex splits the command separated by spaces, except when the space // this regex splits the command separated by spaces, except when the space
// is escaped by a backslash or surrounded by quotes // is escaped by a backslash or surrounded by quotes
let regex = r#"([^\s"'\\]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\\ )+|\\|\n"#; let regex = r#"([^\s"'\\]+|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\\ )+|\\|\n"#;
@ -120,6 +123,8 @@ pub fn split_command(command: &str) -> Vec<String> {
.find_iter(command) .find_iter(command)
.map(|cap| cap.as_str().to_owned()) .map(|cap| cap.as_str().to_owned())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
#[cfg(debug_assertions)]
eprintln!("split_command: {:?}", split_command);
split_command split_command
} }
@ -247,15 +252,16 @@ pub fn alias_map(shell: &str) -> Option<HashMap<String, String>> {
} }
pub fn expand_alias(map: &HashMap<String, String>, command: &str) -> Option<String> { pub fn expand_alias(map: &HashMap<String, String>, command: &str) -> Option<String> {
#[cfg(debug_assertions)] let (command, args) = if let Some(split) = command.split_once(' ') {
eprintln!("expand_alias: command: {}", command); (split.0, split.1)
let command = if let Some(split) = command.split_once(' ') {
split.0
} else { } else {
command (command, "")
}; };
map.get(command) if let Some(expand) = map.get(command) {
.map(|expand| command.replacen(command, expand, 1)) Some(format!("{} {}", expand, args))
} else {
None
}
} }
pub fn expand_alias_multiline(map: &HashMap<String, String>, command: &str) -> Option<String> { pub fn expand_alias_multiline(map: &HashMap<String, String>, command: &str) -> Option<String> {

View file

@ -252,6 +252,8 @@ pub fn confirm_suggestion(data: &Data, highlighted: &str) -> Result<(), String>
let shell = &data.shell; let shell = &data.shell;
let command = &data.suggest.clone().unwrap(); let command = &data.suggest.clone().unwrap();
#[cfg(debug_assertions)]
eprintln!("running command: {command}");
let now = Instant::now(); let now = Instant::now();
let process = run_suggestion(data, command); let process = run_suggestion(data, command);