Skip to content

Commit a4449a6

Browse files
authored
Merge pull request #532 from Bluemangoo/feature/top-tui-x
top: tui impl `X`
2 parents de50df2 + 8ec2f94 commit a4449a6

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub(crate) enum InputEvent {
2323
NumaNode,
2424
FilterUser,
2525
FilterEUser,
26+
WidthIncrement,
2627
}
2728

2829
macro_rules! char {
@@ -138,6 +139,14 @@ pub fn handle_input(
138139

139140
should_update.store(true, Ordering::Relaxed);
140141
}
142+
char!('X') => {
143+
let mut stat = tui_stat.write().unwrap();
144+
stat.input_label = "width incr is 0, change to (0 default, -1 auto) ".into();
145+
stat.input_value.clear();
146+
stat.input_mode = InputMode::Input(InputEvent::WidthIncrement);
147+
148+
should_update.store(true, Ordering::Relaxed);
149+
}
141150
char!('x') => {
142151
let mut stat = tui_stat.write().unwrap();
143152
stat.highlight_sorted = !stat.highlight_sorted;
@@ -383,5 +392,28 @@ fn handle_input_value(
383392
stat.reset_input();
384393
should_update.store(true, Ordering::Relaxed);
385394
}
395+
InputEvent::WidthIncrement => {
396+
let input_value = { tui_stat.read().unwrap().input_value.parse::<isize>() };
397+
398+
if input_value.is_err() || input_value.as_ref().is_ok_and(|v| *v < -1) {
399+
let is_empty = { tui_stat.read().unwrap().input_value.trim().is_empty() };
400+
let mut stat = tui_stat.write().unwrap();
401+
stat.reset_input();
402+
if !is_empty {
403+
stat.input_message = Some(" Unacceptable integer ".into());
404+
}
405+
should_update.store(true, Ordering::Relaxed);
406+
return;
407+
}
408+
let input_value = input_value.unwrap();
409+
let mut stat = tui_stat.write().unwrap();
410+
stat.width_increment = if input_value == -1 {
411+
None
412+
} else {
413+
Some(input_value as usize)
414+
};
415+
stat.reset_input();
416+
should_update.store(true, Ordering::Relaxed);
417+
}
386418
}
387419
}

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod input;
88
pub mod stat;
99

1010
pub use input::*;
11+
use std::borrow::Cow;
1112

1213
use crate::header::{format_memory, Header};
1314
use crate::tui::color::TuiColor;
@@ -409,9 +410,26 @@ impl<'a> Tui<'a> {
409410
.iter()
410411
.position(|f| f == sorter)
411412
.unwrap_or(0);
413+
let user_width = {
414+
if let Some(width) = self.stat.width_increment {
415+
10 + width
416+
} else if let Some(user_column_nth) =
417+
self.proc_list.fields.iter().position(|f| f == "USER")
418+
{
419+
let users: Vec<&String> = self
420+
.proc_list
421+
.collected
422+
.iter()
423+
.map(|item| &item[user_column_nth])
424+
.collect();
425+
users.iter().map(|u| u.len()).max().unwrap_or_default() + 1
426+
} else {
427+
10
428+
}
429+
};
412430
let build_constraint = |field: &str| match field {
413431
"PID" => Constraint::Length(7),
414-
"USER" => Constraint::Length(10),
432+
"USER" => Constraint::Length(user_width as u16),
415433
"PR" => Constraint::Length(4),
416434
"NI" => Constraint::Length(4),
417435
"VIRT" => Constraint::Length(8),
@@ -452,12 +470,20 @@ impl<'a> Tui<'a> {
452470
.map(|(n, c)| {
453471
let c = if column_coordinates.2 > 0 {
454472
if c.len() < column_coordinates.2 {
455-
""
473+
// handle offset
474+
Cow::Borrowed("")
475+
} else {
476+
Cow::Borrowed(&c[column_coordinates.2..])
477+
}
478+
} else if let Constraint::Length(length) = &constraints[n] {
479+
// truncate if too long
480+
if c.len() > *length as usize {
481+
Cow::Owned(format!("{}+", &c[0..*length as usize - 2]))
456482
} else {
457-
&c[column_coordinates.2..]
483+
Cow::Borrowed(c.as_str())
458484
}
459485
} else {
460-
c
486+
Cow::Borrowed(c.as_str())
461487
};
462488
if highlight_sorted && n == highlight_column {
463489
Cell::from(Span::styled(
@@ -477,7 +503,7 @@ impl<'a> Tui<'a> {
477503

478504
let mut state = TableState::default().with_offset(list_coordinates.0);
479505

480-
let table = Table::new(rows, constraints).header(header);
506+
let table = Table::new(rows, constraints.clone()).header(header);
481507
StatefulWidget::render(table, area, buf, &mut state);
482508
}
483509
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub(crate) struct TuiStat {
3131
pub show_coordinates: bool,
3232
pub show_zeros: bool,
3333
pub irix_mode: bool,
34+
pub width_increment: Option<usize>, // None means auto
3435

3536
pub filter: Option<crate::Filter>,
3637
}
@@ -68,6 +69,7 @@ impl TuiStat {
6869
show_coordinates: false,
6970
show_zeros: true,
7071
irix_mode: true,
72+
width_increment: None,
7173

7274
filter: None,
7375
}

0 commit comments

Comments
 (0)