Skip to main content

git_stk/
style.rs

1//! Semantic terminal styles. Styled lines must be printed through
2//! `anstream::println!`/`eprintln!`, which strip color for pipes, NO_COLOR,
3//! and consoles that cannot render it.
4
5use anstyle::{AnsiColor, Style};
6
7use crate::providers::ReviewState;
8
9/// Branch names.
10pub const BRANCH: Style = AnsiColor::Cyan.on_default();
11/// The branch you are standing on.
12pub const CURRENT: Style = AnsiColor::Green.on_default().bold();
13/// Secondary detail: URLs, the trunk tag, counts.
14pub const DIM: Style = Style::new().dimmed();
15/// The `hint:` prefix.
16pub const HINT: Style = AnsiColor::Cyan.on_default();
17/// The `warning:` prefix.
18pub const WARN: Style = AnsiColor::Yellow.on_default();
19/// The `error:` prefix.
20pub const ERROR: Style = AnsiColor::Red.on_default().bold();
21
22/// Review states, matching the ledger emoji: green open, purple merged,
23/// red closed.
24pub const OPEN: Style = AnsiColor::Green.on_default();
25pub const MERGED: Style = AnsiColor::Magenta.on_default();
26pub const CLOSED: Style = AnsiColor::Red.on_default();
27
28pub fn paint(style: Style, text: &str) -> String {
29    format!("{style}{text}{style:#}")
30}
31
32/// A branch name in the branch color.
33pub fn branch(name: &str) -> String {
34    paint(BRANCH, name)
35}
36
37/// Secondary detail: ids, urls, skip lines, previews.
38pub fn dim(text: &str) -> String {
39    paint(DIM, text)
40}
41
42/// Completion lines ("... complete", "merged ...").
43pub fn success(text: &str) -> String {
44    paint(OPEN, text)
45}
46
47/// Notable-but-not-fatal lines.
48pub fn warn(text: &str) -> String {
49    paint(WARN, text)
50}
51
52/// The shared `hint:` prefix.
53pub fn hint_prefix() -> String {
54    paint(HINT, "hint:")
55}
56
57/// The shared `error:` prefix.
58pub fn error_prefix() -> String {
59    paint(ERROR, "error:")
60}
61
62/// A review state in its color.
63pub fn state(state: &ReviewState) -> String {
64    let style = match state {
65        ReviewState::Open => OPEN,
66        ReviewState::Merged => MERGED,
67        ReviewState::Closed => CLOSED,
68        ReviewState::Unknown(_) => DIM,
69    };
70    paint(style, &state.to_string())
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn paint_wraps_text_in_escape_codes() {
79        let painted = paint(BRANCH, "feature/a");
80        assert!(painted.contains("feature/a"));
81        assert!(painted.starts_with('\u{1b}'));
82        assert!(painted.ends_with('m'));
83    }
84
85    #[test]
86    fn state_uses_the_ledger_palette() {
87        assert!(state(&ReviewState::Open).contains("open"));
88        assert!(state(&ReviewState::Merged).contains("merged"));
89        assert!(state(&ReviewState::Closed).contains("closed"));
90        assert_ne!(
91            state(&ReviewState::Open),
92            state(&ReviewState::Merged),
93            "states should not share a style"
94        );
95    }
96}