Skip to content

Commit de50df2

Browse files
authored
Merge pull request #531 from Bluemangoo/feature/top-tui-i
top: tui impl `I` (irix or solaris)
2 parents 1b94527 + 9eb7079 commit de50df2

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/uu/top/src/picker.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,19 @@ fn todo(_pid: u32, _stat: Stat) -> Box<dyn Column> {
223223
Box::new("TODO".to_string())
224224
}
225225

226-
fn cpu(pid: u32, _stat: Stat) -> Box<dyn Column> {
226+
fn cpu(pid: u32, stat: Stat) -> Box<dyn Column> {
227227
let binding = sysinfo().read().unwrap();
228228
let Some(proc) = binding.process(Pid::from_u32(pid)) else {
229229
return PercentValue::new_boxed(0.0);
230230
};
231231

232-
PercentValue::new_boxed(proc.cpu_usage())
232+
let cpu_usage = if stat.1.irix_mode {
233+
proc.cpu_usage()
234+
} else {
235+
proc.cpu_usage() / binding.cpus().len() as f32
236+
};
237+
238+
PercentValue::new_boxed(cpu_usage)
233239
}
234240

235241
fn pid(pid: u32, _stat: Stat) -> Box<dyn Column> {

src/uu/top/src/top.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
121121
{
122122
let header = Header::new(&tui_stat.read().unwrap());
123123
let proc_list = ProcList::new(&settings, &tui_stat.read().unwrap());
124-
tui_stat.write().unwrap().input_error = None;
124+
tui_stat.write().unwrap().input_message = None;
125125
*data.write().unwrap() = (header, proc_list);
126126
should_update.store(true, Ordering::Relaxed);
127127
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ pub fn handle_input(
7474
data.write().unwrap().1 = ProcList::new(settings, &tui_stat.read().unwrap());
7575
should_update.store(true, Ordering::Relaxed);
7676
}
77+
char!('I') => {
78+
{
79+
let mut stat = tui_stat.write().unwrap();
80+
stat.irix_mode = !stat.irix_mode;
81+
stat.input_message = Some(format!(
82+
" Irix mode {} ",
83+
if stat.irix_mode { "On" } else { "Off" }
84+
));
85+
}
86+
87+
data.write().unwrap().1 = ProcList::new(settings, &tui_stat.read().unwrap());
88+
should_update.store(true, Ordering::Relaxed);
89+
}
7790
char!('l') => {
7891
let mut stat = tui_stat.write().unwrap();
7992
stat.show_load_avg = !stat.show_load_avg;
@@ -303,7 +316,7 @@ fn handle_input_value(
303316
if input_value.is_err() {
304317
let mut stat = tui_stat.write().unwrap();
305318
stat.reset_input();
306-
stat.input_error = Some(" invalid number ".into());
319+
stat.input_message = Some(" invalid number ".into());
307320
should_update.store(true, Ordering::Relaxed);
308321
return;
309322
}
@@ -323,7 +336,7 @@ fn handle_input_value(
323336
{
324337
let mut stat = tui_stat.write().unwrap();
325338
stat.reset_input();
326-
stat.input_error = Some(" invalid numa node ".into());
339+
stat.input_message = Some(" invalid numa node ".into());
327340
should_update.store(true, Ordering::Relaxed);
328341
return;
329342
}
@@ -350,7 +363,7 @@ fn handle_input_value(
350363
Err(_) => {
351364
let mut stat = tui_stat.write().unwrap();
352365
stat.reset_input();
353-
stat.input_error = Some(" invalid user ".into());
366+
stat.input_message = Some(" invalid user ".into());
354367
should_update.store(true, Ordering::Relaxed);
355368
return;
356369
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ impl<'a> Tui<'a> {
354354

355355
fn render_input(&self, area: Rect, buf: &mut Buffer) {
356356
let colorful = self.stat.colorful;
357-
if let Some(v) = self.stat.input_error.as_ref() {
357+
if let Some(v) = self.stat.input_message.as_ref() {
358358
let layout = Layout::new(
359359
Direction::Horizontal,
360360
[Constraint::Length(v.len() as u16), Constraint::Fill(1)],

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub(crate) struct TuiStat {
1111
pub input_mode: InputMode,
1212
pub input_label: String,
1313
pub input_value: String,
14-
pub input_error: Option<String>,
14+
pub input_message: Option<String>, // Info or error
1515

1616
pub show_load_avg: bool,
1717
pub cpu_graph_mode: CpuGraphMode,
@@ -30,6 +30,7 @@ pub(crate) struct TuiStat {
3030
pub highlight_bold: bool,
3131
pub show_coordinates: bool,
3232
pub show_zeros: bool,
33+
pub irix_mode: bool,
3334

3435
pub filter: Option<crate::Filter>,
3536
}
@@ -47,7 +48,7 @@ impl TuiStat {
4748
input_mode: InputMode::Command,
4849
input_label: String::new(),
4950
input_value: String::new(),
50-
input_error: None,
51+
input_message: None,
5152

5253
show_load_avg: true,
5354
cpu_graph_mode: CpuGraphMode::default(),
@@ -66,6 +67,7 @@ impl TuiStat {
6667
highlight_bold: false,
6768
show_coordinates: false,
6869
show_zeros: true,
70+
irix_mode: true,
6971

7072
filter: None,
7173
}
@@ -75,7 +77,7 @@ impl TuiStat {
7577
self.input_mode = InputMode::Command;
7678
self.input_label.clear();
7779
self.input_value.clear();
78-
self.input_error = None;
80+
self.input_message = None;
7981
}
8082
}
8183

0 commit comments

Comments
 (0)