Skip to main content

rpi_cli/
brand.rs

1//! The startup brand lockup, rendered from the README logo with the same
2//! black-and-white ASCII palette as https://neucrack.com/tools/ascii.
3//!
4//! The logo is kept as text rather than loading the README JPEG so it works in
5//! plain terminals, remote sessions, and terminals without image protocols.
6//! Its rows use fixed single-cell geometry and are scaled down for the
7//! terminal's character aspect ratio.
8
9use rpi_tui::{ansi::visible_width, theme as current_theme, Component};
10
11/// A compact ASCII approximation used in the terminal window/tab title.
12/// The full gear is rendered by [`render_lockup`].
13pub const MARK: &str = "@";
14
15const LOGO_ROWS: [&str; 18] = [
16    "                                            ",
17    "                    :::.                    ",
18    "             -=:   *=..#:   ==:             ",
19    "           =#. -=--:   .--==. -%            ",
20    "        .  =#  :===========-. -*:  .        ",
21    "      :*--=..++-           .=*- ---=*       ",
22    "      =+:  +*.                -#:  =+:      ",
23    "       == ++      -%===**:     .%. %.       ",
24    "    -+--  @     .#:.+===.++     =* :--#     ",
25    "    =*-: .@     .: %*.:%..@  .  =* .:-%     ",
26    "      .+- *= =#==- %=--:=*: :+  %: %:       ",
27    "      :+=  *++*    %-.@-.   :%:#: .+=.      ",
28    "      -*.:-..*+    %-.@    .:%= --.-*.      ",
29    "       .-..=*. .===%-.@====-  =*:.::        ",
30    "           :%. .=--:   .--==  =#            ",
31    "            .-==   *=..#.  .==:             ",
32    "                    :::.                    ",
33    "                                            ",
34];
35
36/// The startup lockup flattened into a terminal window/tab title.
37pub fn window_title(status: &str) -> String {
38    if status.is_empty() {
39        format!("{MARK} rpi")
40    } else {
41        format!("{MARK} rpi {status}")
42    }
43}
44
45/// Apply the current theme to the monochrome ASCII output.
46fn style_logo_row(row: &str) -> String {
47    current_theme().colors.dim.fg(row)
48}
49
50fn render_lockup() -> Vec<String> {
51    let width = LOGO_ROWS
52        .iter()
53        .map(|row| visible_width(row))
54        .max()
55        .unwrap_or(0);
56    LOGO_ROWS
57        .iter()
58        .map(|row| {
59            let padded = format!(
60                "{row}{}",
61                " ".repeat(width.saturating_sub(visible_width(row)))
62            );
63            style_logo_row(&padded)
64        })
65        .collect()
66}
67
68/// The startup logo. Add one to the welcome header.
69pub struct BrandLockup;
70
71impl BrandLockup {
72    pub fn new() -> Self {
73        Self
74    }
75}
76
77impl Default for BrandLockup {
78    fn default() -> Self {
79        Self::new()
80    }
81}
82
83impl Component for BrandLockup {
84    fn render(&self, _width: usize) -> Vec<String> {
85        render_lockup()
86    }
87
88    fn invalidate(&self) {
89        // Nothing is cached.
90    }
91
92    fn as_any(&self) -> &dyn std::any::Any {
93        self
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100    use rpi_tui::ansi::{strip_ansi, visible_width};
101
102    fn plain() -> Vec<String> {
103        render_lockup().iter().map(|r| strip_ansi(r)).collect()
104    }
105
106    #[test]
107    fn logo_has_the_readme_lockup_shape() {
108        let rows = plain();
109        assert_eq!(rows.len(), 18);
110        assert!(rows.iter().any(|row| row.contains("#")));
111        assert!(rows.iter().any(|row| row.contains("+=")));
112        assert!(rows[1].contains(":::"));
113        assert!(rows[16].contains(":::"));
114    }
115
116    #[test]
117    fn logo_rows_have_stable_terminal_geometry() {
118        let rows = plain();
119        let widths: Vec<usize> = rows.iter().map(|row| visible_width(row)).collect();
120        assert!(widths.iter().all(|width| *width == widths[0]));
121        assert_eq!(widths[0], 44);
122    }
123
124    #[test]
125    fn title_keeps_the_logo_identity_and_status_last() {
126        assert_eq!(window_title(""), "@ rpi");
127        assert_eq!(window_title("⟳"), "@ rpi ⟳");
128        assert_eq!(window_title("↻"), "@ rpi ↻");
129        assert!(!window_title("⟳").contains('\n'));
130    }
131}