diff --git a/Cargo.lock b/Cargo.lock index 6e8e84a..412e891 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -710,7 +710,7 @@ dependencies = [ [[package]] name = "runrs" -version = "0.2.6" +version = "0.2.7" dependencies = [ "reqwest", "rust_util", diff --git a/Cargo.toml b/Cargo.toml index eba5dff..6bf9885 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runrs" -version = "0.2.6" +version = "0.2.7" edition = "2018" license = "MIT/Apache-2.0" description = "A Tool for Run Rust Scripts" diff --git a/src/list.rs b/src/list.rs index 70f9e30..7a7a386 100644 --- a/src/list.rs +++ b/src/list.rs @@ -21,7 +21,7 @@ struct ScriptMeta { script_sha256: String, } -pub fn list_scripts() { +pub fn list_scripts(filter: Option<&String>) { debugging!("Loading URL: {}", FILE_META); let response = reqwest::blocking::get(FILE_META).unwrap_or_else(|e| { failure_and_exit!("Get file-meta.json failed: {}", e); @@ -35,10 +35,10 @@ pub fn list_scripts() { }); debugging!("Response text: {}", &text); + let user_home = get_user_home().expect("Get user home failed!"); let script_meta_map: BTreeMap = serde_json::from_str(&text).expect("Parse script-meta.json failed."); let mut messages = vec![]; - messages.push(format!("Found {} script(s):", script_meta_map.len())); for (_, script_meta) in &script_meta_map { let script_name = &script_meta.script_name; let real_script_name = if script_name.ends_with(SCRIPT_HYPHEN_EXT) { @@ -46,7 +46,14 @@ pub fn list_scripts() { } else { script_name.to_string() }; - let user_home = get_user_home().expect("Get user home failed!"); + + if let Some(filter) = filter { + if !real_script_name.contains(filter) { + // NOT CONTAINS `filter`, SKIP + continue; + } + } + let full_script_path = PathBuf::from(&user_home).join("bin").join(&real_script_name); let is_script_exists = full_script_path.is_file(); let script_sha256 = if is_script_exists { @@ -59,11 +66,16 @@ pub fn list_scripts() { let script_size = rust_util::util_size::get_display_size(script_meta.script_length as i64); let padding_length = 40 - real_script_name.len(); messages.push(format!("- {} {} {} {}", - iff!(is_script_exists, iff!(is_script_matches, "✅", "✔️ "), "❌"), + iff!(is_script_exists, iff!(is_script_matches, "✅", "✔️ "), "➖"), real_script_name, ".".repeat(iff!(padding_length < 1, 1, padding_length)), script_size, )); } + if filter.is_some() { + messages.insert(0, format!("Total {} script(s), found {} script(s):", script_meta_map.len(), messages.len())); + } else { + messages.insert(0, format!("Found {} script(s):", script_meta_map.len())); + } success!("{}", messages.join("\n")); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3c18ee5..313de38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -69,7 +69,8 @@ fn main() { return; } if first_argument == "--list" || first_argument == "-l" { - list_scripts(); + let filter = args.get(1); + list_scripts(filter); return; } if first_argument == "--install" || first_argument == "-i" {