diff options
| -rw-r--r-- | examples/live/build.zig | 11 | ||||
| -rw-r--r-- | examples/live/build.zig.zon | 8 | ||||
| -rw-r--r-- | examples/live/src/main.zig | 122 | ||||
| -rw-r--r-- | examples/live/test/live.script | 39 |
4 files changed, 132 insertions, 48 deletions
diff --git a/examples/live/build.zig b/examples/live/build.zig index 9d0983d..ccfd32d 100644 --- a/examples/live/build.zig +++ b/examples/live/build.zig @@ -30,7 +30,16 @@ pub fn build(b: *std.Build) void { .{ .name = "scope", .module = sb.modules.scope }, }; - const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = imports }); + // The editor: helix's keys over a value kept in State. Its tree-sitter + // set is the smallest there is (Zig's grammar), as the language's own + // grammar is handed in at run time. + const pardes = b.dependency("pardes", .{ .target = target, .optimize = optimize, .@"tree-sitter" = .zig, .editor = true }); + const app = sb.app(.{ .name = "live", .root_source_file = b.path("src/main.zig"), .imports = &.{ + .{ .name = "Lang", .module = lang }, + .{ .name = "shaders", .module = shaders.module }, + .{ .name = "scope", .module = sb.modules.scope }, + .{ .name = "pardes-editor", .module = pardes.module("pardes-editor") }, + } }); const steps = app.addSteps(.{ .scripts = &.{b.path("test/live.script")} }); const lang_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/Lang.zig"), .target = target, .optimize = optimize }) }); steps.test_step.dependOn(&b.addRunArtifact(lang_test).step); diff --git a/examples/live/build.zig.zon b/examples/live/build.zig.zon index 27bd831..d5d265a 100644 --- a/examples/live/build.zig.zon +++ b/examples/live/build.zig.zon @@ -2,7 +2,13 @@ .name = .live, .version = "0.0.1", .dependencies = .{ - .sketchbook = .{ .path = "../.." }, + // `../..` spelled as pardes spells it (its `../sketchbook`): a + // package is told by its path's spelling, and one file cannot be + // in two. + .sketchbook = .{ .path = "../../../sketchbook" }, + // pardes's editor as a library (`pardes-editor`), from the checkout + // beside sketchbook's. + .pardes = .{ .path = "../../../02-pardes-code" }, }, .minimum_zig_version = "0.17.0", .paths = .{""}, diff --git a/examples/live/src/main.zig b/examples/live/src/main.zig index 965152a..e62522f 100644 --- a/examples/live/src/main.zig +++ b/examples/live/src/main.zig @@ -18,6 +18,7 @@ const gpu = sk.abi.gpu; const Lang = @import("Lang"); const shaders = @import("shaders"); const scope = @import("scope"); +const pe = @import("pardes-editor"); pub const name = "live"; pub const width = 1280; @@ -31,11 +32,15 @@ const stream = 1; /// The most samples synthesised in one frame. const max_block = 8192; +/// The text: pardes's editor, helix's keys over a value of plain data +/// (src/lib_editor.zig in pardes), its capacities sized to the language's +/// 4 KiB of text. The undo ring keeps whole texts, the oldest dropped: four +/// at the most text, hundreds of a short program. 39,264 bytes in all, of a +/// State of 43,480. +pub const Editor = pe.Editor(.{ .text = 4096, .undo = 16 * 1024, .registers = 1024, .selections = 64 }); + pub const State = struct { - // ponytail: a plain buffer, typed characters appended and backspace - // deleting the last; part 2 puts pardes's editor here. - text: [4096]u8 = @splat(0), - len: u32 = 0, + ed: Editor = .{}, /// The last program that parsed. out: Lang.Tape = .{}, color: Lang.Tape = .{}, @@ -72,35 +77,45 @@ pub fn command(ss: *Session, c: *sk.Console, line: []const u8) !bool { return true; } +/// What the editor thaws its value into for each call, reset after it: +/// `update` may not allocate from the host, and a fixed buffer keeps a +/// call deterministic and bounded. A key at the most text took 29 KiB of it. +var scratch: [128 * 1024]u8 = undefined; + pub fn update(s: *State, in: *const sk.Input) void { - var edited = false; - for (in.events) |e| switch (e.kind) { - .tick => s.time += e.x, - .text => { - var utf8: [4]u8 = undefined; - const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; - if (s.len + n > s.text.len) continue; - @memcpy(s.text[s.len..][0..n], utf8[0..n]); - s.len += n; - edited = true; - }, - .key_down => switch (e.key()) { - .backspace => if (s.len > 0) { - s.len -= 1; - while (s.len > 0 and s.text[s.len] & 0xc0 == 0x80) s.len -= 1; - edited = true; + const revision = s.ed.revision; + // a chord's key_down may come with its letter as text too: not typed + var chord = false; + for (in.events) |e| { + var utf8: [4]u8 = undefined; + const k: pe.Key = switch (e.kind) { + .tick => { + s.time += e.x; + continue; }, - .@"return", .keypad_enter => if (s.len < s.text.len) { - s.text[s.len] = '\n'; - s.len += 1; - edited = true; + .text => text: { + const n = std.unicode.utf8Encode(@intCast(@min(e.value, 0x10ffff)), &utf8) catch continue; + if (chord) { + chord = false; + continue; + } + break :text .{ .cp = @intCast(@min(e.value, 0x10ffff)), .text = utf8[0..n] }; }, - else => {}, - }, - else => {}, - }; - if (!edited) return; - switch (Lang.compile(s.text[0..s.len])) { + .key_down => chord: { + const k = key(e) orelse continue; + chord = (k.ctrl or k.alt) and k.cp >= ' ' and k.cp < 0x7f; + break :chord k; + }, + else => continue, + }; + var fba: std.heap.FixedBufferAllocator = .init(&scratch); + // ponytail: a key the text has no room for is dropped, as part 1's + // buffer dropped it; the editor leaves the value as it was. + _ = s.ed.key(fba.allocator(), null, k); + } + if (s.ed.revision == revision) return; + var text: [Editor.capacity.text]u8 = undefined; + switch (Lang.compile(s.ed.read(&text))) { .program => |p| { s.out = p.out; s.color = p.color; @@ -110,6 +125,51 @@ pub fn update(s: *State, in: *const sk.Input) void { } } +/// A key_down as the editor takes it: the specials, and chords (ctrl or alt +/// with a letter, digit or sign), helix's `A-I` an upper-case letter with +/// alt. A printable without a chord comes as `text`, so it is not one here. +fn key(e: sk.Event) ?pe.Key { + const mods = e.value; + const ctrl = mods & sk.Key.Mod.ctrl != 0; + const alt = mods & sk.Key.Mod.alt != 0; + const shift = mods & sk.Key.Mod.shift != 0; + const special: u21 = switch (e.key()) { + .escape => pe.Key.escape, + .@"return", .keypad_enter, .return2 => pe.Key.enter, + .backspace => pe.Key.backspace, + .tab => pe.Key.tab, + .delete => pe.Key.delete, + .left => pe.Key.left, + .right => pe.Key.right, + .up => pe.Key.up, + .down => pe.Key.down, + .home => pe.Key.home, + .end => pe.Key.end, + .page_up => pe.Key.page_up, + .page_down => pe.Key.page_down, + else => 0, + }; + if (special != 0) return .{ .cp = special, .ctrl = ctrl, .alt = alt, .shift = shift }; + if (!ctrl and !alt) return null; + const scan = @backingInt(e.key()); + const cp: u21 = switch (e.key()) { + .a, .b, .c, .d, .e, .f, .g, .h, .i, .j, .k, .l, .m, .n, .o, .p, .q, .r, .s, .t, .u, .v, .w, .x, .y, .z => @as(u21, if (shift) 'A' else 'a') + scan - @backingInt(sk.Key.a), + .@"1", .@"2", .@"3", .@"4", .@"5", .@"6", .@"7", .@"8", .@"9" => '1' + @as(u21, scan - @backingInt(sk.Key.@"1")), + .@"0" => '0', + .space => ' ', + .minus => '-', + .equals => '=', + .left_bracket => '[', + .right_bracket => ']', + .semicolon => ';', + .comma => ',', + .period => '.', + .slash => '/', + else => return null, + }; + return .{ .cp = cp, .ctrl = ctrl, .alt = alt }; +} + const synth: gpu.Kernel = .{ .code = shaders.synth_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1 }; const picture_pipeline: gpu.Pipeline = .{ .vertex = .{ .code = shaders.fullscreen_vert }, @@ -220,7 +280,7 @@ const red = 0xff5a50; // ponytail: one face at one size, so a column is a fixed advance; part 2 // draws the editor's text with sketchbook's new text instead. fn code(s: *const State, f: *sk.Frame) void { - const text = s.text[0..s.len]; + const text = s.ed.bytes(f.scratch) catch return; const cell = f.host.textSizeIn(.code, scale, "M"); const lines = std.mem.count(u8, text, "\n") + 1; var widest: f32 = 0; diff --git a/examples/live/test/live.script b/examples/live/test/live.script index 695d16a..ecf3d77 100644 --- a/examples/live/test/live.script +++ b/examples/live/test/live.script @@ -1,9 +1,12 @@ -# A program typed, its tapes after each line, a broken line and its mend; -# then this run's session played back, and sought to two moments marked as -# they happened. (A mark ends a clip, `record`, so the session is replayed.) +# A program typed through the editor (helix's keys: `i`, text, Escape, +# motions), its tapes after each line, a broken line and its mend (an +# undo); then this run's session played back, and sought to two moments +# marked as they happened. (A mark ends a clip, `record`, so the session is +# replayed.) # Screenshots land in .zig-cache/live/ for review; they are compared over # the picture alone (640,300 600x300: not the text, the scope or the time). frames 2 +type i type out = sin(tau * 220 * t) * 0.3 expect problem.len 0 expect out.len 8 @@ -17,45 +20,51 @@ type color = hsv(x, 0.5, y) expect color.len 4 expect color.code[0].op x expect color.code[3].op hsv +key escape +expect ed.cursor.mode normal frames 2 console save typed screenshot .zig-cache/live/gradient.png -# An edit: y becomes x, and the picture changes. -key backspace -key backspace -type x) +# An edit: y becomes x (to the line's end, a character left, replaced), +# and the picture changes. +type gl +type h +type rx expect color.len 3 expect color.code[2].op hsv expect color.code[2].c 0 frames 2 screenshot .zig-cache/live/edited.png expect-different-screenshot .zig-cache/live/gradient.png .zig-cache/live/edited.png 640 300 600 300 -# Broken: the tape and the picture stay, the problem is set at its byte. -key backspace +# Broken: the ) deleted; the tape and the picture stay, the problem is set +# at its byte. +type l +type d expect problem.at 52 expect color.len 3 expect color.code[2].c 0 frames 2 screenshot .zig-cache/live/broken.png expect-same-screenshot .zig-cache/live/edited.png .zig-cache/live/broken.png 0 640 300 600 300 -# Mended. -type ) +# Mended: undone. +type u expect problem.len 0 expect color.len 3 console save end # Played back from the start, then sought: each moment's text, tapes and # time as they were live, leaf by leaf (a mark is taken before its frame's -# tick: 0.2667 s and 0.4833 s; a seek lands on the last frame at or before). +# tick: 0.3 s and 0.5333 s; a seek lands on the last frame at or before). console play session 0 frames 100 -console seek 488 +console seek 538 expect-snapshot end -console seek 272 +console seek 305 expect-snapshot typed expect color.len 4 screenshot .zig-cache/live/sought.png expect-same-screenshot .zig-cache/live/gradient.png .zig-cache/live/sought.png 0 0 0 1000 600 # Live again from there: the typed program, before its edit. console stop -expect len 53 +expect ed.front 53 +expect ed.back 0 expect color.code[3].op hsv |
