summaryrefslogtreecommitdiff
path: root/rola-cli/src/output/ansi_control
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-06-18 20:56:05 +0800
committer魏曹先生 <1992414357@qq.com>2026-06-18 20:56:05 +0800
commit68daa10abfe3015beca966825d32cf67c9f5d5d7 (patch)
treec28f0470240e7cdc3748cee57ef74353514c47b7 /rola-cli/src/output/ansi_control
parent669898193bebeadc975881bee496fe0239df76a0 (diff)
feat(bucket): implement bucket initialization and logging infrastructure
Add bucket init logic with directory structure creation and log macros for tracing
Diffstat (limited to 'rola-cli/src/output/ansi_control')
-rw-r--r--rola-cli/src/output/ansi_control/colorize_wrapper.rs209
1 files changed, 209 insertions, 0 deletions
diff --git a/rola-cli/src/output/ansi_control/colorize_wrapper.rs b/rola-cli/src/output/ansi_control/colorize_wrapper.rs
new file mode 100644
index 0000000..4c92f66
--- /dev/null
+++ b/rola-cli/src/output/ansi_control/colorize_wrapper.rs
@@ -0,0 +1,209 @@
+use colored::ColoredString;
+use colored::Colorize as _ColoredColorize;
+
+use super::is_ansi_enabled;
+
+macro_rules! if_ansi {
+ ($self:expr, $method:ident $(, $arg:expr)*) => {{
+ if is_ansi_enabled() {
+ _ColoredColorize::$method($self $(, $arg)*)
+ } else {
+ _ColoredColorize::clear($self)
+ }
+ }};
+}
+
+/// A drop-in wrapper for [`colored::Colorize`] that respects the global ANSI
+/// output flag controlled by [`enable_ansi()`](super::enable_ansi) /
+/// [`disable_ansi()`](super::disable_ansi).
+///
+/// When ANSI is **disabled**, all colorization methods return a plain
+/// [`ColoredString`] with no foreground/background color or style —
+/// effectively a no-op.
+///
+/// Import this trait instead of `colored::Colorize` to automatically honour
+/// the `--no-color` / `--quiet` flags set by the CLI.
+///
+/// # Example
+///
+/// ```ignore
+/// use rola_cli::res::ansi_control::Colorize;
+///
+/// println!("{}", "error".red().bold());
+/// // ^ ANSI on → red bold text
+/// // ^ ANSI off → plain "error"
+/// ```
+pub trait Colorize: _ColoredColorize {
+ fn black(self) -> ColoredString {
+ if_ansi!(self, black)
+ }
+ fn red(self) -> ColoredString {
+ if_ansi!(self, red)
+ }
+ fn green(self) -> ColoredString {
+ if_ansi!(self, green)
+ }
+ fn yellow(self) -> ColoredString {
+ if_ansi!(self, yellow)
+ }
+ fn blue(self) -> ColoredString {
+ if_ansi!(self, blue)
+ }
+ fn magenta(self) -> ColoredString {
+ if_ansi!(self, magenta)
+ }
+ fn purple(self) -> ColoredString {
+ if_ansi!(self, purple)
+ }
+ fn cyan(self) -> ColoredString {
+ if_ansi!(self, cyan)
+ }
+ fn white(self) -> ColoredString {
+ if_ansi!(self, white)
+ }
+
+ fn bright_black(self) -> ColoredString {
+ if_ansi!(self, bright_black)
+ }
+ fn bright_red(self) -> ColoredString {
+ if_ansi!(self, bright_red)
+ }
+ fn bright_green(self) -> ColoredString {
+ if_ansi!(self, bright_green)
+ }
+ fn bright_yellow(self) -> ColoredString {
+ if_ansi!(self, bright_yellow)
+ }
+ fn bright_blue(self) -> ColoredString {
+ if_ansi!(self, bright_blue)
+ }
+ fn bright_magenta(self) -> ColoredString {
+ if_ansi!(self, bright_magenta)
+ }
+ fn bright_purple(self) -> ColoredString {
+ if_ansi!(self, bright_purple)
+ }
+ fn bright_cyan(self) -> ColoredString {
+ if_ansi!(self, bright_cyan)
+ }
+ fn bright_white(self) -> ColoredString {
+ if_ansi!(self, bright_white)
+ }
+
+ fn truecolor(self, r: u8, g: u8, b: u8) -> ColoredString {
+ if_ansi!(self, truecolor, r, g, b)
+ }
+
+ fn color<C: Into<colored::Color>>(self, color: C) -> ColoredString {
+ if is_ansi_enabled() {
+ _ColoredColorize::color(self, color)
+ } else {
+ _ColoredColorize::clear(self)
+ }
+ }
+
+ fn on_black(self) -> ColoredString {
+ if_ansi!(self, on_black)
+ }
+ fn on_red(self) -> ColoredString {
+ if_ansi!(self, on_red)
+ }
+ fn on_green(self) -> ColoredString {
+ if_ansi!(self, on_green)
+ }
+ fn on_yellow(self) -> ColoredString {
+ if_ansi!(self, on_yellow)
+ }
+ fn on_blue(self) -> ColoredString {
+ if_ansi!(self, on_blue)
+ }
+ fn on_magenta(self) -> ColoredString {
+ if_ansi!(self, on_magenta)
+ }
+ fn on_purple(self) -> ColoredString {
+ if_ansi!(self, on_purple)
+ }
+ fn on_cyan(self) -> ColoredString {
+ if_ansi!(self, on_cyan)
+ }
+ fn on_white(self) -> ColoredString {
+ if_ansi!(self, on_white)
+ }
+
+ fn on_bright_black(self) -> ColoredString {
+ if_ansi!(self, on_bright_black)
+ }
+ fn on_bright_red(self) -> ColoredString {
+ if_ansi!(self, on_bright_red)
+ }
+ fn on_bright_green(self) -> ColoredString {
+ if_ansi!(self, on_bright_green)
+ }
+ fn on_bright_yellow(self) -> ColoredString {
+ if_ansi!(self, on_bright_yellow)
+ }
+ fn on_bright_blue(self) -> ColoredString {
+ if_ansi!(self, on_bright_blue)
+ }
+ fn on_bright_magenta(self) -> ColoredString {
+ if_ansi!(self, on_bright_magenta)
+ }
+ fn on_bright_purple(self) -> ColoredString {
+ if_ansi!(self, on_bright_purple)
+ }
+ fn on_bright_cyan(self) -> ColoredString {
+ if_ansi!(self, on_bright_cyan)
+ }
+ fn on_bright_white(self) -> ColoredString {
+ if_ansi!(self, on_bright_white)
+ }
+
+ fn on_truecolor(self, r: u8, g: u8, b: u8) -> ColoredString {
+ if_ansi!(self, on_truecolor, r, g, b)
+ }
+
+ fn on_color<C: Into<colored::Color>>(self, color: C) -> ColoredString {
+ if is_ansi_enabled() {
+ _ColoredColorize::on_color(self, color)
+ } else {
+ _ColoredColorize::clear(self)
+ }
+ }
+
+ fn clear(self) -> ColoredString {
+ if_ansi!(self, clear)
+ }
+ fn normal(self) -> ColoredString {
+ if_ansi!(self, normal)
+ }
+ fn bold(self) -> ColoredString {
+ if_ansi!(self, bold)
+ }
+ fn dimmed(self) -> ColoredString {
+ if_ansi!(self, dimmed)
+ }
+ fn italic(self) -> ColoredString {
+ if_ansi!(self, italic)
+ }
+ fn underline(self) -> ColoredString {
+ if_ansi!(self, underline)
+ }
+ fn blink(self) -> ColoredString {
+ if_ansi!(self, blink)
+ }
+ #[allow(deprecated)]
+ fn reverse(self) -> ColoredString {
+ if_ansi!(self, reverse)
+ }
+ fn reversed(self) -> ColoredString {
+ if_ansi!(self, reversed)
+ }
+ fn hidden(self) -> ColoredString {
+ if_ansi!(self, hidden)
+ }
+ fn strikethrough(self) -> ColoredString {
+ if_ansi!(self, strikethrough)
+ }
+}
+
+impl<T: _ColoredColorize> Colorize for T {}