From 7bcaafcfc8c9adb0567496c660c9ef987187e1cd Mon Sep 17 00:00:00 2001 From: Rickard Natt och Dag Date: Mon, 13 Mar 2023 11:15:15 +0100 Subject: [PATCH] feat(run): add fuzzy filtering --- Cargo.lock | 58 ++++++++++++++++++++++++++++++--------------- Cargo.toml | 2 +- src/commands/run.rs | 14 +++++++---- src/lib.rs | 4 ++-- 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6174d86..89cbe6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,9 +30,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "block-buffer" -version = "0.10.3" +version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69cce20737498f97b993470a6e536b8523f0af7892a4f928cceb1ac5e52ebe7e" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ "generic-array", ] @@ -79,7 +79,7 @@ dependencies = [ "heck", "proc-macro-error", "proc-macro2", - "quote 1.0.23", + "quote 1.0.25", "syn 1.0.109", ] @@ -153,6 +153,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af3c796f3b0b408d9fd581611b47fa850821fcb84aa640b83a3c1a5be2d691f2" dependencies = [ "console", + "fuzzy-matcher", "shell-words", "tempfile", "zeroize", @@ -225,6 +226,15 @@ dependencies = [ "instant", ] +[[package]] +name = "fuzzy-matcher" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54614a3312934d066701a80f20f15fa3b56d67ac7722b39eea5b4c9dd1d66c94" +dependencies = [ + "thread_local", +] + [[package]] name = "generic-array" version = "0.14.6" @@ -348,9 +358,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.139" +version = "0.2.140" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c" [[package]] name = "linux-raw-sys" @@ -420,7 +430,7 @@ dependencies = [ "pest", "pest_meta", "proc-macro2", - "quote 1.0.23", + "quote 1.0.25", "syn 1.0.109", ] @@ -443,7 +453,7 @@ checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" dependencies = [ "proc-macro-error-attr", "proc-macro2", - "quote 1.0.23", + "quote 1.0.25", "syn 1.0.109", "version_check", ] @@ -455,15 +465,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" dependencies = [ "proc-macro2", - "quote 1.0.23", + "quote 1.0.25", "version_check", ] [[package]] name = "proc-macro2" -version = "1.0.51" +version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d727cae5b39d21da60fa540906919ad737832fe0b1c165da3a34d6548c849d6" +checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224" dependencies = [ "unicode-ident", ] @@ -476,9 +486,9 @@ checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" [[package]] name = "quote" -version = "1.0.23" +version = "1.0.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +checksum = "5308e8208729c3e1504a6cfad0d5daacc4614c9a2e65d1ea312a34b5cb00fe84" dependencies = [ "proc-macro2", ] @@ -542,21 +552,21 @@ checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" [[package]] name = "serde" -version = "1.0.152" +version = "1.0.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +checksum = "71f2b4817415c6d4210bfe1c7bfcf4801b2d904cb4d0e1a8fdb651013c9e86b8" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.152" +version = "1.0.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +checksum = "d071a94a3fac4aff69d023a7f411e33f40f3483f8c5190b1953822b6b76d7630" dependencies = [ "proc-macro2", - "quote 1.0.23", + "quote 1.0.25", "syn 1.0.109", ] @@ -629,7 +639,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", - "quote 1.0.23", + "quote 1.0.25", "unicode-ident", ] @@ -680,10 +690,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5420d42e90af0c38c3290abcca25b9b3bdf379fc9f55c528f53a269d9c9a267e" dependencies = [ "proc-macro2", - "quote 1.0.23", + "quote 1.0.25", "syn 1.0.109", ] +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if 1.0.0", + "once_cell", +] + [[package]] name = "toml" version = "0.5.11" diff --git a/Cargo.toml b/Cargo.toml index df1ffd8..9788bbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ clap = { version = "4.1.7", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" handlebars = "4.0.0" -dialoguer = "0.10.0" +dialoguer = { version = "0.10.3", features = ["fuzzy-select"] } colored = "2.0.0" include-dir-macro = "0.2" indicatif = "0.16.2" diff --git a/src/commands/run.rs b/src/commands/run.rs index c1b8669..8bc1497 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -1,15 +1,14 @@ use crate::utils::{helpers, node, pkg_json}; -use colored::*; -use dialoguer::{theme::ColorfulTheme, Select}; +use dialoguer::{theme::ColorfulTheme, FuzzySelect}; use helpers::Result; -pub fn run() -> Result<()> { +pub fn run(name: Option) -> Result<()> { let pkg = pkg_json::Package::new()?; let selections: Vec<_> = pkg .scripts .iter() - .map(|(script, cmd)| format!("{} ({})", script.bold(), cmd.magenta())) + .map(|(script, cmd)| format!("{} ({})", script, cmd)) .collect(); let scripts: Vec<_> = pkg @@ -18,8 +17,13 @@ pub fn run() -> Result<()> { .map(|script| script.to_string()) .collect(); - let selection = Select::with_theme(&ColorfulTheme::default()) + let selection = FuzzySelect::with_theme(&ColorfulTheme::default()) + .default(0) .with_prompt("Select a script to run") + .with_initial_text(match name { + Some(name) => name, + None => "".to_string(), + }) .items(&selections) .interact() .unwrap(); diff --git a/src/lib.rs b/src/lib.rs index a6ce006..a23206c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -106,7 +106,7 @@ enum Cli { Rescript { name: String }, /// Display and execute available package.json scripts - Run, + Run { name: Option }, /// Uninstall a Node package Uninstall { @@ -152,7 +152,7 @@ pub fn run() -> Result<()> { Cli::Remove(RemoveCommand::Prettier) => remove::prettier()?, Cli::Rescript { name } => rescript::run(name)?, - Cli::Run => run::run()?, + Cli::Run { name } => run::run(name)?, Cli::Uninstall { name } => uninstall::run(name)?, Cli::UpdateDependencies => update_dependencies::run()?,