mirror of
https://github.com/jht5945/finding.git
synced 2025-12-27 21:20:05 +08:00
refactor
This commit is contained in:
91
src/main.rs
91
src/main.rs
@@ -1,6 +1,10 @@
|
||||
extern crate argparse;
|
||||
extern crate rust_util;
|
||||
|
||||
use std::{
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use argparse::{ArgumentParser, StoreTrue, Store};
|
||||
use rust_util::*;
|
||||
|
||||
@@ -15,7 +19,39 @@ Written by Hatter Jiang
|
||||
"#, VERSION);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
fn find_huge_files(huge_file_size: &String, dir_path: &Path) {
|
||||
let huge_file_size_bytes = match parse_size(&huge_file_size) {
|
||||
Err(err) => {
|
||||
print_message(MessageType::ERROR, &format!("Parse size failed: {}", err));
|
||||
return;
|
||||
},
|
||||
Ok(bytes) => bytes as u64,
|
||||
};
|
||||
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
|
||||
match p.metadata() {
|
||||
Err(_) => (),
|
||||
Ok(metadata) => {
|
||||
let len = metadata.len();
|
||||
if len >= huge_file_size_bytes {
|
||||
match p.to_str() {
|
||||
None => (),
|
||||
Some(p_str) => {
|
||||
print_lastline("");
|
||||
print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}, &|p| {
|
||||
match p.to_str() {
|
||||
None => (),
|
||||
Some(p_str) => print_lastline(&format!("Scanning: {}", p_str)),
|
||||
}
|
||||
true
|
||||
}).unwrap_or(());
|
||||
print_lastline("");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut version = false;
|
||||
@@ -26,9 +62,10 @@ fn main() {
|
||||
let mut ap = ArgumentParser::new();
|
||||
ap.set_description("finding - command line find tool.");
|
||||
ap.refer(&mut target).add_option(&["-t", "--target"], Store, "Target, text, huge[file], default text");
|
||||
ap.refer(&mut dir).add_option(&["-d", "--dir"], Store, "Target directory, default current dir(.)");
|
||||
ap.refer(&mut huge_file_size).add_option(&["--huge-file"], Store, "Huge file size, default 100M");
|
||||
ap.refer(&mut version).add_option(&["-v", "--version"], StoreTrue, "Print version");
|
||||
ap.refer(&mut dir).add_argument("DIR", Store, "Dir name, default current dir(.)");
|
||||
//ap.refer(&mut dir).add_argument("DIR", Store, "Dir name, default current dir(.)");
|
||||
ap.parse_args_or_exit();
|
||||
}
|
||||
|
||||
@@ -45,53 +82,7 @@ fn main() {
|
||||
Some(path) => path,
|
||||
};
|
||||
match target.as_str() {
|
||||
"huge" | "hugefile" => {
|
||||
let huge_file_size_bytes = match parse_size(&huge_file_size) {
|
||||
Err(err) => {
|
||||
print_message(MessageType::ERROR, &format!("Parse size failed: {}", err));
|
||||
return;
|
||||
},
|
||||
Ok(bytes) => bytes as u64,
|
||||
};
|
||||
walk_dir(&dir_path, &|_, _| (/* do not process error */), &|p| {
|
||||
match p.metadata() {
|
||||
Err(_) => (),
|
||||
Ok(metadata) => {
|
||||
let len = metadata.len();
|
||||
if len >= huge_file_size_bytes {
|
||||
match p.to_str() {
|
||||
None => (),
|
||||
Some(p_str) => {
|
||||
print_lastline("");
|
||||
print_message(MessageType::OK, &format!("{} [{}]", p_str, get_display_size(len as i64)));
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}, &|p| {
|
||||
match p.to_str() {
|
||||
None => (),
|
||||
Some(p_str) => print_lastline(&format!("Scanning: {}", p_str)),
|
||||
}
|
||||
true
|
||||
}).unwrap_or(());
|
||||
print_lastline("");
|
||||
return;
|
||||
},
|
||||
_ => (),
|
||||
"huge" | "hugefile" => find_huge_files(&huge_file_size, &dir_path),
|
||||
unknown => print_message(MessageType::ERROR, &format!("Unknown command: {}", unknown)),
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------
|
||||
println!("{:?}", get_home_path());
|
||||
println!("{:?}", get_absolute_path("."));
|
||||
println!("{:?}", get_absolute_path("../"));
|
||||
println!("{:?}", get_absolute_path("~"));
|
||||
println!("{:?}", get_absolute_path("~/.jssp"));
|
||||
println!("{:?}", get_absolute_path("~/.jsspx"));
|
||||
|
||||
|
||||
walk_dir(get_home_path().unwrap().as_path(), &|_, _| (), &|p| println!("{:?}",p), &|_| false).unwrap();
|
||||
|
||||
println!("Hello, world!");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user