Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Interactive mode #61

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading