From dccc86be272091f4f14be52951f8c02996318cd0 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 29 May 2026 14:17:14 +0800 Subject: Enable color feature for clap and fix help and error rendering --- CHANGELOG.md | 4 +++- examples/example-clap-binding/Cargo.toml | 8 ++++++-- examples/example-clap-binding/src/main.rs | 15 ++++++++++----- mingling_macros/src/dispatcher_clap.rs | 11 ++++++----- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36806ce..fd8f36a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ ### Fixes: -None +1. **\[macros:dispatcher_clap\]** Fixed the issue where clap error messages (`DisplayHelp` and parse errors from `try_parse_from`) could not output ANSI + - For error paths, use `e.render().ansi()` instead of `e.to_string()` to prevent ANSI codes from being stripped by `strip_str` in `StyledStr::Display` + - For help info paths, use with `BasicProgramSetup`, output ANSI-colored help content through the mingling framework's `render_help` flow ### Optimizings: diff --git a/examples/example-clap-binding/Cargo.toml b/examples/example-clap-binding/Cargo.toml index d261786..a019442 100644 --- a/examples/example-clap-binding/Cargo.toml +++ b/examples/example-clap-binding/Cargo.toml @@ -11,5 +11,9 @@ features = ["clap"] # Import `clap` to your project [dependencies.clap] version = "4.6.1" -# Enable `derive` features -features = ["derive"] +features = [ + # Enable `derive` feature to support `clap::Parser` + "derive", + # Enable `color` feature to support ANSI colors + "color", +] diff --git a/examples/example-clap-binding/src/main.rs b/examples/example-clap-binding/src/main.rs index fd813a6..aed437d 100644 --- a/examples/example-clap-binding/src/main.rs +++ b/examples/example-clap-binding/src/main.rs @@ -3,8 +3,8 @@ //! > This example demonstrates how to bind clap_derive to Mingling //! //! **Note**: -//! If the `error` parameter of the `dispatcher_clap!` macro is enabled, parameters will be parsed using `try_parse_from` -//! This will cause clap's ColorChoice output to be plain text without ANSI colors +//! If the `error` parameter of the `dispatcher_clap!` macro is enabled, arguments will be parsed using `try_parse_from`. +//! If you need such output to support ANSI colors, enable the `color` feature of `clap`. //! //! Run: //! ```bash @@ -38,21 +38,26 @@ //! For more information, try '--help'. //! ``` -use mingling::{macros::dispatcher_clap, prelude::*, Groupped}; +use mingling::{macros::dispatcher_clap, prelude::*, setup::BasicProgramSetup, Groupped}; fn main() { let mut program = ThisProgram::new(); + // --------- IMPORTANT --------- + // Introduce BasicProgramSetup to support ["--help", "-h"] options + program.with_setup(BasicProgramSetup); + // Set clap help output mode program.stdout_setting.clap_help_print_behaviour = - mingling::ClapHelpPrintBehaviour::PrintDirectly; - // mingling::ClapHelpPrintBehaviour::WriteToRenderResult + mingling::ClapHelpPrintBehaviour::WriteToRenderResult; + // mingling::ClapHelpPrintBehaviour::PrintDirectly // // PrintDirectly: // Let Clap print help information directly to stdout // // WriteToRenderResult: // Capture Clap's help information and write to RenderResult + // --------- IMPORTANT --------- program.with_dispatcher(CMDGreet); program.exec_and_exit(); diff --git a/mingling_macros/src/dispatcher_clap.rs b/mingling_macros/src/dispatcher_clap.rs index 42b5276..8ef2125 100644 --- a/mingling_macros/src/dispatcher_clap.rs +++ b/mingling_macros/src/dispatcher_clap.rs @@ -173,7 +173,7 @@ pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream match <#struct_name as ::clap::Parser>::try_parse_from(clap_args) { Ok(parsed) => parsed.to_chain(), Err(e) => { - return #error_struct::new(e.to_string()).to_render() + return #error_struct::new(format!("{}", e.render().ansi())).to_render() }, } } @@ -205,15 +205,16 @@ pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream #[allow(non_snake_case)] #[::mingling::macros::help] fn #help_fn_name(_prev: #struct_name) { + use std::io::Write; use clap::ColorChoice; let this = ::mingling::this::<#program_path>(); match this.stdout_setting.clap_help_print_behaviour { ::mingling::ClapHelpPrintBehaviour::WriteToRenderResult => { - <#struct_name as ::clap::CommandFactory>::command() - .color(ColorChoice::Always) - .write_help(__renderer_inner_result) - .unwrap(); + let mut cmd = <#struct_name as ::clap::CommandFactory>::command() + .color(ColorChoice::Always); + let styled = cmd.render_help(); + write!(__renderer_inner_result, "{}", styled.ansi()).unwrap(); } ::mingling::ClapHelpPrintBehaviour::PrintDirectly => { let mut command = <#struct_name as ::clap::CommandFactory>::command(); -- cgit