fix: missing privileged prefix

This commit is contained in:
iff 2024-12-29 16:38:36 +01:00
parent 96ac92414b
commit 11d2e3656f
3 changed files with 22 additions and 8 deletions

View file

@ -1,15 +1,14 @@
use crate::shell::Data;
use crate::shell::PRIVILEGE_LIST;
use colored::*;
use pay_respects_utils::evals::split_command;
// 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> {
pub fn highlight_difference(data: &Data, suggested_command: &str) -> Option<String> {
// let replaced_newline = suggested_command.replace('\n', r" {{newline}} ");
let shell = &data.shell;
let last_command = &data.command;
let mut split_suggested_command = split_command(suggested_command);
let split_last_command = split_command(last_command);
@ -60,6 +59,20 @@ pub fn highlight_difference(
split_suggested_command[len] =
split_suggested_command[len].clone() + "\"".red().bold().to_string().as_str();
}
if let Some(sudo) = data.privilege.clone() {
if suggested_command.contains("&&")
|| suggested_command.contains("||")
|| suggested_command.contains('>') {
split_suggested_command[0] = format!("{} -c \"", shell).blue().to_string()
+ &split_suggested_command[0];
let len = split_suggested_command.len() - 1;
split_suggested_command[len] =
split_suggested_command[len].clone() + "\"".blue().to_string().as_str();
}
split_suggested_command.insert(0, sudo.blue().to_string());
}
let highlighted = split_suggested_command.join(" ");
Some(highlighted.replace(" \n ", "\n"))