/* 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 #include "term.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; int hold; /* scrolled to the end on purpose (a search match): it stays there instead of following */ } 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); /* Pi's ScrollView.scrollTo: top clamped to 0..max. disable_follow keeps a view * that lands on the end from following; it holds until the next scroll. */ void tui_scroll_to(tui_scroll *s, size_t top, int disable_follow); 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); /* The URL of the OSC 8 link drawn at column c of line, or NULL. malloc'd. */ char *tui_line_link_at(const char *line, size_t c); /* 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); /* ---- transcript search (Pi's alt-screen-search.ts) -------------------------- * The rendered lines are searched as one text: escape sequences dropped, every * run of whitespace (a line break too) one space, so a match can wrap across * rows. The query is normalised the same way, trimmed, and matched * case-insensitively (ASCII, Latin-1/Extended-A, Greek, Cyrillic), without * overlaps. A match is the display columns it covers on each row it touches. */ typedef struct { size_t row, c0, c1; /* columns [c0, c1) of content line row */ } tui_seg; typedef struct { size_t first, n; /* segs[first .. first + n) */ } tui_hit; typedef struct { tui_seg *segs; tui_hit *hits; size_t nsegs, nhits; } tui_found; /* Every match of query in lines 0..nlines-1 (line_at gives line i), in order. * An empty (all-blank) query has none. 0, or -1 when out of memory. */ int tui_search_find(size_t nlines, const char *(*line_at)(void *ctx, size_t i), void *ctx, const char *query, tui_found *out); void tui_found_free(tui_found *f); /* Pi's getAltScreenSearchMatchKey: the first row and column and the last row * and column of hit i, to find the same match again after the text changed. */ void tui_hit_key(const tui_found *f, size_t i, size_t key[4]); /* The index of the hit with this key, or -1. */ long tui_found_by_key(const tui_found *f, const size_t key[4]); /* How the next selection is chosen (Pi's SearchSelectionMode). */ enum { TUI_PICK_QUERY, TUI_PICK_RETAIN, TUI_PICK_NEXT, TUI_PICK_PREV }; /* The hit to select: QUERY the first at or after anchor_row (else the first); * NEXT / PREV step from base (the current hit, -1 when none) and wrap; RETAIN * keeps base, clamped. -1 when there are no hits. */ long tui_search_pick(const tui_found *f, int mode, long base, size_t anchor_row); /* The top that shows hit i: the current one when the hit is in view, else its * first row less a third of the viewport. */ size_t tui_search_reveal(const tui_scroll *s, const tui_found *f, size_t i); /* A highlighted span of one row: columns [c0, c1); current: the selected hit. */ typedef struct { size_t c0, c1; int current; } tui_span; /* The spans on content line row (hit `selected` is the current one), in column * order, at most cap; how many there are. */ size_t tui_found_row(const tui_found *f, long selected, size_t row, tui_span *out, size_t cap); /* line with columns [c0, c1) wrapped in open ... close: each run of text * between the line's own escapes is wrapped on its own, so a reset inside the * range does not end it. malloc'd. */ char *tui_line_style(const char *line, size_t c0, size_t c1, const char *open, const char *close); /* line with columns [col, col+width) replaced by text (width columns wide); * the line is padded with spaces up to col. malloc'd. */ char *tui_line_splice(const char *line, size_t col, size_t width, const char *text); /* Column spans [x0, x1) inside the search box's bottom row (row 2) for the prev/next arrows, * or 0/0 when the box is too narrow to show them. */ typedef struct { size_t prev_x0, prev_x1; size_t next_x0, next_x1; } tui_search_hit_cols; /* Pi's AltScreenSearchComponent: the three rows of the search box width * columns wide, appended to out: the query with a reverse-video cursor at * byte offset `cursor` (a dim placeholder when empty), a dim "i/n" or * "No matches" once there is a query (idx < 0: no hit selected), and the * key hints in the bottom border. Key names are as shown ("Shift+Enter"). * If hits is non-NULL it receives the column spans of the prev/next arrows * inside the box's bottom row. 0 or -1. */ int tui_search_box(const char *query, size_t cursor, long idx, size_t count, const char *prev_key, const char *next_key, size_t width, tui_lines *out, tui_search_hit_cols *hits); /* Edits a search query buffer in place with a byte cursor *cur (0..strlen(buf)), handling * text/paste insertion at cursor, Left/Right/Home/End/word motion, Backspace/Delete, * word deletion and Ctrl+U/K. 1 when k was handled, 0 otherwise. */ int tui_search_edit(char *buf, size_t cap, size_t *cur, const tui_key *k); #endif