mirror of
https://github.com/jht5945/finding.git
synced 2026-01-12 12:00:03 +08:00
Compare commits
10 Commits
c03ab9ff4c
...
0e4024337f
| Author | SHA1 | Date | |
|---|---|---|---|
| 0e4024337f | |||
| 03bbba3193 | |||
| 39b6cbde6a | |||
| 93d39ecd50 | |||
| 6b7c2b3759 | |||
| 640293989c | |||
| efa006f745 | |||
| 2ce9540577 | |||
| 8008f27518 | |||
| be4a25f0c7 |
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -127,10 +127,10 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "finding"
|
name = "finding"
|
||||||
version = "0.1.3"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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 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)",
|
"term_size 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
@@ -237,7 +237,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.1.0"
|
version = "0.2.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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_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 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-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 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 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"
|
"checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "finding"
|
name = "finding"
|
||||||
version = "0.1.3"
|
version = "0.2.0"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "command line finding tool"
|
description = "command line finding tool"
|
||||||
@@ -12,4 +12,4 @@ license = "MIT"
|
|||||||
argparse = "0.2.2"
|
argparse = "0.2.2"
|
||||||
term = "0.5.2"
|
term = "0.5.2"
|
||||||
term_size = "0.3.1"
|
term_size = "0.3.1"
|
||||||
rust_util = "0.1.0"
|
rust_util = "0.2.2"
|
||||||
|
|||||||
@@ -1,20 +1,15 @@
|
|||||||
|
|
||||||
use std:: {
|
use std:: {
|
||||||
fs::File,
|
fs::File,
|
||||||
path::Path,
|
path::Path,
|
||||||
io::prelude::*,
|
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> {
|
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)));
|
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)));
|
return Err(new_box_error(&format!("File is not file: {:?}", file)));
|
||||||
}
|
}
|
||||||
let file_len = file.metadata()?.len();
|
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)));
|
return Err(new_box_error(&format!("File too large: {:?}, len: {}", file, file_len)));
|
||||||
}
|
}
|
||||||
let mut f = File::open(file)?;
|
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)?;
|
f.read_to_string(&mut content)?;
|
||||||
|
|
||||||
Ok(content)
|
Ok(content)
|
||||||
|
|||||||
199
src/main.rs
199
src/main.rs
@@ -6,33 +6,47 @@ extern crate rust_util;
|
|||||||
mod opt;
|
mod opt;
|
||||||
mod local_util;
|
mod local_util;
|
||||||
|
|
||||||
use std::{
|
use std::{ cell::RefCell, path::Path, time::SystemTime, };
|
||||||
cell::RefCell,
|
|
||||||
path::Path,
|
|
||||||
time::SystemTime,
|
|
||||||
};
|
|
||||||
|
|
||||||
use opt::*;
|
use opt::*;
|
||||||
use rust_util::{
|
use rust_util::{
|
||||||
|
iff,
|
||||||
XResult,
|
XResult,
|
||||||
new_box_error,
|
new_box_error,
|
||||||
util_file::*,
|
util_file::*,
|
||||||
util_size::*,
|
util_size::*,
|
||||||
util_msg::*,
|
util_msg::*,
|
||||||
};
|
};
|
||||||
use local_util::*;
|
use local_util::read_file_content;
|
||||||
|
|
||||||
|
const EMPTY: &str = "";
|
||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
const GIT_HASH: &str = env!("GIT_HASH");
|
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() {
|
fn print_version() {
|
||||||
println!(r#"finding {} - {}
|
println!(r#"finding {} - {}
|
||||||
Copyright (C) 2019 Hatter Jiang.
|
Copyright (C) 2019-2020 Hatter Jiang.
|
||||||
License MIT <https://opensource.org/licenses/MIT>
|
License MIT <https://opensource.org/licenses/MIT>
|
||||||
|
|
||||||
Written by Hatter Jiang"#, VERSION, &GIT_HASH[0..7]);
|
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) {
|
fn find_huge_files(options: &Options, dir_path: &Path) {
|
||||||
let total_file_count_cell = RefCell::new(0u64);
|
let total_file_count_cell = RefCell::new(0u64);
|
||||||
let huge_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| {
|
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
|
||||||
total_file_count_cell.replace_with(|&mut c| c + 1);
|
total_file_count_cell.replace_with(|&mut c| c + 1);
|
||||||
match p.metadata() {
|
match p.metadata() {
|
||||||
Err(err) => {
|
Err(err) => if options.verbose {
|
||||||
if options.verbose {
|
|
||||||
if let Some(p_str) = p.to_str() {
|
if let Some(p_str) = p.to_str() {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::WARN, &format!("Read file {} meta failed: {}", p_str, err));
|
||||||
print_message(MessageType::WARN, &format!("Read file {} meta failed: {}", p_str, err));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return;
|
|
||||||
},
|
},
|
||||||
Ok(metadata) => {
|
Ok(metadata) => {
|
||||||
let len = metadata.len();
|
let len = metadata.len();
|
||||||
if len >= options.parsed_huge_file_size {
|
if len >= options.parsed_huge_file_size {
|
||||||
huge_file_count_cell.replace_with(|&mut c| c + 1);
|
huge_file_count_cell.replace_with(|&mut c| c + 1);
|
||||||
huge_file_size_cell.replace_with(|&mut c| c + len);
|
huge_file_size_cell.replace_with(|&mut c| c + len);
|
||||||
match p.to_str() {
|
if let Some(p_str) = p.to_str() {
|
||||||
None => (),
|
clear_n_print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
|
||||||
Some(p_str) => {
|
|
||||||
print_lastline("");
|
|
||||||
print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}, &|p| {
|
}, &|p| {
|
||||||
match p.to_str() {
|
if let Some(p_str) = p.to_str() {
|
||||||
None => (),
|
|
||||||
Some(p_str) => {
|
|
||||||
if options.skip_link_dir && is_symlink(p) {
|
if options.skip_link_dir && is_symlink(p) {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str));
|
||||||
print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str));
|
|
||||||
}
|
}
|
||||||
return false;
|
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
|
true
|
||||||
}).unwrap_or(());
|
}).ok();
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::OK, &format!("Total file count: {}, huge file count: {}, total huge file size: {}",
|
||||||
print_message(MessageType::OK, &format!("Total file count: {}, huge file count: {}, total huge file size: {}",
|
|
||||||
total_file_count_cell.into_inner(),
|
total_file_count_cell.into_inner(),
|
||||||
huge_file_count_cell.into_inner(),
|
huge_file_count_cell.into_inner(),
|
||||||
get_display_size(huge_file_size_cell.into_inner() as i64)));
|
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 {
|
fn match_lines(tag: &str, content: &str, options: &Options) -> bool {
|
||||||
let search_text = &options.search_text;
|
let search_text = &options.search_text;
|
||||||
let lines = content.lines();
|
let lines = content.lines();
|
||||||
let mut match_lines_vec = vec![];
|
let mut match_lines_vec = vec![];
|
||||||
let mut l_no = 0usize;
|
let mut line_no = 0usize;
|
||||||
let the_search_text = &match options.ignore_case {
|
let the_search_text = &iff!(options.ignore_case, search_text.to_lowercase(), search_text.to_string());
|
||||||
true => search_text.to_lowercase(),
|
|
||||||
false => search_text.to_string(),
|
|
||||||
};
|
|
||||||
for ln in lines {
|
for ln in lines {
|
||||||
if options.filter_large_line && ln.len() as u64 >= options.parsed_large_line_size {
|
if options.filter_large_line && ln.len() as u64 >= options.parsed_large_line_size {
|
||||||
if options.verbose {
|
if options.verbose {
|
||||||
print_lastline("");
|
clear_n_print_message(MessageType::INFO, &format!("Skip large line: {} bytes", ln.len()));
|
||||||
print_message(MessageType::INFO, &format!("Skip large line: {} bytes", ln.len()));
|
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let matches = match options.ignore_case {
|
let matches = iff!(options.ignore_case, ln.to_lowercase().contains(the_search_text), ln.contains(the_search_text));
|
||||||
true => ln.to_lowercase().contains(the_search_text),
|
let matches_line_content = match &options.filter_line_content {
|
||||||
false => ln.contains(the_search_text),
|
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 {
|
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() {
|
||||||
if !match_lines_vec.is_empty() {
|
false
|
||||||
print_lastline("");
|
} else {
|
||||||
print_message(MessageType::OK, &format!("Find in {}:", tag));
|
clear_n_print_message(MessageType::OK, &format!("Find in {}:", tag));
|
||||||
for i in 0..match_lines_vec.len() {
|
for match_line in &match_lines_vec {
|
||||||
print!("{}: ", match_lines_vec[i].line_number + 1);
|
print!("{}: ", match_line.line_number + 1);
|
||||||
match options.ignore_case {
|
if options.ignore_case {
|
||||||
true => println!("{}", match_lines_vec[i].line_string),
|
println!("{}", match_line.line_string);
|
||||||
false => {
|
} else {
|
||||||
let ss: Vec<&str> = match_lines_vec[i].line_string.split(search_text).collect();
|
let ss: Vec<&str> = match_line.line_string.split(search_text).collect();
|
||||||
for j in 0..ss.len() {
|
for j in 0..ss.len() {
|
||||||
print!("{}", ss[j]);
|
print!("{}", ss[j]);
|
||||||
if j < ss.len() -1 {
|
if j < ss.len() - 1 {
|
||||||
print_color(Some(term::color::RED), true, search_text);
|
print_color(Some(term::color::RED), true, search_text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!();
|
println!();
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
matches_any = true;
|
true
|
||||||
}
|
}
|
||||||
matches_any
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_text_files(options: &Options, dir_path: &Path) {
|
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 {
|
if options.ignore_case {
|
||||||
print_message(MessageType::WARN, "Using ignore case mode, highlight print is disabled.");
|
print_message(MessageType::WARN, "Using ignore case mode, highlight print is disabled.");
|
||||||
}
|
}
|
||||||
let file_exts = match options.file_ext.as_str() {
|
let file_exts = match &options.file_ext {
|
||||||
"" => vec![],
|
ext if ext.is_empty() => vec![],
|
||||||
ext => {
|
ext => ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| ".".to_owned() + s).collect(),
|
||||||
ext.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).map(|s| String::from(".") + s).collect()
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
let total_file_count_cell = RefCell::new(0u64);
|
let total_file_count_cell = RefCell::new(0u64);
|
||||||
let scaned_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| {
|
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
|
||||||
total_file_count_cell.replace_with(|&mut c| c + 1);
|
total_file_count_cell.replace_with(|&mut c| c + 1);
|
||||||
let p_str = match p.to_str() {
|
let p_str = match p.to_str() {
|
||||||
None => return,
|
Some(s) => s, None => return,
|
||||||
Some(s) => s,
|
|
||||||
};
|
};
|
||||||
if !file_exts.is_empty() {
|
if !file_exts.is_empty() && !file_exts.iter().any(|file_ext| p_str.to_lowercase().ends_with(file_ext)) {
|
||||||
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 {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
if !options.filter_file_name.is_empty() && !p_str.contains(options.filter_file_name.as_str()) {
|
||||||
if !options.filter_file_name.is_empty() {
|
|
||||||
if ! p_str.contains(options.filter_file_name.as_str()) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
let file_content = match read_file_content(p, options.parsed_large_text_file_size) {
|
let file_content = match read_file_content(p, options.parsed_large_text_file_size) {
|
||||||
Err(err) => {
|
Ok(c) => c, Err(err) => {
|
||||||
if options.verbose {
|
if options.verbose { clear_n_print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err)); }
|
||||||
print_lastline("");
|
|
||||||
print_message(MessageType::WARN, &format!("Read file {} failed: {}", p_str, err));
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
Ok(c) => c,
|
|
||||||
};
|
};
|
||||||
scaned_file_count_cell.replace_with(|&mut c| c + 1);
|
scaned_file_count_cell.replace_with(|&mut c| c + 1);
|
||||||
if match_lines(p_str, &file_content, &options) {
|
if match_lines(p_str, &file_content, &options) {
|
||||||
@@ -219,33 +175,29 @@ fn find_text_files(options: &Options, dir_path: &Path) {
|
|||||||
}
|
}
|
||||||
}, &|p| {
|
}, &|p| {
|
||||||
total_dir_count_cell.replace_with(|&mut c| c + 1);
|
total_dir_count_cell.replace_with(|&mut c| c + 1);
|
||||||
match p.to_str() {
|
if let Some(p_str) = p.to_str() {
|
||||||
None => (),
|
if (!options.scan_dot_git_dir) && p_str.ends_with("/.git") {
|
||||||
Some(p_str) => {
|
if options.verbose { clear_n_print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str)); }
|
||||||
if (! options.scan_dot_git) && p_str.ends_with("/.git") {
|
return false;
|
||||||
if options.verbose {
|
|
||||||
print_lastline("");
|
|
||||||
print_message(MessageType::INFO, &format!("Skip .git dir: {}", p_str));
|
|
||||||
}
|
}
|
||||||
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
if options.skip_dot_dir && p_str.contains("/.") {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
if options.skip_link_dir && is_symlink(p) {
|
if options.skip_link_dir && is_symlink(p) {
|
||||||
if options.verbose {
|
if options.verbose { clear_n_print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str)); }
|
||||||
print_lastline("");
|
|
||||||
print_message(MessageType::INFO, &format!("Skip link dir: {}", p_str));
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
scaned_dir_count_cell.replace_with(|&mut c| c + 1);
|
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
|
true
|
||||||
}).unwrap_or(());
|
}).ok();
|
||||||
print_lastline("");
|
print_lastline(EMPTY);
|
||||||
print_message(MessageType::OK, &format!("Total dir count: {}, scaned dir count: {}",
|
print_message(MessageType::OK, &format!("Total dir count: {}, scaned dir count: {}",
|
||||||
total_dir_count_cell.into_inner(),
|
total_dir_count_cell.into_inner(),
|
||||||
scaned_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()));
|
matched_file_count_cell.into_inner()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() -> XResult<()> {
|
fn main() -> XResult<()> {
|
||||||
let options = Options::new_and_parse_args()?;
|
let options = Options::new_and_parse_args()?;
|
||||||
|
|
||||||
@@ -265,18 +216,14 @@ fn main() -> XResult<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let dir_path = match get_absolute_path(&options.dir) {
|
let dir_path = match get_absolute_path(&options.dir) {
|
||||||
None => {
|
|
||||||
return Err(new_box_error(&format!("Cannot find dir: {}", options.dir)));
|
|
||||||
},
|
|
||||||
Some(path) => path,
|
Some(path) => path,
|
||||||
|
None => return Err(new_box_error(&format!("Cannot find dir: {}", options.dir))),
|
||||||
};
|
};
|
||||||
let start = SystemTime::now();
|
let start = SystemTime::now();
|
||||||
match options.target.as_str() {
|
match options.target.as_str() {
|
||||||
"huge" | "hugefile" => find_huge_files(&options, &dir_path),
|
"huge" | "hugefile" => find_huge_files(&options, &dir_path),
|
||||||
"text" => find_text_files(&options, &dir_path),
|
"text" => find_text_files(&options, &dir_path),
|
||||||
unknown => {
|
others => return Err(new_box_error(&format!("Unknown command: {}", others))),
|
||||||
return Err(new_box_error(&format!("Unknown command: {}", unknown)));
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
let cost_millis = SystemTime::now().duration_since(start.clone()).unwrap().as_millis();
|
let cost_millis = SystemTime::now().duration_since(start.clone()).unwrap().as_millis();
|
||||||
print_message(MessageType::OK, &format!("Finding finished, cost {} ms", cost_millis));
|
print_message(MessageType::OK, &format!("Finding finished, cost {} ms", cost_millis));
|
||||||
|
|||||||
16
src/opt.rs
16
src/opt.rs
@@ -1,10 +1,5 @@
|
|||||||
|
|
||||||
use argparse::{ArgumentParser, StoreTrue, Store};
|
use argparse::{ArgumentParser, StoreTrue, Store};
|
||||||
|
use rust_util::{ XResult, util_size::*, };
|
||||||
use rust_util::{
|
|
||||||
XResult,
|
|
||||||
util_size::*,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
pub version: bool,
|
pub version: bool,
|
||||||
@@ -19,9 +14,10 @@ pub struct Options {
|
|||||||
pub filter_large_line: bool,
|
pub filter_large_line: bool,
|
||||||
pub large_line_size: String,
|
pub large_line_size: String,
|
||||||
pub parsed_large_line_size: u64,
|
pub parsed_large_line_size: u64,
|
||||||
pub scan_dot_git: bool,
|
pub scan_dot_git_dir: bool,
|
||||||
pub skip_dot_dir: bool,
|
pub skip_dot_dir: bool,
|
||||||
pub skip_link_dir: bool,
|
pub skip_link_dir: bool,
|
||||||
|
pub skip_target_dir: bool,
|
||||||
pub filter_file_name: String,
|
pub filter_file_name: String,
|
||||||
pub filter_line_content: String,
|
pub filter_line_content: String,
|
||||||
pub verbose: bool,
|
pub verbose: bool,
|
||||||
@@ -43,9 +39,10 @@ impl Options {
|
|||||||
filter_large_line: false,
|
filter_large_line: false,
|
||||||
large_line_size: String::from("10KB"),
|
large_line_size: String::from("10KB"),
|
||||||
parsed_large_line_size: 0u64,
|
parsed_large_line_size: 0u64,
|
||||||
scan_dot_git: false,
|
scan_dot_git_dir: false,
|
||||||
skip_dot_dir: false,
|
skip_dot_dir: false,
|
||||||
skip_link_dir: false,
|
skip_link_dir: false,
|
||||||
|
skip_target_dir: false,
|
||||||
filter_file_name: String::new(),
|
filter_file_name: String::new(),
|
||||||
filter_line_content: String::new(),
|
filter_line_content: String::new(),
|
||||||
verbose: false,
|
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.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.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.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_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_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_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.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");
|
ap.refer(&mut self.version).add_option(&["-v", "--version"], StoreTrue, "Print version");
|
||||||
|
|||||||
Reference in New Issue
Block a user