From f1f7ed8dbecab8dbd9bab728995e065c0957b08d Mon Sep 17 00:00:00 2001 From: iff Date: Sun, 17 Nov 2024 14:57:22 +0100 Subject: [PATCH] feat: multiline alias expansion --- src/runtime_rules.rs | 14 ++------------ src/shell.rs | 30 ++++++++++++++++++++++++------ src/suggestions.rs | 6 +++--- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/runtime_rules.rs b/src/runtime_rules.rs index 1b1c5db..42e88cb 100644 --- a/src/runtime_rules.rs +++ b/src/runtime_rules.rs @@ -117,12 +117,7 @@ pub fn runtime_match( if pure_suggest.contains("{{command}}") { pure_suggest = pure_suggest.replace("{{command}}", last_command); } - return eval_suggest( - &pure_suggest, - last_command, - error_msg, - shell - ); + return eval_suggest(&pure_suggest, last_command, error_msg, shell); } } } @@ -150,12 +145,7 @@ fn eval_condition( } } -fn eval_suggest( - suggest: &str, - last_command: &str, - error_msg: &str, - shell: &str, -) -> Option { +fn eval_suggest(suggest: &str, last_command: &str, error_msg: &str, shell: &str) -> Option { let mut suggest = suggest.to_owned(); if suggest.contains("{{command}}") { suggest = suggest.replace("{{command}}", "{last_command}"); diff --git a/src/shell.rs b/src/shell.rs index ee92b65..81de39e 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -80,10 +80,10 @@ pub fn expand_alias(shell: &str, full_command: &str) -> String { } let split_command = full_command.split_whitespace().collect::>(); - let command = if PRIVILEGE_LIST.contains(&split_command[0]) { - split_command[1] + let (command, pure_command) = if PRIVILEGE_LIST.contains(&split_command[0]) { + (split_command[1], Some(split_command[1..].join(" "))) } else { - split_command[0] + (split_command[0], 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() { - full_command.to_string() - } else { - full_command.replacen(command, &expanded_command.unwrap(), 1) + return full_command.to_string(); + }; + + 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::>(); + 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) { diff --git a/src/suggestions.rs b/src/suggestions.rs index 36467e6..8acb300 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -8,7 +8,7 @@ use regex_lite::Regex; use crate::files::{get_best_match_file, get_path_files}; 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 { let split_command = split_command(last_command); @@ -221,7 +221,7 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu continue; } let mut command = command.replacen(&_p, "", 1); - command = expand_alias(shell, &command); + command = expand_alias_multiline(shell, &command); let now = Instant::now(); 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 process = std::process::Command::new(shell) .arg("-c")