1
0
mirror of https://github.com/jht5945/finding.git synced 2026-01-12 12:00:03 +08:00

Compare commits

..

10 Commits

Author SHA1 Message Date
0e4024337f unwrap_or -> ok 2020-04-28 00:42:17 +08:00
03bbba3193 l_no -> line_no 2020-04-28 00:40:43 +08:00
39b6cbde6a fix clippy 2020-04-28 00:39:17 +08:00
93d39ecd50 use iter any 2020-04-28 00:38:05 +08:00
6b7c2b3759 ref c 2020-04-28 00:25:25 +08:00
640293989c ref verbose 2020-04-28 00:17:29 +08:00
efa006f745 add --skip-target-dir 2020-04-28 00:11:34 +08:00
2ce9540577 use clear_n_print_message 2020-04-28 00:04:10 +08:00
8008f27518 fix clippy 2020-04-28 00:03:40 +08:00
be4a25f0c7 update 2020-04-27 23:33:21 +08:00
5 changed files with 115 additions and 175 deletions

8
Cargo.lock generated
View File

@@ -127,10 +127,10 @@ dependencies = [
[[package]]
name = "finding"
version = "0.1.3"
version = "0.2.0"
dependencies = [
"argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rust_util 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rust_util 0.2.2 (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.1.0"
version = "0.2.2"
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.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5dc9b9d45cf4e9a8a28c1c622f0a2bba9af88b85ec04c494d80605a717c1c022"
"checksum rust_util 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd9973675144c03fdf6f9a559ea5c254b0008e90a8992b1f317f728edd3512bc"
"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"

View File

@@ -1,6 +1,6 @@
[package]
name = "finding"
version = "0.1.3"
version = "0.2.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
description = "command line finding tool"
@@ -12,4 +12,4 @@ license = "MIT"
argparse = "0.2.2"
term = "0.5.2"
term_size = "0.3.1"
rust_util = "0.1.0"
rust_util = "0.2.2"

View File

@@ -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<String> {
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<String> {
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)

View File

@@ -6,33 +6,47 @@ extern crate rust_util;
mod opt;
mod local_util;
use std::{
cell::RefCell,
path::Path,
time::SystemTime,
};
use std::{ cell::RefCell, path::Path, time::SystemTime, };
use opt::*;
use rust_util::{
iff,
XResult,
new_box_error,
util_file::*,
util_size::*,
util_msg::*,
};
use local_util::*;
use local_util::read_file_content;
const EMPTY: &str = "";
const VERSION: &str = env!("CARGO_PKG_VERSION");
const GIT_HASH: &str = env!("GIT_HASH");
#[derive(Debug)]
struct MatchLine {
line_number: usize,
line_string: String,
}
impl MatchLine {
fn new(line_number: usize, line_string: String) -> MatchLine {
MatchLine { line_number, line_string, }
}
}
fn print_version() {
println!(r#"finding {} - {}
Copyright (C) 2019 Hatter Jiang.
Copyright (C) 2019-2020 Hatter Jiang.
License MIT <https://opensource.org/licenses/MIT>
Written by Hatter Jiang"#, VERSION, &GIT_HASH[0..7]);
}
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);
@@ -40,125 +54,85 @@ fn find_huge_files(options: &Options, dir_path: &Path) {
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
total_file_count_cell.replace_with(|&mut c| c + 1);
match p.metadata() {
Err(err) => {
if options.verbose {
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;
},
Ok(metadata) => {
let len = metadata.len();
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 let Some(p_str) = p.to_str() {
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;
}
print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10))
},
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: {}",
}).ok();
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)));
}
#[derive(Debug)]
struct MatchLine {
line_number: usize,
line_string: String,
}
impl MatchLine {
fn new(l_no: usize, l_str: String) -> MatchLine {
MatchLine {
line_number: l_no,
line_string: l_str,
}
}
}
fn match_lines(tag: &str, content: &str, options: &Options) -> bool {
let search_text = &options.search_text;
let lines = content.lines();
let mut match_lines_vec = vec![];
let mut l_no = 0usize;
let the_search_text = &match options.ignore_case {
true => search_text.to_lowercase(),
false => search_text.to_string(),
};
let mut line_no = 0usize;
let the_search_text = &iff!(options.ignore_case, search_text.to_lowercase(), search_text.to_string());
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;
}
let matches = match options.ignore_case {
true => ln.to_lowercase().contains(the_search_text),
false => ln.contains(the_search_text),
let matches = iff!(options.ignore_case, ln.to_lowercase().contains(the_search_text), ln.contains(the_search_text));
let matches_line_content = match &options.filter_line_content {
c if c.is_empty() => true,
c => ln.contains(c),
};
let mut matches_line_content = true;
if !options.filter_line_content.is_empty(){
if ! ln.contains(options.filter_line_content.as_str()) {
matches_line_content = false;
}
}
if matches && matches_line_content {
match_lines_vec.push(MatchLine::new(l_no, ln.to_string()));
match_lines_vec.push(MatchLine::new(line_no, ln.to_string()));
}
l_no += 1;
line_no += 1;
}
let mut matches_any = false;
if !match_lines_vec.is_empty() {
print_lastline("");
print_message(MessageType::OK, &format!("Find in {}:", tag));
for i in 0..match_lines_vec.len() {
print!("{}: ", match_lines_vec[i].line_number + 1);
match options.ignore_case {
true => println!("{}", match_lines_vec[i].line_string),
false => {
let ss: Vec<&str> = match_lines_vec[i].line_string.split(search_text).collect();
if match_lines_vec.is_empty() {
false
} else {
clear_n_print_message(MessageType::OK, &format!("Find in {}:", tag));
for match_line in &match_lines_vec {
print!("{}: ", match_line.line_number + 1);
if options.ignore_case {
println!("{}", match_line.line_string);
} else {
let ss: Vec<&str> = match_line.line_string.split(search_text).collect();
for j in 0..ss.len() {
print!("{}", ss[j]);
if j < ss.len() -1 {
if j < ss.len() - 1 {
print_color(Some(term::color::RED), true, search_text);
}
}
println!();
},
}
}
matches_any = true;
true
}
matches_any
}
fn find_text_files(options: &Options, dir_path: &Path) {
@@ -169,11 +143,9 @@ fn find_text_files(options: &Options, dir_path: &Path) {
if options.ignore_case {
print_message(MessageType::WARN, "Using ignore case mode, highlight print is disabled.");
}
let file_exts = match options.file_ext.as_str() {
"" => vec![],
ext => {
ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| String::from(".") + s).collect()
},
let file_exts = match &options.file_ext {
ext if ext.is_empty() => vec![],
ext => ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| ".".to_owned() + s).collect(),
};
let total_file_count_cell = RefCell::new(0u64);
let scaned_file_count_cell = RefCell::new(0u64);
@@ -183,35 +155,19 @@ fn find_text_files(options: &Options, dir_path: &Path) {
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
total_file_count_cell.replace_with(|&mut c| c + 1);
let p_str = match p.to_str() {
None => return,
Some(s) => s,
Some(s) => s, None => return,
};
if !file_exts.is_empty() {
let mut file_ext_matches = false;
for file_ext in &file_exts {
if p_str.to_lowercase().ends_with(file_ext) {
file_ext_matches = true;
break;
}
}
if ! file_ext_matches {
if !file_exts.is_empty() && !file_exts.iter().any(|file_ext| p_str.to_lowercase().ends_with(file_ext)) {
return;
}
}
if !options.filter_file_name.is_empty() {
if ! p_str.contains(options.filter_file_name.as_str()) {
if !options.filter_file_name.is_empty() && !p_str.contains(options.filter_file_name.as_str()) {
return;
}
}
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));
}
Ok(c) => c, Err(err) => {
if options.verbose { clear_n_print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err)); }
return;
},
Ok(c) => c,
};
scaned_file_count_cell.replace_with(|&mut c| c + 1);
if match_lines(p_str, &file_content, &options) {
@@ -219,33 +175,29 @@ fn find_text_files(options: &Options, dir_path: &Path) {
}
}, &|p| {
total_dir_count_cell.replace_with(|&mut c| c + 1);
match p.to_str() {
None => (),
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));
if let Some(p_str) = p.to_str() {
if (!options.scan_dot_git_dir) && p_str.ends_with("/.git") {
if options.verbose { clear_n_print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str)); }
return false;
}
if options.skip_target_dir && p_str.ends_with("/target") {
if options.verbose { clear_n_print_message(MessageType::INFO, &format!("Skip target dir: {}", p_str)); }
return false;
}
if options.skip_dot_dir && p_str.contains("/.") {
if options.verbose { clear_n_print_message(MessageType::INFO, &format!("Skip dot(.) dir: {}", p_str)); }
return false;
}
if options.skip_link_dir && is_symlink(p) {
if options.verbose {
print_lastline("");
print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str));
}
if options.verbose { clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); }
return false;
}
scaned_dir_count_cell.replace_with(|&mut c| c + 1);
print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10))
},
print_lastline(&get_term_width_message(&format!("Scanning: {}", p_str), 10));
}
true
}).unwrap_or(());
print_lastline("");
}).ok();
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 +207,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,18 +216,14 @@ fn main() -> XResult<()> {
}
let dir_path = match get_absolute_path(&options.dir) {
None => {
return Err(new_box_error(&format!("Cannot find dir: {}", options.dir)));
},
Some(path) => path,
None => return Err(new_box_error(&format!("Cannot find dir: {}", options.dir))),
};
let start = SystemTime::now();
match options.target.as_str() {
"huge" | "hugefile" => find_huge_files(&options, &dir_path),
"text" => find_text_files(&options, &dir_path),
unknown => {
return Err(new_box_error(&format!("Unknown command: {}", unknown)));
},
others => return Err(new_box_error(&format!("Unknown command: {}", others))),
}
let cost_millis = SystemTime::now().duration_since(start.clone()).unwrap().as_millis();
print_message(MessageType::OK, &format!("Finding finished, cost {} ms", cost_millis));

View File

@@ -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,
@@ -19,9 +14,10 @@ pub struct Options {
pub filter_large_line: bool,
pub large_line_size: String,
pub parsed_large_line_size: u64,
pub scan_dot_git: bool,
pub scan_dot_git_dir: bool,
pub skip_dot_dir: bool,
pub skip_link_dir: bool,
pub skip_target_dir: bool,
pub filter_file_name: String,
pub filter_line_content: String,
pub verbose: bool,
@@ -43,9 +39,10 @@ impl Options {
filter_large_line: false,
large_line_size: String::from("10KB"),
parsed_large_line_size: 0u64,
scan_dot_git: false,
scan_dot_git_dir: false,
skip_dot_dir: false,
skip_link_dir: false,
skip_target_dir: false,
filter_file_name: String::new(),
filter_line_content: String::new(),
verbose: false,
@@ -65,9 +62,10 @@ impl Options {
ap.refer(&mut self.ignore_case).add_option(&["-i", "--ignore-case"], StoreTrue, "Ignore case, default false");
ap.refer(&mut self.filter_large_line).add_option(&["--filter-large-line"], StoreTrue, "Filter large line");
ap.refer(&mut self.large_line_size).add_option(&["--large-line-size"], Store, "Large line, default 10KB");
ap.refer(&mut self.scan_dot_git).add_option(&["--scan-dot-git"], StoreTrue, "Scan dot git");
ap.refer(&mut self.scan_dot_git_dir).add_option(&["--scan-dot-git"], StoreTrue, "Scan dot git");
ap.refer(&mut self.skip_dot_dir).add_option(&["--skip-dot-dir"], StoreTrue, "Skipt dot dir [Text Mode]");
ap.refer(&mut self.skip_link_dir).add_option(&["--skip-link-dir"], StoreTrue, "Skip link dir");
ap.refer(&mut self.skip_target_dir).add_option(&["--skip-target-dir"], StoreTrue, "Skip target dir");
ap.refer(&mut self.filter_file_name).add_option(&["--filter-file-name"], Store, "Filter file name [Text Mode]");
ap.refer(&mut self.filter_line_content).add_option(&["--filter-line-content"], Store, "Filter line content [Text Mode]");
ap.refer(&mut self.version).add_option(&["-v", "--version"], StoreTrue, "Print version");