Skip to main content

cornerstone/
theme.rs

1//! Theme: every human-visible string, glyph, and color the library emits lives
2//! here, so nothing is hardcoded in the machinery. The default theme is neutral
3//! — no personal voice — and any field can be overridden wholesale or piecemeal.
4//!
5//! ```ignore
6//! use cornerstone::prelude::*;
7//!
8//! let theme = Theme::default()
9//!     .success_glyph("»")
10//!     .error_label("fatal:");
11//! App::new("tool").theme(theme) /* ... */ ;
12//! ```
13//!
14//! The active theme is process-global (set once at startup by the app) so that
15//! free functions in [`crate::out`] can reach it without threading it through
16//! every call.
17
18use crate::style::Color;
19use std::sync::RwLock;
20
21/// All customizable presentation. Construct with [`Theme::default`] and override
22/// fields with the builder methods.
23#[derive(Debug, Clone)]
24pub struct Theme {
25    /// Glyph placed before a success line (default: none — neutral).
26    pub success_glyph: String,
27    /// Label before a warning (default: `"warning:"`).
28    pub warn_label: String,
29    /// Label before an error (default: `"error:"`).
30    pub error_label: String,
31    /// Label before a debug line (default: `"debug:"`).
32    pub debug_label: String,
33    /// Prefix shown before dry-run actions (default: `"[dry-run]"`).
34    pub dry_run_prefix: String,
35    /// Underlying color roles. Each maps a semantic role to a [`Color`].
36    pub color_success: Color,
37    pub color_warn: Color,
38    pub color_error: Color,
39    pub color_debug: Color,
40    pub color_heading: Color,
41    pub color_accent: Color,
42    /// The confirmation flag name for destructive operations. Kept overridable
43    /// so a tool can word its own confirmation, but defaults to the standard.
44    pub confirm_flag: String,
45    /// Message shown when a destructive op is refused for lack of confirmation.
46    /// `{flag}` is replaced with the confirmation flag name.
47    pub confirm_refusal: String,
48    /// Progress bar fill and empty cells.
49    pub progress_fill: String,
50    pub progress_empty: String,
51    /// Progress bar width in cells.
52    pub progress_width: usize,
53}
54
55impl Default for Theme {
56    fn default() -> Self {
57        Theme {
58            success_glyph: String::new(),      // neutral: no decorative check by default
59            warn_label: "warning:".into(),
60            error_label: "error:".into(),
61            debug_label: "debug:".into(),
62            dry_run_prefix: "[dry-run]".into(),
63            color_success: Color::Green,
64            color_warn: Color::Yellow,
65            color_error: Color::Red,
66            color_debug: Color::Gray,
67            color_heading: Color::Cyan,
68            color_accent: Color::Cyan,
69            confirm_flag: crate::guard::DEFAULT_CONFIRM_FLAG.into(),
70            confirm_refusal: "refusing to proceed without confirmation; pass --{flag}".into(),
71            progress_fill: "#".into(),         // neutral ASCII by default
72            progress_empty: "-".into(),
73            progress_width: 30,
74        }
75    }
76}
77
78impl Theme {
79    pub fn success_glyph<S: Into<String>>(mut self, s: S) -> Self { self.success_glyph = s.into(); self }
80    pub fn warn_label<S: Into<String>>(mut self, s: S) -> Self { self.warn_label = s.into(); self }
81    pub fn error_label<S: Into<String>>(mut self, s: S) -> Self { self.error_label = s.into(); self }
82    pub fn debug_label<S: Into<String>>(mut self, s: S) -> Self { self.debug_label = s.into(); self }
83    pub fn dry_run_prefix<S: Into<String>>(mut self, s: S) -> Self { self.dry_run_prefix = s.into(); self }
84    pub fn confirm_flag<S: Into<String>>(mut self, s: S) -> Self { self.confirm_flag = s.into(); self }
85    pub fn confirm_refusal<S: Into<String>>(mut self, s: S) -> Self { self.confirm_refusal = s.into(); self }
86    pub fn color_success(mut self, c: Color) -> Self { self.color_success = c; self }
87    pub fn color_warn(mut self, c: Color) -> Self { self.color_warn = c; self }
88    pub fn color_error(mut self, c: Color) -> Self { self.color_error = c; self }
89    pub fn color_heading(mut self, c: Color) -> Self { self.color_heading = c; self }
90    pub fn color_accent(mut self, c: Color) -> Self { self.color_accent = c; self }
91    pub fn progress_cells<S: Into<String>>(mut self, fill: S, empty: S) -> Self {
92        self.progress_fill = fill.into();
93        self.progress_empty = empty.into();
94        self
95    }
96    pub fn progress_width(mut self, w: usize) -> Self { self.progress_width = w; self }
97
98    /// Render the refusal message with the flag name substituted.
99    pub fn confirm_refusal_message(&self) -> String {
100        self.confirm_refusal.replace("{flag}", &self.confirm_flag)
101    }
102}
103
104static ACTIVE: RwLock<Option<Theme>> = RwLock::new(None);
105
106/// Install the active theme (called once by the app at startup).
107pub fn set_active(theme: Theme) {
108    *ACTIVE.write().unwrap() = Some(theme);
109}
110
111/// Access the active theme, falling back to the default if none is installed.
112/// Returns an owned clone so callers don't hold the lock.
113pub fn active() -> Theme {
114    ACTIVE
115        .read()
116        .unwrap()
117        .clone()
118        .unwrap_or_default()
119}