55
66#[ cfg( target_os = "linux" ) ]
77use crate :: { CpuLoad , Meminfo , ProcData } ;
8+ use clap:: ArgMatches ;
89
910#[ cfg( target_os = "linux" ) ]
1011pub type Picker = (
1112 ( String , String ) ,
12- Box < dyn Fn ( & ProcData , Option < & ProcData > , & mut Vec < String > , & mut usize ) > ,
13+ Box < dyn Fn ( & ProcData , Option < & ProcData > , & ArgMatches , & mut Vec < String > , & mut usize ) > ,
1314) ;
1415
1516#[ cfg( target_os = "linux" ) ]
16- pub fn get_pickers ( ) -> Vec < Picker > {
17+ pub fn get_pickers ( matches : & ArgMatches ) -> Vec < Picker > {
18+ let wide = matches. get_flag ( "wide" ) ;
1719 vec ! [
18- concat_helper( ( "procs" . into( ) , " r b" . into( ) ) , get_process_info) ,
1920 concat_helper(
20- (
21- "-----------memory----------" . into( ) ,
22- " swpd free buff cache" . into( ) ,
23- ) ,
21+ if wide {
22+ ( "--procs--" . into( ) , " r b" . into( ) )
23+ } else {
24+ ( "procs" . into( ) , " r b" . into( ) )
25+ } ,
26+ get_process_info,
27+ ) ,
28+ concat_helper(
29+ if wide {
30+ (
31+ "-----------------------memory----------------------" . into( ) ,
32+ if matches. get_flag( "active" ) {
33+ " swpd free inact active" . into( )
34+ } else {
35+ " swpd free buff cache" . into( )
36+ } ,
37+ )
38+ } else {
39+ (
40+ "-----------memory----------" . into( ) ,
41+ if matches. get_flag( "active" ) {
42+ " swpd free inact active" . into( )
43+ } else {
44+ " swpd free buff cache" . into( )
45+ } ,
46+ )
47+ } ,
2448 get_memory_info,
2549 ) ,
2650 concat_helper( ( "---swap--" . into( ) , " si so" . into( ) ) , get_swap_info) ,
2751 concat_helper( ( "-----io----" . into( ) , " bi bo" . into( ) ) , get_io_info) ,
2852 concat_helper( ( "-system--" . into( ) , " in cs" . into( ) ) , get_system_info) ,
2953 concat_helper(
30- ( "-------cpu-------" . into( ) , "us sy id wa st gu" . into( ) ) ,
54+ if wide {
55+ (
56+ "----------cpu----------" . into( ) ,
57+ " us sy id wa st gu" . into( ) ,
58+ )
59+ } else {
60+ ( "-------cpu-------" . into( ) , "us sy id wa st gu" . into( ) )
61+ } ,
3162 get_cpu_info,
3263 ) ,
3364 ]
@@ -36,16 +67,17 @@ pub fn get_pickers() -> Vec<Picker> {
3667#[ cfg( target_os = "linux" ) ]
3768fn concat_helper (
3869 title : ( String , String ) ,
39- func : impl Fn ( & ProcData , Option < & ProcData > ) -> Vec < ( usize , String ) > + ' static ,
70+ func : impl Fn ( & ProcData , Option < & ProcData > , & ArgMatches ) -> Vec < ( usize , String ) > + ' static ,
4071) -> Picker {
4172 (
4273 title,
4374 Box :: from (
4475 move |proc_data : & ProcData ,
4576 proc_data_before : Option < & ProcData > ,
77+ matches : & ArgMatches ,
4678 data : & mut Vec < String > ,
4779 data_len_excess : & mut usize | {
48- let output = func ( proc_data, proc_data_before) ;
80+ let output = func ( proc_data, proc_data_before, matches ) ;
4981 output. iter ( ) . for_each ( |( len, value) | {
5082 let len = if * data_len_excess > * len {
5183 0
@@ -76,39 +108,55 @@ macro_rules! diff {
76108fn get_process_info (
77109 proc_data : & ProcData ,
78110 _proc_data_before : Option < & ProcData > ,
111+ matches : & ArgMatches ,
79112) -> Vec < ( usize , String ) > {
80113 let runnable = proc_data. stat . get ( "procs_running" ) . unwrap ( ) ;
81114 let blocked = proc_data. stat . get ( "procs_blocked" ) . unwrap ( ) ;
115+ let len = if matches. get_flag ( "wide" ) { 4 } else { 2 } ;
82116
83- vec ! [ ( 2 , runnable. to_string( ) ) , ( 2 , blocked. to_string( ) ) ]
117+ vec ! [ ( len , runnable. to_string( ) ) , ( len , blocked. to_string( ) ) ]
84118}
85119
86120#[ cfg( target_os = "linux" ) ]
87121fn get_memory_info (
88122 proc_data : & ProcData ,
89123 _proc_data_before : Option < & ProcData > ,
124+ matches : & ArgMatches ,
90125) -> Vec < ( usize , String ) > {
91126 use bytesize:: * ;
92-
127+ let len = if matches . get_flag ( "wide" ) { 12 } else { 6 } ;
93128 let memory_info = Meminfo :: from_proc_map ( & proc_data. meminfo ) ;
94129
95130 let swap_used = ( memory_info. swap_total - memory_info. swap_free ) . as_u64 ( ) / KB ;
96131 let free = memory_info. mem_free . as_u64 ( ) / KB ;
132+
133+ if matches. get_flag ( "active" ) {
134+ let inactive = memory_info. inactive . as_u64 ( ) / KB ;
135+ let active = memory_info. active . as_u64 ( ) / KB ;
136+ return vec ! [
137+ ( len, format!( "{}" , swap_used) ) ,
138+ ( len, format!( "{}" , free) ) ,
139+ ( len, format!( "{}" , inactive) ) ,
140+ ( len, format!( "{}" , active) ) ,
141+ ] ;
142+ }
143+
97144 let buffer = memory_info. buffers . as_u64 ( ) / KB ;
98145 let cache = memory_info. cached . as_u64 ( ) / KB ;
99146
100147 vec ! [
101- ( 6 , format!( "{}" , swap_used) ) ,
102- ( 6 , format!( "{}" , free) ) ,
103- ( 6 , format!( "{}" , buffer) ) ,
104- ( 6 , format!( "{}" , cache) ) ,
148+ ( len , format!( "{}" , swap_used) ) ,
149+ ( len , format!( "{}" , free) ) ,
150+ ( len , format!( "{}" , buffer) ) ,
151+ ( len , format!( "{}" , cache) ) ,
105152 ]
106153}
107154
108155#[ cfg( target_os = "linux" ) ]
109156fn get_swap_info (
110157 proc_data : & ProcData ,
111158 proc_data_before : Option < & ProcData > ,
159+ _matches : & ArgMatches ,
112160) -> Vec < ( usize , String ) > {
113161 let period = diff ! ( proc_data, proc_data_before, uptime. 0 ) ;
114162 let swap_in = diff ! (
@@ -129,7 +177,11 @@ fn get_swap_info(
129177}
130178
131179#[ cfg( target_os = "linux" ) ]
132- fn get_io_info ( proc_data : & ProcData , proc_data_before : Option < & ProcData > ) -> Vec < ( usize , String ) > {
180+ fn get_io_info (
181+ proc_data : & ProcData ,
182+ proc_data_before : Option < & ProcData > ,
183+ _matches : & ArgMatches ,
184+ ) -> Vec < ( usize , String ) > {
133185 let period = diff ! ( proc_data, proc_data_before, uptime. 0 ) ;
134186 let read_bytes = diff ! (
135187 proc_data,
@@ -152,6 +204,7 @@ fn get_io_info(proc_data: &ProcData, proc_data_before: Option<&ProcData>) -> Vec
152204fn get_system_info (
153205 proc_data : & ProcData ,
154206 proc_data_before : Option < & ProcData > ,
207+ _matches : & ArgMatches ,
155208) -> Vec < ( usize , String ) > {
156209 let period = diff ! ( proc_data, proc_data_before, uptime. 0 ) ;
157210
@@ -182,15 +235,18 @@ fn get_system_info(
182235fn get_cpu_info (
183236 proc_data : & ProcData ,
184237 _proc_data_before : Option < & ProcData > ,
238+ matches : & ArgMatches ,
185239) -> Vec < ( usize , String ) > {
240+ let len = if matches. get_flag ( "wide" ) { 3 } else { 2 } ;
241+
186242 let cpu_load = CpuLoad :: from_proc_map ( & proc_data. stat ) ;
187243
188244 vec ! [
189- ( 2 , format!( "{:.0}" , cpu_load. user) ) ,
190- ( 2 , format!( "{:.0}" , cpu_load. system) ) ,
191- ( 2 , format!( "{:.0}" , cpu_load. idle) ) ,
192- ( 2 , format!( "{:.0}" , cpu_load. io_wait) ) ,
193- ( 2 , format!( "{:.0}" , cpu_load. steal_time) ) ,
194- ( 2 , format!( "{:.0}" , cpu_load. guest) ) ,
245+ ( len , format!( "{:.0}" , cpu_load. user) ) ,
246+ ( len , format!( "{:.0}" , cpu_load. system) ) ,
247+ ( len , format!( "{:.0}" , cpu_load. idle) ) ,
248+ ( len , format!( "{:.0}" , cpu_load. io_wait) ) ,
249+ ( len , format!( "{:.0}" , cpu_load. steal_time) ) ,
250+ ( len , format!( "{:.0}" , cpu_load. guest) ) ,
195251 ]
196252}
0 commit comments