Skip to content

Commit 9b86686

Browse files
committed
top: tui z (color/mono)
1 parent 1a0db60 commit 9b86686

File tree

4 files changed

+121
-31
lines changed

4 files changed

+121
-31
lines changed

src/uu/top/src/tui/color.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use ratatui::prelude::Stylize;
2+
use ratatui::style::{Color, Styled};
3+
4+
pub(crate) trait TuiColor<'a, T>: Sized {
5+
fn primary(self, colorful: bool) -> T;
6+
fn bg_primary(self, colorful: bool) -> T;
7+
fn secondary(self, colorful: bool) -> T;
8+
fn bg_secondary(self, colorful: bool) -> T;
9+
fn error(self, colorful: bool) -> T;
10+
}
11+
12+
impl<'a, T, U> TuiColor<'a, T> for U
13+
where
14+
U: Styled<Item = T>,
15+
{
16+
fn primary(self, colorful: bool) -> T {
17+
let style = self.style();
18+
if colorful {
19+
self.red()
20+
} else {
21+
self.set_style(style)
22+
}
23+
}
24+
25+
fn bg_primary(self, colorful: bool) -> T {
26+
let style = self.style().fg(Color::Black);
27+
if colorful {
28+
self.set_style(style.bg(Color::Red))
29+
} else {
30+
self.set_style(style.bg(Color::White))
31+
}
32+
}
33+
34+
fn secondary(self, colorful: bool) -> T {
35+
let style = self.style();
36+
if colorful {
37+
self.yellow()
38+
} else {
39+
self.set_style(style)
40+
}
41+
}
42+
43+
fn bg_secondary(self, colorful: bool) -> T {
44+
let style = self.style().fg(Color::Black);
45+
if colorful {
46+
self.set_style(style.bg(Color::Yellow))
47+
} else {
48+
self.set_style(style.bg(Color::White))
49+
}
50+
}
51+
52+
fn error(self, colorful: bool) -> T {
53+
let style = self.style();
54+
if colorful {
55+
self.set_style(style.fg(Color::Black).bg(Color::Red))
56+
} else {
57+
self.set_style(style)
58+
}
59+
}
60+
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ pub fn handle_input(
112112
stat.memory_graph_mode = stat.memory_graph_mode.next();
113113
should_update.store(true, Ordering::Relaxed);
114114
}
115+
Event::Key(KeyEvent {
116+
code: KeyCode::Char('z'),
117+
..
118+
}) => {
119+
let mut stat = tui_stat.write().unwrap();
120+
stat.colorful = !stat.colorful;
121+
122+
should_update.store(true, Ordering::Relaxed);
123+
}
115124
Event::Key(KeyEvent {
116125
code: KeyCode::Up, ..
117126
}) => {
@@ -174,17 +183,17 @@ fn handle_input_value(
174183
Ok(v) => v,
175184
Err(_) => {
176185
let mut stat = tui_stat.write().unwrap();
177-
stat.input_error = Some(" invalid numa node ".into());
178186
stat.reset_input();
187+
stat.input_error = Some(" invalid numa node ".into());
179188
should_update.store(true, Ordering::Relaxed);
180189
return;
181190
}
182191
};
183192
let numa_nodes = get_numa_nodes();
184193
if !numa_nodes.contains_key(&input_value) {
185194
let mut stat = tui_stat.write().unwrap();
186-
stat.input_error = Some(" invalid numa node ".into());
187195
stat.reset_input();
196+
stat.input_error = Some(" invalid numa node ".into());
188197
should_update.store(true, Ordering::Relaxed);
189198
return;
190199
}

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

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6+
mod color;
67
mod input;
78
pub mod stat;
9+
810
pub use input::*;
911

1012
use crate::header::{format_memory, Header};
13+
use crate::tui::color::TuiColor;
1114
use crate::tui::stat::{CpuGraphMode, MemoryGraphMode, TuiStat};
1215
use crate::ProcList;
1316
use ratatui::prelude::*;
@@ -64,6 +67,7 @@ impl<'a> Tui<'a> {
6467

6568
fn render_header(&self, area: Rect, buf: &mut Buffer) {
6669
let constraints = vec![Constraint::Length(1); self.calc_header_height() as usize];
70+
let colorful = self.stat.colorful;
6771

6872
let cpu = &self.header.cpu;
6973

@@ -100,7 +104,7 @@ impl<'a> Tui<'a> {
100104
if i_columns > 0 {
101105
Line::from(vec![
102106
Span::raw(" "),
103-
Span::styled(" ", Style::default().bg(Color::Yellow)),
107+
Span::styled(" ", Style::default().bg_secondary(colorful)),
104108
Span::raw(" "),
105109
])
106110
.render(cpu_column.as_ref().unwrap()[column_offset - 1], buf);
@@ -118,7 +122,7 @@ impl<'a> Tui<'a> {
118122
.split(area);
119123
i_columns += 1;
120124

121-
Span::styled(format!("%{tag:<6}:",), Style::default().red())
125+
Span::styled(format!("%{tag:<6}:",), Style::default().primary(colorful))
122126
.render(line_layout[0], buf);
123127
let percentage = if print_percentage {
124128
format!("{:>5.0}", ((red + yellow) * 100.0).round())
@@ -127,7 +131,10 @@ impl<'a> Tui<'a> {
127131
};
128132
Line::from(vec![
129133
Span::raw(format!("{l:>5.1}")),
130-
Span::styled(format!("/{r:<5.1}{percentage}"), Style::default().red()),
134+
Span::styled(
135+
format!("/{r:<5.1}{percentage}"),
136+
Style::default().primary(colorful),
137+
),
131138
])
132139
.render(line_layout[1], buf);
133140
Paragraph::new("[").render(line_layout[2], buf);
@@ -139,17 +146,17 @@ impl<'a> Tui<'a> {
139146
let red_span = Span::styled(
140147
content.to_string().repeat(red_width as usize),
141148
if content == ' ' {
142-
Style::default().bg(Color::Red)
149+
Style::default().bg_primary(colorful)
143150
} else {
144-
Style::default().red()
151+
Style::default().primary(colorful)
145152
},
146153
);
147154
let yellow_span = Span::styled(
148155
content.to_string().repeat(yellow_width as usize),
149156
if content == ' ' {
150-
Style::default().bg(Color::Yellow)
157+
Style::default().bg_secondary(colorful)
151158
} else {
152-
Style::default().yellow()
159+
Style::default().secondary(colorful)
153160
},
154161
);
155162

@@ -175,17 +182,17 @@ impl<'a> Tui<'a> {
175182
if self.stat.cpu_graph_mode != CpuGraphMode::Hide {
176183
let task = &self.header.task;
177184
let task_line = vec![
178-
Span::styled("Tasks: ", Style::default().red()),
185+
Span::styled("Tasks: ", Style::default().primary(colorful)),
179186
Span::raw(task.total.to_string()),
180-
Span::styled(" total, ", Style::default().red()),
187+
Span::styled(" total, ", Style::default().primary(colorful)),
181188
Span::raw(task.running.to_string()),
182-
Span::styled(" running, ", Style::default().red()),
189+
Span::styled(" running, ", Style::default().primary(colorful)),
183190
Span::raw(task.sleeping.to_string()),
184-
Span::styled(" sleeping, ", Style::default().red()),
191+
Span::styled(" sleeping, ", Style::default().primary(colorful)),
185192
Span::raw(task.stopped.to_string()),
186-
Span::styled(" stopped, ", Style::default().red()),
193+
Span::styled(" stopped, ", Style::default().primary(colorful)),
187194
Span::raw(task.zombie.to_string()),
188-
Span::styled(" zombie", Style::default().red()),
195+
Span::styled(" zombie", Style::default().primary(colorful)),
189196
];
190197
Line::from(task_line).render(header_layout[i], buf);
191198
i += 1;
@@ -254,28 +261,34 @@ impl<'a> Tui<'a> {
254261

255262
if self.stat.memory_graph_mode == MemoryGraphMode::Sum {
256263
Line::from(vec![
257-
Span::styled(format!("{unit_name} Mem : "), Style::default().red()),
264+
Span::styled(
265+
format!("{unit_name} Mem : "),
266+
Style::default().primary(colorful),
267+
),
258268
Span::raw(format!("{:8.1}", format_memory(mem.total, unit))),
259-
Span::styled(" total, ", Style::default().red()),
269+
Span::styled(" total, ", Style::default().primary(colorful)),
260270
Span::raw(format!("{:8.1}", format_memory(mem.free, unit))),
261-
Span::styled(" free, ", Style::default().red()),
271+
Span::styled(" free, ", Style::default().primary(colorful)),
262272
Span::raw(format!("{:8.1}", format_memory(mem.used, unit))),
263-
Span::styled(" used, ", Style::default().red()),
273+
Span::styled(" used, ", Style::default().primary(colorful)),
264274
Span::raw(format!("{:8.1}", format_memory(mem.buff_cache, unit))),
265-
Span::styled(" buff/cache", Style::default().red()),
275+
Span::styled(" buff/cache", Style::default().primary(colorful)),
266276
])
267277
.render(header_layout[i], buf);
268278
i += 1;
269279
Line::from(vec![
270-
Span::styled(format!("{unit_name} Swap: "), Style::default().red()),
280+
Span::styled(
281+
format!("{unit_name} Swap: "),
282+
Style::default().primary(colorful),
283+
),
271284
Span::raw(format!("{:8.1}", format_memory(mem.total_swap, unit))),
272-
Span::styled(" total, ", Style::default().red()),
285+
Span::styled(" total, ", Style::default().primary(colorful)),
273286
Span::raw(format!("{:8.1}", format_memory(mem.free_swap, unit))),
274-
Span::styled(" free, ", Style::default().red()),
287+
Span::styled(" free, ", Style::default().primary(colorful)),
275288
Span::raw(format!("{:8.1}", format_memory(mem.used_swap, unit))),
276-
Span::styled(" used, ", Style::default().red()),
289+
Span::styled(" used, ", Style::default().primary(colorful)),
277290
Span::raw(format!("{:8.1}", format_memory(mem.available, unit))),
278-
Span::styled(" avail Mem", Style::default().red()),
291+
Span::styled(" avail Mem", Style::default().primary(colorful)),
279292
])
280293
.render(header_layout[i], buf);
281294
} else {
@@ -322,21 +335,28 @@ impl<'a> Tui<'a> {
322335
}
323336

324337
fn render_input(&self, area: Rect, buf: &mut Buffer) {
338+
let colorful = self.stat.colorful;
325339
if let Some(v) = self.stat.input_error.as_ref() {
340+
let layout = Layout::new(
341+
Direction::Horizontal,
342+
[Constraint::Length(v.len() as u16), Constraint::Fill(1)],
343+
)
344+
.split(area);
326345
Paragraph::new(v.as_str())
327-
.style(Style::default().fg(Color::Red))
328-
.render(area, buf);
346+
.style(Style::default().error(colorful))
347+
.render(layout[0], buf);
329348
return;
330349
}
331350
let input = Line::from(vec![
332-
Span::styled(&self.stat.input_label, Style::default().red()),
351+
Span::styled(&self.stat.input_label, Style::default().primary(colorful)),
333352
Span::raw(" "),
334353
Span::raw(&self.stat.input_value),
335354
]);
336355
input.render(area, buf);
337356
}
338357

339358
fn render_list(&mut self, area: Rect, buf: &mut Buffer) {
359+
let colorful = self.stat.colorful;
340360
let build_constraint = |field: &str| match field {
341361
"PID" => Constraint::Length(7),
342362
"USER" => Constraint::Length(10),
@@ -362,9 +382,8 @@ impl<'a> Tui<'a> {
362382

363383
self.stat.list_offset = min(self.stat.list_offset, self.proc_list.collected.len() - 1);
364384

365-
let header = Row::new(self.proc_list.fields.clone())
366-
.style(Style::default().bg(Color::Yellow))
367-
.height(1);
385+
let header =
386+
Row::new(self.proc_list.fields.clone()).style(Style::default().bg_secondary(colorful));
368387

369388
let rows = self.proc_list.collected.iter().map(|item| {
370389
let cells = item.iter().map(|c| Cell::from(c.as_str()));

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(crate) struct TuiStat {
1818
pub memory_graph_mode: MemoryGraphMode,
1919
pub cpu_column: u16,
2020
pub list_offset: usize,
21+
pub colorful: bool,
2122
pub delay: Duration,
2223
}
2324

@@ -35,6 +36,7 @@ impl TuiStat {
3536
memory_graph_mode: MemoryGraphMode::default(),
3637
cpu_column: 2,
3738
list_offset: 0,
39+
colorful: true,
3840
delay: Duration::from_millis(1500), // 1.5s
3941
}
4042
}

0 commit comments

Comments
 (0)