Skip to content

Commit 1c5ff43

Browse files
committed
snice&skill: implement warnings
1 parent b6dc9c1 commit 1c5ff43

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

src/uu/skill/src/skill.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
5252
return Err(USimpleError::new(1, "no process selection criteria"));
5353
}
5454

55-
if settings.verbose {
56-
let output = construct_verbose_result(&pids, &results).trim().to_owned();
55+
let error_only = settings.warnings || !settings.verbose;
56+
if settings.verbose || settings.warnings {
57+
let output = construct_verbose_result(&pids, &results, error_only, take_action)
58+
.trim()
59+
.to_owned();
5760
println!("{output}");
5861
} else if !take_action {
5962
pids.iter().for_each(|pid| println!("{pid}"));

src/uu/snice/src/action.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl SelectedTarget {
9292
}
9393

9494
#[allow(unused)]
95-
#[derive(Debug, Clone)]
95+
#[derive(Debug, Clone, Eq, PartialEq)]
9696
pub enum ActionResult {
9797
PermissionDenied,
9898
Success,

src/uu/snice/src/process_matcher.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Settings {
1414
pub display: Option<SignalDisplay>,
1515
pub expressions: Option<Vec<SelectedTarget>>,
1616
pub verbose: bool,
17+
pub warnings: bool,
1718
pub no_action: bool,
1819
}
1920

@@ -31,6 +32,7 @@ impl Settings {
3132
display,
3233
expressions: Self::targets(matches),
3334
verbose: matches.get_flag("verbose"),
35+
warnings: matches.get_flag("warnings"),
3436
no_action: matches.get_flag("no-action"),
3537
})
3638
}
@@ -88,7 +90,7 @@ pub fn clap_args() -> Vec<Arg> {
8890
arg!(-L --table "list all signal names in a nice table"),
8991
arg!(-n --"no-action" "do not actually kill processes; just print what would happen"),
9092
arg!(-v --verbose "explain what is being done"),
91-
// arg!(-w --warnings "enable warnings (not implemented)"),
93+
arg!(-w --warnings "enable warnings (not implemented)"),
9294
// Expressions
9395
arg!(-c --command <command> ... "expression is a command name"),
9496
arg!(-p --pid <pid> ... "expression is a process id number")

src/uu/snice/src/snice.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
110110
return Err(USimpleError::new(1, "no process selection criteria"));
111111
}
112112

113-
if settings.verbose {
114-
let output = construct_verbose_result(&pids, &results).trim().to_owned();
113+
let error_only = settings.warnings || !settings.verbose;
114+
if settings.verbose || settings.warnings {
115+
let output = construct_verbose_result(&pids, &results, error_only, take_action)
116+
.trim()
117+
.to_owned();
115118
println!("{output}");
116119
} else if !take_action {
117120
pids.iter().for_each(|pid| println!("{pid}"));
@@ -122,14 +125,30 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
122125
}
123126

124127
#[allow(unused)]
125-
pub fn construct_verbose_result(pids: &[u32], action_results: &[Option<ActionResult>]) -> String {
128+
pub fn construct_verbose_result(
129+
pids: &[u32],
130+
action_results: &[Option<ActionResult>],
131+
error_only: bool,
132+
take_action: bool,
133+
) -> String {
126134
let mut table = action_results
127135
.iter()
128136
.enumerate()
129137
.map(|(index, it)| (pids[index], it))
130138
.filter(|(_, it)| it.is_some())
139+
.filter(|v| {
140+
!error_only
141+
|| !take_action
142+
|| v.1
143+
.clone()
144+
.is_some_and(|v| v == ActionResult::PermissionDenied)
145+
})
131146
.map(|(pid, action)| (pid, action.clone().unwrap()))
132147
.map(|(pid, action)| {
148+
if !take_action && action == ActionResult::Success {
149+
return (pid, None);
150+
}
151+
133152
let process = process_snapshot().process(Pid::from_u32(pid)).unwrap();
134153

135154
let tty =
@@ -148,10 +167,20 @@ pub fn construct_verbose_result(pids: &[u32], action_results: &[Option<ActionRes
148167
.unwrap_or("?".as_ref());
149168
let cmd = cmd.to_str().unwrap();
150169

151-
(tty, user, pid, cmd, action)
170+
(pid, Some((tty, user, cmd, action)))
171+
})
172+
.filter(|(_, v)| match v {
173+
None => true,
174+
Some((tty, _, _, _)) => tty.is_ok(),
175+
})
176+
.map(|(pid, v)| match v {
177+
None => {
178+
row![pid]
179+
}
180+
Some((tty, user, cmd, action)) => {
181+
row![tty.unwrap().tty(), user, pid, cmd, action]
182+
}
152183
})
153-
.filter(|(tty, _, _, _, _)| tty.is_ok())
154-
.map(|(tty, user, pid, cmd, action)| row![tty.unwrap().tty(), user, pid, cmd, action])
155184
.collect::<Table>();
156185

157186
table.set_format(*FORMAT_CLEAN);

0 commit comments

Comments
 (0)