From 93d5806e490910b070ae5a88631f9a309a4d11f2 Mon Sep 17 00:00:00 2001 From: amatgil Date: Mon, 3 Oct 2022 00:56:32 +0200 Subject: [PATCH 1/2] Attempted to add speed indicator on both sender and receiver (untested) --- src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3779a47..29f34ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -411,7 +411,8 @@ pub fn sender(args: &Vec, on_progress: F) { sc.write_safe(&buf[..read]).expect("send error"); bytes_sent += read as u64; if (bytes_sent % (br * 20) as u64) < (br as u64) { - print!("\r\x1b[KSent {} bytes", bytes_sent); + print!("\r\x1b[KSent {} bytes; Speed: {} kb/s", + bytes_sent, (read / 1000) / last_update as usize ); stdout().flush().unwrap(); } if unix_millis() - last_update > 100 { @@ -478,7 +479,8 @@ pub fn receiver(args: &Vec, on_progress: F) { file.flush().expect("file flush error"); bytes_received += amount as u64; if (bytes_received % (br * 20) as u64) < (br as u64) { - print!("\r\x1b[KReceived {} bytes", bytes_received); + print!("\r\x1b[KSent {} bytes; Speed: {} kb/s", + bytes_sent, (read / 1000) / last_update as usize ); stdout().flush().unwrap(); } if unix_millis() - last_update > 100 { From 15c4327c85810624ef70d0088cdf5ed199086e3f Mon Sep 17 00:00:00 2001 From: amatgil Date: Mon, 3 Oct 2022 18:22:03 +0200 Subject: [PATCH 2/2] Should be working correctly :)) --- src/main.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 29f34ee..03d6318 100644 --- a/src/main.rs +++ b/src/main.rs @@ -399,6 +399,7 @@ pub fn sender(args: &Vec, on_progress: F) { sc.write_safe(&len.to_be_bytes()) .expect("unable to send file length"); println!("Length: {}", &len); + let mut time_elapsed = unix_millis(); loop { let read = file.read(&mut buf).expect("file read error"); if read == 0 && !env::var("QFT_STREAM").is_ok() { @@ -412,8 +413,9 @@ pub fn sender(args: &Vec, on_progress: F) { bytes_sent += read as u64; if (bytes_sent % (br * 20) as u64) < (br as u64) { print!("\r\x1b[KSent {} bytes; Speed: {} kb/s", - bytes_sent, (read / 1000) / last_update as usize ); + bytes_sent, br as usize * 20 / (unix_millis() - time_elapsed) as usize ); stdout().flush().unwrap(); + time_elapsed = unix_millis(); } if unix_millis() - last_update > 100 { on_progress((bytes_sent + begin) as f32 / len as f32); @@ -466,6 +468,7 @@ pub fn receiver(args: &Vec, on_progress: F) { ]); file.set_len(len).expect("unable to set file length"); println!("Length: {}", &len); + let mut time_elapsed = unix_millis(); loop { let (mbuf, amount) = sc.read_safe(buf).expect("read error"); let buf = &mbuf.leak()[..amount]; @@ -479,9 +482,10 @@ pub fn receiver(args: &Vec, on_progress: F) { file.flush().expect("file flush error"); bytes_received += amount as u64; if (bytes_received % (br * 20) as u64) < (br as u64) { - print!("\r\x1b[KSent {} bytes; Speed: {} kb/s", - bytes_sent, (read / 1000) / last_update as usize ); + print!("\r\x1b[KReceived {} bytes; Speed: {} kb/s", + bytes_received, br as usize * 20 / (unix_millis() - time_elapsed) as usize ); stdout().flush().unwrap(); + time_elapsed = unix_millis(); } if unix_millis() - last_update > 100 { on_progress((bytes_received + begin) as f32 / len as f32);