zeph-tui 0.22.0

Ratatui-based TUI dashboard with real-time metrics for Zeph
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::collections::HashMap;
use std::sync::LazyLock;

use ratatui::style::{Color, Modifier, Style};
use ratatui::text::Span;
use tree_sitter::Language;
use tree_sitter_highlight::{HighlightConfiguration, HighlightEvent, Highlighter};

use crate::theme::SyntaxTheme;

const CAPTURE_NAMES: &[&str] = &[
    "attribute",
    "boolean",
    "comment",
    "constant",
    "constant.builtin",
    "constructor",
    "escape",
    "function",
    "function.builtin",
    "function.call",
    "function.method",
    "keyword",
    "keyword.operator",
    "label",
    "number",
    "operator",
    "property",
    "punctuation",
    "punctuation.bracket",
    "punctuation.delimiter",
    "punctuation.special",
    "storageclass",
    "string",
    "string.escape",
    "string.special",
    "tag",
    "text.literal",
    "text.reference",
    "text.title",
    "text.uri",
    "type",
    "type.builtin",
    "type.qualifier",
    "variable",
    "variable.builtin",
    "variable.parameter",
];

// Custom bash query because tree-sitter-bash doesn't bundle a highlights.scm
// compatible with tree-sitter-highlight's capture convention.
const BASH_HIGHLIGHTS_QUERY: &str = r#"
[(string) (raw_string) (heredoc_body) (heredoc_start)] @string
(command_name) @function
(variable_name) @property
["case" "do" "done" "elif" "else" "esac" "export" "fi" "for" "function" "if" "in" "select" "then" "unset" "until" "while"] @keyword
(comment) @comment
(function_definition name: (word) @function)
(file_descriptor) @number
["$" "&&" ">" ">>" "<" "|"] @operator
((command (_) @constant) (#match? @constant "^-"))
"#;

static LANG_ALIASES: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
    HashMap::from([
        ("rs", "rust"),
        ("py", "python"),
        ("js", "javascript"),
        ("sh", "bash"),
        ("shell", "bash"),
        ("ts", "typescript"),
        ("tsx", "typescript"),
        ("mts", "typescript"),
        ("cts", "typescript"),
        ("golang", "go"),
        ("yml", "yaml"),
        ("md", "markdown"),
        ("mysql", "sql"),
        ("psql", "sql"),
        ("postgres", "sql"),
        ("sequel", "sql"),
    ])
});

/// Process-wide singleton [`SyntaxHighlighter`], initialised on first access.
///
/// Use this instead of constructing a new highlighter to avoid redundant
/// tree-sitter grammar compilation on each frame.
///
/// # Examples
///
/// ```rust
/// use zeph_tui::highlight::SYNTAX_HIGHLIGHTER;
/// use zeph_tui::theme::SyntaxTheme;
///
/// let theme = SyntaxTheme::default();
/// let spans = SYNTAX_HIGHLIGHTER.highlight("rust", "let x = 1;", &theme);
/// assert!(spans.is_some());
/// ```
pub static SYNTAX_HIGHLIGHTER: LazyLock<SyntaxHighlighter> = LazyLock::new(SyntaxHighlighter::new);

/// Tree-sitter-based syntax highlighter for TUI code blocks.
///
/// Supports Rust, Python, JavaScript, TypeScript, Go, JSON, TOML, YAML, SQL,
/// Markdown (block-level), and Bash out of the box. Language aliases
/// (`"rs"` → `"rust"`, `"ts"` → `"typescript"`, `"yml"` → `"yaml"`, etc.)
/// are resolved transparently.
///
/// Construct via the [`SYNTAX_HIGHLIGHTER`] static for process-level sharing,
/// or call the private `new` method directly in tests.
///
/// # Supported languages
///
/// | Identifier   | Aliases                          |
/// |--------------|----------------------------------|
/// | `rust`       | `rs`                             |
/// | `python`     | `py`                             |
/// | `javascript` | `js`                             |
/// | `typescript` | `ts`, `tsx`, `mts`, `cts`        |
/// | `go`         | `golang`                         |
/// | `bash`       | `sh`, `shell`                    |
/// | `json`       | —                                |
/// | `toml`       | —                                |
/// | `yaml`       | `yml`                            |
/// | `sql`        | `mysql`, `psql`, `postgres`, `sequel` |
/// | `markdown`   | `md`                             |
///
/// # Examples
///
/// ```rust
/// use zeph_tui::highlight::SYNTAX_HIGHLIGHTER;
/// use zeph_tui::theme::SyntaxTheme;
///
/// let theme = SyntaxTheme::default();
///
/// // Known language → styled spans
/// let spans = SYNTAX_HIGHLIGHTER.highlight("rust", "fn main() {}", &theme);
/// assert!(spans.is_some());
///
/// // Alias works the same way
/// let spans = SYNTAX_HIGHLIGHTER.highlight("rs", "let x = 1;", &theme);
/// assert!(spans.is_some());
///
/// // Unknown language → None
/// assert!(SYNTAX_HIGHLIGHTER.highlight("brainfuck", "+++", &theme).is_none());
/// ```
pub struct SyntaxHighlighter {
    configs: HashMap<&'static str, HighlightConfiguration>,
}

impl SyntaxHighlighter {
    #[allow(clippy::too_many_lines)]
    fn new() -> Self {
        let mut configs = HashMap::new();

        let mut register = |name: &'static str,
                            language: Language,
                            lang_name: &str,
                            highlights_query: &str,
                            injections_query: &str| {
            let Ok(mut config) = HighlightConfiguration::new(
                language,
                lang_name.to_string(),
                highlights_query,
                injections_query,
                "",
            ) else {
                return;
            };
            config.configure(CAPTURE_NAMES);
            configs.insert(name, config);
        };

        register(
            "rust",
            tree_sitter_rust::LANGUAGE.into(),
            "rust",
            tree_sitter_rust::HIGHLIGHTS_QUERY,
            tree_sitter_rust::INJECTIONS_QUERY,
        );

        register(
            "python",
            tree_sitter_python::LANGUAGE.into(),
            "python",
            tree_sitter_python::HIGHLIGHTS_QUERY,
            "",
        );

        register(
            "javascript",
            tree_sitter_javascript::LANGUAGE.into(),
            "javascript",
            tree_sitter_javascript::HIGHLIGHT_QUERY,
            tree_sitter_javascript::INJECTIONS_QUERY,
        );

        // TypeScript grammar only ships a delta query over JS — register TS with
        // the full JS query prepended so string/comment/function highlighting works.
        let ts_query = [
            tree_sitter_javascript::HIGHLIGHT_QUERY,
            "\n",
            tree_sitter_typescript::HIGHLIGHTS_QUERY,
        ]
        .concat();
        register(
            "typescript",
            tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
            "typescript",
            &ts_query,
            "",
        );

        register(
            "go",
            tree_sitter_go::LANGUAGE.into(),
            "go",
            tree_sitter_go::HIGHLIGHTS_QUERY,
            "",
        );

        register(
            "json",
            tree_sitter_json::LANGUAGE.into(),
            "json",
            tree_sitter_json::HIGHLIGHTS_QUERY,
            "",
        );

        register(
            "toml",
            tree_sitter_toml_ng::LANGUAGE.into(),
            "toml",
            tree_sitter_toml_ng::HIGHLIGHTS_QUERY,
            "",
        );

        register(
            "yaml",
            tree_sitter_yaml::LANGUAGE.into(),
            "yaml",
            tree_sitter_yaml::HIGHLIGHTS_QUERY,
            "",
        );

        register(
            "sql",
            tree_sitter_sequel::LANGUAGE.into(),
            "sql",
            tree_sitter_sequel::HIGHLIGHTS_QUERY,
            "",
        );

        register(
            "bash",
            tree_sitter_bash::LANGUAGE.into(),
            "bash",
            BASH_HIGHLIGHTS_QUERY,
            "",
        );

        // Markdown block-level highlighting only. The block grammar covers
        // headings, markers, links, and code fence delimiters.
        register(
            "markdown",
            tree_sitter_md::LANGUAGE.into(),
            "markdown",
            tree_sitter_md::HIGHLIGHT_QUERY_BLOCK,
            tree_sitter_md::INJECTION_QUERY_BLOCK,
        );

        Self { configs }
    }

    /// Highlight `code` for the given `lang` using `theme`.
    ///
    /// Returns `None` if the language is unsupported or if tree-sitter fails
    /// to parse the input. The returned spans concatenate to the original
    /// source text unchanged.
    ///
    /// # Arguments
    ///
    /// * `lang` — language identifier or alias (case-insensitive).
    /// * `code` — source code to highlight.
    /// * `theme` — style mapping for each token class.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_tui::highlight::SYNTAX_HIGHLIGHTER;
    /// use zeph_tui::theme::SyntaxTheme;
    ///
    /// let theme = SyntaxTheme::default();
    /// let spans = SYNTAX_HIGHLIGHTER.highlight("rust", "let x = 42;", &theme).unwrap();
    /// let text: String = spans.iter().map(|s| s.content.as_ref()).collect();
    /// assert_eq!(text, "let x = 42;");
    /// ```
    pub fn highlight(
        &self,
        lang: &str,
        code: &str,
        theme: &SyntaxTheme,
    ) -> Option<Vec<Span<'static>>> {
        let lang_lower = lang.to_lowercase();
        let canonical = LANG_ALIASES
            .get(lang_lower.as_str())
            .copied()
            .unwrap_or(lang_lower.as_str());
        let config = self.configs.get(canonical)?;

        let mut highlighter = Highlighter::new();
        let events = highlighter
            .highlight(config, code.as_bytes(), None, |_| None)
            .ok()?;

        let mut spans = Vec::new();
        let mut style_stack: Vec<Style> = Vec::new();

        for event in events {
            match event.ok()? {
                HighlightEvent::Source { start, end } => {
                    let text = code.get(start..end).unwrap_or_default();
                    let style = style_stack.last().copied().unwrap_or(theme.default);
                    spans.push(Span::styled(text.to_string(), style));
                }
                HighlightEvent::HighlightStart(highlight) => {
                    let style = capture_to_style(highlight.0, theme);
                    style_stack.push(style);
                }
                HighlightEvent::HighlightEnd => {
                    style_stack.pop();
                }
            }
        }

        Some(spans)
    }
}

fn capture_to_style(index: usize, theme: &SyntaxTheme) -> Style {
    match CAPTURE_NAMES.get(index).copied().unwrap_or_default() {
        "attribute" | "storageclass" => theme.attribute,
        "boolean" | "constant" | "constant.builtin" => theme.constant,
        "comment" => theme.comment,
        "constructor" | "type" | "type.builtin" | "type.qualifier" | "tag" => theme.r#type,
        "escape" | "string" | "string.escape" | "string.special" | "text.literal" => theme.string,
        "function" | "function.builtin" | "function.call" | "function.method"
        | "text.reference" | "text.uri" => theme.function,
        "keyword" | "keyword.operator" | "text.title" => theme.keyword,
        "label" | "property" | "variable" | "variable.builtin" | "variable.parameter" => {
            theme.variable
        }
        "number" => theme.number,
        "operator" => theme.operator,
        "punctuation" | "punctuation.bracket" | "punctuation.delimiter" | "punctuation.special" => {
            theme.punctuation
        }
        _ => theme.default,
    }
}

impl Default for SyntaxTheme {
    fn default() -> Self {
        Self {
            keyword: Style::default()
                .fg(Color::Rgb(198, 120, 221))
                .add_modifier(Modifier::BOLD),
            string: Style::default().fg(Color::Rgb(152, 195, 121)),
            comment: Style::default()
                .fg(Color::Rgb(92, 99, 112))
                .add_modifier(Modifier::ITALIC),
            function: Style::default().fg(Color::Rgb(97, 175, 239)),
            r#type: Style::default().fg(Color::Rgb(229, 192, 123)),
            number: Style::default().fg(Color::Rgb(209, 154, 102)),
            operator: Style::default().fg(Color::Rgb(171, 178, 191)),
            variable: Style::default().fg(Color::Rgb(224, 108, 117)),
            attribute: Style::default().fg(Color::Rgb(229, 192, 123)),
            punctuation: Style::default().fg(Color::Rgb(171, 178, 191)),
            constant: Style::default().fg(Color::Rgb(209, 154, 102)),
            default: Style::default().fg(Color::Rgb(190, 175, 145)),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::*;

    // ---- existing tests ----

    #[test]
    fn highlight_rust_code() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight("rust", "let x = 42;", &theme);
        assert!(spans.is_some());
        let spans = spans.unwrap();
        assert!(!spans.is_empty());
        let text: String = spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(text, "let x = 42;");
    }

    #[test]
    fn highlight_python_code() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight("python", "def foo():\n    pass", &theme);
        assert!(spans.is_some());
    }

    #[test]
    fn highlight_unknown_lang_returns_none() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(hl.highlight("brainfuck", "+++", &theme).is_none());
    }

    #[test]
    fn highlight_json_code() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight("json", r#"{"key": "value"}"#, &theme);
        assert!(spans.is_some());
    }

    #[test]
    fn highlight_js_code() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight("js", "const x = 1;", &theme);
        assert!(spans.is_some());
    }

    #[test]
    fn highlight_alias_rs() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(hl.highlight("rs", "fn main() {}", &theme).is_some());
    }

    #[test]
    fn highlight_empty_string() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight("rust", "", &theme);
        assert!(spans.is_some());
        assert!(spans.unwrap().is_empty());
    }

    #[test]
    fn highlight_malformed_code_no_panic() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        // Malformed Rust — should not panic, tree-sitter is error-tolerant
        let spans = hl.highlight("rust", "fn {{{{ let !!!", &theme);
        assert!(spans.is_some());
    }

    #[test]
    fn highlight_toml_code() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight("toml", "[package]\nname = \"foo\"", &theme);
        assert!(spans.is_some());
    }

    #[test]
    fn highlight_bash_code() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight("bash", "echo \"hello\"", &theme);
        assert!(spans.is_some());
    }

    #[test]
    fn rust_keywords_get_keyword_style() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight("rust", "let x = 1;", &theme).unwrap();
        let let_span = spans.iter().find(|s| s.content.as_ref() == "let").unwrap();
        assert_eq!(let_span.style, theme.keyword);
    }

    // ---- new language tests ----

    fn assert_multi_style(lang: &str, code: &str) {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        let spans = hl.highlight(lang, code, &theme).unwrap_or_else(|| {
            panic!("highlight returned None for language '{lang}'");
        });
        let styles: HashSet<_> = spans.iter().map(|s| s.style).collect();
        assert!(
            styles.len() > 1,
            "expected >1 distinct style for '{lang}', got {}: {:?}",
            styles.len(),
            spans.iter().map(|s| s.content.as_ref()).collect::<Vec<_>>()
        );
    }

    #[test]
    fn highlight_typescript_multi_style() {
        assert_multi_style(
            "typescript",
            "const greet = (name: string): void => {\n  console.log(name);\n};",
        );
    }

    #[test]
    fn highlight_typescript_alias_ts() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(hl.highlight("ts", "let x: number = 1;", &theme).is_some());
    }

    #[test]
    fn highlight_typescript_alias_tsx() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(
            hl.highlight("tsx", "const x: string = 'hello';", &theme)
                .is_some()
        );
    }

    #[test]
    fn highlight_go_multi_style() {
        assert_multi_style(
            "go",
            "package main\n\nimport \"fmt\"\n\nfunc main() {\n    fmt.Println(\"hello\")\n}",
        );
    }

    #[test]
    fn highlight_go_alias_golang() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(hl.highlight("golang", "func f() {}", &theme).is_some());
    }

    #[test]
    fn highlight_yaml_multi_style() {
        assert_multi_style("yaml", "name: foo\nversion: \"1.0\"\nenabled: true\n");
    }

    #[test]
    fn highlight_yaml_alias_yml() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(
            hl.highlight("yml", "key: value\nlist:\n  - a\n  - b", &theme)
                .is_some()
        );
    }

    #[test]
    fn highlight_sql_multi_style() {
        assert_multi_style("sql", "SELECT id, name FROM users WHERE active = true;");
    }

    #[test]
    fn highlight_sql_alias_mysql() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(hl.highlight("mysql", "SELECT * FROM t;", &theme).is_some());
    }

    #[test]
    fn highlight_markdown_headings_and_links() {
        assert_multi_style(
            "markdown",
            "# Hello World\n\n[link text](https://example.com)\n",
        );
    }

    #[test]
    fn highlight_markdown_alias_md() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(hl.highlight("md", "# Title\nSome text.", &theme).is_some());
    }

    #[test]
    fn highlight_unknown_still_none_after_new_langs() {
        let hl = &*SYNTAX_HIGHLIGHTER;
        let theme = SyntaxTheme::default();
        assert!(hl.highlight("brainfuck", "+++", &theme).is_none());
        assert!(hl.highlight("cobol", "DISPLAY 'hi'", &theme).is_none());
    }
}