mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2026-02-02 07:35:10 +01:00
fix: check if the commands are similar when checking difference
This commit is contained in:
parent
22b6ad4694
commit
73ad099b34
3 changed files with 16 additions and 8 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
use crate::style::highlight_difference;
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
mod files;
|
mod files;
|
||||||
|
|
@ -16,8 +17,9 @@ fn main() {
|
||||||
let corrected_command = suggestions::suggest_command(&shell, &last_command);
|
let corrected_command = suggestions::suggest_command(&shell, &last_command);
|
||||||
|
|
||||||
if let Some(corrected_command) = corrected_command {
|
if let Some(corrected_command) = corrected_command {
|
||||||
if corrected_command != last_command {
|
let command_difference = highlight_difference(&shell, &corrected_command, &last_command);
|
||||||
suggestions::confirm_suggestion(&shell, &corrected_command, &last_command);
|
if let Some(command) = command_difference {
|
||||||
|
suggestions::confirm_suggestion(&shell, &command);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
src/style.rs
10
src/style.rs
|
|
@ -4,9 +4,15 @@ use colored::*;
|
||||||
|
|
||||||
// to_string() is necessary here, otherwise there won't be color in the output
|
// to_string() is necessary here, otherwise there won't be color in the output
|
||||||
#[warn(clippy::unnecessary_to_owned)]
|
#[warn(clippy::unnecessary_to_owned)]
|
||||||
pub fn highlight_difference(shell: &str, suggested_command: &str, last_command: &str) -> String {
|
pub fn highlight_difference(shell: &str, suggested_command: &str, last_command: &str) -> Option<String> {
|
||||||
let mut split_suggested_command = split_command(suggested_command);
|
let mut split_suggested_command = split_command(suggested_command);
|
||||||
let split_last_command = split_command(last_command);
|
let split_last_command = split_command(last_command);
|
||||||
|
println!("split_suggested_command: {:?}", split_suggested_command);
|
||||||
|
println!("split_last_command: {:?}", split_last_command);
|
||||||
|
|
||||||
|
if split_suggested_command == split_last_command {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
let privileged = PRIVILEGE_LIST.contains(&split_suggested_command[0].as_str());
|
let privileged = PRIVILEGE_LIST.contains(&split_suggested_command[0].as_str());
|
||||||
|
|
||||||
|
|
@ -50,5 +56,5 @@ pub fn highlight_difference(shell: &str, suggested_command: &str, last_command:
|
||||||
highlighted = split_suggested_command.join(" ");
|
highlighted = split_suggested_command.join(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
highlighted
|
Some(highlighted)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ use rule_parser::parse_rules;
|
||||||
|
|
||||||
use crate::files::{get_directory_files, get_path_files};
|
use crate::files::{get_directory_files, get_path_files};
|
||||||
use crate::shell::{command_output, PRIVILEGE_LIST};
|
use crate::shell::{command_output, PRIVILEGE_LIST};
|
||||||
use crate::style::highlight_difference;
|
|
||||||
|
|
||||||
pub fn suggest_command(shell: &str, last_command: &str) -> Option<String> {
|
pub fn suggest_command(shell: &str, last_command: &str) -> Option<String> {
|
||||||
let err = command_output(shell, last_command);
|
let err = command_output(shell, last_command);
|
||||||
|
|
@ -54,6 +53,7 @@ fn opt_regex(regex: &str, command: &mut String) -> String {
|
||||||
for opt in opts.clone() {
|
for opt in opts.clone() {
|
||||||
*command = command.replace(&opt, "");
|
*command = command.replace(&opt, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.join(" ")
|
opts.join(" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,7 +119,7 @@ fn suggest_typo(typo: &str, candidates: Vec<String>) -> String {
|
||||||
suggestion
|
suggestion
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_similar(typo: &str, candidates: Vec<String>) -> Option<String> {
|
pub fn find_similar(typo: &str, candidates: Vec<String>) -> Option<String> {
|
||||||
let mut min_distance = 10;
|
let mut min_distance = 10;
|
||||||
let mut min_distance_index = None;
|
let mut min_distance_index = None;
|
||||||
for (i, candidate) in candidates.iter().enumerate() {
|
for (i, candidate) in candidates.iter().enumerate() {
|
||||||
|
|
@ -158,8 +158,8 @@ 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, last_command: &str) {
|
pub fn confirm_suggestion(shell: &str, command: &str) {
|
||||||
println!("{}\n", highlight_difference(shell, command, last_command));
|
println!{"{}\n", command}
|
||||||
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();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue