diff --git a/packages/copper/Cargo.toml b/packages/copper/Cargo.toml index 72b3650..38fdcfb 100644 --- a/packages/copper/Cargo.toml +++ b/packages/copper/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "pistonite-cu" -version = "0.9.1" +version = "0.9.2" edition = "2024" description = "Battery-included common utils to speed up development of rust tools" repository = "https://github.com/Pistonite/cu" diff --git a/packages/copper/src/cli/flags.rs b/packages/copper/src/cli/flags.rs index 51961e5..ac09fac 100644 --- a/packages/copper/src/cli/flags.rs +++ b/packages/copper/src/cli/flags.rs @@ -325,7 +325,10 @@ fn handle_result(start: Instant, result: crate::Result<()>) -> std::process::Exi } } -fn reset_color() { +/// Reset terminal color +/// +/// **You do not need to call this if using the `#[cu::cli]` macro**. +pub fn reset_color() { use std::io::IsTerminal as _; use std::io::Write as _; let mut stdout = std::io::stdout(); diff --git a/packages/copper/src/cli/mod.rs b/packages/copper/src/cli/mod.rs index dd1c1bd..6c7cf0b 100644 --- a/packages/copper/src/cli/mod.rs +++ b/packages/copper/src/cli/mod.rs @@ -138,10 +138,10 @@ mod flags; #[cfg(all(feature = "coroutine", feature = "cli"))] pub use flags::__co_run; #[cfg(feature = "cli")] -pub use flags::{__run, Flags, print_help, try_parse}; +pub use flags::{__run, Flags, print_help, reset_color, try_parse}; mod print_init; -pub use print_init::{DefaultLogConfig, LogConfig, level}; +pub use print_init::{DefaultLogConfig, LogConfig, init_options, level}; mod macros; pub use macros::__print_with_level; diff --git a/packages/copper/src/cli/print_init.rs b/packages/copper/src/cli/print_init.rs index f18abfd..6b17e66 100644 --- a/packages/copper/src/cli/print_init.rs +++ b/packages/copper/src/cli/print_init.rs @@ -30,10 +30,42 @@ pub fn level(lv: &str) { ); } -/// Set global print options. This is usually called from clap args +/// Set global CLI options +/// +/// **You do not need to call this if using the `#[cu::cli]` macro**. +/// This is useful to initialize logging without using a cu-compatible command line args, +/// say for a program that should not use clap to parse its arguments. +/// +/// ```rust,no_run +/// # use pistonite_cu as cu; +/// use std::sync::Arc; +/// +/// fn main() { +/// cu::cli::init_options( +/// cu::lv::Color::Auto, +/// cu::lv::Print::Normal, +/// None, +/// Arc::new(cu::cli::DefaultLogConfig) +/// ); +/// +/// cu::hint!("hello!"); +/// +/// cu::cli::reset_color(); +/// } +/// ``` /// /// If prompt option is `None`, it will be `Interactive` unless env var `CI` is `true` or `1`, in which case it becomes `No`. /// Prompt option is ignored unless `prompt` feature is enabled +/// +/// ## Difference to `#[cu::cli]` macro +/// - The main function is not wrapped, meaning any `cu::Result` you got you need to display it +/// yourself to the user. +/// - Need to call [`cu::cli::reset_color`] manually to reset color. +/// - RUST_BACKTRACE is not enabled automatically in trace level. To enable it you can +/// check `lv::T::enabled()` after calling `init_options` and use `unsafe { std::env::set_var(...) +/// }` +/// - `finished in XX.XXs` time is not printed +#[doc(alias = "init_cli")] pub fn init_options( color: lv::Color, level: lv::Print,