From 11d2e3656f645d5ab5ebdc1b26e60b3eba02e321 Mon Sep 17 00:00:00 2001 From: iff Date: Sun, 29 Dec 2024 16:38:36 +0100 Subject: [PATCH] fix: missing privileged prefix --- core/src/shell.rs | 3 ++- core/src/style.rs | 23 ++++++++++++++++++----- core/src/suggestions.rs | 4 ++-- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/core/src/shell.rs b/core/src/shell.rs index c8f4d1f..9d6c4cb 100644 --- a/core/src/shell.rs +++ b/core/src/shell.rs @@ -155,7 +155,8 @@ impl Data { init.split(); init.update_error(None); - #[cfg(debug_assertions)] { + #[cfg(debug_assertions)] + { eprintln!("shell: {}", init.shell); eprintln!("command: {}", init.command); eprintln!("error: {}", init.error); diff --git a/core/src/style.rs b/core/src/style.rs index f4184dc..d5da808 100644 --- a/core/src/style.rs +++ b/core/src/style.rs @@ -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 { +pub fn highlight_difference(data: &Data, suggested_command: &str) -> Option { // 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")) diff --git a/core/src/suggestions.rs b/core/src/suggestions.rs index 141dba0..4bdb733 100644 --- a/core/src/suggestions.rs +++ b/core/src/suggestions.rs @@ -67,7 +67,7 @@ pub fn select_candidate(data: &mut Data) { eprintln!("candidates: {candidates:?}"); if candidates.len() == 1 { let suggestion = candidates[0].to_string(); - let highlighted = highlight_difference(&data.shell, &suggestion, &data.command).unwrap(); + let highlighted = highlight_difference(data, &suggestion).unwrap(); eprintln!("{}\n", highlighted); let confirm = format!("[{}]", t!("confirm-yes")).green(); eprintln!("{}: {} {}", t!("confirm"), confirm, "[Ctrl+C]".red()); @@ -77,7 +77,7 @@ pub fn select_candidate(data: &mut Data) { } else { let mut highlight_candidates = candidates .iter() - .map(|candidate| highlight_difference(&data.shell, candidate, &data.command).unwrap()) + .map(|candidate| highlight_difference(data, candidate).unwrap()) .collect::>(); for candidate in highlight_candidates.iter_mut() {