rho-coding-agent 3.11.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! Key handling shared by masked single-line fields: the login API-key
//! field and a daemon's secret ask. The field edits its own value; each
//! owner decides what Enter and Esc mean.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use super::line_editor::LineEditor;

/// What a key did to a masked field.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum MaskedKey {
    /// Plain Enter: the owner reads the value.
    Submit,
    /// Esc: the owner backs out.
    Cancel,
    /// The key edited the value, moved the caret, or did nothing. The field
    /// keeps the keyboard either way, so no key leaks past it.
    Handled,
}

/// Applies `key` to `editor`. Control and Alt chords never type, so a
/// shortcut cannot slip a letter into the secret.
pub(super) fn apply_masked_key(editor: &mut LineEditor, key: KeyEvent) -> MaskedKey {
    match (key.modifiers, key.code) {
        (_, KeyCode::Esc) => return MaskedKey::Cancel,
        (KeyModifiers::NONE, KeyCode::Enter) => return MaskedKey::Submit,
        (_, KeyCode::Backspace) => editor.backspace(),
        (_, KeyCode::Delete) => editor.delete(),
        (_, KeyCode::Left) => editor.move_cursor_left(),
        (_, KeyCode::Right) => editor.move_cursor_right(),
        (_, KeyCode::Home) => editor.move_cursor_home(),
        (_, KeyCode::End) => editor.move_cursor_end(),
        (modifiers, KeyCode::Char(ch))
            if !modifiers.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) =>
        {
            editor.insert_char(ch);
        }
        _ => {}
    }
    MaskedKey::Handled
}

#[cfg(test)]
#[path = "masked_input_tests.rs"]
mod tests;