/* 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);
/* 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);
/* 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);
/* 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);
/* 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