Skip to content

Commit 320896e

Browse files
committed
top: tui impl 0
1 parent 391cd26 commit 320896e

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

src/uu/top/src/picker.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ pub fn sysinfo() -> &'static RwLock<System> {
2424
}
2525

2626
pub trait Column {
27-
fn as_string(&self) -> String;
27+
fn as_string(&self, show_zeros: bool) -> String;
2828
fn cmp_dyn(&self, other: &dyn Column) -> Ordering;
2929
fn as_any(&self) -> &dyn Any;
3030
}
3131

3232
impl Column for String {
33-
fn as_string(&self) -> String {
33+
fn as_string(&self, _show_zeros: bool) -> String {
3434
self.clone()
3535
}
3636

@@ -47,7 +47,10 @@ impl Column for String {
4747
}
4848

4949
impl Column for u32 {
50-
fn as_string(&self) -> String {
50+
fn as_string(&self, show_zeros: bool) -> String {
51+
if !show_zeros && self == &0 {
52+
return String::new();
53+
}
5154
self.to_string()
5255
}
5356

@@ -64,7 +67,10 @@ impl Column for u32 {
6467
}
6568

6669
impl Column for Option<i32> {
67-
fn as_string(&self) -> String {
70+
fn as_string(&self, show_zeros: bool) -> String {
71+
if !show_zeros && self == &Some(0) {
72+
return String::new();
73+
}
6874
self.map(|v| v.to_string()).unwrap_or_default()
6975
}
7076

@@ -96,7 +102,10 @@ impl PercentValue {
96102
}
97103

98104
impl Column for PercentValue {
99-
fn as_string(&self) -> String {
105+
fn as_string(&self, show_zeros: bool) -> String {
106+
if !show_zeros && self.value == 0.0 {
107+
return String::new();
108+
}
100109
format!("{:.1}", self.value)
101110
}
102111

@@ -123,7 +132,10 @@ impl MemValue {
123132
}
124133

125134
impl Column for MemValue {
126-
fn as_string(&self) -> String {
135+
fn as_string(&self, show_zeros: bool) -> String {
136+
if !show_zeros && self.value == 0 {
137+
return String::new();
138+
}
127139
let mem_mb = self.value as f64 / bytesize::MIB as f64;
128140
if mem_mb >= 10000.0 {
129141
format!("{:.1}g", self.value as f64 / bytesize::GIB as f64)
@@ -156,7 +168,10 @@ impl TimeMSValue {
156168
}
157169

158170
impl Column for TimeMSValue {
159-
fn as_string(&self) -> String {
171+
fn as_string(&self, show_zeros: bool) -> String {
172+
if !show_zeros && self.min == 0 && self.sec < 0.01 {
173+
return String::new();
174+
}
160175
format!("{}:{:0>5.2}", self.min, self.sec)
161176
}
162177

src/uu/top/src/top.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ fn collect(settings: &Settings, fields: &[String], tui_stat: &TuiStat) -> Vec<Ve
235235
}
236236
collected
237237
.into_iter()
238-
.map(|it| it.into_iter().map(|c| c.as_string()).collect())
238+
.map(|it| {
239+
it.into_iter()
240+
.map(|c| c.as_string(tui_stat.show_zeros))
241+
.collect()
242+
})
239243
.collect()
240244
}
241245

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ pub fn handle_input(
104104
stat.colorful = !stat.colorful;
105105
should_update.store(true, Ordering::Relaxed);
106106
}
107+
char!('0') => {
108+
{
109+
// drop the lock as soon as possible
110+
let mut stat = tui_stat.write().unwrap();
111+
stat.show_zeros = !stat.show_zeros;
112+
}
113+
114+
data.write().unwrap().1 = ProcList::new(settings, &tui_stat.read().unwrap());
115+
should_update.store(true, Ordering::Relaxed);
116+
}
107117
char!('1') => {
108118
let mut stat = tui_stat.write().unwrap();
109119
stat.cpu_value_mode = stat.cpu_value_mode.next();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(crate) struct TuiStat {
2828
pub highlight_sorted: bool,
2929
pub highlight_bold: bool,
3030
pub show_coordinates: bool,
31+
pub show_zeros: bool,
3132
}
3233

3334
impl TuiStat {
@@ -60,6 +61,7 @@ impl TuiStat {
6061
highlight_sorted: false,
6162
highlight_bold: false,
6263
show_coordinates: false,
64+
show_zeros: true,
6365
}
6466
}
6567

0 commit comments

Comments
 (0)