-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add a readme.rs example - Use VHS to generate the output gifs - Adds a script to generate and publish all the gifs in one go - Images are published to VHS rather than committed to the repo to avoid bloating the repo size See <https://github.com/charmbracelet/vhs> for more info on VHS Alternative to: #258
- Loading branch information
Showing
12 changed files
with
438 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
//! The purpose of this example is to provide simple examples of how to use each of the dialoguer | ||
//! prompts. | ||
use std::{env::args, thread, time::Duration}; | ||
|
||
#[cfg(feature = "fuzzy-select")] | ||
use dialoguer::FuzzySelect; | ||
use dialoguer::{theme::ColorfulTheme, Confirm, MultiSelect, Password, Select, Sort}; | ||
|
||
fn main() -> dialoguer::Result<()> { | ||
match args().nth(1) { | ||
None => println!("No argument provided"), | ||
Some(arg) => run(arg)?, | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn run(arg: String) -> Result<(), dialoguer::Error> { | ||
match arg.as_str() { | ||
"confirm" => confirm()?, | ||
"confirm-with-default" => confirm_with_default()?, | ||
"input" => input()?, | ||
"password" => password()?, | ||
"editor" => editor()?, | ||
"select" => select()?, | ||
"multi-select" => multi_select()?, | ||
#[cfg(feature = "fuzzy-select")] | ||
"fuzzy-select" => fuzzy_select()?, | ||
"sort" => sort()?, | ||
_ => println!("Invalid argument"), | ||
} | ||
thread::sleep(Duration::from_secs(3)); // give the VHS tape time to capture the effect | ||
Ok(()) | ||
} | ||
|
||
fn confirm() -> dialoguer::Result<()> { | ||
if Confirm::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("Do you want to continue?") | ||
.interact()? | ||
{ | ||
println!("Looks like you want to continue"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn confirm_with_default() -> dialoguer::Result<()> { | ||
if Confirm::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("Do you want to continue?") | ||
.default(true) | ||
.interact()? | ||
{ | ||
println!("Looks like you want to continue"); | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn input() -> dialoguer::Result<()> { | ||
let name: String = dialoguer::Input::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("What is your name?") | ||
.interact()?; | ||
println!("Hello, {name}"); | ||
Ok(()) | ||
} | ||
|
||
fn password() -> dialoguer::Result<()> { | ||
let password: String = Password::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("Enter your password") | ||
.interact()?; | ||
println!("Your password is: {password}"); | ||
Ok(()) | ||
} | ||
|
||
fn editor() -> dialoguer::Result<()> { | ||
match dialoguer::Editor::new().edit("Some content")? { | ||
Some(content) => println!("Content: {content:?}"), | ||
None => println!("File was not saved"), | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn select() -> dialoguer::Result<()> { | ||
let items = vec!["Apple", "Banana", "Cherry"]; | ||
let selection = Select::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("What is your favourite fruit?") | ||
.items(&items) | ||
.interact()?; | ||
println!("You picked: {selection}", selection = items[selection]); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(feature = "fuzzy-select")] | ||
fn fuzzy_select() -> dialoguer::Result<()> { | ||
let items = vec!["Apple", "Banana", "Cherry"]; | ||
let selection = FuzzySelect::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("What is your favourite fruit?") | ||
.items(&items) | ||
.interact()?; | ||
println!("You picked: {selection}", selection = items[selection]); | ||
Ok(()) | ||
} | ||
|
||
fn multi_select() -> dialoguer::Result<()> { | ||
let items = vec!["Apple", "Banana", "Cherry"]; | ||
let selection = MultiSelect::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("What are your favourite fruits?") | ||
.items(&items) | ||
.interact()?; | ||
let selected_items: Vec<_> = selection.iter().map(|i| items[*i]).collect(); | ||
println!("You picked: {selected_items:?}"); | ||
Ok(()) | ||
} | ||
|
||
fn sort() -> dialoguer::Result<()> { | ||
let items = vec!["Apple", "Banana", "Cherry"]; | ||
let selection = Sort::with_theme(&ColorfulTheme::default()) | ||
.with_prompt("Sort the fruits") | ||
.items(&items) | ||
.interact()?; | ||
let sorted_items: Vec<_> = selection.iter().map(|i| items[*i]).collect(); | ||
println!("You sorted: {sorted_items:?}"); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Output target/confirm-with-default.gif | ||
|
||
Set Width 1200 | ||
Set Height 250 | ||
Set Theme "Aardvark Blue" | ||
|
||
Hide | ||
Type "cargo run --quiet --example readme confirm-with-default" Enter | ||
Show | ||
Sleep 2s | ||
Enter | ||
Sleep 2s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Output target/confirm.gif | ||
|
||
Set Width 1200 | ||
Set Height 250 | ||
Set Theme "Aardvark Blue" | ||
|
||
Hide | ||
Type "cargo run --quiet --example readme confirm" Enter | ||
Show | ||
Sleep 2s | ||
Type "y" | ||
Sleep 2s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Output target/editor.gif | ||
|
||
Set Width 1200 | ||
Set Height 250 | ||
Set Theme "Aardvark Blue" | ||
|
||
Hide | ||
Type "EDITOR=vim cargo run --quiet --example readme editor" Enter | ||
Show | ||
Sleep 2s | ||
Type "CHello, World!" Escape | ||
Type "ZZ" | ||
Sleep 2s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
Output target/fuzzy-select.gif | ||
|
||
Set Width 1200 | ||
Set Height 350 | ||
Set Theme "Aardvark Blue" | ||
|
||
Hide | ||
Type "cargo run --quiet --example readme --features=fuzzy-select fuzzy-select" Enter | ||
Show | ||
Sleep 2s | ||
Type "a" | ||
Sleep 1s | ||
Down | ||
Sleep 1s | ||
Enter | ||
|
||
Sleep 2s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Output target/input.gif | ||
|
||
Set Width 1200 | ||
Set Height 250 | ||
Set Theme "Aardvark Blue" | ||
|
||
Hide | ||
Type "cargo run --quiet --example readme input" Enter | ||
Show | ||
Sleep 2s | ||
Type "pksunkara" Enter | ||
Sleep 2s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Output target/multi-select.gif | ||
|
||
Set Width 1200 | ||
Set Height 350 | ||
Set Theme "Aardvark Blue" | ||
|
||
Hide | ||
Type "cargo run --quiet --example readme multi-select" Enter | ||
Show | ||
Sleep 2s | ||
Down | ||
Sleep 1s | ||
Space | ||
Sleep 1s | ||
Down | ||
Sleep 1s | ||
Space | ||
Sleep 1s | ||
Enter | ||
|
||
Sleep 2s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Output target/input.gif | ||
|
||
Set Width 1200 | ||
Set Height 250 | ||
Set Theme "Aardvark Blue" | ||
|
||
Hide | ||
Type "cargo run --quiet --example readme password" Enter | ||
Show | ||
Sleep 2s | ||
Type "Password123!" Enter | ||
Sleep 2s |
Oops, something went wrong.