Skip to content

Commit eb52572

Browse files
committed
top: tui implement 4, l
1 parent ba9014c commit eb52572

File tree

3 files changed

+109
-23
lines changed

3 files changed

+109
-23
lines changed

src/uu/top/src/top.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
157157
uucore::error::set_exit_code(0);
158158
break;
159159
}
160+
event::Event::Key(KeyEvent {
161+
code: KeyCode::Char('l'),
162+
..
163+
}) => {
164+
let mut stat = tui_stat.write().unwrap();
165+
stat.show_load_avg = !stat.show_load_avg;
166+
should_update.store(true, Ordering::Relaxed);
167+
}
160168
event::Event::Key(KeyEvent {
161169
code: KeyCode::Char('t'),
162170
..
@@ -175,6 +183,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
175183
should_update.store(true, Ordering::Relaxed);
176184
data.write().unwrap().0.update_cpu(&stat);
177185
}
186+
event::Event::Key(KeyEvent {
187+
code: KeyCode::Char('4'),
188+
..
189+
}) => {
190+
let mut stat = tui_stat.write().unwrap();
191+
stat.cpu_column = stat.cpu_column % 8 + 1;
192+
should_update.store(true, Ordering::Relaxed);
193+
}
178194
event::Event::Key(KeyEvent {
179195
code: KeyCode::Char('m'),
180196
..

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

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,50 +34,114 @@ impl<'a> Tui<'a> {
3434
}
3535

3636
fn calc_header_height(&self) -> u16 {
37-
let mut height = 1;
37+
let mut height = 0;
3838

39-
if self.stat.cpu_graph_mode != CpuGraphMode::Hide {
39+
if self.stat.show_load_avg {
4040
height += 1;
41-
height += self.header.cpu.len() as u16;
41+
}
42+
43+
let mut columns = 0;
44+
if self.stat.cpu_graph_mode != CpuGraphMode::Hide {
45+
height += 1; // task line
46+
if self.stat.cpu_graph_mode == CpuGraphMode::Sum {
47+
height += self.header.cpu.len() as u16;
48+
} else {
49+
columns += self.header.cpu.len() as u16;
50+
}
4251
}
4352
if self.stat.memory_graph_mode != MemoryGraphMode::Hide {
44-
height += 2;
53+
if self.stat.memory_graph_mode == MemoryGraphMode::Sum {
54+
height += 2;
55+
} else {
56+
columns += 2;
57+
}
58+
}
59+
height += columns / self.stat.cpu_column;
60+
if columns % self.stat.cpu_column != 0 {
61+
height += 1;
4562
}
4663

4764
height
4865
}
4966

5067
fn render_header(&self, area: Rect, buf: &mut Buffer) {
51-
let mut constraints = vec![Constraint::Length(1)];
68+
let mut constraints = vec![];
5269

5370
let cpu = &self.header.cpu;
5471

72+
if self.stat.show_load_avg {
73+
constraints.push(Constraint::Length(1)); // load avg line
74+
}
75+
76+
let mut columns = 0;
5577
if self.stat.cpu_graph_mode != CpuGraphMode::Hide {
56-
constraints.extend(vec![Constraint::Length(1); cpu.len() + 1]);
78+
constraints.push(Constraint::Length(1)); // task line
79+
80+
if self.stat.cpu_graph_mode == CpuGraphMode::Sum {
81+
constraints.extend(vec![Constraint::Length(1); cpu.len()]);
82+
} else {
83+
columns += self.header.cpu.len() as u16;
84+
}
5785
}
5886
if self.stat.memory_graph_mode != MemoryGraphMode::Hide {
59-
constraints.extend(vec![Constraint::Length(1); 2]);
87+
if self.stat.memory_graph_mode == MemoryGraphMode::Sum {
88+
constraints.extend(vec![Constraint::Length(1); 2]);
89+
} else {
90+
columns += 2;
91+
}
92+
}
93+
constraints.extend(vec![
94+
Constraint::Length(1);
95+
(columns / self.stat.cpu_column) as usize
96+
]);
97+
if columns % self.stat.cpu_column != 0 {
98+
constraints.push(Constraint::Length(1));
6099
}
61100
let header_layout = Layout::new(Direction::Vertical, constraints).split(area);
62101
let mut i = 0;
63102

64-
let render_bars = |bars_to_render: Vec<(String, f64, f64, f64, f64, char, bool)>,
65-
buf: &mut Buffer,
66-
i: usize| {
103+
let mut i_columns = 0;
104+
let mut cpu_column = None;
105+
let mut render_bars = |bars_to_render: Vec<(String, f64, f64, f64, f64, char, bool)>,
106+
buf: &mut Buffer,
107+
i: usize| {
67108
let mut i = i;
68109
for (tag, l, r, red, yellow, content, print_percentage) in bars_to_render {
110+
if cpu_column.is_none() || i_columns >= self.stat.cpu_column as usize {
111+
let mut constraints = vec![Constraint::Min(25)];
112+
for _ in 0..self.stat.cpu_column {
113+
constraints.extend(vec![Constraint::Length(3), Constraint::Min(25)]);
114+
}
115+
let line =
116+
Layout::new(Direction::Horizontal, constraints).split(header_layout[i]);
117+
i += 1;
118+
i_columns = 0;
119+
cpu_column = Some(line);
120+
}
121+
122+
let column_offset = i_columns * 2;
123+
let area = cpu_column.as_ref().unwrap()[column_offset];
124+
if i_columns > 0 {
125+
Line::from(vec![
126+
Span::raw(" "),
127+
Span::styled(" ", Style::default().bg(Color::Yellow)),
128+
Span::raw(" "),
129+
])
130+
.render(cpu_column.as_ref().unwrap()[column_offset - 1], buf);
131+
}
69132
let line_layout = Layout::new(
70133
Direction::Horizontal,
71134
[
72135
Constraint::Length(10),
73-
Constraint::Length(16),
136+
Constraint::Length(if self.stat.cpu_column < 3 { 16 } else { 0 }),
74137
Constraint::Length(1),
75138
Constraint::Min(0),
76139
Constraint::Length(1),
77140
],
78141
)
79-
.split(header_layout[i]);
80-
i += 1;
142+
.split(area);
143+
i_columns += 1;
144+
81145
Span::styled(format!("%{tag:<6}:",), Style::default().red())
82146
.render(line_layout[0], buf);
83147
let percentage = if print_percentage {
@@ -120,15 +184,17 @@ impl<'a> Tui<'a> {
120184
i
121185
};
122186

123-
let uptime = format!(
124-
"top - {time} {uptime}, {user}, {load_average}",
125-
time = self.header.uptime.time,
126-
uptime = self.header.uptime.uptime,
127-
user = self.header.uptime.user,
128-
load_average = self.header.uptime.load_average,
129-
);
130-
Paragraph::new(uptime).render(header_layout[0], buf);
131-
i += 1;
187+
if self.stat.show_load_avg {
188+
let load_avg = format!(
189+
"top - {time} {uptime}, {user}, {load_average}",
190+
time = self.header.uptime.time,
191+
uptime = self.header.uptime.uptime,
192+
user = self.header.uptime.user,
193+
load_average = self.header.uptime.load_average,
194+
);
195+
Paragraph::new(load_avg).render(header_layout[i], buf);
196+
i += 1;
197+
}
132198

133199
if self.stat.cpu_graph_mode != CpuGraphMode::Hide {
134200
let task = &self.header.task;
@@ -145,7 +211,7 @@ impl<'a> Tui<'a> {
145211
Span::raw(task.zombie.to_string()),
146212
Span::styled(" zombie", Style::default().red()),
147213
];
148-
Line::from(task_line).render(header_layout[1], buf);
214+
Line::from(task_line).render(header_layout[i], buf);
149215
i += 1;
150216

151217
let mut cpu_bars = Vec::new();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@
66
use std::time::Duration;
77

88
pub(crate) struct TuiStat {
9+
pub show_load_avg: bool,
910
pub cpu_graph_mode: CpuGraphMode,
1011
pub cpu_value_mode: CpuValueMode,
1112
pub memory_graph_mode: MemoryGraphMode,
13+
pub cpu_column: u16,
1214
pub list_offset: usize,
1315
pub delay: Duration,
1416
}
1517

1618
impl TuiStat {
1719
pub fn new() -> Self {
1820
Self {
21+
show_load_avg: true,
1922
cpu_graph_mode: CpuGraphMode::default(),
2023
cpu_value_mode: CpuValueMode::default(),
2124
memory_graph_mode: MemoryGraphMode::default(),
25+
cpu_column: 2,
2226
list_offset: 0,
2327
delay: Duration::from_millis(1500), // 1.5s
2428
}

0 commit comments

Comments
 (0)