Skip to content
pi-c Documentation
Login

Documentation

/* Inline drawing (render.c). The screen is the terminal's own scrollback:
 * finished output is committed once and left there; only a small live
 * area at the bottom (the reply being written, status, editor, footer) is
 * redrawn, and only from its first changed line. Every write is wrapped in
 * synchronized output (CSI ?2026h ... CSI ?2026l). */
#ifndef PIC_TUI_RENDER_H
#define PIC_TUI_RENDER_H

#include <stddef.h>

typedef struct tui_render tui_render;

/* Writes to fd (the terminal). The live area starts empty at the cursor. */
tui_render *tui_render_new(int fd);
void tui_render_free(tui_render *r);

/* Replaces the live area with lines[0..n) (each already fits in the width,
 * no newlines; may contain SGR) and puts the cursor at line crow, column
 * ccol of it (crow < 0: cursor hidden). Lines equal to the previous draw's
 * are not rewritten: the cursor moves up to the first line that differs,
 * clears to the end of the screen and writes from there. More lines than
 * the terminal has rows: only the last rows-1 are drawn. 0, or -1 on a
 * write error. */
int tui_render_draw(tui_render *r, const char *const *lines, size_t n, int crow, int ccol, int rows);
/* A line ending in this (a reset) fills the whole width and runs into the
 * next one: no line break is written between them, the terminal wraps them
 * itself, so its own selection takes them as one line (a link too long for
 * the window). The dock's rows in fullscreen, any row in regular mode. */
#define TUI_ROW_WRAPS "\x1b[0;0m"

/* Writes lines[0..n) permanently above the live area (they scroll into the
 * terminal's history and are never redrawn), then redraws the live area
 * as last drawn below them. */
int tui_render_commit(tui_render *r, const char *const *lines, size_t n);

/* Replaces every committed line with lines[0..n): Pi draws the whole
 * transcript again when what is above the live area changes (Ctrl+O,
 * Ctrl+T). Fullscreen: the next draw shows them. Regular: the screen and its
 * scrollback are cleared and the lines written again, and the next draw
 * starts the live area below them. 0 or -1. */
int tui_render_replace(tui_render *r, const char *const *lines, size_t n);
/* After a resize: the next draw rewrites the whole live area. The old live
 * area is cleared from its first row down (reflow may leave stray lines
 * above it in the history; they are not chased). */
void tui_render_invalidate(tui_render *r);

/* Leaves the cursor on a fresh line below the live area, shown. Fullscreen:
 * leaves the alternate screen and, when the options say so (Pi's
 * fullscreenExitOutput "transcript"), prints the transcript and the dock as
 * last drawn to the normal screen. */
int tui_render_finish(tui_render *r);

/* ---- fullscreen (Pi's tuiMode "fullscreen") -------------------------------
 * Every committed line is kept, in both modes. Fullscreen draws on the
 * alternate screen (tui_term_alt): a transcript viewport over the committed
 * lines followed by the live lines before split, and below it the dock, the
 * lines from split on (status, editor, footer), pinned to the bottom rows.
 * The viewport follows the end until it is scrolled up; committed lines wider
 * than the screen after a resize are laid out again (tui_line_layout). */

/* Switches fullscreen on or off for a terminal of cols x rows. On: the
 * regular live area is left where it is and the alternate screen taken. Off:
 * the alternate screen is left and the transcript printed to the normal
 * screen, where the next draw starts a regular live area. 0 or -1. */
int tui_render_fullscreen(tui_render *r, int on, int cols, int rows);
int tui_render_is_fullscreen(const tui_render *r);
/* The terminal's size (call on resize, with tui_render_invalidate). */
void tui_render_size(tui_render *r, int cols, int rows);
/* bar: TUI_BAR_* (Pi's fullscreenScrollbar); exit_transcript: finish prints
 * the transcript (else only leaves the screen: Pi's "resume-hint"). */
void tui_render_options(tui_render *r, int bar, int exit_transcript);

/* tui_render_draw with lines[0..split) the live end of the transcript and
 * lines[split..n) the dock (crow counts from lines[0] as there). Regular
 * mode draws them all as the live area, as tui_render_draw does. */
int tui_render_draw_split(tui_render *r, const char *const *lines, size_t n, size_t split, int crow, int ccol, int rows);

/* Scrolling the viewport (fullscreen; no-ops otherwise), redrawn by the next
 * draw: by lines (negative: up), a page (dir -1 PageUp, 1 PageDown; Pi's
 * height less 4 lines), or to the start (end 0) or the end (end 1, which
 * follows again). 1 when the view moved. */
int tui_render_scroll(tui_render *r, long lines);
int tui_render_page(tui_render *r, int dir);
int tui_render_edge(tui_render *r, int end);

/* Whether the view is scrolled up (the jump-to-latest button shows at the right of the viewport's last row),
 * and whether column x, row y (0-based) is on the button. */
int tui_render_scrolled(const tui_render *r);
int tui_render_jump_hit(const tui_render *r, int x, int y);

/* A mouse button at column x, row y (0-based; TUI_MOUSE_PRESS, _DRAG or
 * _RELEASE, the left button) selects transcript text: a press in the
 * viewport anchors a selection (elsewhere clears it), a drag extends it, a
 * release ends it. 1 when a release left text selected. */
int tui_render_mouse(tui_render *r, int what, int x, int y);
/* The selected text (tui_selection_text), malloc'd; NULL when none. */
char *tui_render_selection(tui_render *r);
/* Fullscreen: the URL of the link drawn at transcript cell x, y (0-based), or
 * NULL. malloc'd. */
char *tui_render_link_at(tui_render *r, int x, int y);

/* ---- transcript search (fullscreen; Pi's tui.altScreen.search*) --------------
 * A box at the top right of the screen takes a query; every match in the
 * transcript (the committed lines and the live end, so a reply being written
 * is searched as it grows) is highlighted with searchMatchBg/searchMatchText,
 * the current one reversed, and the viewport scrolls to it. The caller edits
 * the query and routes the keys; none of these do anything in regular mode. */
/* Names the keys shown in the box for the previous and next match. */
void tui_render_search_keys(tui_render *r, const char *prev, const char *next);
/* Opens the box, or closes it when it is open. 1 when it did anything. */
int tui_render_search_toggle(tui_render *r);
void tui_render_search_close(tui_render *r);
int tui_render_search_active(const tui_render *r);
/* The query as typed; setting it selects the first match from the current
 * one (or from the top of the view) on. */
const char *tui_render_search_query(const tui_render *r);
void tui_render_search_set(tui_render *r, const char *query);
/* The next (dir 1) or previous (dir -1) match, wrapping; no-op without a query. */
void tui_render_search_step(tui_render *r, int dir);
/* The matches found at the last draw, and the index of the current one (-1:
 * none) in *index. */
size_t tui_render_search_hits(const tui_render *r, long *index);

/* ---- a box over the transcript (fullscreen) ----------------------------------
 * lines[0..n), each WIDTH columns wide, drawn by the next draws centred over
 * the viewport, above the search box, with the cursor at line crow, column
 * ccol of them (crow < 0: hidden); the dock's cursor is hidden while a box
 * shows. A row of the viewport whose visible cells are unchanged is not
 * written. n 0 takes the box away. Regular mode draws no box. 0 or -1. */
int tui_render_box(tui_render *r, const char *const *lines, size_t n, size_t width, int crow, int ccol);

/* Milliseconds until a redraw is due by itself (the auto scrollbar hiding
 * again), or -1. */
long tui_render_wake(const tui_render *r);

/* Handing the terminal to another program (Ctrl+Z, an external editor):
 * pause leaves the screen as finish does but prints nothing in fullscreen;
 * resume takes the screen back (fullscreen) or starts a new live area at the
 * cursor (regular). The transcript is kept. */
int tui_render_pause(tui_render *r);
int tui_render_resume(tui_render *r);

#endif