format: add sh -c when displaying

This commit is contained in:
iff 2023-08-05 01:42:27 +02:00
parent fa040c4590
commit 9e17c0c68e
3 changed files with 27 additions and 10 deletions

View file

@ -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)

View file

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

View file

@ -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();