comfit-core 0.1.0

Comfit highlighting engine, theme model, and renderers
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! Capture styles, terminal colors, and optional TOML theme loading.

use std::{collections::BTreeMap, fmt::Write};

use rgb::RGB8;

/// Default styles, capture styles, bracket colors, and line emphasis.
#[derive(Debug, Clone, Default)]
pub struct Theme {
    /// Display name from the theme source.
    pub name: String,
    /// Base style inherited by tokens.
    pub default: Style,
    /// Capture styles keyed by dotted query capture names.
    pub styles: BTreeMap<String, Style>,
    /// Bracket colors cycled by nesting depth. An empty palette disables bracket coloring.
    pub rainbow: Vec<Color>,
    /// Style applied to selected lines by a renderer.
    pub line_highlight: Style,
}

/// Optional colors, font modifiers, and underline settings.
#[derive(Debug, Clone, Default)]
pub struct Style {
    /// Foreground color, if explicitly set.
    pub fg: Option<Color>,
    /// Background color, if explicitly set.
    pub bg: Option<Color>,
    /// Font and visibility modifiers. An empty list resets inherited modifiers.
    pub modifiers: Vec<Modifier>,
    /// Underline settings, if explicitly set.
    pub underline: Option<Underline>,
}

/// Truecolor, indexed terminal color, or the output default.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Color {
    /// A 24-bit RGB color.
    Rgb(RGB8),
    /// An index in the 256-color terminal palette.
    Indexed(u8),
    /// The terminal default, represented as `inherit` in CSS.
    Default,
}

/// Optional underline color and shape.
#[derive(Debug, Clone, Default)]
pub struct Underline {
    /// Underline color. Uses the output default if omitted.
    pub color: Option<Color>,
    /// Underline shape. Uses a straight line if omitted.
    pub style: Option<UnderlineStyle>,
}

/// Underline shapes supported by the renderers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnderlineStyle {
    /// A single straight line.
    Line,
    /// A wavy underline.
    Curl,
    /// A dashed underline.
    Dashed,
    /// A dotted underline.
    Dotted,
    /// Two straight lines.
    DoubleLine,
}

/// Font, visibility, and terminal display modifiers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Modifier {
    /// Bold text.
    Bold,
    /// Dimmed text.
    Dim,
    /// Italic text.
    Italic,
    /// Slow terminal blinking. CSS uses blink decoration.
    SlowBlink,
    /// Rapid terminal blinking. CSS uses blink decoration.
    RapidBlink,
    /// Swap foreground and background colors.
    Reversed,
    /// Hide text without removing its space.
    Hidden,
    /// Strike through text.
    CrossedOut,
}

impl Theme {
    /// Combine the default style with styles matching the capture name and its prefixes.
    ///
    /// More specific styles override colors and underlines when those values are set.
    /// Each matching style replaces the modifier list, including an empty list.
    /// Leading dots are ignored when building prefixes. Empty prefixes still match
    /// the empty style key. Only the final modifier list is cloned.
    ///
    /// # Examples
    ///
    /// ```
    /// use comfit_core::{Color, Style, Theme};
    ///
    /// let mut theme = Theme::default();
    /// theme.styles.insert("function".into(), Style {
    ///     fg: Some(Color::Indexed(4)),
    ///     ..Style::default()
    /// });
    ///
    /// // A more specific capture inherits matching prefix styles.
    /// assert_eq!(theme.style("function.call").fg, Some(Color::Indexed(4)));
    /// ```
    pub fn style(&self, capture: &str) -> Style {
        let mut result = Style {
            fg: self.default.fg,
            bg: self.default.bg,
            underline: self.default.underline.clone(),
            modifiers: Vec::new(),
        };
        let mut modifiers = &self.default.modifiers;
        let normalized = capture.trim_start_matches('.');
        let mut end = 0;

        for part in capture.split('.') {
            if end > 0 {
                end += 1;
            }
            end += part.len();

            if let Some(style) = self.styles.get(&normalized[..end]) {
                if style.fg.is_some() {
                    result.fg = style.fg;
                }

                if style.bg.is_some() {
                    result.bg = style.bg;
                }

                modifiers = &style.modifiers;

                if style.underline.is_some() {
                    result.underline.clone_from(&style.underline);
                }
            }
        }

        result.modifiers = modifiers.clone();
        result
    }

    /// Load default, capture, rainbow, and line-highlight styles from TOML.
    ///
    /// # Errors
    ///
    /// Returns [`crate::Error::Theme`] for invalid TOML or unsupported color, modifier,
    /// underline, or table values.
    #[cfg(feature = "theme-toml")]
    pub fn from_toml(source: &str) -> Result<Self, crate::Error> {
        let value: toml::Value =
            toml::from_str(source).map_err(|error| crate::Error::Theme(error.to_string()))?;
        let mut theme = Self {
            name: value
                .get("name")
                .and_then(toml::Value::as_str)
                .unwrap_or_default()
                .to_owned(),
            ..Self::default()
        };

        if let Some(default) = value.get("default") {
            theme.default = parse_style(default)?;
        }

        if let Some(styles) = value.get("styles") {
            let styles = styles
                .as_table()
                .ok_or_else(|| crate::Error::Theme("styles must be a table".into()))?;

            for (name, style) in styles {
                theme.styles.insert(name.clone(), parse_style(style)?);
            }
        }

        if let Some(colors) = value.get("rainbow") {
            let colors = colors
                .as_array()
                .ok_or_else(|| crate::Error::Theme("rainbow must be an array".into()))?;

            for color in colors {
                theme.rainbow.push(parse_color(color)?);
            }
        }

        if let Some(style) = value.get("line_highlight") {
            theme.line_highlight = parse_style(style)?;
        }

        Ok(theme)
    }
}

impl Color {
    /// Convert a color to CSS, using the standard terminal palette for indexed colors.
    pub fn to_css(self) -> String {
        let mut css = String::new();
        self.write_css(&mut css);
        css
    }

    fn write_css(self, css: &mut String) {
        let color = match self {
            Self::Default => {
                css.push_str("inherit");
                return;
            }
            Self::Rgb(color) => color,
            Self::Indexed(index) if index < 16 => {
                const COLORS: [(u8, u8, u8); 16] = [
                    (0, 0, 0),
                    (128, 0, 0),
                    (0, 128, 0),
                    (128, 128, 0),
                    (0, 0, 128),
                    (128, 0, 128),
                    (0, 128, 128),
                    (192, 192, 192),
                    (128, 128, 128),
                    (255, 0, 0),
                    (0, 255, 0),
                    (255, 255, 0),
                    (0, 0, 255),
                    (255, 0, 255),
                    (0, 255, 255),
                    (255, 255, 255),
                ];
                COLORS[index as usize].into()
            }

            Self::Indexed(index) if index >= 232 => {
                let shade = 8 + (index - 232) * 10;
                RGB8::new(shade, shade, shade)
            }

            Self::Indexed(index) => {
                let index = index - 16;
                let level = |value: u8| if value == 0 { 0 } else { 55 + value * 40 };
                RGB8::new(level(index / 36), level(index / 6 % 6), level(index % 6))
            }
        };

        write!(css, "#{:02x}{:02x}{:02x}", color.r, color.g, color.b).unwrap();
    }
}

impl Style {
    /// Render CSS declarations for explicitly set style properties.
    ///
    /// Reversed colors are swapped before rendering. Terminal blink speeds both map
    /// to CSS blink decoration. Browser support determines the visible result.
    pub fn to_css(&self) -> String {
        let mut css = String::new();
        self.write_css(&mut css);
        css
    }

    /// Append declarations for explicitly set properties without clearing the buffer.
    pub(crate) fn write_css(&self, css: &mut String) {
        let (foreground, background) = if self.modifiers.contains(&Modifier::Reversed) {
            (self.bg, self.fg)
        } else {
            (self.fg, self.bg)
        };

        if let Some(color) = foreground {
            css.push_str("color:");
            color.write_css(css);
            css.push(';');
        }

        if let Some(color) = background {
            css.push_str("background-color:");
            color.write_css(css);
            css.push(';');
        }

        for modifier in &self.modifiers {
            css.push_str(match modifier {
                Modifier::Bold => "font-weight:bold;",
                Modifier::Dim => "opacity:0.5;",
                Modifier::Italic => "font-style:italic;",
                Modifier::Hidden => "visibility:hidden;",
                Modifier::CrossedOut => "text-decoration-line:line-through;",
                Modifier::SlowBlink | Modifier::RapidBlink => "text-decoration-line:blink;",
                Modifier::Reversed => "",
            });
        }

        if let Some(underline) = &self.underline {
            let crossed = self.modifiers.contains(&Modifier::CrossedOut);
            css.push_str(if crossed {
                "text-decoration-line:underline line-through;"
            } else {
                "text-decoration-line:underline;"
            });
            let style = match underline.style.unwrap_or(UnderlineStyle::Line) {
                UnderlineStyle::Line => "solid",
                UnderlineStyle::Curl => "wavy",
                UnderlineStyle::Dashed => "dashed",
                UnderlineStyle::Dotted => "dotted",
                UnderlineStyle::DoubleLine => "double",
            };

            css.push_str("text-decoration-style:");
            css.push_str(style);
            css.push(';');

            if let Some(color) = underline.color {
                css.push_str("text-decoration-color:");
                color.write_css(css);
                css.push(';');
            }
        }
    }
}

impl std::str::FromStr for Color {
    type Err = crate::Error;

    /// Parse a color from `#RGB`, `#RRGGBB`, or `default`.
    ///
    /// # Errors
    ///
    /// Returns [`crate::Error::Theme`] for unsupported formats or invalid hex digits.
    fn from_str(color: &str) -> Result<Self, Self::Err> {
        match color {
            "default" => Ok(Color::Default),
            color
                if color.len() == 4
                    && color.starts_with('#')
                    && color[1..].bytes().all(|byte| byte.is_ascii_hexdigit()) =>
            {
                let value = u16::from_str_radix(&color[1..], 16).unwrap();

                Ok(Color::Rgb(RGB8::new(
                    ((value >> 8) as u8) * 17,
                    ((value >> 4 & 15) as u8) * 17,
                    ((value & 15) as u8) * 17,
                )))
            }
            color
                if color.len() == 7
                    && color.starts_with('#')
                    && color[1..].bytes().all(|byte| byte.is_ascii_hexdigit()) =>
            {
                let value = u32::from_str_radix(&color[1..], 16).unwrap();

                Ok(Color::Rgb(RGB8::new(
                    (value >> 16) as u8,
                    (value >> 8) as u8,
                    value as u8,
                )))
            }
            _ => Err(crate::Error::Theme(format!("invalid color: {color:?}"))),
        }
    }
}

#[cfg(feature = "theme-toml")]
fn parse_color(value: &toml::Value) -> Result<Color, crate::Error> {
    if let Some(index) = value
        .as_integer()
        .and_then(|index| u8::try_from(index).ok())
    {
        return Ok(Color::Indexed(index));
    }

    value
        .as_str()
        .and_then(|color| color.parse().ok())
        .ok_or_else(|| crate::Error::Theme(format!("invalid color: {value}")))
}

#[cfg(feature = "theme-toml")]
fn parse_style(value: &toml::Value) -> Result<Style, crate::Error> {
    let value = value
        .as_table()
        .ok_or_else(|| crate::Error::Theme("style must be a table".into()))?;

    let mut style = Style::default();

    if let Some(color) = value.get("fg") {
        style.fg = Some(parse_color(color)?);
    }

    if let Some(color) = value.get("bg") {
        style.bg = Some(parse_color(color)?);
    }

    if let Some(modifiers) = value.get("modifiers") {
        let modifiers = modifiers
            .as_array()
            .ok_or_else(|| crate::Error::Theme("modifiers must be an array".into()))?;

        for modifier in modifiers {
            style.modifiers.push(match modifier.as_str() {
                Some("bold") => Modifier::Bold,
                Some("dim") => Modifier::Dim,
                Some("italic") => Modifier::Italic,
                Some("slow_blink") => Modifier::SlowBlink,
                Some("rapid_blink") => Modifier::RapidBlink,
                Some("reversed") => Modifier::Reversed,
                Some("hidden") => Modifier::Hidden,
                Some("crossed_out") => Modifier::CrossedOut,
                _ => return Err(crate::Error::Theme(format!("invalid modifier: {modifier}"))),
            });
        }
    }

    if let Some(value) = value.get("underline") {
        let value = value
            .as_table()
            .ok_or_else(|| crate::Error::Theme("underline must be a table".into()))?;
        let color = value.get("color").map(parse_color).transpose()?;
        let underline = match value
            .get("style")
            .and_then(toml::Value::as_str)
            .unwrap_or("line")
        {
            "line" => UnderlineStyle::Line,
            "curl" => UnderlineStyle::Curl,
            "dashed" => UnderlineStyle::Dashed,
            "dotted" => UnderlineStyle::Dotted,
            "double_line" => UnderlineStyle::DoubleLine,
            other => {
                return Err(crate::Error::Theme(format!(
                    "invalid underline style: {other}"
                )));
            }
        };
        style.underline = Some(Underline {
            color,
            style: Some(underline),
        });
    }

    Ok(style)
}