• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dcdpr / jp / 22154979281

18 Feb 2026 07:44PM UTC coverage: 55.288% (+1.3%) from 54.027%
22154979281

Pull #395

github

web-flow
Merge 0b5125385 into 76444fafa
Pull Request #395: Vet

780 of 1027 new or added lines in 36 files covered. (75.95%)

3 existing lines in 3 files now uncovered.

11606 of 20992 relevant lines covered (55.29%)

117.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/crates/jp_term/src/code.rs
1
use syntect::{
2
    easy::HighlightLines,
3
    highlighting::ThemeSet,
4
    parsing::SyntaxSet,
5
    util::{LinesWithEndings, as_24_bit_terminal_escaped},
6
};
7

8
pub struct Config {
9
    /// The language sytax to use for syntax highlighting.
10
    pub language: Option<String>,
11

12
    /// The color theme to use for syntax highlighting.
13
    ///
14
    /// If `None`, no coloring will be used.
15
    pub theme: Option<String>,
16
}
17

18
/// Format a code block.
19
///
20
/// Returns `true` if the code block was formatted successfully, and `false`
21
/// if the code block was not formatted because the language was unknown.
22
///
23
/// # Errors
24
///
25
/// Returns an error if the code block could not be formatted.
NEW
26
pub fn format(content: &str, buf: &mut String, config: &Config) -> Result<bool, syntect::Error> {
×
NEW
27
    let ss = SyntaxSet::load_defaults_newlines();
×
28

NEW
29
    let syntax = match config.language.as_deref() {
×
NEW
30
        Some(lang) => match ss.find_syntax_by_token(lang) {
×
NEW
31
            Some(s) => s,
×
NEW
32
            None => return Ok(false),
×
33
        },
NEW
34
        None => ss.find_syntax_plain_text(),
×
35
    };
36

NEW
37
    let Some(theme_name) = config.theme.as_deref() else {
×
NEW
38
        buf.push_str(content);
×
NEW
39
        return Ok(true);
×
40
    };
41

NEW
42
    let ts = ThemeSet::load_defaults();
×
NEW
43
    let Some(theme) = ts.themes.get(theme_name) else {
×
NEW
44
        buf.push_str(content);
×
NEW
45
        return Ok(true);
×
46
    };
47

NEW
48
    let mut h = HighlightLines::new(syntax, theme);
×
NEW
49
    for line in LinesWithEndings::from(content) {
×
NEW
50
        let ranges = h.highlight_line(line, &ss)?;
×
NEW
51
        let escaped = as_24_bit_terminal_escaped(&ranges, true);
×
NEW
52
        buf.push_str(&escaped);
×
53
    }
NEW
54
    buf.push_str("\x1b[0m");
×
55

NEW
56
    Ok(true)
×
UNCOV
57
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc