mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 06:20:09 +01:00
fix: don't rerun command when retrying
This commit is contained in:
parent
dc6eb12b73
commit
950b5f824d
2 changed files with 38 additions and 41 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::style::highlight_difference;
|
use crate::{shell::command_output, style::highlight_difference};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
|
|
@ -14,8 +14,9 @@ fn main() {
|
||||||
"No _PR_SHELL in environment. Did you aliased the binary with the correct arguments?",
|
"No _PR_SHELL in environment. Did you aliased the binary with the correct arguments?",
|
||||||
);
|
);
|
||||||
let mut last_command = shell::last_command_expanded_alias(&shell);
|
let mut last_command = shell::last_command_expanded_alias(&shell);
|
||||||
|
let mut error_msg = command_output(&shell, &last_command);
|
||||||
loop {
|
loop {
|
||||||
let corrected_command = suggestions::suggest_command(&shell, &last_command);
|
let corrected_command = suggestions::suggest_command(&shell, &last_command, &error_msg);
|
||||||
|
|
||||||
if let Some(corrected_command) = corrected_command {
|
if let Some(corrected_command) = corrected_command {
|
||||||
let command_difference =
|
let command_difference =
|
||||||
|
|
@ -33,6 +34,7 @@ fn main() {
|
||||||
format!("{}", "Looking for new suggestion...".cyan().bold());
|
format!("{}", "Looking for new suggestion...".cyan().bold());
|
||||||
println!("\n{}\n", retry_message);
|
println!("\n{}\n", retry_message);
|
||||||
last_command = corrected_command;
|
last_command = corrected_command;
|
||||||
|
error_msg = execution.err().unwrap();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
|
use std::process::Stdio;
|
||||||
|
|
||||||
use regex_lite::Regex;
|
use regex_lite::Regex;
|
||||||
|
|
||||||
use rule_parser::parse_rules;
|
use rule_parser::parse_rules;
|
||||||
|
|
||||||
use crate::files::{get_best_match_file, get_path_files};
|
use crate::files::{get_best_match_file, get_path_files};
|
||||||
use crate::shell::{command_output, PRIVILEGE_LIST};
|
use crate::shell::{PRIVILEGE_LIST};
|
||||||
|
|
||||||
pub fn suggest_command(shell: &str, last_command: &str) -> Option<String> {
|
|
||||||
let err = command_output(shell, last_command);
|
|
||||||
|
|
||||||
|
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);
|
||||||
let executable = match PRIVILEGE_LIST.contains(&split_command[0].as_str()) {
|
let executable = match PRIVILEGE_LIST.contains(&split_command[0].as_str()) {
|
||||||
true => split_command.get(1).expect("No command found.").as_str(),
|
true => split_command.get(1).expect("No command found.").as_str(),
|
||||||
|
|
@ -15,12 +15,12 @@ pub fn suggest_command(shell: &str, last_command: &str) -> Option<String> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if !PRIVILEGE_LIST.contains(&executable) {
|
if !PRIVILEGE_LIST.contains(&executable) {
|
||||||
let suggest = match_pattern("privilege", last_command, &err, shell);
|
let suggest = match_pattern("privilege", last_command, error_msg, shell);
|
||||||
if suggest.is_some() {
|
if suggest.is_some() {
|
||||||
return suggest;
|
return suggest;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let suggest = match_pattern(executable, last_command, &err, shell);
|
let suggest = match_pattern(executable, last_command, error_msg, shell);
|
||||||
if let Some(suggest) = suggest {
|
if let Some(suggest) = suggest {
|
||||||
if PRIVILEGE_LIST.contains(&executable) {
|
if PRIVILEGE_LIST.contains(&executable) {
|
||||||
return Some(format!("{} {}", split_command[0], suggest));
|
return Some(format!("{} {}", split_command[0], suggest));
|
||||||
|
|
@ -28,7 +28,7 @@ pub fn suggest_command(shell: &str, last_command: &str) -> Option<String> {
|
||||||
return Some(suggest);
|
return Some(suggest);
|
||||||
}
|
}
|
||||||
|
|
||||||
let suggest = match_pattern("general", last_command, &err, shell);
|
let suggest = match_pattern("general", last_command, error_msg, shell);
|
||||||
if let Some(suggest) = suggest {
|
if let Some(suggest) = suggest {
|
||||||
return Some(suggest);
|
return Some(suggest);
|
||||||
}
|
}
|
||||||
|
|
@ -46,28 +46,23 @@ fn match_pattern(
|
||||||
|
|
||||||
fn check_executable(shell: &str, executable: &str) -> bool {
|
fn check_executable(shell: &str, executable: &str) -> bool {
|
||||||
match shell {
|
match shell {
|
||||||
"nu" => {
|
"nu" => std::process::Command::new(shell)
|
||||||
std::process::Command::new(shell)
|
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(format!("if (which {} | is-empty) {{ exit 1 }}", executable))
|
.arg(format!("if (which {} | is-empty) {{ exit 1 }}", executable))
|
||||||
.output()
|
.output()
|
||||||
.expect("failed to execute process")
|
.expect("failed to execute process")
|
||||||
.status
|
.status
|
||||||
.success()
|
.success(),
|
||||||
}
|
_ => std::process::Command::new(shell)
|
||||||
_ => {
|
|
||||||
std::process::Command::new(shell)
|
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(format!("command -v {}", executable))
|
.arg(format!("command -v {}", executable))
|
||||||
.output()
|
.output()
|
||||||
.expect("failed to execute process")
|
.expect("failed to execute process")
|
||||||
.status
|
.status
|
||||||
.success()
|
.success(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
fn opt_regex(regex: &str, command: &mut String) -> String {
|
fn opt_regex(regex: &str, command: &mut String) -> String {
|
||||||
let regex = Regex::new(regex).unwrap();
|
let regex = Regex::new(regex).unwrap();
|
||||||
let opts = regex
|
let opts = regex
|
||||||
|
|
@ -190,7 +185,7 @@ fn compare_string(a: &str, b: &str) -> usize {
|
||||||
matrix[a.chars().count()][b.chars().count()]
|
matrix[a.chars().count()][b.chars().count()]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Result<(), ()> {
|
pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Result<(), String> {
|
||||||
println!("{}\n", highlighted);
|
println!("{}\n", highlighted);
|
||||||
println!("Press enter to execute the suggestion. Or press Ctrl+C to exit.");
|
println!("Press enter to execute the suggestion. Or press Ctrl+C to exit.");
|
||||||
std::io::stdin().read_line(&mut String::new()).unwrap();
|
std::io::stdin().read_line(&mut String::new()).unwrap();
|
||||||
|
|
@ -203,15 +198,15 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu
|
||||||
.arg(shell)
|
.arg(shell)
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(command)
|
.arg(command)
|
||||||
.spawn()
|
.stdout(Stdio::piped())
|
||||||
.expect("failed to execute process")
|
.output()
|
||||||
.wait()
|
.expect("failed to execute process");
|
||||||
.expect("failed to wait on process");
|
|
||||||
|
|
||||||
if process.success() {
|
if process.status.success() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else {
|
} else {
|
||||||
return Err(());
|
let error_msg = String::from_utf8_lossy(&process.stderr);
|
||||||
|
return Err(error_msg.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -219,14 +214,14 @@ pub fn confirm_suggestion(shell: &str, command: &str, highlighted: &str) -> Resu
|
||||||
let process = std::process::Command::new(shell)
|
let process = std::process::Command::new(shell)
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(command)
|
.arg(command)
|
||||||
.spawn()
|
.stdout(Stdio::piped())
|
||||||
.expect("failed to execute process")
|
.output()
|
||||||
.wait()
|
.expect("failed to execute process");
|
||||||
.expect("failed to wait on process");
|
|
||||||
|
|
||||||
if process.success() {
|
if process.status.success() {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(())
|
let error_msg = String::from_utf8_lossy(&process.stderr);
|
||||||
|
Err(error_msg.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue