Skip to main content

pxh/
ui.rs

1//! How pxh talks to a human.
2//!
3//! Every diagnostic line pxh prints -- as opposed to history rows, JSON, or
4//! shell-config text, which are *data* -- goes through here, so a message's
5//! severity is a named thing rather than a prefix typed at the call site.
6//!
7//! Styling is always emitted by `render`; whether it survives is the sink's
8//! decision. The sinks print through `anstream`, which strips ANSI when stderr
9//! is not a terminal and honors `NO_COLOR`, `CLICOLOR`, `CLICOLOR_FORCE`, and
10//! `TERM=dumb`. stdout is reserved for data and is never written from here.
11
12/// What a message *is*. Decides the color and the leading word.
13#[derive(Clone, Copy, PartialEq, Eq, Debug)]
14pub enum Level {
15    /// Something is wrong but the command continues.
16    Warn,
17    /// The command failed.
18    Error,
19    /// A suggestion subordinate to a nearby line (`run pxh maintenance ...`).
20    Hint,
21}
22
23/// The palette. Nothing outside this module spells an SGR sequence.
24pub mod sgr {
25    pub const YELLOW: &str = "\x1b[33m";
26    pub const RED: &str = "\x1b[31m";
27    pub const DIM: &str = "\x1b[2m";
28    pub const RESET: &str = "\x1b[0m";
29}
30
31use sgr::{DIM, RED, RESET, YELLOW};
32
33/// Render one message: `<word>: <text>`, colored. Pure, so the vocabulary is
34/// testable without a terminal.
35fn render(level: Level, text: &str) -> String {
36    let (sgr, word) = match level {
37        Level::Warn => (YELLOW, "warning"),
38        Level::Error => (RED, "error"),
39        Level::Hint => (DIM, "hint"),
40    };
41    format!("{sgr}{word}: {text}{RESET}")
42}
43
44fn emit(level: Level, text: &str) {
45    anstream::eprintln!("{}", render(level, text));
46}
47
48/// Something is wrong; the command continues.
49pub fn warn(text: &str) {
50    emit(Level::Warn, text);
51}
52
53/// The command failed. Rendered `error: <text>`.
54pub fn error(text: &str) {
55    emit(Level::Error, text);
56}
57
58/// A suggestion: `hint: run pxh maintenance to reclaim disk space`.
59pub fn hint(text: &str) {
60    emit(Level::Hint, text);
61}
62
63/// `1 secret` / `3 secrets` -- every counted noun pxh prints goes through
64/// here, so nothing says `secret(s)`. Nouns pluralize with a plain `s`; that
65/// is true of everything pxh counts (secret, issue, entry, command, day, hour).
66pub fn count(n: usize, noun: &str) -> String {
67    if n == 1 { format!("1 {noun}") } else { format!("{n} {noun}s") }
68}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn count_pluralizes_everything_but_one() {
76        assert_eq!(count(0, "secret"), "0 secrets");
77        assert_eq!(count(1, "secret"), "1 secret");
78        assert_eq!(count(2, "issue"), "2 issues");
79    }
80
81    #[test]
82    fn levels_say_the_lowercase_word() {
83        assert_eq!(render(Level::Error, "boom"), format!("{RED}error: boom{RESET}"));
84        assert_eq!(render(Level::Warn, "hmm"), format!("{YELLOW}warning: hmm{RESET}"));
85        assert_eq!(render(Level::Hint, "try x"), format!("{DIM}hint: try x{RESET}"));
86    }
87
88    /// Whatever a sink later decides, the rendered bytes must strip back to
89    /// exactly the text a non-terminal should see.
90    #[test]
91    fn stripping_a_rendered_line_leaves_plain_text() {
92        let styled = render(Level::Error, "unable to open database");
93        let plain = anstream::adapter::strip_str(&styled).to_string();
94        assert_eq!(plain, "error: unable to open database");
95    }
96}