1
0
mirror of https://github.com/jht5945/rust_util.git synced 2026-01-12 07:10:05 +08:00

Compare commits

...

2 Commits

Author SHA1 Message Date
53747a2337 fix clippy 2020-01-11 11:57:43 +08:00
5074a46c61 fix clippy 2020-01-11 11:51:28 +08:00
4 changed files with 34 additions and 47 deletions

View File

@@ -12,9 +12,10 @@ use super::{
}; };
pub fn get_home_str() -> Option<String> { pub fn get_home_str() -> Option<String> {
match util_os::is_macos_or_linux() { if util_os::is_macos_or_linux() {
true => env::var("HOME").ok(), env::var("HOME").ok()
false => None, } else {
None
} }
} }
@@ -81,12 +82,8 @@ fn walk_dir_with_depth_check<FError, FProcess, FFilter>(depth: &mut u32, dir: &P
} else if sub_dir.is_dir() { } else if sub_dir.is_dir() {
if func_filter_dir(&sub_dir) { if func_filter_dir(&sub_dir) {
*depth += 1; *depth += 1;
match walk_dir_with_depth_check(depth, &sub_dir, func_walk_error, func_process_file, func_filter_dir) { if let Err(err) = walk_dir_with_depth_check(depth, &sub_dir, func_walk_error, func_process_file, func_filter_dir) {
Err(err) => { func_walk_error(&sub_dir, err);
func_walk_error(&sub_dir, err);
()
},
Ok(_) => (),
} }
*depth -= 1; *depth -= 1;
} }

View File

@@ -9,26 +9,24 @@ lazy_static! {
pub enum MessageType { INFO, OK, WARN, ERROR, DEBUG, } pub enum MessageType { INFO, OK, WARN, ERROR, DEBUG, }
pub fn is_atty() -> bool{ pub fn is_atty() -> bool{
let isatty = unsafe { libc::isatty(libc::STDOUT_FILENO as i32) } != 0; let stdout_fileno = unsafe { libc::isatty(libc::STDOUT_FILENO as i32) };
isatty stdout_fileno != 0
} }
pub fn print_color(color: Option<term::color::Color>, is_bold: bool, m: &str) { pub fn print_color(color: Option<term::color::Color>, is_bold: bool, m: &str) {
let mut t = term::stdout().unwrap(); let mut t = term::stdout().unwrap();
match *IS_ATTY { if *IS_ATTY {
true => { if let Some(c) = color {
match color { t.fg(c).unwrap();
Some(c) => t.fg(c).unwrap(), }
None => (), if is_bold {
} t.attr(term::Attr::Bold).unwrap();
if is_bold { }
t.attr(term::Attr::Bold).unwrap(); write!(t, "{}", m).unwrap();
} t.reset().unwrap();
write!(t, "{}", m).unwrap(); } else {
t.reset().unwrap(); write!(t, "{}", m).unwrap();
}, }
false => write!(t, "{}", m).unwrap(),
};
} }
pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m: &str) { pub fn print_color_and_flush(color: Option<term::color::Color>, is_bold: bool, m: &str) {

View File

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

View File

@@ -9,20 +9,20 @@ pub const SIZE_TB: i64 = SIZE_PB * SIZE_KB;
pub fn parse_size(size: &str) -> XResult<i64> { pub fn parse_size(size: &str) -> XResult<i64> {
let lower_size = size.to_lowercase(); let lower_size = size.to_lowercase();
let no_last_b_size = if lower_size.ends_with("b") { let no_last_b_size = if lower_size.ends_with('b') {
&lower_size[0..lower_size.len()-1] &lower_size[0..lower_size.len()-1]
} else { } else {
&lower_size &lower_size
}; };
if no_last_b_size.ends_with("k") { 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); 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") { } 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); 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") { } 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); 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") { } 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); 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") { } 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); return Ok((SIZE_PB as f64 * no_last_b_size[0..no_last_b_size.len()-1].parse::<f64>()?) as i64);
} }
@@ -31,16 +31,16 @@ pub fn parse_size(size: &str) -> XResult<i64> {
pub fn get_display_size(size: i64) -> String { pub fn get_display_size(size: i64) -> String {
if size < SIZE_KB { if size < SIZE_KB {
return size.to_string(); size.to_string()
} else if size < SIZE_MB { } else if size < SIZE_MB {
return format!("{:.*}KB", 2, (size as f64) / 1024.); format!("{:.*}KB", 2, (size as f64) / 1024.)
} else if size < SIZE_GB { } else if size < SIZE_GB {
return format!("{:.*}MB", 2, (size as f64) / 1024. / 1024.); format!("{:.*}MB", 2, (size as f64) / 1024. / 1024.)
} else if size < SIZE_TB { } else if size < SIZE_TB {
return format!("{:.*}GB", 2, (size as f64) / 1024. / 1024. / 1024.); format!("{:.*}GB", 2, (size as f64) / 1024. / 1024. / 1024.)
} else if size < SIZE_PB { } else if size < SIZE_PB {
return format!("{:.*}TB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024.); format!("{:.*}TB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024.)
} else { } else {
return format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.); format!("{:.*}PB", 2, (size as f64) / 1024. / 1024. / 1024. / 1024. / 1024.)
} }
} }