aboutsummaryrefslogtreecommitdiffstats
path: root/src/screen.c
diff options
context:
space:
mode:
authorsl0p <sl0p@sl0p.foo>2026-08-13 22:35:57 +0200
committersl0p <sl0p@sl0p.foo>2026-08-13 22:35:57 +0200
commitd2940b9643496ed898450f8bd2c640aeb6f8b7a1 (patch)
treef98ca5f6c4fb2ba632c08edaab09c917d199316c /src/screen.c
parentchore: stop tracking __pycache__ (diff)
downloadslosh-d2940b9643496ed898450f8bd2c640aeb6f8b7a1.tar.gz
slosh-d2940b9643496ed898450f8bd2c640aeb6f8b7a1.tar.xz
slosh-d2940b9643496ed898450f8bd2c640aeb6f8b7a1.zip
sl0ptty: M1 — layout tree, splits, focus, frames, and the hit list in use
app_t owns the tree, focus, the prefix and what a key means; the interactive client and the headless driver are now both thin front ends over it, so there is exactly one implementation of every command. Layout is the pure function D6 promised: layout(tree, rect) recomputed every frame, remainder spread across children so nothing drifts, and splitting in a direction that already exists grows that split rather than nesting — three splits give three equal columns, not 1/2 + 1/4 + 1/4. Chrome: rounded frames, aspect-corrected gap, centred titles. The split button is budgeted BEFORE the title and hit-tested from the rect it was drawn in, which is the fix the fork needed four attempts to land. Every painted element registers (rect, action) as it is painted; a click is a lookup. Mouse into a pane is translated to pane-local coordinates. Three bugs the harness caught, none of which unit tests would have: - one click on the split button split twice: press *and* release both fired. Chrome now acts on press; forwarding to a pane still gets every event. - callbacks were registered as &fn, but the option is pointer-typed, so the stored 'callback' was a stack address. Any OSC 2 or DSR jumped into a dead frame. Now a hard segfault turned into two regression tests. - a keystroke that changed nothing still emitted a cursor hide/show pair. The cursor is hidden lazily now, so an idle frame costs zero bytes. 96 checks green in 6s (40 unit, 23 screen, 33 layout), plus 9 live.
Diffstat (limited to 'src/screen.c')
-rw-r--r--src/screen.c52
1 files changed, 47 insertions, 5 deletions
diff --git a/src/screen.c b/src/screen.c
index 9d31640..e36f461 100644
--- a/src/screen.c
+++ b/src/screen.c
@@ -119,13 +119,36 @@ void screen_put_utf8(screen_t *s, uint16_t x, uint16_t y, const char *txt,
cell_t *c = screen_at(s, x, y);
if (!c) return;
if (len > sizeof c->text) len = sizeof c->text;
+ memset(c->text, 0, sizeof c->text);
memcpy(c->text, txt, len);
c->len = (uint8_t)len;
+ c->width = 1;
c->fg = fg;
c->bg = bg;
c->attrs = attrs;
}
+static size_t u8_len(unsigned char b) {
+ if (b < 0x80) return 1;
+ if ((b & 0xe0) == 0xc0) return 2;
+ if ((b & 0xf0) == 0xe0) return 3;
+ if ((b & 0xf8) == 0xf0) return 4;
+ return 1;
+}
+
+uint16_t screen_text(screen_t *s, uint16_t x, uint16_t y, const char *txt,
+ color_t fg, color_t bg, uint16_t attrs) {
+ uint16_t n = 0;
+ for (const char *p = txt; *p;) {
+ size_t len = u8_len((unsigned char)*p);
+ if (x + n >= s->cols) break;
+ screen_put_utf8(s, (uint16_t)(x + n), y, p, len, fg, bg, attrs);
+ p += len;
+ n++;
+ }
+ return n; /* cells written, so a caller can lay out what follows */
+}
+
static bool color_eq(color_t a, color_t b) {
if (a.set != b.set) return false;
if (!a.set) return true;
@@ -161,11 +184,16 @@ static bool style_eq(const cell_t *a, const cell_t *b) {
void screen_flush(screen_t *s, int fd) {
s->out_len = 0;
- out_str(s, "\x1b[?25l"); /* hide cursor while painting */
+
+ /* The cursor is hidden lazily, on the first cell we actually repaint, so a
+ * frame with no changes emits nothing at all rather than a hide/show pair.
+ * A keystroke that changes nothing should cost zero bytes. */
+ bool painted = false;
if (s->force_full) {
- out_str(s, "\x1b[0m\x1b[H\x1b[2J");
+ out_str(s, "\x1b[?25l\x1b[0m\x1b[H\x1b[2J");
memset(s->prev, 0, (size_t)s->cols * s->rows * sizeof(cell_t));
+ painted = true;
}
bool have_pos = false, have_style = false;
@@ -179,6 +207,10 @@ void screen_flush(screen_t *s, int fd) {
if (c->width == 0) continue; /* tail of a wide cell: never painted */
if (cell_eq(c, p)) continue;
+ if (!painted) {
+ out_str(s, "\x1b[?25l");
+ painted = true;
+ }
if (!have_pos || cx != x || cy != y) {
out_fmt(s, "\x1b[%u;%uH", y + 1, x + 1);
cx = x;
@@ -199,9 +231,19 @@ void screen_flush(screen_t *s, int fd) {
}
}
- if (s->cursor_visible && s->cursor_x < s->cols && s->cursor_y < s->rows) {
- out_fmt(s, "\x1b[%u;%uH", s->cursor_y + 1, s->cursor_x + 1);
- out_str(s, "\x1b[?25h");
+ bool cursor_moved = s->cursor_visible != s->shown_cursor_visible ||
+ s->cursor_x != s->shown_cursor_x ||
+ s->cursor_y != s->shown_cursor_y;
+ if (painted || cursor_moved) {
+ if (s->cursor_visible && s->cursor_x < s->cols && s->cursor_y < s->rows) {
+ out_fmt(s, "\x1b[%u;%uH", s->cursor_y + 1, s->cursor_x + 1);
+ out_str(s, "\x1b[?25h");
+ } else if (painted) {
+ out_str(s, "\x1b[?25l");
+ }
+ s->shown_cursor_visible = s->cursor_visible;
+ s->shown_cursor_x = s->cursor_x;
+ s->shown_cursor_y = s->cursor_y;
}
s->force_full = false;