/* 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; the colours are
* the current theme's (theme.h). */
#ifndef PIC_TUI_VIEW_H
#define PIC_TUI_VIEW_H
#include <stddef.h>
#include "width.h"
/* One editor border row of exactly cols columns in the border colour: "─"
* all the way, or "── " then status (wrapped in status_sgr, shortened to fit)
* then "─" to the edge. status NULL or "": no status. 0 or -1. */
int tui_border_line(const char *status_sgr, const char *status, size_t cols, tui_lines *out);
/* tui_border_line in another colour (an SGR sequence): Pi colours the editor's border by thinking level, or
* as bash mode. */
int tui_border_line_in(const char *color_sgr, const char *status_sgr, const char *status, size_t cols, tui_lines *out);
/* 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 */
const char *content; /* write: the text written, shown in place of the result (Pi's write renderer) */
} tui_tool;
/* A tool call as a block of lines for cols columns, appended to out: a header
* (state mark, title), the diff, the content written or the 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)" (", T total" for content). 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.
* cols > 0: a path too long for it is shortened on the left ("…/end/of/it")
* so the directory, branch and name stay. */
void tui_footer_where(const char *ws, const char *home, const char *branch, const char *name, size_t cols, char *out,
size_t cap);
/* path in at most cols columns: as it is, or "…" and as much of its end as fits. */
void tui_fit_path(const char *path, size_t cols, 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"); short of room, the model's
* "provider/" goes before the rest is cut. */
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