66#[ cfg( target_os = "linux" ) ]
77use crate :: { CpuLoad , Meminfo , ProcData } ;
88use clap:: ArgMatches ;
9+ use uucore:: error:: USimpleError ;
910
1011#[ cfg( target_os = "linux" ) ]
1112pub type Picker = (
@@ -64,6 +65,22 @@ pub fn get_pickers(matches: &ArgMatches) -> Vec<Picker> {
6465 ]
6566}
6667
68+ fn with_unit ( x : u64 , arg : & ArgMatches ) -> u64 {
69+ if let Some ( unit) = arg. get_one :: < String > ( "unit" ) {
70+ return match unit. as_str ( ) {
71+ "k" => x / bytesize:: KB ,
72+ "K" => x / bytesize:: KIB ,
73+ "m" => x / bytesize:: MB ,
74+ "M" => x / bytesize:: MIB ,
75+ _ => panic ! (
76+ "{:?}" ,
77+ USimpleError :: new( 1 , "-S requires k, K, m or M (default is KiB)" , )
78+ ) ,
79+ } ;
80+ }
81+ x / bytesize:: KIB
82+ }
83+
6784#[ cfg( target_os = "linux" ) ]
6885fn concat_helper (
6986 title : ( String , String ) ,
@@ -123,16 +140,18 @@ fn get_memory_info(
123140 _proc_data_before : Option < & ProcData > ,
124141 matches : & ArgMatches ,
125142) -> Vec < ( usize , String ) > {
126- use bytesize:: * ;
127143 let len = if matches. get_flag ( "wide" ) { 12 } else { 6 } ;
128144 let memory_info = Meminfo :: from_proc_map ( & proc_data. meminfo ) ;
129145
130- let swap_used = ( memory_info. swap_total - memory_info. swap_free ) . as_u64 ( ) / KB ;
131- let free = memory_info. mem_free . as_u64 ( ) / KB ;
146+ let swap_used = with_unit (
147+ ( memory_info. swap_total - memory_info. swap_free ) . as_u64 ( ) ,
148+ matches,
149+ ) ;
150+ let free = with_unit ( memory_info. mem_free . as_u64 ( ) , matches) ;
132151
133152 if matches. get_flag ( "active" ) {
134- let inactive = memory_info. inactive . as_u64 ( ) / KB ;
135- let active = memory_info. active . as_u64 ( ) / KB ;
153+ let inactive = with_unit ( memory_info. inactive . as_u64 ( ) , matches ) ;
154+ let active = with_unit ( memory_info. active . as_u64 ( ) , matches ) ;
136155 return vec ! [
137156 ( len, format!( "{}" , swap_used) ) ,
138157 ( len, format!( "{}" , free) ) ,
@@ -141,8 +160,8 @@ fn get_memory_info(
141160 ] ;
142161 }
143162
144- let buffer = memory_info. buffers . as_u64 ( ) / KB ;
145- let cache = memory_info. cached . as_u64 ( ) / KB ;
163+ let buffer = with_unit ( memory_info. buffers . as_u64 ( ) , matches ) ;
164+ let cache = with_unit ( memory_info. cached . as_u64 ( ) , matches ) ;
146165
147166 vec ! [
148167 ( len, format!( "{}" , swap_used) ) ,
0 commit comments