Skip to content
pi-c Documentation
Login

Documentation

/* The fullscreen transcript viewport (viewport.c): the scroll position over
 * laid-out lines, Pi's scrollbar geometry, mouse-wheel acceleration, and a
 * text selection over those lines. Pure functions: no terminal, no clock. */
#ifndef PIC_TUI_VIEWPORT_H
#define PIC_TUI_VIEWPORT_H

#include <stddef.h>

#include "width.h"

/* Pi's ScrollView with follow "end": while it follows, new content keeps the
 * last line in view; scrolling up stops following, and reaching the end again
 * resumes it. */
typedef struct {
    size_t content; /* lines of content */
    size_t height;  /* rows of the viewport */
    size_t top;     /* the first content line shown */
    int follow;
} tui_scroll;

void tui_scroll_init(tui_scroll *s);
/* The largest top: content - height, or 0. */
size_t tui_scroll_max(const tui_scroll *s);
/* New content or viewport sizes (Pi's updateLayout): a following view stays at
 * the end, any other keeps its top, clamped. */
void tui_scroll_layout(tui_scroll *s, size_t content, size_t height);
/* Moves by delta lines (negative: up), clamped; 1 when the top moved. */
int tui_scroll_by(tui_scroll *s, long delta);
/* Lines PageUp (dir -1) or PageDown (dir 1) moves: the viewport height less
 * Pi's 4-line overlap, at least 1, signed by dir. */
long tui_scroll_page(const tui_scroll *s, int dir);
void tui_scroll_start(tui_scroll *s);
void tui_scroll_end(tui_scroll *s);

/* Pi's getScrollbarGeometry, rows relative to the viewport's top: the thumb
 * is height^2/content rows rounded (at least 2, at most height), placed by
 * top/max rounded; content that fits gets a full-height thumb. 0 when the
 * viewport has no rows. Whether to draw it is the caller's (TUI_BAR_*). */
typedef struct {
    size_t thumb_top, thumb_height;
} tui_scrollbar;
int tui_scrollbar_geometry(const tui_scroll *s, tui_scrollbar *g);

enum { TUI_BAR_AUTO, TUI_BAR_ALWAYS, TUI_BAR_HIDDEN };
/* "auto", "always", "hidden" (Pi's fullscreenScrollbar); anything else is auto. */
int tui_scrollbar_mode(const char *setting);

/* Pi's WheelScrollAccelerator: lines per wheel event. A fixed count (1..100)
 * is used as is. Auto (lines 0) on a terminal that already accelerates (local
 * macOS) is 1; elsewhere it follows the event rate within one gesture (same
 * direction, gaps up to 200 ms; gaps under 5 ms are one notch's burst): 100 ms
 * apart is 1 line, 50 ms 2, at most 6, with the fraction carried. */
typedef struct {
    int lines;      /* 0: auto */
    int accelerate; /* auto speeds up fast spins */
    double last;    /* ms of the last event; < 0: none */
    int dir;
    double avg;     /* average gap in ms; < 0: none yet */
    double carry;
} tui_wheel;
void tui_wheel_init(tui_wheel *w, int lines, int accelerate);
int tui_wheel_next(tui_wheel *w, int dir, double now_ms);
/* Pi's fullscreenWheelScrollLines: "auto" (or unset) is 0; a number is
 * clamped to 1..100; anything else is auto. */
int tui_wheel_lines(const char *setting);

/* A point in the content: a line and a display column. */
typedef struct {
    size_t line, col;
} tui_point;

/* A selection from a press (anchor) to where the pointer is (focus). The cell
 * under the focus is included; anchor == focus is a click, not a selection. */
typedef struct {
    tui_point anchor, focus;
    int on; /* a selection exists */
} tui_selection;

/* The selection in order; 0 when there is none (or it is a click). */
int tui_selection_bounds(const tui_selection *s, tui_point *start, tui_point *end);
/* The columns [c0, c1) selected on line `line` whose width is `width`; 0 when
 * none of it is. */
int tui_selection_cols(const tui_selection *s, size_t line, size_t width, size_t *c0, size_t *c1);
/* The selected text: each line's selected columns without escape sequences,
 * trailing spaces dropped, joined with "\n". malloc'd; NULL when nothing is
 * selected (or out of memory). line_at returns content line i ("" past the end). */
char *tui_selection_text(const tui_selection *s, const char *(*line_at)(void *ctx, size_t i), void *ctx);

/* The printable text of columns [c0, c1) of line: escape sequences dropped, a
 * wide character kept when it starts inside the range. malloc'd. */
char *tui_line_plain(const char *line, size_t c0, size_t c1);
/* line with columns [c0, c1) in reverse video (kept through the line's own
 * SGR changes). malloc'd. */
char *tui_line_highlight(const char *line, size_t c0, size_t c1);
/* line cut to cols-1 columns and padded to them, then glyph in the last column
 * (the scrollbar). malloc'd. */
char *tui_line_bar(const char *line, size_t cols, const char *glyph);
/* A committed line laid out for cols columns, appended to out: as is when it
 * fits; cut when only spaces overflow (a padded background); else wrapped
 * with tui_wrap. 0 or -1. */
int tui_line_layout(const char *line, size_t cols, tui_lines *out);

#endif