Speed indicator #5

Merged
amatgil merged 2 commits from main into main 2022-10-03 18:44:31 +02:00

View file

@ -399,6 +399,7 @@ pub fn sender<F: Fn(f32)>(args: &Vec<String>, 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() {
@ -411,8 +412,10 @@ pub fn sender<F: Fn(f32)>(args: &Vec<String>, 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, 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);
@ -465,6 +468,7 @@ pub fn receiver<F: Fn(f32)>(args: &Vec<String>, 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];
@ -478,8 +482,10 @@ pub fn receiver<F: Fn(f32)>(args: &Vec<String>, 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[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);