rustimal 0.6.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
//! Translate keyboard events into the byte sequences a shell expects (xterm style).

use alacritty_terminal::term::TermMode;
use winit::event::KeyEvent;
use winit::keyboard::{Key, ModifiersState, NamedKey};

pub fn encode_key(event: &KeyEvent, mods: ModifiersState, mode: TermMode) -> Option<Vec<u8>> {
    let ctrl = mods.control_key();
    let alt = mods.alt_key();
    let shift = mods.shift_key();
    let modifier_param = 1 + shift as u8 + 2 * alt as u8 + 4 * ctrl as u8;

    if let Key::Named(named) = &event.logical_key {
        let csi_mod = |base: &str, suffix: char| -> Vec<u8> {
            if modifier_param > 1 {
                format!("\x1b[{base};{modifier_param}{suffix}").into_bytes()
            } else {
                format!("\x1b[{base}{suffix}").into_bytes()
            }
        };
        let cursor = |c: char| -> Vec<u8> {
            if modifier_param > 1 {
                format!("\x1b[1;{modifier_param}{c}").into_bytes()
            } else if mode.contains(TermMode::APP_CURSOR) {
                format!("\x1bO{c}").into_bytes()
            } else {
                format!("\x1b[{c}").into_bytes()
            }
        };
        let fkey_ss3 = |c: char| -> Vec<u8> {
            if modifier_param > 1 {
                format!("\x1b[1;{modifier_param}{c}").into_bytes()
            } else {
                format!("\x1bO{c}").into_bytes()
            }
        };
        let with_alt = |b: &[u8]| -> Vec<u8> {
            let mut v = Vec::with_capacity(b.len() + 1);
            if alt {
                v.push(0x1b);
            }
            v.extend_from_slice(b);
            v
        };
        let bytes = match named {
            // Shift+Enter inserts a newline in Claude Code (same as Alt+Enter).
            NamedKey::Enter if shift && !ctrl => b"\x1b\r".to_vec(),
            NamedKey::Enter => with_alt(b"\r"),
            NamedKey::Tab if shift => b"\x1b[Z".to_vec(),
            NamedKey::Tab => with_alt(b"\t"),
            NamedKey::Backspace if ctrl => with_alt(b"\x08"),
            NamedKey::Backspace => with_alt(b"\x7f"),
            NamedKey::Escape => with_alt(b"\x1b"),
            NamedKey::Space if ctrl => vec![0],
            NamedKey::Space => with_alt(b" "),
            NamedKey::ArrowUp => cursor('A'),
            NamedKey::ArrowDown => cursor('B'),
            NamedKey::ArrowRight => cursor('C'),
            NamedKey::ArrowLeft => cursor('D'),
            NamedKey::Home => cursor('H'),
            NamedKey::End => cursor('F'),
            NamedKey::Insert => csi_mod("2", '~'),
            NamedKey::Delete => csi_mod("3", '~'),
            NamedKey::PageUp => csi_mod("5", '~'),
            NamedKey::PageDown => csi_mod("6", '~'),
            NamedKey::F1 => fkey_ss3('P'),
            NamedKey::F2 => fkey_ss3('Q'),
            NamedKey::F3 => fkey_ss3('R'),
            NamedKey::F4 => fkey_ss3('S'),
            NamedKey::F5 => csi_mod("15", '~'),
            NamedKey::F6 => csi_mod("17", '~'),
            NamedKey::F7 => csi_mod("18", '~'),
            NamedKey::F8 => csi_mod("19", '~'),
            NamedKey::F9 => csi_mod("20", '~'),
            NamedKey::F10 => csi_mod("21", '~'),
            NamedKey::F11 => csi_mod("23", '~'),
            NamedKey::F12 => csi_mod("24", '~'),
            _ => return event.text.as_ref().map(|t| t.as_bytes().to_vec()),
        };
        return Some(bytes);
    }

    let text = event.text.as_deref().unwrap_or("");
    let printable = !text.is_empty() && !text.chars().any(|c| c.is_control());

    // AltGr arrives as Ctrl+Alt on Windows; trust the composed text.
    if ctrl && alt && printable {
        return Some(text.as_bytes().to_vec());
    }

    if ctrl
        && let Key::Character(s) = &event.logical_key {
            let c = s.chars().next()?.to_ascii_lowercase();
            let code = match c {
                'a'..='z' => c as u8 - b'a' + 1,
                '@' | '2' | ' ' => 0,
                '[' | '3' => 0x1b,
                '\\' | '4' => 0x1c,
                ']' | '5' => 0x1d,
                '^' | '6' => 0x1e,
                '_' | '-' | '7' | '/' => 0x1f,
                '?' | '8' => 0x7f,
                _ => return printable.then(|| text.as_bytes().to_vec()),
            };
            let mut v = Vec::with_capacity(2);
            if alt {
                v.push(0x1b);
            }
            v.push(code);
            return Some(v);
        }

    if alt {
        let s = if printable {
            text.to_string()
        } else if let Key::Character(s) = &event.logical_key {
            s.to_string()
        } else {
            return None;
        };
        let mut v = vec![0x1b];
        v.extend_from_slice(s.as_bytes());
        return Some(v);
    }

    (!text.is_empty()).then(|| text.as_bytes().to_vec())
}