@@ -8,6 +8,7 @@ mod input;
88pub mod stat;
99
1010pub use input:: * ;
11+ use std:: borrow:: Cow ;
1112
1213use crate :: header:: { format_memory, Header } ;
1314use 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}
0 commit comments