mirror of
https://github.com/TECHNOFAB11/pay-respects.git
synced 2025-12-12 06:20:09 +01:00
format: add sh -c when displaying
This commit is contained in:
parent
fa040c4590
commit
9e17c0c68e
3 changed files with 27 additions and 10 deletions
|
|
@ -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!
|
||||
|
||||

|
||||
|
||||
(This is not the correct syntax for shell, but the command is always executed as `sudo shell -c "echo ..."`, so it runs)
|
||||

|
||||
|
||||

|
||||
|
||||
|
|
|
|||
31
src/style.rs
31
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
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue