feat: multiline alias expansion

This commit is contained in:
iff 2024-11-17 14:57:22 +01:00
parent ae7307ad3a
commit f1f7ed8dbe
3 changed files with 29 additions and 21 deletions

View file

@ -117,12 +117,7 @@ pub fn runtime_match(
if pure_suggest.contains("{{command}}") { if pure_suggest.contains("{{command}}") {
pure_suggest = pure_suggest.replace("{{command}}", last_command); pure_suggest = pure_suggest.replace("{{command}}", last_command);
} }
return eval_suggest( return eval_suggest(&pure_suggest, last_command, error_msg, shell);
&pure_suggest,
last_command,
error_msg,
shell
);
} }
} }
} }
@ -150,12 +145,7 @@ fn eval_condition(
} }
} }
fn eval_suggest( fn eval_suggest(suggest: &str, last_command: &str, error_msg: &str, shell: &str) -> Option<String> {
suggest: &str,
last_command: &str,
error_msg: &str,
shell: &str,
) -> Option<String> {
let mut suggest = suggest.to_owned(); let mut suggest = suggest.to_owned();
if suggest.contains("{{command}}") { if suggest.contains("{{command}}") {
suggest = suggest.replace("{{command}}", "{last_command}"); suggest = suggest.replace("{{command}}", "{last_command}");

View file

@ -80,10 +80,10 @@ pub fn expand_alias(shell: &str, full_command: &str) -> String {
} }
let split_command = full_command.split_whitespace().collect::<Vec<&str>>(); let split_command = full_command.split_whitespace().collect::<Vec<&str>>();
let command = if PRIVILEGE_LIST.contains(&split_command[0]) { let (command, pure_command) = if PRIVILEGE_LIST.contains(&split_command[0]) {
split_command[1] (split_command[1], Some(split_command[1..].join(" ")))
} else { } else {
split_command[0] (split_command[0], None)
}; };
let mut expanded_command = Option::None; let mut expanded_command = Option::None;
@ -126,10 +126,28 @@ pub fn expand_alias(shell: &str, full_command: &str) -> String {
}; };
if expanded_command.is_none() { if expanded_command.is_none() {
full_command.to_string() return full_command.to_string();
} else { };
full_command.replacen(command, &expanded_command.unwrap(), 1)
let expanded_command = expanded_command.unwrap();
if pure_command.is_some() {
let pure_command = pure_command.unwrap();
if pure_command.starts_with(&expanded_command) {
return full_command.to_string();
}
} }
full_command.replacen(command, &expanded_command, 1)
}
pub fn expand_alias_multiline(shell: &str, full_command: &str) -> String {
let lines = full_command.lines().collect::<Vec<&str>>();
let mut expanded = String::new();
for line in lines {
expanded = format!("{}\n{}", expanded, expand_alias(shell, line));
}
expanded
} }
pub fn initialization(shell: &str, binary_path: &str, auto_alias: &str) { pub fn initialization(shell: &str, binary_path: &str, auto_alias: &str) {

View file

@ -8,7 +8,7 @@ use regex_lite::Regex;
use crate::files::{get_best_match_file, get_path_files}; use crate::files::{get_best_match_file, get_path_files};
use crate::rules::match_pattern; use crate::rules::match_pattern;
use crate::shell::{expand_alias, PRIVILEGE_LIST}; use crate::shell::{expand_alias_multiline, PRIVILEGE_LIST};
pub fn suggest_command(shell: &str, last_command: &str, error_msg: &str) -> Option<String> { pub fn suggest_command(shell: &str, last_command: &str, error_msg: &str) -> Option<String> {
let split_command = split_command(last_command); let split_command = split_command(last_command);
@ -221,7 +221,7 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu
continue; continue;
} }
let mut command = command.replacen(&_p, "", 1); let mut command = command.replacen(&_p, "", 1);
command = expand_alias(shell, &command); command = expand_alias_multiline(shell, &command);
let now = Instant::now(); let now = Instant::now();
let process = std::process::Command::new(p) let process = std::process::Command::new(p)
@ -258,7 +258,7 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu
} }
} }
let command = expand_alias(shell, command); let command = expand_alias_multiline(shell, command);
let now = Instant::now(); let now = Instant::now();
let process = std::process::Command::new(shell) let process = std::process::Command::new(shell)
.arg("-c") .arg("-c")