pub mod json;
pub mod sarif;
pub mod text;
use crate::model::Repo;
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)),
}
}
#[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 {
if n == 1 {
return format!("{n} {word}");
}
let sibilant = word.ends_with('s') || word.ends_with('x') || word.ends_with('z') || word.ends_with("ch") || word.ends_with("sh");
format!("{n} {word}{}", if sibilant { "es" } else { "s" })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plural_spells_sibilants_with_es() {
assert_eq!(plural(1, "match"), "1 match");
assert_eq!(plural(2, "match"), "2 matches");
assert_eq!(plural(2, "host"), "2 hosts");
assert_eq!(plural(2, "file"), "2 files");
assert_eq!(plural(2, "box"), "2 boxes");
}
}