panasyn 0.1.0

A lightweight GPU-accelerated terminal emulator for macOS and Linux.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/// Core key action — independent of any windowing toolkit.
#[derive(Debug, Clone, PartialEq)]
pub enum KeyAction {
    Char(char),
    Ctrl(u8),
    AltChar(char),
    Enter,
    Backspace,
    Delete,
    Insert,
    Tab,
    BackTab,
    Escape,
    ArrowUp,
    ArrowDown,
    ArrowLeft,
    ArrowRight,
    Home,
    End,
    PageUp,
    PageDown,
    Function(u8),
    None,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackspaceMode {
    Delete,
    CtrlH,
}

impl BackspaceMode {
    pub fn from_config(value: &str) -> Self {
        if value.eq_ignore_ascii_case("ctrl_h") || value.eq_ignore_ascii_case("ctrl-h") {
            Self::CtrlH
        } else {
            Self::Delete
        }
    }
}

/// Convert a key action into PTY byte sequence.
///
/// `app_cursor` — if true, arrow keys use ESC O A/B/C/D instead of ESC [ A/B/C/D.
pub fn action_to_bytes(
    action: &KeyAction,
    app_cursor: bool,
    backspace: BackspaceMode,
) -> Option<Vec<u8>> {
    match action {
        KeyAction::Char(c) => {
            let mut buf = [0u8; 4];
            let s = c.encode_utf8(&mut buf);
            Some(s.as_bytes().to_vec())
        }
        KeyAction::Ctrl(code) => Some(vec![*code]),
        KeyAction::AltChar(c) => {
            let mut buf = [0u8; 4];
            let s = c.encode_utf8(&mut buf);
            let mut result = vec![0x1b];
            result.extend_from_slice(s.as_bytes());
            Some(result)
        }
        KeyAction::Enter => Some(vec![b'\r']),
        KeyAction::Backspace => Some(match backspace {
            BackspaceMode::Delete => vec![0x7f],
            BackspaceMode::CtrlH => vec![0x08],
        }),
        KeyAction::Delete => Some(b"\x1b[3~".to_vec()),
        KeyAction::Insert => Some(b"\x1b[2~".to_vec()),
        KeyAction::Tab => Some(vec![b'\t']),
        KeyAction::BackTab => Some(b"\x1b[Z".to_vec()),
        KeyAction::Escape => Some(vec![0x1b]),
        KeyAction::ArrowUp => Some(if app_cursor {
            b"\x1bOA".to_vec()
        } else {
            b"\x1b[A".to_vec()
        }),
        KeyAction::ArrowDown => Some(if app_cursor {
            b"\x1bOB".to_vec()
        } else {
            b"\x1b[B".to_vec()
        }),
        KeyAction::ArrowLeft => Some(if app_cursor {
            b"\x1bOD".to_vec()
        } else {
            b"\x1b[D".to_vec()
        }),
        KeyAction::ArrowRight => Some(if app_cursor {
            b"\x1bOC".to_vec()
        } else {
            b"\x1b[C".to_vec()
        }),
        KeyAction::Home => Some(b"\x1b[H".to_vec()),
        KeyAction::End => Some(b"\x1b[F".to_vec()),
        KeyAction::PageUp => Some(b"\x1b[5~".to_vec()),
        KeyAction::PageDown => Some(b"\x1b[6~".to_vec()),
        KeyAction::Function(n) => function_key_bytes(*n),
        KeyAction::None => None,
    }
}

fn function_key_bytes(n: u8) -> Option<Vec<u8>> {
    Some(match n {
        1 => b"\x1bOP".to_vec(),
        2 => b"\x1bOQ".to_vec(),
        3 => b"\x1bOR".to_vec(),
        4 => b"\x1bOS".to_vec(),
        5 => b"\x1b[15~".to_vec(),
        6 => b"\x1b[17~".to_vec(),
        7 => b"\x1b[18~".to_vec(),
        8 => b"\x1b[19~".to_vec(),
        9 => b"\x1b[20~".to_vec(),
        10 => b"\x1b[21~".to_vec(),
        11 => b"\x1b[23~".to_vec(),
        12 => b"\x1b[24~".to_vec(),
        _ => return None,
    })
}

// ---------------------------------------------------------------------------
// Winit glue
// ---------------------------------------------------------------------------

use winit::event::{ElementState, KeyEvent};
use winit::keyboard::{Key, KeyCode, ModifiersState, PhysicalKey};

/// Translate a winit `KeyEvent` + `ModifiersState` into a `KeyAction`.
///
/// Special physical keys (arrows, Enter, Backspace, etc.) are checked **first**
/// so they always produce the correct action regardless of `event.text`.
pub fn winit_to_action(event: &KeyEvent, mods: &ModifiersState, option_as_meta: bool) -> KeyAction {
    if event.state != ElementState::Pressed {
        return KeyAction::None;
    }

    let ctrl = mods.control_key();
    let alt = mods.alt_key();

    // Cmd+key → system-level shortcuts (copy, paste, etc.)
    // Handled at the App level before reaching here
    if mods.super_key() {
        return KeyAction::None;
    }

    // --- Physical key lookup (highest priority) ---
    match event.physical_key {
        PhysicalKey::Code(KeyCode::Enter) => return KeyAction::Enter,
        PhysicalKey::Code(KeyCode::Backspace) => return KeyAction::Backspace,
        PhysicalKey::Code(KeyCode::Delete) => return KeyAction::Delete,
        PhysicalKey::Code(KeyCode::Insert) => return KeyAction::Insert,
        PhysicalKey::Code(KeyCode::Tab) if mods.shift_key() => return KeyAction::BackTab,
        PhysicalKey::Code(KeyCode::Tab) => return KeyAction::Tab,
        PhysicalKey::Code(KeyCode::Escape) => return KeyAction::Escape,
        PhysicalKey::Code(KeyCode::ArrowUp) => return KeyAction::ArrowUp,
        PhysicalKey::Code(KeyCode::ArrowDown) => return KeyAction::ArrowDown,
        PhysicalKey::Code(KeyCode::ArrowLeft) => return KeyAction::ArrowLeft,
        PhysicalKey::Code(KeyCode::ArrowRight) => return KeyAction::ArrowRight,
        PhysicalKey::Code(KeyCode::Home) => return KeyAction::Home,
        PhysicalKey::Code(KeyCode::End) => return KeyAction::End,
        PhysicalKey::Code(KeyCode::PageUp) => return KeyAction::PageUp,
        PhysicalKey::Code(KeyCode::PageDown) => return KeyAction::PageDown,
        PhysicalKey::Code(KeyCode::F1) => return KeyAction::Function(1),
        PhysicalKey::Code(KeyCode::F2) => return KeyAction::Function(2),
        PhysicalKey::Code(KeyCode::F3) => return KeyAction::Function(3),
        PhysicalKey::Code(KeyCode::F4) => return KeyAction::Function(4),
        PhysicalKey::Code(KeyCode::F5) => return KeyAction::Function(5),
        PhysicalKey::Code(KeyCode::F6) => return KeyAction::Function(6),
        PhysicalKey::Code(KeyCode::F7) => return KeyAction::Function(7),
        PhysicalKey::Code(KeyCode::F8) => return KeyAction::Function(8),
        PhysicalKey::Code(KeyCode::F9) => return KeyAction::Function(9),
        PhysicalKey::Code(KeyCode::F10) => return KeyAction::Function(10),
        PhysicalKey::Code(KeyCode::F11) => return KeyAction::Function(11),
        PhysicalKey::Code(KeyCode::F12) => return KeyAction::Function(12),
        _ => {}
    }

    if ctrl
        && !alt
        && matches!(
            &event.logical_key,
            Key::Named(winit::keyboard::NamedKey::Space)
        )
    {
        return KeyAction::Ctrl(0x00);
    }

    // --- Ctrl+letter → control codes (0x01–0x1A) ---
    if ctrl
        && !alt
        && let Key::Character(ch) = &event.logical_key
        && let Some(byte) = ch.bytes().next()
        && byte.is_ascii_alphabetic()
    {
        let code = byte.to_ascii_lowercase() & 0x1f;
        return KeyAction::Ctrl(code);
    }

    // --- Alt+letter → ESC + letter ---
    if option_as_meta
        && alt
        && !ctrl
        && let Some(text) = &event.text
        && let Some(c) = text.chars().next()
    {
        return KeyAction::AltChar(c);
    }

    // --- Printable text ---
    if !ctrl
        && let Some(text) = &event.text
        && let Some(c) = text.chars().next()
    {
        return KeyAction::Char(c);
    }
    if !ctrl
        && let Key::Character(text) = &event.logical_key
        && let Some(c) = text.chars().next()
    {
        return KeyAction::Char(c);
    }
    if !ctrl
        && !alt
        && let PhysicalKey::Code(code) = event.physical_key
        && let Some(c) = physical_ascii_fallback(code, mods.shift_key())
    {
        return KeyAction::Char(c);
    }

    KeyAction::None
}

fn physical_ascii_fallback(code: KeyCode, shift: bool) -> Option<char> {
    let c = match code {
        KeyCode::Space => ' ',
        KeyCode::KeyA => 'a',
        KeyCode::KeyB => 'b',
        KeyCode::KeyC => 'c',
        KeyCode::KeyD => 'd',
        KeyCode::KeyE => 'e',
        KeyCode::KeyF => 'f',
        KeyCode::KeyG => 'g',
        KeyCode::KeyH => 'h',
        KeyCode::KeyI => 'i',
        KeyCode::KeyJ => 'j',
        KeyCode::KeyK => 'k',
        KeyCode::KeyL => 'l',
        KeyCode::KeyM => 'm',
        KeyCode::KeyN => 'n',
        KeyCode::KeyO => 'o',
        KeyCode::KeyP => 'p',
        KeyCode::KeyQ => 'q',
        KeyCode::KeyR => 'r',
        KeyCode::KeyS => 's',
        KeyCode::KeyT => 't',
        KeyCode::KeyU => 'u',
        KeyCode::KeyV => 'v',
        KeyCode::KeyW => 'w',
        KeyCode::KeyX => 'x',
        KeyCode::KeyY => 'y',
        KeyCode::KeyZ => 'z',
        KeyCode::Digit0 => {
            return Some(if shift { ')' } else { '0' });
        }
        KeyCode::Digit1 => {
            return Some(if shift { '!' } else { '1' });
        }
        KeyCode::Digit2 => {
            return Some(if shift { '@' } else { '2' });
        }
        KeyCode::Digit3 => {
            return Some(if shift { '#' } else { '3' });
        }
        KeyCode::Digit4 => {
            return Some(if shift { '$' } else { '4' });
        }
        KeyCode::Digit5 => {
            return Some(if shift { '%' } else { '5' });
        }
        KeyCode::Digit6 => {
            return Some(if shift { '^' } else { '6' });
        }
        KeyCode::Digit7 => {
            return Some(if shift { '&' } else { '7' });
        }
        KeyCode::Digit8 => {
            return Some(if shift { '*' } else { '8' });
        }
        KeyCode::Digit9 => {
            return Some(if shift { '(' } else { '9' });
        }
        KeyCode::Minus => {
            return Some(if shift { '_' } else { '-' });
        }
        KeyCode::Equal => {
            return Some(if shift { '+' } else { '=' });
        }
        KeyCode::BracketLeft => {
            return Some(if shift { '{' } else { '[' });
        }
        KeyCode::BracketRight => {
            return Some(if shift { '}' } else { ']' });
        }
        KeyCode::Backslash => {
            return Some(if shift { '|' } else { '\\' });
        }
        KeyCode::Semicolon => {
            return Some(if shift { ':' } else { ';' });
        }
        KeyCode::Quote => {
            return Some(if shift { '"' } else { '\'' });
        }
        KeyCode::Backquote => {
            return Some(if shift { '~' } else { '`' });
        }
        KeyCode::Comma => {
            return Some(if shift { '<' } else { ',' });
        }
        KeyCode::Period => {
            return Some(if shift { '>' } else { '.' });
        }
        KeyCode::Slash => {
            return Some(if shift { '?' } else { '/' });
        }
        _ => return None,
    };
    Some(if shift { c.to_ascii_uppercase() } else { c })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_enter() {
        assert_eq!(
            action_to_bytes(&KeyAction::Enter, false, BackspaceMode::Delete),
            Some(vec![b'\r'])
        );
    }

    #[test]
    fn test_backspace() {
        assert_eq!(
            action_to_bytes(&KeyAction::Backspace, false, BackspaceMode::Delete),
            Some(vec![0x7f])
        );
        assert_eq!(
            action_to_bytes(&KeyAction::Backspace, false, BackspaceMode::CtrlH),
            Some(vec![0x08])
        );
    }

    #[test]
    fn test_delete() {
        assert_eq!(
            action_to_bytes(&KeyAction::Delete, false, BackspaceMode::Delete),
            Some(b"\x1b[3~".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::Insert, false, BackspaceMode::Delete),
            Some(b"\x1b[2~".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::BackTab, false, BackspaceMode::Delete),
            Some(b"\x1b[Z".to_vec())
        );
    }

    #[test]
    fn test_arrows_normal() {
        assert_eq!(
            action_to_bytes(&KeyAction::ArrowUp, false, BackspaceMode::Delete),
            Some(b"\x1b[A".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::ArrowDown, false, BackspaceMode::Delete),
            Some(b"\x1b[B".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::ArrowLeft, false, BackspaceMode::Delete),
            Some(b"\x1b[D".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::ArrowRight, false, BackspaceMode::Delete),
            Some(b"\x1b[C".to_vec())
        );
    }

    #[test]
    fn test_arrows_app_cursor() {
        assert_eq!(
            action_to_bytes(&KeyAction::ArrowUp, true, BackspaceMode::Delete),
            Some(b"\x1bOA".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::ArrowDown, true, BackspaceMode::Delete),
            Some(b"\x1bOB".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::ArrowLeft, true, BackspaceMode::Delete),
            Some(b"\x1bOD".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::ArrowRight, true, BackspaceMode::Delete),
            Some(b"\x1bOC".to_vec())
        );
    }

    #[test]
    fn test_control_codes() {
        for byte in 0x01..=0x1a {
            assert_eq!(
                action_to_bytes(&KeyAction::Ctrl(byte), false, BackspaceMode::Delete),
                Some(vec![byte])
            );
        }
    }

    #[test]
    fn test_alt_char() {
        assert_eq!(
            action_to_bytes(&KeyAction::AltChar('a'), false, BackspaceMode::Delete),
            Some(vec![0x1b, b'a'])
        );
        assert_eq!(
            action_to_bytes(&KeyAction::AltChar('Z'), false, BackspaceMode::Delete),
            Some(vec![0x1b, b'Z'])
        );
    }

    #[test]
    fn test_function_keys() {
        assert_eq!(
            action_to_bytes(&KeyAction::Function(1), false, BackspaceMode::Delete),
            Some(b"\x1bOP".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::Function(12), false, BackspaceMode::Delete),
            Some(b"\x1b[24~".to_vec())
        );
        assert_eq!(
            action_to_bytes(&KeyAction::Function(13), false, BackspaceMode::Delete),
            None
        );
    }

    #[test]
    fn test_app_cursor_non_arrows() {
        // Non-arrow keys should be the same regardless of app_cursor
        assert_eq!(
            action_to_bytes(&KeyAction::Enter, false, BackspaceMode::Delete),
            action_to_bytes(&KeyAction::Enter, true, BackspaceMode::Delete)
        );
        assert_eq!(
            action_to_bytes(&KeyAction::Home, false, BackspaceMode::Delete),
            action_to_bytes(&KeyAction::Home, true, BackspaceMode::Delete)
        );
    }
}