OXYGENHUB

Coverage / src/cli/ui.rs 83.1%

152 of 183 lines run, 57 of 97 functions · run #325 on main · run missed

1
//! Terminal presentation: colour, emphasis, and a live spinner while a
2
//! command works. Everything degrades to plain text when stdout is not a
3
//! terminal, when `NO_COLOR` is set, or with `--plain`.
4
5
use console::{style, StyledObject, Term};
6
use indicatif::{ProgressBar, ProgressStyle};
7
use std::sync::atomic::{AtomicBool, Ordering};
8
use std::time::Duration;
9
10
static PLAIN: AtomicBool = AtomicBool::new(false);
11
static UNICODE: AtomicBool = AtomicBool::new(false);
12
static INIT: std::sync::Once = std::sync::Once::new();
13
14
/// Decide once whether to use colour, glyphs, and animation.
15157×
pub fn init(plain_flag: bool) {
16157×
    INIT.call_once(|| {
17149×
        let no_color = std::env::var_os("NO_COLOR").is_some();
18149×
        let tty = Term::stdout().features().is_attended();
19149×
        let plain = plain_flag || no_color || !tty;
20149×
        PLAIN.store(plain, Ordering::Relaxed);
21149×
        console::set_colors_enabled(!plain);
22149×
        let utf8 = ["LC_ALL", "LC_CTYPE", "LANG"]
23149×
            .iter()
24447×
            .filter_map(|k| std::env::var(k).ok())
25149×
            .find(|v| !v.is_empty())
26149×
            .map(|v| {
270×
                v.to_ascii_uppercase().contains("UTF-8") || v.to_ascii_uppercase().contains("UTF8")
280×
            })
29149×
            .unwrap_or(false);
30149×
        UNICODE.store(!plain && utf8, Ordering::Relaxed);
31149×
    });
32157×
}
33
3459×
pub fn plain() -> bool {
3559×
    PLAIN.load(Ordering::Relaxed)
3659×
}
37
38
/// Whether the terminal takes Unicode glyphs (a UTF-8 locale, not `--plain`).
39191×
pub fn unicode() -> bool {
40191×
    UNICODE.load(Ordering::Relaxed)
41191×
}
42
43
/// A glyph with an ASCII fallback for plain output and non-UTF-8 locales.
44190×
pub fn glyph<'a>(fancy: &'a str, ascii: &'a str) -> &'a str {
45190×
    if unicode() {
460×
        fancy
47
    } else {
48190×
        ascii
49
    }
50190×
}
51
52
/// A finished action: a tick and the verb in green.
5388×
pub fn done<T: std::fmt::Display>(v: T) -> String {
5488×
    format!("{} {}", ok(glyph("✔", "*")), ok(v))
5588×
}
56
57
/// A warning line prefix.
583×
pub fn caution() -> String {
593×
    warn(glyph("⚠", "!"))
603×
}
61
62
/// An informational note prefix.
6317×
pub fn note() -> String {
6417×
    dim(glyph("›", ">"))
6517×
}
66
67
/// A section title.
683×
pub fn section<T: std::fmt::Display>(v: T) -> String {
693×
    format!("{} {}", s(glyph("▍", "#")).cyan(), s(v).bold())
703×
}
71
72
/// `  key   value`, with the key dimmed and padded.
7342×
pub fn kv<T: std::fmt::Display>(key: &str, value: T) -> String {
7442×
    format!("  {} {value}", dim(format!("{key:<8}")))
7542×
}
76
77
/// An author name.
7823×
pub fn author<T: std::fmt::Display>(v: T) -> String {
7923×
    s(v).magenta().to_string()
8023×
}
81
82
/// A bookmark or reference name.
8378×
pub fn bookmark<T: std::fmt::Display>(v: T) -> String {
8478×
    s(v).green().bold().to_string()
8578×
}
86
87
/// The subject line of a message.
8828×
pub fn subject<T: std::fmt::Display>(v: T) -> String {
8928×
    s(v).bold().to_string()
9028×
}
91
92
/// A dimmed separator between the parts of a metadata line.
9320×
pub fn sep() -> String {
9420×
    dim(format!(" {} ", glyph("·", "-")))
9520×
}
96
97
/// Milliseconds since the epoch as a relative time ("3 minutes ago"), or the
98
/// date when older than a month.
9913×
pub fn ago(ms: u64) -> String {
10013×
    let now = std::time::SystemTime::now()
10113×
        .duration_since(std::time::UNIX_EPOCH)
10213×
        .map(|d| d.as_millis() as u64)
10313×
        .unwrap_or(0);
10413×
    let secs = now.saturating_sub(ms) / 1000;
10513×
    let unit = |n: u64, w: &str| format!("{n} {w}{} ago", if n == 1 { "" } else { "s" });
10613×
    match secs {
10713×
        0..=44 => "just now".into(),
1086×
        45..=89 => "a minute ago".into(),
1095×
        90..=2_699 => unit(secs.div_ceil(60), "minute"),
1104×
        2_700..=5_399 => "an hour ago".into(),
1114×
        5_400..=79_199 => unit(secs.div_ceil(3_600), "hour"),
1123×
        79_200..=129_599 => "yesterday".into(),
1132×
        129_600..=2_591_999 => unit(secs.div_ceil(86_400), "day"),
1141×
        _ => super::format::when(ms)
1151×
            .split(' ')
1161×
            .next()
1171×
            .unwrap_or("")
1181×
            .to_string(),
119
    }
12013×
}
121
122
/// The bullet for a log entry: the working copy, a head, or an ordinary change.
1234×
pub fn log_bullet(working_copy: bool, head: bool, conflicted: bool) -> String {
1244×
    if working_copy {
1251×
        s(glyph("◉", "@")).cyan().bold().to_string()
1263×
    } else if conflicted {
1270×
        err(glyph("✱", "!"))
1283×
    } else if head {
1292×
        ok(glyph("●", "*"))
130
    } else {
1311×
        dim(glyph("○", "o"))
132
    }
1334×
}
134
135
/// A status glyph and word for a working-copy change.
1362×
pub fn status_mark(tag: &str) -> String {
1372×
    let (g, a, word) = match tag {
1382×
        "A" => ("✚", "+", "added"),
1391×
        "M" => ("✎", "~", "modified"),
1401×
        "D" => ("✖", "-", "removed"),
1411×
        "X" => ("⚙", "x", "mode"),
1420×
        "C!" => ("‼", "!", "markers"),
1430×
        "R" => ("➜", ">", "renamed"),
1440×
        "V" => ("✔", "r", "resolved"),
1450×
        _ => ("?", "?", "unknown"),
146
    };
1472×
    let text = format!("{} {word:<9}", glyph(g, a));
1482×
    self::tag_style(tag, &text)
1492×
}
150
1512×
fn tag_style(t: &str, text: &str) -> String {
1522×
    match t {
1532×
        "A" | "V" => s(text).green().to_string(),
1541×
        "R" => s(text).cyan().to_string(),
1551×
        "M" | "X" => s(text).yellow().to_string(),
1560×
        "D" | "C!" => s(text).red().to_string(),
1570×
        _ => text.to_string(),
158
    }
1592×
}
160
161776×
fn s<T: std::fmt::Display>(v: T) -> StyledObject<T> {
162776×
    style(v)
163776×
}
164
165
/// A ChangeId prefix.
166103×
pub fn id<T: std::fmt::Display>(v: T) -> String {
167103×
    s(v).cyan().bold().to_string()
168103×
}
169
/// A hash prefix.
17040×
pub fn hash<T: std::fmt::Display>(v: T) -> String {
17140×
    s(v).dim().to_string()
17240×
}
17328×
pub fn path<T: std::fmt::Display>(v: T) -> String {
17428×
    s(v).bold().to_string()
17528×
}
176207×
pub fn ok<T: std::fmt::Display>(v: T) -> String {
177207×
    s(v).green().to_string()
178207×
}
1796×
pub fn warn<T: std::fmt::Display>(v: T) -> String {
1806×
    s(v).yellow().to_string()
1816×
}
18210×
pub fn err<T: std::fmt::Display>(v: T) -> String {
18310×
    s(v).red().bold().to_string()
18410×
}
185244×
pub fn dim<T: std::fmt::Display>(v: T) -> String {
186244×
    s(v).dim().to_string()
187244×
}
1880×
pub fn heading<T: std::fmt::Display>(v: T) -> String {
1890×
    s(v).bold().underlined().to_string()
1900×
}
191
/// A live spinner with a message, shown only on a terminal. Drop or call
192
/// `done` to clear it; `done` prints the final line in its place.
193
pub struct Spinner {
194
    bar: Option<ProgressBar>,
195
}
196
19759×
pub fn spinner(msg: &str) -> Spinner {
19859×
    if plain() {
19959×
        return Spinner { bar: None };
2000×
    }
2010×
    let bar = ProgressBar::new_spinner();
2020×
    bar.set_style(
2030×
        ProgressStyle::with_template("{spinner:.cyan} {msg}")
2040×
            .unwrap()
2050×
            .tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏", " "]),
206
    );
2070×
    bar.set_message(msg.to_string());
2080×
    bar.enable_steady_tick(Duration::from_millis(80));
2090×
    Spinner { bar: Some(bar) }
21059×
}
211
212
impl Spinner {
213
    /// Replace the spinner with a final line (or print it plainly). The
214
    /// line is printed as given: callers start it with their own mark.
21540×
    pub fn done(self, msg: &str) {
21640×
        if let Some(b) = &self.bar {
2170×
            b.finish_and_clear();
21840×
        }
21940×
        println!("{msg}");
22040×
    }
221
    /// Say what is happening now: the spinner's message, or, with no
222
    /// terminal, a line on stderr, so a long command is never silent.
2230×
    pub fn set(&self, msg: &str) {
2240×
        match &self.bar {
2250×
            Some(b) => b.set_message(msg.to_string()),
2260×
            None => eprintln!("{} {msg}", dim(glyph("›", ">"))),
227
        }
2280×
    }
229
    /// Clear the spinner without printing.
23017×
    pub fn clear(self) {
23117×
        if let Some(b) = &self.bar {
2320×
            b.finish_and_clear();
23317×
        }
23417×
    }
235
}
236
237
#[cfg(test)]
238
mod tests {
239
    use super::*;
240
241
    #[test]
2421×
    fn relative_times() {
2431×
        let now = std::time::SystemTime::now()
2441×
            .duration_since(std::time::UNIX_EPOCH)
2451×
            .unwrap()
2461×
            .as_millis() as u64;
2471×
        assert_eq!(ago(now), "just now");
2481×
        assert_eq!(ago(now - 60_000), "a minute ago");
2491×
        assert_eq!(ago(now - 5 * 60_000), "5 minutes ago");
2501×
        assert_eq!(ago(now - 3 * 3_600_000), "3 hours ago");
2511×
        assert_eq!(ago(now - 25 * 3_600_000), "yesterday");
2521×
        assert_eq!(ago(now - 4 * 86_400_000), "4 days ago");
2531×
        assert_eq!(ago(0), "1970-01-01");
2541×
        assert_eq!(glyph("✔", "*"), if unicode() { "✔" } else { "*" });
2551×
    }
256
}