use std::io::IsTerminal;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
Error,
Warning,
Note,
}
impl Severity {
fn label(self) -> &'static str {
match self {
Severity::Error => "error",
Severity::Warning => "warning",
Severity::Note => "note",
}
}
fn color(self) -> &'static str {
match self {
Severity::Error => "\x1b[31m",
Severity::Warning => "\x1b[33m",
Severity::Note => "\x1b[36m",
}
}
}
#[derive(Debug, Clone)]
pub struct Diag {
pub path: PathBuf,
pub line: u32,
pub col: u32,
pub len: u32,
pub severity: Severity,
pub message: String,
pub source: &'static str,
}
pub fn print_diag(d: &Diag) {
let color = std::env::var_os("NO_COLOR").is_none() && std::io::stdout().is_terminal();
let (c0, c1, dim, dim0) = if color {
(d.severity.color(), "\x1b[0m", "\x1b[2m", "\x1b[0m")
} else {
("", "", "", "")
};
println!(
"{}:{}:{}: {c0}{}{c1}: {} {dim}[glslint/{}]{dim0}",
d.path.display(),
d.line,
d.col,
d.severity.label(),
d.message,
d.source,
);
}