Skip to content

Commit ae9c723

Browse files
committed
top: tui impl R
1 parent 55c361b commit ae9c723

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

src/uu/top/src/top.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,6 @@ fn selected_fields() -> Vec<String> {
200200

201201
fn collect(settings: &Settings, fields: &[String], tui_stat: &TuiStat) -> Vec<Vec<String>> {
202202
let pickers = pickers(fields);
203-
let sorter_nth = fields
204-
.iter()
205-
.position(|f| f == &tui_stat.sorter)
206-
.unwrap_or(0);
207203

208204
let pids = sysinfo()
209205
.read()
@@ -225,7 +221,18 @@ fn collect(settings: &Settings, fields: &[String], tui_stat: &TuiStat) -> Vec<Ve
225221
.collect::<Vec<_>>()
226222
})
227223
.collect::<Vec<Vec<Box<dyn Column>>>>();
228-
collected.sort_by(|a, b| a[sorter_nth].cmp_dyn(&*b[sorter_nth]));
224+
225+
let sorter = if tui_stat.sort_by_pid {
226+
"PID"
227+
} else {
228+
&tui_stat.sorter
229+
};
230+
let sorter_nth = fields.iter().position(|f| f == sorter).unwrap_or(0);
231+
if tui_stat.sort_by_pid {
232+
collected.sort_by(|a, b| a[sorter_nth].cmp_dyn(&*b[sorter_nth])); // reverse
233+
} else {
234+
collected.sort_by(|a, b| b[sorter_nth].cmp_dyn(&*a[sorter_nth]));
235+
}
229236
collected
230237
.into_iter()
231238
.map(|it| it.into_iter().map(|c| c.as_string()).collect())

src/uu/top/src/tui/input.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ pub fn handle_input(
7575
stat.memory_graph_mode = stat.memory_graph_mode.next();
7676
should_update.store(true, Ordering::Relaxed);
7777
}
78+
char!('R') => {
79+
{
80+
let mut stat = tui_stat.write().unwrap();
81+
stat.sort_by_pid = !stat.sort_by_pid;
82+
}
83+
84+
data.write().unwrap().1 = ProcList::new(settings, &tui_stat.read().unwrap());
85+
should_update.store(true, Ordering::Relaxed);
86+
}
7887
char!('t') => {
7988
let mut stat = tui_stat.write().unwrap();
8089
stat.cpu_graph_mode = stat.cpu_graph_mode.next();

src/uu/top/src/tui/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,16 @@ impl<'a> Tui<'a> {
359359
let colorful = self.stat.colorful;
360360
let highlight_sorted = self.stat.highlight_sorted;
361361
let highlight_bold = self.stat.highlight_bold;
362+
let sorter = if self.stat.sort_by_pid {
363+
"PID"
364+
} else {
365+
&self.stat.sorter
366+
};
362367
let highlight_column = self
363368
.proc_list
364369
.fields
365370
.iter()
366-
.position(|f| f == &self.stat.sorter)
371+
.position(|f| f == sorter)
367372
.unwrap_or(0);
368373
let build_constraint = |field: &str| match field {
369374
"PID" => Constraint::Length(7),

src/uu/top/src/tui/stat.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) struct TuiStat {
2323
pub full_command_line: bool,
2424
pub delay: Duration,
2525
pub sorter: String,
26+
pub sort_by_pid: bool,
2627
pub highlight_sorted: bool,
2728
pub highlight_bold: bool,
2829
}
@@ -52,6 +53,7 @@ impl TuiStat {
5253
full_command_line: true,
5354
delay: Duration::from_millis(1500), // 1.5s
5455
sorter: filter,
56+
sort_by_pid: false,
5557
highlight_sorted: false,
5658
highlight_bold: false,
5759
}

0 commit comments

Comments
 (0)