1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 07:30:05 +08:00

add src/util_io.rs, src/util_os.rs

This commit is contained in:
2019-08-06 08:43:29 +08:00
parent 0a582517aa
commit 933239b842
3 changed files with 85 additions and 68 deletions

View File

@@ -7,7 +7,6 @@ use std::{
io::{self, Write, Error, ErrorKind},
path::{Path, PathBuf},
process::Command,
time::SystemTime,
};
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
@@ -17,30 +16,13 @@ pub const SIZE_GB: i64 = SIZE_MB * SIZE_KB;
pub const SIZE_PB: i64 = SIZE_GB * SIZE_KB;
pub const SIZE_TB: i64 = SIZE_PB * SIZE_KB;
pub mod util_io;
pub mod util_os;
pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;
pub fn is_macos() -> bool {
if cfg!(target_os = "macos") {
true
} else {
false
}
}
pub fn is_linux() -> bool {
if cfg!(target_os = "linux") {
true
} else {
false
}
}
pub fn is_macos_or_linux() -> bool {
is_macos() || is_linux()
}
pub fn get_home_str() -> Option<String> {
match is_macos_or_linux() {
match util_os::is_macos_or_linux() {
true => env::var("HOME").ok(),
false => None,
}
@@ -264,49 +246,3 @@ pub fn extract_package_and_wait(dir: &str, file_name: &str) -> io::Result<()> {
cmd.arg(file_name).current_dir(dir);
run_command_and_wait(&mut cmd)
}
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 {
//let written_cell = RefCell::new(0u64);
let start = SystemTime::now();
let written = copy_io_callback(reader, writer, total, &|total, written, _len| {
//written_cell.replace_with(|&mut w| w + len as u64);
//let written = *written_cell.borrow();
let cost = SystemTime::now().duration_since(start.clone()).unwrap().as_secs();
let mut download_speed = "-".to_string();
if cost > 0 {
download_speed = format!("{}/s", get_display_size((written / cost) as i64));
}
if total > 0 {
print_lastline(&format!("Downloading, Total: {}, Downloaded: {}, Speed: {}",
get_display_size(total),
get_display_size(written as i64),
download_speed));
} else {
print_lastline(&format!("Downloading, Downloaded: {}, Speed: {}",
get_display_size(written as i64),
download_speed));
}
});
println!();
written
}
pub fn copy_io_callback<R: ?Sized, W: ?Sized, FCallback>(reader: &mut R, writer: &mut W, total: i64, callback: &FCallback) -> io::Result<u64>
where R: io::Read,
W: io::Write,
FCallback: Fn(i64, u64, usize) -> () {
let mut written = 0u64;
let mut buf: [u8; DEFAULT_BUF_SIZE] = [0u8; DEFAULT_BUF_SIZE];
loop {
let len = match reader.read(&mut buf) {
Ok(0) => return Ok(written),
Ok(len) => len,
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
writer.write_all(&buf[..len])?;
written += len as u64;
callback(total, written, len);
}
}

61
src/util_io.rs Normal file
View File

@@ -0,0 +1,61 @@
use std::{
io::{self, ErrorKind},
time::SystemTime,
};
use super::get_display_size;
use super::print_lastline;
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
pub const SIZE_KB: i64 = 1024;
pub const SIZE_MB: i64 = SIZE_KB * SIZE_KB;
pub const SIZE_GB: i64 = SIZE_MB * SIZE_KB;
pub const SIZE_PB: i64 = SIZE_GB * SIZE_KB;
pub const SIZE_TB: i64 = SIZE_PB * SIZE_KB;
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 {
//let written_cell = RefCell::new(0u64);
let start = SystemTime::now();
let written = copy_io_callback(reader, writer, total, &|total, written, _len| {
//written_cell.replace_with(|&mut w| w + len as u64);
//let written = *written_cell.borrow();
let cost = SystemTime::now().duration_since(start.clone()).unwrap().as_secs();
let mut download_speed = "-".to_string();
if cost > 0 {
download_speed = format!("{}/s", get_display_size((written / cost) as i64));
}
if total > 0 {
print_lastline(&format!("Downloading, Total: {}, Downloaded: {}, Speed: {}",
get_display_size(total),
get_display_size(written as i64),
download_speed));
} else {
print_lastline(&format!("Downloading, Downloaded: {}, Speed: {}",
get_display_size(written as i64),
download_speed));
}
});
println!();
written
}
pub fn copy_io_callback<R: ?Sized, W: ?Sized, FCallback>(reader: &mut R, writer: &mut W, total: i64, callback: &FCallback) -> io::Result<u64>
where R: io::Read,
W: io::Write,
FCallback: Fn(i64, u64, usize) -> () {
let mut written = 0u64;
let mut buf: [u8; DEFAULT_BUF_SIZE] = [0u8; DEFAULT_BUF_SIZE];
loop {
let len = match reader.read(&mut buf) {
Ok(0) => return Ok(written),
Ok(len) => len,
Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
writer.write_all(&buf[..len])?;
written += len as u64;
callback(total, written, len);
}
}

20
src/util_os.rs Normal file
View File

@@ -0,0 +1,20 @@
pub fn is_macos() -> bool {
if cfg!(target_os = "macos") {
true
} else {
false
}
}
pub fn is_linux() -> bool {
if cfg!(target_os = "linux") {
true
} else {
false
}
}
pub fn is_macos_or_linux() -> bool {
is_macos() || is_linux()
}