Files
splinter/src/ui.c
T
Konrad d2e06b316d splinter: curses TUI on NetBSD curses, CDE palettes as themes
The harness now has the interface it was missing: a chat pane on three
quarters of the screen, a sidebar on one quarter, a status bar underneath.

- vendor/netbsd-curses: NetBSD's curses and terminfo, ported edition,
  vendored with PROVENANCE.md. The build of that one directory needs GNU
  make; splinter's own Makefile stays POSIX make.
- tools/genpalettes.py turns CDE's *.dp palette files into a C table. 42
  palettes, generated, checked in with the generator, so the source of every
  colour is a file in the CDE tree and not a guess.
- themes: roles are derived, not invented - background is the darkest of the
  eight colour sets, foreground the lightest, border the second darkest,
  accent the most saturated of the rest, status the accent darkened by a
  third. Delphinium is the default. Unknown palette is an error.
- colours map to the terminal through xterm's 256-colour cube when available,
  the 16 basic colours otherwise, so a palette looks the same everywhere
  within what the terminal can show.
- ui.c redraws only on change; the sidebar shows session, transport, theme
  and last-turn numbers. Widgets are a labelled empty area - the registry
  lands next.

Six test binaries green, including test_theme (42 palettes, ordering
invariants, unknown name rejected, colour indices in range).
2026-11-07 11:27:09 +00:00

362 lines
8.6 KiB
C

/*
* splinter -- curses chat interface.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Layout, fixed and deliberate:
*
* +---------------------------------+------------+
* | history | sidebar |
* | three quarters | one quarter|
* +---------------------------------+------------+
* | status bar |
* +----------------------------------------------+
*
* Only the UI thread ever calls into curses. Anything that wants to appear in
* the sidebar hands over finished text and never touches a WINDOW.
*
* Redraws happen when something changed -- after a turn, after a key -- so an
* idle splinter burns no CPU.
*
* Keys: Enter sends, Ctrl-L redraws, Ctrl-C drops the input line, Ctrl-D quits.
*/
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "theme.h"
#include "ui.h"
#define MAX_ENTRIES 512
struct entry {
char *role;
char *text;
};
struct ui_state {
struct entry entries[MAX_ENTRIES];
size_t n;
long last_ms;
size_t last_steps;
size_t last_calls;
const char *endpoint;
int quit;
struct sp_theme theme;
};
static WINDOW *ui_hist;
static WINDOW *ui_side;
static WINDOW *ui_input;
static WINDOW *ui_status;
enum {
PAIR_MAIN = 1, /* fg on bg */
PAIR_ACCENT, /* accent on bg */
PAIR_DIM, /* border on bg */
PAIR_STATUS /* bg on status */
};
static void
init_colors(const struct sp_theme *t)
{
short n = (short)(COLORS > 0 ? COLORS : 8);
if (!has_colors())
return;
start_color();
use_default_colors();
init_pair(PAIR_MAIN, sp_color_rgb(t->fg[0], t->fg[1], t->fg[2], n),
sp_color_rgb(t->bg[0], t->bg[1], t->bg[2], n));
init_pair(PAIR_ACCENT, sp_color_rgb(t->accent[0], t->accent[1],
t->accent[2], n), sp_color_rgb(t->bg[0], t->bg[1], t->bg[2], n));
init_pair(PAIR_DIM, sp_color_rgb(t->border[0], t->border[1],
t->border[2], n), sp_color_rgb(t->bg[0], t->bg[1], t->bg[2], n));
init_pair(PAIR_STATUS, sp_color_rgb(t->bg[0], t->bg[1], t->bg[2], n),
sp_color_rgb(t->status[0], t->status[1], t->status[2], n));
}
static int
entry_height(int width, size_t len)
{
if (width < 2)
return 1;
return (int)((len + (size_t)width - 1) / (size_t)width);
}
static void
draw_history(struct ui_state *s)
{
int rows, cols, y = 0;
size_t idx, start, used = 0;
getmaxyx(ui_hist, rows, cols);
werase(ui_hist);
/* the newest entries that fit, then painted top-down */
start = s->n;
for (idx = s->n; idx > 0; idx--) {
const struct entry *e = &s->entries[idx - 1];
size_t need = (size_t)entry_height(cols,
strlen(e->role) + strlen(e->text) + 2);
if (used + need > (size_t)rows)
break;
used += need;
start = idx - 1;
}
for (idx = start; idx < s->n && y >= 0 && y < rows; idx++) {
const struct entry *e = &s->entries[idx];
int ny, nx;
wmove(ui_hist, y, 0);
if (strcmp(e->role, "you") == 0)
wattron(ui_hist, COLOR_PAIR(PAIR_ACCENT) | A_BOLD);
else if (strcmp(e->role, "agent") == 0)
wattron(ui_hist, COLOR_PAIR(PAIR_MAIN));
else if (strcmp(e->role, "error") == 0)
wattron(ui_hist, COLOR_PAIR(PAIR_ACCENT) | A_BOLD);
else
wattron(ui_hist, COLOR_PAIR(PAIR_DIM));
waddstr(ui_hist, e->role);
wattroff(ui_hist, A_BOLD);
waddstr(ui_hist, ": ");
waddnstr(ui_hist, e->text, rows * cols);
wattrset(ui_hist, A_NORMAL);
getyx(ui_hist, ny, nx);
y = nx == 0 ? ny : ny + 1;
}
wnoutrefresh(ui_hist);
}
static void
sidebar_heading(int *y, int cols, const char *text)
{
wattron(ui_side, COLOR_PAIR(PAIR_DIM) | A_BOLD);
mvwaddnstr(ui_side, *y, 0, text, cols - 1);
wattroff(ui_side, A_BOLD);
(*y)++;
wattron(ui_side, COLOR_PAIR(PAIR_MAIN));
}
static void
draw_sidebar(struct ui_state *s, const char *id, const char *dir)
{
int cols, y = 0;
cols = getmaxx(ui_side);
werase(ui_side);
sidebar_heading(&y, cols, "session");
mvwaddnstr(ui_side, y++, 0, id != NULL ? id : "-", cols - 1);
if (dir != NULL)
mvwaddnstr(ui_side, y++, 0, dir, cols - 1);
y++;
sidebar_heading(&y, cols, "transport");
mvwaddnstr(ui_side, y++, 0, s->endpoint != NULL ? s->endpoint : "script",
cols - 1);
y++;
sidebar_heading(&y, cols, "theme");
wattron(ui_side, COLOR_PAIR(PAIR_ACCENT));
mvwaddnstr(ui_side, y++, 0, s->theme.name, cols - 1);
wattron(ui_side, COLOR_PAIR(PAIR_MAIN));
y++;
sidebar_heading(&y, cols, "last turn");
mvwprintw(ui_side, y++, 0, "%ld ms", s->last_ms);
mvwprintw(ui_side, y++, 0, "%zu steps", s->last_steps);
mvwprintw(ui_side, y++, 0, "%zu tool calls", s->last_calls);
y++;
sidebar_heading(&y, cols, "widgets");
wattron(ui_side, COLOR_PAIR(PAIR_DIM));
mvwaddnstr(ui_side, y, 0, "none loaded", cols - 1);
wattrset(ui_side, A_NORMAL);
wnoutrefresh(ui_side);
}
static void
draw_status(struct ui_state *s)
{
int cols = getmaxx(ui_status);
werase(ui_status);
wattron(ui_status, COLOR_PAIR(PAIR_STATUS));
mvwhline(ui_status, 0, 0, ' ', cols);
mvwaddnstr(ui_status, 0, 1, "splinter " SP_VERSION, cols - 2);
{
char right[96];
snprintf(right, sizeof(right), "%s %ldms ", s->theme.name,
s->last_ms);
if ((int)strlen(right) + 1 < cols)
mvwaddnstr(ui_status, 0, cols - (int)strlen(right) - 1,
right, cols - 1);
}
wattroff(ui_status, COLOR_PAIR(PAIR_STATUS));
wnoutrefresh(ui_status);
}
static void
add_entry(struct ui_state *s, const char *role, const char *text)
{
struct entry *e;
if (s->n == MAX_ENTRIES) {
free(s->entries[0].role);
free(s->entries[0].text);
memmove(&s->entries[0], &s->entries[1],
sizeof(s->entries[0]) * (MAX_ENTRIES - 1));
s->n--;
}
e = &s->entries[s->n++];
e->role = strdup(role);
e->text = strdup(text != NULL ? text : "");
}
static void
layout(void)
{
int rows, cols, chat_w, side_w;
getmaxyx(stdscr, rows, cols);
chat_w = cols * 3 / 4;
if (cols - chat_w < 12)
chat_w = cols > 24 ? cols - 16 : cols;
side_w = cols - chat_w;
ui_hist = newwin(rows - 2, chat_w, 0, 0);
ui_side = newwin(rows - 2, side_w, 0, chat_w);
ui_input = newwin(1, cols, rows - 2, 0);
ui_status = newwin(1, cols, rows - 1, 0);
keypad(ui_input, TRUE);
}
int
sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name)
{
struct ui_state s;
struct sp_buf answer = { 0 }, err = { 0 };
char input[1024];
int rc = 0;
memset(&s, 0, sizeof(s));
s.endpoint = endpoint;
if (sp_buf_init(&answer) != 0 || sp_buf_init(&err) != 0)
return 1;
if (sp_theme_load(theme_name, &s.theme, &err) != 0) {
fprintf(stderr, "splinter: %s\n",
err.p != NULL ? err.p : "theme");
return 1;
}
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
curs_set(1);
init_colors(&s.theme);
layout();
add_entry(&s, "splinter", "ready. Enter sends, Ctrl-D quits.");
for (;;) {
struct timespec t0, t1;
int ch;
draw_history(&s);
draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL,
ag->sess != NULL ? ag->sess->dir : NULL);
draw_status(&s);
werase(ui_input);
mvwaddstr(ui_input, 0, 0, "> ");
wnoutrefresh(ui_input);
doupdate();
{
size_t len = 0;
wmove(ui_input, 0, 2);
wrefresh(ui_input);
for (;;) {
ch = wgetch(ui_input);
if (ch == '\n' || ch == '\r' || ch == KEY_ENTER)
break;
if (ch == 4) {
s.quit = 1;
break;
}
if (ch == 12)
break;
if (ch == 3) {
len = 0;
werase(ui_input);
mvwaddstr(ui_input, 0, 0, "> ");
wrefresh(ui_input);
continue;
}
if (ch == KEY_BACKSPACE || ch == 127 || ch == 8) {
if (len > 0) {
len--;
mvwaddch(ui_input, 0,
(int)len + 2, ' ');
wmove(ui_input, 0,
(int)len + 2);
wrefresh(ui_input);
}
continue;
}
if (ch >= 32 && ch < 127 &&
len < sizeof(input) - 1) {
input[len++] = (char)ch;
waddch(ui_input, (chtype)ch);
wrefresh(ui_input);
}
}
input[len] = '\0';
}
if (s.quit)
break;
if (input[0] == '\0')
continue;
add_entry(&s, "you", input);
draw_history(&s);
draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL,
ag->sess != NULL ? ag->sess->dir : NULL);
draw_status(&s);
werase(ui_input);
mvwaddstr(ui_input, 0, 0, " working...");
wrefresh(ui_input);
doupdate();
sp_buf_clear(&answer);
sp_buf_clear(&err);
clock_gettime(CLOCK_MONOTONIC, &t0);
rc = sp_agent_run(ag, input, &answer, &err);
clock_gettime(CLOCK_MONOTONIC, &t1);
s.last_ms = (long)((t1.tv_sec - t0.tv_sec) * 1000 +
(t1.tv_nsec - t0.tv_nsec) / 1000000);
s.last_steps = ag->stats.steps;
s.last_calls = ag->stats.tool_calls;
if (rc == 0)
add_entry(&s, "agent", answer.p);
else
add_entry(&s, "error", err.p != NULL ? err.p :
"turn failed");
}
endwin();
sp_buf_free(&answer);
sp_buf_free(&err);
return rc;
}