@@ -23,6 +23,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2323 let mut section: Vec < String > = vec ! [ ] ;
2424 let mut title: Vec < String > = vec ! [ ] ;
2525 let mut data: Vec < String > = vec ! [ ] ;
26+ let mut data_len_excess = 0 ;
2627
2728 #[ cfg( target_os = "linux" ) ]
2829 let func = [
@@ -38,7 +39,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3839 let func: [ ConcatFunc ; 0 ] = [ ] ;
3940
4041 func. iter ( )
41- . for_each ( |f| f ( & mut section, & mut title, & mut data) ) ;
42+ . for_each ( |f| f ( & mut section, & mut title, & mut data, & mut data_len_excess ) ) ;
4243
4344 println ! ( "{}" , section. join( " " ) ) ;
4445 println ! ( "{}" , title. join( " " ) ) ;
@@ -47,16 +48,26 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4748 Ok ( ( ) )
4849}
4950
50- type ConcatFunc = Box < dyn Fn ( & mut Vec < String > , & mut Vec < String > , & mut Vec < String > ) > ;
51+ type ConcatFunc = Box < dyn Fn ( & mut Vec < String > , & mut Vec < String > , & mut Vec < String > , & mut usize ) > ;
5152
5253#[ allow( dead_code) ]
53- fn concat_helper ( func : impl Fn ( ) -> ( String , String , String ) + ' static ) -> ConcatFunc {
54+ fn concat_helper (
55+ func : impl Fn ( ) -> ( String , String , Vec < ( usize , String ) > ) + ' static ,
56+ ) -> ConcatFunc {
5457 Box :: from (
55- move |section : & mut Vec < String > , title : & mut Vec < String > , data : & mut Vec < String > | {
58+ move |section : & mut Vec < String > ,
59+ title : & mut Vec < String > ,
60+ data : & mut Vec < String > ,
61+ data_len_excess : & mut usize | {
5662 let output = func ( ) ;
5763 section. push ( output. 0 ) ;
5864 title. push ( output. 1 ) ;
59- data. push ( output. 2 ) ;
65+ output. 2 . iter ( ) . for_each ( |( len, value) | {
66+ let len = len - * data_len_excess;
67+ let formatted_value = format ! ( "{:>width$}" , value, width = len) ;
68+ * data_len_excess = formatted_value. len ( ) - len;
69+ data. push ( formatted_value) ;
70+ } ) ;
6071 } ,
6172 )
6273}
@@ -70,19 +81,19 @@ fn up_secs() -> f64 {
7081}
7182
7283#[ cfg( target_os = "linux" ) ]
73- fn get_process_info ( ) -> ( String , String , String ) {
84+ fn get_process_info ( ) -> ( String , String , Vec < ( usize , String ) > ) {
7485 let stat = procfs:: KernelStats :: current ( ) . unwrap ( ) ;
7586 let runnable = stat. procs_running . unwrap_or_default ( ) ;
7687 let blocked = stat. procs_blocked . unwrap_or_default ( ) ;
7788 (
7889 "procs" . into ( ) ,
7990 " r b" . into ( ) ,
80- format ! ( "{:>2} {:>2} " , runnable, blocked) ,
91+ vec ! [ ( 2 , format!( "{} " , runnable) ) , ( 2 , format! ( "{}" , blocked) ) ] ,
8192 )
8293}
8394
8495#[ cfg( target_os = "linux" ) ]
85- fn get_memory_info ( ) -> ( String , String , String ) {
96+ fn get_memory_info ( ) -> ( String , String , Vec < ( usize , String ) > ) {
8697 let memory_info = procfs:: Meminfo :: current ( ) . unwrap ( ) ;
8798 let swap_used = ( memory_info. swap_total - memory_info. swap_free ) / 1024 ;
8899 let free = memory_info. mem_free / 1024 ;
@@ -92,46 +103,49 @@ fn get_memory_info() -> (String, String, String) {
92103 (
93104 "-----------memory----------" . into ( ) ,
94105 " swpd free buff cache" . into ( ) ,
95- format ! ( "{:>6} {:>6} {:>5} {:>6}" , swap_used, free, buffer, cache) ,
106+ vec ! [
107+ ( 6 , format!( "{}" , swap_used) ) ,
108+ ( 6 , format!( "{}" , free) ) ,
109+ ( 6 , format!( "{}" , buffer) ) ,
110+ ( 6 , format!( "{}" , cache) ) ,
111+ ] ,
96112 )
97113}
98114
99115#[ cfg( target_os = "linux" ) ]
100- fn get_swap_info ( ) -> ( String , String , String ) {
116+ fn get_swap_info ( ) -> ( String , String , Vec < ( usize , String ) > ) {
101117 let uptime = up_secs ( ) ;
102118 let vmstat = procfs:: vmstat ( ) . unwrap ( ) ;
103119 let swap_in = vmstat. get ( "pswpin" ) . unwrap ( ) ;
104120 let swap_out = vmstat. get ( "pswpout" ) . unwrap ( ) ;
105121 (
106122 "---swap--" . into ( ) ,
107123 " si so" . into ( ) ,
108- format ! (
109- "{:>2} {:>4}" ,
110- * swap_in as f64 / uptime,
111- * swap_out as f64 / uptime
112- ) ,
124+ vec ! [
125+ ( 4 , format!( "{:.0}" , * swap_in as f64 / uptime) ) ,
126+ ( 4 , format!( "{:.0}" , * swap_out as f64 / uptime) ) ,
127+ ] ,
113128 )
114129}
115130
116131#[ cfg( target_os = "linux" ) ]
117- fn get_io_info ( ) -> ( String , String , String ) {
132+ fn get_io_info ( ) -> ( String , String , Vec < ( usize , String ) > ) {
118133 let uptime = up_secs ( ) ;
119134 let vmstat = procfs:: vmstat ( ) . unwrap ( ) ;
120135 let read_bytes = vmstat. get ( "pgpgin" ) . unwrap ( ) ;
121136 let write_bytes = vmstat. get ( "pgpgout" ) . unwrap ( ) ;
122137 (
123138 "-----io----" . into ( ) ,
124139 " bi bo" . into ( ) ,
125- format ! (
126- "{:>5.0} {:>5.0}" ,
127- * read_bytes as f64 / uptime,
128- * write_bytes as f64 / uptime
129- ) ,
140+ vec ! [
141+ ( 5 , format!( "{:.0}" , * read_bytes as f64 / uptime) ) ,
142+ ( 5 , format!( "{:.0}" , * write_bytes as f64 / uptime) ) ,
143+ ] ,
130144 )
131145}
132146
133147#[ cfg( target_os = "linux" ) ]
134- fn get_system_info ( ) -> ( String , String , String ) {
148+ fn get_system_info ( ) -> ( String , String , Vec < ( usize , String ) > ) {
135149 let uptime = up_secs ( ) ;
136150 let stat = parse_proc_file ( "/proc/stat" ) ;
137151
@@ -148,30 +162,28 @@ fn get_system_info() -> (String, String, String) {
148162 (
149163 "-system--" . into ( ) ,
150164 " in cs" . into ( ) ,
151- format ! (
152- "{:>4.0} {:>4.0}" ,
153- interrupts as f64 / uptime,
154- context_switches as f64 / uptime
155- ) ,
165+ vec ! [
166+ ( 4 , format!( "{:.0}" , interrupts as f64 / uptime) ) ,
167+ ( 4 , format!( "{:.0}" , context_switches as f64 / uptime) ) ,
168+ ] ,
156169 )
157170}
158171
159172#[ cfg( target_os = "linux" ) ]
160- fn get_cpu_info ( ) -> ( String , String , String ) {
173+ fn get_cpu_info ( ) -> ( String , String , Vec < ( usize , String ) > ) {
161174 let cpu_load = CpuLoad :: current ( ) ;
162175
163176 (
164177 "-------cpu-------" . into ( ) ,
165178 "us sy id wa st gu" . into ( ) ,
166- format ! (
167- "{:>2.0} {:>2.0} {:>2.0} {:>2.0} {:>2.0} {:>2.0}" ,
168- cpu_load. user,
169- cpu_load. system,
170- cpu_load. idle,
171- cpu_load. io_wait,
172- cpu_load. steal_time,
173- cpu_load. guest
174- ) ,
179+ vec ! [
180+ ( 2 , format!( "{:.0}" , cpu_load. user) ) ,
181+ ( 2 , format!( "{:.0}" , cpu_load. system) ) ,
182+ ( 2 , format!( "{:.0}" , cpu_load. idle) ) ,
183+ ( 2 , format!( "{:.0}" , cpu_load. io_wait) ) ,
184+ ( 2 , format!( "{:.0}" , cpu_load. steal_time) ) ,
185+ ( 2 , format!( "{:.0}" , cpu_load. guest) ) ,
186+ ] ,
175187 )
176188}
177189
0 commit comments