Display transfer speed

This commit is contained in:
ChristianVisintin
2020-12-18 17:06:17 +01:00
parent 1b99d63c47
commit 2a52a19552
5 changed files with 59 additions and 24 deletions

View File

@@ -23,7 +23,8 @@ Released on ??
- MacOS: `/Users/Alice/Library/Application Support/termscp/bookmarks.toml`
- Enhancements:
- File explorer:
- Log how long it took to upload/download a file
- Log how long it took to upload/download a file and the transfer speed
- Display in progress bar the transfer speed (bytes/seconds)
- Bugfix:
- File mode of file on remote is now reported on local file after being downloaded (unix, linux, macos only)
- Scp: when username was not provided, it didn't fallback to current username

View File

@@ -381,16 +381,22 @@ impl FileTransferActivity {
/// Draw progress popup
pub(super) fn draw_popup_progress(&self, text: String) -> Gauge {
// Calculate ETA
let elapsed_secs: u64 = self.transfer.started.elapsed().as_secs();
let eta: String = match self.transfer.progress as u64 {
0 => String::from("--:--"), // NOTE: would divide by 0 :D
_ => {
let elapsed_secs: u64 = self.transfer.started.elapsed().as_secs();
let eta: u64 =
((elapsed_secs * 100) / (self.transfer.progress as u64)) - elapsed_secs;
format!("{:0width$}:{:0width$}", (eta / 60), (eta % 60), width = 2)
}
};
let label = format!("{:.2}% - ETA {}", self.transfer.progress, eta);
// Calculate bytes/s
let label = format!(
"{:.2}% - ETA {} ({}/s)",
self.transfer.progress,
eta,
ByteSize(self.transfer.bytes_per_second())
);
Gauge::default()
.block(Block::default().borders(Borders::ALL).title(text))
.gauge_style(

View File

@@ -69,18 +69,4 @@ impl FileTransferActivity {
InputField::Logs => InputField::Explorer,
}
}
/// ### set_progress
///
/// Calculate progress percentage based on current progress
pub(super) fn set_progress(&mut self, it: usize, sz: usize) {
let mut prog: f64 = ((it as f64) * 100.0) / (sz as f64);
// Check value
if prog > 100.0 {
prog = 100.0;
} else if prog < 0.0 {
prog = 0.0;
}
self.transfer.progress = prog;
}
}

View File

@@ -209,9 +209,11 @@ impl LogRecord {
///
/// TransferStates contains the states related to the transfer process
struct TransferStates {
pub progress: f64, // Current read/write progress (percentage)
pub started: Instant, // Instant the transfer process started
pub aborted: bool, // Describes whether the transfer process has been aborted
pub progress: f64, // Current read/write progress (percentage)
pub started: Instant, // Instant the transfer process started
pub aborted: bool, // Describes whether the transfer process has been aborted
pub bytes_written: usize, // Bytes written during transfer
pub bytes_total: usize, // Total bytes to write
}
impl TransferStates {
@@ -223,6 +225,8 @@ impl TransferStates {
progress: 0.0,
started: Instant::now(),
aborted: false,
bytes_written: 0,
bytes_total: 0,
}
}
@@ -233,6 +237,36 @@ impl TransferStates {
self.progress = 0.0;
self.started = Instant::now();
self.aborted = false;
self.bytes_written = 0;
self.bytes_total = 0;
}
/// ### set_progress
///
/// Calculate progress percentage based on current progress
pub fn set_progress(&mut self, w: usize, sz: usize) {
self.bytes_written = w;
self.bytes_total = sz;
let mut prog: f64 = ((self.bytes_written as f64) * 100.0) / (self.bytes_total as f64);
// Check value
if prog > 100.0 {
prog = 100.0;
} else if prog < 0.0 {
prog = 0.0;
}
self.progress = prog;
}
/// ### byte_per_second
///
/// Calculate bytes per second
pub fn bytes_per_second(&self) -> u64 {
// bytes_written : elapsed_secs = x : 1
let elapsed_secs: u64 = self.started.elapsed().as_secs();
match elapsed_secs {
0 => 0, // NOTE: would divide by 0 :D
_ => self.bytes_written as u64 / elapsed_secs,
}
}
}

View File

@@ -19,9 +19,15 @@
*
*/
// Deps
extern crate bytesize;
// Locals
use super::{FileTransferActivity, FsEntry, InputMode, LogLevel, PopupType};
use crate::utils::fmt_millis;
// Ext
use bytesize::ByteSize;
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::time::Instant;
@@ -221,7 +227,7 @@ impl FileTransferActivity {
}
}
// Increase progress
self.set_progress(total_bytes_written, file_size);
self.transfer.set_progress(total_bytes_written, file_size);
// Draw only if a significant progress has been made (performance improvement)
if last_progress_val < self.transfer.progress - 1.0 {
// Draw
@@ -240,10 +246,11 @@ impl FileTransferActivity {
self.log(
LogLevel::Info,
format!(
"Saved file \"{}\" to \"{}\" (took {} seconds)",
"Saved file \"{}\" to \"{}\" (took {} seconds; at {}/s)",
file.abs_path.display(),
remote_path.display(),
fmt_millis(self.transfer.started.elapsed()),
ByteSize(self.transfer.bytes_per_second()),
)
.as_ref(),
);
@@ -504,7 +511,7 @@ impl FileTransferActivity {
}
}
// Set progress
self.set_progress(total_bytes_written, file.size);
self.transfer.set_progress(total_bytes_written, file.size);
// Draw only if a significant progress has been made (performance improvement)
if last_progress_val < self.transfer.progress - 1.0 {
// Draw
@@ -550,10 +557,11 @@ impl FileTransferActivity {
self.log(
LogLevel::Info,
format!(
"Saved file \"{}\" to \"{}\" (took {} seconds)",
"Saved file \"{}\" to \"{}\" (took {} seconds; at {}/s)",
file.abs_path.display(),
local_file_path.display(),
fmt_millis(self.transfer.started.elapsed()),
ByteSize(self.transfer.bytes_per_second()),
)
.as_ref(),
);