Skip to content

Commit 391cd26

Browse files
committed
top: tui impl C
1 parent 6fe102f commit 391cd26

File tree

3 files changed

+80
-44
lines changed

3 files changed

+80
-44
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ pub fn handle_input(
5555
stat.highlight_bold = !stat.highlight_bold;
5656
should_update.store(true, Ordering::Relaxed);
5757
}
58+
char!('C') => {
59+
let mut stat = tui_stat.write().unwrap();
60+
stat.show_coordinates = !stat.show_coordinates;
61+
should_update.store(true, Ordering::Relaxed);
62+
}
5863
char!('c') => {
5964
{
6065
// drop the lock as soon as possible

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

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,22 @@ impl<'a> Tui<'a> {
6565
height
6666
}
6767

68-
fn calc_offset(&self) -> ((usize, usize), (usize, usize, usize)) {
68+
fn calc_list_coordinates(&self) -> (usize, usize) {
6969
let list_total = self.proc_list.collected.len();
7070
let list_offset = self.stat.list_offset;
71+
(list_offset, list_total)
72+
}
7173

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
74+
fn calc_column_coordinates(&self) -> (usize, usize, usize) {
75+
let total_columns = self.proc_list.fields.len();
76+
let horizontal_offset = self.stat.horizontal_offset;
77+
let column_coordinate = min(horizontal_offset, total_columns - 1);
78+
let horizontal_offset = if horizontal_offset >= total_columns {
79+
horizontal_offset - (total_columns - 1)
7780
} else {
78-
let o = horizontal_offset;
79-
horizontal_offset = 0;
80-
o
81+
0
8182
};
82-
(
83-
(list_offset, list_total),
84-
(column, horizontal_total, horizontal_offset * 8),
85-
)
83+
(column_coordinate, total_columns, horizontal_offset * 8)
8684
}
8785

8886
fn render_header(&self, area: Rect, buf: &mut Buffer) {
@@ -367,11 +365,32 @@ impl<'a> Tui<'a> {
367365
.render(layout[0], buf);
368366
return;
369367
}
370-
let input = Line::from(vec![
371-
Span::styled(&self.stat.input_label, Style::default().primary(colorful)),
372-
Span::raw(" "),
373-
Span::raw(&self.stat.input_value),
374-
]);
368+
let input = if !self.stat.input_label.is_empty() || !self.stat.input_value.is_empty() {
369+
Line::from(vec![
370+
Span::styled(&self.stat.input_label, Style::default().primary(colorful)),
371+
Span::raw(" "),
372+
Span::raw(&self.stat.input_value),
373+
])
374+
} else if self.stat.show_coordinates {
375+
let list_coordinates = self.calc_list_coordinates();
376+
let column_coordinates = self.calc_column_coordinates();
377+
Line::from(vec![
378+
Span::raw(format!(
379+
" scroll coordinates: y = {}/{} (tasks), x = {}/{} (fields)",
380+
list_coordinates.0 + 1,
381+
list_coordinates.1,
382+
column_coordinates.0 + 1,
383+
column_coordinates.1
384+
)),
385+
Span::raw(if column_coordinates.2 > 0 {
386+
format!(" + {}", column_coordinates.2)
387+
} else {
388+
String::new()
389+
}),
390+
])
391+
} else {
392+
Line::from("")
393+
};
375394
input.render(area, buf);
376395
}
377396

@@ -406,47 +425,57 @@ impl<'a> Tui<'a> {
406425
_ => Constraint::Length(0),
407426
};
408427

409-
let (list_offset, column_offset) = self.calc_offset();
428+
let list_coordinates = self.calc_list_coordinates();
429+
let column_coordinates = self.calc_column_coordinates();
410430

411431
let constraints: Vec<Constraint> = self
412432
.proc_list
413433
.fields
414434
.iter()
415435
.map(|field| build_constraint(field))
416-
.skip(column_offset.0)
436+
.skip(column_coordinates.0)
417437
.collect();
418438

419-
let header = Row::new(self.proc_list.fields.clone().split_off(column_offset.0))
420-
.style(Style::default().bg_secondary(colorful));
439+
let header = Row::new(
440+
self.proc_list
441+
.fields
442+
.clone()
443+
.split_off(column_coordinates.0),
444+
)
445+
.style(Style::default().bg_secondary(colorful));
421446

422447
let rows = self.proc_list.collected.iter().map(|item| {
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-
""
448+
let cells = item
449+
.iter()
450+
.enumerate()
451+
.skip(column_coordinates.0)
452+
.map(|(n, c)| {
453+
let c = if column_coordinates.2 > 0 {
454+
if c.len() < column_coordinates.2 {
455+
""
456+
} else {
457+
&c[column_coordinates.2..]
458+
}
459+
} else {
460+
c
461+
};
462+
if highlight_sorted && n == highlight_column {
463+
Cell::from(Span::styled(
464+
c,
465+
if highlight_bold {
466+
Style::default().bg_primary(colorful)
467+
} else {
468+
Style::default().primary(colorful)
469+
},
470+
))
427471
} else {
428-
&c[column_offset.2..]
472+
Cell::from(c)
429473
}
430-
} else {
431-
c
432-
};
433-
if highlight_sorted && n == highlight_column {
434-
Cell::from(Span::styled(
435-
c,
436-
if highlight_bold {
437-
Style::default().bg_primary(colorful)
438-
} else {
439-
Style::default().primary(colorful)
440-
},
441-
))
442-
} else {
443-
Cell::from(c)
444-
}
445-
});
474+
});
446475
Row::new(cells).height(1)
447476
});
448477

449-
let mut state = TableState::default().with_offset(list_offset.0);
478+
let mut state = TableState::default().with_offset(list_coordinates.0);
450479

451480
let table = Table::new(rows, constraints).header(header);
452481
StatefulWidget::render(table, area, buf, &mut state);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub(crate) struct TuiStat {
2727
pub sort_by_pid: bool,
2828
pub highlight_sorted: bool,
2929
pub highlight_bold: bool,
30+
pub show_coordinates: bool,
3031
}
3132

3233
impl TuiStat {
@@ -58,6 +59,7 @@ impl TuiStat {
5859
sort_by_pid: false,
5960
highlight_sorted: false,
6061
highlight_bold: false,
62+
show_coordinates: false,
6163
}
6264
}
6365

0 commit comments

Comments
 (0)