rpi-cli 0.3.2

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates — a Rust port of @earendil-works/pi-coding-agent's CLI surface
Documentation
//! The startup brand lockup: the three-bar mark, the `rpi` wordmark, and the
//! Rust crab standing in for the language name.
//!
//! The layout exists as a module rather than inline in the welcome header for
//! one reason: **width must be unambiguous.** The lockup this replaced drew a
//! crab above a box containing `π`. `π` is East-Asian *Ambiguous* — width 1 in
//! most locales and width 2 under a CJK locale — so the box borders walked out
//! of alignment depending on the user's system. The mark here is built from `█`,
//! which is single-width everywhere; the crab is the only wide glyph, so it is
//! the one thing the layout has to account for, and a test pins its width.

use rpi_tui::{bold as tui_bold, theme as current_theme, Component};

use crate::args::is_truthy_env_flag;

/// Set to `1`/`true`/`yes` to render the lockup without the crab.
pub const NO_EMOJI_ENV: &str = "RPI_NO_EMOJI";

/// The Rust mascot, standing where the word "rust" used to be.
///
/// Emoji render correctly in every terminal we target, but the rest of the UI
/// deliberately avoids them (see the tool status indicator comment in
/// `rpi-tui`), so this one is opt-out rather than mandatory.
pub const CRAB: &str = "🦀";

/// Separator between `rpi` and the crab.
const SEPARATOR: &str = "·";

/// Pure form of the opt-out check, so the logic is testable without mutating the
/// process environment (which is global state and races with parallel tests).
pub(crate) fn crab_enabled_from(value: Option<&str>) -> bool {
    !is_truthy_env_flag(value)
}

fn crab_enabled() -> bool {
    crab_enabled_from(std::env::var(NO_EMOJI_ENV).ok().as_deref())
}

/// The five rows of the mark and wordmark. `crab` adds the mascot after the
/// wordmark; without it the wordmark stands alone.
fn render_lockup(crab: bool) -> Vec<String> {
    let c = current_theme().colors;
    let bar = c.dim.fg("██");
    let bar_accent = c.accent.fg("██");

    // Name plus the language mark. Without the crab there is nothing to
    // separate, so the separator goes with it rather than dangling.
    let name = if crab {
        format!(
            "{} {} {CRAB}",
            c.accent.fg(&tui_bold("rpi")),
            c.muted.fg(SEPARATOR)
        )
    } else {
        c.accent.fg(&tui_bold("rpi"))
    };

    vec![
        format!("      {bar_accent}"),
        format!("      {bar_accent}"),
        format!("   {bar} {bar_accent}"),
        format!("   {bar} {bar_accent} {bar}"),
        format!("   {bar} {bar_accent} {bar}   {name}"),
    ]
}

/// The startup lockup. Add one to the welcome header.
pub struct BrandLockup;

impl BrandLockup {
    pub fn new() -> Self {
        Self
    }
}

impl Default for BrandLockup {
    fn default() -> Self {
        Self::new()
    }
}

impl Component for BrandLockup {
    fn render(&self, _width: usize) -> Vec<String> {
        render_lockup(crab_enabled())
    }

    fn invalidate(&self) {
        // Nothing is cached.
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rpi_tui::ansi::{strip_ansi, visible_width};

    fn plain(crab: bool) -> Vec<String> {
        render_lockup(crab).iter().map(|r| strip_ansi(r)).collect()
    }

    #[test]
    fn mark_rows_match_the_site_brand() {
        let rows = plain(true);

        // Three bottom-aligned bars: the tallest in the middle, one row taller
        // than the left bar and two taller than the right one.
        assert_eq!(rows[0], "      ██");
        assert_eq!(rows[1], "      ██");
        assert_eq!(rows[2], "   ██ ██");
        assert_eq!(rows[3], "   ██ ██ ██");
    }

    #[test]
    fn crab_stands_in_for_the_language_name() {
        let rows = plain(true);

        // The crab replaces the word "rust" rather than sitting beside it.
        assert_eq!(rows[4], "   ██ ██ ██   rpi · 🦀");

        // The crab is wider than the word it replaces, and the layout must
        // account for exactly that much: the separator plus the emoji.
        let without = visible_width(&plain(false)[4]);
        assert_eq!(
            visible_width(&rows[4]) - without,
            visible_width(" · ") + visible_width(CRAB)
        );
    }

    #[test]
    fn emoji_width_is_the_one_thing_the_layout_depends_on() {
        // If a dependency bump changed this, the lockup would silently shift.
        assert_eq!(visible_width(CRAB), 2, "crab width assumption changed");
    }

    #[test]
    fn disabling_drops_the_crab_and_its_separator() {
        assert!(crab_enabled_from(None));
        for value in ["1", "true", "yes", "TRUE", "Yes"] {
            assert!(!crab_enabled_from(Some(value)), "{value} should disable");
        }
        for value in ["", "0", "false", "no", "off"] {
            assert!(crab_enabled_from(Some(value)), "{value} should not disable");
        }

        let rows = plain(false);
        assert_eq!(rows.len(), 5);
        assert!(!rows[4].contains(CRAB));
        // No dangling separator once the crab is gone.
        assert!(!rows[4].contains(SEPARATOR), "got {:?}", rows[4]);
        assert!(rows[4].ends_with("rpi"), "got {:?}", rows[4]);
    }
}