Something went wrong. Try again.
Charm-like tui library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266//! A buffer that tracks which lines/cells changed since the last render.const std = @import("std");const ir = @import("root.zig");
const Buffer = ir.Buffer;const Allocator = std.mem.Allocator;const LineData = ir.LineData;const Rectangle = ir.Rectangle;const Line = ir.Line;const Cell = ir.Cell;const t = std.testing;
const RenderBuffer = @This();
const Size = ir.Size;
buffer: Buffer,touched: []LineData = &.{},
pub fn init(allocator: Allocator, w: Size, h: Size) !RenderBuffer { var buffer: RenderBuffer = .{ .buffer = try .init(allocator, w, h) }; errdefer buffer.deinit(); buffer.touched = try allocator.alloc(LineData, h); @memset(buffer.touched, .untouched); return buffer;}
pub fn deinit(self: *RenderBuffer) void { self.buffer.deinit(); self.buffer.allocator.free(self.touched);}
pub fn width(self: RenderBuffer) Size { return self.buffer.width();}
pub fn height(self: RenderBuffer) Size { return self.buffer.height();}
pub fn bounds(self: RenderBuffer) Rectangle { return self.buffer.bounds();}
pub fn line(self: *RenderBuffer, y: Size) ?*Line { return self.buffer.line(y);}
pub fn cellAt(self: *RenderBuffer, x: Size, y: Size) ?*Cell { return self.buffer.cellAt(x, y);}
/// Resizes the grid and re-syncs the touched table to the new height.pub fn resize(self: *RenderBuffer, w: Size, h: Size) !void { try self.buffer.resize(w, h); if (self.touched.len == h) return; self.touched = try if (self.touched.len == 0) self.buffer.allocator.alloc(LineData, h) else self.buffer.allocator.realloc(self.touched, h);
@memset(self.touched, .untouched);}
/// Marks a line n cells starting at x as touched.pub fn touchLine(self: *RenderBuffer, x: Size, y: Size, n: Size) void { if (y >= self.touched.len) return; switch (self.touched[y]) { .untouched, .synced => self.touched[y] = .changes(x, x + n, 0), .changed => |*ld| { ld.first_cell = @min(ld.first_cell, x); ld.last_cell = @max(ld.last_cell, x + n); }, }}
pub fn touch(self: *RenderBuffer, x: Size, y: Size) void { self.touchLine(x, y, 0);}
pub fn touchedLines(self: *RenderBuffer) Size { var count: usize = 0; for (self.touched) |l| { if (l == .changed) count += 1; } return @intCast(count);}
pub fn setCell(self: *RenderBuffer, x: Size, y: Size, cell: ?Cell) void { if (!cellEqual(self.buffer.cellAt(x, y), cell)) { var w: Size = 1; if (cell) |cc| { if (cc.width > 0) w = cc.width; } if (self.buffer.cellAt(x, y)) |p| { if (p.width > 0) w = @max(w, p.width); } self.touchLine(x, y, w); } self.buffer.setCell(x, y, cell);}
pub fn insertLineArea(self: *RenderBuffer, y: Size, n: Size, cell: ?Cell, area: Rectangle) void { self.buffer.insertLineArea(y, n, cell, area); var i = area.min_y; while (i < area.max_y) : (i += 1) { self.touchLine(area.min_x, i, area.width()); if (i >= n) self.touchLine(area.min_x, i - n, area.width()); }}
pub fn deleteLineArea(self: *RenderBuffer, y: Size, n: Size, cell: ?Cell, area: Rectangle) void { self.buffer.deleteLineArea(y, n, cell, area); var i = area.min_y; while (i < area.max_y) : (i += 1) { self.touchLine(area.min_x, i, area.width()); self.touchLine(area.min_x, i + n, area.width()); }}
pub fn insertCellArea(self: *RenderBuffer, x: Size, y: Size, n: Size, cell: ?Cell, area: Rectangle) void { self.buffer.insertCellArea(x, y, n, cell, area); var nn = n; if (x + nn > area.max_x) nn = area.max_x - x; self.touchLine(x, y, nn);}
pub fn deleteCellArea(self: *RenderBuffer, x: Size, y: Size, n: Size, cell: ?Cell, area: Rectangle) void { self.buffer.deleteCellArea(x, y, n, cell, area); var nn = n; const remaining = area.max_x - x; if (nn > remaining) nn = remaining; self.touchLine(x, y, nn);}
pub fn clear(self: *RenderBuffer) void { self.buffer.clear(); var y: Size = 0; while (y < self.height()) : (y += 1) self.touchLine(0, y, self.width());}
pub fn clearArea(self: *RenderBuffer, area: Rectangle) void { self.buffer.clearArea(area); var y = area.min_y; while (y < area.max_y) : (y += 1) { self.touchLine(area.min_x, y, area.width()); }}
pub fn fill(self: *RenderBuffer, cell: ?Cell) void { self.fillArea(cell, self.buffer.bounds());}
pub fn fillArea(self: *RenderBuffer, cell: ?Cell, area: Rectangle) void { self.buffer.fillArea(cell, area); var y = area.min_y; while (y < area.max_y) : (y += 1) { self.touchLine(area.min_x, y, area.width()); }}
test "RenderBuffer touch merging" { var buffer: RenderBuffer = try .init(t.allocator, 10, 5); defer buffer.deinit();
buffer.touchLine(3, 1, 2); // covers [3,5) buffer.touchLine(0, 1, 1); // covers [0,1) -> merged [0,5) const l = switch (buffer.touched[1]) { .changed => |ld| ld, else => return t.expect(false), }; try t.expect(l.first_cell == 0); try t.expect(l.last_cell == 5);
buffer.touchLine(6, 1, 2); // [6,8) disjoint -> merges [0,8) const l2 = switch (buffer.touched[1]) { .changed => |ld| ld, else => return t.expect(false), }; try t.expect(l2.first_cell == 0); try t.expect(l2.last_cell == 8);
try t.expect(buffer.touched[2] == .untouched); try t.expectEqual(1, buffer.touchedLines());}
test "RenderBuffer setCell differs marks touched" { var buffer: RenderBuffer = try .init(t.allocator, 10, 5); defer buffer.deinit(); buffer.setCell(2, 0, .of("a", 1)); const l = switch (buffer.touched[0]) { .changed => |ld| ld, else => return t.expect(false), }; try t.expectEqual(2, l.first_cell); try t.expectEqual(3, l.last_cell);
// Same cell again: no mark. buffer.touched[0] = .untouched; buffer.setCell(2, 0, .of("a", 1)); // touched[0] is still whatever it was; setCell only touches when the cell // differs, and since it was reset to be "same as cell", no new touch. try t.expect(buffer.touched[0] == .untouched);}
test "RenderBuffer clear marks all lines" { var buffer: RenderBuffer = try .init(t.allocator, 4, 3); defer buffer.deinit(); buffer.clear(); try t.expectEqual(3, buffer.touchedLines()); for (buffer.touched) |touched| { const l = switch (touched) { .changed => |ld| ld, else => continue, }; try t.expect(l.first_cell == 0); try t.expect(l.last_cell == 4); }}
/// Whether two cells (one a grid pointer, one a value) are equal. A nil cell/// is never equal to anything, mirroring Go's `cellEqual` (the nil check is/// deliberately distinct because the renderer treats nil as EmptyCell).fn cellEqual(a: ?*const Cell, b: ?Cell) bool { const this = a orelse return false; const that = b orelse return false; return this.equal(that);}
test "fuzz buffer" { try t.fuzz({}, fuzzBuffer, .{ .corpus = &.{ "", "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b" }, });}
fn fuzzBuffer(_: void, smith: *t.Smith) anyerror!void { var rb: RenderBuffer = try .init(t.allocator, 16, 8); defer rb.deinit();
const rounds: usize = 1 + @as(usize, smith.index(32)); var round: usize = 0; while (round < rounds) : (round += 1) { const w = rb.width(); const h = rb.height(); const x: Size = @intCast(smith.index(w)); const y: Size = @intCast(smith.index(h)); const n = smith.valueRangeAtMost(u8, 0, 8); const area: Rectangle = .new(0, 0, w, h); switch (smith.value(u4)) { 0 => rb.setCell(x, y, null), 1 => rb.setCell(x, y, .space), 2 => rb.setCell(x, y, .of("a", 1)), 3 => rb.setCell(x, y, .of("世", 2)), 4 => rb.insertLineArea(y, n, null, area), 5 => rb.deleteLineArea(y, n, null, area), 6 => rb.insertCellArea(x, y, n, null, area), 7 => rb.deleteCellArea(x, y, n, null, area), 8 => rb.fillArea(.space, area), 9 => rb.clearArea(area), 10 => try rb.resize(@intCast(smith.valueRangeAtMost(u16, 1, 24)), @intCast(smith.valueRangeAtMost(u16, 1, 16))), 11 => rb.clear(), else => _ = rb.cellAt(x, y), } }}