mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 14:30:10 +01:00
feat: auto adapt suggestion to shell syntax
This commit is contained in:
parent
171b3d4607
commit
8f9aac6bdc
3 changed files with 47 additions and 20 deletions
|
|
@ -62,7 +62,10 @@ fn main() {
|
||||||
if command.is_none() {
|
if command.is_none() {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
command.unwrap()
|
|
||||||
|
let mut command = command.unwrap();
|
||||||
|
shell::shell_syntax(&shell, &mut command);
|
||||||
|
command
|
||||||
};
|
};
|
||||||
|
|
||||||
let highlighted_suggestion = {
|
let highlighted_suggestion = {
|
||||||
|
|
|
||||||
34
src/shell.rs
34
src/shell.rs
|
|
@ -246,3 +246,37 @@ end
|
||||||
|
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn shell_syntax(shell: &str, command: &mut String) {
|
||||||
|
#[allow(clippy::single_match)]
|
||||||
|
match shell {
|
||||||
|
"nushell" => {
|
||||||
|
*command = command.replace(" && ", " and ");
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn shell_evaluated_commands(shell: &str, command: &str) -> Option<String> {
|
||||||
|
let lines = command
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.trim().trim_end_matches(['\\', ';', '|', '&']))
|
||||||
|
.collect::<Vec<&str>>();
|
||||||
|
let mut dirs = Vec::new();
|
||||||
|
for line in lines {
|
||||||
|
if let Some(dir) = line.strip_prefix("cd ") {
|
||||||
|
dirs.push(dir.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let cd_dir = dirs.join("");
|
||||||
|
if cd_dir.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::single_match)]
|
||||||
|
match shell {
|
||||||
|
"nushell" => Some(cd_dir),
|
||||||
|
_ => Some(format!("cd {}", cd_dir)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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_multiline, PRIVILEGE_LIST};
|
use crate::shell::{expand_alias_multiline, shell_evaluated_commands, 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);
|
||||||
|
|
@ -237,8 +237,10 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
if process.success() {
|
if process.success() {
|
||||||
println!("{}", shell_evaluated_commands(&command));
|
let cd = shell_evaluated_commands(shell, &command);
|
||||||
return Ok(());
|
if let Some(cd) = cd {
|
||||||
|
println!("{}", cd);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if now.elapsed() > Duration::from_secs(3) {
|
if now.elapsed() > Duration::from_secs(3) {
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
@ -272,7 +274,10 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
if process.success() {
|
if process.success() {
|
||||||
println!("{}", shell_evaluated_commands(&command));
|
let cd = shell_evaluated_commands(shell, &command);
|
||||||
|
if let Some(cd) = cd {
|
||||||
|
println!("{}", cd);
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
if now.elapsed() > Duration::from_secs(3) {
|
if now.elapsed() > Duration::from_secs(3) {
|
||||||
|
|
@ -291,18 +296,3 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu
|
||||||
Err(error_msg.to_string())
|
Err(error_msg.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shell_evaluated_commands(command: &str) -> String {
|
|
||||||
let lines = command
|
|
||||||
.lines()
|
|
||||||
.map(|line| line.trim().trim_end_matches(['\\', ';', '|', '&']))
|
|
||||||
.collect::<Vec<&str>>();
|
|
||||||
let mut commands = Vec::new();
|
|
||||||
for line in lines {
|
|
||||||
if line.starts_with("cd ") {
|
|
||||||
commands.push(line.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
commands.join(" && \\ \n")
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue