Skip to content

Commit

Permalink
feat(run): add fuzzy filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Rickard Natt och Dag committed Mar 13, 2023
1 parent b854dc8 commit 7bcaafc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 27 deletions.
58 changes: 39 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 9 additions & 5 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -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<String>) -> 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
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ enum Cli {
Rescript { name: String },

/// Display and execute available package.json scripts
Run,
Run { name: Option<String> },

/// Uninstall a Node package
Uninstall {
Expand Down Expand Up @@ -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()?,
Expand Down

0 comments on commit 7bcaafc

Please sign in to comment.