mirror of
https://github.com/jht5945/finding.git
synced 2025-12-29 14:10:05 +08:00
refactor
This commit is contained in:
81
src/main.rs
81
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,37 +19,7 @@ Written by Hatter Jiang
|
||||
"#, VERSION);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
fn main() {
|
||||
let mut version = false;
|
||||
let mut target = String::from("text");
|
||||
let mut huge_file_size = String::from("100M");
|
||||
let mut dir = String::from(".");
|
||||
{
|
||||
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 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.parse_args_or_exit();
|
||||
}
|
||||
|
||||
if version {
|
||||
print_version();
|
||||
return;
|
||||
}
|
||||
|
||||
let dir_path = match get_absolute_path(&dir) {
|
||||
None => {
|
||||
print_message(MessageType::ERROR, &format!("Cannot find dir: {}", dir));
|
||||
return;
|
||||
},
|
||||
Some(path) => path,
|
||||
};
|
||||
match target.as_str() {
|
||||
"huge" | "hugefile" => {
|
||||
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));
|
||||
@@ -77,21 +51,38 @@ fn main() {
|
||||
true
|
||||
}).unwrap_or(());
|
||||
print_lastline("");
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut version = false;
|
||||
let mut target = String::from("text");
|
||||
let mut huge_file_size = String::from("100M");
|
||||
let mut dir = String::from(".");
|
||||
{
|
||||
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.parse_args_or_exit();
|
||||
}
|
||||
|
||||
if version {
|
||||
print_version();
|
||||
return;
|
||||
}
|
||||
|
||||
let dir_path = match get_absolute_path(&dir) {
|
||||
None => {
|
||||
print_message(MessageType::ERROR, &format!("Cannot find dir: {}", dir));
|
||||
return;
|
||||
},
|
||||
_ => (),
|
||||
Some(path) => path,
|
||||
};
|
||||
match target.as_str() {
|
||||
"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