summaryrefslogtreecommitdiff
path: root/src/builtins_json.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-01 13:05:10 -0300
committerGabriel Schneider <[email protected]>2026-10-01 13:16:47 -0300
commitf700b2af8e874c586e6996b8311c3983203c6637 (patch)
tree89c7de940f888a0917e5851c8a362c83de50b389 /src/builtins_json.zig
parentf6a511c61d7ef2ff680f70077b236559a595be69 (diff)
downloadpardes-f700b2af8e874c586e6996b8311c3983203c6637.tar.gz
pardes-f700b2af8e874c586e6996b8311c3983203c6637.zip
Builtins are chips that link to a glossary generated from the registry, and the virtual filesystem is drawn as a tree
#word draws a builtin as a chip on the tag's ground over a rule (Look and Exec in acme's look green and exec red), a <code class="word"> in HTML, and links it to its entry in a new Builtins chapter wherever that chapter is in the same output (the book, the site). The chapter is drawn from docs/typ/builtins-tty.json and builtins-gui.json, which `zig build builtins-json -Dplatform=tty|gui` writes from the registry and the Help rows (src/builtins_json.zig, tools/gen_builtins_json.zig; summaryOf in ninep/ctl.zig is now pub); style.typ merges them, marking the window-only words. The doc steps refresh their build's file first, and each build's unit-test fails when its file is stale. Everyday words first (`everyday` in style.typ), then groups by leader prefix as Help has them, then settings, the words with no path, debugging and internals, and last an A-Z index of every word. `zig build book` joins the doc steps. #fstree draws a directory tree with its connectors, a description column and links to the sections that document each file; reference.typ draws the served tree with it, each pane, pty and column file on its own line, and a test in config.zig checks it against ninep/tree.zig's files both ways. The cheatsheet has a two-level version. On the site the tree reads as `tree` text in w3m and wraps descriptions under the names on a phone. The site's tutor draws its B1, B2, B3 and 1-2 chords as mouse glyphs; the landing credits Plan 9 and acme, as does the book's title page; install.typ no longer lists the mirrors (the footer does). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/builtins_json.zig')
-rw-r--r--src/builtins_json.zig97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/builtins_json.zig b/src/builtins_json.zig
new file mode 100644
index 00000000..7f998388
--- /dev/null
+++ b/src/builtins_json.zig
@@ -0,0 +1,97 @@
+//! Every builtin as JSON, for the docs' glossary (docs/typ/builtins-tty.json
+//! and builtins-gui.json, a build's list each; style.typ merges them):
+//! the rows the `Help` builtin draws (pardes.builtin_rows: the leader path
+//! and the other shortcuts) with what /commands says of each word (its doc
+//! comment's first sentence, or a setting's doc). One source, so the
+//! glossary and Help cannot drift; the test below fails when the committed
+//! copy is stale, and `zig build builtins-json` rewrites it.
+const std = @import("std");
+const pardes = @import("pardes.zig");
+const builtins = @import("builtins.zig");
+const config = @import("config.zig");
+const ctl = @import("ninep/ctl.zig");
+
+fn str(w: *std.Io.Writer, s: []const u8) !void {
+ try w.writeByte('"');
+ for (s) |c| switch (c) {
+ '"' => try w.writeAll("\\\""),
+ '\\' => try w.writeAll("\\\\"),
+ '\n' => try w.writeAll("\\n"),
+ 0...9, 11...0x1f => try w.print("\\u{x:0>4}", .{c}),
+ else => try w.writeByte(c),
+ };
+ try w.writeByte('"');
+}
+
+const Entry = struct { name: []const u8, row: usize };
+
+fn lessName(_: void, a: Entry, b: Entry) bool {
+ return std.mem.lessThan(u8, a.name, b.name);
+}
+
+pub fn write(w: *std.Io.Writer) !void {
+ var order: [pardes.builtin_rows.len]Entry = undefined;
+ for (pardes.builtin_rows, 0..) |row, i| order[i] = .{ .name = @tagName(row.cmd), .row = i };
+ std.mem.sort(Entry, &order, {}, lessName);
+ try w.writeAll("[\n");
+ for (order, 0..) |e, n| {
+ const row = pardes.builtin_rows[e.row];
+ const b = row.cmd;
+ const name = @tagName(b);
+ try w.writeAll(" {\"name\": ");
+ try str(w, name);
+ try w.writeAll(", \"leader\": ");
+ if (row.path) |p| {
+ try w.writeAll("\"SPC");
+ for (p) |c| try w.print(" {c}", .{c});
+ try w.writeByte('"');
+ } else try w.writeAll("null");
+ // Help's line ends in the other shortcuts, after the name's column.
+ try w.writeAll(", \"also\": [");
+ const at = std.mem.indexOf(u8, row.line, name).? + name.len;
+ const rest = std.mem.trim(u8, row.line[at..], " ");
+ var parts = std.mem.splitSequence(u8, rest, ", ");
+ var first = true;
+ while (parts.next()) |part| if (part.len > 0) {
+ if (!first) try w.writeAll(", ");
+ first = false;
+ try str(w, part);
+ };
+ try w.writeAll("], \"arg\": ");
+ try w.writeAll(if (builtins.registry.takesArg(b)) "true" else "false");
+ try w.writeAll(", \"scope\": ");
+ try str(w, if (b == .Edit) "both" else if (builtins.registry.scope(b) == .session) "root" else "pane");
+ const setting = config.Runtime.find(name);
+ try w.writeAll(", \"setting\": ");
+ try w.writeAll(if (setting != null) "true" else "false");
+ try w.writeAll(", \"choices\": ");
+ if (setting) |s| if (config.Runtime.choices(s.action)) |v| try str(w, v) else try w.writeAll("null") else try w.writeAll("null");
+ var buf: [2048]u8 = undefined;
+ const doc = if (setting) |s| s.doc else ctl.summaryOf(name, &buf);
+ try w.writeAll(", \"doc\": ");
+ try str(w, doc);
+ try w.writeAll(if (n + 1 < order.len) "},\n" else "}\n");
+ }
+ try w.writeAll("]\n");
+}
+
+test "docs/typ/builtins-<platform>.json is what the registry says now" {
+ // a terminal build checks its file, a window build its own
+ const file = switch (pardes.platform) {
+ .tty => "builtins-tty.json",
+ .gui => "builtins-gui.json",
+ else => return error.SkipZigTest,
+ };
+ const gpa = std.testing.allocator;
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ defer out.deinit();
+ try write(&out.writer);
+ var dir = try std.Io.Dir.cwd().openDir(std.testing.io, @import("doc_typ").dir, .{});
+ defer dir.close(std.testing.io);
+ const committed = try dir.readFileAlloc(std.testing.io, file, gpa, .limited(1 << 20));
+ defer gpa.free(committed);
+ if (!std.mem.eql(u8, committed, out.written())) {
+ std.debug.print("docs/typ/{s} is stale: run `zig build builtins-json -Dplatform={s} --prefix <dir>`\n", .{ file, @tagName(pardes.platform) });
+ return error.StaleBuiltinsJson;
+ }
+}