Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/copper/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
5 changes: 4 additions & 1 deletion packages/copper/src/cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions packages/copper/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
34 changes: 33 additions & 1 deletion packages/copper/src/cli/print_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down