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            Severity::Advice => ("advice", "\x1b[36m"),
18        };
19        let (open, close) = match color {
20            true => (shade, "\x1b[0m"),
21            false => ("", ""),
22        };
23        let at = d.file.as_deref().map_or(String::new(), |f| format!(" {f}"));
24        out.push_str(&format!(
25            "{open}{word}{close}[{}]{at}: {}\n",
26            d.code, d.message
27        ));
28        if let Some(help) = d.help.as_deref() {
29            out.push_str(&format!("  help: {help}\n"));
30        }
31    }
32    out.push_str(&format!("{} diagnostic(s)", diagnostics.len()));
33    out
34}