Skip to main content

sva_cli/
terminal.rs

1// Concern: renders diagnostics already answered as terminal lines | Non-concern: finding them (lint.rs), the JSON rendering of the same objects (output.rs) | IO: (&[Diagnostic], color) -> text
2
3use sva_core::{Diagnostic, Severity};
4
5pub fn colored() -> bool {
6    use std::io::IsTerminal;
7    std::io::stdout().is_terminal()
8}
9
10/// The objects the envelope carries, one line each.
11pub fn diagnostics_text(diagnostics: &[Diagnostic], color: bool) -> String {
12    let mut out = String::new();
13    for d in diagnostics {
14        let (word, shade) = match d.severity {
15            Severity::Error => ("error", "\x1b[31m"),
16            Severity::Warning => ("warning", "\x1b[33m"),
17        };
18        let (open, close) = match color {
19            true => (shade, "\x1b[0m"),
20            false => ("", ""),
21        };
22        let at = d.file.as_deref().map_or(String::new(), |f| format!(" {f}"));
23        out.push_str(&format!(
24            "{open}{word}{close}[{}]{at}: {}\n",
25            d.code, d.message
26        ));
27        if let Some(help) = d.help.as_deref() {
28            out.push_str(&format!("  help: {help}\n"));
29        }
30    }
31    out.push_str(&format!("{} diagnostic(s)", diagnostics.len()));
32    out
33}