magi-code 0.96.1

Repository-aware CLI coding agent for terminal work
Documentation
use super::*;

pub(super) fn is_standalone_ctrl_key(key: KeyEvent) -> bool {
    match key.code {
        KeyCode::Modifier(ModifierKeyCode::LeftControl | ModifierKeyCode::RightControl) => {
            !key.modifiers.intersects(
                KeyModifiers::SHIFT
                    | KeyModifiers::ALT
                    | KeyModifiers::SUPER
                    | KeyModifiers::HYPER
                    | KeyModifiers::META,
            )
        }
        _ => false,
    }
}

pub(super) fn shifted_enter_modifiers(modifiers: KeyModifiers) -> bool {
    modifiers.contains(KeyModifiers::SHIFT)
}

pub(in crate::tui) fn char_autofocuses_prompt(ch: char) -> bool {
    ch.is_alphanumeric() || ch == '/'
}

pub(in crate::tui) fn printable_char_for_text_input(key: KeyEvent) -> Option<char> {
    let KeyCode::Char(ch) = key.code else {
        return None;
    };

    if key.modifiers.is_empty() {
        Some(ch)
    } else if key.modifiers == KeyModifiers::SHIFT {
        Some(shift_printable_char(ch))
    } else {
        None
    }
}

pub(crate) fn text_char_for_key_burst(key: KeyEvent) -> Option<char> {
    if key.kind == KeyEventKind::Release {
        return None;
    }
    if key.modifiers == KeyModifiers::NONE {
        return match key.code {
            KeyCode::Enter => Some('\n'),
            KeyCode::Tab => Some('\t'),
            _ => printable_char_for_text_input(key),
        };
    }
    printable_char_for_text_input(key)
}

fn shift_printable_char(ch: char) -> char {
    if ch.is_ascii_lowercase() {
        return ch.to_ascii_uppercase();
    }

    match ch {
        '`' => '~',
        '1' => '!',
        '2' => '@',
        '3' => '#',
        '4' => '$',
        '5' => '%',
        '6' => '^',
        '7' => '&',
        '8' => '*',
        '9' => '(',
        '0' => ')',
        '-' => '_',
        '=' => '+',
        '[' => '{',
        ']' => '}',
        '\\' => '|',
        ';' => ':',
        '\'' => '"',
        ',' => '<',
        '.' => '>',
        '/' => '?',
        _ => ch,
    }
}