diff --git a/src/uu/skill/src/skill.rs b/src/uu/skill/src/skill.rs index 602082a..48660e8 100644 --- a/src/uu/skill/src/skill.rs +++ b/src/uu/skill/src/skill.rs @@ -52,8 +52,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { return Err(USimpleError::new(1, "no process selection criteria")); } - if settings.verbose { - let output = construct_verbose_result(&pids, &results).trim().to_owned(); + let error_only = settings.warnings || !settings.verbose; + if settings.verbose || settings.warnings { + let output = construct_verbose_result(&pids, &results, error_only, take_action) + .trim() + .to_owned(); println!("{output}"); } else if !take_action { pids.iter().for_each(|pid| println!("{pid}")); diff --git a/src/uu/snice/src/action.rs b/src/uu/snice/src/action.rs index dff7bd3..80cbd3a 100644 --- a/src/uu/snice/src/action.rs +++ b/src/uu/snice/src/action.rs @@ -92,7 +92,7 @@ impl SelectedTarget { } #[allow(unused)] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Eq, PartialEq)] pub enum ActionResult { PermissionDenied, Success, diff --git a/src/uu/snice/src/process_matcher.rs b/src/uu/snice/src/process_matcher.rs index cc09b10..0768efd 100644 --- a/src/uu/snice/src/process_matcher.rs +++ b/src/uu/snice/src/process_matcher.rs @@ -14,6 +14,7 @@ pub struct Settings { pub display: Option, pub expressions: Option>, pub verbose: bool, + pub warnings: bool, pub no_action: bool, } @@ -31,6 +32,7 @@ impl Settings { display, expressions: Self::targets(matches), verbose: matches.get_flag("verbose"), + warnings: matches.get_flag("warnings"), no_action: matches.get_flag("no-action"), }) } @@ -88,7 +90,7 @@ pub fn clap_args() -> Vec { arg!(-L --table "list all signal names in a nice table"), arg!(-n --"no-action" "do not actually kill processes; just print what would happen"), arg!(-v --verbose "explain what is being done"), - // arg!(-w --warnings "enable warnings (not implemented)"), + arg!(-w --warnings "enable warnings (not implemented)"), // Expressions arg!(-c --command ... "expression is a command name"), arg!(-p --pid ... "expression is a process id number") diff --git a/src/uu/snice/src/snice.rs b/src/uu/snice/src/snice.rs index 74d5323..5754967 100644 --- a/src/uu/snice/src/snice.rs +++ b/src/uu/snice/src/snice.rs @@ -110,8 +110,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { return Err(USimpleError::new(1, "no process selection criteria")); } - if settings.verbose { - let output = construct_verbose_result(&pids, &results).trim().to_owned(); + let error_only = settings.warnings || !settings.verbose; + if settings.verbose || settings.warnings { + let output = construct_verbose_result(&pids, &results, error_only, take_action) + .trim() + .to_owned(); println!("{output}"); } else if !take_action { pids.iter().for_each(|pid| println!("{pid}")); @@ -122,14 +125,30 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } #[allow(unused)] -pub fn construct_verbose_result(pids: &[u32], action_results: &[Option]) -> String { +pub fn construct_verbose_result( + pids: &[u32], + action_results: &[Option], + error_only: bool, + take_action: bool, +) -> String { let mut table = action_results .iter() .enumerate() .map(|(index, it)| (pids[index], it)) .filter(|(_, it)| it.is_some()) + .filter(|v| { + !error_only + || !take_action + || v.1 + .clone() + .is_some_and(|v| v == ActionResult::PermissionDenied) + }) .map(|(pid, action)| (pid, action.clone().unwrap())) .map(|(pid, action)| { + if !take_action && action == ActionResult::Success { + return (pid, None); + } + let process = process_snapshot().process(Pid::from_u32(pid)).unwrap(); let tty = @@ -148,10 +167,20 @@ pub fn construct_verbose_result(pids: &[u32], action_results: &[Option true, + Some((tty, _, _, _)) => tty.is_ok(), + }) + .map(|(pid, v)| match v { + None => { + row![pid] + } + Some((tty, user, cmd, action)) => { + row![tty.unwrap().tty(), user, pid, cmd, action] + } }) - .filter(|(tty, _, _, _, _)| tty.is_ok()) - .map(|(tty, user, pid, cmd, action)| row![tty.unwrap().tty(), user, pid, cmd, action]) .collect::(); table.set_format(*FORMAT_CLEAN);