Artifact cc9d0ae58109074f74dbcdbaa761050289fe1ccf1b1f3db99966a441be39bed4:
- File
src/tui/view.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: 5210) [annotate] [blame] [check-ins using] [more...]
/* What the transcript and footer look like (view.c): tool-call blocks, edit * diffs, durations, the footer lines, the retry status. Pure functions * (the git branch reads one file): no terminal, no events, no globals. */ #ifndef PIC_TUI_VIEW_H #define PIC_TUI_VIEW_H #include <stddef.h> #include "width.h" /* Pi's formatToolDuration: "4.2s", "1m 5s", "1h 2m 3s". */ void tui_fmt_duration(long ms, char *out, size_t cap); /* Pi's formatTokens: "999", "1.5k", "15k", "1.5M". */ void tui_fmt_tokens(long long n, char *out, size_t cap); /* "12B", "1.5KB", "2.0MB". */ void tui_fmt_size(size_t bytes, char *out, size_t cap); /* One replacement as unified-diff lines, malloc'd, "\n"-separated, each * starting with ' ' (context, up to 2 lines each side), '-' or '+'. The * common leading and trailing lines are trimmed, so interleaved changes * show as one removed block and one added block. "" when the texts are * equal; NULL when out of memory. */ char *tui_diff_text(const char *old_text, const char *new_text); enum { TUI_TOOL_PENDING, TUI_TOOL_OK, TUI_TOOL_ERROR }; typedef struct { const char *name; /* the tool */ const char *title; /* what it acts on: "$ ls -la", "t.txt:10-20" */ const char *diff; /* edit: tui_diff_text output, else NULL */ const char *output; /* the result text; NULL while pending */ int state; long ms; /* elapsed so far (pending) or how long it took; < 0: unknown */ } tui_tool; /* A tool call as a block of lines for cols columns, appended to out: a header * (state mark, title), the diff or output indented two columns and dimmed * (errors red), and "Took 4.2s" / "Elapsed 1.2s". Collapsed (expanded = 0) a * block shows the last 5 lines of bash output and the first 10 of other output * (24 of a diff), with "... (N more lines, ctrl+o to expand)". 0, or -1 (out * of memory). */ int tui_tool_lines(const tui_tool *t, int expanded, size_t cols, tui_lines *out); typedef struct { long long in, out, cache_read, cache_write, context; long window; /* context window in tokens; <= 0: unknown */ const char *model; const char *level; /* thinking level; NULL or "": the model has none */ int expanded; /* tool output is expanded */ double cost; /* the session's cost in dollars; <= 0: not shown */ } tui_footer; /* Footer line 1: "~/dir (branch) • name"; ws is shortened to ~ under home. */ void tui_footer_where(const char *ws, const char *home, const char *branch, const char *name, char *out, size_t cap); /* Footer line 2, dimmed and fitted to cols: "↑in ↓out Rcache Wcache $cost pct%/window", * the percentage yellow past 70 and red past 90, the model right-aligned with * " • level" (" • thinking off" for "off"). */ void tui_footer_stats(const tui_footer *f, size_t cols, char *out, size_t cap); /* The branch of the git checkout at or above dir (a ".git" directory, or a * file pointing at one): its name, or "detached". 1 found, 0 none (out is "" then). */ int tui_git_branch(const char *dir, char *out, size_t cap); /* "Retrying (2/3) in 8s… (esc to cancel)"; the delay part only when delay_ms * is > 0, counted down by elapsed_ms. */ void tui_retry_status(int attempt, int max_attempts, long delay_ms, long elapsed_ms, char *out, size_t cap); enum { TUI_BASH_RUNNING, TUI_BASH_DONE, TUI_BASH_CANCELLED }; typedef struct { const char *command; const char *output; /* as the command wrote it: ANSI sequences and CRs are cleaned here */ int state; /* TUI_BASH_* */ int exit_code; /* DONE: a nonzero code is shown */ int no_context; /* "!!": drawn dim; "!" is drawn green */ } tui_bash; /* A command the user ran with "!" or "!!", as Pi's BashExecutionComponent shows * it: a blank row, a rule, "$ command" in bold, the last 20 lines of output * (all, expanded; the last 2000 at most), "... N more lines (ctrl+o to * expand)", "(cancelled)" or "(exit N)", or "Running... (esc to cancel)", and a * rule. 0, or -1 (out of memory). */ int tui_bash_lines(const tui_bash *b, int expanded, size_t cols, tui_lines *out); /* What Pi puts in the model's context for a "!" command (bashExecutionToText): * "Ran `cmd`", the output in a fence (its last 2000 lines / 50KB, escape * sequences removed), "(command cancelled)" or "Command exited with code N". * malloc'd; NULL when out of memory. */ char *tui_bash_context(const char *command, const char *output, int cancelled, int exit_code); /* Pi's compact startup key hints: "esc interrupt · ctrl+c/ctrl+d clear/exit ..." (dim keys). */ void tui_startup_hints(char *out, size_t cap); /* One startup list, as Pi's loaded-resources section: "[Name]" and the items on * one dim line indented two columns, comma-separated and wrapped. sort: by name * (not for context files, which keep their load order). Nothing when n is 0. */ int tui_loaded_section(const char *name, const char *const *items, size_t n, int sort, size_t cols, tui_lines *out); /* The terminal title escape (OSC 0): "pic - NAME - DIR" or "pic - DIR", DIR the * workspace's last component; control characters are dropped. */ void tui_title(const char *workspace, const char *name, char *out, size_t cap); #endif