diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 15:58:05 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 17:53:43 -0300 |
| commit | e9f9f65ace7650a02cee6bf2b2eb466522485834 (patch) | |
| tree | caf5b7a2344b8da002f3952e3225e0db38b492fc /addons | |
| parent | 75e4787be773c8cbfd2e60e59d409b9556898365 (diff) | |
| download | pardes-e9f9f65ace7650a02cee6bf2b2eb466522485834.tar.gz pardes-e9f9f65ace7650a02cee6bf2b2eb466522485834.zip | |
kinds #36, the five kinds on ABI 3: fake.c with every function (view painting each view's size, search through found, FakeJob cancel) and PA_PICTURES, snake, life and image over the module's prefix and open(path, bytes), image without a record (a Restore reads its file), and pdf.c ported mechanically (tick into pa_tick with its [P/N] prefix, a table of the views it is told, its strip laid out for the view its last key or click came from, the text layer painted for a window of glyphs and read on the tick while some view shows text, a body read keeping nothing, search through found, the record its fit, tint and place, PA_PICTURES); the goldens moved once, said in the report: the art words from the first frame, fake's view line, a pane ticked when opened or loaded, a view told after a frame re-wrapped its tag
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'addons')
| -rw-r--r-- | addons/fake/fake.c | 52 | ||||
| -rw-r--r-- | addons/fake/fake.out | 72 | ||||
| -rw-r--r-- | addons/fake/fake.script | 7 | ||||
| -rw-r--r-- | addons/image/image.zig | 42 | ||||
| -rw-r--r-- | addons/life/life.out | 1 | ||||
| -rw-r--r-- | addons/life/life.zig | 10 | ||||
| -rw-r--r-- | addons/pdf/pdf.c | 121 | ||||
| -rw-r--r-- | addons/pdf/pdf.out | 65 | ||||
| -rw-r--r-- | addons/snake/snake.out | 12 | ||||
| -rw-r--r-- | addons/snake/snake.zig | 9 |
10 files changed, 242 insertions, 149 deletions
diff --git a/addons/fake/fake.c b/addons/fake/fake.c index 81a78741..648dcace 100644 --- a/addons/fake/fake.c +++ b/addons/fake/fake.c @@ -5,7 +5,8 @@ * cc -shared -fPIC -I src/kinds -o fake.so addons/fake/fake.c * * A pane of it counts: keys and clicks and ticks and words each add to a - * count it paints, dumps and answers `where` with; `:N` sets it. */ + * count it paints, dumps and answers `where` with; `:N` sets it. It paints + * which view it is painted for and that view's size as `view` told it. */ #include "pardes_addon.h" /* The loader's tests build it wrong on purpose: another ABI, no export, @@ -26,6 +27,7 @@ typedef struct { unsigned count, ticks; char last[16]; uint8_t pixels[2 * 2 * 4]; + uint16_t cols[8], rows[8]; /* each view's, by id */ } fake; static int init(const pa_api *a) { @@ -45,11 +47,17 @@ static void *open(const char *path, const uint8_t *bytes, uint64_t len) { static void close(void *st) { (void)st; } -static uint64_t tick(void *st, uint64_t now, uint32_t flags) { +static void tick(void *st, pa_tick *t) { fake *f = st; - (void)flags; f->ticks++; - return f->ticks < 3 ? now + 100000000u : 0; /* ten a second, three times */ + t->next = f->ticks < 3 ? t->now + 100000000u : 0; /* ten a second, three times */ + memcpy(t->prefix, "fake", 4), t->prefix_len = 4; +} + +static void view(void *st, const pa_view *v) { + fake *f = st; + if (v->id == 0 || v->id > 8) return; + f->cols[v->id - 1] = v->cols, f->rows[v->id - 1] = v->rows; } static void num(char *out, unsigned n) { @@ -66,8 +74,16 @@ static void paint(void *st, const pa_window *w, pa_paint *out) { num(line + 6, f ? f->count : 0); pa_put(w, 0, 0, line, PA_INDEX(2), PA_DEFAULT, 0); if (f && f->last[0]) pa_put(w, 1, 0, f->last, PA_DEFAULT, PA_DEFAULT, 0); - const char *p = "fake"; - for (out->prefix_len = 0; p[out->prefix_len]; out->prefix_len++) out->prefix[out->prefix_len] = p[out->prefix_len]; + if (f && w->view >= 1 && w->view <= 8) { + char v[48] = "view "; + v[5] = (char)('0' + w->view), v[6] = ' '; + num(v + 7, f->cols[w->view - 1]); + char *e = v + strlen(v); + *e++ = 'x'; + num(e, f->rows[w->view - 1]); + if (w->glyphs) memcpy(v + strlen(v), " glyphs", 8); + pa_put(w, 3, 0, v, PA_DEFAULT, PA_DEFAULT, 0); + } if (f && out->cap > 0 && w->rows > 2) { pa_placement *pl = &out->placements[out->n++]; *pl = (pa_placement){.rgba = f->pixels, .w = 2, .h = 2, .src = {0, 0, 2, 2}, .dst = {0, 2, 2, 1}, .key = f->count}; @@ -131,7 +147,11 @@ static int ctl(void *st, const char *line, uint32_t n, char *reply, uint32_t cap f->count += by ? by : 1; return PA_OK; } - if (n >= 7 && line[4] == 'J') return api->job("abc", 3) ? PA_OK : 1; + if (n >= 7 && line[4] == 'J') { /* FakeJob cancel: its answer dropped */ + uint32_t token = api->job("abc", 3); + if (token && n > 8) api->cancel(token); + return token ? PA_OK : 1; + } const char *no = "no such fake word"; uint32_t k = 0; while (no[k] && k + 1 < cap) { reply[k] = no[k]; k++; } @@ -161,6 +181,18 @@ static void job_done(void *st, uint32_t token, const uint8_t *out, uint32_t n) { f->count += n; } +/* `/`: a row naming the count, at once. */ +static void search(void *st, const char *pat, uint32_t n) { + fake *f = st; + char row[64] = "fake:1 count "; + num(row + 13, f->count); + size_t k = strlen(row); + row[k++] = ' '; + for (uint32_t i = 0; i < n && k < sizeof row - 1; i++) row[k++] = pat[i]; + row[k++] = '\n'; + api->found(row, (uint32_t)k); +} + #ifndef FAKE_NO_DUMP static uint32_t dump(void *st, uint8_t *buf, uint32_t n) { fake *f = st; @@ -179,18 +211,18 @@ static int load(void *st, const uint8_t *buf, uint32_t n, uint32_t version) { static const pa_word words[] = { PA_WORD(FAKE_WORD, "zf", PA_ARG_NONE, 1, "a fake pane, counting"), PA_WORD("FakeBump", 0, PA_ARG_OPTIONAL, 0, "add to the count"), - PA_WORD("FakeJob", 0, PA_ARG_NONE, 0, "count a job's answer"), + PA_WORD("FakeJob", 0, PA_ARG_OPTIONAL, 0, "count a job's answer (cancel: drop it)"), }; const pa_ops pardes_addon = { - .head = PA_ADDON(FAKE_NAME, 1, ".fake", words, 3), + .head = PA_ADDON(FAKE_NAME, 1, PA_PICTURES, ".fake", words, 3), #ifdef FAKE_ABI .head.abi = FAKE_ABI, #endif .init = init, .open = open, .close = close, .tick = tick, .paint = paint, .key = key, .mouse = mouse, .body_len = body_len, .body = body, .address = address, .where = where, .ctl = ctl, .ctl_read = ctl_read, - .job_run = job_run, .job_done = job_done, + .job_run = job_run, .job_done = job_done, .search = search, .view = view, #ifndef FAKE_NO_DUMP .dump = dump, .load = load, #endif diff --git a/addons/fake/fake.out b/addons/fake/fake.out index 3693f2c3..1699ffa6 100644 --- a/addons/fake/fake.out +++ b/addons/fake/fake.out @@ -1,71 +1,117 @@ > seed 7 > size 60 20 window 58x7 -tag fake +tag fake petscii:on palette:commodore ascii:on |count 0 | | -| +|view 1 58x7 glyphs | | | placement 2x2 src 0,0 2x2 over 0,2 2x1 key 4b3cd99b86ea3486 > key ab -tag fake +tag fake petscii:on palette:commodore ascii:on |count 2 |kb | -| +|view 1 58x7 glyphs | | | placement 2x2 src 0,0 2x2 over 0,2 2x1 key fdcaab9076ad067e > click 3 1 -tag fake +tag fake petscii:on palette:commodore ascii:on |count 3 |m3 | -| +|view 1 58x7 glyphs | | | placement 2x2 src 0,0 2x2 over 0,2 2x1 key 71e39225f0cec2a5 > FakeBump 10 -tag fake +tag fake petscii:on palette:commodore ascii:on |count 13 |m3 | -| +|view 1 58x7 glyphs | | | placement 2x2 src 0,0 2x2 over 0,2 2x1 key 54740e23065232cf > FakeJob -tag fake +tag fake petscii:on palette:commodore ascii:on |count 19 |m3 | +|view 1 58x7 glyphs +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716 +> FakeJob cancel +tag fake petscii:on palette:commodore ascii:on +|count 19 +|m3 | +|view 1 58x7 glyphs | | | placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716 -> tick 100 -tag fake +> view 2 on +tag fake petscii:on palette:commodore ascii:on +|count 19 +|m3 +| +|view 2 58x7 +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716 +> size 70 30 +window 68x12 +tag fake petscii:on palette:commodore ascii:on +|count 19 +|m3 +| +|view 2 68x12 +| +| +| +| +| +| +| +| +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716 +> view 1 off +tag fake petscii:on palette:commodore ascii:on |count 19 |m3 | +|view 1 58x7 glyphs | | | +placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716 +> tick 100 +tag fake petscii:on palette:commodore ascii:on +|count 19 +|m3 +| +|view 1 58x7 glyphs +| +| | placement 2x2 src 0,0 2x2 over 0,2 2x1 key 540d6c1b4eee5716 > settle -tag fake +tag fake petscii:on palette:commodore ascii:on |count 19 |m3 | -| +|view 1 58x7 glyphs | | | diff --git a/addons/fake/fake.script b/addons/fake/fake.script index 35407cd7..58fc9f4c 100644 --- a/addons/fake/fake.script +++ b/addons/fake/fake.script @@ -5,5 +5,12 @@ key ab click 3 1 FakeBump 10 FakeJob +# A job asked and cancelled: its answer is dropped, the count stays. +FakeJob cancel +# A second view, a window's (pictures): the kind is told its size and +# paints for it; then the first, a terminal's, as it was. +view 2 on +size 70 30 +view 1 off tick 100 settle diff --git a/addons/image/image.zig b/addons/image/image.zig index fbcc501d..88ae1e2d 100644 --- a/addons/image/image.zig +++ b/addons/image/image.zig @@ -1,7 +1,7 @@ //! image.zig: pictures as a pardes addon (.png .jpg .gif .bmp .ppm .pgm .tga). -//! A pane keeps its file's bytes (its record in a dump) and decodes them on -//! a job with stb_image (stb.c), at most max_dim pixels a side; the core -//! shows the RGBA fitted to the pane (or as glyph art, its Petscii word). +//! A pane decodes its file's bytes on a job with stb_image (stb.c), at most +//! max_dim pixels a side; the core shows the RGBA fitted to the pane (or as +//! glyph art, its Petscii word). It has no record: a Restore reads the file. const std = @import("std"); const addon = @import("addon"); @@ -31,29 +31,26 @@ fn info(bytes: []const u8) ?[2]u32 { } pub const Image = struct { - raw: []const u8 = &.{}, // the file's bytes w: u32 = 0, h: u32 = 0, rgba: ?[]const u8 = null, // w x h, once its job is done - pub const head: addon.Head = .{ .name = "image", .version = 1, .exts = ".png .jpg .jpeg .gif .bmp .ppm .pgm .tga" }; + pub const head: addon.Head = .{ .name = "image", .version = 1, .exts = ".png .jpg .jpeg .gif .bmp .ppm .pgm .tga", .pictures = true }; - /// The bytes kept, and decoded on a job. - fn keep(im: *Image, bytes: []const u8) void { - im.raw = addon.arena.dupe(u8, bytes) catch return; - if (bytes.len > 0) _ = addon.job(im.raw); - } - - /// One it cannot read (corrupt, truncated, not what its name says) is - /// refused, rather than shown as a blank pane. - pub fn open(im: *Image, bytes: ?[]const u8) void { + /// Decoded on a job; one it cannot read (corrupt, truncated, not what + /// its name says) is refused, rather than shown as a blank pane. + pub fn open(_: *Image, _: ?[]const u8, bytes: ?[]const u8) void { const b = bytes orelse return; if (info(b) == null) return addon.say("not an image pardes can read", .err); - im.keep(b); + _ = addon.job(b); + } + + /// The core follows it with its art words and the path. + pub fn prefix(_: *const Image, w: *std.Io.Writer) !void { + try w.writeAll("img"); } pub fn paint(im: *const Image, w: addon.Window, out: addon.Paint) void { - out.prefix("img", .{}); // the core follows it with its art words and the path const rgba = im.rgba orelse return; out.place(.{ .rgba = rgba.ptr, .w = im.w, .h = im.h, .src = .{ .w = im.w, .h = im.h }, .dst = .{ .w = w.cols, .h = w.rows } }); } @@ -90,19 +87,6 @@ pub const Image = struct { im.rgba = addon.arena.dupe(u8, out[8..]) catch return; im.w, im.h = .{ w, h }; } - - /// The record is the file's bytes. - pub fn dump(im: *const Image, buf: []u8) usize { - if (im.raw.len <= buf.len) @memcpy(buf[0..im.raw.len], im.raw); - return im.raw.len; - } - - /// On a state with no picture (a Restore's), the record's; one open - /// made from the file as it is now (it changed on disk) keeps that one. - pub fn load(im: *Image, bytes: []const u8, _: u32) bool { - if (im.raw.len == 0) im.keep(bytes); - return true; - } }; pub export const pardes_addon = addon.ops(Image); diff --git a/addons/life/life.out b/addons/life/life.out index 6e5b3f73..fb5a7b13 100644 --- a/addons/life/life.out +++ b/addons/life/life.out @@ -175,7 +175,6 @@ tag gen 3 alive 130 paused |........#.#...##.###........#... |.......###....#...##.....######. |.....##.##...................##. -|.....###....##.........#....###. > key r tag gen 0 alive 130 paused |.#.#.......##......#...#..#..... diff --git a/addons/life/life.zig b/addons/life/life.zig index 2ef26c56..2f459e07 100644 --- a/addons/life/life.zig +++ b/addons/life/life.zig @@ -22,7 +22,7 @@ const Life = extern struct { pub const head: addon.Head = .{ .name = "life", .version = 1, .words = &.{.{ .name = "Life", .opens = true, .doc = "a pane of Conway's Game of Life" }} }; - pub fn open(s: *Life, _: ?[]const u8) void { + pub fn open(s: *Life, _: ?[]const u8, _: ?[]const u8) void { s.seed = addon.seed(); s.reseed(); } @@ -47,9 +47,11 @@ const Life = extern struct { return true; } - pub fn paint(s: *Life, w: addon.Window, p: addon.Paint) void { - p.prefix("gen {d} alive {d}{s}", .{ s.gen, s.alive(), if (s.paused) " paused" else "" }); - for (s.cells, 0..) |row, y| for (row, 0..) |on, x| { + pub fn prefix(s: *const Life, w: *std.Io.Writer) !void { + try w.print("gen {d} alive {d}{s}", .{ s.gen, s.alive(), if (s.paused) " paused" else "" }); + } + + pub fn paint(s: *const Life, w: addon.Window, _: addon.Paint) void { for (s.cells, 0..) |row, y| for (row, 0..) |on, x| { const cursor = w.focused and x == s.cx and y == s.cy; w.put(x, y, if (on) "#" else ".", .{ .attrs = .{ .reverse = cursor } }); }; diff --git a/addons/pdf/pdf.c b/addons/pdf/pdf.c index 5c156dec..5a0448b1 100644 --- a/addons/pdf/pdf.c +++ b/addons/pdf/pdf.c @@ -13,7 +13,8 @@ * rows, repainted on the loop and said as rows (`from`, `dirty_*`). The * body is the text of the page shown; `/` lists hits as `file.pdf:P:H`; * PdfSections lists the outline; a click on a link follows it. The record - * in a dump is the file's bytes, with its fit, tint and place. */ + * in a dump is its fit, tint and place. The strip is laid out for one view + * at a time: the one its last key or click came from, else the first told. */ #include <math.h> #include <pthread.h> #include <stdatomic.h> @@ -37,6 +38,7 @@ #define FLING_GRAIN 64u /* a fling's band starts and ends on these rows */ #define MARKS 1024 /* highlights on a page */ #define RECORD_VERSION 1u +#define VIEWS 8 /* views a pane is told of, by id */ static const pa_api *api; @@ -51,7 +53,6 @@ static void *bridge_realloc(void *user, void *p, size_t n) { (void)user; return static void bridge_free(void *user, void *p) { (void)user; unheap(p); } static int init(const pa_api *a) { - if (a->size < sizeof(pa_api)) return 1; /* a core before 2.2: no heap, no amount */ api = a; static const pardes_pdf_allocator hooks = {0, bridge_alloc, bridge_realloc, bridge_free}; return pardes_pdf_set_allocator(&hooks) == PARDES_PDF_OK ? PA_OK : 1; @@ -213,7 +214,7 @@ typedef struct pdf { uint64_t height; /* the strip's */ uint32_t vw, vh; /* the window the strip is laid out for, in pixels */ int laid, laid_fit, fit; - pa_view view; + pa_view view, views[VIEWS]; /* the one laid out for, and each told */ double top; /* the strip's pixel at the window's top */ int want_page; /* -1, else laid out next at this page, want_frac down it */ double want_frac, want_x, want_y; /* or an outline's place there (NAN: none) */ @@ -831,6 +832,13 @@ static void scroll_by(pdf *st, double px) { /* A terminal without graphics: the page's text layer, a page a step. */ static int text_mode(const pdf *st) { return st->view.glyphs != 0; } +/* Some view told shows it as text: the page's text is read for it. */ +static int any_text(const pdf *st) { + for (int i = 0; i < VIEWS; i++) + if (st->views[i].cols && st->views[i].glyphs) return 1; + return text_mode(st); +} + /* Down (dir 1) or up `n` steps of `px` pixels, or of a page as text. */ static void move(pdf *st, int dir, double px, uint32_t n) { if (text_mode(st)) go_page(st, st->page + dir * (int)n); @@ -1049,9 +1057,11 @@ static void close(void *v) { st->d = 0; } -static uint64_t tick(void *v, uint64_t now, uint32_t flags) { +static void tick(void *v, pa_tick *t) { pdf *st = v; - if (!st || !st->d) return 0; + uint64_t now = t->now; + uint32_t flags = t->flags; + if (!st || !st->d) return; /* A fling is a window's travel or more between frames; a tick a page's * landing made (no turn since) neither starts nor ends one. */ if (flags & PA_FRAMED) st->framed = 1; @@ -1061,7 +1071,6 @@ static uint64_t tick(void *v, uint64_t now, uint32_t flags) { st->fling = st->vh && st->travel >= st->vh; } st->landed = 0, st->since_tick = 0; - api->view(&st->view); layout(st); to_page(st, page_at(st, st->top)); /* white tinted is the theme's paper */ @@ -1076,25 +1085,37 @@ static uint64_t tick(void *v, uint64_t now, uint32_t flags) { } if (st->query_len && st->hits_page != st->page) search_page(st, st->page, 1); if (st->reveal) reveal_hit(st); - if (text_mode(st)) { - uint32_t len; - page_text(st, &len); - } else { - reconcile(st); - } + uint32_t len; + if (any_text(st)) page_text(st, &len); + if (!text_mode(st)) reconcile(st); uint64_t next = st->hover_wait == 2 ? st->hover_at : 0; /* flinging: a tick when it rests, for the whole pages */ if (st->fling && (!next || now + FLING_NS < next)) next = now + FLING_NS; - return next; + t->next = next; + int n = snprintf(t->prefix, sizeof t->prefix, "[%d/%d]", st->page + 1, st->pages); + t->prefix_len = n > 0 ? (uint32_t)n : 0; +} + +/* A view told: kept by its id, laid out for when it is the one (or none is). */ +static void view(void *v, const pa_view *pv) { + pdf *st = v; + if (!st || pv->id < 1 || pv->id > VIEWS) return; + st->views[pv->id - 1] = *pv; + if (pv->id == st->view.id || !st->view.cols) st->view = *pv; + for (int i = 0; i < VIEWS && !st->view.cols; i++) + if (st->views[i].cols) st->view = st->views[i]; +} + +/* An input from view `id`: the strip is laid out for it from now on. */ +static void adopt(pdf *st, uint8_t id) { + if (id >= 1 && id <= VIEWS && st->views[id - 1].cols) st->view = st->views[id - 1]; } static void paint(void *v, const pa_window *win, pa_paint *out) { const pdf *st = v; - int n = st && st->d ? snprintf(out->prefix, sizeof out->prefix, "[%d/%d]", st->page + 1, st->pages) : 0; - out->prefix_len = n > 0 ? (uint32_t)n : 0; if (!st || !st->laid) return; out->pointer = st->hover_page >= 0 && st->hover_link ? PA_POINTER_LINK : PA_POINTER_DEFAULT; - if (text_mode(st)) { /* the page's text layer, its lines from the top */ + if (win->glyphs) { /* the page's text layer, its lines from the top */ /* (read on the tick: paint changes nothing) */ uint32_t len = st->text_page == st->page ? st->text_len : 0, at = 0; const char *t = st->text; @@ -1154,7 +1175,7 @@ static void paint(void *v, const pa_window *win, pa_paint *out) { } out->n++; } - if (st->height) { /* the rail's thumb; init refused a core before 2.1 */ + if (st->height) { /* the rail's thumb */ out->at = (float)(st->top / (double)st->height); out->span = (float)((double)st->vh / (double)st->height); } @@ -1199,6 +1220,7 @@ static int cancel(pdf *st) { static int key(void *v, const pa_key *k) { pdf *st = v; if (!st || !st->d) return 0; + adopt(st, k->view); uint32_t cp = k->cp, cell = st->view.cell_h ? st->view.cell_h : 16; int ctrl = k->ctrl; if (cp == 0x1b) return k->shift ? 0 : cancel(st); /* Shift-Esc: the editor's, leaving at once */ @@ -1259,6 +1281,7 @@ static int key(void *v, const pa_key *k) { static int mouse(void *v, const pa_mouse *m) { pdf *st = v; if (!st || !st->d) return 0; + adopt(st, m->view); uint32_t cell = st->view.cell_h ? st->view.cell_h : 16; if (m->button == PA_WHEEL_UP || m->button == PA_WHEEL_DOWN) { /* a notch a row, a touchpad's fraction of one its fraction; as text a @@ -1341,19 +1364,21 @@ static int mouse(void *v, const pa_mouse *m) { /* ---- the body, places, search ---- */ -static uint64_t body_len(void *v) { - uint32_t n = 0; - if (v && ((pdf *)v)->d) page_text(v, &n); - return n; -} - +/* The page's text: what a tick read for a view's paint, else MuPDF's, + * not kept (a read changes nothing a frame shows). */ static uint32_t body(void *v, uint64_t off, char *buf, uint32_t n) { - uint32_t len = 0; - if (!v || !((pdf *)v)->d) return 0; - const char *t = page_text(v, &len); - return pa_copy(t, len, off, buf, n); + pdf *st = v; + if (!st || !st->d) return 0; + if (st->text_page == st->page) return buf ? pa_copy(st->text, st->text_len, off, buf, n) : st->text_len; + pardes_pdf_text t; + if (pardes_pdf_page_text(st->d->base, st->page, &t) != PARDES_PDF_OK) return 0; + uint32_t k = buf ? pa_copy(t.data, t.len, off, buf, n) : (uint32_t)t.len; + pardes_pdf_drop_text(st->d->base, t.handle); + return k; } +static uint64_t body_len(void *v) { return body(v, 0, 0, 0); } + /* `text` as a number from `*i`, stopped at `end`: 0 when none is there. */ static int number(const char *t, uint32_t *i, uint32_t end, uint64_t *out) { uint32_t from = *i; @@ -1570,26 +1595,29 @@ static uint32_t where(void *v, char *buf, uint32_t n) { return k; } -/* `/`: every hit, `file.pdf:P:H pattern` a row, at most MAX_HITS. */ -static uint32_t search(void *v, const char *pat, uint32_t n, char *buf, uint32_t cap) { +/* `/`: every hit, `file.pdf:P:H pattern` a row, at most MAX_HITS, found + * at once (on the loop: 7c moves it to a job). */ +static void search(void *v, const char *pat, uint32_t n) { pdf *st = v; - if (!st || !st->d || n == 0 || n > 255) return 0; + if (!st || !st->d || n == 0 || n > 255) return; if (st->query_len != n || memcmp(st->query, pat, n)) { memcpy(st->query, pat, n), st->query_len = n; drop_hits(st); st->hit = 0, st->reveal = 1; } - uint32_t shown = n > 200 ? 200 : n, len = 0, rows = 0; + uint32_t shown = n > 200 ? 200 : n, len = 0, rows = 0, cap = MAX_HITS * 512; while (shown > 0 && shown < n && ((unsigned char)pat[shown] & 0xc0) == 0x80) shown--; + char *buf = heap(cap); + if (!buf) return; for (int p = 0; p < st->pages && rows < MAX_HITS; p++) { uint32_t hits = search_page(st, p, 0); for (uint32_t h = 0; h < hits && rows < MAX_HITS; h++, rows++) { int k = snprintf(buf + len, cap - len, "%s:%d:%u %.*s\n", st->name, p + 1, h + 1, (int)shown, pat); - if (k < 0 || (uint32_t)k >= cap - len) return len; - len += (uint32_t)k; + if (k > 0 && (uint32_t)k < cap - len) len += (uint32_t)k; } } - return len; + api->found(buf, len); + unheap(buf); } /* ---- words ---- */ @@ -1636,7 +1664,7 @@ static uint32_t ctl_read(void *v, char *buf, uint32_t n) { return k > 0 && (uint32_t)k < n ? (uint32_t)k : 0; } -/* ---- the record: its fit, tint and place, then the file's bytes ---- */ +/* ---- the record: its fit, tint and place ---- */ typedef struct record { uint32_t version, fit, tint, page; @@ -1645,30 +1673,23 @@ typedef struct record { static uint32_t dump(void *v, uint8_t *buf, uint32_t n) { pdf *st = v; - if (!st || !st->d) return 0; - size_t len = 0; - const unsigned char *bytes = pardes_pdf_bytes(st->d->base, &len); - if (!bytes || len > 0xffffffffu - sizeof(record)) return 0; - uint32_t total = (uint32_t)(sizeof(record) + len); - if (total > n) return total; + if (!st || !st->d || n < sizeof(record)) return 0; record r = {RECORD_VERSION, (uint32_t)st->fit, (uint32_t)st->tint, (uint32_t)st->page, 0}; if (st->laid && st->ph[st->page]) r.frac = (st->top - (double)st->y[st->page]) / st->ph[st->page]; memcpy(buf, &r, sizeof r); - memcpy(buf + sizeof r, bytes, len); - return total; + return sizeof r; } -/* On a state open made from its file (a Restore's, from nothing): the - * document from the record. On one open made from the file as it is now - * (it changed on disk): only its fit, tint and place. */ +/* On a state open made from its file as it is (a Restore's, a change on + * disk): its fit, tint and place. A record of before 3 has the file's + * bytes after them, unread. */ static int load(void *v, const uint8_t *buf, uint32_t n, uint32_t version) { pdf *st = v; record r; (void)version; - if (!st || n < sizeof r) return 1; + if (!st || !st->d || n < sizeof r) return 1; memcpy(&r, buf, sizeof r); if (r.version != RECORD_VERSION || r.fit > 1 || r.tint > 2) return 1; - if (!st->d && !reopen_bytes(st, buf + sizeof r, n - sizeof r)) return 1; st->fit = (int)r.fit, st->tint = (int)r.tint; st->page = (int)(r.page < (uint32_t)st->pages ? r.page : (uint32_t)st->pages - 1); st->want_page = st->page, st->want_frac = r.frac; @@ -1682,8 +1703,8 @@ static const pa_word words[] = { }; const pa_ops pardes_addon = { - .head = PA_ADDON("pdf", 1, ".pdf", words, 3), + .head = PA_ADDON("pdf", 1, PA_PICTURES, ".pdf", words, 3), .init = init, .open = open, .close = close, .tick = tick, .paint = paint, .key = key, .mouse = mouse, .body_len = body_len, .body = body, .address = address, .where = where, .ctl = ctl, .ctl_read = ctl_read, - .job_run = job_run, .job_done = job_done, .dump = dump, .load = load, .search = search, + .job_run = job_run, .job_done = job_done, .dump = dump, .load = load, .search = search, .view = view, }; diff --git a/addons/pdf/pdf.out b/addons/pdf/pdf.out index 23849db2..a6ea68b3 100644 --- a/addons/pdf/pdf.out +++ b/addons/pdf/pdf.out @@ -1,18 +1,17 @@ > open docs/design.pdf -window 78x10 -tag [1/16] -| -| -| -| -| -| -| -| -| -| +window 78x9 +tag [1/16] petscii:on palette:commodore ascii:on docs/design.pdf +|Pardes: A Text Environment +|Architecture and implementation of the rewrite +|Abstract. Pardes is a text environment in the acme tradition: columns of panes +|body a live terminal, a file, an image or a PDF. It is one program with five t +|SDL3 window, a browser tab driven by vanilla JavaScript, an AppKit application +|— where the prototype had three parallel implementations of one editor. Two se +|is a state machine over plain values: Event in, Surface and a queue of Effect +|said in those types exists in pardes. Downward, host.VTable is twenty-one opti +|one answered in-process, so the zero-method host is both the test harness and > graphics on -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -25,7 +24,7 @@ tag [1/16] placement 794x1123 src 0,0 794x184 over 0,0 78x9 key 3017bc3ce49f9593 at 0,0 624x144 > size 60 20 window 58x7 -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -35,7 +34,7 @@ tag [1/16] | placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key j -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -45,7 +44,7 @@ tag [1/16] | placement 794x1123 src 0,27 794x193 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key 3j -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -55,7 +54,7 @@ tag [1/16] | placement 794x1123 src 0,109 794x193 over 0,0 58x7 key 71e39225f0cec2a5 at 0,0 464x112 > key G -tag [16/16] +tag [16/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -65,7 +64,7 @@ tag [16/16] | placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 1182c0220e1b5a25 at 0,0 464x112 > key gg -tag [1/16] +tag [1/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -75,7 +74,7 @@ tag [1/16] | placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 5400263dddff499a at 0,0 464x112 > key 3gg -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -85,7 +84,7 @@ tag [3/16] | placement 794x1123 src 0,0 794x192 over 0,0 58x7 key 1da05d9257cc481d at 0,0 464x112 > PdfFit -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -93,11 +92,11 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 1da05d9257cc481d at 192,0 79x112 +placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key acaee8c0ff4f63a6 at 192,0 79x112 > ctl =fit:height tint:filtered > PdfTint full -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -105,11 +104,11 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 1da05d9257cc481d at 192,0 79x112 +placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 11acd08483d3011b at 192,0 79x112 > ctl =fit:height tint:full > PdfTint loud -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf |ent: PdfTint takes disabled, filtered or full, not "loud" | | @@ -117,9 +116,9 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x963 over 0,1 58x6 key 1da05d9257cc481d at 192,0 79x96 +placement 794x1123 src 0,0 794x1123 over 0,1 58x6 key a790cfe920570b8b at 198,0 67x96 > key <esc> -tag [3/16] +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf |ent: PdfTint takes disabled, filtered or full, not "loud" | Last | @@ -127,11 +126,10 @@ tag [3/16] | | | -placement 794x1123 src 0,0 794x1123 over 0,2 58x5 key 13a14ce76b83da1b at 204,0 56x80 +placement 794x1123 src 0,0 794x1123 over 0,2 58x5 key b7d9755f4d1c3b06 at 204,0 56x80 > dump -dump 636117 bytes -tag [3/16] -| +dump 24 bytes +tag [3/16] petscii:off palette:commodore ascii:on docs/design.pdf | | | @@ -139,8 +137,9 @@ tag [3/16] | | | +placement 794x1123 src 0,0 794x1123 over 0,0 58x7 key 4a2b3987bae4cbef at 192,0 79x112 > graphics off -tag [3/16] +tag [3/16] petscii:on palette:commodore ascii:on docs/design.pdf |The watch path, in full, because it is the ask with the mo |machinery behind it. The tty and SDL hosts and the detache |daemon implement it in src/file_watch.zig, with Linux in @@ -149,7 +148,7 @@ tag [3/16] |instead, then restats the exact path under a pane-generati |guard. All three macOS watchers mark the same two things f > key j -tag [4/16] +tag [4/16] petscii:on palette:commodore ascii:on docs/design.pdf |/// How to route a method, read off its own name. A method |/// is a COMPILE ERROR rather than a silent push, because |/// forgotten pull is invisible in every unit test and obv @@ -158,7 +157,7 @@ tag [4/16] | if (std.mem.startsWith(u8, name, "pull_")) return true | if (std.mem.startsWith(u8, name, "push_")) { > key 2k -tag [2/16] +tag [2/16] petscii:on palette:commodore ascii:on docs/design.pdf |the core — src/pardes.zig |update(ev) — one dispatch, mutates |render(arena) → *Surface diff --git a/addons/snake/snake.out b/addons/snake/snake.out index 79bc5f19..6869c5cc 100644 --- a/addons/snake/snake.out +++ b/addons/snake/snake.out @@ -283,10 +283,10 @@ dump 240 bytes tag Snake 1 |###################### |# # -|# oo # |# o # -|# * @ # -|# # +|# o # +|# * o # +|# @ # |###################### | | @@ -310,15 +310,15 @@ tag Snake 1 | | > tick 150 -tag Snake 1 +tag Snake 1 over |###################### |# # |# o # |# o # |# * o # -|# @ # +|# x # |###################### -| +|game over: any key | | | diff --git a/addons/snake/snake.zig b/addons/snake/snake.zig index badafc54..cdbf74d9 100644 --- a/addons/snake/snake.zig +++ b/addons/snake/snake.zig @@ -76,7 +76,7 @@ pub const Snake = extern struct { s.placeFood(); } - pub fn open(s: *Snake, _: ?[]const u8) void { + pub fn open(s: *Snake, _: ?[]const u8, _: ?[]const u8) void { s.rng = addon.seed() | 1; s.restart(); } @@ -88,14 +88,17 @@ pub const Snake = extern struct { return if (!flags.focused or s.dead) s.next.stop() else s.next.every(now, period); } - pub fn paint(s: *const Snake, w: addon.Window, out: addon.Paint) void { + pub fn prefix(s: *const Snake, w: *std.Io.Writer) !void { + try w.print("Snake {d}{s}", .{ s.score, if (s.dead) " over" else "" }); + } + + pub fn paint(s: *const Snake, w: addon.Window, _: addon.Paint) void { const wall: addon.Style = .{ .fg = .index(8) }; for (0..H + 2) |y| for (0..W + 2) |x| if (x % (W + 1) == 0 or y % (H + 1) == 0) w.put(x, y, "#", wall); w.put(1 + s.food.x, 1 + s.food.y, "*", .{ .fg = .index(1), .attrs = .{ .bold = true } }); for (s.segs[0..s.len], 0..) |p, i| w.put(1 + p.x, 1 + p.y, if (i > 0) "o" else if (s.dead) "x" else "@", .{ .fg = .index(2), .attrs = .{ .bold = i == 0 } }); if (s.dead) w.put(0, H + 2, "game over: any key", .{ .fg = .index(1) }) else if (!w.focused) w.put(0, H + 2, "paused", wall); - out.prefix("Snake {d}{s}", .{ s.score, if (s.dead) " over" else "" }); } pub fn key(s: *Snake, k: addon.Key) bool { |
