Skip to content

Commit 6fe102f

Browse files
committed
top: tui impl left right (horizontal offset)
1 parent 383fd8f commit 6fe102f

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ pub fn handle_input(
183183
stat.list_offset += 1;
184184
should_update.store(true, Ordering::Relaxed);
185185
}
186+
Event::Key(KeyEvent {
187+
code: KeyCode::Left,
188+
..
189+
}) => {
190+
let mut stat = tui_stat.write().unwrap();
191+
if stat.horizontal_offset > 0 {
192+
stat.horizontal_offset -= 1;
193+
should_update.store(true, Ordering::Relaxed);
194+
}
195+
}
196+
Event::Key(KeyEvent {
197+
code: KeyCode::Right,
198+
..
199+
}) => {
200+
let mut stat = tui_stat.write().unwrap();
201+
stat.horizontal_offset += 1;
202+
should_update.store(true, Ordering::Relaxed);
203+
}
186204
Event::Resize(_, _) => should_update.store(true, Ordering::Relaxed),
187205
_ => {}
188206
},

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

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ impl<'a> Tui<'a> {
6565
height
6666
}
6767

68+
fn calc_offset(&self) -> ((usize, usize), (usize, usize, usize)) {
69+
let list_total = self.proc_list.collected.len();
70+
let list_offset = self.stat.list_offset;
71+
72+
let horizontal_total = self.proc_list.fields.len();
73+
let mut horizontal_offset = self.stat.horizontal_offset;
74+
let column = if horizontal_offset >= horizontal_total {
75+
horizontal_offset -= horizontal_total - 1;
76+
horizontal_total - 1
77+
} else {
78+
let o = horizontal_offset;
79+
horizontal_offset = 0;
80+
o
81+
};
82+
(
83+
(list_offset, list_total),
84+
(column, horizontal_total, horizontal_offset * 8),
85+
)
86+
}
87+
6888
fn render_header(&self, area: Rect, buf: &mut Buffer) {
6989
let constraints = vec![Constraint::Length(1); self.calc_header_height() as usize];
7090
let colorful = self.stat.colorful;
@@ -386,20 +406,30 @@ impl<'a> Tui<'a> {
386406
_ => Constraint::Length(0),
387407
};
388408

409+
let (list_offset, column_offset) = self.calc_offset();
410+
389411
let constraints: Vec<Constraint> = self
390412
.proc_list
391413
.fields
392414
.iter()
393415
.map(|field| build_constraint(field))
416+
.skip(column_offset.0)
394417
.collect();
395418

396-
self.stat.list_offset = min(self.stat.list_offset, self.proc_list.collected.len() - 1);
397-
398-
let header =
399-
Row::new(self.proc_list.fields.clone()).style(Style::default().bg_secondary(colorful));
419+
let header = Row::new(self.proc_list.fields.clone().split_off(column_offset.0))
420+
.style(Style::default().bg_secondary(colorful));
400421

401422
let rows = self.proc_list.collected.iter().map(|item| {
402-
let cells = item.iter().enumerate().map(|(n, c)| {
423+
let cells = item.iter().enumerate().skip(column_offset.0).map(|(n, c)| {
424+
let c = if column_offset.2 > 0 {
425+
if c.len() < column_offset.2 {
426+
""
427+
} else {
428+
&c[column_offset.2..]
429+
}
430+
} else {
431+
c
432+
};
403433
if highlight_sorted && n == highlight_column {
404434
Cell::from(Span::styled(
405435
c,
@@ -410,13 +440,13 @@ impl<'a> Tui<'a> {
410440
},
411441
))
412442
} else {
413-
Cell::from(c.as_str())
443+
Cell::from(c)
414444
}
415445
});
416446
Row::new(cells).height(1)
417447
});
418448

419-
let mut state = TableState::default().with_offset(self.stat.list_offset);
449+
let mut state = TableState::default().with_offset(list_offset.0);
420450

421451
let table = Table::new(rows, constraints).header(header);
422452
StatefulWidget::render(table, area, buf, &mut state);
@@ -425,6 +455,7 @@ impl<'a> Tui<'a> {
425455

426456
impl Widget for Tui<'_> {
427457
fn render(mut self, area: Rect, buf: &mut Buffer) {
458+
self.stat.list_offset = min(self.stat.list_offset, self.proc_list.collected.len() - 1);
428459
let layout = Layout::new(
429460
Direction::Vertical,
430461
[

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub(crate) struct TuiStat {
1919
pub memory_graph_mode: MemoryGraphMode,
2020
pub cpu_column: u16,
2121
pub list_offset: usize,
22+
pub horizontal_offset: usize,
2223
pub colorful: bool,
2324
pub full_command_line: bool,
2425
pub delay: Duration,
@@ -49,6 +50,7 @@ impl TuiStat {
4950
memory_graph_mode: MemoryGraphMode::default(),
5051
cpu_column: 2,
5152
list_offset: 0,
53+
horizontal_offset: 0,
5254
colorful: true,
5355
full_command_line: true,
5456
delay: Duration::from_millis(1500), // 1.5s

0 commit comments

Comments
 (0)