//! A cell-drawing surface plus the fast-path helpers uv keeps in its `screen` //! package (`screen/screen.go` + `screen/context.go`). //! //! [`Screen`] is a value-carried interface: a pointer to a static vtable of //! thunk functions. A concrete grid type implements it by embedding a //! `screen: Screen` field whose vtable entries point at its own named thunks; //! each thunk recovers the concrete pointer with //! `@fieldParentPtr("screen", ...)` and forwards. `ir.Screen` (uv `ScreenBuffer`) //! and the upcoming `Window` follow this pattern, so there is no generic //! binding glue. (Because the vtable is a comptime constant of named function //! references, the embedded value is safe to copy.) //! //! The fast-path helpers ([`clear`], [`clearArea`], [`fill`], [`fillArea`], //! [`clone`], [`cloneArea`]) mirror uv's `screen` package: when the concrete //! screen filled in the corresponding vtable entry (always true for //! `ScreenBuffer`/`Window`) the helper calls it; a null entry falls back to //! the naive loops. [`Context`] is a stateful drawing surface (style/link/ //! position cursor, `print`/`write`/`drawString`) ported from //! `screen/context.go`. const std = @import("std"); const ir = @import("root.zig"); pub const Context = @import("Context.zig"); const Allocator = std.mem.Allocator; const Rectangle = ir.Rectangle; const Cell = ir.Cell; const Method = ir.width.Method; const Buffer = ir.Buffer; const Size = ir.Size; /// A position in screen-local coordinates (origin at the screen's bounds min). pub const Pos = struct { x: Size = 0, y: Size = 0, }; /// Mutable trait for a cell grid (the `uv.Screen` methods: `Bounds`, `CellAt`, /// `SetCell`, `WidthMethod`, plus fast paths). Concrete types embed this by /// value as their `screen` field and point the vtable at their own thunks. pub const Screen = struct { vtable: *const VTable, pub const VTable = struct { bounds: *const fn (screen: *Screen) Rectangle, cell_at: *const fn (screen: *Screen, x: Size, y: Size) ?*Cell, set_cell: *const fn (screen: *Screen, x: Size, y: Size, cell: ?Cell) void, width_method: *const fn (screen: *Screen) Method, /// Fast paths (uv `Clear`/`Fill`/… type assertions); null means the /// helpers use their naive loops. clear: *const fn (screen: *Screen) anyerror!void = unimplementedClear, clear_area: *const fn (screen: *Screen, area: Rectangle) anyerror!void = unimplementedClearArea, fill: *const fn (screen: *Screen, cell: ?Cell) anyerror!void = unimplementedFill, fill_area: *const fn (screen: *Screen, cell: ?Cell, area: Rectangle) anyerror!void = unimplementedFillArea, clone: *const fn (screen: *Screen, allocator: Allocator) anyerror!Buffer = unimplementedClone, clone_area: *const fn (screen: *Screen, allocator: Allocator, area: Rectangle) anyerror!?Buffer = unimplementedCloneArea, }; pub fn bounds(self: *Screen) Rectangle { return self.vtable.bounds(self); } pub fn cellAt(self: *Screen, x: Size, y: Size) ?*Cell { return self.vtable.cell_at(self, x, y); } pub fn setCell(self: *Screen, x: Size, y: Size, cell: ?Cell) void { self.vtable.set_cell(self, x, y, cell); } pub fn clear(self: *Screen) !void { return self.vtable.clear(self) catch |err| switch (err) { error.Unimplemented => fill(self, null), else => err, }; } pub fn clearArea(self: *Screen, area: Rectangle) !void { return self.vtable.clear_area(self, area) catch |err| switch (err) { error.Unimplemented => fillArea(self, null, area), else => err, }; } pub fn clone(self: *Screen, allocator: Allocator) !Buffer { return self.vtable.clone(self, allocator) catch |err| switch (err) { error.Unimplemented => cloneArea(self, allocator, self.bounds()), else => err, }; } pub fn fill(self: *Screen, cell: ?Cell) !void { return self.vtable.fill(self, cell) catch |err| switch (err) { error.Unimplemented => fillArea(self, cell, self.bounds()), else => err, }; } /// Fills `area` with `cell` (nil = empty cells). Uses the vtable's `fill_area` /// when present, else writes cells in a loop. Mirrors uv `screen.FillArea`. pub fn fillArea(self: *Screen, cell: ?Cell, area: Rectangle) !void { return self.vtable.fill_area(self, cell, area) catch |err| switch (err) { error.Unimplemented => { var width: Size = 1; if (cell) |this| { if (this.width > 1) width = this.width; } var y = area.min_y; while (y < area.max_y) : (y += 1) { var x = area.min_x; while (x < area.max_x) : (x += width) { self.setCell(x, y, cell); } } }, else => err, }; } pub fn cloneArea(self: *Screen, allocator: Allocator, area: Rectangle) !?Buffer { return self.vtable.clone_area(self, allocator, area) catch |err| switch (err) { error.Unimplemented => { var buffer: Buffer = try .init(allocator, area.width(), area.height()); errdefer buffer.deinit(); var y = area.min_y; while (y < area.max_y) : (y += 1) { var x = area.min_x; while (x < area.max_x) { const cell = self.cellAt(x, y) orelse { x += 1; continue; }; if (cell.isWidePlaceholder()) { x += 1; continue; } buffer.setCell(x - area.min_x, y - area.min_y, cell.*); x += @max(cell.width, 1); } } return buffer; }, else => err, }; } pub fn widthMethod(self: *Screen) Method { return self.vtable.width_method(self); } fn unimplementedClear(_: *Screen) !void { return error.Unimplemented; } fn unimplementedClearArea(_: *Screen, _: Rectangle) !void { return error.Unimplemented; } fn unimplementedFill(_: *Screen, _: ?Cell) !void { return error.Unimplemented; } fn unimplementedFillArea(_: *Screen, _: ?Cell, _: Rectangle) !void { return error.Unimplemented; } fn unimplementedClone(_: *Screen, _: Allocator) !Buffer { return error.Unimplemented; } fn unimplementedCloneArea(_: *Screen, _: Allocator, _: Rectangle) !?Buffer { return error.Unimplemented; } };