egrix 0.1.0

Find leaked credentials and suspicious network destinations in AI agent transcripts. Local, offline, redacted by default.
//! Turning a [`Report`](crate::model::Report) into output. One module per
//! format; all three read the same report, so they cannot disagree.

pub mod json;
pub mod sarif;
pub mod text;

use crate::model::Repo;

/// A repository's label: its short name, or its remote (falling back to the
/// path when it has none).
pub fn repo_label(repo: Option<&Repo>, remote: bool) -> String {
    match repo {
        None => "(no repository)".into(),
        Some(r) if !remote => r.name.clone(),
        Some(r) => r.remote.clone().unwrap_or_else(|| format!("{} (no remote)", r.path)),
    }
}

/// ANSI colors, on only when writing to a terminal that did not opt out.
#[derive(Clone, Copy)]
pub struct Style {
    enabled: bool,
}

impl Style {
    pub fn detect() -> Self {
        use std::io::IsTerminal;
        Self { enabled: std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none() }
    }
    fn paint(self, code: &str, s: &str) -> String {
        if self.enabled {
            format!("\x1b[{code}m{s}\x1b[0m")
        } else {
            s.to_string()
        }
    }
    pub fn bad(self, s: &str) -> String {
        self.paint("31", s)
    }
    pub fn warn(self, s: &str) -> String {
        self.paint("33", s)
    }
    pub fn good(self, s: &str) -> String {
        self.paint("32", s)
    }
    pub fn dim(self, s: &str) -> String {
        self.paint("2", s)
    }
    pub fn bold(self, s: &str) -> String {
        self.paint("1", s)
    }
}

pub fn plural(n: usize, word: &str) -> String {
    format!("{n} {word}{}", if n == 1 { "" } else { "s" })
}