diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-11 18:54:27 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-11 18:55:52 +0800 |
| commit | 9634eee9a3277e10b0cece42c603aaff801446fd (patch) | |
| tree | 219bd8ac1be2377e07f66b8c8c1ace3df16c38aa /examples | |
| parent | 3fcd049e738ad7e4377609104a1dce5f4dfa2cf9 (diff) | |
Add example-completion and update workspace dependencies
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/example-basic/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/example-completion/Cargo.toml | 9 | ||||
| -rw-r--r-- | examples/example-completion/src/main.rs | 114 | ||||
| -rw-r--r-- | examples/example-general-renderer/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/example-picker/Cargo.toml | 2 |
5 files changed, 129 insertions, 3 deletions
diff --git a/examples/example-basic/Cargo.toml b/examples/example-basic/Cargo.toml index eb10e2a..aeb1f6f 100644 --- a/examples/example-basic/Cargo.toml +++ b/examples/example-basic/Cargo.toml @@ -4,6 +4,6 @@ version.workspace = true edition = "2024" [dependencies] -mingling.workspace = true +mingling = { path = "../../mingling" } tokio.workspace = true serde.workspace = true diff --git a/examples/example-completion/Cargo.toml b/examples/example-completion/Cargo.toml new file mode 100644 index 0000000..02c5e33 --- /dev/null +++ b/examples/example-completion/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "example-completion" +version.workspace = true +edition = "2024" + +[dependencies] +mingling = { path = "../../mingling", features = ["comp", "parser"] } +tokio.workspace = true +serde.workspace = true diff --git a/examples/example-completion/src/main.rs b/examples/example-completion/src/main.rs new file mode 100644 index 0000000..06a0969 --- /dev/null +++ b/examples/example-completion/src/main.rs @@ -0,0 +1,114 @@ +//! `Mingling` Example - Completion +//! +//! # How to Deploy +//! 1. Enable the `comp` feature +//! ```toml +//! mingling = { version = "0.1.5", features = [ +//! "comp", // Enable this feature +//! "parser" +//! ] } +//! ``` +//! +//! 2. Write `build.rs` to generate completion scripts at compile time +//! ```rust +//! use mingling::build::{build_comp_scripts, build_comp_scripts_with_bin_name}; +//! fn main() { +//! // Generate completion scripts for the current program +//! build_comp_scripts().unwrap(); +//! +//! // Or specify a specific name +//! // build_comp_scripts_with_bin_name("your_bin").unwrap(); +//! } +//! ``` +//! +//! 3. Write `main.rs`, adding completion logic for your command entry point +//! 4. Execute `cargo install --path ./`, then run the corresponding completion script in your shell + +use mingling::{ + AnyOutput, Groupped, ShellContext, Suggest, + macros::{chain, completion, dispatcher, gen_program, r_println, renderer, suggest}, + marker::NextProcess, + parser::{Pickable, Picker}, +}; + +// Define dispatcher `FruitCommand`, directing subcommand "fruit" to `FruitEntry` +dispatcher!("fruit", FruitCommand => FruitEntry); + +#[completion(FruitEntry)] +fn comp_fruit_command(ctx: &ShellContext) -> Suggest { + if ctx.current_word.starts_with("-") { + return suggest! { + "--name": "Fruit name", + "--type": "Fruit type" + }; + } + if ctx.previous_word == "--name" { + return suggest!(); + } + if ctx.previous_word == "--type" { + return suggest! { + "apple", "banana" + }; + } + return suggest!(); +} + +#[tokio::main] +async fn main() { + let mut program = ThisProgram::new(); + program.with_dispatcher(CompletionDispatcher); // Add completion dispatcher + program.with_dispatcher(FruitCommand); + program.exec().await; +} + +#[derive(Groupped)] +struct FruitInfo { + name: String, + fruit_type: FruitType, +} + +#[derive(Default, Debug)] +enum FruitType { + #[default] + Apple, + Banana, + Other(String), +} + +impl Pickable for FruitType { + type Output = FruitType; + + fn pick(args: &mut mingling::parser::Argument, flag: mingling::Flag) -> Option<Self::Output> { + let name = args.pick_argument(flag); + match name { + Some(name) => match name.as_str() { + "apple" => Some(FruitType::Apple), + "banana" => Some(FruitType::Banana), + other => Some(FruitType::Other(other.to_string())), + }, + None => None, + } + } +} + +#[chain] +async fn parse_fruit_info(prev: FruitEntry) -> NextProcess { + let picker = Picker::<ThisProgram>::from(prev.inner); + let (fruit_name, fruit_type) = picker.pick("--name").pick("--type").unpack_directly(); + let info = FruitInfo { + name: fruit_name, + fruit_type, + }; + AnyOutput::new(info).route_renderer() +} + +#[renderer] +fn render_fruit(prev: FruitInfo) { + if let FruitType::Other(other) = prev.fruit_type { + r_println!("Fruit name: {}, Type: {:?} (Unknown)", prev.name, other); + } else { + r_println!("Fruit name: {}, Type: {:?}", prev.name, prev.fruit_type); + } +} + +gen_program!(); diff --git a/examples/example-general-renderer/Cargo.toml b/examples/example-general-renderer/Cargo.toml index 22403a2..3457b99 100644 --- a/examples/example-general-renderer/Cargo.toml +++ b/examples/example-general-renderer/Cargo.toml @@ -4,6 +4,9 @@ version.workspace = true edition = "2024" [dependencies] -mingling.workspace = true +mingling = { path = "../../mingling", features = [ + "parser", + "general_renderer", +] } tokio.workspace = true serde.workspace = true diff --git a/examples/example-picker/Cargo.toml b/examples/example-picker/Cargo.toml index 2e514f7..dfc24a1 100644 --- a/examples/example-picker/Cargo.toml +++ b/examples/example-picker/Cargo.toml @@ -4,6 +4,6 @@ version.workspace = true edition = "2024" [dependencies] -mingling.workspace = true +mingling = { path = "../../mingling", features = ["parser"] } tokio.workspace = true serde.workspace = true |
