11use crate :: color:: { ColoredString , Colors , Elem } ;
2+ use crate :: flags:: size_separator:: SizeSeparator ;
23use crate :: flags:: { Flags , SizeFlag } ;
3- use num_format:: { Locale , ToFormattedString as _} ;
4+
5+ use num_format:: ToFormattedString as _;
46use std:: fs:: Metadata ;
57
68const KB : u64 = 1024 ;
@@ -37,36 +39,28 @@ impl Size {
3739 self . bytes
3840 }
3941
40- #[ cfg( windows) ]
41- fn format_bytes_with_separator ( & self ) -> String {
42- self . bytes . to_formatted_string ( & Locale :: en)
43- }
44-
45- #[ cfg( not( windows) ) ]
46- fn format_bytes_with_separator ( & self ) -> String {
47- use num_format:: SystemLocale ;
48-
49- if let Ok ( system_locale) = SystemLocale :: default ( ) {
50- self . bytes . to_formatted_string ( & system_locale)
42+ fn format_size ( & self , number : f64 , separator : SizeSeparator ) -> String {
43+ let formatted = if let Some ( locale) = separator. get_locale ( ) {
44+ // Convert to integer for thousands separator formatting
45+ let int_number = number as u64 ;
46+ int_number. to_formatted_string ( & locale)
5147 } else {
52- self . bytes . to_formatted_string ( & Locale :: en)
53- }
48+ format ! ( "{0:.1$}" , number, if number < 10.0 { 1 } else { 0 } )
49+ } ;
50+
51+ formatted
5452 }
5553
56- fn format_bytes ( & self , flags : & Flags ) -> String {
57- if flags . size == SizeFlag :: BytesWithSeparator {
58- self . format_bytes_with_separator ( )
54+ fn format_bytes ( & self , separator : SizeSeparator ) -> String {
55+ if let Some ( locale ) = separator . get_locale ( ) {
56+ self . bytes . to_formatted_string ( & locale )
5957 } else {
6058 self . bytes . to_string ( )
6159 }
6260 }
6361
64- fn format_size ( & self , number : f64 ) -> String {
65- format ! ( "{0:.1$}" , number, if number < 10.0 { 1 } else { 0 } )
66- }
67-
6862 fn get_unit ( & self , flags : & Flags ) -> Unit {
69- if matches ! ( flags. size, SizeFlag :: Bytes | SizeFlag :: BytesWithSeparator ) {
63+ if flags. size == SizeFlag :: Bytes {
7064 return Unit :: Byte ;
7165 }
7266
@@ -135,11 +129,29 @@ impl Size {
135129 let unit = self . get_unit ( flags) ;
136130
137131 match unit {
138- Unit :: Byte => self . format_bytes ( flags) ,
139- Unit :: Kilo => self . format_size ( ( ( self . bytes as f64 / KB as f64 ) * 10.0 ) . round ( ) / 10.0 ) ,
140- Unit :: Mega => self . format_size ( ( ( self . bytes as f64 / MB as f64 ) * 10.0 ) . round ( ) / 10.0 ) ,
141- Unit :: Giga => self . format_size ( ( ( self . bytes as f64 / GB as f64 ) * 10.0 ) . round ( ) / 10.0 ) ,
142- Unit :: Tera => self . format_size ( ( ( self . bytes as f64 / TB as f64 ) * 10.0 ) . round ( ) / 10.0 ) ,
132+ Unit :: Byte => {
133+ if let Some ( separator) = & flags. size_separator {
134+ self . format_bytes ( separator. clone ( ) )
135+ } else {
136+ self . format_bytes ( SizeSeparator :: default ( ) )
137+ }
138+ }
139+ Unit :: Kilo => self . format_size (
140+ ( ( self . bytes as f64 / KB as f64 ) * 10.0 ) . round ( ) / 10.0 ,
141+ SizeSeparator :: default ( ) ,
142+ ) ,
143+ Unit :: Mega => self . format_size (
144+ ( ( self . bytes as f64 / MB as f64 ) * 10.0 ) . round ( ) / 10.0 ,
145+ SizeSeparator :: default ( ) ,
146+ ) ,
147+ Unit :: Giga => self . format_size (
148+ ( ( self . bytes as f64 / GB as f64 ) * 10.0 ) . round ( ) / 10.0 ,
149+ SizeSeparator :: default ( ) ,
150+ ) ,
151+ Unit :: Tera => self . format_size (
152+ ( ( self . bytes as f64 / TB as f64 ) * 10.0 ) . round ( ) / 10.0 ,
153+ SizeSeparator :: default ( ) ,
154+ ) ,
143155 }
144156 }
145157
@@ -167,7 +179,7 @@ impl Size {
167179 Unit :: Giga => String :: from ( 'G' ) ,
168180 Unit :: Tera => String :: from ( 'T' ) ,
169181 } ,
170- SizeFlag :: Bytes | SizeFlag :: BytesWithSeparator => String :: from ( "" ) ,
182+ SizeFlag :: Bytes => String :: from ( "" ) ,
171183 }
172184 }
173185}
@@ -176,6 +188,7 @@ impl Size {
176188mod test {
177189 use super :: { Size , GB , KB , MB , TB } ;
178190 use crate :: color:: { Colors , ThemeOption } ;
191+ use crate :: flags:: size_separator:: SizeSeparator ;
179192 use crate :: flags:: { Flags , SizeFlag } ;
180193
181194 #[ test]
@@ -204,7 +217,8 @@ mod test {
204217 assert_eq ! ( size. value_string( & flags) . as_str( ) , "44040192" ) ;
205218 assert_eq ! ( size. unit_string( & flags) . as_str( ) , "" ) ;
206219
207- flags. size = SizeFlag :: BytesWithSeparator ;
220+ flags. size = SizeFlag :: Bytes ;
221+ flags. size_separator = Some ( SizeSeparator :: new ( Some ( "en" . to_string ( ) ) ) ) ;
208222 assert_eq ! ( size. value_string( & flags) . as_str( ) , "44,040,192" ) ;
209223 assert_eq ! ( size. unit_string( & flags) . as_str( ) , "" ) ;
210224 }
@@ -224,7 +238,8 @@ mod test {
224238 assert_eq ! ( size. value_string( & flags) . as_str( ) , "44040192" ) ;
225239 assert_eq ! ( size. unit_string( & flags) . as_str( ) , "" ) ;
226240
227- flags. size = SizeFlag :: BytesWithSeparator ;
241+ flags. size = SizeFlag :: Bytes ;
242+ flags. size_separator = Some ( SizeSeparator :: new ( Some ( "en" . to_string ( ) ) ) ) ;
228243 assert_eq ! ( size. value_string( & flags) . as_str( ) , "44,040,192" ) ;
229244 assert_eq ! ( size. unit_string( & flags) . as_str( ) , "" ) ;
230245 }
0 commit comments