mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 07:30:05 +08:00
add src/util_size.rs
This commit is contained in:
46
src/lib.rs
46
src/lib.rs
@@ -9,16 +9,10 @@ use std::{
|
||||
process::Command,
|
||||
};
|
||||
|
||||
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 mod util_io;
|
||||
pub mod util_os;
|
||||
pub mod util_msg;
|
||||
pub mod util_size;
|
||||
|
||||
pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
@@ -114,44 +108,6 @@ pub fn new_box_ioerror(m: &str) -> Box<dyn std::error::Error> {
|
||||
Box::new(Error::new(ErrorKind::Other, m))
|
||||
}
|
||||
|
||||
pub fn parse_size(size: &str) -> XResult<i64> {
|
||||
let lower_size = size.to_lowercase();
|
||||
let no_last_b_size = if lower_size.ends_with("b") {
|
||||
&lower_size[0..lower_size.len()-1]
|
||||
} else {
|
||||
&lower_size
|
||||
};
|
||||
if no_last_b_size.ends_with("k") {
|
||||
return Ok((SIZE_KB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
} else if no_last_b_size.ends_with("m") {
|
||||
return Ok((SIZE_MB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
} else if no_last_b_size.ends_with("g") {
|
||||
return Ok((SIZE_GB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
} else if no_last_b_size.ends_with("t") {
|
||||
return Ok((SIZE_TB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
} else if no_last_b_size.ends_with("p") {
|
||||
return Ok((SIZE_PB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
|
||||
}
|
||||
|
||||
Ok(no_last_b_size.parse::<i64>()?)
|
||||
}
|
||||
|
||||
pub fn get_display_size(size: i64) -> String {
|
||||
if size < SIZE_KB {
|
||||
return size.to_string();
|
||||
} else if size < SIZE_MB {
|
||||
return format!("{:.*}KB", 2, (size as f64) / 1024.);
|
||||
} else if size < SIZE_GB {
|
||||
return format!("{:.*}MB", 2, (size as f64) / 1024. / 1024.);
|
||||
} else if size < SIZE_TB {
|
||||
return format!("{:.*}GB", 2, (size as f64) / 1024. / 1024. / 1024.);
|
||||
} else if size < SIZE_PB {
|
||||
return format!("{:.*}TB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024.);
|
||||
} else {
|
||||
return format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_command_and_wait(cmd: &mut Command) -> io::Result<()> {
|
||||
cmd.spawn()?.wait()?;
|
||||
Ok(())
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::{
|
||||
time::SystemTime,
|
||||
};
|
||||
|
||||
use super::get_display_size;
|
||||
use super::util_size::get_display_size;
|
||||
use super::util_msg::print_lastline;
|
||||
|
||||
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
|
||||
|
||||
Reference in New Issue
Block a user