Skip to content

Commit

Permalink
feat: Interactive mode (#61)
Browse files Browse the repository at this point in the history
* WIP

* WIP

* done
  • Loading branch information
shixinhuang99 committed Mar 7, 2024
1 parent e49bf6f commit 04876c5
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 13 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ path = "src/main.rs"
anyhow = "1.0.70"
chrono = "0.4.24"
clap = { version = "4.1.13", features = ["derive"] }
dialoguer = { version = "0.11.0", default-features = false, features = [
"fuzzy-select",
] }
dircpy = { version = "0.3.15", default-features = false }
directories = "5.0.1"
fs-err = "2.9.0"
Expand Down
16 changes: 12 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ pub struct Cli {
/// Display of scafalra's data storage location
#[arg(long)]
pub proj_dir: bool,

/// Interactive mode
#[arg(short, long, global = true)]
pub interactive: bool,
}

#[derive(Subcommand)]
Expand Down Expand Up @@ -54,13 +58,17 @@ pub struct ListArgs {

#[derive(Args, Debug)]
pub struct RemoveArgs {
pub names: Vec<String>,
/// Template name List
pub names: Option<Vec<String>>,
}

#[derive(Args, Debug)]
pub struct RenameArgs {
pub name: String,
pub new_name: String,
/// Template name
pub name: Option<String>,

/// New Template name
pub new_name: Option<String>,
}

#[derive(Args, Debug)]
Expand Down Expand Up @@ -104,7 +112,7 @@ pub struct AddArgs {
#[derive(Args, Debug)]
pub struct CreateArgs {
/// Template name
pub name: String,
pub name: Option<String>,

/// Specified directory(defaults to the current directory)
pub directory: Option<PathBuf>,
Expand Down
33 changes: 33 additions & 0 deletions src/interactive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use anyhow::Result;
use dialoguer::{theme::ColorfulTheme, FuzzySelect, Input, MultiSelect};

pub fn fuzzy_select(itmes: Vec<&String>) -> Result<Option<&String>> {
let idx = FuzzySelect::with_theme(&ColorfulTheme::default())
.with_prompt(
"Typing to search, use ↑↓ to pick, hit 'Enter' to confirm, or hit 'Esc' to exit",
)
.items(&itmes)
.highlight_matches(true)
.interact_opt()?;

Ok(idx.map(|i| itmes[i]))
}

pub fn multi_select(itmes: Vec<&String>) -> Result<Option<Vec<&String>>> {
let vi = MultiSelect::with_theme(&ColorfulTheme::default())
.with_prompt(
"Use ↑↓ to pick, hit 'Enter' to confirm, hit 'Space' to select, or hit 'Esc' to exit",
)
.items(&itmes)
.interact_opt()?;

Ok(vi.map(|vi| vi.into_iter().map(|i| itmes[i]).collect()))
}

pub fn input(prompt: &str) -> Result<String> {
let ret = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt(prompt)
.interact_text()?;

Ok(ret)
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod cli;
mod colorize;
mod config;
mod debug;
mod interactive;
mod json;
mod path_ext;
mod repository;
Expand Down Expand Up @@ -46,6 +47,10 @@ fn run() -> Result<()> {
return Ok(());
}

if cli.interactive {
scafalra.interactive_mode = true;
}

if let Some(command) = cli.command {
match command {
Command::List(args) => scafalra.list(args),
Expand Down

0 comments on commit 04876c5

Please sign in to comment.