use crate::style::Color;
use std::sync::RwLock;
#[derive(Debug, Clone)]
pub struct Theme {
pub success_glyph: String,
pub warn_label: String,
pub error_label: String,
pub debug_label: String,
pub dry_run_prefix: String,
pub color_success: Color,
pub color_warn: Color,
pub color_error: Color,
pub color_debug: Color,
pub color_heading: Color,
pub color_accent: Color,
pub confirm_flag: String,
pub confirm_refusal: String,
pub progress_fill: String,
pub progress_empty: String,
pub progress_width: usize,
}
impl Default for Theme {
fn default() -> Self {
Theme {
success_glyph: String::new(), 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(), 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 }
pub fn confirm_refusal_message(&self) -> String {
self.confirm_refusal.replace("{flag}", &self.confirm_flag)
}
}
static ACTIVE: RwLock<Option<Theme>> = RwLock::new(None);
pub fn set_active(theme: Theme) {
*ACTIVE.write().unwrap() = Some(theme);
}
pub fn active() -> Theme {
ACTIVE
.read()
.unwrap()
.clone()
.unwrap_or_default()
}