Skip to main content

rpi_cli/
brand.rs

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