From 98f84f99e79cccc2344aff2efe10823540d0c7b7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 03:06:49 -0300 Subject: plain: the records the core and the terminal both name, a leaf module (docs/next-terminal.md, Phase 1, its first step): src/plain/plain.zig takes Color, FontRole, CellStyle, Cell, RectPx, RectCell, Raster and Placement from surface.zig and Key from Event.zig, moved as they are, and imports std alone; surface.zig and Event.zig re-export them under their old names, so no caller changes; src/term/'s files name them through `plain` (pager.zig and TerminalImages.zig no longer reach surface.zig); build.zig's wireCore gives every core module the `plain` import; locations.Row stays (it names look.Spot and walk's digest: not free); core-test 985 of 985, lib-test --- src/plain/plain.zig | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/plain/plain.zig (limited to 'src/plain/plain.zig') diff --git a/src/plain/plain.zig b/src/plain/plain.zig new file mode 100644 index 00000000..9c2cbb5a --- /dev/null +++ b/src/plain/plain.zig @@ -0,0 +1,158 @@ +//! The plain records the core and the terminal both name +//! (docs/next-terminal.md, Phase 1): a frame's cells, their styles and +//! colours, the pictures over them and a key read. Data that needs +//! nothing else, in a module of its own: the core (surface.zig and +//! Event.zig, under their old names) and the terminal (src/term/) both +//! import it, and neither imports the other. +const std = @import("std"); + +pub const Color = union(enum) { default, index: u8, rgb: [3]u8 }; + +pub const FontRole = enum(u8) { body, tagline }; + +pub const CellStyle = struct { + fg: Color = .default, + bg: Color = .default, + bold: bool = false, + dim: bool = false, + italic: bool = false, + blink: bool = false, + reverse: bool = false, + invisible: bool = false, + strikethrough: bool = false, + ul: enum(u3) { off, single, double, curly, dotted, dashed } = .off, + font_role: FontRole = .body, +}; + +pub const Cell = struct { + text: [7]u8 = @splat(' '), + len: u8 = 1, + style: CellStyle = .{}, + default: bool = true, + + pub fn grapheme(c: *const Cell) []const u8 { + return c.text[0..c.len]; + } + + pub fn visuallyEqual(a: *const Cell, b: *const Cell) bool { + if (a.default or b.default) return a.default and b.default; + return a.len == b.len and + std.mem.eql(u8, a.grapheme(), b.grapheme()) and + std.meta.eql(a.style, b.style); + } + + pub fn printableAscii(c: *const Cell) ?u8 { + if (c.default) return ' '; + if (c.len != 1) return null; + const byte = c.text[0]; + return if (byte >= ' ' and byte <= '~') byte else null; + } +}; + +/// A rectangle in pixels. +pub const RectPx = struct { + x: u32 = 0, + y: u32 = 0, + w: u32 = 0, + h: u32 = 0, +}; + +/// A rectangle in the frame's cells. +pub const RectCell = struct { x: u16 = 0, y: u16 = 0, w: u16 = 0, h: u16 = 0 }; + +/// The pixels a placement shows: RGBA, straight alpha, `stride` bytes a +/// row. They are the pane's, and stay put until its next frame. +pub const Raster = struct { + ptr: [*]const u8, + w: u32, + h: u32, + stride: u32, + + pub fn of(rgba: []const u8, w: usize, h: usize) Raster { + return .{ .ptr = rgba.ptr, .w = @intCast(w), .h = @intCast(h), .stride = @intCast(w * 4) }; + } + + pub fn bytes(r: Raster) []const u8 { + return r.ptr[0 .. @as(usize, r.stride) * r.h]; + } +}; + +/// A picture over the frame's cells, which a frontend realises: the window +/// as a texture kept by `key`, the tty as a kitty image, or either as glyph +/// art in `dst`'s cells (Art.zig, the frontends' own) where it cannot show +/// pixels or the pane asks for art. The frame lists them a pane at a time, +/// lowest first. +pub const Placement = struct { + pane: u8, + raster: Raster, + /// The part of `raster` shown. + src: RectPx, + /// The cells it lies over, which clip it. + dst: RectCell, + /// Where it goes in `dst`, in pixels from `dst`'s top left corner; + /// null: the frontend fits `src` there, no larger than it is, at the + /// top left (as glyph art, centred). + at: ?RectPx = null, + /// The pixels' identity (`keyOf`): one key is one picture, so a + /// frontend keeps what it uploaded under it. + key: u64, + /// A terminal program's kitty `z=`: below 0 it is under the text, + /// which a tty passes on to the terminal outside. + z: i32 = 0, + /// Rows of `raster` that alone differ from the picture under the key + /// `from`: a frontend still holding that one updates those rows. + dirty: ?Dirty = null, + art: Art = .{}, + + pub const Dirty = struct { from: u64, y: u32, h: u32 }; + + /// How the pane wants it drawn as glyph art: the Petscii, Palette and + /// Ascii words on an image pane, Petscii and TermImages on a terminal. + pub const Art = struct { + /// Glyph art even where pixels could be shown (Petscii on). + glyph: bool = false, + palette: Palette = .commodore, + /// Letters and signs among the glyphs, not only blocks and lines. + ascii: bool = true, + + pub const Palette = enum { commodore, terminal }; + }; + + /// A pane's picture `page` (a PDF's page, a terminal's image id; 0 for + /// an image) as of `revision`. + pub fn keyOf(serial: u32, page: u32, revision: u32) u64 { + return std.hash.Wyhash.hash(0, std.mem.asBytes(&[3]u32{ serial, page, revision })); + } +}; + +/// A key a frontend read (Event.zig). +pub const Key = struct { + cp: u21, // codepoint or one of the specials below + text: []const u8 = "", + ctrl: bool = false, + alt: bool = false, + shift: bool = false, + + pub const enter: u21 = 0x0D; + pub const escape: u21 = 0x1B; + pub const tab: u21 = 0x09; + pub const backspace: u21 = 0x7F; + // private-use plane for non-text keys, matching no real codepoint + pub const up: u21 = 0xF0001; + pub const down: u21 = 0xF0002; + pub const left: u21 = 0xF0003; + pub const right: u21 = 0xF0004; + pub const home: u21 = 0xF0005; + pub const end: u21 = 0xF0006; + pub const page_up: u21 = 0xF0007; + pub const page_down: u21 = 0xF0008; + pub const delete: u21 = 0xF0009; + pub const insert: u21 = 0xF000A; + pub const print_screen: u21 = 0xF000B; + pub const scroll_lock: u21 = 0xF000C; + pub const pause: u21 = 0xF000D; + pub const menu: u21 = 0xF000E; + /// F1 to F25, in order: Fn is `f1 + n - 1`. + pub const f1: u21 = 0xF0011; + pub const f25: u21 = f1 + 24; +}; -- cgit v1.3