mirror of
https://github.com/uutils/procps.git
synced 2026-06-10 16:14:00 -07:00
snice&skill: implement warnings
This commit is contained in:
@@ -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}"));
|
||||
|
||||
@@ -92,7 +92,7 @@ impl SelectedTarget {
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
pub enum ActionResult {
|
||||
PermissionDenied,
|
||||
Success,
|
||||
|
||||
@@ -14,6 +14,7 @@ pub struct Settings {
|
||||
pub display: Option<SignalDisplay>,
|
||||
pub expressions: Option<Vec<SelectedTarget>>,
|
||||
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> {
|
||||
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 <command> ... "expression is a command name"),
|
||||
arg!(-p --pid <pid> ... "expression is a process id number")
|
||||
|
||||
@@ -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<ActionResult>]) -> String {
|
||||
pub fn construct_verbose_result(
|
||||
pids: &[u32],
|
||||
action_results: &[Option<ActionResult>],
|
||||
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<ActionRes
|
||||
.unwrap_or("?".as_ref());
|
||||
let cmd = cmd.to_str().unwrap();
|
||||
|
||||
(tty, user, pid, cmd, action)
|
||||
(pid, Some((tty, user, cmd, action)))
|
||||
})
|
||||
.filter(|(_, v)| match v {
|
||||
None => 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>();
|
||||
|
||||
table.set_format(*FORMAT_CLEAN);
|
||||
|
||||
Reference in New Issue
Block a user