Skip to content

Commit c67a740

Browse files
authored
Merge pull request #1832 from konstankinollc/master
Fix displaying ETA output for download tracker, make it consistent & treat output as integers.
2 parents 232413c + 0ea79ee commit c67a740

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ test = false # no unit tests
9595
[[bin]]
9696
name = "rustup-init"
9797
path = "src/cli/main.rs"
98-
test = false # no unit tests
9998

10099
[profile.release]
101100
lto = true

src/cli/download_tracker.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,15 @@ impl DownloadTracker {
118118
}
119119
self.prepare_for_new_download();
120120
}
121+
// we're doing modular arithmetic, treat as integer
122+
pub fn from_seconds(sec: u32) -> (u32, u32, u32, u32) {
123+
let d = sec / (24 * 3600);
124+
let h = sec % (24 * 3600) / 3600;
125+
let min = sec % 3600 / 60;
126+
let sec = sec % 60;
127+
128+
(d, h, min, sec)
129+
}
121130
/// Resets the state to be ready for a new download.
122131
fn prepare_for_new_download(&mut self) {
123132
self.content_len = None;
@@ -188,28 +197,11 @@ impl fmt::Display for Duration {
188197
if sec.is_infinite() {
189198
write!(f, "Unknown")
190199
} else {
191-
// we're doing modular arithmetic, treat as integer
192-
let sec = sec as u32;
193-
if sec > 48 * 3600 {
194-
let d = sec / (24 * 3600);
195-
let h = sec % (24 * 3600);
196-
let min = sec % 3600;
197-
let sec = sec % 60;
198-
199-
write!(f, "{:3} days {:2} h {:2} min {:2} s", d, h, min, sec) // XYZ days PQ h RS min TU s
200-
} else if sec > 6_000 {
201-
let h = sec / 3600;
202-
let min = sec % 3600;
203-
let sec = sec % 60;
204-
205-
write!(f, "{:3} h {:2} min {:2} s", h, min, sec) // XYZ h PQ min RS s
206-
} else if sec > 100 {
207-
let min = sec / 60;
208-
let sec = sec % 60;
209-
210-
write!(f, "{:3} min {:2} s", min, sec) // XYZ min PQ s
211-
} else {
212-
write!(f, "{:3.0} s", self.0) // XYZ s
200+
match DownloadTracker::from_seconds(sec as u32) {
201+
(d, h, m, s) if d > 0 => write!(f, "{:3.0}d {:2.0}h {:2.0}m {:2.0}s", d, h, m, s),
202+
(0, h, m, s) if h > 0 => write!(f, "{:2.0}h {:2.0}m {:2.0}s", h, m, s),
203+
(0, 0, m, s) if m > 0 => write!(f, "{:2.0}m {:2.0}s", m, s),
204+
(_, _, _, s) => write!(f, "{:2.0}s", s),
213205
}
214206
}
215207
}
@@ -233,3 +225,24 @@ impl fmt::Display for Size {
233225
}
234226
}
235227
}
228+
229+
#[cfg(test)]
230+
mod tests {
231+
232+
#[test]
233+
fn download_tracker_from_seconds_test() {
234+
use crate::download_tracker::DownloadTracker;
235+
assert_eq!(DownloadTracker::from_seconds(2), (0, 0, 0, 2));
236+
237+
assert_eq!(DownloadTracker::from_seconds(60), (0, 0, 1, 0));
238+
239+
assert_eq!(DownloadTracker::from_seconds(3600), (0, 1, 0, 0));
240+
241+
assert_eq!(DownloadTracker::from_seconds(3600 * 24), (1, 0, 0, 0));
242+
243+
assert_eq!(DownloadTracker::from_seconds(52292), (0, 14, 31, 32));
244+
245+
assert_eq!(DownloadTracker::from_seconds(222292), (2, 13, 44, 52));
246+
}
247+
248+
}

0 commit comments

Comments
 (0)