mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-29 08:30:04 +08:00
add copy_io_with_head
This commit is contained in:
@@ -9,8 +9,14 @@ use super::util_msg::print_lastline;
|
|||||||
|
|
||||||
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
|
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
|
||||||
|
|
||||||
|
|
||||||
pub fn copy_io<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W, total: i64) -> io::Result<u64>
|
pub fn copy_io<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W, total: i64) -> io::Result<u64>
|
||||||
where R: io::Read, W: io::Write {
|
where R: io::Read, W: io::Write {
|
||||||
|
copy_io_with_head(reader, writer, total, "Downloading")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn copy_io_with_head<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W, total: i64, head: &str) -> io::Result<u64>
|
||||||
|
where R: io::Read, W: io::Write {
|
||||||
//let written_cell = RefCell::new(0u64);
|
//let written_cell = RefCell::new(0u64);
|
||||||
let start = SystemTime::now();
|
let start = SystemTime::now();
|
||||||
let written = copy_io_callback(reader, writer, total, &|total, written, _len| {
|
let written = copy_io_callback(reader, writer, total, &|total, written, _len| {
|
||||||
@@ -22,12 +28,14 @@ pub fn copy_io<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W, total: i64)
|
|||||||
download_speed = format!("{}/s", get_display_size((written / cost) as i64));
|
download_speed = format!("{}/s", get_display_size((written / cost) as i64));
|
||||||
}
|
}
|
||||||
if total > 0 {
|
if total > 0 {
|
||||||
print_lastline(&format!("Downloading, Total: {}, Downloaded: {}, Speed: {}",
|
print_lastline(&format!("{}, Total: {}, Finished: {}, Speed: {}",
|
||||||
|
head,
|
||||||
get_display_size(total),
|
get_display_size(total),
|
||||||
get_display_size(written as i64),
|
get_display_size(written as i64),
|
||||||
download_speed));
|
download_speed));
|
||||||
} else {
|
} else {
|
||||||
print_lastline(&format!("Downloading, Downloaded: {}, Speed: {}",
|
print_lastline(&format!("{}, Finished: {}, Speed: {}",
|
||||||
|
head,
|
||||||
get_display_size(written as i64),
|
get_display_size(written as i64),
|
||||||
download_speed));
|
download_speed));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user