1use crate::style::Color;
19use std::sync::RwLock;
20
21#[derive(Debug, Clone)]
24pub struct Theme {
25 pub success_glyph: String,
27 pub warn_label: String,
29 pub error_label: String,
31 pub debug_label: String,
33 pub dry_run_prefix: String,
35 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 pub confirm_flag: String,
45 pub confirm_refusal: String,
48 pub progress_fill: String,
50 pub progress_empty: String,
51 pub progress_width: usize,
53}
54
55impl Default for Theme {
56 fn default() -> Self {
57 Theme {
58 success_glyph: String::new(), 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(), 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 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
106pub fn set_active(theme: Theme) {
108 *ACTIVE.write().unwrap() = Some(theme);
109}
110
111pub fn active() -> Theme {
114 ACTIVE
115 .read()
116 .unwrap()
117 .clone()
118 .unwrap_or_default()
119}