rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Collect a single-line secret without echoing it into a host terminal
//! log: `rho login`'s API key and `rho mcp secret set`'s value.

use std::io::{self, IsTerminal, Read, Write};

use crossterm::event::{Event, KeyCode, KeyEventKind, KeyModifiers};

use crate::tui::InlinePromptInput;

/// Reads `label` hidden from the terminal; refuses without one.
pub(super) async fn read(label: &str) -> anyhow::Result<String> {
    if !crate::stdio::stdin_is_terminal() || !io::stderr().is_terminal() {
        anyhow::bail!("entering {label} requires an interactive terminal");
    }
    let secret = read_hidden(label).await;
    // Raw mode has ended, so this newline also returns the cursor.
    let _ = writeln!(io::stderr());
    secret
}

/// Reads `label` hidden from the terminal, or, when standard input is not
/// one, all of standard input with surrounding whitespace removed, for a
/// script that pipes the value in.
pub(super) async fn read_or_stdin(label: &str) -> anyhow::Result<String> {
    if crate::stdio::stdin_is_terminal() {
        return read(label).await;
    }
    let text = tokio::task::spawn_blocking(|| {
        let mut text = String::new();
        io::stdin().read_to_string(&mut text).map(|_| text)
    })
    .await??;
    let text = text.trim();
    if text.is_empty() {
        anyhow::bail!("{label} cannot be empty");
    }
    if text.chars().any(char::is_control) {
        anyhow::bail!("{label} must be a single line");
    }
    Ok(text.to_owned())
}

async fn read_hidden(label: &str) -> anyhow::Result<String> {
    let mut input = InlinePromptInput::acquire()?;
    write!(
        io::stderr(),
        "enter {label} (input hidden; Enter save, Esc cancel): "
    )?;
    io::stderr().flush()?;

    let mut secret = String::new();
    loop {
        match input.next().await? {
            Event::Key(key) if key.kind != KeyEventKind::Release => match key.code {
                KeyCode::Esc => anyhow::bail!("cancelled"),
                KeyCode::Char('c' | 'C' | 'd' | 'D')
                    if key.modifiers.contains(KeyModifiers::CONTROL) =>
                {
                    anyhow::bail!("cancelled");
                }
                KeyCode::Enter => {
                    let secret = secret.trim();
                    if secret.is_empty() {
                        anyhow::bail!("{label} cannot be empty");
                    }
                    return Ok(secret.to_owned());
                }
                KeyCode::Backspace => {
                    secret.pop();
                }
                KeyCode::Char(ch)
                    if !ch.is_control()
                        && !key
                            .modifiers
                            .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) =>
                {
                    secret.push(ch);
                }
                _ => {}
            },
            Event::Paste(text) => {
                let text = text.trim();
                if text.chars().any(char::is_control) {
                    anyhow::bail!("{label} must be a single line");
                }
                secret.push_str(text);
            }
            _ => {}
        }
    }
}