snice&skill: implement warnings

This commit is contained in:
Bluemangoo
2025-08-14 09:32:35 +08:00
parent b6dc9c140f
commit 1c5ff43507
4 changed files with 44 additions and 10 deletions
+5 -2
View File
@@ -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}"));
+1 -1
View File
@@ -92,7 +92,7 @@ impl SelectedTarget {
}
#[allow(unused)]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ActionResult {
PermissionDenied,
Success,
+3 -1
View File
@@ -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")
+35 -6
View File
@@ -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);