feat: expand alias for suggestion

This commit is contained in:
iff 2024-11-16 00:25:28 +01:00
parent 45de0ad8e3
commit 7b9cecfd8d
4 changed files with 22 additions and 16 deletions

View file

@ -15,5 +15,5 @@ pattern = [
]
suggest = [
'''
ls {{command[1:]}} --almost-all --color=auto --group-directories-first '''
ls {{command[1:]}}'''
]

View file

@ -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 = {

View file

@ -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::<Vec<&str>>();
let split_command = full_command.split_whitespace().collect::<Vec<&str>>();
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) {

View file

@ -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<String> {
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")
}