-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththeme.rs
More file actions
233 lines (212 loc) · 6.88 KB
/
theme.rs
File metadata and controls
233 lines (212 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! Theme configuration for user output
//!
//! This module provides theme support for user-facing messages, allowing customization
//! of visual symbols used throughout the output.
/// Output theme controlling symbols and formatting
///
/// A theme defines the visual appearance of user-facing messages through
/// configurable symbols. Themes enable consistent styling across all output
/// and support different environments (terminals, CI/CD, accessibility needs).
///
/// # Predefined Themes
///
/// - **Emoji** (default): Unicode emoji symbols for interactive terminals
/// - **Plain**: Text labels like `[INFO]`, `[OK]` for CI/CD environments
/// - **ASCII**: Basic ASCII characters for limited terminal support
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::cli::views::Theme;
///
/// // Use emoji theme (default)
/// let theme = Theme::emoji();
/// assert_eq!(theme.progress_symbol(), "⏳");
///
/// // Use plain text theme for CI/CD
/// let theme = Theme::plain();
/// assert_eq!(theme.success_symbol(), "[OK]");
///
/// // Use ASCII theme for limited terminals
/// let theme = Theme::ascii();
/// assert_eq!(theme.error_symbol(), "[x]");
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(clippy::struct_field_names)]
pub struct Theme {
progress_symbol: String,
success_symbol: String,
warning_symbol: String,
error_symbol: String,
detail_symbol: String,
debug_symbol: String,
}
impl Theme {
/// Create emoji theme with Unicode symbols (default)
///
/// Best for interactive terminals with good Unicode support.
/// Uses emoji characters that are visually distinctive and widely supported.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::cli::views::Theme;
///
/// let theme = Theme::emoji();
/// assert_eq!(theme.progress_symbol(), "⏳");
/// assert_eq!(theme.success_symbol(), "✅");
/// assert_eq!(theme.warning_symbol(), "⚠️");
/// assert_eq!(theme.error_symbol(), "❌");
/// ```
#[must_use]
pub fn emoji() -> Self {
Self {
progress_symbol: "⏳".to_string(),
success_symbol: "✅".to_string(),
warning_symbol: "⚠️".to_string(),
error_symbol: "❌".to_string(),
detail_symbol: "📋".to_string(),
debug_symbol: "🔍".to_string(),
}
}
/// Create plain text theme for CI/CD environments
///
/// Uses text labels like `[INFO]`, `[OK]`, `[WARN]`, `[ERROR]` that work
/// in any environment without Unicode support. Ideal for CI/CD pipelines
/// and log aggregation systems.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::cli::views::Theme;
///
/// let theme = Theme::plain();
/// assert_eq!(theme.progress_symbol(), "[INFO]");
/// assert_eq!(theme.success_symbol(), "[OK]");
/// assert_eq!(theme.warning_symbol(), "[WARN]");
/// assert_eq!(theme.error_symbol(), "[ERROR]");
/// ```
#[must_use]
pub fn plain() -> Self {
Self {
progress_symbol: "[INFO]".to_string(),
success_symbol: "[OK]".to_string(),
warning_symbol: "[WARN]".to_string(),
error_symbol: "[ERROR]".to_string(),
detail_symbol: "[DETAIL]".to_string(),
debug_symbol: "[DEBUG]".to_string(),
}
}
/// Create ASCII-only theme using basic characters
///
/// Uses simple ASCII characters that work on any terminal.
/// Good for environments with limited character set support or
/// when maximum compatibility is required.
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::cli::views::Theme;
///
/// let theme = Theme::ascii();
/// assert_eq!(theme.progress_symbol(), "=>");
/// assert_eq!(theme.success_symbol(), "[+]");
/// assert_eq!(theme.warning_symbol(), "[!]");
/// assert_eq!(theme.error_symbol(), "[x]");
/// ```
#[must_use]
pub fn ascii() -> Self {
Self {
progress_symbol: "=>".to_string(),
success_symbol: "[+]".to_string(),
warning_symbol: "[!]".to_string(),
error_symbol: "[x]".to_string(),
detail_symbol: "[~]".to_string(),
debug_symbol: "[?]".to_string(),
}
}
/// Get the progress symbol for this theme
#[must_use]
pub fn progress_symbol(&self) -> &str {
&self.progress_symbol
}
/// Get the success symbol for this theme
#[must_use]
pub fn success_symbol(&self) -> &str {
&self.success_symbol
}
/// Get the warning symbol for this theme
#[must_use]
pub fn warning_symbol(&self) -> &str {
&self.warning_symbol
}
/// Get the error symbol for this theme
#[must_use]
pub fn error_symbol(&self) -> &str {
&self.error_symbol
}
/// Get the detail symbol for this theme (verbose progress)
#[must_use]
pub fn detail_symbol(&self) -> &str {
&self.detail_symbol
}
/// Get the debug symbol for this theme (debug-level details)
#[must_use]
pub fn debug_symbol(&self) -> &str {
&self.debug_symbol
}
}
impl Default for Theme {
/// Create the default theme (emoji)
///
/// # Examples
///
/// ```rust
/// use torrust_tracker_deployer_lib::presentation::cli::views::Theme;
///
/// let theme = Theme::default();
/// assert_eq!(theme.progress_symbol(), "⏳");
/// ```
fn default() -> Self {
Self::emoji()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_return_emoji_symbols_when_using_emoji_theme() {
let theme = Theme::emoji();
assert_eq!(theme.progress_symbol(), "⏳");
assert_eq!(theme.success_symbol(), "✅");
assert_eq!(theme.warning_symbol(), "⚠️");
assert_eq!(theme.error_symbol(), "❌");
}
#[test]
fn it_should_return_text_labels_when_using_plain_theme() {
let theme = Theme::plain();
assert_eq!(theme.progress_symbol(), "[INFO]");
assert_eq!(theme.success_symbol(), "[OK]");
assert_eq!(theme.warning_symbol(), "[WARN]");
assert_eq!(theme.error_symbol(), "[ERROR]");
}
#[test]
fn it_should_return_ascii_symbols_when_using_ascii_theme() {
let theme = Theme::ascii();
assert_eq!(theme.progress_symbol(), "=>");
assert_eq!(theme.success_symbol(), "[+]");
assert_eq!(theme.warning_symbol(), "[!]");
assert_eq!(theme.error_symbol(), "[x]");
}
#[test]
fn it_should_default_to_emoji_theme_when_using_default() {
let theme = Theme::default();
assert_eq!(theme, Theme::emoji());
}
#[test]
fn themes_should_be_cloneable() {
let theme1 = Theme::emoji();
let theme2 = theme1.clone();
assert_eq!(theme1, theme2);
}
}