Artifact 9083fd4b27457f1bf6916e023698de90c713368166515230af44a57e51b519bb:
- File
src/tui/term.h
— part of check-in
[6b33552ab9]
at
2026-11-20 01:33:41
on branch tui-round-3
— TUI round 3: steering keys, live output, retry countdown, durable !cmd
Enter steers a running reply and Alt+Enter queues a follow-up (Shift+Enter or Ctrl+J for a newline); the queue shows Pi's labels, delivered steering appears where it lands, returned messages go back to the editor, and Esc aborts over the command pipe with signals as the fallback. The running bash block shows its output as it arrives; retries count down and Esc cancels the wait. pic bash CMD (optionally --no-context, -c or a conversation id) runs a command and journals it as Pi's bash execution message, so !cmd survives a restart and reaches the model as Pi does. The footer shows cost when usage carries one. (user: dmestas, size: 3552) [annotate] [blame] [check-ins using] [more...]
/* The terminal: raw mode and key decoding (term.c). */ #ifndef PIC_TUI_TERM_H #define PIC_TUI_TERM_H #include <stddef.h> typedef enum { TUI_KEY_CHAR, /* text: one UTF-8 character */ TUI_KEY_ENTER, /* CR */ TUI_KEY_NEWLINE, /* Ctrl+J (LF), Shift+Enter (CSI 13;2u or CSI 27;2;13~) */ TUI_KEY_TAB, TUI_KEY_BACKSPACE, /* DEL or Ctrl+H */ TUI_KEY_DELETE, /* CSI 3~ */ TUI_KEY_LEFT, TUI_KEY_RIGHT, TUI_KEY_UP, TUI_KEY_DOWN, TUI_KEY_HOME, /* CSI H, CSI 1~, SS3 H, Ctrl+A */ TUI_KEY_END, /* CSI F, CSI 4~, SS3 F, Ctrl+E */ TUI_KEY_PAGEUP, TUI_KEY_PAGEDOWN, TUI_KEY_WORD_LEFT, /* Alt+B, Alt/Ctrl+Left (CSI 1;3D, CSI 1;5D) */ TUI_KEY_WORD_RIGHT, /* Alt+F, Alt/Ctrl+Right */ TUI_KEY_DELETE_WORD, /* Ctrl+W, Alt+Backspace */ TUI_KEY_KILL_LINE, /* Ctrl+K: to the end of the line */ TUI_KEY_KILL_START, /* Ctrl+U: to the start of the line */ TUI_KEY_ESC, /* a lone Escape (see tui_keys_flush) */ TUI_KEY_CTRL_C, TUI_KEY_CTRL_D, TUI_KEY_CTRL_L, TUI_KEY_CTRL_O, TUI_KEY_PASTE, /* bracketed paste: paste/paste_len, CR and CRLF turned into LF */ TUI_KEY_CTRL_S, TUI_KEY_CTRL_P, TUI_KEY_BACKTAB, /* Shift+Tab: CSI Z */ TUI_KEY_CTRL_T, TUI_KEY_DELETE_WORD_FWD, /* Alt+D, Alt+Delete */ TUI_KEY_YANK, /* Ctrl+Y */ TUI_KEY_YANK_POP, /* Alt+Y */ TUI_KEY_UNDO, /* Ctrl+- (0x1F), kitty CSI 45;5u */ TUI_KEY_JUMP, /* Ctrl+]: jump forward to the next typed character */ TUI_KEY_JUMP_BACK, /* Ctrl+Alt+]: backward */ TUI_KEY_CTRL_G, /* external editor */ TUI_KEY_CTRL_X, /* copy the last reply */ TUI_KEY_CTRL_Z, /* suspend */ TUI_KEY_ALT_UP, /* CSI 1;3A: restore queued messages */ TUI_KEY_CTRL_P_BACK, /* Shift+Ctrl+P: CSI 112;6u or CSI 27;6;112~ (needs a terminal that reports modifiers) */ TUI_KEY_ALT_ENTER, /* ESC CR, CSI 13;3u, CSI 27;3;13~: queue a follow-up (Pi's app.message.followUp) */ } tui_key_kind; typedef struct { tui_key_kind kind; char text[8]; /* TUI_KEY_CHAR: the character, NUL-terminated */ char *paste; /* TUI_KEY_PASTE: malloc'd; the caller frees it */ size_t paste_len; } tui_key; /* An incremental decoder: bytes in, keys out. Unknown escape sequences are * consumed whole and dropped; other control bytes are dropped. */ typedef struct tui_keys tui_keys; tui_keys *tui_keys_new(void); void tui_keys_free(tui_keys *k); void tui_keys_feed(tui_keys *k, const char *buf, size_t n); /* 1 with the next decoded key in *out, 0 when none is complete yet. */ int tui_keys_next(tui_keys *k, tui_key *out); /* A lone ESC (or an unfinished sequence) is waiting for more bytes; the * caller waits ~50 ms for more input, then calls tui_keys_flush, which * turns a lone ESC into TUI_KEY_ESC. An unfinished CSI/SS3 or UTF-8 * sequence stays pending (a slow link may deliver the rest later) and is * dropped only after 3 consecutive flushes with no new input. */ int tui_keys_waiting(const tui_keys *k); void tui_keys_flush(tui_keys *k); /* Raw mode on fd (no echo, no canonical input, no signals from keys, no * CR translation) plus bracketed paste; the previous state is saved. * tui_term_restore puts it back and turns bracketed paste off; it is safe * to call more than once and from a signal handler. */ int tui_term_raw(int fd); void tui_term_restore(void); /* Columns and rows of the terminal on fd; 80x24 when unknown. */ void tui_term_size(int fd, int *cols, int *rows); #endif