@@ -42,6 +42,8 @@ pub struct DownloadTracker {
42
42
/// If we have displayed progress, this is the number of characters we
43
43
/// rendered, so we can erase it cleanly.
44
44
displayed_charcount : Option < usize > ,
45
+ /// What units to show progress in
46
+ units : Vec < String > ,
45
47
}
46
48
47
49
impl DownloadTracker {
@@ -56,6 +58,7 @@ impl DownloadTracker {
56
58
last_sec : None ,
57
59
term : term2:: stdout ( ) ,
58
60
displayed_charcount : None ,
61
+ units : vec ! [ "B" . into( ) ; 1 ] ,
59
62
}
60
63
}
61
64
@@ -76,6 +79,15 @@ impl DownloadTracker {
76
79
self . download_finished ( ) ;
77
80
true
78
81
}
82
+ Notification :: Install ( In :: Utils ( Un :: DownloadPushUnits ( units) ) ) => {
83
+ self . push_units ( units. into ( ) ) ;
84
+ true
85
+ }
86
+ Notification :: Install ( In :: Utils ( Un :: DownloadPopUnits ) ) => {
87
+ self . pop_units ( ) ;
88
+ true
89
+ }
90
+
79
91
_ => false ,
80
92
}
81
93
}
@@ -139,11 +151,13 @@ impl DownloadTracker {
139
151
}
140
152
/// Display the tracked download information to the terminal.
141
153
fn display ( & mut self ) {
142
- let total_h = Size ( self . total_downloaded ) ;
154
+ // Panic if someone pops the default bytes unit...
155
+ let units = & self . units . last ( ) . unwrap ( ) ;
156
+ let total_h = Size ( self . total_downloaded , units) ;
143
157
let sum = self . downloaded_last_few_secs . iter ( ) . fold ( 0 , |a, & v| a + v) ;
144
158
let len = self . downloaded_last_few_secs . len ( ) ;
145
159
let speed = if len > 0 { sum / len } else { 0 } ;
146
- let speed_h = Size ( speed) ;
160
+ let speed_h = Size ( speed, units ) ;
147
161
let elapsed_h = Duration ( precise_time_s ( ) - self . start_sec ) ;
148
162
149
163
// First, move to the start of the current line and clear it.
@@ -163,7 +177,7 @@ impl DownloadTracker {
163
177
164
178
let output = match self . content_len {
165
179
Some ( content_len) => {
166
- let content_len_h = Size ( content_len) ;
180
+ let content_len_h = Size ( content_len, units ) ;
167
181
let content_len = content_len as f64 ;
168
182
let percent = ( self . total_downloaded as f64 / content_len) * 100. ;
169
183
let remaining = content_len - self . total_downloaded as f64 ;
@@ -184,6 +198,14 @@ impl DownloadTracker {
184
198
let _ = self . term . flush ( ) ;
185
199
self . displayed_charcount = Some ( output. chars ( ) . count ( ) ) ;
186
200
}
201
+
202
+ pub fn push_units ( & mut self , new_units : String ) {
203
+ self . units . push ( new_units) ;
204
+ }
205
+
206
+ pub fn pop_units ( & mut self ) {
207
+ self . units . pop ( ) ;
208
+ }
187
209
}
188
210
189
211
/// Human readable representation of duration(seconds).
@@ -207,21 +229,21 @@ impl fmt::Display for Duration {
207
229
}
208
230
}
209
231
210
- /// Human readable size (bytes )
211
- struct Size ( usize ) ;
232
+ /// Human readable size (some units )
233
+ struct Size < ' a > ( usize , & ' a str ) ;
212
234
213
- impl fmt:: Display for Size {
235
+ impl < ' a > fmt:: Display for Size < ' a > {
214
236
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
215
- const KIB : f64 = 1024.0 ;
216
- const MIB : f64 = KIB * KIB ;
237
+ const KI : f64 = 1024.0 ;
238
+ const MI : f64 = KI * KI ;
217
239
let size = self . 0 as f64 ;
218
240
219
- if size >= MIB {
220
- write ! ( f, "{:5.1} MiB " , size / MIB ) // XYZ.P MiB
221
- } else if size >= KIB {
222
- write ! ( f, "{:5.1} KiB " , size / KIB )
241
+ if size >= MI {
242
+ write ! ( f, "{:5.1} Mi{} " , size / MI , self . 1 ) // XYZ.P Mi
243
+ } else if size >= KI {
244
+ write ! ( f, "{:5.1} Ki{} " , size / KI , self . 1 )
223
245
} else {
224
- write ! ( f, "{:3.0} B " , size)
246
+ write ! ( f, "{:3.0} {} " , size, self . 1 )
225
247
}
226
248
}
227
249
}
0 commit comments