From 9e17c0c68ea2b1cf86b388a6046df305264aa4e3 Mon Sep 17 00:00:00 2001 From: iff Date: Sat, 5 Aug 2023 01:42:27 +0200 Subject: [PATCH] format: add sh -c when displaying --- README.md | 4 +--- src/style.rs | 31 +++++++++++++++++++++++++------ src/suggestions.rs | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c1ffaa6..5b2c1d3 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,7 @@ Typed a wrong command? Pay Respects will try to correct your wrong console comma - ✏️ **Easy to write rules**: You don't need to know Rust. The rules are written in a TOML file that is simple to work with! - 🪶 **Tiny binary size**: Not even 1MB! -![example-sudo-echo](img/example-sudo-echo.png) - -(This is not the correct syntax for shell, but the command is always executed as `sudo shell -c "echo ..."`, so it runs) +![example-sudo-echo-write](img/example-sudo-echo-write.png) ![example-typo-paru](img/example-typo-paru.png) diff --git a/src/style.rs b/src/style.rs index 43b1b48..c0ef7b7 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,12 +1,15 @@ +use crate::shell::PRIVILEGE_LIST; use crate::suggestions::split_command; use colored::*; // to_string() is necessary here, otherwise there won't be color in the output #[warn(clippy::unnecessary_to_owned)] -pub fn highlight_difference(suggested_command: &str, last_command: &str) -> String { - let split_suggested_command = split_command(suggested_command); +pub fn highlight_difference(shell: &str, suggested_command: &str, last_command: &str) -> String { + let mut split_suggested_command = split_command(suggested_command); let split_last_command = split_command(last_command); + let privileged = PRIVILEGE_LIST.contains(&split_suggested_command[0].as_str()); + let mut old_entries = Vec::new(); for command in &split_suggested_command { if command.is_empty() { @@ -20,15 +23,31 @@ pub fn highlight_difference(suggested_command: &str, last_command: &str) -> Stri } } - let mut highlighted = suggested_command.to_string(); - 'next: for entry in &split_suggested_command { + // let mut highlighted = suggested_command.to_string(); + 'next: for entry in split_suggested_command.iter_mut() { for old in &old_entries { if old == entry { - highlighted = highlighted.replace(entry, &entry.cyan().to_string()); + *entry = entry.cyan().to_string(); continue 'next; } } - highlighted = highlighted.replace(entry, &entry.red().bold().to_string()); + *entry = entry.red().bold().to_string(); + } + + let highlighted; + if privileged + && (suggested_command.contains("&&") + || suggested_command.contains("||") + || suggested_command.contains('>')) + { + split_suggested_command[1] = + format!("{} -c \"", shell).red().bold().to_string() + &split_suggested_command[1]; + let len = split_suggested_command.len() - 1; + split_suggested_command[len] = + split_suggested_command[len].clone() + "\"".red().bold().to_string().as_str(); + highlighted = split_suggested_command.join(" "); + } else { + highlighted = split_suggested_command.join(" "); } highlighted diff --git a/src/suggestions.rs b/src/suggestions.rs index b7946de..6e13fc8 100644 --- a/src/suggestions.rs +++ b/src/suggestions.rs @@ -157,7 +157,7 @@ fn compare_string(a: &str, b: &str) -> usize { } pub fn confirm_suggestion(shell: &str, command: &str, last_command: &str) { - println!("{}\n", highlight_difference(command, last_command)); + println!("{}\n", highlight_difference(shell, command, last_command)); println!("Press enter to execute the suggestion. Or press Ctrl+C to exit."); std::io::stdin().read_line(&mut String::new()).unwrap();