Skip to main content

rpi_cli/
brand.rs

1//! 启动时的品牌标志:一枚会自转的 3D 齿轮徽章。
2//!
3//! 形状由 [`shape`] 描述,[`render`] 负责光栅化,[`spin`] 提供片头动画的时钟。
4//! 输出是纯 ASCII(单宽字符 + 主题色),所以纯文本终端、远程会话、
5//! 没有图片协议的终端里都能显示。
6
7pub(crate) mod render;
8mod shape;
9mod spin;
10
11use rpi_tui::Component;
12
13pub use spin::{animating, replay, set_enabled};
14
15/// 终端窗口/标签标题里的紧凑标记。
16pub const MARK: &str = "@";
17
18/// 把标志压成一行终端窗口标题。
19pub fn window_title(status: &str) -> String {
20    if status.is_empty() {
21        format!("{MARK} rpi")
22    } else {
23        format!("{MARK} rpi {status}")
24    }
25}
26
27/// 启动标志。加到欢迎页 header 里。
28pub struct BrandLockup;
29
30impl BrandLockup {
31    pub fn new() -> Self {
32        Self
33    }
34}
35
36impl Default for BrandLockup {
37    fn default() -> Self {
38        Self::new()
39    }
40}
41
42impl Component for BrandLockup {
43    fn render(&self, _width: usize) -> Vec<String> {
44        render::lockup(spin::angle())
45    }
46
47    fn invalidate(&self) {
48        // Nothing is cached.
49    }
50
51    fn as_any(&self) -> &dyn std::any::Any {
52        self
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn lockup_fits_the_welcome_header() {
62        let rows = BrandLockup::new().render(80);
63        assert_eq!(rows.len(), render::ROWS);
64    }
65
66    #[test]
67    fn title_keeps_the_logo_identity_and_status_last() {
68        assert_eq!(window_title(""), "@ rpi");
69        assert_eq!(window_title("⟳"), "@ rpi ⟳");
70        assert_eq!(window_title("↻"), "@ rpi ↻");
71        assert!(!window_title("⟳").contains('\n'));
72    }
73}