cornerlibstone 0.1.0

Lib Cornerstone — a zero-dependency foundation for building command-line tools in Rust: argument parsing, nested subcommands, themeable help in multiple presets, structured errors and exit codes, config loading, interactive prompts, shell-completion and man/markdown generation, logging, JSON output, and root/destructive-operation guards.
Documentation
//! Theme: every human-visible string, glyph, and color the library emits lives
//! here, so nothing is hardcoded in the machinery. The default theme is neutral
//! — no personal voice — and any field can be overridden wholesale or piecemeal.
//!
//! ```ignore
//! use cornerstone::prelude::*;
//!
//! let theme = Theme::default()
//!     .success_glyph("»")
//!     .error_label("fatal:");
//! App::new("tool").theme(theme) /* ... */ ;
//! ```
//!
//! The active theme is process-global (set once at startup by the app) so that
//! free functions in [`crate::out`] can reach it without threading it through
//! every call.

use crate::style::Color;
use std::sync::RwLock;

/// All customizable presentation. Construct with [`Theme::default`] and override
/// fields with the builder methods.
#[derive(Debug, Clone)]
pub struct Theme {
    /// Glyph placed before a success line (default: none — neutral).
    pub success_glyph: String,
    /// Label before a warning (default: `"warning:"`).
    pub warn_label: String,
    /// Label before an error (default: `"error:"`).
    pub error_label: String,
    /// Label before a debug line (default: `"debug:"`).
    pub debug_label: String,
    /// Prefix shown before dry-run actions (default: `"[dry-run]"`).
    pub dry_run_prefix: String,
    /// Underlying color roles. Each maps a semantic role to a [`Color`].
    pub color_success: Color,
    pub color_warn: Color,
    pub color_error: Color,
    pub color_debug: Color,
    pub color_heading: Color,
    pub color_accent: Color,
    /// The confirmation flag name for destructive operations. Kept overridable
    /// so a tool can word its own confirmation, but defaults to the standard.
    pub confirm_flag: String,
    /// Message shown when a destructive op is refused for lack of confirmation.
    /// `{flag}` is replaced with the confirmation flag name.
    pub confirm_refusal: String,
    /// Progress bar fill and empty cells.
    pub progress_fill: String,
    pub progress_empty: String,
    /// Progress bar width in cells.
    pub progress_width: usize,
}

impl Default for Theme {
    fn default() -> Self {
        Theme {
            success_glyph: String::new(),      // neutral: no decorative check by default
            warn_label: "warning:".into(),
            error_label: "error:".into(),
            debug_label: "debug:".into(),
            dry_run_prefix: "[dry-run]".into(),
            color_success: Color::Green,
            color_warn: Color::Yellow,
            color_error: Color::Red,
            color_debug: Color::Gray,
            color_heading: Color::Cyan,
            color_accent: Color::Cyan,
            confirm_flag: crate::guard::DEFAULT_CONFIRM_FLAG.into(),
            confirm_refusal: "refusing to proceed without confirmation; pass --{flag}".into(),
            progress_fill: "#".into(),         // neutral ASCII by default
            progress_empty: "-".into(),
            progress_width: 30,
        }
    }
}

impl Theme {
    pub fn success_glyph<S: Into<String>>(mut self, s: S) -> Self { self.success_glyph = s.into(); self }
    pub fn warn_label<S: Into<String>>(mut self, s: S) -> Self { self.warn_label = s.into(); self }
    pub fn error_label<S: Into<String>>(mut self, s: S) -> Self { self.error_label = s.into(); self }
    pub fn debug_label<S: Into<String>>(mut self, s: S) -> Self { self.debug_label = s.into(); self }
    pub fn dry_run_prefix<S: Into<String>>(mut self, s: S) -> Self { self.dry_run_prefix = s.into(); self }
    pub fn confirm_flag<S: Into<String>>(mut self, s: S) -> Self { self.confirm_flag = s.into(); self }
    pub fn confirm_refusal<S: Into<String>>(mut self, s: S) -> Self { self.confirm_refusal = s.into(); self }
    pub fn color_success(mut self, c: Color) -> Self { self.color_success = c; self }
    pub fn color_warn(mut self, c: Color) -> Self { self.color_warn = c; self }
    pub fn color_error(mut self, c: Color) -> Self { self.color_error = c; self }
    pub fn color_heading(mut self, c: Color) -> Self { self.color_heading = c; self }
    pub fn color_accent(mut self, c: Color) -> Self { self.color_accent = c; self }
    pub fn progress_cells<S: Into<String>>(mut self, fill: S, empty: S) -> Self {
        self.progress_fill = fill.into();
        self.progress_empty = empty.into();
        self
    }
    pub fn progress_width(mut self, w: usize) -> Self { self.progress_width = w; self }

    /// Render the refusal message with the flag name substituted.
    pub fn confirm_refusal_message(&self) -> String {
        self.confirm_refusal.replace("{flag}", &self.confirm_flag)
    }
}

static ACTIVE: RwLock<Option<Theme>> = RwLock::new(None);

/// Install the active theme (called once by the app at startup).
pub fn set_active(theme: Theme) {
    *ACTIVE.write().unwrap() = Some(theme);
}

/// Access the active theme, falling back to the default if none is installed.
/// Returns an owned clone so callers don't hold the lock.
pub fn active() -> Theme {
    ACTIVE
        .read()
        .unwrap()
        .clone()
        .unwrap_or_default()
}