From 7b9cecfd8d228d812a3cc26bfabf1c56b8ed4ad8 Mon Sep 17 00:00:00 2001 From: iff Date: Sat, 16 Nov 2024 00:25:28 +0100 Subject: [PATCH] feat: expand alias for suggestion --- rules/cat.toml | 2 +- src/main.rs | 3 ++- src/shell.rs | 23 +++++++++++++---------- src/suggestions.rs | 10 ++++++---- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/rules/cat.toml b/rules/cat.toml index eac15cb..441f0cc 100644 --- a/rules/cat.toml +++ b/rules/cat.toml @@ -15,5 +15,5 @@ pattern = [ ] suggest = [ ''' -ls {{command[1:]}} --almost-all --color=auto --group-directories-first ''' +ls {{command[1:]}}''' ] diff --git a/src/main.rs b/src/main.rs index 5c53432..545289c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,8 @@ fn main() { } }; - let mut last_command = shell::last_command_expanded_alias(&shell); + let mut last_command = shell::last_command(&shell); + last_command = shell::expand_alias(&shell, &last_command); let mut error_msg = command_output(&shell, &last_command); loop { let suggestion = { diff --git a/src/shell.rs b/src/shell.rs index 99bd82b..ee92b65 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -38,7 +38,7 @@ pub fn command_output(shell: &str, command: &str) -> String { } } -fn last_command(shell: &str) -> String { +pub fn last_command(shell: &str) -> String { let last_command = match std::env::var("_PR_LAST_COMMAND") { Ok(command) => command, Err(_) => { @@ -69,25 +69,24 @@ fn last_command(shell: &str) -> String { } } -pub fn last_command_expanded_alias(shell: &str) -> String { +pub fn expand_alias(shell: &str, full_command: &str) -> String { let alias = std::env::var("_PR_ALIAS").expect(&t!( "no-env-setup", var = "_PR_ALIAS", help = "pay-respects -h" )); - let last_command = last_command(shell); if alias.is_empty() { - return last_command; + return full_command.to_string(); } - let split_command = last_command.split_whitespace().collect::>(); + let split_command = full_command.split_whitespace().collect::>(); let command = if PRIVILEGE_LIST.contains(&split_command[0]) { split_command[1] } else { split_command[0] }; - let mut expanded_command = command.to_string(); + let mut expanded_command = Option::None; match shell { "bash" => { @@ -96,7 +95,7 @@ pub fn last_command_expanded_alias(shell: &str) -> String { let alias = line.replace(format!("alias {}='", command).as_str(), ""); let alias = alias.trim_end_matches('\'').trim_start_matches('\''); - expanded_command = alias.to_string(); + expanded_command = Some(alias.to_string()); } } } @@ -106,7 +105,7 @@ pub fn last_command_expanded_alias(shell: &str) -> String { let alias = line.replace(format!("{}=", command).as_str(), ""); let alias = alias.trim_start_matches('\'').trim_end_matches('\''); - expanded_command = alias.to_string(); + expanded_command = Some(alias.to_string()); } } } @@ -116,7 +115,7 @@ pub fn last_command_expanded_alias(shell: &str) -> String { let alias = line.replace(format!("alias {} ", command).as_str(), ""); let alias = alias.trim_start_matches('\'').trim_end_matches('\''); - expanded_command = alias.to_string(); + expanded_command = Some(alias.to_string()); } } } @@ -126,7 +125,11 @@ pub fn last_command_expanded_alias(shell: &str) -> String { } }; - last_command.replacen(command, &expanded_command, 1) + if expanded_command.is_none() { + full_command.to_string() + } else { + full_command.replacen(command, &expanded_command.unwrap(), 1) + } } pub fn initialization(shell: &str, binary_path: &str, auto_alias: &str) { diff --git a/src/suggestions.rs b/src/suggestions.rs index 91adbe7..e0184d2 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::PRIVILEGE_LIST; +use crate::shell::{expand_alias, PRIVILEGE_LIST}; pub fn suggest_command(shell: &str, last_command: &str, error_msg: &str) -> Option { let split_command = split_command(last_command); @@ -217,7 +217,8 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu if !command.starts_with(&_p) { continue; } - let command = command.replacen(&_p, "", 1); + let mut command = command.replacen(&_p, "", 1); + command = expand_alias(shell, &command); let now = Instant::now(); let process = std::process::Command::new(p) @@ -254,10 +255,11 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu } } + let command = expand_alias(shell, command); let now = Instant::now(); let process = std::process::Command::new(shell) .arg("-c") - .arg(command) + .arg(&command) // .stdout(Stdio::inherit()) .stdout(stderr().as_fd().try_clone_to_owned().unwrap()) .stderr(Stdio::inherit()) @@ -299,5 +301,5 @@ fn shell_evaluated_commands(command: &str) -> String { } } - commands.join(" && ") + commands.join(" && \\ \n") }