pay-respects/src/style.rs

36 lines
978 B
Rust
Raw Normal View History

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)]
2023-08-01 20:29:02 +02:00
pub fn highlight_difference(suggested_command: &str, last_command: &str) -> String {
let split_suggested_command = split_command(suggested_command);
2023-07-31 23:42:19 +02:00
let split_last_command = split_command(last_command);
2023-07-30 18:40:18 +02:00
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
}
let mut highlighted = suggested_command.to_string();
2023-08-01 20:52:17 +02:00
'next: for entry in &split_suggested_command {
for old in &old_entries {
if old == entry {
highlighted = highlighted.replace(entry, &entry.cyan().to_string());
continue 'next;
}
2023-07-30 18:40:18 +02:00
}
2023-08-01 20:52:17 +02:00
highlighted = highlighted.replace(entry, &entry.red().bold().to_string());
2023-07-30 18:40:18 +02:00
}
2023-08-01 20:29:02 +02:00
highlighted
2023-07-30 18:40:18 +02:00
}