/* The fullscreen viewport's math and line operations; see viewport.h. */
#include "tui/viewport.h"
#include <stdlib.h>
#include <string.h>
/* Pi's PAGE_SCROLL_OVERLAP (tui-alt-screen.ts). */
#define PAGE_OVERLAP 4
/* wheel-scroll.ts */
#define BURST_GAP_MS 5.0
#define GESTURE_GAP_MS 200.0
#define REFERENCE_GAP_MS 100.0
#define MAX_AUTO_LINES 6.0
#define REVERSE "\x1b[7m"
#define REVERSE_OFF "\x1b[27m"
#define RESET "\x1b[0m"
/* ---- scrolling ------------------------------------------------------------ */
void tui_scroll_init(tui_scroll *s)
{
memset(s, 0, sizeof *s);
s->follow = 1;
}
size_t tui_scroll_max(const tui_scroll *s)
{
return s->content > s->height ? s->content - s->height : 0;
}
void tui_scroll_layout(tui_scroll *s, size_t content, size_t height)
{
s->content = content;
s->height = height;
size_t max = tui_scroll_max(s);
if (s->follow || s->top > max)
s->top = max;
if (s->top == max)
s->follow = 1;
}
int tui_scroll_by(tui_scroll *s, long delta)
{
size_t max = tui_scroll_max(s), start = s->follow ? max : s->top, next;
if (delta < 0)
next = (size_t)-delta >= start ? 0 : start - (size_t)-delta;
else
next = (size_t)delta >= max - start ? max : start + (size_t)delta;
s->top = next;
s->follow = next == max;
return next != start;
}
long tui_scroll_page(const tui_scroll *s, int dir)
{
long n = s->height > PAGE_OVERLAP + 1 ? (long)(s->height - PAGE_OVERLAP) : 1;
return dir < 0 ? -n : n;
}
void tui_scroll_start(tui_scroll *s)
{
s->top = 0;
s->follow = s->content <= s->height;
}
void tui_scroll_end(tui_scroll *s)
{
s->top = tui_scroll_max(s);
s->follow = 1;
}
int tui_scrollbar_geometry(const tui_scroll *s, tui_scrollbar *g)
{
size_t h = s->height, max = tui_scroll_max(s);
if (h == 0)
return 0;
if (s->content <= h) {
g->thumb_top = 0;
g->thumb_height = h;
return 1;
}
/* round(h*h / content), at least min(2, h), at most h */
size_t th = (h * h * 2 + s->content) / (s->content * 2), lo = h < 2 ? h : 2;
if (th > h)
th = h;
if (th < lo)
th = lo;
size_t room = h - th, top = s->top > max ? max : s->top;
g->thumb_height = th;
g->thumb_top = max ? (top * room * 2 + max) / (max * 2) : 0;
return 1;
}
int tui_scrollbar_mode(const char *setting)
{
if (setting && strcmp(setting, "always") == 0)
return TUI_BAR_ALWAYS;
if (setting && strcmp(setting, "hidden") == 0)
return TUI_BAR_HIDDEN;
return TUI_BAR_AUTO;
}
/* ---- the wheel ------------------------------------------------------------ */
void tui_wheel_init(tui_wheel *w, int lines, int accelerate)
{
w->lines = lines;
w->accelerate = accelerate;
w->last = -1;
w->dir = 0;
w->avg = -1;
w->carry = 0;
}
int tui_wheel_next(tui_wheel *w, int dir, double now)
{
if (w->lines > 0)
return w->lines;
if (!w->accelerate)
return 1;
double gap = w->last < 0 ? GESTURE_GAP_MS + 1 : now - w->last;
int same = dir == w->dir && gap <= GESTURE_GAP_MS;
w->last = now;
w->dir = dir;
if (!same) {
w->avg = -1;
w->carry = 0;
return 1;
}
if (gap < BURST_GAP_MS)
return 1;
w->avg = w->avg < 0 ? gap : (w->avg + gap) / 2;
double lines = REFERENCE_GAP_MS / w->avg;
if (lines < 1)
lines = 1;
if (lines > MAX_AUTO_LINES)
lines = MAX_AUTO_LINES;
lines += w->carry;
int whole = (int)lines;
w->carry = lines - whole;
return whole;
}
int tui_wheel_lines(const char *setting)
{
char *end = NULL;
long v;
if (!setting || !*setting || strcmp(setting, "auto") == 0)
return 0;
v = strtol(setting, &end, 10);
if (!end || end == setting || *end)
return 0;
return v < 1 ? 1 : v > 100 ? 100 : (int)v;
}
/* ---- walking a styled line ------------------------------------------------- */
/* Bytes of the escape sequence at s (s[0] is ESC): CSI up to its final byte,
* OSC up to BEL or ESC \, else ESC and one byte. */
static size_t esc_len(const char *s, size_t n)
{
size_t i;
if (n < 2)
return n;
if (s[1] == '[') {
for (i = 2; i < n && !((unsigned char)s[i] >= 0x40 && (unsigned char)s[i] <= 0x7e); i++) {
}
return i < n ? i + 1 : n;
}
if (s[1] == ']') {
for (i = 2; i < n; i++) {
if (s[i] == '\a')
return i + 1;
if (s[i] == 0x1b && i + 1 < n && s[i + 1] == '\\')
return i + 2;
}
return n;
}
return 2;
}
/* The character at s: its bytes and columns. */
static size_t char_at(const char *s, size_t n, int *cols)
{
uint32_t cp;
size_t used = tui_utf8_decode(s, n, &cp);
*cols = cp == '\t' ? 4 : tui_cp_width(cp);
return used;
}
typedef struct {
char *buf;
size_t n, cap;
int oom;
} sbuf;
static void sb_add(sbuf *b, const char *s, size_t n)
{
if (b->oom)
return;
if (b->n + n + 1 > b->cap) {
size_t cap = b->cap ? b->cap : 64;
while (cap < b->n + n + 1)
cap *= 2;
char *p = realloc(b->buf, cap);
if (!p) {
b->oom = 1;
return;
}
b->buf = p, b->cap = cap;
}
memcpy(b->buf + b->n, s, n);
b->n += n;
b->buf[b->n] = '\0';
}
static void sb_str(sbuf *b, const char *s)
{
sb_add(b, s, strlen(s));
}
static char *sb_done(sbuf *b)
{
if (b->oom) {
free(b->buf);
return NULL;
}
if (!b->buf)
return strdup("");
return b->buf;
}
char *tui_line_plain(const char *line, size_t c0, size_t c1)
{
sbuf b = {NULL, 0, 0, 0};
size_t n = strlen(line), i = 0, col = 0;
while (i < n && col < c1) {
if (line[i] == 0x1b) {
i += esc_len(line + i, n - i);
continue;
}
int w;
size_t used = char_at(line + i, n - i, &w);
if (col >= c0 && w >= 0) {
if (line[i] == '\t')
sb_add(&b, " ", 4);
else if ((unsigned char)line[i] >= 0x20)
sb_add(&b, line + i, used);
}
col += (size_t)(w > 0 ? w : 0);
i += used;
}
return sb_done(&b);
}
char *tui_line_highlight(const char *line, size_t c0, size_t c1)
{
sbuf b = {NULL, 0, 0, 0};
size_t n = strlen(line), i = 0, col = 0;
int on = 0;
while (i < n) {
if (line[i] == 0x1b) {
size_t e = esc_len(line + i, n - i);
sb_add(&b, line + i, e);
if (on && e >= 3 && line[i + 1] == '[' && line[i + e - 1] == 'm')
sb_str(&b, REVERSE); /* an SGR reset would end it */
i += e;
continue;
}
int w;
size_t used = char_at(line + i, n - i, &w);
if (!on && col >= c0 && col < c1 && w > 0) {
sb_str(&b, REVERSE);
on = 1;
} else if (on && col >= c1 && w > 0) {
sb_str(&b, REVERSE_OFF);
on = 0;
}
sb_add(&b, line + i, used);
col += (size_t)(w > 0 ? w : 0);
i += used;
}
if (on)
sb_str(&b, REVERSE_OFF);
return sb_done(&b);
}
char *tui_line_bar(const char *line, size_t cols, const char *glyph)
{
sbuf b = {NULL, 0, 0, 0};
size_t room = cols > 0 ? cols - 1 : 0, n = strlen(line);
size_t fit = tui_fit(line, n, room), w = tui_width(line, fit);
sb_add(&b, line, fit);
sb_str(&b, RESET);
for (; w < room; w++)
sb_add(&b, " ", 1);
if (cols > 0)
sb_str(&b, glyph);
return sb_done(&b);
}
int tui_line_layout(const char *line, size_t cols, tui_lines *out)
{
size_t n = strlen(line), fit;
if (cols < 1)
cols = 1;
if (tui_width(line, n) <= cols)
return tui_lines_add(out, line, n);
fit = tui_fit(line, n, cols);
{
char *rest = tui_line_plain(line + fit, 0, (size_t)-1);
int blank = rest && rest[strspn(rest, " ")] == '\0';
free(rest);
if (blank) {
/* Keep the escapes after the cut (a closing reset, a link end). */
sbuf b = {NULL, 0, 0, 0};
sb_add(&b, line, fit);
for (size_t i = fit; i < n;) {
if (line[i] == 0x1b) {
size_t e = esc_len(line + i, n - i);
sb_add(&b, line + i, e);
i += e;
} else {
i++;
}
}
char *s = sb_done(&b);
int rc = s ? tui_lines_add(out, s, strlen(s)) : -1;
free(s);
return rc;
}
}
return tui_wrap(line, n, cols, out);
}
/* ---- selection ---------------------------------------------------------- */
static int before(tui_point a, tui_point b)
{
return a.line < b.line || (a.line == b.line && a.col < b.col);
}
int tui_selection_bounds(const tui_selection *s, tui_point *start, tui_point *end)
{
if (!s->on || (s->anchor.line == s->focus.line && s->anchor.col == s->focus.col))
return 0;
if (before(s->anchor, s->focus)) {
*start = s->anchor;
*end = s->focus;
} else {
*start = s->focus;
*end = s->anchor;
}
return 1;
}
int tui_selection_cols(const tui_selection *s, size_t line, size_t width, size_t *c0, size_t *c1)
{
tui_point a, b;
size_t lo = 0, hi = width;
if (!tui_selection_bounds(s, &a, &b) || line < a.line || line > b.line)
return 0;
if (line == a.line)
lo = a.col < width ? a.col : width;
if (line == b.line)
hi = b.col + 1 < width ? b.col + 1 : width;
if (lo >= hi)
return 0;
*c0 = lo;
*c1 = hi;
return 1;
}
char *tui_selection_text(const tui_selection *s, const char *(*line_at)(void *ctx, size_t i), void *ctx)
{
tui_point a, b;
sbuf out = {NULL, 0, 0, 0};
if (!tui_selection_bounds(s, &a, &b))
return NULL;
for (size_t l = a.line; l <= b.line; l++) {
const char *line = line_at(ctx, l);
size_t w = tui_width(line, strlen(line)), c0, c1;
if (l > a.line)
sb_add(&out, "\n", 1);
if (tui_selection_cols(s, l, w, &c0, &c1)) {
char *p = tui_line_plain(line, c0, c1);
if (!p) {
free(out.buf);
return NULL;
}
size_t len = strlen(p);
while (len > 0 && p[len - 1] == ' ')
len--;
sb_add(&out, p, len);
free(p);
}
}
char *text = sb_done(&out);
if (text && !*text) {
free(text);
return NULL;
}
return text;
}