marver 0.0.28

A TUI workspace for AI agent sessions: tmux orchestration, git worktree management, and repo control in one place.
Documentation
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
//! Translating key events into the bytes a terminal would send.

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

/// xterm's modifier parameter: 1 plus a bitmask.
fn modifier_param(mods: KeyModifiers) -> u8 {
    let mut value = 1;
    if mods.contains(KeyModifiers::SHIFT) {
        value += 1;
    }
    if mods.contains(KeyModifiers::ALT) {
        value += 2;
    }
    if mods.contains(KeyModifiers::CONTROL) {
        value += 4;
    }
    value
}

fn is_plain(mods: KeyModifiers) -> bool {
    modifier_param(mods) == 1
}

/// A cursor or edit key: `ESC [ <param> <final>`, or `ESC [ 1 ; <mod> <final>`
/// when modified.
fn csi(final_byte: u8, mods: KeyModifiers) -> Vec<u8> {
    if is_plain(mods) {
        vec![0x1b, b'[', final_byte]
    } else {
        format!("\x1b[1;{}{}", modifier_param(mods), final_byte as char).into_bytes()
    }
}

/// A tilde-terminated key: `ESC [ <n> ~`, or `ESC [ <n> ; <mod> ~`.
fn tilde(number: u8, mods: KeyModifiers) -> Vec<u8> {
    if is_plain(mods) {
        format!("\x1b[{number}~").into_bytes()
    } else {
        format!("\x1b[{};{}~", number, modifier_param(mods)).into_bytes()
    }
}

/// The control byte a `Ctrl`-modified character produces, if any.
fn control_byte(c: char) -> Option<u8> {
    match c {
        'a'..='z' => Some(c as u8 - b'a' + 1),
        'A'..='Z' => Some(c as u8 - b'A' + 1),
        ' ' | '@' => Some(0x00),
        '[' => Some(0x1b),
        '\\' => Some(0x1c),
        ']' => Some(0x1d),
        '^' => Some(0x1e),
        '_' | '/' => Some(0x1f),
        // What the terminal actually sends for ctrl+\ ] ^ / in legacy
        // encoding.
        '4'..='7' => Some(0x1c + (c as u8 - b'4')),
        // ^?
        '?' => Some(0x7f),
        _ => None,
    }
}

/// A single byte, prefixed with ESC when Alt is held.
fn alt_prefixed(byte: u8, mods: KeyModifiers) -> Vec<u8> {
    if mods.contains(KeyModifiers::ALT) {
        vec![0x1b, byte]
    } else {
        vec![byte]
    }
}

/// Encode a key event as the bytes to write to a pane.
pub fn encode(event: KeyEvent) -> Option<Vec<u8>> {
    use ratatui::crossterm::event::KeyEventKind;
    if event.kind == KeyEventKind::Release {
        return None;
    }

    let mods = event.modifiers;
    let bytes = match event.code {
        KeyCode::Char(c) => {
            if mods.contains(KeyModifiers::CONTROL) {
                let byte = control_byte(c)?;
                if mods.contains(KeyModifiers::ALT) {
                    vec![0x1b, byte]
                } else {
                    vec![byte]
                }
            } else if mods.contains(KeyModifiers::ALT) {
                let mut out = vec![0x1b];
                out.extend_from_slice(c.to_string().as_bytes());
                out
            } else {
                c.to_string().into_bytes()
            }
        }

        // Carriage return, not newline: that is what a terminal sends, and
        // line-discipline turns it into a newline on the far side.
        KeyCode::Enter => {
            if mods.intersects(KeyModifiers::SHIFT | KeyModifiers::CONTROL) {
                format!("\x1b[13;{}u", modifier_param(mods)).into_bytes()
            } else {
                alt_prefixed(b'\r', mods)
            }
        }
        KeyCode::Tab => alt_prefixed(b'\t', mods),
        KeyCode::BackTab => vec![0x1b, b'[', b'Z'],
        // DEL, not BS.
        KeyCode::Backspace if mods.contains(KeyModifiers::CONTROL) => alt_prefixed(0x08, mods),
        KeyCode::Backspace => alt_prefixed(0x7f, mods),
        KeyCode::Esc => alt_prefixed(0x1b, mods),

        KeyCode::Up => csi(b'A', mods),
        KeyCode::Down => csi(b'B', mods),
        KeyCode::Right => csi(b'C', mods),
        KeyCode::Left => csi(b'D', mods),
        KeyCode::End => csi(b'F', mods),
        KeyCode::Home => csi(b'H', mods),

        KeyCode::Insert => tilde(2, mods),
        KeyCode::Delete => tilde(3, mods),
        KeyCode::PageUp => tilde(5, mods),
        KeyCode::PageDown => tilde(6, mods),

        // F1..F4 are SS3-encoded when unmodified, CSI otherwise.
        KeyCode::F(n @ 1..=4) => {
            let final_byte = b'P' + (n - 1);
            if is_plain(mods) {
                vec![0x1b, b'O', final_byte]
            } else {
                format!("\x1b[1;{}{}", modifier_param(mods), final_byte as char).into_bytes()
            }
        }
        KeyCode::F(n) => {
            let number = match n {
                5 => 15,
                6..=10 => 17 + (n - 6),
                11 => 23,
                12 => 24,
                _ => return None,
            };
            tilde(number, mods)
        }

        KeyCode::Null => vec![0x00],
        // Modifier presses, media keys, and everything else produce no input.
        _ => return None,
    };

    Some(bytes)
}

/// Render bytes as the space-separated hex `tmux send-keys -H` expects.
pub fn to_hex(bytes: &[u8]) -> String {
    bytes
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect::<Vec<_>>()
        .join(" ")
}

/// The full `send-keys` command for a key event, ready for control mode.
pub fn send_keys_command(pane: &str, event: KeyEvent) -> Option<String> {
    let bytes = encode(event)?;
    if bytes.is_empty() {
        return None;
    }
    Some(format!("send-keys -t {} -H {}", pane, to_hex(&bytes)))
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::crossterm::event::{KeyEventKind, KeyEventState};

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn with(code: KeyCode, mods: KeyModifiers) -> KeyEvent {
        KeyEvent::new(code, mods)
    }

    fn enc(code: KeyCode) -> Vec<u8> {
        encode(key(code)).expect("should encode")
    }

    fn enc_mod(code: KeyCode, mods: KeyModifiers) -> Vec<u8> {
        encode(with(code, mods)).expect("should encode")
    }

    #[test]
    fn plain_characters_are_their_utf8() {
        assert_eq!(enc(KeyCode::Char('a')), b"a");
        assert_eq!(enc(KeyCode::Char('Z')), b"Z");
        assert_eq!(enc(KeyCode::Char('£')), "£".as_bytes());
        assert_eq!(enc(KeyCode::Char('🦀')), "🦀".as_bytes());
    }

    #[test]
    fn enter_sends_carriage_return_not_newline() {
        assert_eq!(enc(KeyCode::Enter), b"\r");
    }

    #[test]
    fn backspace_sends_del_not_backspace() {
        // 0x08 would make most shells beep rather than erase.
        assert_eq!(enc(KeyCode::Backspace), &[0x7f]);
    }

    #[test]
    fn control_letters_map_to_control_bytes() {
        let ctrl = KeyModifiers::CONTROL;
        assert_eq!(enc_mod(KeyCode::Char('a'), ctrl), &[0x01]);
        assert_eq!(enc_mod(KeyCode::Char('c'), ctrl), &[0x03]);
        assert_eq!(enc_mod(KeyCode::Char('d'), ctrl), &[0x04]);
        assert_eq!(enc_mod(KeyCode::Char('z'), ctrl), &[0x1a]);
        // Case must not change the byte.
        assert_eq!(enc_mod(KeyCode::Char('C'), ctrl), &[0x03]);
    }

    #[test]
    fn control_punctuation_is_handled() {
        let ctrl = KeyModifiers::CONTROL;
        assert_eq!(enc_mod(KeyCode::Char(' '), ctrl), &[0x00]);
        assert_eq!(
            enc_mod(KeyCode::Char('['), ctrl),
            &[0x1b],
            "Ctrl-[ is Escape"
        );
        assert_eq!(enc_mod(KeyCode::Char('\\'), ctrl), &[0x1c]);
        assert_eq!(enc_mod(KeyCode::Char(']'), ctrl), &[0x1d]);
        assert_eq!(enc_mod(KeyCode::Char('_'), ctrl), &[0x1f]);
    }

    #[test]
    fn control_on_a_key_with_no_control_byte_produces_nothing() {
        assert_eq!(
            encode(with(KeyCode::Char('1'), KeyModifiers::CONTROL)),
            None
        );
    }

    #[test]
    fn the_control_bytes_a_legacy_terminal_actually_sends_are_encoded() {
        // marver does not push the keyboard-enhancement flags, so a terminal
        // cannot report "ctrl and a bracket" — it sends the control byte, and
        // crossterm hands back what that byte looks like.
        let ctrl = KeyModifiers::CONTROL;
        assert_eq!(enc_mod(KeyCode::Char('4'), ctrl), &[0x1c], "ctrl+\\");
        assert_eq!(enc_mod(KeyCode::Char('5'), ctrl), &[0x1d], "ctrl+]");
        assert_eq!(enc_mod(KeyCode::Char('6'), ctrl), &[0x1e], "ctrl+^");
        assert_eq!(enc_mod(KeyCode::Char('7'), ctrl), &[0x1f], "ctrl+/");
        assert_eq!(enc_mod(KeyCode::Char('/'), ctrl), &[0x1f]);
        assert_eq!(enc_mod(KeyCode::Char('?'), ctrl), &[0x7f], "ctrl+? is DEL");
    }

    #[test]
    fn alt_survives_on_the_named_keys() {
        // These arrive from the terminal as ESC + the byte; dropping the ESC
        // turns delete-previous-word into delete-one-character.
        let alt = KeyModifiers::ALT;
        assert_eq!(enc_mod(KeyCode::Backspace, alt), &[0x1b, 0x7f]);
        assert_eq!(enc_mod(KeyCode::Enter, alt), &[0x1b, b'\r']);
        assert_eq!(enc_mod(KeyCode::Tab, alt), &[0x1b, b'\t']);
        assert_eq!(enc_mod(KeyCode::Esc, alt), &[0x1b, 0x1b]);
    }

    #[test]
    fn shift_enter_is_a_new_line_not_a_submit() {
        // A terminal set up for Claude Code sends CSI-u for shift+enter.
        assert_eq!(enc_mod(KeyCode::Enter, KeyModifiers::SHIFT), b"\x1b[13;2u");
        assert_ne!(
            enc_mod(KeyCode::Enter, KeyModifiers::SHIFT),
            enc(KeyCode::Enter),
            "shift+enter must not be indistinguishable from enter"
        );
        assert_ne!(
            enc_mod(KeyCode::Enter, KeyModifiers::CONTROL),
            enc(KeyCode::Enter)
        );
    }

    #[test]
    fn ctrl_backspace_is_distinct_from_backspace() {
        assert_ne!(
            enc_mod(KeyCode::Backspace, KeyModifiers::CONTROL),
            enc(KeyCode::Backspace),
            "delete-word must not be delete-character"
        );
    }

    #[test]
    fn alt_prefixes_escape() {
        assert_eq!(enc_mod(KeyCode::Char('b'), KeyModifiers::ALT), b"\x1bb");
        assert_eq!(
            enc_mod(
                KeyCode::Char('c'),
                KeyModifiers::ALT | KeyModifiers::CONTROL
            ),
            &[0x1b, 0x03],
            "alt-ctrl-c is escape then the control byte"
        );
    }

    #[test]
    fn arrows_are_csi_sequences() {
        assert_eq!(enc(KeyCode::Up), b"\x1b[A");
        assert_eq!(enc(KeyCode::Down), b"\x1b[B");
        assert_eq!(enc(KeyCode::Right), b"\x1b[C");
        assert_eq!(enc(KeyCode::Left), b"\x1b[D");
        assert_eq!(enc(KeyCode::Home), b"\x1b[H");
        assert_eq!(enc(KeyCode::End), b"\x1b[F");
    }

    #[test]
    fn modified_arrows_carry_a_modifier_parameter() {
        assert_eq!(enc_mod(KeyCode::Up, KeyModifiers::SHIFT), b"\x1b[1;2A");
        assert_eq!(enc_mod(KeyCode::Up, KeyModifiers::ALT), b"\x1b[1;3A");
        assert_eq!(enc_mod(KeyCode::Up, KeyModifiers::CONTROL), b"\x1b[1;5A");
        assert_eq!(
            enc_mod(KeyCode::Left, KeyModifiers::CONTROL | KeyModifiers::SHIFT),
            b"\x1b[1;6D"
        );
        assert_eq!(
            enc_mod(
                KeyCode::Right,
                KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SHIFT
            ),
            b"\x1b[1;8C"
        );
    }

    #[test]
    fn edit_keys_are_tilde_sequences() {
        assert_eq!(enc(KeyCode::Insert), b"\x1b[2~");
        assert_eq!(enc(KeyCode::Delete), b"\x1b[3~");
        assert_eq!(enc(KeyCode::PageUp), b"\x1b[5~");
        assert_eq!(enc(KeyCode::PageDown), b"\x1b[6~");
        assert_eq!(
            enc_mod(KeyCode::Delete, KeyModifiers::CONTROL),
            b"\x1b[3;5~"
        );
    }

    #[test]
    fn function_keys_split_between_ss3_and_csi() {
        assert_eq!(enc(KeyCode::F(1)), b"\x1bOP");
        assert_eq!(enc(KeyCode::F(4)), b"\x1bOS");
        assert_eq!(enc(KeyCode::F(5)), b"\x1b[15~");
        assert_eq!(enc(KeyCode::F(6)), b"\x1b[17~");
        assert_eq!(enc(KeyCode::F(10)), b"\x1b[21~");
        assert_eq!(enc(KeyCode::F(12)), b"\x1b[24~");
        // F1..F4 switch to CSI form once modified.
        assert_eq!(enc_mod(KeyCode::F(1), KeyModifiers::SHIFT), b"\x1b[1;2P");
    }

    #[test]
    fn there_is_no_f13() {
        assert_eq!(encode(key(KeyCode::F(13))), None);
    }

    #[test]
    fn tab_and_backtab_differ() {
        assert_eq!(enc(KeyCode::Tab), b"\t");
        assert_eq!(enc(KeyCode::BackTab), b"\x1b[Z");
    }

    #[test]
    fn key_releases_produce_nothing() {
        // Without this every keystroke would be delivered twice on terminals
        // that report release events.
        let release = KeyEvent::new_with_kind_and_state(
            KeyCode::Char('a'),
            KeyModifiers::NONE,
            KeyEventKind::Release,
            KeyEventState::NONE,
        );
        assert_eq!(encode(release), None);

        let repeat = KeyEvent::new_with_kind_and_state(
            KeyCode::Char('a'),
            KeyModifiers::NONE,
            KeyEventKind::Repeat,
            KeyEventState::NONE,
        );
        assert_eq!(encode(repeat), Some(b"a".to_vec()), "repeats do type");
    }

    #[test]
    fn hex_encoding_is_what_tmux_expects() {
        assert_eq!(to_hex(&[0x1b, b'[', b'A']), "1b 5b 41");
        assert_eq!(to_hex(&[0x00, 0x0f]), "00 0f");
    }

    #[test]
    fn builds_a_complete_send_keys_command() {
        let cmd = send_keys_command("%3", key(KeyCode::Up)).unwrap();
        assert_eq!(cmd, "send-keys -t %3 -H 1b 5b 41");
    }

    #[test]
    fn keys_with_no_encoding_yield_no_command() {
        assert_eq!(send_keys_command("%0", key(KeyCode::F(13))), None);
    }
}