inspect-format 0.1.0

Formatting and rendering for inspect-rs
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Themes mapping semantic roles to terminal styles.

use crate::{
    color_choice::ColorLevel,
    role::StyleRole,
    style::{AnsiColor, Style},
};

/// A collection of styles for each semantic role.
///
/// Themes map each [`StyleRole`] to a concrete [`Style`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Theme {
    styles: [Style; StyleRole::COUNT],
}

impl Default for Theme {
    fn default() -> Self {
        Self::dark()
    }
}

impl Theme {
    /// Canonical list of all built-in theme names.
    pub const NAMES: &'static [&'static str] = &[
        "dark",
        "light",
        "monochrome",
        "ansi16",
        "gruvbox",
        "nord",
        "dracula",
        "solarized-dark",
        "solarized-light",
        "one-dark",
        "catppuccin",
        "tokyo-night",
        "monokai",
        "ayu-dark",
    ];

    /// Look up a built-in theme by name (case-insensitive, kebab-case or snake_case).
    pub fn by_name(name: &str) -> Option<Self> {
        let normalized = name.trim().to_ascii_lowercase().replace('_', "-");
        match normalized.as_str() {
            "default" | "dark" => Some(Self::dark()),
            "light" => Some(Self::light()),
            "monochrome" | "mono" | "plain" => Some(Self::monochrome()),
            "ansi16" | "ansi" => Some(Self::ansi16()),
            "gruvbox" => Some(Self::gruvbox()),
            "nord" => Some(Self::nord()),
            "dracula" => Some(Self::dracula()),
            "solarized-dark" => Some(Self::solarized_dark()),
            "solarized-light" => Some(Self::solarized_light()),
            "one-dark" | "onedark" => Some(Self::one_dark()),
            "catppuccin" | "catppuccin-mocha" => Some(Self::catppuccin_mocha()),
            "tokyo-night" | "tokyonight" => Some(Self::tokyo_night()),
            "monokai" => Some(Self::monokai()),
            "ayu" | "ayu-dark" => Some(Self::ayu_dark()),
            _ => None,
        }
    }

    /// Create a theme with empty (plain) styles for every role.
    pub const fn empty() -> Self {
        Self { styles: [Style::new(); StyleRole::COUNT] }
    }

    /// The default dark theme.
    ///
    /// Calibrated for modern dark terminal backgrounds with restrained,
    /// warm developer syntax highlighting.
    pub fn dark() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(224, 138, 90))
            .with(StyleRole::Field, Style::new().fg_rgb(110, 180, 235))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(180, 145, 225))
            .with(StyleRole::Key, Style::new().fg_rgb(105, 205, 195))
            .with(StyleRole::String, Style::new().fg_rgb(142, 198, 128))
            .with(StyleRole::Number, Style::new().fg_rgb(235, 190, 95))
            .with(StyleRole::Boolean, Style::new().fg_rgb(195, 135, 185))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(140, 145, 155))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(115, 120, 130))
            .with(StyleRole::Index, Style::new().fg_rgb(115, 185, 195))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(130, 135, 145))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(235, 95, 95))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(225, 180, 75))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(240, 85, 85))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(120, 125, 135))
    }

    /// A light theme optimized for light / white background terminals.
    pub fn light() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(175, 75, 25))
            .with(StyleRole::Field, Style::new().fg_rgb(30, 95, 160))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(115, 45, 150))
            .with(StyleRole::Key, Style::new().fg_rgb(20, 115, 120))
            .with(StyleRole::String, Style::new().fg_rgb(35, 125, 55))
            .with(StyleRole::Number, Style::new().fg_rgb(165, 105, 15))
            .with(StyleRole::Boolean, Style::new().fg_rgb(140, 35, 105))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(110, 115, 125))
            .with(StyleRole::Punctuation, Style::new().fg_rgb(120, 125, 135))
            .with(StyleRole::Index, Style::new().fg_rgb(40, 105, 135))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(105, 110, 120))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(185, 30, 30))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(155, 95, 20))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(195, 20, 20))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(110, 115, 125))
    }

    /// A monochrome theme using clear terminal effects (bold, dim, italic, underline) without colors.
    pub fn monochrome() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold())
            .with(StyleRole::Field, Style::new())
            .with(StyleRole::Variant, Style::new().bold().underline())
            .with(StyleRole::Key, Style::new().underline())
            .with(StyleRole::String, Style::new())
            .with(StyleRole::Number, Style::new().bold())
            .with(StyleRole::Boolean, Style::new().bold())
            .with(StyleRole::Null, Style::new().dimmed().italic())
            .with(StyleRole::Punctuation, Style::new().dimmed())
            .with(StyleRole::Index, Style::new().dimmed())
            .with(StyleRole::Metadata, Style::new().dimmed())
            .with(StyleRole::Sensitive, Style::new().bold().underline())
            .with(StyleRole::Truncated, Style::new().dimmed().italic())
            .with(StyleRole::Error, Style::new().bold().underline())
            .with(StyleRole::Muted, Style::new().dimmed())
    }

    /// A theme using only standard 16 ANSI colors for maximum compatibility.
    pub fn ansi16() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_ansi(AnsiColor::Yellow))
            .with(StyleRole::Field, Style::new().fg_ansi(AnsiColor::Cyan))
            .with(StyleRole::Variant, Style::new().bold().fg_ansi(AnsiColor::Magenta))
            .with(StyleRole::Key, Style::new().fg_ansi(AnsiColor::Cyan))
            .with(StyleRole::String, Style::new().fg_ansi(AnsiColor::Green))
            .with(StyleRole::Number, Style::new().fg_ansi(AnsiColor::Yellow))
            .with(StyleRole::Boolean, Style::new().fg_ansi(AnsiColor::Magenta))
            .with(StyleRole::Null, Style::new().fg_ansi(AnsiColor::BrightBlack))
            .with(StyleRole::Punctuation, Style::new().fg_ansi(AnsiColor::BrightBlack))
            .with(StyleRole::Index, Style::new().fg_ansi(AnsiColor::Cyan))
            .with(StyleRole::Metadata, Style::new().fg_ansi(AnsiColor::BrightBlack))
            .with(StyleRole::Sensitive, Style::new().bold().fg_ansi(AnsiColor::BrightRed))
            .with(StyleRole::Truncated, Style::new().fg_ansi(AnsiColor::Yellow))
            .with(StyleRole::Error, Style::new().bold().fg_ansi(AnsiColor::Red))
            .with(StyleRole::Muted, Style::new().fg_ansi(AnsiColor::BrightBlack))
    }

    /// The Gruvbox theme (warm, earthy palette).
    pub fn gruvbox() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(254, 128, 25))
            .with(StyleRole::Field, Style::new().fg_rgb(131, 165, 152))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(211, 134, 155))
            .with(StyleRole::Key, Style::new().fg_rgb(142, 192, 124))
            .with(StyleRole::String, Style::new().fg_rgb(184, 187, 38))
            .with(StyleRole::Number, Style::new().fg_rgb(250, 189, 47))
            .with(StyleRole::Boolean, Style::new().fg_rgb(211, 134, 155))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(146, 131, 116))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(146, 131, 116))
            .with(StyleRole::Index, Style::new().fg_rgb(142, 192, 124))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(146, 131, 116))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(251, 73, 52))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(250, 189, 47))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(251, 73, 52))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(102, 92, 84))
    }

    /// The Nord theme (arctic, cool bluish-slate palette).
    pub fn nord() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(208, 135, 112))
            .with(StyleRole::Field, Style::new().fg_rgb(136, 192, 208))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(180, 142, 173))
            .with(StyleRole::Key, Style::new().fg_rgb(129, 161, 193))
            .with(StyleRole::String, Style::new().fg_rgb(163, 190, 140))
            .with(StyleRole::Number, Style::new().fg_rgb(235, 203, 139))
            .with(StyleRole::Boolean, Style::new().fg_rgb(180, 142, 173))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(147, 156, 172))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(97, 110, 136))
            .with(StyleRole::Index, Style::new().fg_rgb(136, 192, 208))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(108, 120, 144))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(191, 97, 106))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(235, 203, 139))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(191, 97, 106))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(76, 86, 106))
    }

    /// The Dracula theme (classic high-contrast dark theme).
    pub fn dracula() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(255, 184, 108))
            .with(StyleRole::Field, Style::new().fg_rgb(139, 233, 253))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(189, 147, 249))
            .with(StyleRole::Key, Style::new().fg_rgb(139, 233, 253))
            .with(StyleRole::String, Style::new().fg_rgb(80, 250, 123))
            .with(StyleRole::Number, Style::new().fg_rgb(241, 250, 140))
            .with(StyleRole::Boolean, Style::new().fg_rgb(255, 121, 198))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(98, 114, 164))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(98, 114, 164))
            .with(StyleRole::Index, Style::new().fg_rgb(139, 233, 253))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(98, 114, 164))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(255, 85, 85))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(241, 250, 140))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(255, 85, 85))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(98, 114, 164))
    }

    /// The Solarized Dark theme.
    pub fn solarized_dark() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(203, 75, 22))
            .with(StyleRole::Field, Style::new().fg_rgb(38, 139, 210))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(108, 113, 196))
            .with(StyleRole::Key, Style::new().fg_rgb(42, 161, 152))
            .with(StyleRole::String, Style::new().fg_rgb(133, 153, 0))
            .with(StyleRole::Number, Style::new().fg_rgb(181, 137, 0))
            .with(StyleRole::Boolean, Style::new().fg_rgb(211, 54, 130))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(101, 123, 131))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(88, 110, 117))
            .with(StyleRole::Index, Style::new().fg_rgb(42, 161, 152))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(88, 110, 117))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(220, 50, 47))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(181, 137, 0))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(220, 50, 47))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(88, 110, 117))
    }

    /// The Solarized Light theme.
    pub fn solarized_light() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(203, 75, 22))
            .with(StyleRole::Field, Style::new().fg_rgb(38, 139, 210))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(108, 113, 196))
            .with(StyleRole::Key, Style::new().fg_rgb(42, 161, 152))
            .with(StyleRole::String, Style::new().fg_rgb(133, 153, 0))
            .with(StyleRole::Number, Style::new().fg_rgb(181, 137, 0))
            .with(StyleRole::Boolean, Style::new().fg_rgb(211, 54, 130))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(147, 161, 161))
            .with(StyleRole::Punctuation, Style::new().fg_rgb(101, 123, 131))
            .with(StyleRole::Index, Style::new().fg_rgb(42, 161, 152))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(131, 148, 150))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(220, 50, 47))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(181, 137, 0))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(220, 50, 47))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(147, 161, 161))
    }

    /// The One Dark theme (iconic Atom/VS Code palette).
    pub fn one_dark() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(229, 192, 123))
            .with(StyleRole::Field, Style::new().fg_rgb(97, 175, 239))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(198, 120, 221))
            .with(StyleRole::Key, Style::new().fg_rgb(86, 182, 194))
            .with(StyleRole::String, Style::new().fg_rgb(152, 195, 121))
            .with(StyleRole::Number, Style::new().fg_rgb(209, 154, 102))
            .with(StyleRole::Boolean, Style::new().fg_rgb(198, 120, 221))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(120, 128, 142))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(92, 99, 112))
            .with(StyleRole::Index, Style::new().fg_rgb(86, 182, 194))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(92, 99, 112))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(224, 108, 117))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(229, 192, 123))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(224, 108, 117))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(92, 99, 112))
    }

    /// The Catppuccin Mocha theme (popular soft pastel comfort palette).
    pub fn catppuccin_mocha() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(250, 179, 135))
            .with(StyleRole::Field, Style::new().fg_rgb(137, 180, 250))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(203, 166, 247))
            .with(StyleRole::Key, Style::new().fg_rgb(148, 226, 213))
            .with(StyleRole::String, Style::new().fg_rgb(166, 227, 161))
            .with(StyleRole::Number, Style::new().fg_rgb(249, 226, 175))
            .with(StyleRole::Boolean, Style::new().fg_rgb(245, 194, 231))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(147, 153, 178))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(108, 112, 134))
            .with(StyleRole::Index, Style::new().fg_rgb(137, 220, 235))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(166, 173, 200))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(243, 139, 168))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(249, 226, 175))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(243, 139, 168))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(88, 91, 112))
    }

    /// Alias for [`Theme::catppuccin_mocha()`].
    pub fn catppuccin() -> Self {
        Self::catppuccin_mocha()
    }

    /// The Tokyo Night theme (neon city night aesthetic).
    pub fn tokyo_night() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(255, 158, 100))
            .with(StyleRole::Field, Style::new().fg_rgb(122, 162, 247))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(187, 154, 247))
            .with(StyleRole::Key, Style::new().fg_rgb(125, 207, 255))
            .with(StyleRole::String, Style::new().fg_rgb(158, 206, 106))
            .with(StyleRole::Number, Style::new().fg_rgb(224, 175, 104))
            .with(StyleRole::Boolean, Style::new().fg_rgb(187, 154, 247))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(120, 124, 153))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(86, 95, 137))
            .with(StyleRole::Index, Style::new().fg_rgb(125, 207, 255))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(86, 95, 137))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(247, 118, 142))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(224, 175, 104))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(247, 118, 142))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(86, 95, 137))
    }

    /// The Monokai theme (the classic Sublime Text / TextMate palette).
    pub fn monokai() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(253, 151, 31))
            .with(StyleRole::Field, Style::new().fg_rgb(102, 217, 239))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(174, 129, 255))
            .with(StyleRole::Key, Style::new().fg_rgb(102, 217, 239))
            .with(StyleRole::String, Style::new().fg_rgb(166, 226, 46))
            .with(StyleRole::Number, Style::new().fg_rgb(230, 219, 116))
            .with(StyleRole::Boolean, Style::new().fg_rgb(249, 38, 114))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(140, 138, 125))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(117, 113, 94))
            .with(StyleRole::Index, Style::new().fg_rgb(102, 217, 239))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(117, 113, 94))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(249, 38, 114))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(230, 219, 116))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(249, 38, 114))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(117, 113, 94))
    }

    /// The Ayu Dark theme.
    pub fn ayu_dark() -> Self {
        Self::empty()
            .with(StyleRole::Type, Style::new().bold().fg_rgb(255, 153, 64))
            .with(StyleRole::Field, Style::new().fg_rgb(54, 163, 217))
            .with(StyleRole::Variant, Style::new().bold().fg_rgb(212, 191, 255))
            .with(StyleRole::Key, Style::new().fg_rgb(149, 230, 203))
            .with(StyleRole::String, Style::new().fg_rgb(184, 204, 82))
            .with(StyleRole::Number, Style::new().fg_rgb(231, 197, 71))
            .with(StyleRole::Boolean, Style::new().fg_rgb(212, 191, 255))
            .with(StyleRole::Null, Style::new().dimmed().fg_rgb(120, 126, 135))
            .with(StyleRole::Punctuation, Style::new().dimmed().fg_rgb(92, 103, 115))
            .with(StyleRole::Index, Style::new().fg_rgb(95, 180, 210))
            .with(StyleRole::Metadata, Style::new().dimmed().fg_rgb(92, 103, 115))
            .with(StyleRole::Sensitive, Style::new().bold().fg_rgb(240, 113, 120))
            .with(StyleRole::Truncated, Style::new().italic().fg_rgb(231, 197, 71))
            .with(StyleRole::Error, Style::new().bold().fg_rgb(240, 113, 120))
            .with(StyleRole::Muted, Style::new().dimmed().fg_rgb(92, 103, 115))
    }

    /// Set the style for a specific semantic role.
    pub const fn with(mut self, role: StyleRole, style: Style) -> Self {
        self.styles[role.index()] = style;
        self
    }

    /// Get the style for a specific semantic role.
    #[inline]
    pub const fn style(&self, role: StyleRole) -> Style {
        self.styles[role.index()]
    }

    /// Adapt theme colors to match the target color capability level.
    pub fn adapt_to(self, level: ColorLevel) -> Self {
        match level {
            ColorLevel::Auto => {
                let detected = ColorLevel::detect();
                if detected == ColorLevel::Auto { self } else { self.adapt_to(detected) }
            }
            ColorLevel::Truecolor => self,
            ColorLevel::Ansi256 => self.to_ansi256(),
            ColorLevel::Ansi16 => self.to_ansi16(),
            ColorLevel::None => self.to_monochrome(),
        }
    }

    /// Convert all colors in the theme to 256-color palette.
    pub fn to_ansi256(self) -> Self {
        let mut new_theme = self;
        let mut i = 0;
        while i < StyleRole::COUNT {
            new_theme.styles[i] = new_theme.styles[i].to_ansi256();
            i += 1;
        }
        new_theme
    }

    /// Convert all colors in the theme to standard 16 ANSI colors.
    pub fn to_ansi16(self) -> Self {
        let mut new_theme = self;
        let mut i = 0;
        while i < StyleRole::COUNT {
            new_theme.styles[i] = new_theme.styles[i].to_ansi16();
            i += 1;
        }
        new_theme
    }

    /// Convert the theme to monochrome (preserve effects like bold/dim, strip colors).
    pub fn to_monochrome(self) -> Self {
        let mut new_theme = self;
        let mut i = 0;
        while i < StyleRole::COUNT {
            new_theme.styles[i] = new_theme.styles[i].to_monochrome();
            i += 1;
        }
        new_theme
    }

    /// True if all styles in the theme are plain/empty.
    pub fn is_plain(&self) -> bool {
        self.styles.iter().all(|s| s.is_plain())
    }
}