Artifact 01d02f2df657d33df9d09108af2a17419b066aaff64021ffa8d66c9d80d1e86b:
- File
src/tui/view.h
— part of check-in
[e6887db767]
at
2026-11-19 22:44:19
on branch tui-parity-2
— TUI: closer to Pi, round 2
!cmd runs a command and adds its output to the next message; !!cmd runs it without. Editor: a kill ring (Ctrl+K, Ctrl+U, Ctrl+W, Alt+D into it; Ctrl+Y, Alt+Y out), undo (Ctrl+-), Ctrl+] to jump to a character, Ctrl+G to edit in $VISUAL or $EDITOR, Ctrl+Z to suspend. Markdown: tables, ordered and nested lists, quotes, italics, strikethrough, links (OSC 8 where the terminal draws them), code-fence labels; user messages on a background. Alt+Up restores queued messages. Startup shows Pi's key hints and the loaded context files, skills and prompt templates, and sets the terminal title. Double Esc opens /tree, Shift+Ctrl+P cycles models backwards, Ctrl+X copies the last reply. (user: dmestas, size: 5128) [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 */ } 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 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