Skip to content

Commit d58aa8a

Browse files
committed
feat: show transfer rate and total bytes received
1 parent 77d993c commit d58aa8a

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

src/cargo/sources/git/utils.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::env;
1515
use std::fmt;
1616
use std::path::{Path, PathBuf};
1717
use std::process::Command;
18+
use std::time::{Duration, Instant};
1819
use url::Url;
1920

2021
fn serialize_str<T, S>(t: &T, s: S) -> Result<S::Ok, S::Error>
@@ -694,12 +695,40 @@ pub fn with_fetch_options(
694695
let mut progress = Progress::new("Fetch", config);
695696
network::with_retry(config, || {
696697
with_authentication(url, git_config, |f| {
698+
let mut last_recv = 0.0; // in Byte
699+
let mut last_rate = 0.0; // in Byte/s
700+
let mut last_update = Instant::now();
697701
let mut rcb = git2::RemoteCallbacks::new();
698702
rcb.credentials(f);
699-
700703
rcb.transfer_progress(|stats| {
704+
let indexed_deltas = stats.indexed_deltas();
705+
let msg = if indexed_deltas > 0 {
706+
// Resolving deltas.
707+
format!(" ({}/{})", indexed_deltas, stats.total_deltas())
708+
} else {
709+
// Receiving objects.
710+
let duration = last_update.elapsed();
711+
let (recv, rate) = if duration > Duration::from_secs(1) {
712+
let recv = stats.received_bytes() as f32;
713+
let rate = (recv - last_recv) / duration.as_secs_f32();
714+
last_recv = recv;
715+
last_rate = rate;
716+
last_update = Instant::now();
717+
(recv, rate)
718+
} else {
719+
(last_recv, last_rate)
720+
};
721+
fn format_bytes(bytes: f32) -> (&'static str, f32) {
722+
static UNITS: [&str; 5] = ["", "K", "M", "G", "T"];
723+
let i = (bytes.log2() / 10.0).min(4.0) as usize;
724+
(UNITS[i], bytes / 1024_f32.powi(i as i32))
725+
}
726+
let (rate_unit, rate) = format_bytes(rate);
727+
let (unit, recv) = format_bytes(recv);
728+
format!(" | {:.2}{}iB | {:.2}{}iB/s", recv, unit, rate, rate_unit)
729+
};
701730
progress
702-
.tick(stats.indexed_objects(), stats.total_objects(), "")
731+
.tick(stats.indexed_objects(), stats.total_objects(), &msg)
703732
.is_ok()
704733
});
705734

0 commit comments

Comments
 (0)