forked from tigerkelly/fbcurses
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.c
More file actions
616 lines (531 loc) · 20.8 KB
/
Copy pathwidgets.c
File metadata and controls
616 lines (531 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
* widgets.c — Extended widget implementations for fbcurses.
*
* Implements:
* Mouse input (GPM-style xterm escape parsing)
* Pop-up menu
* Single-line text-input editor
* Toast / notification popup
* Table / data-grid
* Vertical gauge
* Sparkline chart
*
* Copyright (c) 2026 Richard Kelly Wiles (rkwiles@twc.com)
*/
#define _POSIX_C_SOURCE 200809L
#include "fbcurses_internal.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <sys/select.h>
#include <unistd.h>
/* ── Internal timing helper ─────────────────────────────────────── */
static long _msNow(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000L + ts.tv_nsec / 1000000L;
}
/* ── Read one byte with timeout (ms=-1 = block) ─────────────────── */
static int _readByteMs(int ms)
{
if (ms >= 0) {
fd_set fds;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
struct timeval tv = { ms / 1000, (ms % 1000) * 1000 };
if (select(1, &fds, NULL, NULL, &tv) <= 0) return -1;
}
unsigned char c;
return (read(STDIN_FILENO, &c, 1) == 1) ? (int)c : -1;
}
/* ══════════════════════════════════════════════════════════════════
* MOUSE
* ══════════════════════════════════════════════════════════════════ */
void fbMouseInit(fbScreen *scr)
{
(void)scr;
/* Enable xterm extended mouse tracking (SGR mode) */
{ ssize_t _wr = write(STDOUT_FILENO, "\033[?1003h", 8); (void)_wr; } /* any-event tracking */
{ ssize_t _wr = write(STDOUT_FILENO, "\033[?1006h", 8); (void)_wr; } /* SGR extended mode */
}
void fbMouseShutdown(fbScreen *scr)
{
(void)scr;
{ ssize_t _wr = write(STDOUT_FILENO, "\033[?1003l", 8); (void)_wr; }
{ ssize_t _wr = write(STDOUT_FILENO, "\033[?1006l", 8); (void)_wr; }
}
bool fbMousePoll(fbScreen *scr, fbMouseEvent *ev)
{
/* Peek for ESC [ < (SGR mouse) */
int c = _readByteMs(0);
if (c < 0) return false;
if (c != 0x1B) { /* not a mouse escape — push-back not available, discard */ return false; }
int a = _readByteMs(20); if (a != '[') return false;
int b = _readByteMs(20); if (b != '<') return false;
/* Read: Cb ; Cx ; Cy M/m */
char buf[64];
int pos = 0;
int ch2;
while (pos < 60) {
ch2 = _readByteMs(20);
if (ch2 < 0) break;
buf[pos++] = (char)ch2;
if (ch2 == 'M' || ch2 == 'm') break;
}
buf[pos] = '\0';
int cb, cx, cy;
char release;
if (sscanf(buf, "%d;%d;%d%c", &cb, &cx, &cy, &release) != 4) return false;
memset(ev, 0, sizeof(*ev));
ev->col = cx - 1;
ev->row = cy - 1;
ev->x = ev->col * FB_FONT_W;
ev->y = ev->row * FB_FONT_H;
int btn = cb & 0x03;
bool pressed = (release == 'M');
bool moved = (cb & 0x20) != 0;
ev->moved = moved;
if (!moved) {
if (btn == 0) ev->left = pressed;
if (btn == 1) ev->middle = pressed;
if (btn == 2) ev->right = pressed;
if (cb == 64) ev->wheelDelta = +1;
if (cb == 65) ev->wheelDelta = -1;
}
(void)scr;
return true;
}
/* ══════════════════════════════════════════════════════════════════
* MENU
* ══════════════════════════════════════════════════════════════════ */
int fbMenu(fbScreen *scr, int col, int row,
const fbMenuItem *items,
fbColor fg, fbColor bg,
fbColor fgSel, fbColor bgSel,
fbBorderStyle border)
{
/* Count items, find the widest label */
int n = 0, maxW = 0;
for (int i = 0; items[i].label != NULL; i++) {
n++;
int l = (int)strlen(items[i].label);
if (l > maxW) maxW = l;
}
if (n == 0) return -1;
int wCols = maxW + 4; /* 1 border + 1 space each side + border */
int wRows = n + 2; /* top/bottom border */
/* Clamp to screen */
int scrC = fbCols(scr), scrR = fbRows(scr);
if (col + wCols > scrC) col = scrC - wCols;
if (row + wRows > scrR) row = scrR - wRows;
if (col < 0) col = 0;
if (row < 0) row = 0;
fbWindow *win = fbNewWindow(scr, col, row, wCols, wRows);
if (!win) return -1;
int sel = 0;
/* Skip any initially disabled items */
while (sel < n && items[sel].disabled) sel++;
for (;;) {
/* Draw */
fbClearWindow(win, bg);
fbDrawBorder(win, border, fg);
for (int i = 0; i < n; i++) {
int r = i + 1;
if (items[i].label[0] == '\0') {
/* Separator */
fbCell *cell;
for (int c2 = 1; c2 < wCols - 1; c2++) {
cell = _fbGetCell(win, c2, r);
cell->ch = L'─'; cell->fg = fg; cell->bg = bg;
cell->attr = FB_ATTR_NONE; cell->dirty = true;
}
} else {
bool isSel = (i == sel) && !items[i].disabled;
fbColor cfg = items[i].disabled ? fbLerp(fg, bg, 0.5f)
: (isSel ? fgSel : fg);
fbColor cbg = isSel ? bgSel : bg;
/* Highlight bar */
if (isSel) {
for (int c2 = 1; c2 < wCols - 1; c2++) {
fbCell *cell = _fbGetCell(win, c2, r);
cell->ch = L' '; cell->fg = cfg; cell->bg = cbg;
cell->dirty = true;
}
}
fbSetColors(win, cfg, cbg);
fbSetAttr(win, isSel ? FB_ATTR_BOLD : FB_ATTR_NONE);
fbPrintAt(win, 2, r, "%s", items[i].label);
}
}
fbRefresh(win);
fbFlush(scr);
int key = fbGetKey(scr);
if (key == FB_KEY_UP || key == 'k') {
do { sel = (sel - 1 + n) % n; }
while (items[sel].disabled || items[sel].label[0] == '\0');
} else if (key == FB_KEY_DOWN || key == 'j') {
do { sel = (sel + 1) % n; }
while (items[sel].disabled || items[sel].label[0] == '\0');
} else if (key == FB_KEY_ENTER || key == '\r' || key == ' ') {
int id = items[sel].id;
fbDelWindow(win);
fbFlush(scr);
return id;
} else if (key == FB_KEY_ESC || key == 'q') {
fbDelWindow(win);
fbFlush(scr);
return -1;
}
}
}
/* ══════════════════════════════════════════════════════════════════
* TEXT-INPUT WIDGET
* ══════════════════════════════════════════════════════════════════ */
struct fbTextInput {
fbWindow *win;
int col, row;
int width;
int maxLen;
char *buf;
int len; /* strlen(buf) */
int cursor; /* insertion point [0,len] */
int scroll; /* first visible char */
};
fbTextInput *fbTextInputNew(fbWindow *win, int col, int row,
int width, int maxLen,
const char *initial)
{
fbTextInput *ti = calloc(1, sizeof(fbTextInput));
if (!ti) return NULL;
ti->buf = calloc(1, (size_t)maxLen + 1);
if (!ti->buf) { free(ti); return NULL; }
ti->win = win;
ti->col = col;
ti->row = row;
ti->width = width;
ti->maxLen = maxLen;
if (initial) {
strncpy(ti->buf, initial, (size_t)maxLen);
ti->buf[maxLen] = '\0';
ti->len = (int)strlen(ti->buf);
ti->cursor = ti->len;
}
return ti;
}
void fbTextInputFree(fbTextInput *ti)
{
if (ti) { free(ti->buf); free(ti); }
}
void fbTextInputDraw(fbTextInput *ti, fbColor fg, fbColor bg, fbColor cursorFg)
{
if (!ti) return;
fbWindow *win = ti->win;
/* Ensure cursor is visible */
if (ti->cursor < ti->scroll)
ti->scroll = ti->cursor;
if (ti->cursor >= ti->scroll + ti->width)
ti->scroll = ti->cursor - ti->width + 1;
for (int i = 0; i < ti->width; i++) {
int charIdx = ti->scroll + i;
int wcol = ti->col + i;
int wrow = ti->row;
if (wcol >= fbWindowCols(win)) break;
fbCell *cell = _fbGetCell(win, wcol, wrow);
bool isCursor = (charIdx == ti->cursor);
cell->ch = (charIdx < ti->len) ? (wchar_t)(unsigned char)ti->buf[charIdx] : L' ';
cell->fg = isCursor ? cursorFg : fg;
cell->bg = isCursor ? fg : bg;
cell->attr = isCursor ? FB_ATTR_REVERSE : FB_ATTR_NONE;
cell->dirty = true;
}
}
bool fbTextInputKey(fbTextInput *ti, int key)
{
if (!ti) return false;
switch (key) {
case FB_KEY_LEFT:
if (ti->cursor > 0) ti->cursor--;
return true;
case FB_KEY_RIGHT:
if (ti->cursor < ti->len) ti->cursor++;
return true;
case FB_KEY_HOME: case 1 /* Ctrl-A */:
ti->cursor = 0;
return true;
case FB_KEY_END: case 5 /* Ctrl-E */:
ti->cursor = ti->len;
return true;
case FB_KEY_BACKSPACE: /* 0x7F */
if (ti->cursor > 0) {
memmove(ti->buf + ti->cursor - 1,
ti->buf + ti->cursor,
(size_t)(ti->len - ti->cursor + 1));
ti->cursor--;
ti->len--;
}
return true;
case FB_KEY_DELETE: case 4 /* Ctrl-D */:
if (ti->cursor < ti->len) {
memmove(ti->buf + ti->cursor,
ti->buf + ti->cursor + 1,
(size_t)(ti->len - ti->cursor));
ti->len--;
}
return true;
case 11 /* Ctrl-K */:
ti->buf[ti->cursor] = '\0';
ti->len = ti->cursor;
return true;
case FB_KEY_ENTER: /* 0x0D == '\r' */
case '\n':
case FB_KEY_ESC: case '\t':
return false; /* let caller handle these */
default:
if (key >= 0x20 && key < 0x100 && ti->len < ti->maxLen) {
memmove(ti->buf + ti->cursor + 1,
ti->buf + ti->cursor,
(size_t)(ti->len - ti->cursor + 1));
ti->buf[ti->cursor] = (char)key;
ti->cursor++;
ti->len++;
return true;
}
return false;
}
}
const char *fbTextInputGet(const fbTextInput *ti)
{
return ti ? ti->buf : "";
}
void fbTextInputSet(fbTextInput *ti, const char *text)
{
if (!ti || !text) return;
strncpy(ti->buf, text, (size_t)ti->maxLen);
ti->buf[ti->maxLen] = '\0';
ti->len = (int)strlen(ti->buf);
ti->cursor = ti->len;
ti->scroll = 0;
}
/* ══════════════════════════════════════════════════════════════════
* TOAST NOTIFICATION
* ══════════════════════════════════════════════════════════════════ */
static const struct {
fbColor border;
fbColor fg;
fbColor bg;
const char *icon;
} _toastStyle[] = {
[FB_TOAST_INFO] = { FB_CYAN, FB_WHITE, FB_RGB(20,40,60), " ℹ " },
[FB_TOAST_SUCCESS] = { FB_BRIGHT_GREEN, FB_WHITE, FB_RGB(15,45,25), " ✓ " },
[FB_TOAST_WARNING] = { FB_BRIGHT_YELLOW, FB_WHITE, FB_RGB(50,40,10), " ⚠ " },
[FB_TOAST_ERROR] = { FB_BRIGHT_RED, FB_WHITE, FB_RGB(55,15,15), " ✗ " },
};
void fbToast(fbScreen *scr, fbToastKind kind, const char *msg, int durationMs)
{
if (kind < 0 || kind > FB_TOAST_ERROR) kind = FB_TOAST_INFO;
const char *icon = _toastStyle[kind].icon;
int msgLen = (int)strlen(msg);
int iconLen = 3; /* icon is always 3 chars */
int wCols = iconLen + msgLen + 4; /* borders + padding */
int scrC = fbCols(scr);
if (wCols > scrC - 2) wCols = scrC - 2;
int col = (scrC - wCols) / 2;
int row = 1; /* near the top */
fbWindow *win = fbNewWindow(scr, col, row, wCols, 3);
if (!win) return;
fbColor bg = _toastStyle[kind].bg;
fbColor fg = _toastStyle[kind].fg;
fbColor border = _toastStyle[kind].border;
fbClearWindow(win, bg);
fbDrawBorder(win, FB_BORDER_ROUNDED, border);
fbSetColors(win, border, bg);
fbSetAttr(win, FB_ATTR_BOLD);
fbPrintAt(win, 1, 1, "%s", icon);
fbSetColors(win, fg, bg);
fbSetAttr(win, FB_ATTR_NONE);
fbPrint(win, "%.*s", wCols - iconLen - 3, msg);
fbRefresh(win);
fbFlush(scr);
if (durationMs <= 0) {
fbGetKey(scr);
} else {
long end = _msNow() + durationMs;
while (_msNow() < end) {
int rem = (int)(end - _msNow());
if (rem < 0) rem = 0;
int key = fbGetKeyTimeout(scr, rem < 50 ? rem : 50);
if (key != FB_KEY_NONE) break;
}
}
/* Erase: mark all cells dirty with bg and re-render blank */
fbClearWindow(win, FB_BLACK);
fbRefresh(win);
fbFlush(scr);
fbDelWindow(win);
}
/* ══════════════════════════════════════════════════════════════════
* TABLE / DATA-GRID
* ══════════════════════════════════════════════════════════════════ */
void fbDrawTable(fbWindow *win, int startCol, int startRow,
const fbTableCol *cols,
const char * const * const *rows, int nRows,
int selRow,
fbColor headerFg, fbColor headerBg,
fbColor cellFg, fbColor cellBg,
fbColor selFg, fbColor selBg)
{
if (!win || !cols) return;
/* Count columns and compute widths */
int nCols = 0;
while (cols[nCols].header) nCols++;
if (nCols == 0) return;
int *colW = calloc((size_t)nCols, sizeof(int));
if (!colW) return;
for (int c = 0; c < nCols; c++) {
colW[c] = cols[c].width > 0 ? cols[c].width
: (int)strlen(cols[c].header);
/* Auto-size: also check data */
if (cols[c].width == 0) {
for (int r = 0; r < nRows; r++) {
int l = rows[r][c] ? (int)strlen(rows[r][c]) : 0;
if (l > colW[c]) colW[c] = l;
}
}
}
int wW = fbWindowCols(win);
int wH = fbWindowRows(win);
/* ── Header row ── */
int cc = startCol;
fbSetColors(win, headerFg, headerBg);
fbSetAttr(win, FB_ATTR_BOLD);
for (int c = 0; c < nCols && cc < wW; c++) {
/* Pad/truncate header */
char hbuf[128];
snprintf(hbuf, sizeof(hbuf), "%-*.*s",
colW[c], colW[c], cols[c].header);
fbPrintAt(win, cc, startRow, "%s", hbuf);
cc += colW[c] + 1; /* +1 for column separator */
}
/* ── Header underline ── */
fbSetAttr(win, FB_ATTR_NONE);
cc = startCol;
for (int c = 0; c < nCols && cc < wW; c++) {
for (int x = cc; x < cc + colW[c] && x < wW; x++) {
fbCell *cell = _fbGetCell(win, x, startRow + 1);
cell->ch = L'─'; cell->fg = headerFg; cell->bg = headerBg;
cell->dirty = true;
}
if (cc + colW[c] < wW) {
fbCell *sep = _fbGetCell(win, cc + colW[c], startRow + 1);
sep->ch = L'┬'; sep->fg = headerFg; sep->bg = headerBg;
sep->dirty = true;
}
cc += colW[c] + 1;
}
/* ── Data rows ── */
for (int r = 0; r < nRows; r++) {
int dataRow = startRow + 2 + r;
if (dataRow >= wH) break;
bool isSel = (r == selRow);
fbColor rfg = isSel ? selFg : cellFg;
fbColor rbg = isSel ? selBg : (r % 2 == 0 ? cellBg : fbDarken(cellBg, 0.08f));
/* Highlight entire row background */
for (int x = startCol; x < wW; x++) {
fbCell *cell = _fbGetCell(win, x, dataRow);
cell->bg = rbg; cell->dirty = true;
}
cc = startCol;
fbSetAttr(win, isSel ? FB_ATTR_BOLD : FB_ATTR_NONE);
for (int c = 0; c < nCols && cc < wW; c++) {
const char *txt = rows[r][c] ? rows[r][c] : "";
int avail = colW[c];
int len = (int)strlen(txt);
char cbuf[256];
if (cols[c].align == FB_ALIGN_RIGHT) {
snprintf(cbuf, sizeof(cbuf), "%*.*s", avail, avail, txt);
} else if (cols[c].align == FB_ALIGN_CENTER) {
int pad = (avail - len) / 2;
snprintf(cbuf, sizeof(cbuf), "%*s%-*.*s",
pad, "", avail - pad, avail - pad, txt);
} else {
snprintf(cbuf, sizeof(cbuf), "%-*.*s", avail, avail, txt);
}
fbSetColors(win, rfg, rbg);
fbPrintAt(win, cc, dataRow, "%s", cbuf);
cc += colW[c] + 1;
}
}
fbSetAttr(win, FB_ATTR_NONE);
free(colW);
}
/* ══════════════════════════════════════════════════════════════════
* VERTICAL GAUGE
* ══════════════════════════════════════════════════════════════════ */
/* Unicode vertical-eighth blocks from bottom to top */
static const wchar_t _vblocks[] = {
L' ', L'▁', L'▂', L'▃', L'▄', L'▅', L'▆', L'▇', L'█'
};
void fbDrawGauge(fbWindow *win, int col, int row,
int height, int value, int maxVal,
fbColor fg, fbColor bg)
{
if (!win || height <= 0 || maxVal <= 0) return;
if (value < 0) value = 0;
if (value > maxVal) value = maxVal;
/* Total "eighths" filled */
int total8 = (int)((long)value * height * 8 / maxVal);
for (int r = 0; r < height; r++) {
int displayRow = row + height - 1 - r; /* bottom-up */
if (displayRow < 0 || displayRow >= fbWindowRows(win)) continue;
if (col < 0 || col >= fbWindowCols(win)) continue;
int rowFilled = total8 - r * 8;
wchar_t glyph;
fbColor gfg, gbg;
if (rowFilled >= 8) {
glyph = L'█'; gfg = fg; gbg = bg;
} else if (rowFilled > 0) {
glyph = _vblocks[rowFilled]; gfg = fg; gbg = bg;
} else {
glyph = L' '; gfg = bg; gbg = bg;
}
fbCell *cell = _fbGetCell(win, col, displayRow);
cell->ch = glyph;
cell->fg = gfg;
cell->bg = gbg;
cell->attr = FB_ATTR_NONE;
cell->dirty = true;
}
}
/* ══════════════════════════════════════════════════════════════════
* SPARKLINE
* ══════════════════════════════════════════════════════════════════ */
void fbDrawSparkline(fbWindow *win, int col, int row,
const float *values, int nValues, int width,
fbColor fg, fbColor bg)
{
if (!win || !values || nValues == 0 || width == 0) return;
for (int i = 0; i < width; i++) {
int c = col + i;
if (c >= fbWindowCols(win)) break;
/* Map data index proportionally to width */
int di = (nValues > width) ? i * nValues / width : i;
if (di >= nValues) di = nValues - 1;
float v = values[di];
if (v < 0.0f) v = 0.0f;
if (v > 1.0f) v = 1.0f;
int eighth = (int)(v * 8.0f);
if (eighth > 8) eighth = 8;
wchar_t glyph = _vblocks[eighth];
/* Colour: gradient from cool to hot */
fbColor gfg = fbLerp(fg, FB_BRIGHT_RED, v * 0.6f);
fbCell *cell = _fbGetCell(win, c, row);
cell->ch = glyph;
cell->fg = gfg;
cell->bg = bg;
cell->attr = FB_ATTR_NONE;
cell->dirty = true;
}
}