pay-respects/src/style.rs

63 lines
1.8 KiB
Rust
Raw Normal View History

2023-08-05 01:42:27 +02:00
use crate::shell::PRIVILEGE_LIST;
2023-08-01 20:29:02 +02:00
use crate::suggestions::split_command;
2023-07-31 23:48:14 +02:00
use colored::*;
2023-07-30 18:40:18 +02:00
2023-08-03 21:26:41 +02:00
// to_string() is necessary here, otherwise there won't be color in the output
#[warn(clippy::unnecessary_to_owned)]
pub fn highlight_difference(shell: &str, suggested_command: &str, last_command: &str) -> Option<String> {
let replaced_newline = suggested_command.replace("\n", r" {{newline}}");
let mut split_suggested_command = split_command(&replaced_newline);
2023-07-31 23:42:19 +02:00
let split_last_command = split_command(last_command);
if split_suggested_command == split_last_command {
return None;
}
2023-07-30 18:40:18 +02:00
2023-08-05 01:42:27 +02:00
let privileged = PRIVILEGE_LIST.contains(&split_suggested_command[0].as_str());
2023-08-01 20:29:02 +02:00
let mut old_entries = Vec::new();
for command in &split_suggested_command {
if command.is_empty() {
continue;
}
2023-08-01 20:52:17 +02:00
for old in split_last_command.clone() {
2023-08-01 20:29:02 +02:00
if command == &old {
old_entries.push(command.clone());
2023-08-01 20:52:17 +02:00
break;
2023-07-30 18:40:18 +02:00
}
}
2023-08-01 20:29:02 +02:00
}
2023-08-05 01:42:27 +02:00
// let mut highlighted = suggested_command.to_string();
'next: for entry in split_suggested_command.iter_mut() {
if entry == r"{{newline}" {
continue
}
2023-08-01 20:52:17 +02:00
for old in &old_entries {
if old == entry {
2023-08-07 19:58:19 +02:00
*entry = entry.blue().to_string();
2023-08-01 20:52:17 +02:00
continue 'next;
}
2023-07-30 18:40:18 +02:00
}
2023-08-05 01:42:27 +02:00
*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(" ");
2023-07-30 18:40:18 +02:00
}
Some(highlighted.replace(r"{{newline}}", "\n"))
2023-07-30 18:40:18 +02:00
}