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 --- mingling_macros/src/dispatcher_clap.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'mingling_macros/src') 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