From bb8103dac9b4729c43d78c7785cd68f064d9c83b Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 2 Aug 2020 22:30:48 +0800 Subject: [PATCH] style: code style --- Cargo.lock | 6 +++--- Cargo.toml | 2 +- src/main.rs | 42 ++++++++++++++++++++++-------------------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42dc05b..d1ecdd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,7 +130,7 @@ name = "finding" version = "0.2.0" dependencies = [ "argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "rust_util 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rust_util 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -237,7 +237,7 @@ dependencies = [ [[package]] name = "rust_util" -version = "0.2.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -357,7 +357,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" "checksum redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecedbca3bf205f8d8f5c2b44d83cd0690e39ee84b951ed649e9f1841132b66d" "checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf" -"checksum rust_util 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd9973675144c03fdf6f9a559ea5c254b0008e90a8992b1f317f728edd3512bc" +"checksum rust_util 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9e9cf201657d8553fd7eddf4c20e00b1bdebca40e9fa2ede5c87f6874d02750f" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)" = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" diff --git a/Cargo.toml b/Cargo.toml index c55f7fd..08397c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,4 @@ license = "MIT" argparse = "0.2.2" term = "0.5.2" term_size = "0.3.1" -rust_util = "0.2.2" +rust_util = "0.6.3" diff --git a/src/main.rs b/src/main.rs index a618327..ab62402 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ extern crate argparse; extern crate term; extern crate term_size; +#[macro_use] extern crate rust_util; mod opt; @@ -8,22 +9,23 @@ mod local_util; use std::{ path::Path, - time::{ Duration, SystemTime,}, + time::{ Duration, SystemTime }, }; use opt::*; use rust_util::{ - iff, XResult, new_box_error, util_file::*, util_size::*, - util_msg::*, }; -use local_util::{ - CountCell, - MatchLine, - read_file_content, +use rust_util::util_msg::{ + MessageType, + print_color, + print_lastline, + print_message, + get_term_width_message, }; +use local_util::{ CountCell, MatchLine, read_file_content }; const EMPTY: &str = ""; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -36,8 +38,8 @@ License MIT Written by Hatter Jiang"#, VERSION, &GIT_HASH[0..7]); if options.verbose { - print_message(MessageType::DEBUG, &format!("Version: {}", VERSION)); - print_message(MessageType::DEBUG, &format!("Git hash: {}", GIT_HASH)); + debugging!("Version: {}", VERSION); + debugging!("Git hash: {}", GIT_HASH); } } @@ -133,11 +135,11 @@ fn match_lines(tag: &str, content: &str, options: &Options) -> bool { fn find_text_files(options: &Options, dir_path: &Path) { if options.search_text.is_empty() { - print_message(MessageType::ERROR, "Param search_text cannot be empty."); + failure!("Param search_text cannot be empty."); return; } if options.ignore_case { - print_message(MessageType::WARN, "Using ignore case mode, highlight print is disabled."); + warning!("Using ignore case mode, highlight print is disabled."); } let file_exts = match &options.file_ext { ext if ext.is_empty() => vec![], @@ -195,13 +197,13 @@ fn find_text_files(options: &Options, dir_path: &Path) { true }).ok(); print_lastline(EMPTY); - print_message(MessageType::OK, &format!("Total dir count: {}, scaned dir count: {}", - total_dir_count.get(), - scaned_dir_count.get())); - print_message(MessageType::OK, &format!("Total file count: {}, scaned file count: {}, matched file count: {}", - total_file_count.get(), - scaned_file_count.get(), - matched_file_count.get())); + success!("Total dir count: {}, scaned dir count: {}", + total_dir_count.get(), + scaned_dir_count.get()); + success!("Total file count: {}, scaned file count: {}, matched file count: {}", + total_file_count.get(), + scaned_file_count.get(), + matched_file_count.get()); } fn main() -> XResult<()> { @@ -222,7 +224,7 @@ fn main() -> XResult<()> { "text" => find_text_files(&options, &dir_path), others => return Err(new_box_error(&format!("Unknown command: {}", others))), } - let cost_millis = SystemTime::now().duration_since(start.clone()).unwrap_or(Duration::from_millis(0)).as_millis(); - print_message(MessageType::OK, &format!("Finding finished, cost {} ms", cost_millis)); + let cost_millis = SystemTime::now().duration_since(start).unwrap_or_else(|_| Duration::from_millis(0)).as_millis(); + success!("Finding finished, cost {} ms", cost_millis); Ok(()) }