diff --git a/src/lib.rs b/src/lib.rs index 88e3cf5..36909df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 = Result>; -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 { - 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(reader: &mut R, writer: &mut W, total: i64) -> io::Result - 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(reader: &mut R, writer: &mut W, total: i64, callback: &FCallback) -> io::Result - 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); - } -} diff --git a/src/util_io.rs b/src/util_io.rs new file mode 100644 index 0000000..e1246c7 --- /dev/null +++ b/src/util_io.rs @@ -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(reader: &mut R, writer: &mut W, total: i64) -> io::Result + 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(reader: &mut R, writer: &mut W, total: i64, callback: &FCallback) -> io::Result + 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); + } +} diff --git a/src/util_os.rs b/src/util_os.rs new file mode 100644 index 0000000..03959d9 --- /dev/null +++ b/src/util_os.rs @@ -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() +}