From be4a25f0c7ba78de10131d39899a01ba68080ee3 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 27 Apr 2020 23:33:21 +0800 Subject: [PATCH] update --- src/local_util.rs | 13 +++------- src/main.rs | 64 ++++++++++++++++++++--------------------------- src/opt.rs | 7 +----- 3 files changed, 32 insertions(+), 52 deletions(-) diff --git a/src/local_util.rs b/src/local_util.rs index 8a3258b..4f90fc1 100644 --- a/src/local_util.rs +++ b/src/local_util.rs @@ -1,20 +1,15 @@ - use std:: { fs::File, path::Path, io::prelude::*, }; - -use rust_util::{ - XResult, - new_box_error, -}; +use rust_util::{ XResult, new_box_error, }; pub fn read_file_content(file: &Path, large_file_len: u64) -> XResult { - if ! file.exists() { + if !file.exists() { return Err(new_box_error(&format!("File not exists: {:?}", file))); } - if ! file.is_file() { + if !file.is_file() { return Err(new_box_error(&format!("File is not file: {:?}", file))); } let file_len = file.metadata()?.len(); @@ -22,7 +17,7 @@ pub fn read_file_content(file: &Path, large_file_len: u64) -> XResult { return Err(new_box_error(&format!("File too large: {:?}, len: {}", file, file_len))); } let mut f = File::open(file)?; - let mut content = String::new(); + let mut content = String::with_capacity(file_len as usize); f.read_to_string(&mut content)?; Ok(content) diff --git a/src/main.rs b/src/main.rs index f68d5be..c8cf2e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ use rust_util::{ }; use local_util::*; +const EMPTY: &str = ""; const VERSION: &str = env!("CARGO_PKG_VERSION"); const GIT_HASH: &str = env!("GIT_HASH"); @@ -33,6 +34,11 @@ License MIT Written by Hatter Jiang"#, VERSION, &GIT_HASH[0..7]); } +pub fn clear_n_print_message(mt: MessageType, message: &str) { + print_lastline(EMPTY); + print_message(mt, message); +} + fn find_huge_files(options: &Options, dir_path: &Path) { let total_file_count_cell = RefCell::new(0u64); let huge_file_count_cell = RefCell::new(0u64); @@ -43,8 +49,7 @@ fn find_huge_files(options: &Options, dir_path: &Path) { Err(err) => { if options.verbose { if let Some(p_str) = p.to_str() { - print_lastline(""); - print_message(MessageType::WARN, &format!("Read file {} meta failed: {}", p_str, err)); + clear_n_print_message(MessageType::WARN, &format!("Read file {} meta failed: {}", p_str, err)); } } return; @@ -54,37 +59,28 @@ fn find_huge_files(options: &Options, dir_path: &Path) { if len >= options.parsed_huge_file_size { huge_file_count_cell.replace_with(|&mut c| c + 1); huge_file_size_cell.replace_with(|&mut c| c + len); - match p.to_str() { - None => (), - Some(p_str) => { - print_lastline(""); - print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64))); - }, + if let Some(p_str) = p.to_str() { + clear_n_print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64))); } } }, } }, &|p| { - match p.to_str() { - None => (), - Some(p_str) => { - if options.skip_link_dir && is_symlink(p) { - if options.verbose { - print_lastline(""); - print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); - } - return false; + if let Some(p_str) = p.to_str() { + if options.skip_link_dir && is_symlink(p) { + if options.verbose { + clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); } - print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10)) - }, + return false; + } + print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10)); } true }).unwrap_or(()); - print_lastline(""); - print_message(MessageType::OK, &format!("Total file count: {}, huge file count: {}, total huge file size: {}", - total_file_count_cell.into_inner(), - huge_file_count_cell.into_inner(), - get_display_size(huge_file_size_cell.into_inner() as i64))); + clear_n_print_message(MessageType::OK, &format!("Total file count: {}, huge file count: {}, total huge file size: {}", + total_file_count_cell.into_inner(), + huge_file_count_cell.into_inner(), + get_display_size(huge_file_size_cell.into_inner() as i64))); } @@ -115,8 +111,7 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool { for ln in lines { if options.filter_large_line && ln.len() as u64 >= options.parsed_large_line_size { if options.verbose { - print_lastline(""); - print_message(MessageType::INFO, &format!("Skip large line: {} bytes", ln.len())); + clear_n_print_message(MessageType::INFO, &format!("Skip large line: {} bytes", ln.len())); } continue; } @@ -138,7 +133,7 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool { let mut matches_any = false; if !match_lines_vec.is_empty() { - print_lastline(""); + print_lastline(EMPTY); print_message(MessageType::OK, &format!("Find in {}:", tag)); for i in 0..match_lines_vec.len() { print!("{}: ", match_lines_vec[i].line_number + 1); @@ -206,8 +201,7 @@ fn find_text_files(options: &Options, dir_path: &Path) { let file_content = match read_file_content(p, options.parsed_large_text_file_size) { Err(err) => { if options.verbose { - print_lastline(""); - print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err)); + clear_n_print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err)); } return; }, @@ -224,8 +218,7 @@ fn find_text_files(options: &Options, dir_path: &Path) { Some(p_str) => { if (! options.scan_dot_git) && p_str.ends_with("/.git") { if options.verbose { - print_lastline(""); - print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str)); + clear_n_print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str)); } return false; } @@ -234,8 +227,7 @@ fn find_text_files(options: &Options, dir_path: &Path) { } if options.skip_link_dir && is_symlink(p) { if options.verbose { - print_lastline(""); - print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); + clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); } return false; } @@ -245,7 +237,7 @@ fn find_text_files(options: &Options, dir_path: &Path) { } true }).unwrap_or(()); - print_lastline(""); + print_lastline(EMPTY); print_message(MessageType::OK, &format!("Total dir count: {}, scaned dir count: {}", total_dir_count_cell.into_inner(), scaned_dir_count_cell.into_inner())); @@ -255,7 +247,6 @@ fn find_text_files(options: &Options, dir_path: &Path) { matched_file_count_cell.into_inner())); } - fn main() -> XResult<()> { let options = Options::new_and_parse_args()?; @@ -265,10 +256,9 @@ fn main() -> XResult<()> { } let dir_path = match get_absolute_path(&options.dir) { - None => { + Some(path) => path, None => { return Err(new_box_error(&format!("Cannot find dir: {}", options.dir))); }, - Some(path) => path, }; let start = SystemTime::now(); match options.target.as_str() { diff --git a/src/opt.rs b/src/opt.rs index ee065ed..417c900 100644 --- a/src/opt.rs +++ b/src/opt.rs @@ -1,10 +1,5 @@ - use argparse::{ArgumentParser, StoreTrue, Store}; - -use rust_util::{ - XResult, - util_size::*, -}; +use rust_util::{ XResult, util_size::*, }; pub struct Options { pub version: bool,