From 425c86179791cd32e2d4ce0ae7f269940b4b0663 Mon Sep 17 00:00:00 2001 From: Can Celik Date: Thu, 10 Sep 2026 21:51:57 +0300 Subject: chore: upgrade libghostty and preserve terminal behavior (#3906) --- vendor/libghostty-vt/src/terminal/PageList.zig | 5131 +++++++++++++++----- vendor/libghostty-vt/src/terminal/Parser.zig | 29 +- vendor/libghostty-vt/src/terminal/Screen.zig | 2108 ++++++-- vendor/libghostty-vt/src/terminal/ScreenSet.zig | 24 +- vendor/libghostty-vt/src/terminal/Selection.zig | 46 +- .../src/terminal/SelectionGesture.zig | 193 +- vendor/libghostty-vt/src/terminal/StringMap.zig | 38 +- vendor/libghostty-vt/src/terminal/Tabstops.zig | 61 +- vendor/libghostty-vt/src/terminal/Terminal.zig | 3650 +++++++++++--- vendor/libghostty-vt/src/terminal/ansi.zig | 13 +- vendor/libghostty-vt/src/terminal/apc.zig | 360 +- vendor/libghostty-vt/src/terminal/apc/glyph.zig | 7 +- .../src/terminal/apc/glyph/Glossary.zig | 73 +- .../src/terminal/apc/glyph/request.zig | 35 +- .../src/terminal/bitmap_allocator.zig | 233 +- .../libghostty-vt/src/terminal/build_options.zig | 271 +- vendor/libghostty-vt/src/terminal/c/allocator.zig | 5 +- vendor/libghostty-vt/src/terminal/c/build_info.zig | 2 +- vendor/libghostty-vt/src/terminal/c/cell.zig | 10 +- vendor/libghostty-vt/src/terminal/c/formatter.zig | 186 +- vendor/libghostty-vt/src/terminal/c/grid_ref.zig | 6 +- .../src/terminal/c/grid_ref_tracked.zig | 12 +- vendor/libghostty-vt/src/terminal/c/io.zig | 774 +++ vendor/libghostty-vt/src/terminal/c/key_encode.zig | 10 +- vendor/libghostty-vt/src/terminal/c/key_event.zig | 4 +- .../src/terminal/c/kitty_graphics.zig | 288 +- vendor/libghostty-vt/src/terminal/c/main.zig | 40 +- vendor/libghostty-vt/src/terminal/c/modes.zig | 4 +- .../libghostty-vt/src/terminal/c/mouse_encode.zig | 12 +- .../libghostty-vt/src/terminal/c/mouse_event.zig | 4 +- vendor/libghostty-vt/src/terminal/c/osc.zig | 7 +- vendor/libghostty-vt/src/terminal/c/paste.zig | 392 ++ vendor/libghostty-vt/src/terminal/c/render.zig | 929 +++- vendor/libghostty-vt/src/terminal/c/result.zig | 3 + vendor/libghostty-vt/src/terminal/c/row.zig | 2 +- vendor/libghostty-vt/src/terminal/c/search.zig | 1009 ++++ vendor/libghostty-vt/src/terminal/c/selection.zig | 61 +- .../src/terminal/c/selection_gesture.zig | 66 +- vendor/libghostty-vt/src/terminal/c/sgr.zig | 11 - vendor/libghostty-vt/src/terminal/c/snapshot.zig | 1527 ++++++ vendor/libghostty-vt/src/terminal/c/style.zig | 124 +- vendor/libghostty-vt/src/terminal/c/sys.zig | 136 +- vendor/libghostty-vt/src/terminal/c/terminal.zig | 3735 +++++++++++--- .../src/terminal/c/types-schema-verify.py | 114 + .../libghostty-vt/src/terminal/c/types.schema.json | 506 ++ vendor/libghostty-vt/src/terminal/c/types.zig | 1287 ++++- vendor/libghostty-vt/src/terminal/clipboard.zig | 244 +- vendor/libghostty-vt/src/terminal/color.zig | 265 +- .../libghostty-vt/src/terminal/compress/Page.zig | 14 + .../src/terminal/compress/lz4_differential.zig | 2 +- vendor/libghostty-vt/src/terminal/cursor.zig | 34 +- vendor/libghostty-vt/src/terminal/dcs.zig | 141 + .../libghostty-vt/src/terminal/device_status.zig | 60 +- vendor/libghostty-vt/src/terminal/formatter.zig | 1523 ++++-- vendor/libghostty-vt/src/terminal/fraction.zig | 142 + vendor/libghostty-vt/src/terminal/hash_map.zig | 315 +- vendor/libghostty-vt/src/terminal/hyperlink.zig | 4 +- vendor/libghostty-vt/src/terminal/kitty.zig | 2 + .../libghostty-vt/src/terminal/kitty/clipboard.zig | 82 + .../src/terminal/kitty/clipboard_command.zig | 502 ++ .../src/terminal/kitty/clipboard_grants.zig | 238 + .../src/terminal/kitty/clipboard_response.zig | 469 ++ .../src/terminal/kitty/clipboard_write.zig | 698 +++ vendor/libghostty-vt/src/terminal/kitty/dnd.zig | 69 + .../src/terminal/kitty/dnd_command.zig | 436 ++ .../libghostty-vt/src/terminal/kitty/dnd_drop.zig | 715 +++ .../src/terminal/kitty/dnd_response.zig | 248 + .../libghostty-vt/src/terminal/kitty/dnd_test.zig | 626 +++ .../libghostty-vt/src/terminal/kitty/graphics.zig | 8 +- .../src/terminal/kitty/graphics_animation.zig | 149 + .../src/terminal/kitty/graphics_command.zig | 914 +++- .../src/terminal/kitty/graphics_exec.zig | 3133 +++++++++++- .../src/terminal/kitty/graphics_image.zig | 1505 +++++- .../src/terminal/kitty/graphics_pixel.zig | 235 + .../src/terminal/kitty/graphics_storage.zig | 3883 +++++++++++++-- .../src/terminal/kitty/graphics_unicode.zig | 78 +- .../libghostty-vt/src/terminal/kitty/windows.zig | 277 ++ vendor/libghostty-vt/src/terminal/lib.zig | 1 + vendor/libghostty-vt/src/terminal/main.zig | 9 + vendor/libghostty-vt/src/terminal/mem.zig | 52 +- vendor/libghostty-vt/src/terminal/modes.zig | 196 +- vendor/libghostty-vt/src/terminal/mouse.zig | 3 +- vendor/libghostty-vt/src/terminal/osc.zig | 264 +- .../src/terminal/osc/kitty_metadata.zig | 94 + vendor/libghostty-vt/src/terminal/osc/parsers.zig | 1 + .../terminal/osc/parsers/clipboard_operation.zig | 20 +- .../src/terminal/osc/parsers/color.zig | 25 +- .../osc/parsers/kitty_clipboard_protocol.zig | 41 +- .../osc/parsers/kitty_desktop_notification.zig | 1201 +++++ .../terminal/osc/parsers/kitty_dnd_protocol.zig | 319 +- .../src/terminal/osc/parsers/kitty_text_sizing.zig | 2 +- .../src/terminal/osc/parsers/semantic_prompt.zig | 88 +- vendor/libghostty-vt/src/terminal/page.zig | 669 ++- vendor/libghostty-vt/src/terminal/parse_table.zig | 60 + vendor/libghostty-vt/src/terminal/paste.zig | 247 + vendor/libghostty-vt/src/terminal/point.zig | 10 +- .../libghostty-vt/src/terminal/ref_counted_set.zig | 209 +- vendor/libghostty-vt/src/terminal/render.zig | 268 +- vendor/libghostty-vt/src/terminal/search.zig | 1 + .../libghostty-vt/src/terminal/search/Thread.zig | 606 +-- .../libghostty-vt/src/terminal/search/active.zig | 6 +- .../libghostty-vt/src/terminal/search/pagelist.zig | 91 +- .../libghostty-vt/src/terminal/search/screen.zig | 264 +- .../src/terminal/search/sliding_window.zig | 79 +- .../libghostty-vt/src/terminal/search/terminal.zig | 767 +++ .../libghostty-vt/src/terminal/search/viewport.zig | 18 +- vendor/libghostty-vt/src/terminal/sgr.zig | 13 +- .../libghostty-vt/src/terminal/snapshot/AGENTS.md | 7 + .../src/terminal/snapshot/checkpoint.zig | 177 + .../src/terminal/snapshot/continuation.zig | 320 ++ .../src/terminal/snapshot/envelope.zig | 104 + .../src/terminal/snapshot/fixture.zig | 479 ++ .../libghostty-vt/src/terminal/snapshot/grid.zig | 2403 +++++++++ .../src/terminal/snapshot/history.zig | 716 +++ .../src/terminal/snapshot/hyperlink.zig | 493 ++ vendor/libghostty-vt/src/terminal/snapshot/io.zig | 55 + .../libghostty-vt/src/terminal/snapshot/main.zig | 182 + .../libghostty-vt/src/terminal/snapshot/page.zig | 1828 +++++++ .../libghostty-vt/src/terminal/snapshot/record.zig | 601 +++ .../libghostty-vt/src/terminal/snapshot/screen.zig | 2460 ++++++++++ .../src/terminal/snapshot/snapshot.ksy | 1073 ++++ .../src/terminal/snapshot/snapshot.zig | 2100 ++++++++ .../libghostty-vt/src/terminal/snapshot/style.zig | 423 ++ .../src/terminal/snapshot/terminal.zig | 1850 +++++++ .../snapshot/testdata/checkpoint-ready-v1.hex | 10 + .../src/terminal/snapshot/testdata/complete-v1.hex | 120 + .../snapshot/testdata/continuation-apc-v1.hex | 8 + .../snapshot/testdata/continuation-csi-v1.hex | 8 + .../snapshot/testdata/continuation-dcs-v1.hex | 8 + .../snapshot/testdata/continuation-esc-v1.hex | 8 + .../snapshot/testdata/continuation-ground-v1.hex | 8 + .../snapshot/testdata/continuation-osc-v1.hex | 8 + .../snapshot/testdata/continuation-utf8-v1.hex | 8 + .../src/terminal/snapshot/testdata/envelope-v1.hex | 10 + .../src/terminal/snapshot/testdata/grid-v1.hex | 15 + .../snapshot/testdata/history-header-v1.hex | 10 + .../snapshot/testdata/hyperlink-explicit-v1.hex | 10 + .../snapshot/testdata/hyperlink-implicit-v1.hex | 10 + .../snapshot/testdata/page-empty-record-v1.hex | 12 + .../terminal/snapshot/testdata/page-header-v1.hex | 11 + .../src/terminal/snapshot/testdata/page-v1.hex | 22 + .../snapshot/testdata/record-page-header-v1.hex | 10 + .../snapshot/testdata/screen-header-v1.hex | 13 + .../snapshot/testdata/screen-saved-cursor-v1.hex | 11 + .../src/terminal/snapshot/testdata/style-v1.hex | 10 + .../snapshot/testdata/terminal-header-v1.hex | 16 + .../src/terminal/snapshot/verify-kaitai.py | 400 ++ vendor/libghostty-vt/src/terminal/stream.zig | 1566 +++++- .../src/terminal/stream_continuation.zig | 606 +++ .../libghostty-vt/src/terminal/stream_terminal.zig | 4612 ++++++++++++++++-- vendor/libghostty-vt/src/terminal/style.zig | 222 +- vendor/libghostty-vt/src/terminal/sys.zig | 48 + vendor/libghostty-vt/src/terminal/tmux/control.zig | 2 +- vendor/libghostty-vt/src/terminal/tmux/output.zig | 24 +- vendor/libghostty-vt/src/terminal/tmux/viewer.zig | 31 +- 155 files changed, 61994 insertions(+), 7307 deletions(-) create mode 100644 vendor/libghostty-vt/src/terminal/c/io.zig create mode 100644 vendor/libghostty-vt/src/terminal/c/search.zig create mode 100644 vendor/libghostty-vt/src/terminal/c/snapshot.zig create mode 100644 vendor/libghostty-vt/src/terminal/c/types-schema-verify.py create mode 100644 vendor/libghostty-vt/src/terminal/c/types.schema.json create mode 100644 vendor/libghostty-vt/src/terminal/fraction.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/clipboard.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/clipboard_command.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/clipboard_grants.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/clipboard_response.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/clipboard_write.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/dnd.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/dnd_command.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/dnd_drop.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/dnd_response.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/dnd_test.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/graphics_animation.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/graphics_pixel.zig create mode 100644 vendor/libghostty-vt/src/terminal/kitty/windows.zig create mode 100644 vendor/libghostty-vt/src/terminal/osc/kitty_metadata.zig create mode 100644 vendor/libghostty-vt/src/terminal/osc/parsers/kitty_desktop_notification.zig create mode 100644 vendor/libghostty-vt/src/terminal/paste.zig create mode 100644 vendor/libghostty-vt/src/terminal/search/terminal.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/AGENTS.md create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/checkpoint.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/continuation.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/envelope.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/fixture.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/grid.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/history.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/hyperlink.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/io.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/main.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/page.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/record.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/screen.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/snapshot.ksy create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/snapshot.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/style.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/terminal.zig create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/checkpoint-ready-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/complete-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-apc-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-csi-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-dcs-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-esc-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-ground-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-osc-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-utf8-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/envelope-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/grid-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/history-header-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/hyperlink-explicit-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/hyperlink-implicit-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/page-empty-record-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/page-header-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/page-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/record-page-header-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/screen-header-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/screen-saved-cursor-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/style-v1.hex create mode 100644 vendor/libghostty-vt/src/terminal/snapshot/testdata/terminal-header-v1.hex create mode 100755 vendor/libghostty-vt/src/terminal/snapshot/verify-kaitai.py create mode 100644 vendor/libghostty-vt/src/terminal/stream_continuation.zig (limited to 'vendor/libghostty-vt/src/terminal') diff --git a/vendor/libghostty-vt/src/terminal/PageList.zig b/vendor/libghostty-vt/src/terminal/PageList.zig index 79aa96f1..94ef9bb0 100644 --- a/vendor/libghostty-vt/src/terminal/PageList.zig +++ b/vendor/libghostty-vt/src/terminal/PageList.zig @@ -9,11 +9,14 @@ const build_options = @import("terminal_options"); const Allocator = std.mem.Allocator; const assert = @import("../quirks.zig").inlineAssert; const fastmem = @import("../fastmem.zig"); +const simd = @import("../simd/main.zig"); const tripwire = @import("../tripwire.zig"); const DoublyLinkedList = @import("../datastruct/main.zig").IntrusiveDoublyLinkedList; +const datastruct = @import("../datastruct/main.zig"); const color = @import("color.zig"); const compression = @import("compress.zig"); const highlight = @import("highlight.zig"); +const hyperlink = @import("hyperlink.zig"); const kitty = @import("kitty.zig"); const terminal_mem = @import("mem.zig"); const point = @import("point.zig"); @@ -26,15 +29,25 @@ const Page = pagepkg.Page; const Row = pagepkg.Row; const log = std.log.scoped(.page_list); +const native_freestanding = builtin.os.tag == .freestanding and + !builtin.target.cpu.arch.isWasm(); -/// The number of PageList.Nodes we preheat the pool with. A node is -/// a very small struct so we can afford to preheat many, but the exact -/// number is uncertain. Any number too large is wasting memory, any number -/// too small will cause the pool to have to allocate more memory later. -/// This should be set to some reasonable minimum that we expect a terminal -/// window to scroll into quickly. +/// The number of pages we preheat the page pool with. For operating systems +/// that support it, pages are demand-paged (see PagePool) so this only +/// costs us address space. For other operating systems, we don't preheat. const page_preheat = 4; +/// The number of nodes we preheat the node pool with. Unlike pages, nodes +/// are ordinary heap memory so every idle preheated node costs real +/// memory. A new PageList needs exactly one node for its first page, so +/// we preheat that one and let the pool grow on demand. +const node_preheat = 1; + +/// The number of pins we preheat the pin pool with: the viewport pin that +/// every PageList tracks and the cursor pin that every Screen tracks. +/// Selections, searches, and so on grow the pool on demand. +const pin_preheat = 2; + /// The list of pages in the screen. These are expected to be in order /// where the first page is the topmost page (scrollback) and the last is /// the bottommost page (the current active page). @@ -294,7 +307,14 @@ const Node = struct { }; /// The memory pool we get page nodes from. -const NodePool = std.heap.MemoryPool(List.Node); +/// +/// We don't use a std memory pool here because it is backed by an arena +/// and we end up paying a lot of wasted memory for the growth factor when +/// in practice we don't usually use many nodes. +/// +/// We don't need the "untouched" property of our UntouchedPool but +/// this gives us a GPA-allocated pool so we reuse it here. +const NodePool = datastruct.UntouchedPool(List.Node, .of(List.Node)); /// The standard page capacity that we use as a starting point for /// all pages. This is chosen as a sane default that fits most terminal @@ -304,18 +324,37 @@ const std_capacity = pagepkg.std_capacity; /// The byte size required for a standard page. const std_size = Page.layout(std_capacity).total_size; +/// True when the page pool is the wasm page pool, which recycles items +/// through a free list shared by the whole module instance instead of +/// dying with the pool. +/// +/// Test builds use the native pool even on wasm so that pool memory goes +/// through the testing allocator and participates in leak detection. +const wasm_page_pool = builtin.target.cpu.arch.isWasm() and !builtin.is_test; + /// The memory pool we use for page memory buffers. We use a separate pool /// so we can allocate these with a page allocator. We have to use a page /// allocator because we need memory that is zero-initialized and page-aligned. -const PagePool = std.heap.MemoryPoolAligned( - [std_size]u8, - .fromByteUnits(std.heap.page_size_min), -); +const PagePool = if (wasm_page_pool) + datastruct.WasmPagePool([std_size]u8) +else untouched: { + // Untouched pools never read/write to the items so that we can + // use demand-paging on operating systems that support it. This makes + // it so that an idle item in the pool costs no physical memory, + // only virtual memory. + // + // Contract for PageList is that every path that returns an item + // to the pool must zero it. + break :untouched datastruct.UntouchedPool( + [std_size]u8, + .fromByteUnits(std.heap.page_size_min), + ); +}; /// List of pins, known as "tracked" pins. These are pins that are kept /// up to date automatically through page-modifying operations. const PinSet = std.AutoArrayHashMapUnmanaged(*Pin, void); -const PinPool = std.heap.MemoryPool(Pin); +const PinPool = std.heap.memory_pool.Managed(Pin); /// The pool of memory used for a pagelist. This can be shared between /// multiple pagelists but it is not threadsafe. @@ -332,11 +371,11 @@ pub const MemoryPool = struct { page_alloc: Allocator, preheat: usize, ) Allocator.Error!MemoryPool { - var node_pool = try NodePool.initPreheated(gen_alloc, preheat); + var node_pool = try NodePool.initCapacity(gen_alloc, gen_alloc, node_preheat); errdefer node_pool.deinit(); - var page_pool = try PagePool.initPreheated(page_alloc, preheat); + var page_pool = try PagePool.initCapacity(gen_alloc, page_alloc, preheat); errdefer page_pool.deinit(); - var pin_pool = try PinPool.initPreheated(gen_alloc, 8); + var pin_pool = try PinPool.initCapacity(gen_alloc, pin_preheat); errdefer pin_pool.deinit(); return .{ .alloc = gen_alloc, @@ -401,15 +440,17 @@ page_size: usize, /// in the state struct. page_compression: IncrementalCompressionState = .{}, -/// Maximum size of the page allocation in bytes. This only includes pages -/// that are used ONLY for scrollback. If the active area is still partially -/// in a page that also includes scrollback, then that page is not included. -explicit_max_size: usize, +/// A node available for immediate reuse by createPage, bypassing the +/// memory pool round trip. This is only set during column reflow +/// (resizeCols), which destroys one source page for roughly every +/// destination page it creates: returning a page buffer to the pool +/// decommits it and taking one back recommits it, and that syscall +/// pair per page is a significant part of reflow cost. Always null +/// outside of an in-progress reflow. +recycle_node: ?*List.Node = null, -/// This is the minimum max size that we will respect due to the rows/cols -/// of the PageList. We must always be able to fit at least the active area -/// and at least two pages for our algorithms. -min_max_size: usize, +/// Limits for scrollback. +limits: Limits, /// The total number of rows represented by this PageList. This is used /// specifically for scrollbar information so we can have the total size. @@ -478,59 +519,6 @@ pub const Viewport = union(enum) { pin, }; -/// Returns the minimum valid "max size" for a given number of rows and cols -/// such that we can fit the active area AND at least two pages. Note we -/// need the two pages for algorithms to work properly (such as grow) but -/// we don't need to fit double the active area. -/// -/// This min size may not be totally correct in the case that a large -/// number of other dimensions makes our row size in a page very small. -/// But this gives us a nice fast heuristic for determining min/max size. -/// Therefore, if the page size is violated you should always also verify -/// that we have enough space for the active area. -fn minMaxSize(cols: size.CellCountInt, rows: size.CellCountInt) usize { - // Invariant required to ensure our divCeil below cannot overflow. - comptime { - const max_rows = std.math.maxInt(size.CellCountInt); - _ = std.math.divCeil(usize, max_rows, 1) catch unreachable; - } - - // Get our capacity to fit our rows. If the cols are too big, it may - // force less rows than we want meaning we need more than one page to - // represent a viewport. - const cap = initialCapacity(cols); - - // Calculate the number of standard sized pages we need to represent - // an active area. - const pages_exact = if (cap.rows >= rows) 1 else std.math.divCeil( - usize, - rows, - cap.rows, - ) catch { - // Not possible: - // - initialCapacity guarantees at least 1 row - // - numerator/denominator can't overflow because of comptime check above - unreachable; - }; - - // We always need at least one page extra so that we - // can fit partial pages to spread our active area across two pages. - // Even for caps that can't fit all rows in a single page, we add one - // because the most extra space we need at any given time is only - // the partial amount of one page. - const pages = pages_exact + 1; - assert(pages >= 2); - - // log.debug("minMaxSize cols={} rows={} cap={} pages={}", .{ - // cols, - // rows, - // cap, - // pages, - // }); - - return PagePool.item_size * pages; -} - /// Calculates the initial capacity for a new page for a given column /// count. This will attempt to fit within std_size at all times so we /// can use our memory pool, but if cols is too big, this will return a @@ -575,12 +563,19 @@ fn initialCapacity(cols: size.CellCountInt) Capacity { return cap; } -/// This is the page allocator we'll use for all our underlying -/// VM page allocations. -inline fn pageAllocator() Allocator { +/// Returns the allocator used for underlying page allocations. +/// +/// `alloc` is the caller-provided allocator. It is used on native freestanding +/// targets, where no OS page allocator is available. Other targets select a +/// platform-specific allocator below. +inline fn pageAllocator(alloc: Allocator) Allocator { // In tests we use our testing allocator so we can detect leaks. if (builtin.is_test) return std.testing.allocator; + // Native freestanding targets don't have an OS page allocator, so use + // the allocator provided by the embedder. + if (native_freestanding) return alloc; + // On non-macOS we use our standard Zig page allocator. if (!builtin.target.os.tag.isDarwin()) return std.heap.page_allocator; @@ -597,29 +592,53 @@ const init_tw = tripwire.module(enum { viewport_pin_track, }, init); +pub const Options = struct { + /// The initial active-area size. This can be resized with `resize`. + cols: size.CellCountInt, + rows: size.CellCountInt, + + /// The maximum number of bytes allocated for pages. The effective limit + /// is raised when necessary to hold the active area. Null is unlimited. + max_size: ?usize = null, + + /// The maximum number of scrollback rows, excluding the active + /// area. Rows are physical (viewed) rows, so a wrapped row counts as + /// multiple rows. Max line pruning only happens at page-boundaries + /// (the minimum internal allocation size) so in practice the max lines + /// is always slightly larger than configured. + /// + /// Null is unlimited. + max_lines: ?usize = null, +}; + /// Initialize the page. The top of the first page in the list is always the /// top of the active area of the screen (important knowledge for quickly /// setting up cursors in Screen). /// -/// max_size is the maximum number of bytes that will be allocated for +/// `max_size` is the maximum number of bytes that will be allocated for /// pages. If this is smaller than the bytes required to show the viewport /// then max_size will be ignored and the viewport will be shown, but no /// scrollback will be created. max_size is always rounded down to the nearest /// terminal page size (not virtual memory page), otherwise we would always /// slightly exceed max_size in the limits. /// -/// If max_size is null then there is no defined limit and the screen will -/// grow forever. In reality, the limit is set to the byte limit that your -/// computer can address in memory. If you somehow require more than that -/// (due to disk paging) then please contribute that yourself and perhaps -/// search deep within yourself to find out why you need that. +/// `max_lines` is the maximum number of physical rows retained as scrollback, +/// excluding the active area. It is a page-granular heuristic: at least one +/// standard page worth of rows is permitted and only complete historical +/// pages are removed. +/// +/// If either limit is null then that dimension has no defined limit and the +/// screen will grow forever. In reality, the limit is set to the amount your +/// computer can address in memory. If you somehow require more than that (due +/// to disk paging) then please contribute that yourself and perhaps search +/// deep within yourself to find out why you need that. pub fn init( alloc: Allocator, - cols: size.CellCountInt, - rows: size.CellCountInt, - max_size: ?usize, + opts: Options, ) Allocator.Error!PageList { const tw = init_tw; + const cols = opts.cols; + const rows = opts.rows; // The screen starts with a single page that is the entire viewport, // and we'll split it thereafter if it gets too large and add more as @@ -627,7 +646,7 @@ pub fn init( try tw.check(.init_memory_pool); var pool = try MemoryPool.init( alloc, - pageAllocator(), + pageAllocator(alloc), page_preheat, ); errdefer pool.deinit(); @@ -640,19 +659,20 @@ pub fn init( cols, rows, ); + errdefer releasePages(&pool, page_list); - // Get our minimum max size, see doc comments for more details. - const min_max_size = minMaxSize(cols, rows); + var limits: Limits = .init(cols, rows); + limits.set(.bytes, opts.max_size); + limits.set(.lines, opts.max_lines); // We always track our viewport pin to ensure this is never an allocation try tw.check(.viewport_pin); const viewport_pin = try pool.pins.create(); viewport_pin.* = .{ .node = page_list.first.? }; - var tracked_pins: PinSet = .{}; - errdefer tracked_pins.deinit(pool.alloc); try tw.check(.viewport_pin_track); - try tracked_pins.putNoClobber(pool.alloc, viewport_pin, {}); + var tracked_pins = try initTrackedPins(pool.alloc, viewport_pin); + errdefer tracked_pins.deinit(pool.alloc); errdefer comptime unreachable; const result: PageList = .{ @@ -663,8 +683,7 @@ pub fn init( .page_serial = page_serial, .page_serial_epoch = 0, .page_size = page_size, - .explicit_max_size = max_size orelse std.math.maxInt(usize), - .min_max_size = min_max_size, + .limits = limits, .total_rows = rows, .tracked_pins = tracked_pins, .viewport = .{ .active = {} }, @@ -675,6 +694,17 @@ pub fn init( return result; } +/// Create the tracked pin set for a new PageList with the viewport pin +/// already tracked. The set is sized for exactly the viewport pin and the +/// cursor pin that every Screen tracks. +fn initTrackedPins(alloc: Allocator, viewport_pin: *Pin) Allocator.Error!PinSet { + var set: PinSet = .{}; + errdefer set.deinit(alloc); + try set.entries.setCapacity(alloc, pin_preheat); + set.putAssumeCapacityNoClobber(viewport_pin, {}); + return set; +} + const initPages_tw = tripwire.module(enum { page_node, page_buf_std, @@ -696,23 +726,14 @@ fn initPages( const cap = initialCapacity(cols); const layout = Page.layout(cap); const pooled = layout.total_size <= std_size; - const page_alloc = pool.pages.arena.child_allocator; + const page_alloc = pool.pages.allocator; // Guaranteed by comptime checks in initialCapacity but // redundant here for safety. assert(layout.total_size <= size.max_page_size); - // If we have an error, we need to clean up our heap-owned pages - // since they're not in the pool. - errdefer { - var it = page_list.first; - while (it) |node| : (it = node.next) { - switch (node.owned) { - .pool => {}, - .heap => page_alloc.free(node.page().memory), - } - } - } + // If we have an error, we need to release the pages we created. + errdefer releasePages(pool, page_list); var rem = rows; while (rem > 0) { @@ -794,6 +815,7 @@ pub inline fn pauseIntegrityChecks(self: *PageList, pause: bool) void { } const IntegrityError = error{ + MaxLinesExceeded, PageSerialInvalid, TotalRowsMismatch, TrackedPinInvalid, @@ -838,6 +860,21 @@ fn verifyIntegrity(self: *const PageList) IntegrityError!void { return IntegrityError.TotalRowsMismatch; } + // A line limit may only be exceeded when the oldest page also contains + // active rows. Complete historical pages are always eligible for pruning. + if (self.total_rows > self.rows) { + const history_rows = self.total_rows - self.rows; + if (history_rows > self.limits.max(.lines) and + self.pages.first.? != self.getTopLeft(.active).node) + { + log.warn( + "PageList integrity violation: max lines exceeded history={} max={}", + .{ history_rows, self.limits.max(.lines) }, + ); + return IntegrityError.MaxLinesExceeded; + } + } + // Verify that all our tracked pins point to valid pages. for (self.tracked_pins.keys()) |p| { if (!self.pinIsValid(p.*)) return error.TrackedPinInvalid; @@ -885,6 +922,45 @@ fn verifyIntegrity(self: *const PageList) IntegrityError!void { } } +/// Release every page in the list during a teardown walk (deinit, +/// reset, or an errdefer unwinding a partially built list): heap-owned +/// pages go back to the page allocator, pool-owned pages back to the +/// page pool, and the nodes back to the node pool's free list. +fn releasePages(pool: *MemoryPool, list: List) void { + const page_alloc = pool.pages.allocator; + var it = list.first; + while (it) |node| { + it = node.next; + const page = node.restore(.discard); + switch (node.owned) { + .pool => releasePoolPage(pool, page), + .heap => page_alloc.free(page.memory), + } + pool.nodes.destroy(node); + } +} + +/// Release a pool-owned page during a teardown walk. Unlike +/// destroyNodeExt, this does not zero the page: on native, the item goes +/// straight back to the page allocator, and zeroing it first would write +/// the whole page (and fault a decommitted mapping back in) only for it +/// to be unmapped. +fn releasePoolPage(pool: *MemoryPool, page: *const Page) void { + const item: *align(std.heap.page_size_min) [std_size]u8 = + @ptrCast(@alignCast(page.memory.ptr)); + + // The wasm pool's items are shared by every pool in the module and + // never return to the allocator, so they go back to the free list + // zeroed for reuse (mirroring destroyNodeExt). + if (comptime wasm_page_pool) { + _ = terminal_mem.decommit(.zero, item, page.memory.len); + pool.pages.destroy(item); + return; + } + + pool.pages.release(item); +} + /// Deinit the pagelist, freeing all page memory and the memory pool. pub fn deinit(self: *PageList) void { // Verify integrity before cleanup @@ -893,20 +969,8 @@ pub fn deinit(self: *PageList) void { // Always deallocate our hashmap. self.tracked_pins.deinit(self.pool.alloc); - // Go through our linked list and deallocate all pages that are - // heap-owned (not in the pool). - const page_alloc = self.pool.pages.arena.child_allocator; - var it = self.pages.first; - while (it) |node| : (it = node.next) { - const page = node.restore(.discard); - switch (node.owned) { - .pool => {}, - .heap => page_alloc.free(page.memory), - } - } - - // Deallocate all the pages. We don't need to deallocate the list or - // nodes because they all reside in the pool. + // Release every page and node back to the pools, then free the pools. + releasePages(&self.pool, self.pages); self.pool.deinit(); } @@ -942,24 +1006,18 @@ pub fn reset(self: *PageList) void { cap.rows, ) catch unreachable; - // Before resetting our pools we need to free any pages that - // are heap-owned since those were allocated outside the pool. - { - const page_alloc = self.pool.pages.arena.child_allocator; - var it = self.pages.first; - while (it) |node| : (it = node.next) { - const page = node.restore(.discard); - switch (node.owned) { - .pool => {}, - .heap => page_alloc.free(page.memory), - } - } - } + // Before resetting our pools we need to release our pages: heap-owned + // pages go back to the page allocator and pool-owned pages back to + // the page pool. + releasePages(&self.pool, self.pages); // Reset our pools to free as much memory as possible while retaining // the capacity for at least the minimum number of pages we need. // The return value is whether memory was reclaimed or not, but in // either case the pool is left in a valid state. + // + // Retained page pool items are zero (see PagePool), so there is + // nothing to scrub before initPages reuses them. _ = self.pool.pages.reset(.{ .retain_with_limit = page_count * PagePool.item_size, }); @@ -967,32 +1025,6 @@ pub fn reset(self: *PageList) void { .retain_with_limit = page_count * NodePool.item_size, }); - // Our page pool relies on mmap to zero our page memory. Since we're - // retaining a certain amount of memory, it won't use mmap and won't - // be zeroed. This block zeroes out all the memory in the pool arena. - { - // Note: we only have to do this for the page pool because the - // nodes are always fully overwritten on each allocation. - const page_arena = &self.pool.pages.arena; - var it = page_arena.state.buffer_list.first; - while (it) |node| : (it = node.next) { - // WARN: Since HeapAllocator's BufNode is not public API, - // we have to hardcode its layout here. We do a comptime assert - // on Zig version to verify we check it on every bump. - const BufNode = struct { - data: usize, - node: std.SinglyLinkedList.Node, - }; - const buf_node: *BufNode = @fieldParentPtr("node", node); - - // The fully allocated buffer - const alloc_buf = @as([*]u8, @ptrCast(buf_node))[0..buf_node.data]; - // The buffer minus our header - const data_buf = alloc_buf[@sizeOf(BufNode)..]; - @memset(data_buf, 0); - } - } - // Initialize our pages. This should not be able to fail since // we retained the capacity for the minimum number of pages we need. self.pages, self.page_size = initPages( @@ -1073,7 +1105,7 @@ pub fn clone( // Setup our pool var pool: MemoryPool = try .init( alloc, - pageAllocator(), + pageAllocator(alloc), page_count, ); errdefer pool.deinit(); @@ -1081,22 +1113,12 @@ pub fn clone( // Create our viewport. In a clone, the viewport always goes // to the top. const viewport_pin = try pool.pins.create(); - var tracked_pins: PinSet = .{}; + var tracked_pins = try initTrackedPins(pool.alloc, viewport_pin); errdefer tracked_pins.deinit(pool.alloc); - try tracked_pins.putNoClobber(pool.alloc, viewport_pin, {}); // Our list of pages var page_list: List = .{}; - errdefer { - const page_alloc = pool.pages.arena.child_allocator; - var page_it = page_list.first; - while (page_it) |node| : (page_it = node.next) { - switch (node.owned) { - .pool => {}, - .heap => page_alloc.free(node.page().memory), - } - } - } + errdefer releasePages(&pool, page_list); // Copy our pages var page_serial: u64 = 0; @@ -1111,6 +1133,11 @@ pub fn clone( &page_serial, &page_size, ); + + // Add the page to the list immediately so that the errdefer + // above releases it if cloning fails. + page_list.append(node); + const dst_page = node.page(); const src_page = chunk.node.page(); assert(node.capacity().rows >= chunk.end - chunk.start); @@ -1125,8 +1152,6 @@ pub fn clone( dst_page.dirty = src_page.dirty; - page_list.append(node); - total_rows += node.rows(); // Remap our tracked pins by changing the page and @@ -1158,8 +1183,7 @@ pub fn clone( .page_serial = page_serial, .page_serial_epoch = 0, .page_size = page_size, - .explicit_max_size = self.explicit_max_size, - .min_max_size = self.min_max_size, + .limits = self.limits, .cols = self.cols, .rows = self.rows, .total_rows = total_rows, @@ -1189,6 +1213,9 @@ pub fn clone( result.total_rows = result.rows; } + // A clone can copy more history than its inherited line limit. + result.limits.enforce(&result, .lines); + result.assertIntegrity(); return result; } @@ -1241,17 +1268,22 @@ pub fn resize(self: *PageList, opts: Resize) Allocator.Error!void { // its too easy to get the logic wrong in here. self.viewport_pin_row_offset = null; - if (!opts.reflow) return try self.resizeWithoutReflow(opts); + if (!opts.reflow) { + try self.resizeWithoutReflow(opts); + // Shrinking the active row count turns former active rows into + // scrollback even without reflow, which can cross the line limit. + self.limits.enforce(self, .lines); + return; + } - // Recalculate our minimum max size. This allows grow to work properly - // when increasing beyond our initial minimum max size or explicit max - // size to fit the active area. - const old_min_max_size = self.min_max_size; - self.min_max_size = minMaxSize( + // Recalculate our minimum limits. This allows grow to work properly when + // increasing beyond the explicit limits to fit the active area. + const old_limits = self.limits; + self.limits.resize( opts.cols orelse self.cols, opts.rows orelse self.rows, ); - errdefer self.min_max_size = old_min_max_size; + errdefer self.limits = old_limits; // On reflow, the main thing that causes reflow is column changes. If // only rows change, reflow is impossible. So we change our behavior based @@ -1288,6 +1320,11 @@ pub fn resize(self: *PageList, opts: Resize) Allocator.Error!void { }, .active, .top => {}, } + + // Column reflow can change the physical history row count, and a row + // resize can move the active boundary. Both may expose whole old pages + // that are now eligible for line-limit pruning. + self.limits.enforce(self, .lines); } /// Resize the pagelist with reflow by adding or removing columns. @@ -1402,6 +1439,14 @@ fn resizeCols( } } + // Reflowed source pages are stashed for reuse as destination + // pages (see recycle_node). Whether we succeed or fail, a stashed + // node must not outlive the reflow. + defer if (self.recycle_node) |node| { + self.recycle_node = null; + self.destroyNode(node); + }; + // Set our new page as the only page. This orphans the existing pages // in the list, but that's fine since we're gonna delete them anyway. self.pages.first = first_rewritten_node; @@ -1417,11 +1462,21 @@ fn resizeCols( if (preserved_cursor) |c| c.tracked_pin else null, ); - // Once we're done reflowing a page, destroy it immediately. - // This frees memory and makes it more likely in memory - // constrained environments that the next reflow will work. - if (row.y == row.node.rows() - 1) { - self.destroyNode(row.node); + // Once we're done reflowing a page, we're done with it, so + // make it available for reuse (or destroy it). Making it + // immediately available frees memory and makes it more + // likely in memory constrained environments that the next + // reflow will work. + if (row.y == row.node.rows() - 1) destroy_node: { + if (self.recycle_node != null or + row.node.owned != .pool or + row.node.data != .resident) + { + self.destroyNode(row.node); + break :destroy_node; + } + + self.recycle_node = row.node; } } @@ -1508,6 +1563,37 @@ const ReflowCursor = struct { /// This is the final row count of the reflowed pages. total_rows: usize, + /// Memoizes the most recent source-to-destination style id + /// mapping. Styled cells come in long runs sharing the same style + /// so this lets writeCell bump the destination ref count directly + /// instead of performing a set lookup for every styled cell. + /// + /// The destination id is only valid for the current destination + /// page, which is why this lives on the cursor: every destination + /// page change goes through init() which resets this. + style_cache: StyleCache, + + /// Memoizes the capacity adjustment for new destination pages + /// (see reflowRow). It only depends on the source page so this is + /// keyed by the source page pointer, which reflow visits + /// sequentially and never revisits. + cap_memo: ?struct { + src_page: *const Page, + cap: Capacity, + }, + + const StyleCache = struct { + src_page: ?*const Page, + src_id: stylepkg.Id, + dst_id: stylepkg.Id, + + const invalid: StyleCache = .{ + .src_page = null, + .src_id = stylepkg.default_id, + .dst_id = stylepkg.default_id, + }; + }; + fn init(node: *List.Node) ReflowCursor { const page = node.page(); const rows = page.rows.ptr(page.memory); @@ -1523,6 +1609,9 @@ const ReflowCursor = struct { // Initially whatever size our input node is. .total_rows = node.rows(), + + .style_cache = .invalid, + .cap_memo = null, }; } @@ -1552,12 +1641,21 @@ const ReflowCursor = struct { if (cols_len == 0 and src_row.semantic_prompt != .none) cols_len = 1; } - // Handle tracked pin adjustments. + // Handle tracked pin adjustments. We also note whether any + // tracked pin is on this row at all so that the per-cell loop + // below can skip pin scans entirely for the overwhelmingly + // common case of a row with no pins. Note we compare nodes + // rather than pages since a node owns exactly one page; this + // is cheaper and avoids `page()` restoring unrelated + // compressed nodes purely for a comparison. + var row_has_pins = false; { const pin_keys = list.tracked_pins.keys(); for (pin_keys) |p| { - if (p.node.page() != src_page or - p.y != src_y) continue; + if (p.node != row.node or p.y != src_y) continue; + + // This row has pins + row_has_pins = true; if (cursor_pin != null and p == cursor_pin.?) continue; @@ -1579,7 +1677,7 @@ const ReflowCursor = struct { // If the cursor is after blanks on the right, those cells are still // before the next write and must reflow with it. if (cursor_pin) |p| { - if (p.node.page() == src_page and p.y == src_y) { + if (p.node == row.node and p.y == src_y) { cols_len = @max(cols_len, p.x + 1); } } @@ -1596,18 +1694,16 @@ const ReflowCursor = struct { // Inherit increased styles or grapheme bytes from the src page // we're reflowing from for new pages. - const cap = src_page.capacity.adjust( - .{ .cols = self.page.size.cols }, - ) catch |err| err: { - comptime assert(@TypeOf(err) == error{OutOfMemory}); - - var cap = src_page.capacity; - cap.cols = self.page.size.cols; - // We're already a non-standard page. We don't want to - // inherit a massive set of rows, so cap it at our std size. - cap.rows = @min(src_page.size.rows, std_capacity.rows); - break :err cap; - }; + // + // This only depends on the source page, which we process row + // by row, so memoize it: computing the adjustment requires a + // full page layout calculation which is much too expensive to + // do for every row. + const cap: Capacity = if (self.cap_memo) |memo| cap: { + if (memo.src_page == src_page) break :cap memo.cap; + // Source page changed, fall through to recompute. + break :cap self.computeAndMemoizeCap(src_page); + } else self.computeAndMemoizeCap(src_page); // Our row isn't blank, write any new rows we deferred. while (self.new_rows > 0) { @@ -1626,11 +1722,32 @@ const ReflowCursor = struct { self.page_row.wrap_continuation = true; } + // Fast path: bulk-copy a run of simple cells directly + // into the destination row. The vast majority of cells + // are narrow cells or complete wide pairs, have no + // managed memory (graphemes, hyperlinks), and share a + // single style in long runs, so this avoids the + // per-cell state machine below for most of the work. + // Rows with tracked pins take the slow path so pin + // remapping behaves identically. + if (!row_has_pins) { + const max_run = @min( + cols_len - x, + @as(usize, self.page.size.cols) - self.x, + ); + const window = cells[x..][0..max_run]; + const run = bulkRunLength(window); + if (run > 0 and self.copyRun(window[0..run], src_page)) { + x += run; + continue; + } + } + // Move any tracked pins from the source. - { + if (row_has_pins) { const pin_keys = list.tracked_pins.keys(); for (pin_keys) |p| { - if (p.node.page() != src_page or + if (p.node != row.node or p.y != src_y or p.x != x) continue; @@ -1653,16 +1770,15 @@ const ReflowCursor = struct { .skip_next => { // Remap any tracked pins at the skipped position (x+1) // since we won't process that cell in the loop. - const pin_keys = list.tracked_pins.keys(); - for (pin_keys) |p| { - if (p.node.page() != src_page or + if (row_has_pins) for (list.tracked_pins.keys()) |p| { + if (p.node != row.node or p.y != src_y or p.x != x + 1) continue; p.node = self.node; p.x = self.x; p.y = self.y; - } + }; x += 2; }, @@ -1700,6 +1816,280 @@ const ReflowCursor = struct { } } + /// Compute and memoize the new-page capacity for the given source + /// page. See the call site in reflowRow for details. + fn computeAndMemoizeCap( + self: *ReflowCursor, + src_page: *const Page, + ) Capacity { + const cap = src_page.capacity.adjust( + .{ .cols = self.page.size.cols }, + ) catch |err| err: { + comptime assert(@TypeOf(err) == error{OutOfMemory}); + + var cap = src_page.capacity; + cap.cols = self.page.size.cols; + // We're already a non-standard page. We don't want to + // inherit a massive set of rows, so cap it at our std size. + cap.rows = @min(src_page.size.rows, std_capacity.rows); + break :err cap; + }; + + self.cap_memo = .{ .src_page = src_page, .cap = cap }; + return cap; + } + + /// True if this cell can be copied verbatim as part of a bulk + /// run: a narrow plain-text or bg-color cell with no managed + /// memory (graphemes, hyperlinks) and no special reflow handling + /// (wide characters, spacers, Kitty virtual placeholders). For + /// these cells writeCell reduces to a copy of the raw cell plus + /// a style ref count adjustment. + inline fn bulkCopyable(cell: pagepkg.Cell) bool { + return switch (cell.content_tag) { + .codepoint => copyable: { + if (cell.wide != .narrow) break :copyable false; + if (cell.hyperlink) break :copyable false; + if (comptime build_options.kitty_graphics) { + // Placeholders must set a row flag, so they take + // the slow path. + if (cell.content.codepoint.data == + kitty.graphics.unicode.placeholder) + { + break :copyable false; + } + } + break :copyable true; + }, + + // Grapheme data must be cloned cell-by-cell. + .codepoint_grapheme => false, + + // These are guaranteed to have no style or grapheme data + // (see writeCell) so they are pure copies. The style + // check is defensive so that a bg cell can never join or + // extend a styled run. + .bg_color_palette, + .bg_color_rgb, + => cell.style_id == stylepkg.default_id, + }; + } + + /// The group length for the vectorized bulk run scan below: the + /// SIMD lane count where the target supports it, otherwise a + /// plain unrolled group like other cell scans use (e.g. the + /// render state scans). + const bulk_group_len = simd.lanes(u64) orelse 8; + + /// Masked compare helper covering every cell field that a run + /// must share to be copied by copyRun: given that the first cell + /// of a run passed the full bulkCopyable predicate, equality on + /// these fields implies the same for every subsequent cell, with + /// the same style. + /// + /// Note this is slightly stricter than bulkCopyable (e.g. a + /// bg-color cell won't extend an unstyled text run even though it + /// is copyable): that only splits the copy into multiple runs, + /// which is still correct. + const BulkRunMask = pagepkg.Mask(pagepkg.Cell, &.{ + "content_tag", + "style_id", + "wide", + "hyperlink", + }, bulk_group_len); + + /// Masked compare helper for detecting the Kitty virtual + /// placeholder codepoint in text cells. Placeholders take the + /// slow path (they must set a row flag), so they terminate a run. + const PlaceholderMask = pagepkg.Mask(pagepkg.Cell, &.{ + "content.codepoint.data", + }, bulk_group_len); + + /// The length of the prefix of cells that can be copied at once + /// with copyRun: bulk-copyable cells sharing a single style. The + /// scan uses masked compares of the raw cell bits (see + /// BulkRunMask), which is significantly cheaper than the + /// field-wise predicate for this hot loop. + fn bulkRunLength(cells: []const pagepkg.Cell) usize { + if (cells.len == 0) return 0; + const first = cells[0]; + + // A run may start on a bulk-copyable narrow cell or on a wide pair. + const pair_start = first.content_tag == .codepoint and + first.wide == .wide and + !first.hyperlink; + if (!pair_start and !bulkCopyable(first)) return 0; + + // Patterns for each cell shape admitted to the run. + var proto = first; + proto.wide = .narrow; + const narrow_pattern = BulkRunMask.pattern(proto); + proto.wide = .wide; + const wide_pattern = BulkRunMask.pattern(proto); + proto.wide = .spacer_tail; + const tail_pattern = BulkRunMask.pattern(proto); + + // Only text cells can contain a placeholder; for bg color + // tags the content bits are a color, so we skip the check for + // those (the tag is part of BulkRunMask, making tags uniform + // per run). + const check_placeholder = build_options.kitty_graphics and + first.content_tag == .codepoint; + const placeholder_pattern = comptime pattern: { + // Never used without kitty graphics (check_placeholder + // is comptime-false), but it must still compile and the + // placeholder codepoint doesn't exist in that build. + if (!build_options.kitty_graphics) break :pattern 0; + + break :pattern PlaceholderMask.pattern(.init( + kitty.graphics.unicode.placeholder, + )); + }; + + var len: usize = 0; + outer: while (true) { + // Vectorized scan: check whole groups of narrow cells at + // once. If a group fully matches, the run extends by the + // whole group; otherwise fall through to the scalar loop + // below, which finds the exact end of the run within it + // or continues through a wide pair. + while (cells.len - len >= bulk_group_len) { + if (!BulkRunMask.eql(cells, len, narrow_pattern)) break; + if (check_placeholder and PlaceholderMask.eqlAny( + cells, + len, + placeholder_pattern, + )) break; + len += bulk_group_len; + } + + while (len < cells.len) { + const cell = cells[len]; + if (BulkRunMask.eqlScalar(cell, narrow_pattern)) { + if (check_placeholder and PlaceholderMask.eqlScalar( + cell, + placeholder_pattern, + )) break :outer; + len += 1; + continue; + } + + // Wide pairs are consumed atomically so the run + // (or window) end can never split a pair. + if (len + 1 < cells.len and + BulkRunMask.eqlScalar(cell, wide_pattern) and + BulkRunMask.eqlScalar(cells[len + 1], tail_pattern)) + { + len += 2; + // Re-enter the vectorized loop: narrow groups + // commonly follow a stretch of pairs. + continue :outer; + } + + break :outer; + } + + break; + } + + return len; + } + + /// Copy a run of bulk-copyable cells (see bulkCopyable) sharing + /// one style into the destination row at the current position, + /// then advance the cursor. The run must fit in the remaining + /// columns of the destination row. + /// + /// Returns false without any state change if the style could not + /// be mapped into the destination page without a capacity + /// change; the caller should fall back to writeCell which + /// handles growing capacity. + fn copyRun( + self: *ReflowCursor, + src_cells: []const pagepkg.Cell, + src_page: *const Page, + ) bool { + assert(!self.pending_wrap); + assert(src_cells.len >= 1); + assert(src_cells.len <= self.page.size.cols - self.x); + + const style_id = src_cells[0].style_id; + const n: u16 = @intCast(src_cells.len); + + // Resolve the destination style id for this run and take one + // reference per cell. This mirrors the per-cell style logic + // in writeCell, including the memoization (see StyleCache). + const dst_style_id: stylepkg.Id = if (style_id == stylepkg.default_id) + stylepkg.default_id + else dst: { + if (self.style_cache.src_page == @as(?*const Page, src_page) and + self.style_cache.src_id == style_id) + { + const id = self.style_cache.dst_id; + self.page.styles.useMultiple(self.page.memory, id, n); + break :dst id; + } + + const style = src_page.styles.get( + src_page.memory, + style_id, + ).*; + + // Any error here (set full or needs rehash) is handled + // by falling back to the slow path, which grows capacity. + // No state has been modified yet at this point. + const id = (self.page.styles.addWithId( + self.page.memory, + style, + style_id, + ) catch return false) orelse style_id; + + // addWithId took one reference, take the rest. + if (n > 1) self.page.styles.useMultiple( + self.page.memory, + id, + n - 1, + ); + + self.style_cache = .{ + .src_page = src_page, + .src_id = style_id, + .dst_id = id, + }; + + break :dst id; + }; + + // Copy the raw cell contents. + const dst_cells: []pagepkg.Cell = @as( + [*]pagepkg.Cell, + @ptrCast(self.page_cell), + )[0..src_cells.len]; + @memcpy(dst_cells, src_cells); + + // If the style resolved to a different id in the destination + // page then rewrite the copied cells to point at it. + if (dst_style_id != style_id) { + for (dst_cells) |*cell| cell.style_id = dst_style_id; + } + if (dst_style_id != stylepkg.default_id) self.page_row.styled = true; + + // Advance the cursor, matching what repeated cursorForward + // calls after each cell write would have done. + const cols = self.page.size.cols; + const cell_ptr: [*]pagepkg.Cell = @ptrCast(self.page_cell); + if (self.x + n == cols) { + self.x = cols - 1; + self.page_cell = @ptrCast(cell_ptr + n - 1); + self.pending_wrap = true; + } else { + self.x += n; + self.page_cell = @ptrCast(cell_ptr + n); + } + + return true; + } + /// Write a cell. On error, this will not unwrite the cell but /// the cell may be incomplete (but valid). For example, if the source /// cell is styled and we failed to allocate space for styles, the @@ -1742,7 +2132,7 @@ const ReflowCursor = struct { // head and wrap before handling it. self.page_cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_head, }; @@ -1762,7 +2152,7 @@ const ReflowCursor = struct { // Edge case, when resizing to 1 column, wide // characters are just destroyed and replaced // with empty narrow cells. - self.page_cell.content.codepoint = 0; + self.page_cell.content.codepoint = .{ .data = 0 }; self.page_cell.wide = .narrow; self.cursorForward(); @@ -1882,7 +2272,7 @@ const ReflowCursor = struct { // Unsafe builds we throw away grapheme data! self.page_cell.content_tag = .codepoint; - self.page_cell.content = .{ .codepoint = 0xFFFD }; + self.page_cell.content = .{ .codepoint = .{ .data = 0xFFFD } }; }; } @@ -1899,32 +2289,9 @@ const ReflowCursor = struct { // Ensure that the string alloc has sufficient capacity // to dupe the link (and the ID if it's not implicit). - const additional_required_string_capacity = - src_link.uri.len + - switch (src_link.id) { - .explicit => |v| v.len, - .implicit => 0, - }; - // Keep trying until we have enough capacity. - while (true) { - if (self.page.string_alloc.alloc( - u8, - self.page.memory, - additional_required_string_capacity, - )) |slice| { - // We have enough capacity, free the test alloc. - self.page.string_alloc.free( - self.page.memory, - slice, - ); - break; - } else |_| { - // Grow our capacity until we can fit the extra bytes. - try self.increaseCapacity( - list, - .string_bytes, - ); - } + // Grow our capacity until the hyperlink fits. + while (!self.hyperlinkStringsFit(src_link)) { + try self.increaseCapacity(list, .string_bytes); } const dst_link = src_link.dupe( @@ -1961,6 +2328,14 @@ const ReflowCursor = struct { error.NeedsRehash => null, }); + // The increaseCapacity call above swapped self.page + // for a new page, so the string capacity check done + // before the first dupe no longer applies. Re-establish + // it against the current page before duping again. + while (!self.hyperlinkStringsFit(src_link)) { + try self.increaseCapacity(list, .string_bytes); + } + // We need to recreate the link into the new page. const dst_link2 = src_link.dupe( src_page, @@ -2031,6 +2406,23 @@ const ReflowCursor = struct { // Copy style data. if (cell.hasStyling()) style: { + // Fast path: styled cells come in long runs sharing the + // same style. If this source style was just mapped into + // the current destination page, bump the ref count + // directly and skip the set lookup. The destination id is + // guaranteed alive because a previously written cell in + // this page holds a reference, and the cache is reset + // whenever the destination page changes (see init). + if (self.style_cache.src_page == src_page and + self.style_cache.src_id == cell.style_id) + { + const id = self.style_cache.dst_id; + self.page.styles.use(self.page.memory, id); + self.page_row.styled = true; + self.page_cell.style_id = id; + break :style; + } + const style = src_page.styles.get( src_page.memory, cell.style_id, @@ -2073,6 +2465,14 @@ const ReflowCursor = struct { }; } orelse cell.style_id; + // Update our style cache with the latest style set so runs + // of cells with the same style are faster to write. + self.style_cache = .{ + .src_page = src_page, + .src_id = cell.style_id, + .dst_id = id, + }; + self.page_row.styled = true; self.page_cell.style_id = id; } @@ -2148,8 +2548,9 @@ const ReflowCursor = struct { } } - // Clear the row from the old page and truncate it. - old_page.clearCells(old_row, 0, old_page.size.cols); + // Reset the row on the old page and truncate it. The retired + // storage must be left in the default state (see resetRow). + old_page.resetRow(old_row); old_page.size.rows -= 1; // If that was the last row in that page @@ -2189,6 +2590,43 @@ const ReflowCursor = struct { self.total_rows = old_total_rows; } + /// True if the string allocator of the current page can fit the + /// allocations that duping the given source hyperlink performs. + /// The test allocations are freed before returning. + /// + /// This must mirror the exact allocation pattern of + /// hyperlink.PageEntry.dupe: the URI and the explicit ID (if any) + /// are allocated separately. The string allocator rounds every + /// allocation up to its chunk size and requires each allocation to + /// be contiguous, so a single combined allocation of the total byte + /// length can succeed where the two separate allocations performed + /// by dupe would fail. + fn hyperlinkStringsFit( + self: *const ReflowCursor, + src_link: *const hyperlink.PageEntry, + ) bool { + const uri_buf = self.page.string_alloc.alloc( + u8, + self.page.memory, + src_link.uri.len, + ) catch return false; + defer self.page.string_alloc.free(self.page.memory, uri_buf); + + switch (src_link.id) { + .implicit => {}, + .explicit => |v| { + const id_buf = self.page.string_alloc.alloc( + u8, + self.page.memory, + v.len, + ) catch return false; + self.page.string_alloc.free(self.page.memory, id_buf); + }, + } + + return true; + } + /// True if this cursor is at the bottom of the page by capacity, /// i.e. we can't scroll anymore. fn bottom(self: *const ReflowCursor) bool { @@ -2314,14 +2752,14 @@ const ReflowCursor = struct { }; fn resizeWithoutReflow(self: *PageList, opts: Resize) Allocator.Error!void { - // We only set the new min_max_size if we're not reflowing. If we are - // reflowing, then resize handles this for us. - const old_min_max_size = self.min_max_size; - self.min_max_size = if (!opts.reflow) minMaxSize( + // We only set the new minimums if we're not reflowing. If we are + // reflowing, then the outer resize call handles this for us. + const old_limits = self.limits; + if (!opts.reflow) self.limits.resize( opts.cols orelse self.cols, opts.rows orelse self.rows, - ) else old_min_max_size; - errdefer self.min_max_size = old_min_max_size; + ); + errdefer self.limits = old_limits; // Important! We have to do cols first because cols may cause us to // destroy pages if we're increasing cols which will free up page_size @@ -2602,11 +3040,7 @@ fn resizeWithoutReflowGrowCols( const prev_page = prev.?.page(); const prev_size = prev_page.size.rows - prev_copied; const prev_rows = prev_page.rows.ptr(prev_page.memory)[prev_size..prev_page.size.rows]; - for (prev_rows) |*row| prev_page.clearCells( - row, - 0, - prev_page.size.cols, - ); + for (prev_rows) |*row| prev_page.resetRow(row); prev_page.size.rows = prev_size; }; @@ -2756,6 +3190,14 @@ fn trimTrailingBlankRows( self.invalidateNodeLayout(row_pin.node); invalidated_node = row_pin.node; } + + // The row has no text but can still carry metadata (e.g. a + // blank prompt continuation line) and background-colored + // cells. The retired storage is re-exposed by the grow() + // fast path without any clearing, so it must be left in the + // default state. + row_pin.node.page().resetRow(row_pin.rowAndCell().row); + row_pin.node.page().size.rows -= 1; if (row_pin.node.page().size.rows == 0) { self.erasePage(row_pin.node); @@ -2808,7 +3250,7 @@ pub fn scroll(self: *PageList, behavior: Scroll) void { defer self.assertIntegrity(); // Special case no-scrollback mode to never allow scrolling. - if (self.explicit_max_size == 0) { + if (self.limits.bytes.explicit == 0) { self.viewport = .active; return; } @@ -3308,13 +3750,11 @@ pub fn split( // p.x remains the same since we're copying the row as-is } - // Clear our rows + // Reset our rows. They are retired into unused page capacity, + // which the grow() fast path re-exposes without any clearing, so + // they must be left in the default state. for (page.rows.ptr(page.memory)[y_start..y_end]) |*row| { - page.clearCells( - row, - 0, - page.size.cols, - ); + page.resetRow(row); } page.size.rows -= y_end - y_start; @@ -3378,7 +3818,7 @@ pub fn scrollbar(self: *PageList) Scrollbar { // it always has SOME extra space (due to the way we allocate by page). // So even with no scrollback we have some growth. It is architecturally // much simpler to just hide that for no-scrollback cases. - if (self.explicit_max_size == 0) return .{ + if (self.limits.bytes.explicit == 0) return .{ .total = self.rows, .offset = 0, .len = self.rows, @@ -3449,6 +3889,11 @@ fn fixupViewport( self: *PageList, removed: usize, ) void { + // Page removal can mark every pin on the removed page as garbage. The + // viewport pin is an internal navigation anchor that is always remapped, + // so it remains valid after the removal. + self.viewport_pin.garbage = false; + switch (self.viewport) { .active => {}, @@ -3474,22 +3919,38 @@ fn fixupViewport( } } -/// Returns the actual max size. This may be greater than the explicit -/// value if the explicit value is less than the min_max_size. +/// Change the maximum logical page allocation at runtime. Null removes the +/// explicit byte limit and zero disables scrollback. +/// +/// Lowering the limit immediately removes eligible complete historical pages. +/// The effective limit may still be raised to fit the active area, and a page +/// which overlaps the active area is never split solely to satisfy this limit. +pub fn setMaxBytes(self: *PageList, max: ?usize) void { + defer self.assertIntegrity(); + + self.limits.set(.bytes, max); + self.limits.enforce(self, .bytes); + if (self.limits.bytes.explicit == 0) self.viewport = .active; +} + +/// Change the maximum number of physical scrollback rows at runtime. Null +/// removes the explicit line limit. /// -/// This value is a HEURISTIC. You cannot assert on this value. We may -/// exceed this value if required to fit the active area. This may be -/// required in some cases if the active area has a large number of -/// graphemes, styles, etc. -pub fn maxSize(self: *const PageList) usize { - return @max(self.explicit_max_size, self.min_max_size); +/// Lowering the limit immediately removes eligible complete historical pages. +/// The effective limit always permits at least one standard page of history, +/// and a page which overlaps the active area is never split for enforcement. +pub fn setMaxLines(self: *PageList, max: ?usize) void { + defer self.assertIntegrity(); + + self.limits.set(.lines, max); + self.limits.enforce(self, .lines); } /// Grow the active area by exactly one row. /// /// This may allocate, but also may not if our current page has more /// capacity we can use. This will prune scrollback if necessary to -/// adhere to max_size. +/// adhere to max_size and max_lines. /// /// This returns the newly allocated page node if there is one. pub fn grow(self: *PageList) Allocator.Error!?*List.Node { @@ -3500,7 +3961,12 @@ pub fn grow(self: *PageList) Allocator.Error!?*List.Node { const last = self.pages.last.?; if (last.capacity().rows > last.rows()) { - // Fast path: we have capacity in the last page. + // Fast path: we have capacity in the last page. The exposed + // row requires no clearing work here: rows in unused page + // capacity are always in the default zero state, either + // because the page memory was never used (pool buffers are + // zeroed) or because whatever retired the row reset it (see + // Page.resetRow). const page = last.page(); page.size.rows += 1; page.assertIntegrity(); @@ -3508,6 +3974,9 @@ pub fn grow(self: *PageList) Allocator.Error!?*List.Node { // Increase our total rows by one self.total_rows += 1; + // Growing inside the last page moves the active boundary without + // allocating; that alone can make the first page wholly historical. + self.limits.enforce(self, .lines); return null; } @@ -3527,7 +3996,7 @@ pub fn grow(self: *PageList) Allocator.Error!?*List.Node { // initial allocation. if (self.pages.first != null and self.pages.first != self.pages.last and - self.page_size + PagePool.item_size > self.maxSize()) + self.page_size + PagePool.item_size > self.limits.max(.bytes)) prune: { const first = self.pages.popFirst().?; assert(first != last); @@ -3609,6 +4078,10 @@ pub fn grow(self: *PageList) Allocator.Error!?*List.Node { // we're reusing an existing page so nothing has changed. page.assertIntegrity(); + + // Byte-limit recycling may leave history above the independent line + // limit, so enforce it after the recycled page becomes the new tail. + self.limits.enforce(self, .lines); return first; } @@ -3627,6 +4100,9 @@ pub fn grow(self: *PageList) Allocator.Error!?*List.Node { // Record the increased row count self.total_rows += 1; + // Appending a page can cross the line limit and can make the oldest + // active-boundary page wholly historical. + self.limits.enforce(self, .lines); return next_node; } @@ -3636,6 +4112,36 @@ pub const IncreaseCapacity = enum { grapheme_bytes, hyperlink_bytes, string_bytes, + + /// Returns the capacity dimension that must be increased for the + /// given row clone error to succeed on retry, or null if the page + /// only needs to be rehashed at its current capacity. + pub fn forCloneError(err: Page.CloneFromError) ?IncreaseCapacity { + return switch (err) { + // Rehash the sets + error.StyleSetNeedsRehash, + error.HyperlinkSetNeedsRehash, + => null, + + // Increase style memory + error.StyleSetOutOfMemory, + => .styles, + + // Increase string memory + error.StringAllocOutOfMemory, + => .string_bytes, + + // Increase hyperlink memory + error.HyperlinkSetOutOfMemory, + error.HyperlinkMapOutOfMemory, + => .hyperlink_bytes, + + // Increase grapheme memory + error.GraphemeMapOutOfMemory, + error.GraphemeAllocOutOfMemory, + => .grapheme_bytes, + }; + } }; pub const IncreaseCapacityError = error{ @@ -3719,7 +4225,65 @@ pub fn increaseCapacity( if (layout.total_size > size.max_page_size) { return error.OutOfSpace; } - }, + + // Doubling alone has a really bad behavior in that if you're + // in a pathological scenario with only one dimension of cap + // increase, you have to pay repeat double costs. Each time you + // double we have to reclone the entire page. As the page gets + // bigger this gets more expensive. + // + // Instead, for some dimensions, we do something else: we look + // at the current utilization, and project that to every row + // in the page capacity (if it fits). We just assume that your + // future workload will look the one you're currently filling. + // If we're wrong, its some wasted capacity but future growth + // still works in other dimensions. + // + // This only applies to the current page being grown. I previously + // tried a high-water-mark style solution to preallocate pages, + // which does work really well, but its not clear when you reset + // the mark. + project: { + // The dimensions we project must measure their current + // usage in the same units as the capacity field. + const used: u64 = switch (comptime tag) { + .grapheme_bytes => page.grapheme_alloc.usedBytes(page.memory), + + // Living item count. Note the capacity field is a + // requested item count that Layout.init rounds in + // both directions (table to the next power of two, + // items to the load factor of that), so this is an + // approximation in capacity units; the headroom + // below absorbs the error and an undershoot only + // costs one more (non-ladder) growth event. + .styles => page.styles.count(), + + else => break :project, + }; + if (used == 0 or page.size.rows == 0) break :project; + + // Full-page need at current density, plus 25% headroom + // for chunk rounding and fragmentation. + const density = used * @as(u64, cap.rows) / page.size.rows; + const projected_raw = density + density / 4; + + // Bound the jump to 32× the pre-growth capacity. Arbitrary + // choice until we can show otherwise. + const projected = @min( + projected_raw, + @as(u64, old) * 32, + std.math.maxInt(Int), + ); + if (projected <= @field(cap, field_name)) break :project; + + // Only take the projection if the resulting page still fits. + var proj_cap = cap; + @field(proj_cap, field_name) = @intCast(projected); + if (Page.layout(proj_cap).total_size > size.max_page_size) + break :project; + cap = proj_cap; + } + }, }; log.info("adjusting page capacity={}", .{cap}); @@ -3769,6 +4333,125 @@ pub fn increaseCapacity( return new_node; } +/// Allocate a new page using the PageList's memory pools. +/// +/// The page is detached: it doesn't contribute to the memory limits or +/// row counts or anything in the PageList. The caller must call `finalize` +/// to add it to the PageList at the appropriate place, or `deinit` to +/// throw it away. +pub fn allocatePage( + self: *PageList, + capacity: Capacity, +) Allocator.Error!PageAllocation { + return .{ + .destination = self, + .node = try createPageExt( + &self.pool, + .{ .cap = capacity }, + &self.page_serial, + null, + ), + }; +} + +/// One PageList-pooled page which has not yet joined the live page sequence. +pub const PageAllocation = struct { + destination: *PageList, + node: ?*List.Node, + + /// Return the fresh page storage for the caller to populate. + pub fn page(self: *PageAllocation) *Page { + return self.node.?.pageAssumeResident(); + } + + /// Release an uncommitted page back to its PageList's pools. + /// + /// This is safe to call after `finalize` succeeds, so callers can defer + /// it unconditionally. + pub fn deinit(self: *PageAllocation) void { + const node = self.node orelse return; + destroyNodeExt( + &self.destination.pool, + node, + null, + ); + self.node = null; + } + + pub const Location = union(enum) { + /// Prepend the page to the start of the list (oldest history). + prepend, + }; + + /// Finalize this complete page and transfer its ownership to the PageList. + /// The parameter determines where it goes into the PageList. + /// + /// Existing pages and tracked pins keep their identity. A pinned viewport + /// keeps showing the same content while its cached absolute row offset + /// moves down by the number of newly inserted rows. + pub fn finalize(self: *PageAllocation, location: Location) FinalizeError!void { + switch (location) { + .prepend => return try self.prepend(), + } + } + + pub const FinalizeError = error{ + InvalidPageDimensions, + RowCountOverflow, + PageSizeOverflow, + MaxSizeExceeded, + MaxLinesExceeded, + }; + + fn prepend(self: *PageAllocation) FinalizeError!void { + const destination = self.destination; + const node = self.node.?; + + // Validate the populated page and all resulting accounting before + // publishing the detached node into the live list. + if (node.cols() == 0 or node.rows() == 0) return error.InvalidPageDimensions; + const total_rows = std.math.add( + usize, + destination.total_rows, + node.rows(), + ) catch return error.RowCountOverflow; + const node_size: usize = switch (node.owned) { + .pool => PagePool.item_size, + .heap => node.pageAssumeResident().memory.len, + }; + const page_size = std.math.add( + usize, + destination.page_size, + node_size, + ) catch return error.PageSizeOverflow; + + // Restored history is exact data, so reject a page which cannot + // coexist with the receiving PageList's configured limits. + if (page_size > destination.limits.max(.bytes)) { + return error.MaxSizeExceeded; + } + if (total_rows - destination.rows > destination.limits.max(.lines)) { + return error.MaxLinesExceeded; + } + + // No fallible work remains. Publish the page and update every cached + // quantity affected by inserting rows above the existing first page. + errdefer comptime unreachable; + destination.pages.prepend(node); + destination.page_size = page_size; + destination.total_rows = total_rows; + if (destination.viewport == .pin) { + if (destination.viewport_pin_row_offset) |*offset| { + offset.* += node.rows(); + } + } + destination.page_compression.markActivity(); + + destination.assertIntegrity(); + self.node = null; + } +}; + /// Options for createPage and createPageExt. const CreatePage = struct { /// The capacity to allocate the page with. @@ -3789,6 +4472,45 @@ inline fn createPage( opts: CreatePage, ) Allocator.Error!*List.Node { // log.debug("create page cap={}", .{opts.cap}); + + // If we have a node available for recycling (only during reflow, + // see recycle_node), reuse it directly rather than going through + // the memory pool. + if (self.recycle_node) |node| recycle: { + // Only a standard pool-owned resident node can be rebuilt + // in place for a standard-size layout. + if (opts.exact_size) break :recycle; + if (node.owned != .pool) break :recycle; + if (node.data != .resident) break :recycle; + const layout = Page.layout(opts.cap); + if (layout.total_size > std_size) break :recycle; + + self.recycle_node = null; + + // The pool guarantees that buffers it hands out are zeroed. + // A pool-owned page dirties only its Page.memory prefix of + // the underlying standard-size item, so zeroing that prefix + // re-establishes the guarantee (this mirrors destroyNodeExt, + // minus the decommit). + const page = &node.data.resident; + const item: *align(std.heap.page_size_min) [std_size]u8 = + @ptrCast(@alignCast(page.memory.ptr)); + @memset(page.memory, 0); + + // Accounting: a pool-owned node always accounts for a full + // pool item in page_size, so destroying the node and + // creating a new pooled one is a net zero. + + node.* = .{ + .data = .{ .resident = .initBuf(.init(item), layout) }, + .serial = self.page_serial, + .owned = .pool, + }; + node.page().size.rows = 0; + self.page_serial += 1; + return node; + } + return try createPageExt( &self.pool, opts, @@ -3808,7 +4530,7 @@ inline fn createPageExt( const layout = Page.layout(opts.cap); const pooled = !opts.exact_size and layout.total_size <= std_size; - const page_alloc = pool.pages.arena.child_allocator; + const page_alloc = pool.pages.allocator; // It would be better to encode this into the Zig error handling // system but that is a big undertaking and we only have a few @@ -3836,12 +4558,9 @@ inline fn createPageExt( // (WASM), the WasmAllocator reuses freed slots without zeroing. // // Otherwise, we rely on pool item buffers being zeroed: fresh items - // come from the OS page allocator (zeroed pages) and destroyNodeExt - // zeroes buffers before returning them to the pool. The one - // exception is the first pointer-size bytes, which hold the pool's - // free list node while a buffer is in the free list; initBuf below - // always overwrites those since the page rows start at offset 0 - // (comptime-asserted in Page). + // come from the OS page allocator (zeroed pages), destroyNodeExt + // zeroes buffers before returning them to the pool, and the pool + // never writes into its items (see PagePool). if (comptime std.debug.runtime_safety or builtin.os.tag == .freestanding) @memset(page_buf, 0); @@ -3868,6 +4587,10 @@ inline fn createPageExt( /// Standard-sized output borrows a page-pool item so repeated compression can /// reuse the same virtual mapping. Oversized pages use a temporary allocation /// from the page allocator and release it immediately after compression. +/// +/// A borrowed item goes back to the pool through zero-mode decommit, which +/// only has to clear the bytes the encoder wrote. Callers therefore pass the +/// dirty length to `deinit` rather than paying to clear the whole item. const CompressionScratch = union(enum) { pooled: *align(std.heap.page_size_min) [std_size]u8, allocated: []align(std.heap.page_size_min) u8, @@ -3885,7 +4608,7 @@ const CompressionScratch = union(enum) { return .{ .pooled = memory }; } - const page_alloc = pool.pages.arena.child_allocator; + const page_alloc = pool.pages.allocator; return .{ .allocated = try page_alloc.alignedAlloc( u8, .fromByteUnits(std.heap.page_size_min), @@ -3900,14 +4623,18 @@ const CompressionScratch = union(enum) { }; } - fn deinit(self: *CompressionScratch, pool: *MemoryPool) void { + fn deinit( + self: *CompressionScratch, + pool: *MemoryPool, + dirty_len: usize, + ) void { switch (self.*) { .pooled => |memory| { - _ = terminal_mem.decommit(.zero, memory, memory.len); + _ = terminal_mem.decommit(.zero, memory, dirty_len); pool.pages.destroy(memory); }, .allocated => |memory| { - const page_alloc = pool.pages.arena.child_allocator; + const page_alloc = pool.pages.allocator; page_alloc.free(memory); }, } @@ -4223,10 +4950,15 @@ fn compressPage(self: *PageList, node: *List.Node) bool { ) catch |err| switch (err) { error.OutOfMemory => return false, }; - defer scratch.deinit(&self.pool); + + // The encoder writes at most `required` bytes and reports exactly + // how many on success. Track that so returning the scratch only + // clears the prefix it dirtied instead of the whole item. + var dirty_len: usize = required; + defer scratch.deinit(&self.pool, dirty_len); var table: compression.lz4.HashTable = undefined; - break :candidate compression.Page.init( + const result = compression.Page.init( self.pool.alloc, page, scratch.bytes()[0..required], @@ -4237,6 +4969,8 @@ fn compressPage(self: *PageList, node: *List.Node) bool { error.OutputTooSmall, => return false, }; + if (result) |compressed| dirty_len = compressed.encoded.len; + break :candidate result; }; // Null means compression crossed the break-even point. The node and its @@ -4307,7 +5041,7 @@ fn destroyNodeExt( }, .heap => { - const page_alloc = pool.pages.arena.child_allocator; + const page_alloc = pool.pages.allocator; page_alloc.free(page.memory); }, } @@ -4315,6 +5049,57 @@ fn destroyNodeExt( pool.nodes.destroy(node); } +/// Clone the given source row into the row at `dst_y` of the given +/// node's page, increasing the node's capacity as necessary to fit the +/// source row's managed memory (styles, hyperlinks, etc.). +/// +/// Since increasing capacity replaces the node in the page list, the +/// (possibly replaced) node is returned and the caller must use it in +/// place of the old node. The source must NOT be on the given node +/// since the node's page memory may be freed on capacity increase. +fn cloneRowGrowCapacity( + self: *PageList, + node: *List.Node, + dst_y: usize, + src_page: *Page, + src_row: *const Row, +) *List.Node { + assert(src_page != node.page()); + + var current = node; + while (true) { + const cur_page = current.page(); + const cur_rows = cur_page.rows.ptr(cur_page.memory.ptr); + cur_page.cloneRowFrom( + src_page, + &cur_rows[dst_y], + src_row, + ) catch |err| { + // Adjust our page capacity to make room for what we + // didn't have space for. + current = self.increaseCapacity( + current, + IncreaseCapacity.forCloneError(err), + ) catch |e| switch (e) { + // We can't gracefully recover from either of these + // here: our callers have already rotated rows, so + // returning an error would leave the page list + // half-mutated (and corrupt), so a crash is better. + error.OutOfMemory, + => @panic("increaseCapacity system allocator OOM"), + + error.OutOfSpace, + => @panic("increaseCapacity OutOfSpace"), + }; + + // Retry the row copy with the increased capacity. + continue; + }; + + return current; + } +} + /// Fast-path function to erase exactly 1 row. Erasing means that the row /// is completely REMOVED, not just cleared. All rows following the removed /// row will be shifted up by 1 to fill the empty space. @@ -4384,9 +5169,16 @@ pub fn eraseRow( // 5 5 5 | 6 // 6 6 6 | 7 // 7 7 7 <' 4 - try page.cloneRowFrom( + // + // The copy may replace the destination node in order to + // increase its capacity. We can discard the replacement + // because we advance to the next node below, and the + // replacement is already linked in its place (so e.g. the + // `node.prev` access in the pin fixups below is correct). + _ = self.cloneRowGrowCapacity( + node, + node.rows() - 1, next_page, - &rows[node.rows() - 1], &next_rows[0], ); @@ -4416,8 +5208,10 @@ pub fn eraseRow( } } - // Clear the final row which was rotated from the top of the page. - page.clearCells(&rows[node.rows() - 1], 0, node.cols()); + // Reset the final row which was rotated from the top of the page. + // A full reset (not just clearing cells) so no metadata from the + // erased row is retained by the new blank row. + page.resetRow(&rows[node.rows() - 1]); } /// A variant of eraseRow that shifts only a bounded number of following @@ -4455,7 +5249,7 @@ pub fn eraseRowBounded( if (node.rows() - pn.y > limit) { // Rotating this bounded region changes its cached row coordinates. self.invalidateNodeLayout(node); - page.clearCells(&rows[pn.y], 0, node.cols()); + page.resetRow(&rows[pn.y]); fastmem.rotateOnce(Row, rows[pn.y..][0 .. limit + 1]); // Mark the whole page as dirty. @@ -4542,9 +5336,15 @@ pub fn eraseRowBounded( const next_page = next.page(); const next_rows = next_page.rows.ptr(next_page.memory.ptr); - try page.cloneRowFrom( + // The copy may replace the destination node in order to + // increase its capacity. We can discard the replacement + // because we advance to the next node below, and the + // replacement is already linked in its place (so e.g. the + // `node.prev` access in the pin fixups below is correct). + _ = self.cloneRowGrowCapacity( + node, + node.rows() - 1, next_page, - &rows[node.rows() - 1], &next_rows[0], ); @@ -4561,7 +5361,7 @@ pub fn eraseRowBounded( if (node.rows() > shifted_limit) { // Rotating this bounded prefix changes its cached row coordinates. self.invalidateNodeLayout(node); - page.clearCells(&rows[0], 0, node.cols()); + page.resetRow(&rows[0]); fastmem.rotateOnce(Row, rows[0 .. shifted_limit + 1]); // Mark the whole page as dirty. @@ -4629,9 +5429,9 @@ pub fn eraseRowBounded( } } - // We reached the end of the page list before the limit, so we clear + // We reached the end of the page list before the limit, so we reset // the final row since it was rotated down from the top of this page. - page.clearCells(&rows[node.rows() - 1], 0, node.cols()); + page.resetRow(&rows[node.rows() - 1]); } /// Erase all history rows, optionally up to a bottom-left bound. @@ -4724,15 +5524,12 @@ fn eraseRows( dst.dirty = true; } - // Clear our remaining cells that we didn't shift or swapped - // in case we grow back into them. + // Reset our remaining rows that we didn't shift or swapped. + // These are retired into unused page capacity, which the + // grow() fast path re-exposes without any clearing, so they + // must be left in the default state. for (scroll_amount..chunk.node.rows()) |i| { - const row: *Row = &rows[i]; - page.clearCells( - row, - 0, - chunk.node.cols(), - ); + page.resetRow(&rows[i]); } // Update any tracked pins to shift their y. If it was in the erased @@ -6073,6 +6870,190 @@ fn markDirty(self: *PageList, pt: point.Point) void { self.pin(pt).?.markDirty(); } +/// Runtime-configurable byte and line limits for a PageList. +const Limits = struct { + bytes: Limit, + lines: Limit, + + /// The limit keys. + pub const Key = std.meta.FieldEnum(Limits); + + pub const Limit = struct { + /// Explicit is the specified maximum value for this limit + /// by the user or maxInt otherwise. + explicit: usize = std.math.maxInt(usize), + + /// Min is the minimum valid maximum for this entry, so if + /// explicit is lower than this then min wins. + min: usize, + }; + + /// Return unlimited limits with minimums for the given PageList size. + pub fn init(cols: size.CellCountInt, rows: size.CellCountInt) Limits { + return .{ + .bytes = .{ .min = minMaxSize(cols, rows) }, + .lines = .{ .min = minMaxLines(cols) }, + }; + } + + /// Set an explicit limit. Null means unlimited. This does not enforce the + /// new value automatically; the caller must still call `enforce`. + pub fn set( + self: *Limits, + comptime key: Key, + value: ?usize, + ) void { + switch (key) { + .bytes => self.bytes.explicit = value orelse std.math.maxInt(usize), + .lines => self.lines.explicit = value orelse std.math.maxInt(usize), + } + } + + /// Recalculate the effective minimums for a new PageList size. + /// + /// This must be called whenever either PageList dimension changes so the + /// effective byte and line limits remain valid for the new size. + pub fn resize( + self: *Limits, + cols: size.CellCountInt, + rows: size.CellCountInt, + ) void { + self.bytes.min = minMaxSize(cols, rows); + self.lines.min = minMaxLines(cols); + } + + /// Return the effective maximum for a limit. + pub fn max(self: *const Limits, key: Key) usize { + return switch (key) { + .bytes => @max(self.bytes.explicit, self.bytes.min), + .lines => @max(self.lines.explicit, self.lines.min), + }; + } + + /// Whether the given limit is currently exceeded. Both limits are + /// heuristics: complete historical pages are the smallest unit that + /// enforcement removes. + pub fn exceeded( + self: *const Limits, + pagelist: *const PageList, + limit: Key, + ) bool { + return switch (limit) { + .bytes => pagelist.page_size > self.max(.bytes), + .lines => pagelist.total_rows > pagelist.rows and + pagelist.total_rows - pagelist.rows > self.max(.lines), + }; + } + + /// Prune complete historical pages until the selected limit is satisfied + /// or the oldest remaining page overlaps the active area. + pub fn enforce( + self: *const Limits, + pagelist: *PageList, + key: Key, + ) void { + if (!self.exceeded(pagelist, key)) return; + + // A partially constructed clone can temporarily contain fewer rows than + // its active area. It has no scrollback to prune. + if (pagelist.total_rows <= pagelist.rows) return; + + // Accumulate the row delta so viewport offsets are fixed up once + // after all eligible pages have been removed. + var removed: usize = 0; + while (self.exceeded(pagelist, key)) { + const first = pagelist.pages.first.?; + + // The page containing the active top may also contain history. Keep + // that boundary page whole even if its history exceeds the heuristic. + if (first == pagelist.getTopLeft(.active).node) break; + + if (removed == 0) pagelist.page_compression.markActivity(); + + const first_rows = first.rows(); + + // Automatic pruning invalidates the content represented by pins in + // the removed page. erasePage remaps them to the next page below but + // only the enforcing caller knows that their original content is gone, + // so mark them as garbage here. + for (pagelist.tracked_pins.keys()) |p| { + if (p.node == first) p.garbage = true; + } + + // erasePage updates the list, pin targets, and byte accounting. + // Row accounting belongs to the caller because erasePage is also used + // by paths that already adjusted total_rows. + pagelist.erasePage(first); + pagelist.total_rows -= first_rows; + removed += first_rows; + } + + // Reconcile viewport mode and cached row offsets with the combined prefix + // removal only after every page and pin points into the final list. + if (removed > 0) pagelist.fixupViewport(removed); + } + + /// Returns the minimum valid "max size" for a given number of rows and cols + /// such that we can fit the active area AND at least two pages. Note we + /// need the two pages for algorithms to work properly (such as grow) but + /// we don't need to fit double the active area. + /// + /// This min size may not be totally correct in the case that a large + /// number of other dimensions makes our row size in a page very small. + /// But this gives us a nice fast heuristic for determining min/max size. + /// Therefore, if the page size is violated you should always also verify + /// that we have enough space for the active area. + fn minMaxSize(cols: size.CellCountInt, rows: size.CellCountInt) usize { + // Invariant required to ensure our divCeil below cannot overflow. + comptime { + const max_rows = std.math.maxInt(size.CellCountInt); + _ = std.math.divCeil(usize, max_rows, 1) catch unreachable; + } + + // Get our capacity to fit our rows. If the cols are too big, it may + // force less rows than we want meaning we need more than one page to + // represent a viewport. + const cap = initialCapacity(cols); + + // Calculate the number of standard sized pages we need to represent + // an active area. + const pages_exact = if (cap.rows >= rows) 1 else std.math.divCeil( + usize, + rows, + cap.rows, + ) catch { + // Not possible: + // - initialCapacity guarantees at least 1 row + // - numerator/denominator can't overflow because of comptime check above + unreachable; + }; + + // We always need at least one page extra so that we + // can fit partial pages to spread our active area across two pages. + // Even for caps that can't fit all rows in a single page, we add one + // because the most extra space we need at any given time is only + // the partial amount of one page. + const pages = pages_exact + 1; + assert(pages >= 2); + + // log.debug("minMaxSize cols={} rows={} cap={} pages={}", .{ + // cols, + // rows, + // cap, + // pages, + // }); + + return PagePool.item_size * pages; + } + + /// Returns the minimum line limit for a given column count. Line limits are + /// page-granular, so we always permit at least one standard page worth of + /// scrollback rows. + fn minMaxLines(cols: size.CellCountInt) usize { + return initialCapacity(cols).rows; + } +}; + /// Represents an exact x/y coordinate within the screen. This is called /// a "pin" because it is a fixed point within the pagelist direct to /// a specific page pointer and memory offset. The benefit is that this @@ -6484,102 +7465,608 @@ pub const Pin = struct { } }; -fn mixedWidthPinListForTest(alloc: Allocator) !PageList { - var result = try init(alloc, 2, 1, null); - errdefer result.deinit(); +/// Build up a PageList manually from a set of Pages. +/// +/// This data structure is transactional: `deinit` releases every page +/// until `finish` is called. This keeps the ownership clear: a complete +/// PageList either owns all its pages or doesn't. +/// +/// This was specifically built to help facilitate snapshot decoding +/// which transfers pages directly, but could be generally useful +/// for other purposes as well. +pub const Builder = struct { + pool: MemoryPool, + pages: List = .{}, + page_serial: u64 = 0, + page_size: usize = 0, + options: Options, + finished: bool = false, + + /// Initialize an empty builder. The options are the final state + /// of the PageList and some validation is done on the finish call + /// to ensure you built up a proper PageList according to those options. + pub fn init( + alloc: Allocator, + options: Options, + ) Allocator.Error!Builder { + return .{ + .pool = try MemoryPool.init( + alloc, + pageAllocator(alloc), + page_preheat, + ), + .options = options, + }; + } - // This deliberately constructs a layout that normal PageList operations - // do not expose yet. Keep integrity checks paused through deinit so the - // fixture can exercise mixed-width traversal in isolation. - result.pauseIntegrityChecks(true); + /// Release all pages when restoration does not finish. + /// + /// This is safe to call after `finish` succeeds, so callers can defer it + /// unconditionally. + pub fn deinit(self: *Builder) void { + if (self.finished) return; - inline for (.{ 4, 3 }) |cols| { - const node = try result.createPage(.{ .cap = .{ - .cols = cols, - .rows = 1, - } }); - node.page().size.rows = 1; - result.pages.append(node); - result.total_rows += 1; + // Free all our in-progress pages + while (self.pages.popFirst()) |node| destroyNodeExt( + &self.pool, + node, + &self.page_size, + ); + // Free memory pool + self.pool.deinit(); + self.* = undefined; } - // Desired geometry is wider than the first and last stored pages. - result.cols = 4; - return result; -} + /// Allocate a new page into the PageList with the given capacity. + /// + /// The caller can then take this page and populate it. When `finish` + /// is called, ownership is transferred to the resulting PageList. + /// Until then, this Builder owns the page. + pub fn allocatePage( + self: *Builder, + capacity: Capacity, + ) Allocator.Error!*Page { + const node = try createPageExt( + &self.pool, + .{ .cap = capacity }, + &self.page_serial, + &self.page_size, + ); + self.pages.append(node); + return node.pageAssumeResident(); + } -test "PageList Pin row movement clamps across mixed-width pages" { + pub const FinishError = Allocator.Error || error{ + InvalidDimensions, + InvalidPageDimensions, + NoPages, + InsufficientRows, + }; + + /// Validate the decoded pages and transfer them into a live PageList. + /// After this succeeds, `deinit` is a no-op because all resources have + /// transferred to the PageList. + pub fn finish(self: *Builder) FinishError!PageList { + // These are basic validations but they're cheap to do and + // we want to be careful we don't let corruption from untrusted + // sources into our PageList which asserts this. + if (self.options.cols == 0 or self.options.rows == 0) { + return error.InvalidDimensions; + } + if (self.pages.first == null) return error.NoPages; + + // Manually count our total rows at this point which we'll + // need for our PageList cache as well as a safety check. + const total_rows: usize = total_rows: { + var total_rows: usize = 0; + var node = self.pages.first; + while (node) |current| : (node = current.next) { + if (current.cols() == 0 or current.rows() == 0) { + return error.InvalidPageDimensions; + } + total_rows += current.rows(); + } + if (total_rows < self.options.rows) return error.InsufficientRows; + break :total_rows total_rows; + }; + + // Get our active pin + const active_top: Pin = active_top: { + var rem = self.options.rows; + var node = self.pages.last; + while (node) |current| : (node = current.prev) { + if (rem <= current.rows()) break :active_top .{ + .node = current, + .y = current.rows() - rem, + }; + rem -= current.rows(); + } else unreachable; + }; + + // Set our viewport up to the active + const viewport_pin = try self.pool.pins.create(); + errdefer self.pool.pins.destroy(viewport_pin); + viewport_pin.* = active_top; + + // Setup our one viewport tracked pin + var tracked_pins = try initTrackedPins(self.pool.alloc, viewport_pin); + errdefer tracked_pins.deinit(self.pool.alloc); + + // Initialize limits + var limits: Limits = .init(self.options.cols, self.options.rows); + limits.set(.bytes, self.options.max_size); + limits.set(.lines, self.options.max_lines); + + const result: PageList = .{ + .cols = self.options.cols, + .rows = self.options.rows, + .pool = self.pool, + .pages = self.pages, + .page_serial = self.page_serial, + .page_serial_epoch = 0, + .page_size = self.page_size, + .limits = limits, + .total_rows = total_rows, + .tracked_pins = tracked_pins, + .viewport = .{ .active = {} }, + .viewport_pin = viewport_pin, + .viewport_pin_row_offset = null, + }; + result.assertIntegrity(); + self.finished = true; + return result; + } +}; + +test "PageList Builder transfers mixed-width pages" { const testing = std.testing; - var s = try mixedWidthPinListForTest(testing.allocator); - defer s.deinit(); + // Build two populated pages whose widths differ from each other and from + // the final active-area width. The first page also includes one row of + // incidental history above the three-row active area. + var result: PageList = result: { + var builder = try Builder.init(testing.allocator, .{ + .cols = 4, + .rows = 3, + .max_size = null, + .max_lines = null, + }); + defer builder.deinit(); - const first = s.pages.first.?; - const second = first.next.?; - const third = second.next.?; + const first = try builder.allocatePage(.{ + .cols = 2, + .rows = 2, + }); + first.size.rows = 2; + first.getRowAndCell(0, 0).cell.* = .init('A'); - try testing.expect((Pin{ .node = third, .x = 2 }).eql( - (Pin{ .node = second, .x = 3 }).down(1).?, - )); - try testing.expect((Pin{ .node = first, .x = 1 }).eql( - (Pin{ .node = second, .x = 3 }).up(1).?, - )); + const second = try builder.allocatePage(.{ + .cols = 4, + .rows = 2, + }); + second.size.rows = 2; + second.getRowAndCell(0, 0).cell.* = .init('B'); - switch ((Pin{ .node = second, .x = 3 }).downOverflow(10)) { - .offset => try testing.expect(false), - .overflow => |overflow| try testing.expect( - (Pin{ .node = third, .x = 2 }).eql(overflow.end), - ), + break :result try builder.finish(); + }; + defer result.deinit(); + + // Successful finish transfers ownership and initializes the PageList's + // desired geometry, viewport, and required tracked viewport pin. + try testing.expectEqual(@as(size.CellCountInt, 4), result.cols); + try testing.expectEqual(@as(size.CellCountInt, 3), result.rows); + try testing.expectEqual(@as(usize, 2), result.totalPages()); + try testing.expectEqual(@as(usize, 1), result.countTrackedPins()); + try testing.expectEqual(Viewport.active, result.viewport); + + // Complete pages and their contents are preserved in insertion order, + // including widths which have not yet been reflowed. + const screen_top = result.getTopLeft(.screen); + try testing.expectEqual(@as(size.CellCountInt, 2), screen_top.node.cols()); + try testing.expectEqual(@as(u21, 'A'), screen_top + .node.page().getRowAndCell(0, 0).cell.codepoint()); + + // The active area is calculated backward from the newest page, so it + // begins at row one of the oldest page and leaves row zero as history. + const active_top = result.getTopLeft(.active); + try testing.expectEqual(screen_top.node, active_top.node); + try testing.expectEqual(@as(size.CellCountInt, 1), active_top.y); + try testing.expectEqual(@as(size.CellCountInt, 4), active_top + .node.next.?.cols()); + try testing.expectEqual(@as(u21, 'B'), active_top + .node.next.?.page().getRowAndCell(0, 0).cell.codepoint()); + + result.assertIntegrity(); +} + +test "PageList Builder validates the finished list" { + const testing = std.testing; + + // The desired PageList geometry must describe a non-empty screen. + { + var builder = try Builder.init(testing.allocator, .{ + .cols = 0, + .rows = 1, + }); + defer builder.deinit(); + try testing.expectError(error.InvalidDimensions, builder.finish()); } - switch ((Pin{ .node = second, .x = 3 }).upOverflow(10)) { - .offset => try testing.expect(false), - .overflow => |overflow| try testing.expect( - (Pin{ .node = first, .x = 1 }).eql(overflow.end), - ), + + // A PageList cannot be finished without any backing pages. + { + var builder = try Builder.init(testing.allocator, .{ + .cols = 1, + .rows = 1, + }); + defer builder.deinit(); + try testing.expectError(error.NoPages, builder.finish()); + } + + // Allocated capacity alone is insufficient: callers must populate a + // nonzero logical page size before transferring ownership. + { + var builder = try Builder.init(testing.allocator, .{ + .cols = 1, + .rows = 1, + }); + defer builder.deinit(); + const page = try builder.allocatePage(.{ .cols = 1, .rows = 1 }); + page.size.rows = 0; + try testing.expectError( + error.InvalidPageDimensions, + builder.finish(), + ); + } + + // The populated pages must contain enough rows to cover the active area. + { + var builder = try Builder.init(testing.allocator, .{ + .cols = 1, + .rows = 2, + }); + defer builder.deinit(); + const page = try builder.allocatePage(.{ .cols = 1, .rows = 1 }); + page.size.rows = 1; + try testing.expectError(error.InsufficientRows, builder.finish()); } } -test "PageList Pin wrapping crosses mixed-width pages" { +test "PageList Builder finish is transactional on allocation failure" { const testing = std.testing; - var s = try mixedWidthPinListForTest(testing.allocator); - defer s.deinit(); + // Construct a valid builder so finish reaches its fallible bookkeeping + // allocations after all page and geometry validation succeeds. + var failing = testing.FailingAllocator.init(testing.allocator, .{}); + var builder = try Builder.init(failing.allocator(), .{ + .cols = 1, + .rows = 1, + }); + defer builder.deinit(); + const page = try builder.allocatePage(.{ .cols = 1, .rows = 1 }); + page.size.rows = 1; - const first = s.pages.first.?; - const second = first.next.?; - const third = second.next.?; + // The pools are preheated, so the next general allocation is the tracked + // viewport pin map created by finish. + failing.fail_index = failing.alloc_index; + try testing.expectError(error.OutOfMemory, builder.finish()); + try testing.expect(failing.has_induced_failure); - try testing.expect((Pin{ .node = third, .x = 2 }).eql( - (Pin{ .node = first, .x = 1 }).rightWrap(7).?, - )); - try testing.expect((Pin{ .node = second, .x = 0 }).eql( - (Pin{ .node = third, .x = 2 }).leftWrap(6).?, - )); - try testing.expect((Pin{ .node = first }).leftWrap(1) == null); - try testing.expect((Pin{ .node = third, .x = 2 }).rightWrap(1) == null); + // Failed finish leaves page ownership with the builder so its normal + // deinit path can release the still-linked page. + try testing.expect(builder.pages.first != null); + try testing.expectEqual(builder.pages.first, builder.pages.last); } -test "PageList Pin rejects columns beyond mixed-width page bounds" { +test "PageList PageAllocation finalizes pages and preserves live state" { const testing = std.testing; - var s = try mixedWidthPinListForTest(testing.allocator); - defer s.deinit(); + // Build an existing list with history, a two-row active area, an external + // active pin, and a pinned viewport whose absolute offset is cached. + var result: PageList = result: { + var builder = try Builder.init(testing.allocator, .{ + .cols = 4, + .rows = 2, + .max_size = null, + .max_lines = null, + }); + defer builder.deinit(); - try testing.expect(s.pin(.{ .screen = .{ .x = 1, .y = 0 } }) != null); - try testing.expect(s.pin(.{ .screen = .{ .x = 2, .y = 0 } }) == null); - try testing.expect(s.pin(.{ .screen = .{ .x = 3, .y = 1 } }) != null); - try testing.expect(s.pin(.{ .screen = .{ .x = 3, .y = 2 } }) == null); -} + const first = try builder.allocatePage(.{ .cols = 3, .rows = 2 }); + first.size.rows = 2; + first.getRowAndCell(0, 0).cell.* = .init('C'); -pub const Cell = struct { - node: *List.Node, - row: *pagepkg.Row, - cell: *pagepkg.Cell, - row_idx: size.CellCountInt, - col_idx: size.CellCountInt, + const second = try builder.allocatePage(.{ .cols = 4, .rows = 2 }); + second.size.rows = 2; + second.getRowAndCell(0, 0).cell.* = .init('D'); - /// Returns true if this cell is marked as dirty. - /// + break :result try builder.finish(); + }; + defer result.deinit(); + + const old_first = result.pages.first.?; + const old_last = result.pages.last.?; + const active_top = result.getTopLeft(.active); + const tracked_active = try result.trackPin(active_top); + result.scroll(.{ .row = 1 }); + try testing.expectEqual(Viewport.pin, result.viewport); + try testing.expectEqual(@as(usize, 1), result.scrollbar().offset); + + // Prepend differently sized historical pages newest-first. Each page is + // populated while detached and joins the live list only on success. + { + var allocation = try result.allocatePage(.{ .cols = 4, .rows = 1 }); + defer allocation.deinit(); + const page = allocation.page(); + page.size.rows = 1; + page.getRowAndCell(0, 0).cell.* = .init('B'); + try allocation.finalize(.prepend); + } + { + var allocation = try result.allocatePage(.{ .cols = 2, .rows = 2 }); + defer allocation.deinit(); + const page = allocation.page(); + page.size.rows = 2; + page.getRowAndCell(0, 0).cell.* = .init('A'); + try allocation.finalize(.prepend); + } + + // Repeated prepends reconstruct oldest-to-newest order without replacing + // any existing nodes or tracked pins. + try testing.expectEqual(@as(usize, 4), result.totalPages()); + try testing.expectEqual(@as(usize, 7), result.total_rows); + try testing.expectEqual(old_last, result.pages.last.?); + try testing.expectEqual(old_first, result.pages.first.?.next.?.next.?); + try testing.expectEqual( + @as(u21, 'A'), + result.pages.first.?.page().getRowAndCell(0, 0).cell.codepoint(), + ); + try testing.expectEqual( + @as(u21, 'B'), + result.pages.first.?.next.? + .page().getRowAndCell(0, 0).cell.codepoint(), + ); + try testing.expect(active_top.eql(result.getTopLeft(.active))); + try testing.expect(active_top.eql(tracked_active.*)); + + // The viewport remains pinned to the same content, while its cached row + // offset and the scrollbar total include the three new historical rows. + try testing.expectEqual(Viewport.pin, result.viewport); + try testing.expectEqual(old_first, result.viewport_pin.node); + try testing.expectEqual(@as(size.CellCountInt, 1), result.viewport_pin.y); + const scrollbar_state = result.scrollbar(); + try testing.expectEqual(@as(usize, 7), scrollbar_state.total); + try testing.expectEqual(@as(usize, 4), scrollbar_state.offset); + try testing.expectEqual(@as(usize, 2), scrollbar_state.len); + + result.assertIntegrity(); +} + +test "PageList PageAllocation stays detached until finalize" { + const testing = std.testing; + + var result = try init(testing.allocator, .{ + .cols = 1, + .rows = 1, + .max_size = null, + .max_lines = null, + }); + defer result.deinit(); + + const initial_first = result.pages.first.?; + const initial_last = result.pages.last.?; + const initial_total_rows = result.total_rows; + const initial_page_size = result.page_size; + + // Allocating and populating a detached page does not alter any live list + // links or accounting. Deinit returns it to the same PageList pools. + var detached = try result.allocatePage(.{ .cols = 1, .rows = 1 }); + detached.page().size.rows = 1; + try testing.expectEqual(initial_first, result.pages.first.?); + try testing.expectEqual(initial_last, result.pages.last.?); + try testing.expectEqual(initial_total_rows, result.total_rows); + try testing.expectEqual(initial_page_size, result.page_size); + result.assertIntegrity(); + detached.deinit(); + result.assertIntegrity(); + + // Invalid populated dimensions leave ownership with the allocation so it + // can still be released normally. + var invalid = try result.allocatePage(.{ .cols = 1, .rows = 1 }); + defer invalid.deinit(); + try testing.expectError( + error.InvalidPageDimensions, + invalid.finalize(.prepend), + ); + + try testing.expectEqual(initial_first, result.pages.first.?); + try testing.expectEqual(initial_last, result.pages.last.?); + try testing.expectEqual(initial_total_rows, result.total_rows); + try testing.expectEqual(initial_page_size, result.page_size); + result.assertIntegrity(); +} + +test "PageList PageAllocation rejects limits before modifying the destination" { + const testing = std.testing; + + var result = try init(testing.allocator, .{ + .cols = 1, + .rows = 1, + .max_size = 0, + .max_lines = null, + }); + defer result.deinit(); + + // The effective minimum permits one complete page beyond the active page. + // Fill that allowance so the following allocation exceeds the byte limit. + { + var allocation = try result.allocatePage(.{ .cols = 1, .rows = 1 }); + defer allocation.deinit(); + allocation.page().size.rows = 1; + try allocation.finalize(.prepend); + } + + const before_first = result.pages.first.?; + const before_total_rows = result.total_rows; + const before_page_size = result.page_size; + + var allocation = try result.allocatePage(.{ .cols = 1, .rows = 1 }); + defer allocation.deinit(); + allocation.page().size.rows = 1; + try testing.expectError( + error.MaxSizeExceeded, + allocation.finalize(.prepend), + ); + + try testing.expectEqual(before_first, result.pages.first.?); + try testing.expectEqual(before_total_rows, result.total_rows); + try testing.expectEqual(before_page_size, result.page_size); + result.assertIntegrity(); +} + +test "PageList PageAllocation allocation failure leaves list unchanged" { + const testing = std.testing; + + var failing = testing.FailingAllocator.init(testing.allocator, .{}); + var result = try init(failing.allocator(), .{ + .cols = 1, + .rows = 1, + .max_size = null, + .max_lines = null, + }); + defer result.deinit(); + + const initial_first = result.pages.first.?; + const initial_total_rows = result.total_rows; + const initial_page_size = result.page_size; + + // Existing pool capacity is deliberately an implementation detail. Allow + // preheated slots to succeed until allocation reaches node-pool growth. + failing.fail_index = failing.alloc_index; + var allocations: [64]PageAllocation = undefined; + var allocation_count: usize = 0; + defer for (allocations[0..allocation_count]) |*allocation| { + allocation.deinit(); + }; + + var failed = false; + for (0..64) |_| { + allocations[allocation_count] = result.allocatePage(.{ + .cols = 1, + .rows = 1, + }) catch |err| { + try testing.expectEqual(error.OutOfMemory, err); + failed = true; + break; + }; + allocation_count += 1; + } + try testing.expect(failed); + try testing.expect(failing.has_induced_failure); + + // Detached allocations and failed pool growth never publish into the live + // list; the deferred cleanup returns every successful allocation. + try testing.expectEqual(initial_first, result.pages.first.?); + try testing.expectEqual(initial_total_rows, result.total_rows); + try testing.expectEqual(initial_page_size, result.page_size); + result.assertIntegrity(); +} + +fn mixedWidthPinListForTest(alloc: Allocator) !PageList { + var result = try init(alloc, .{ .cols = 2, .rows = 1 }); + errdefer result.deinit(); + + // This deliberately constructs a layout that normal PageList operations + // do not expose yet. Keep integrity checks paused through deinit so the + // fixture can exercise mixed-width traversal in isolation. + result.pauseIntegrityChecks(true); + + inline for (.{ 4, 3 }) |cols| { + const node = try result.createPage(.{ .cap = .{ + .cols = cols, + .rows = 1, + } }); + node.page().size.rows = 1; + result.pages.append(node); + result.total_rows += 1; + } + + // Desired geometry is wider than the first and last stored pages. + result.cols = 4; + return result; +} + +test "PageList Pin row movement clamps across mixed-width pages" { + const testing = std.testing; + + var s = try mixedWidthPinListForTest(testing.allocator); + defer s.deinit(); + + const first = s.pages.first.?; + const second = first.next.?; + const third = second.next.?; + + try testing.expect((Pin{ .node = third, .x = 2 }).eql( + (Pin{ .node = second, .x = 3 }).down(1).?, + )); + try testing.expect((Pin{ .node = first, .x = 1 }).eql( + (Pin{ .node = second, .x = 3 }).up(1).?, + )); + + switch ((Pin{ .node = second, .x = 3 }).downOverflow(10)) { + .offset => try testing.expect(false), + .overflow => |overflow| try testing.expect( + (Pin{ .node = third, .x = 2 }).eql(overflow.end), + ), + } + switch ((Pin{ .node = second, .x = 3 }).upOverflow(10)) { + .offset => try testing.expect(false), + .overflow => |overflow| try testing.expect( + (Pin{ .node = first, .x = 1 }).eql(overflow.end), + ), + } +} + +test "PageList Pin wrapping crosses mixed-width pages" { + const testing = std.testing; + + var s = try mixedWidthPinListForTest(testing.allocator); + defer s.deinit(); + + const first = s.pages.first.?; + const second = first.next.?; + const third = second.next.?; + + try testing.expect((Pin{ .node = third, .x = 2 }).eql( + (Pin{ .node = first, .x = 1 }).rightWrap(7).?, + )); + try testing.expect((Pin{ .node = second, .x = 0 }).eql( + (Pin{ .node = third, .x = 2 }).leftWrap(6).?, + )); + try testing.expect((Pin{ .node = first }).leftWrap(1) == null); + try testing.expect((Pin{ .node = third, .x = 2 }).rightWrap(1) == null); +} + +test "PageList Pin rejects columns beyond mixed-width page bounds" { + const testing = std.testing; + + var s = try mixedWidthPinListForTest(testing.allocator); + defer s.deinit(); + + try testing.expect(s.pin(.{ .screen = .{ .x = 1, .y = 0 } }) != null); + try testing.expect(s.pin(.{ .screen = .{ .x = 2, .y = 0 } }) == null); + try testing.expect(s.pin(.{ .screen = .{ .x = 3, .y = 1 } }) != null); + try testing.expect(s.pin(.{ .screen = .{ .x = 3, .y = 2 } }) == null); +} + +pub const Cell = struct { + node: *List.Node, + row: *pagepkg.Row, + cell: *pagepkg.Cell, + row_idx: size.CellCountInt, + col_idx: size.CellCountInt, + + /// Returns true if this cell is marked as dirty. + /// /// This is not very performant this is primarily used for assertions /// and testing. pub fn isDirty(self: Cell) bool { @@ -6661,7 +8148,7 @@ fn expectLivePageSerialsValidForTest(self: *const PageList) !void { test "PageList Pin rightWrap exact row multiple" { const testing = std.testing; - var s = try init(testing.allocator, 10, 3, null); + var s = try init(testing.allocator, .{ .cols = 10, .rows = 3 }); defer s.deinit(); const start = s.pin(.{ .active = .{ .x = 5, .y = 0 } }).?; @@ -6677,7 +8164,7 @@ test "PageList Pin rightWrap exact row multiple" { test "PageList Pin leftWrap exact row multiple" { const testing = std.testing; - var s = try init(testing.allocator, 10, 3, null); + var s = try init(testing.allocator, .{ .cols = 10, .rows = 3 }); defer s.deinit(); const start = s.pin(.{ .active = .{ .x = 5, .y = 2 } }).?; @@ -6693,7 +8180,7 @@ test "PageList Pin leftWrap exact row multiple" { test "PageList Pin rightWrap maximum distance" { const testing = std.testing; - var s = try init(testing.allocator, 1, 3, null); + var s = try init(testing.allocator, .{ .cols = 1, .rows = 3 }); defer s.deinit(); const start = s.pin(.{ .active = .{ .y = 0 } }).?; @@ -6703,7 +8190,7 @@ test "PageList Pin rightWrap maximum distance" { test "PageList Pin leftWrap maximum distance" { const testing = std.testing; - var s = try init(testing.allocator, 1, 3, null); + var s = try init(testing.allocator, .{ .cols = 1, .rows = 3 }); defer s.deinit(); const start = s.pin(.{ .active = .{ .y = 2 } }).?; @@ -6713,7 +8200,7 @@ test "PageList Pin leftWrap maximum distance" { test "PageList incremental compression skips visible history" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(3); @@ -6766,7 +8253,7 @@ test "PageList incremental compression skips visible history" { test "PageList owns incremental compression state" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const state: IncrementalCompressionState = .{ @@ -6840,7 +8327,7 @@ test "PageList owns incremental compression state" { test "PageList replacements preserve compression continuation and mark activity" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const state: IncrementalCompressionState = .{ @@ -6868,7 +8355,7 @@ test "PageList replacements preserve compression continuation and mark activity" test "PageList incremental compression bounds inspected pages" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(incremental_compression_max_inspected + 1); @@ -6912,7 +8399,7 @@ test "PageList incremental compression bounds inspected pages" { test "PageList incremental compression advances after failure" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(2); @@ -6937,7 +8424,7 @@ test "PageList incremental compression advances after allocation failure" { var failing = testing.FailingAllocator.init(testing.allocator, .{}); const alloc = failing.allocator(); - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(2); const first = s.pages.first.?; @@ -6965,7 +8452,7 @@ test "PageList incremental compression advances after decommit failure" { const tw = compressPage_tw; defer tw.end(.reset) catch unreachable; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(2); @@ -6985,7 +8472,7 @@ test "PageList incremental compression advances after decommit failure" { test "PageList incremental compression restarts after replacement" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(1); @@ -7016,7 +8503,7 @@ test "PageList incremental compression restarts after replacement" { test "PageList incremental compression restarts after reset" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(1); @@ -7035,7 +8522,7 @@ test "PageList incremental compression restarts after reset" { test "PageList incremental compression restarts after active boundary resize" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(1); @@ -7072,12 +8559,11 @@ test "PageList incremental compression restarts after active boundary resize" { test "PageList incremental compression restarts after prune reuse" { const testing = std.testing; - var s = try init( - testing.allocator, - 80, - 24, - 2 * PagePool.item_size, - ); + var s = try init(testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_size = 2 * PagePool.item_size, + }); defer s.deinit(); try s.growColdPagesForTest(1); @@ -7103,12 +8589,11 @@ test "PageList incremental compression restarts after prune reuse" { test "PageList bounded pruning after partial erase preserves live serials" { const testing = std.testing; - var s = try init( - testing.allocator, - 80, - 24, - 2 * PagePool.item_size, - ); + var s = try init(testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_size = 2 * PagePool.item_size, + }); defer s.deinit(); while (s.totalPages() < 2) _ = try s.grow(); @@ -7129,7 +8614,7 @@ test "PageList bounded pruning after partial erase preserves live serials" { test "PageList partial erase restarts compression before continuation" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(incremental_compression_max_inspected + 1); _ = s.compress(.full); @@ -7162,12 +8647,11 @@ test "PageList partial erase restarts compression before continuation" { test "PageList bounded pruning after split invalidation preserves live serials" { const testing = std.testing; - var s = try init( - testing.allocator, - 80, - 24, - 2 * PagePool.item_size, - ); + var s = try init(testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_size = 2 * PagePool.item_size, + }); defer s.deinit(); while (s.totalPages() < 2) _ = try s.grow(); @@ -7191,12 +8675,11 @@ test "PageList bounded pruning after split invalidation preserves live serials" test "PageList repeated bounded pruning after split preserves live serials" { const testing = std.testing; - var s = try init( - testing.allocator, - 80, - 24, - 3 * PagePool.item_size, - ); + var s = try init(testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_size = 3 * PagePool.item_size, + }); defer s.deinit(); const epoch = s.page_serial_epoch; @@ -7227,12 +8710,11 @@ test "PageList repeated bounded pruning after split preserves live serials" { test "PageList bounded pruning after front replacement preserves live serials" { const testing = std.testing; - var s = try init( - testing.allocator, - 80, - 24, - 2 * PagePool.item_size, - ); + var s = try init(testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_size = 2 * PagePool.item_size, + }); defer s.deinit(); while (s.totalPages() < 2) _ = try s.grow(); @@ -7251,12 +8733,11 @@ test "PageList bounded pruning after front replacement preserves live serials" { test "PageList bounded pruning after middle replacement preserves live serials" { const testing = std.testing; - var s = try init( - testing.allocator, - 80, - 24, - 3 * PagePool.item_size, - ); + var s = try init(testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_size = 3 * PagePool.item_size, + }); defer s.deinit(); while (s.totalPages() < 3) _ = try s.grow(); @@ -7277,7 +8758,7 @@ test "PageList bounded pruning after middle replacement preserves live serials" test "PageList incremental compression restarts after earlier replacement" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(3); @@ -7305,7 +8786,7 @@ test "PageList incremental compression restarts after earlier replacement" { test "PageList incremental compression keeps progress after tail growth" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(incremental_compression_max_inspected + 1); _ = s.compress(.full); @@ -7335,7 +8816,7 @@ test "PageList incremental compression keeps progress after tail growth" { test "PageList memory stats do not restore compressed pages" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(2); @@ -7408,7 +8889,7 @@ test "PageList preserved page keeps compressed storage" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const node = s.pages.first.?; @@ -7469,7 +8950,7 @@ test "PageList preserved page keeps compressed storage" { try testing.expect(page_.dirty); try testing.expectEqual( @as(u21, 'X'), - page_.getRowAndCell(3, 2).cell.content.codepoint, + page_.getRowAndCell(3, 2).cell.content.codepoint.data, ); // The node still owns the same compressed representation, and neither its @@ -7488,7 +8969,7 @@ test "PageList preserved page keeps compressed storage" { test "PageList memory stats include unused pool backing" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Pool allocation ownership is based on the requested layout fitting in a @@ -7530,7 +9011,7 @@ test "PageList memory stats include unused pool backing" { test "PageList does not compress the mixed history and active page" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // One additional row creates history, but the history and all active rows @@ -7552,7 +9033,7 @@ test "PageList compresses only complete cold history pages" { // More active rows than one page at these dimensions can hold ensures the // active area spans multiple nodes when the pass chooses its boundary. const active_rows = initialCapacity(80).rows + 1; - var s = try init(alloc, 80, active_rows, null); + var s = try init(alloc, .{ .cols = 80, .rows = active_rows }); defer s.deinit(); try s.growColdPagesForTest(2); @@ -7612,7 +9093,7 @@ test "PageList compresses only complete cold history pages" { test "PageList lazily restores compressed history made active by resize" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(1); @@ -7642,7 +9123,7 @@ test "PageList lazily restores compressed history made active by resize" { _ = s.compress(.full); try testing.expect(first.isCompressed()); const cell = s.getCell(.{ .active = .{} }).?; - try testing.expectEqual(@as(u21, 'X'), cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'X'), cell.cell.content.codepoint.data); try testing.expect(!first.isCompressed()); try testing.expectEqual(memory_ptr, first.page().memory.ptr); try testing.expectEqual(memory_len, first.page().memory.len); @@ -7652,11 +9133,11 @@ test "PageList lazily restores compressed history made active by resize" { test "PageList full and incremental compression skip a spanning viewport" { const testing = std.testing; - var full = try init(testing.allocator, 80, 24, null); + var full = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer full.deinit(); try full.growColdPagesForTest(3); - var incremental = try init(testing.allocator, 80, 24, null); + var incremental = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer incremental.deinit(); try incremental.growColdPagesForTest(3); @@ -7708,7 +9189,7 @@ test "PageList full and incremental compression skip a spanning viewport" { test "PageList cold compression continues after an incompressible page" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growColdPagesForTest(2); @@ -7740,7 +9221,7 @@ test "PageList compression restores through page access" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const node = s.pages.first.?; @@ -7766,7 +9247,7 @@ test "PageList compression restores through page access" { const page_pin: Pin = .{ .node = node, .x = 3, .y = 2 }; try testing.expectEqual( @as(u21, 'X'), - page_pin.rowAndCell().cell.content.codepoint, + page_pin.rowAndCell().cell.content.codepoint.data, ); try testing.expect(!node.isCompressed()); try testing.expectEqual(memory_ptr, node.page().memory.ptr); @@ -7791,7 +9272,7 @@ test "PageList compression restores through page access" { try testing.expect(!node.isCompressed()); try testing.expectEqual( @as(u21, 'X'), - cloned.pages.first.?.page().getRowAndCell(3, 2).cell.content.codepoint, + cloned.pages.first.?.page().getRowAndCell(3, 2).cell.content.codepoint.data, ); } @@ -7799,7 +9280,7 @@ test "PageList compression uses temporary scratch for oversized pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); var node = s.pages.first.?; @@ -7828,7 +9309,7 @@ test "PageList compression leaves incompressible pages resident" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const node = s.pages.first.?; @@ -7848,7 +9329,7 @@ test "PageList compression leaves incompressible pages resident" { test "PageList reset discards malformed compressed data" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const node = s.pages.first.?; @@ -7863,7 +9344,7 @@ test "PageList reset discards malformed compressed data" { test "PageList deinit discards malformed compressed data" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); const node = s.pages.first.?; try testing.expect(s.compressPage(node)); @memset(node.data.compressed.encoded, 0xFF); @@ -7874,12 +9355,11 @@ test "PageList deinit discards malformed compressed data" { test "PageList prune reuses malformed compressed page memory" { const testing = std.testing; - var s = try init( - testing.allocator, - 80, - 24, - 2 * PagePool.item_size, - ); + var s = try init(testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_size = 2 * PagePool.item_size, + }); defer s.deinit(); // Allocate the second page so the first one can be pruned and reused. @@ -7910,7 +9390,7 @@ test "PageList" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try testing.expect(s.viewport == .active); try testing.expect(s.pages.first != null); @@ -7947,12 +9427,10 @@ test "PageList init error" { tw.errorAlways(tag, error.OutOfMemory); try std.testing.expectError( error.OutOfMemory, - init( - std.testing.allocator, - 80, - 24, - null, - ), + init(std.testing.allocator, .{ + .cols = 80, + .rows = 24, + }), ); } @@ -7966,12 +9444,10 @@ test "PageList init error" { const cols: size.CellCountInt = if (tag == .page_buf_std) 80 else std_capacity.maxCols().? + 1; try std.testing.expectError( error.OutOfMemory, - init( - std.testing.allocator, - cols, - 24, - null, - ), + init(std.testing.allocator, .{ + .cols = cols, + .rows = 24, + }), ); } @@ -7984,12 +9460,10 @@ test "PageList init error" { tw.errorAfter(tag, error.OutOfMemory, 1); try std.testing.expectError( error.OutOfMemory, - init( - std.testing.allocator, - std_capacity.maxCols().? + 1, - std_capacity.rows + 1, - null, - ), + init(std.testing.allocator, .{ + .cols = std_capacity.maxCols().? + 1, + .rows = std_capacity.rows + 1, + }), ); } } @@ -8010,7 +9484,7 @@ test "PageList init rows across two pages" { }; // Init - var s = try init(alloc, cap.cols, rows, null); + var s = try init(alloc, .{ .cols = cap.cols, .rows = rows }); defer s.deinit(); try testing.expect(s.viewport == .active); try testing.expect(s.pages.first != null); @@ -8034,12 +9508,10 @@ test "PageList init more than max cols" { // Initialize with more columns than we can fit in our standard // capacity. This is going to force us to go to a non-standard page // immediately. - var s = try init( - alloc, - std_capacity.maxCols().? + 1, - 80, - null, - ); + var s = try init(alloc, .{ + .cols = std_capacity.maxCols().? + 1, + .rows = 80, + }); defer s.deinit(); try testing.expect(s.viewport == .active); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); @@ -8063,7 +9535,7 @@ test "PageList pointFromPin active no history" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); { @@ -8096,7 +9568,7 @@ test "PageList pointFromPin active with history" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(30); @@ -8127,7 +9599,7 @@ test "PageList pointFromPin active from prior page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up at least 5 pages. const page = s.pages.last.?.page(); @@ -8169,7 +9641,7 @@ test "PageList pointFromPin traverse pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up at least 2 pages. @@ -8253,7 +9725,7 @@ test "PageList active after grow" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); @@ -8297,7 +9769,7 @@ test "PageList grow allows exceeding max size for active area" { // Setup our initial page so that we fully take up one page. const cap = try std_capacity.adjust(.{ .cols = 5 }); - var s = try init(alloc, 5, cap.rows, 0); + var s = try init(alloc, .{ .cols = 5, .rows = cap.rows, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); @@ -8330,7 +9802,7 @@ test "PageList grow prune required with a single page" { const alloc = testing.allocator; // Need scrollback > 0 to have a scrollbar to test - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // This block is all test setup. There is nothing required about this @@ -8375,7 +9847,7 @@ test "PageList scrollbar with max_size 0 after grow" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, 0); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = 0 }); defer s.deinit(); // Grow some rows (simulates normal terminal output) @@ -8394,7 +9866,7 @@ test "PageList scroll with max_size 0 no history" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, 0); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = 0 }); defer s.deinit(); try s.growRows(10); @@ -8416,7 +9888,7 @@ test "PageList scroll top" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -8479,7 +9951,7 @@ test "PageList scroll delta row back" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -8535,7 +10007,7 @@ test "PageList scroll delta row back overflow" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -8582,7 +10054,7 @@ test "PageList scroll delta row back overflow" { test "PageList scroll minimum row delta" { const testing = std.testing; - var s = try init(testing.allocator, 10, 3, null); + var s = try init(testing.allocator, .{ .cols = 10, .rows = 3 }); defer s.deinit(); // Create one row of history so scrolling all the way back has an @@ -8597,7 +10069,7 @@ test "PageList scroll delta row forward" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -8646,7 +10118,7 @@ test "PageList scroll delta row forward into active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); s.scroll(.{ .delta_row = 2 }); @@ -8670,7 +10142,7 @@ test "PageList scroll delta row back without space preserves active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); s.scroll(.{ .delta_row = -1 }); @@ -8695,7 +10167,7 @@ test "PageList scroll to pin" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -8742,7 +10214,7 @@ test "PageList scroll to pin in active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -8770,7 +10242,7 @@ test "PageList scroll to pin at top" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -8800,7 +10272,7 @@ test "PageList scroll to row 0" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -8849,7 +10321,7 @@ test "PageList scroll to row in scrollback" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(20); @@ -8897,7 +10369,7 @@ test "PageList scroll to row in middle" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(50); @@ -8940,7 +10412,7 @@ test "PageList scroll to row at active boundary" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(20); @@ -8979,7 +10451,7 @@ test "PageList scroll to row beyond active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(10); @@ -9006,7 +10478,7 @@ test "PageList scroll to row without scrollback" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); s.scroll(.{ .row = 5 }); @@ -9032,7 +10504,7 @@ test "PageList scroll to row then delta" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(30); @@ -9095,7 +10567,7 @@ test "PageList scroll to row with cache fast path down" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(50); @@ -9158,7 +10630,7 @@ test "PageList scroll to row with cache fast path up" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(50); @@ -9221,21 +10693,21 @@ test "PageList scroll clear" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); { const cell = s.getCell(.{ .active = .{ .x = 0, .y = 0 } }).?; cell.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } { const cell = s.getCell(.{ .active = .{ .x = 0, .y = 1 } }).?; cell.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } @@ -9254,7 +10726,7 @@ test "PageList: jump zero prompts" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 3, null); + var s = try init(alloc, .{ .cols = 5, .rows = 3 }); defer s.deinit(); try s.growRows(3); try testing.expect(s.pages.first == s.pages.last); @@ -9281,7 +10753,7 @@ test "PageList: jump zero prompts" { test "PageList: jump minimum prompt delta" { const testing = std.testing; - var s = try init(testing.allocator, 10, 3, null); + var s = try init(testing.allocator, .{ .cols = 10, .rows = 3 }); defer s.deinit(); s.scroll(.{ .delta_prompt = std.math.minInt(isize) }); @@ -9292,7 +10764,7 @@ test "Screen: jump back one prompt" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 3, null); + var s = try init(alloc, .{ .cols = 5, .rows = 3 }); defer s.deinit(); try s.growRows(3); try testing.expect(s.pages.first == s.pages.last); @@ -9361,7 +10833,7 @@ test "Screen: jump forward prompt skips multiline continuation" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 3, null); + var s = try init(alloc, .{ .cols = 5, .rows = 3 }); defer s.deinit(); try s.growRows(7); @@ -9410,7 +10882,7 @@ test "PageList grow fit in capacity" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // So we know we're using capacity to grow @@ -9432,7 +10904,7 @@ test "PageList grow allocate" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow to capacity @@ -9497,63 +10969,478 @@ test "PageList Cell screenPoint supports long scrollback" { } }, cell.screenPoint()); } -test "PageList grow prune scrollback" { +test "PageList set max bytes prunes immediately and can be raised" { const testing = std.testing; - const alloc = testing.allocator; + const cols: size.CellCountInt = 80; + const page_rows: usize = initialCapacity(cols).rows; - // Use std_size to limit scrollback so pruning is triggered. - var s = try init(alloc, 80, 24, std_size); + var s = try init(testing.allocator, .{ + .cols = cols, + .rows = 1, + .max_size = null, + }); defer s.deinit(); - // Grow to capacity - const page1_node = s.pages.last.?; - const page1 = page1_node.page(); - for (0..page1.capacity.rows - page1.size.rows) |_| { - try testing.expect(try s.grow() == null); - } + // Build four complete pages of history followed by the active row. + try s.growRows(4 * page_rows); + try testing.expectEqual(@as(usize, 5), s.totalPages()); + + const removed = s.pages.first.?; + const retained = s.pages.last.?.prev.?; + const removed_pin = try s.trackPin(.{ .node = removed }); + defer s.untrackPin(removed_pin); + const retained_pin = try s.trackPin(.{ .node = retained }); + defer s.untrackPin(retained_pin); + + s.scroll(.{ .pin = retained_pin.* }); + try testing.expectEqual(3 * page_rows, s.scrollbar().offset); + + // The active-area minimum is two pages. Lowering below that immediately + // removes all older complete historical pages. + s.setMaxBytes(PagePool.item_size); + try testing.expectEqual(PagePool.item_size, s.limits.bytes.explicit); + try testing.expectEqual(2 * PagePool.item_size, s.limits.max(.bytes)); + try testing.expectEqual(s.limits.max(.bytes), s.page_size); + try testing.expectEqual(@as(usize, 2), s.totalPages()); + try testing.expectEqual(page_rows, s.total_rows - s.rows); + try testing.expectEqual(retained, s.pages.first.?); + try testing.expectEqual(retained, removed_pin.node); + try testing.expect(removed_pin.garbage); + try testing.expectEqual(retained, retained_pin.node); + try testing.expect(!retained_pin.garbage); + try testing.expectEqual(@as(usize, 0), s.scrollbar().offset); + + // Raising the limit doesn't allocate or otherwise change retained data, + // but subsequent growth can exceed the previous effective limit. + const limited_size = s.page_size; + const limited_rows = s.total_rows; + s.setMaxBytes(8 * PagePool.item_size); + try testing.expectEqual(limited_size, s.page_size); + try testing.expectEqual(limited_rows, s.total_rows); + try s.growRows(2 * page_rows); + try testing.expect(s.page_size > limited_size); + + // Null restores unlimited growth and likewise preserves current data. + const raised_size = s.page_size; + const raised_rows = s.total_rows; + s.setMaxBytes(null); + try testing.expectEqual( + std.math.maxInt(usize), + s.limits.bytes.explicit, + ); + try testing.expectEqual(raised_size, s.page_size); + try testing.expectEqual(raised_rows, s.total_rows); + try s.growRows(5 * page_rows); + try testing.expect(s.page_size > 8 * PagePool.item_size); +} - // Grow and allocate one more page. Then fill that page up. - const page2_node = (try s.grow()).?; - const page2 = page2_node.page(); - for (0..page2.capacity.rows - page2.size.rows) |_| { - try testing.expect(try s.grow() == null); - } +test "PageList set max bytes zero preserves active boundary" { + const testing = std.testing; - // Get our page size - const old_page_size = s.page_size; + var s = try init(testing.allocator, .{ + .cols = 80, + .rows = 1, + .max_size = null, + }); + defer s.deinit(); - // Create a tracked pin in the first page - const p = try s.trackPin(s.pin(.{ .screen = .{} }).?); - defer s.untrackPin(p); - try testing.expect(p.node == s.pages.first.?); + // Make the sole page larger than the effective zero-byte limit. Its first + // row will be history, but the same indivisible page also contains active. + while (s.page_size <= s.limits.bytes.min) { + _ = try s.increaseCapacity(s.pages.first.?, .grapheme_bytes); + } + _ = try s.grow(); + try testing.expectEqual(@as(usize, 1), s.totalPages()); + try testing.expectEqual(s.pages.first.?, s.getTopLeft(.active).node); + try testing.expect(s.getTopLeft(.active).y > 0); - // Scroll back to create a pinned viewport (not active) - const pin_y = page1.capacity.rows / 2; - s.scroll(.{ .pin = s.pin(.{ .screen = .{ .y = pin_y } }).? }); - try testing.expect(s.viewport == .pin); + s.scroll(.top); + try testing.expect(s.viewport == .top); - // Get the scrollbar state to populate the cache - const scrollbar_before = s.scrollbar(); - try testing.expectEqual(pin_y, scrollbar_before.offset); + s.setMaxBytes(0); + try testing.expectEqual(@as(usize, 0), s.limits.bytes.explicit); + try testing.expect(s.page_size > s.limits.max(.bytes)); + try testing.expectEqual(@as(usize, 1), s.totalPages()); + try testing.expect(s.viewport == .active); + try testing.expectEqual(Scrollbar{ + .total = s.rows, + .offset = 0, + .len = s.rows, + }, s.scrollbar()); - // Next should create a new page, but it should reuse our first - // page since we're at max size. - const new = (try s.grow()).?; - try testing.expect(s.pages.last.? == new); - try testing.expectEqual(s.page_size, old_page_size); + // No-scrollback mode cannot be moved back into the retained boundary row. + s.scroll(.top); + try testing.expect(s.viewport == .active); +} - // Our first should now be page2 and our last should be page1 - try testing.expectEqual(page2_node, s.pages.first.?); - try testing.expectEqual(page1_node, s.pages.last.?); +test "PageList set max lines prunes immediately and can be raised" { + const testing = std.testing; + const cols: size.CellCountInt = 80; + const page_rows: usize = initialCapacity(cols).rows; + const lowered_lines = page_rows + page_rows / 2; - // Our tracked pin should point to the top-left of the first page - try testing.expect(p.node == s.pages.first.?); - try testing.expect(p.x == 0); - try testing.expect(p.y == 0); - try testing.expect(p.garbage); + var s = try init(testing.allocator, .{ + .cols = cols, + .rows = 1, + .max_size = null, + .max_lines = null, + }); + defer s.deinit(); - // Verify the viewport offset cache was invalidated. After pruning, - // the offset should have changed because we removed rows from + try s.growRows(4 * page_rows); + try testing.expectEqual(@as(usize, 5), s.totalPages()); + + const removed = s.pages.first.?; + const retained = s.pages.last.?.prev.?; + const removed_pin = try s.trackPin(.{ .node = removed }); + defer s.untrackPin(removed_pin); + const retained_pin = try s.trackPin(.{ .node = retained }); + defer s.untrackPin(retained_pin); + + s.scroll(.{ .pin = retained_pin.* }); + try testing.expectEqual(3 * page_rows, s.scrollbar().offset); + + // Whole-page enforcement undershoots a non-page-aligned line limit. + s.setMaxLines(lowered_lines); + try testing.expectEqual(lowered_lines, s.limits.lines.explicit); + try testing.expectEqual(lowered_lines, s.limits.max(.lines)); + try testing.expectEqual(page_rows, s.total_rows - s.rows); + try testing.expectEqual(@as(usize, 2), s.totalPages()); + try testing.expectEqual(retained, s.pages.first.?); + try testing.expectEqual(retained, removed_pin.node); + try testing.expect(removed_pin.garbage); + try testing.expectEqual(retained, retained_pin.node); + try testing.expect(!retained_pin.garbage); + try testing.expectEqual(@as(usize, 0), s.scrollbar().offset); + + const limited_size = s.page_size; + const limited_rows = s.total_rows; + s.setMaxLines(4 * page_rows); + try testing.expectEqual(limited_size, s.page_size); + try testing.expectEqual(limited_rows, s.total_rows); + try s.growRows(2 * page_rows); + try testing.expect(s.total_rows - s.rows > lowered_lines); + + const raised_size = s.page_size; + const raised_rows = s.total_rows; + s.setMaxLines(null); + try testing.expectEqual( + std.math.maxInt(usize), + s.limits.lines.explicit, + ); + try testing.expectEqual(raised_size, s.page_size); + try testing.expectEqual(raised_rows, s.total_rows); + try s.growRows(3 * page_rows); + try testing.expect(s.total_rows - s.rows > 4 * page_rows); +} + +test "PageList set max limits remain independent" { + const testing = std.testing; + const cols: size.CellCountInt = 80; + const page_rows: usize = initialCapacity(cols).rows; + const byte_limit = 3 * PagePool.item_size; + const line_limit = page_rows / 2; + + var s = try init(testing.allocator, .{ + .cols = cols, + .rows = 1, + .max_size = null, + .max_lines = null, + }); + defer s.deinit(); + + try s.growRows(4 * page_rows); + + // The byte setter leaves the line limit unlimited. + s.setMaxBytes(byte_limit); + try testing.expectEqual(byte_limit, s.limits.bytes.explicit); + try testing.expectEqual( + std.math.maxInt(usize), + s.limits.lines.explicit, + ); + try testing.expectEqual(@as(usize, 3), s.totalPages()); + try testing.expectEqual(2 * page_rows, s.total_rows - s.rows); + + // The smaller runtime line limit prunes one more complete page without + // changing the configured byte limit. Its effective value is raised to + // the existing one-page minimum. + s.setMaxLines(line_limit); + try testing.expectEqual(byte_limit, s.limits.bytes.explicit); + try testing.expectEqual(line_limit, s.limits.lines.explicit); + try testing.expectEqual(page_rows, s.limits.max(.lines)); + try testing.expectEqual(@as(usize, 2), s.totalPages()); + try testing.expectEqual(page_rows, s.total_rows - s.rows); + + // Removing only the line limit leaves byte enforcement in effect. + s.setMaxLines(null); + try s.growRows(3 * page_rows); + try testing.expectEqual(byte_limit, s.page_size); + try testing.expectEqual(@as(usize, 3), s.totalPages()); + try testing.expect(s.total_rows - s.rows > page_rows); +} + +test "PageList max lines uses one-page minimum" { + const testing = std.testing; + const cols: size.CellCountInt = 80; + const page_rows: usize = initialCapacity(cols).rows; + + var s = try init(testing.allocator, .{ + .cols = cols, + .rows = 1, + .max_lines = page_rows / 2, + }); + defer s.deinit(); + + try testing.expectEqual(page_rows, s.limits.max(.lines)); + + // The requested limit is below one page, so a complete page of history + // remains valid. + try s.growRows(page_rows); + try testing.expectEqual(page_rows, s.total_rows - s.rows); + try testing.expectEqual(@as(usize, 2), s.totalPages()); + + const first = s.pages.first.?; + const old_page_size = s.page_size; + + // One more row puts us over the effective limit. The now-complete + // historical page is removed rather than partially trimmed. + _ = try s.grow(); + try testing.expectEqual(@as(usize, 1), s.total_rows - s.rows); + try testing.expectEqual(@as(usize, 1), s.totalPages()); + try testing.expect(s.pages.first.? != first); + try testing.expectEqual( + old_page_size - PagePool.item_size, + s.page_size, + ); +} + +test "PageList max lines does not round larger limits" { + const testing = std.testing; + const cols: size.CellCountInt = 80; + const page_rows: usize = initialCapacity(cols).rows; + const max_lines = page_rows + page_rows / 2; + + var s = try init(testing.allocator, .{ + .cols = cols, + .rows = 1, + .max_lines = max_lines, + }); + defer s.deinit(); + + try testing.expectEqual(max_lines, s.limits.max(.lines)); + try s.growRows(max_lines); + try testing.expectEqual(max_lines, s.total_rows - s.rows); + + const first = s.pages.first.?; + const retained = first.next.?; + const removed_pin = try s.trackPin(.{ .node = first }); + defer s.untrackPin(removed_pin); + const retained_pin = try s.trackPin(.{ .node = retained }); + defer s.untrackPin(retained_pin); + + s.scroll(.{ .pin = retained_pin.* }); + try testing.expectEqual(page_rows, s.scrollbar().offset); + + const old_page_size = s.page_size; + _ = try s.grow(); + + // Whole-page pruning undershoots the requested limit without rounding it. + try testing.expectEqual( + max_lines + 1 - page_rows, + s.total_rows - s.rows, + ); + try testing.expectEqual(retained, s.pages.first.?); + try testing.expectEqual(retained, removed_pin.node); + try testing.expect(removed_pin.garbage); + try testing.expectEqual(retained, retained_pin.node); + try testing.expect(!retained_pin.garbage); + try testing.expectEqual(@as(usize, 0), s.scrollbar().offset); + try testing.expectEqual( + old_page_size - PagePool.item_size, + s.page_size, + ); +} + +test "PageList max lines and max size enforce the smaller limit" { + const testing = std.testing; + const cols: size.CellCountInt = 80; + const page_rows: usize = initialCapacity(cols).rows; + + // A line limit of one page keeps the logical allocation below a much + // larger byte limit. + { + var s = try init(testing.allocator, .{ + .cols = cols, + .rows = 1, + .max_size = 8 * PagePool.item_size, + .max_lines = page_rows, + }); + defer s.deinit(); + + try s.growRows(4 * page_rows); + try testing.expect( + s.total_rows - s.rows <= s.limits.max(.lines), + ); + try testing.expect(s.totalPages() <= 2); + try testing.expect(s.page_size < s.limits.max(.bytes)); + } + + // A two-page byte limit prunes before the larger line limit is reached. + { + var s = try init(testing.allocator, .{ + .cols = cols, + .rows = 1, + .max_size = PagePool.item_size, + .max_lines = 4 * page_rows, + }); + defer s.deinit(); + + try s.growRows(2 * page_rows); + try testing.expect( + s.total_rows - s.rows < s.limits.max(.lines), + ); + try testing.expectEqual(s.limits.max(.bytes), s.page_size); + } +} + +test "PageList max lines applies to resize and clone" { + const testing = std.testing; + const cols: size.CellCountInt = 80; + const page_rows: usize = initialCapacity(cols).rows; + + var s = try init(testing.allocator, .{ + .cols = cols, + .rows = 2, + .max_lines = page_rows, + }); + defer s.deinit(); + + try s.growRows(page_rows); + try testing.expectEqual(page_rows, s.total_rows - s.rows); + + // Prevent row shrinking from trimming the trailing active row instead of + // turning it into history. + const cell = s.getCell(.{ .active = .{ .y = 1 } }).?; + cell.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 'A' } }, + }; + + try s.resize(.{ .rows = 1, .reflow = false }); + try testing.expectEqual(@as(usize, 1), s.total_rows - s.rows); + + const new_cols: size.CellCountInt = cols + 1; + try s.resize(.{ .cols = new_cols, .reflow = true }); + try testing.expectEqual( + Limits.minMaxLines(new_cols), + s.limits.lines.min, + ); + + // Exercise the same active-row shrink through the reflow path. Reflow + // completes before the newly historical complete page is pruned. + { + var reflowed = try init(testing.allocator, .{ + .cols = cols, + .rows = 2, + .max_lines = page_rows, + }); + defer reflowed.deinit(); + + try reflowed.growRows(page_rows); + const active_cell = reflowed.getCell(.{ .active = .{ .y = 1 } }).?; + active_cell.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 'A' } }, + }; + + try reflowed.resize(.{ + .cols = new_cols, + .rows = 1, + .reflow = true, + }); + try testing.expectEqual( + Limits.minMaxLines(new_cols), + reflowed.limits.lines.min, + ); + try testing.expect( + reflowed.total_rows - reflowed.rows <= + reflowed.limits.max(.lines) or + reflowed.pages.first.? == + reflowed.getTopLeft(.active).node, + ); + } + + var cloned = try s.clone(testing.allocator, .{ + .top = .{ .screen = .{} }, + }); + defer cloned.deinit(); + + try testing.expectEqual(s.limits, cloned.limits); + + try cloned.growRows(2 * cloned.limits.max(.lines)); + try testing.expect( + cloned.total_rows - cloned.rows <= cloned.limits.max(.lines) or + cloned.pages.first.? == cloned.getTopLeft(.active).node, + ); +} + +test "PageList grow prune scrollback" { + const testing = std.testing; + const alloc = testing.allocator; + + // Use std_size to limit scrollback so pruning is triggered. + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = std_size }); + defer s.deinit(); + + // Grow to capacity + const page1_node = s.pages.last.?; + const page1 = page1_node.page(); + for (0..page1.capacity.rows - page1.size.rows) |_| { + try testing.expect(try s.grow() == null); + } + + // Grow and allocate one more page. Then fill that page up. + const page2_node = (try s.grow()).?; + const page2 = page2_node.page(); + for (0..page2.capacity.rows - page2.size.rows) |_| { + try testing.expect(try s.grow() == null); + } + + // Get our page size + const old_page_size = s.page_size; + + // Create a tracked pin in the first page + const p = try s.trackPin(s.pin(.{ .screen = .{} }).?); + defer s.untrackPin(p); + try testing.expect(p.node == s.pages.first.?); + + // Scroll back to create a pinned viewport (not active) + const pin_y = page1.capacity.rows / 2; + s.scroll(.{ .pin = s.pin(.{ .screen = .{ .y = pin_y } }).? }); + try testing.expect(s.viewport == .pin); + + // Get the scrollbar state to populate the cache + const scrollbar_before = s.scrollbar(); + try testing.expectEqual(pin_y, scrollbar_before.offset); + + // Next should create a new page, but it should reuse our first + // page since we're at max size. + const new = (try s.grow()).?; + try testing.expect(s.pages.last.? == new); + try testing.expectEqual(s.page_size, old_page_size); + + // Our first should now be page2 and our last should be page1 + try testing.expectEqual(page2_node, s.pages.first.?); + try testing.expectEqual(page1_node, s.pages.last.?); + + // Our tracked pin should point to the top-left of the first page + try testing.expect(p.node == s.pages.first.?); + try testing.expect(p.x == 0); + try testing.expect(p.y == 0); + try testing.expect(p.garbage); + + // Verify the viewport offset cache was invalidated. After pruning, + // the offset should have changed because we removed rows from // the beginning. { const scrollbar_after = s.scrollbar(); @@ -9571,7 +11458,7 @@ test "PageList grow prune scrollback with viewport pin not in pruned page" { const alloc = testing.allocator; // Use std_size to limit scrollback so pruning is triggered. - var s = try init(alloc, 80, 24, std_size); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = std_size }); defer s.deinit(); // Grow to capacity of first page @@ -9630,7 +11517,7 @@ test "PageList eraseRows invalidates viewport offset cache" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up several pages worth of history @@ -9670,7 +11557,7 @@ test "PageList eraseRow invalidates viewport offset cache" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up several pages worth of history @@ -9709,7 +11596,7 @@ test "PageList eraseRowBounded invalidates viewport offset cache" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up several pages worth of history @@ -9748,7 +11635,7 @@ test "PageList eraseRowBounded invalidates viewport offset cache" { test "PageList row erasure renews affected page generations" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); while (s.totalPages() < 2) _ = try s.grow(); @@ -9778,7 +11665,7 @@ test "PageList row erasure renews affected page generations" { test "PageList trailing row truncation renews page generation" { const testing = std.testing; - var s = try init(testing.allocator, 80, 24, null); + var s = try init(testing.allocator, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const node = s.pages.last.?; @@ -9796,7 +11683,7 @@ test "PageList eraseRowBounded multi-page invalidates viewport offset cache" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up several pages worth of history @@ -9837,7 +11724,7 @@ test "PageList eraseRowBounded full page shift invalidates viewport offset cache const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up several pages worth of history @@ -9880,7 +11767,7 @@ test "PageList eraseRowBounded exhausts pages invalidates viewport offset cache" const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up several pages worth of history @@ -9925,7 +11812,7 @@ test "PageList increaseCapacity to increase styles" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); const original_styles_cap = s.pages.first.?.capacity().styles; @@ -9940,7 +11827,7 @@ test "PageList increaseCapacity to increase styles" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -9965,8 +11852,70 @@ test "PageList increaseCapacity to increase styles" { const rac = page.getRowAndCell(x, y); try testing.expectEqual( @as(u21, @intCast(x)), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, + ); + } + } + } +} + +test "PageList increaseCapacity styles projects capacity from page density" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); + defer s.deinit(); + + const original_cap = s.pages.first.?.capacity().styles; + + // Write styled cells so the page has a measurable per-row style + // density (unlike the plain doubling test above, which grows a + // page with no styles in use). + const bold: stylepkg.Style = .{ .flags = .{ .bold = true } }; + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + for (0..s.rows) |y| { + for (0..s.cols) |x| { + const rac = page.getRowAndCell(x, y); + const style_id = try page.styles.add(page.memory, bold); + rac.row.styled = true; + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, + .style_id = style_id, + }; + } + } + } + + _ = try s.increaseCapacity(s.pages.first.?, .styles); + + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + // The page uses only two active rows out of thousands of rows + // of capacity, so the projected full-page need saturates the + // 32x-per-event growth bound instead of merely doubling. + try testing.expectEqual( + original_cap * 32, + page.capacity.styles, + ); + + // All cell content and styles are preserved by the growth. + for (0..s.rows) |y| { + for (0..s.cols) |x| { + const rac = page.getRowAndCell(x, y); + try testing.expectEqual( + @as(u21, @intCast(x + 1)), + rac.cell.content.codepoint.data, ); + try testing.expect(rac.cell.style_id != stylepkg.default_id); + try testing.expect(bold.eql( + page.styles.get(page.memory, rac.cell.style_id).*, + )); } } } @@ -9976,7 +11925,7 @@ test "PageList increaseCapacity to increase graphemes" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); const original_cap = s.pages.first.?.capacity().grapheme_bytes; @@ -9990,7 +11939,7 @@ test "PageList increaseCapacity to increase graphemes" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -10009,8 +11958,72 @@ test "PageList increaseCapacity to increase graphemes" { const rac = page.getRowAndCell(x, y); try testing.expectEqual( @as(u21, @intCast(x)), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, + ); + } + } + } +} + +test "PageList increaseCapacity graphemes projects capacity from page density" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); + defer s.deinit(); + + const original_cap = s.pages.first.?.capacity().grapheme_bytes; + + // Write cells with grapheme data so the page has a measurable + // per-row grapheme density (unlike the plain doubling test above, + // which grows a page with no grapheme usage). + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + for (0..s.rows) |y| { + for (0..s.cols) |x| { + const rac = page.getRowAndCell(x, y); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, + }; + try page.appendGrapheme(rac.row, rac.cell, 0x0301); + try page.appendGrapheme(rac.row, rac.cell, 0x0302); + } + } + } + + _ = try s.increaseCapacity(s.pages.first.?, .grapheme_bytes); + + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + // The page uses only two active rows out of thousands of rows + // of capacity, so the projected full-page need saturates the + // 32x-per-event growth bound instead of merely doubling. + try testing.expectEqual( + original_cap * 32, + page.capacity.grapheme_bytes, + ); + + // All cell and grapheme content is preserved by the growth. + try testing.expectEqual( + @as(usize, s.rows * s.cols), + page.graphemeCount(), + ); + for (0..s.rows) |y| { + for (0..s.cols) |x| { + const rac = page.getRowAndCell(x, y); + try testing.expectEqual( + @as(u21, @intCast(x + 1)), + rac.cell.content.codepoint.data, ); + const cps = page.lookupGrapheme(rac.cell).?; + try testing.expectEqual(@as(usize, 2), cps.len); + try testing.expectEqual(@as(u21, 0x0301), cps[0]); + try testing.expectEqual(@as(u21, 0x0302), cps[1]); } } } @@ -10020,7 +12033,7 @@ test "PageList increaseCapacity to increase hyperlinks" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); const original_cap = s.pages.first.?.capacity().hyperlink_bytes; @@ -10034,7 +12047,7 @@ test "PageList increaseCapacity to increase hyperlinks" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -10053,7 +12066,7 @@ test "PageList increaseCapacity to increase hyperlinks" { const rac = page.getRowAndCell(x, y); try testing.expectEqual( @as(u21, @intCast(x)), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, ); } } @@ -10064,7 +12077,7 @@ test "PageList increaseCapacity to increase string_bytes" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); const original_cap = s.pages.first.?.capacity().string_bytes; @@ -10078,7 +12091,7 @@ test "PageList increaseCapacity to increase string_bytes" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -10097,7 +12110,7 @@ test "PageList increaseCapacity to increase string_bytes" { const rac = page.getRowAndCell(x, y); try testing.expectEqual( @as(u21, @intCast(x)), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, ); } } @@ -10108,7 +12121,7 @@ test "PageList increaseCapacity tracked pins" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); // Create a tracked pin on the first page @@ -10131,7 +12144,7 @@ test "PageList increaseCapacity returns OutOfSpace at max capacity" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); // Keep increasing styles capacity until we get OutOfSpace @@ -10153,7 +12166,7 @@ test "PageList increaseCapacity after col shrink" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 2, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 2, .max_size = 0 }); defer s.deinit(); // Shrink columns @@ -10181,7 +12194,7 @@ test "PageList increaseCapacity multi-page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow to create a second page @@ -10220,7 +12233,7 @@ test "PageList increaseCapacity preserves dirty flag" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 4, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 4, .max_size = 0 }); defer s.deinit(); // Set page dirty flag and mark some rows as dirty @@ -10251,7 +12264,7 @@ test "PageList pageIterator single page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // The viewport should be within a single page @@ -10274,7 +12287,7 @@ test "PageList pageIterator two pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow to capacity @@ -10310,7 +12323,7 @@ test "PageList pageIterator history two pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow to capacity @@ -10340,7 +12353,7 @@ test "PageList pageIterator reverse single page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // The viewport should be within a single page @@ -10363,7 +12376,7 @@ test "PageList pageIterator reverse two pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow to capacity @@ -10403,7 +12416,7 @@ test "PageList pageIterator reverse history two pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow to capacity @@ -10433,7 +12446,7 @@ test "PageList PageIterator reverse count includes row zero" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, null); + var s = try init(alloc, .{ .cols = 2, .rows = 2 }); defer s.deinit(); var it: PageIterator = .{ @@ -10451,7 +12464,7 @@ test "PageList PageIterator count crosses page boundaries" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const first = s.pages.first.?; @@ -10503,7 +12516,7 @@ test "PageList cellIterator" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10512,7 +12525,7 @@ test "PageList cellIterator" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -10553,7 +12566,7 @@ test "PageList cellIterator reverse" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10562,7 +12575,7 @@ test "PageList cellIterator reverse" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -10603,7 +12616,7 @@ test "PageList promptIterator left_up" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 20, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10660,7 +12673,7 @@ test "PageList promptIterator right_down" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 20, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10717,7 +12730,7 @@ test "PageList promptIterator right_down continuation at start" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 20, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10760,7 +12773,7 @@ test "PageList promptIterator right_down with prompt before continuation" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 20, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10799,7 +12812,7 @@ test "PageList promptIterator right_down limit inclusive" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 20, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10831,7 +12844,7 @@ test "PageList promptIterator left_up limit inclusive" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 20, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10864,7 +12877,7 @@ test "PageList highlightSemanticContent prompt" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10879,7 +12892,7 @@ test "PageList highlightSemanticContent prompt" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, .semantic_content = .prompt, }; } @@ -10889,7 +12902,7 @@ test "PageList highlightSemanticContent prompt" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'B' }, + .content = .{ .codepoint = .{ .data = 'B' } }, .semantic_content = .input, }; } @@ -10918,7 +12931,7 @@ test "PageList highlightSemanticContent prompt with output" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10933,7 +12946,7 @@ test "PageList highlightSemanticContent prompt with output" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -10943,7 +12956,7 @@ test "PageList highlightSemanticContent prompt with output" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'l' }, + .content = .{ .codepoint = .{ .data = 'l' } }, .semantic_content = .input, }; } @@ -10953,7 +12966,7 @@ test "PageList highlightSemanticContent prompt with output" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -10983,7 +12996,7 @@ test "PageList highlightSemanticContent prompt multiline" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -10998,7 +13011,7 @@ test "PageList highlightSemanticContent prompt multiline" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11009,7 +13022,7 @@ test "PageList highlightSemanticContent prompt multiline" { const cell = page.getRowAndCell(x, 6).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'c' }, + .content = .{ .codepoint = .{ .data = 'c' } }, .semantic_content = .input, }; } @@ -11039,7 +13052,7 @@ test "PageList highlightSemanticContent prompt only" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11053,7 +13066,7 @@ test "PageList highlightSemanticContent prompt only" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11083,7 +13096,7 @@ test "PageList highlightSemanticContent prompt to end of screen" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11097,7 +13110,7 @@ test "PageList highlightSemanticContent prompt to end of screen" { const cell = page.getRowAndCell(x, 15).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11106,7 +13119,7 @@ test "PageList highlightSemanticContent prompt to end of screen" { const cell = page.getRowAndCell(x, 15).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'c' }, + .content = .{ .codepoint = .{ .data = 'c' } }, .semantic_content = .input, }; } @@ -11131,7 +13144,7 @@ test "PageList highlightSemanticContent input basic" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11146,7 +13159,7 @@ test "PageList highlightSemanticContent input basic" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11156,7 +13169,7 @@ test "PageList highlightSemanticContent input basic" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'l' }, + .content = .{ .codepoint = .{ .data = 'l' } }, .semantic_content = .input, }; } @@ -11186,7 +13199,7 @@ test "PageList highlightSemanticContent input with output" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11201,7 +13214,7 @@ test "PageList highlightSemanticContent input with output" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11211,7 +13224,7 @@ test "PageList highlightSemanticContent input with output" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'c' }, + .content = .{ .codepoint = .{ .data = 'c' } }, .semantic_content = .input, }; } @@ -11221,7 +13234,7 @@ test "PageList highlightSemanticContent input with output" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11251,7 +13264,7 @@ test "PageList highlightSemanticContent input multiline with continuation" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11266,7 +13279,7 @@ test "PageList highlightSemanticContent input multiline with continuation" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11276,7 +13289,7 @@ test "PageList highlightSemanticContent input multiline with continuation" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'c' }, + .content = .{ .codepoint = .{ .data = 'c' } }, .semantic_content = .input, }; } @@ -11288,7 +13301,7 @@ test "PageList highlightSemanticContent input multiline with continuation" { const cell = page.getRowAndCell(x, 6).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '>' }, + .content = .{ .codepoint = .{ .data = '>' } }, .semantic_content = .prompt, }; } @@ -11298,7 +13311,7 @@ test "PageList highlightSemanticContent input multiline with continuation" { const cell = page.getRowAndCell(x, 6).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'd' }, + .content = .{ .codepoint = .{ .data = 'd' } }, .semantic_content = .input, }; } @@ -11328,7 +13341,7 @@ test "PageList highlightSemanticContent input no input returns null" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11343,7 +13356,7 @@ test "PageList highlightSemanticContent input no input returns null" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11353,7 +13366,7 @@ test "PageList highlightSemanticContent input no input returns null" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11376,7 +13389,7 @@ test "PageList highlightSemanticContent input to end of screen" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11390,7 +13403,7 @@ test "PageList highlightSemanticContent input to end of screen" { const cell = page.getRowAndCell(x, 15).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11399,7 +13412,7 @@ test "PageList highlightSemanticContent input to end of screen" { const cell = page.getRowAndCell(x, 15).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'c' }, + .content = .{ .codepoint = .{ .data = 'c' } }, .semantic_content = .input, }; } @@ -11424,7 +13437,7 @@ test "PageList highlightSemanticContent input prompt only returns null" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11439,7 +13452,7 @@ test "PageList highlightSemanticContent input prompt only returns null" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11471,7 +13484,7 @@ test "PageList highlightSemanticContent output basic" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11486,7 +13499,7 @@ test "PageList highlightSemanticContent output basic" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11496,7 +13509,7 @@ test "PageList highlightSemanticContent output basic" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'l' }, + .content = .{ .codepoint = .{ .data = 'l' } }, .semantic_content = .input, }; } @@ -11506,7 +13519,7 @@ test "PageList highlightSemanticContent output basic" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11542,7 +13555,7 @@ test "PageList highlightSemanticContent output multiline" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11557,7 +13570,7 @@ test "PageList highlightSemanticContent output multiline" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11567,7 +13580,7 @@ test "PageList highlightSemanticContent output multiline" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'l' }, + .content = .{ .codepoint = .{ .data = 'l' } }, .semantic_content = .input, }; } @@ -11577,7 +13590,7 @@ test "PageList highlightSemanticContent output multiline" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11588,7 +13601,7 @@ test "PageList highlightSemanticContent output multiline" { const cell = page.getRowAndCell(x, 6).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11599,7 +13612,7 @@ test "PageList highlightSemanticContent output multiline" { const cell = page.getRowAndCell(x, 7).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11633,7 +13646,7 @@ test "PageList highlightSemanticContent output stops at next prompt" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11648,7 +13661,7 @@ test "PageList highlightSemanticContent output stops at next prompt" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11658,7 +13671,7 @@ test "PageList highlightSemanticContent output stops at next prompt" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'l' }, + .content = .{ .codepoint = .{ .data = 'l' } }, .semantic_content = .input, }; } @@ -11668,7 +13681,7 @@ test "PageList highlightSemanticContent output stops at next prompt" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11679,7 +13692,7 @@ test "PageList highlightSemanticContent output stops at next prompt" { const cell = page.getRowAndCell(x, 6).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11688,7 +13701,7 @@ test "PageList highlightSemanticContent output stops at next prompt" { const cell = page.getRowAndCell(x, 6).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11718,7 +13731,7 @@ test "PageList highlightSemanticContent output to end of screen" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11732,7 +13745,7 @@ test "PageList highlightSemanticContent output to end of screen" { const cell = page.getRowAndCell(x, 15).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11741,7 +13754,7 @@ test "PageList highlightSemanticContent output to end of screen" { const cell = page.getRowAndCell(x, 15).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'c' }, + .content = .{ .codepoint = .{ .data = 'c' } }, .semantic_content = .input, }; } @@ -11750,7 +13763,7 @@ test "PageList highlightSemanticContent output to end of screen" { const cell = page.getRowAndCell(x, 15).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11761,7 +13774,7 @@ test "PageList highlightSemanticContent output to end of screen" { const cell = page.getRowAndCell(x, 16).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11790,7 +13803,7 @@ test "PageList highlightSemanticContent output no output returns null" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11805,7 +13818,7 @@ test "PageList highlightSemanticContent output no output returns null" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11815,7 +13828,7 @@ test "PageList highlightSemanticContent output no output returns null" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'c' }, + .content = .{ .codepoint = .{ .data = 'c' } }, .semantic_content = .input, }; } @@ -11850,7 +13863,7 @@ test "PageList highlightSemanticContent output skips empty cells" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 20, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 20, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -11865,7 +13878,7 @@ test "PageList highlightSemanticContent output skips empty cells" { const cell = page.getRowAndCell(x, 5).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '$' }, + .content = .{ .codepoint = .{ .data = '$' } }, .semantic_content = .prompt, }; } @@ -11879,7 +13892,7 @@ test "PageList highlightSemanticContent output skips empty cells" { const cell = page.getRowAndCell(x, 6).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'l' }, + .content = .{ .codepoint = .{ .data = 'l' } }, .semantic_content = .input, }; } @@ -11893,7 +13906,7 @@ test "PageList highlightSemanticContent output skips empty cells" { const cell = page.getRowAndCell(x, y).cell; cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'o' }, + .content = .{ .codepoint = .{ .data = 'o' } }, .semantic_content = .output, }; } @@ -11927,7 +13940,7 @@ test "PageList erase" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -11961,7 +13974,7 @@ test "PageList erase reaccounts page size" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); const start_size = s.page_size; @@ -11988,7 +14001,7 @@ test "PageList erase row with tracked pin resets to top-left" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up at least 5 pages. @@ -12025,7 +14038,7 @@ test "PageList erase row with tracked pin shifts" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Put a tracked pin in the history @@ -12046,7 +14059,7 @@ test "PageList erase row with tracked pin is erased" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Put a tracked pin in the history @@ -12067,7 +14080,7 @@ test "PageList erase resets viewport to active if moves within active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up at least 5 pages. @@ -12096,7 +14109,7 @@ test "PageList erase resets viewport if inside erased page but not active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up at least 5 pages. @@ -12125,7 +14138,7 @@ test "PageList erase resets viewport to active if top is inside active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow so we take up at least 5 pages. @@ -12153,7 +14166,7 @@ test "PageList erase active regrows automatically" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try testing.expect(s.totalRows() == s.rows); s.eraseActive(10); @@ -12164,7 +14177,7 @@ test "PageList erase a one-row active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 1, null); + var s = try init(alloc, .{ .cols = 10, .rows = 1 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -12174,7 +14187,7 @@ test "PageList erase a one-row active" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } @@ -12184,7 +14197,7 @@ test "PageList erase a one-row active" { // The row should be empty { const get = s.getCell(.{ .active = .{ .x = 0, .y = 0 } }).?; - try testing.expectEqual(@as(u21, 0), get.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), get.cell.content.codepoint.data); } } @@ -12192,7 +14205,7 @@ test "PageList eraseRowBounded less than full row" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 10, null); + var s = try init(alloc, .{ .cols = 80, .rows = 10 }); defer s.deinit(); // Pins @@ -12229,7 +14242,7 @@ test "PageList eraseRowBounded with pin at top" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 10, null); + var s = try init(alloc, .{ .cols = 80, .rows = 10 }); defer s.deinit(); // Pins @@ -12254,7 +14267,7 @@ test "PageList eraseRowBounded full rows single page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 10, null); + var s = try init(alloc, .{ .cols = 80, .rows = 10 }); defer s.deinit(); // Pins @@ -12287,7 +14300,7 @@ test "PageList eraseRowBounded full rows two pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 10, null); + var s = try init(alloc, .{ .cols = 80, .rows = 10 }); defer s.deinit(); // Grow to two pages so our active area straddles @@ -12359,17 +14372,238 @@ test "PageList eraseRowBounded full rows two pages" { try testing.expectEqual(@as(usize, 0), p_out.x); } -test "PageList clone" { +test "PageList eraseRow hyperlink-dense row crosses page boundary" { + // Regression test: when eraseRow shifts rows up across a page + // boundary, the top row of the next page is cloned into the last + // row of the previous page. If the previous page doesn't have + // enough capacity for the managed memory of that row (hyperlinks, + // styles, etc.) the error propagated out AFTER the previous page + // had already been rotated and its tracked pins moved, leaving + // the page list half-mutated. + // + // eraseRow must instead increase the destination page's capacity + // and retry, the same way insertLines/deleteLines and + // cursorScrollAbove handle their cross-page copies. const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 10 }); defer s.deinit(); - try testing.expectEqual(@as(usize, s.rows), s.totalRows()); - var s2 = try s.clone(alloc, .{ - .top = .{ .screen = .{} }, - }); + // Grow to two pages so our active area straddles them: the first + // page is exactly full and the second page holds the last 5 rows + // of the active area. + { + const page = s.pages.last.?.page(); + page.pauseIntegrityChecks(true); + for (0..page.capacity.rows - page.size.rows) |_| _ = try s.grow(); + page.pauseIntegrityChecks(false); + try s.growRows(5); + try testing.expectEqual(@as(usize, 2), s.totalPages()); + try testing.expectEqual(@as(usize, 5), s.pages.last.?.rows()); + } + + // Mark each active row with a codepoint so we can verify the + // shift afterwards. Row y gets codepoint '0' + y at x = 0. + for (0..10) |y| { + const row_pin = s.pin(.{ .active = .{ .y = @intCast(y) } }).?; + row_pin.rowAndCell().cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast('0' + y) } }, + }; + } + + // Fill the top row of the second page (active y=5) with more + // unique hyperlinks ('A' through 'J') than the first page's + // default hyperlink capacity can hold. We must increase the + // second page's capacity to even create such a row; the first + // page keeps its default capacity. + const link_count: usize = 10; + while (s.pages.last.?.page().hyperlink_set.layout.cap <= link_count) { + _ = try s.increaseCapacity(s.pages.last.?, .hyperlink_bytes); + } + try testing.expect(s.pages.first.?.page().hyperlink_set.layout.cap < link_count); + { + const page = s.pages.last.?.page(); + for (0..link_count) |x| { + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{x}); + const id = try page.insertHyperlink(.{ + .id = .{ .implicit = @intCast(x) }, + .uri = uri, + }); + const rac = page.getRowAndCell(x, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast('A' + x) } }, + }; + try page.setHyperlink(rac.row, rac.cell, id); + page.hyperlink_set.use(page.memory, id); + } + } + + // Track a pin in the shifted region of the first page to verify + // it survives the capacity change of its node. + const p = try s.trackPin(s.pin(.{ .active = .{ .x = 3, .y = 1 } }).?); + defer s.untrackPin(p); + + // Erase the first active row. The dense hyperlink row must cross + // the page boundary into the first page, which requires growing + // the first page's hyperlink capacity. + try s.eraseRow(.{ .active = .{ .y = 0 } }); + + // Every remaining row shifted up by one: the '0' marker row was + // erased, the dense row moved up across the page boundary to + // row 4, and the last row was cleared. + const expected = [10]u21{ '1', '2', '3', '4', 'A', '6', '7', '8', '9', 0 }; + for (expected, 0..) |cp, y| { + const list_cell = s.getCell(.{ .active = .{ .y = @intCast(y) } }).?; + try testing.expectEqual(cp, list_cell.cell.content.codepoint.data); + } + + // Every cell of the dense row must still resolve to a real + // hyperlink entry with the correct URI. A half-applied erase + // leaves cells whose hyperlink flag is set but that have no map + // entry, which aborts in clearCells later. + for (0..link_count) |x| { + const list_cell = s.getCell(.{ .active = .{ + .x = @intCast(x), + .y = 4, + } }).?; + try testing.expect(list_cell.cell.hyperlink); + const page: *Page = list_cell.node.page(); + const id = page.lookupHyperlink(list_cell.cell).?; + const link = page.hyperlink_set.get(page.memory, id); + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{x}); + try testing.expectEqualStrings(uri, link.uri.slice(page.memory)); + } + + // All pages must pass integrity checks. + var node_: ?*List.Node = s.pages.first; + while (node_) |node| : (node_ = node.next) node.page().assertIntegrity(); + + // Our tracked pin shifted up by one row and still points into + // the (possibly replaced) first page. + try testing.expectEqual(s.pages.first.?, p.node); + const p_pt = s.pointFromPin(.active, p.*).?.active; + try testing.expectEqual(@as(u32, 3), p_pt.x); + try testing.expectEqual(@as(u32, 0), p_pt.y); +} + +test "PageList eraseRowBounded hyperlink-dense row crosses page boundary" { + // Same as the eraseRow variant above but for eraseRowBounded, + // which has the same rotate-then-clone structure and had the + // same bug: a cross-page row clone failure propagated out after + // the first page had already been rotated. + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 80, .rows = 10 }); + defer s.deinit(); + + // Grow to two pages so our active area straddles them: the first + // page is exactly full and the second page holds the last 5 rows + // of the active area. + { + const page = s.pages.last.?.page(); + page.pauseIntegrityChecks(true); + for (0..page.capacity.rows - page.size.rows) |_| _ = try s.grow(); + page.pauseIntegrityChecks(false); + try s.growRows(5); + try testing.expectEqual(@as(usize, 2), s.totalPages()); + try testing.expectEqual(@as(usize, 5), s.pages.last.?.rows()); + } + + // Mark each active row with a codepoint so we can verify the + // shift afterwards. Row y gets codepoint '0' + y at x = 0. + for (0..10) |y| { + const row_pin = s.pin(.{ .active = .{ .y = @intCast(y) } }).?; + row_pin.rowAndCell().cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast('0' + y) } }, + }; + } + + // Fill the top row of the second page (active y=5) with more + // unique hyperlinks ('A' through 'J') than the first page's + // default hyperlink capacity can hold. We must increase the + // second page's capacity to even create such a row; the first + // page keeps its default capacity. + const link_count: usize = 10; + while (s.pages.last.?.page().hyperlink_set.layout.cap <= link_count) { + _ = try s.increaseCapacity(s.pages.last.?, .hyperlink_bytes); + } + try testing.expect(s.pages.first.?.page().hyperlink_set.layout.cap < link_count); + { + const page = s.pages.last.?.page(); + for (0..link_count) |x| { + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{x}); + const id = try page.insertHyperlink(.{ + .id = .{ .implicit = @intCast(x) }, + .uri = uri, + }); + const rac = page.getRowAndCell(x, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast('A' + x) } }, + }; + try page.setHyperlink(rac.row, rac.cell, id); + page.hyperlink_set.use(page.memory, id); + } + } + + // Erase the first active row with a limit that extends into the + // second page (5 rows remain in the first page, so a limit of 6 + // forces the cross-page path). The dense hyperlink row must cross + // the page boundary into the first page. + try s.eraseRowBounded(.{ .active = .{ .y = 0 } }, 6); + + // Rows within the limit shifted up by one: the '0' marker row was + // erased, the dense row moved up across the page boundary to + // row 4, row 6 is the new blank row, and rows past the limit are + // unchanged. + const expected = [10]u21{ '1', '2', '3', '4', 'A', '6', 0, '7', '8', '9' }; + for (expected, 0..) |cp, y| { + const list_cell = s.getCell(.{ .active = .{ .y = @intCast(y) } }).?; + try testing.expectEqual(cp, list_cell.cell.content.codepoint.data); + } + + // Every cell of the dense row must still resolve to a real + // hyperlink entry with the correct URI. A half-applied erase + // leaves cells whose hyperlink flag is set but that have no map + // entry, which aborts in clearCells later. + for (0..link_count) |x| { + const list_cell = s.getCell(.{ .active = .{ + .x = @intCast(x), + .y = 4, + } }).?; + try testing.expect(list_cell.cell.hyperlink); + const page: *Page = list_cell.node.page(); + const id = page.lookupHyperlink(list_cell.cell).?; + const link = page.hyperlink_set.get(page.memory, id); + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{x}); + try testing.expectEqualStrings(uri, link.uri.slice(page.memory)); + } + + // All pages must pass integrity checks. + var node_: ?*List.Node = s.pages.first; + while (node_) |node| : (node_ = node.next) node.page().assertIntegrity(); +} + +test "PageList clone" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); + defer s.deinit(); + try testing.expectEqual(@as(usize, s.rows), s.totalRows()); + + var s2 = try s.clone(alloc, .{ + .top = .{ .screen = .{} }, + }); defer s2.deinit(); try testing.expectEqual(@as(usize, s.rows), s2.totalRows()); } @@ -12378,7 +14612,7 @@ test "PageList clone partial trimmed right" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 20, null); + var s = try init(alloc, .{ .cols = 80, .rows = 20 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); try s.growRows(30); @@ -12395,7 +14629,7 @@ test "PageList clone partial trimmed left" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 20, null); + var s = try init(alloc, .{ .cols = 80, .rows = 20 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); try s.growRows(30); @@ -12411,7 +14645,7 @@ test "PageList clone partial trimmed left reclaims styles" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 20, null); + var s = try init(alloc, .{ .cols = 80, .rows = 20 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); try s.growRows(30); @@ -12430,7 +14664,7 @@ test "PageList clone partial trimmed left reclaims styles" { rac.row.styled = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, .style_id = style_id, }; page.styles.use(page.memory, style_id); @@ -12460,7 +14694,7 @@ test "PageList clone partial trimmed both" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 20, null); + var s = try init(alloc, .{ .cols = 80, .rows = 20 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); try s.growRows(30); @@ -12477,7 +14711,7 @@ test "PageList clone less than active" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); @@ -12492,7 +14726,7 @@ test "PageList clone remap tracked pin" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); @@ -12520,7 +14754,7 @@ test "PageList clone remap tracked pin not in cloned area" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); @@ -12544,7 +14778,7 @@ test "PageList clone full dirty" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try testing.expectEqual(@as(usize, s.rows), s.totalRows()); @@ -12571,7 +14805,7 @@ test "PageList resize (no reflow) more rows" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 3, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 3), s.totalRows()); @@ -12604,7 +14838,7 @@ test "PageList resize (no reflow) more rows with history" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 3, null); + var s = try init(alloc, .{ .cols = 10, .rows = 3 }); defer s.deinit(); try s.growRows(50); { @@ -12643,7 +14877,7 @@ test "PageList resize (no reflow) less rows" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 10), s.totalRows()); @@ -12656,7 +14890,7 @@ test "PageList resize (no reflow) less rows" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } @@ -12677,7 +14911,7 @@ test "PageList resize (no reflow) one rows" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 10), s.totalRows()); @@ -12690,7 +14924,7 @@ test "PageList resize (no reflow) one rows" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } @@ -12711,7 +14945,7 @@ test "PageList resize (no reflow) less rows cursor on bottom" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 10), s.totalRows()); @@ -12724,7 +14958,7 @@ test "PageList resize (no reflow) less rows cursor on bottom" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -12737,7 +14971,7 @@ test "PageList resize (no reflow) less rows cursor on bottom" { .x = cursor.x, .y = cursor.y, } }).?; - try testing.expectEqual(@as(u21, 9), get.cell.content.codepoint); + try testing.expectEqual(@as(u21, 9), get.cell.content.codepoint.data); } // Resize @@ -12763,7 +14997,7 @@ test "PageList resize (no reflow) less rows cursor in scrollback" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 10), s.totalRows()); @@ -12776,7 +15010,7 @@ test "PageList resize (no reflow) less rows cursor in scrollback" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -12789,7 +15023,7 @@ test "PageList resize (no reflow) less rows cursor in scrollback" { .x = cursor.x, .y = cursor.y, } }).?; - try testing.expectEqual(@as(u21, 2), get.cell.content.codepoint); + try testing.expectEqual(@as(u21, 2), get.cell.content.codepoint.data); } // Resize @@ -12817,7 +15051,7 @@ test "PageList resize (no reflow) less rows trims blank lines" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 5, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -12827,7 +15061,7 @@ test "PageList resize (no reflow) less rows trims blank lines" { const rac = page.getRowAndCell(0, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } @@ -12849,7 +15083,7 @@ test "PageList resize (no reflow) less rows trims blank lines" { .x = cursor.x, .y = cursor.y, } }).?; - try testing.expectEqual(@as(u21, 'A'), get.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'A'), get.cell.content.codepoint.data); } // Resize @@ -12876,7 +15110,7 @@ test "PageList resize (no reflow) less rows trims blank lines cursor in blank li const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 5, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -12886,7 +15120,7 @@ test "PageList resize (no reflow) less rows trims blank lines cursor in blank li const rac = page.getRowAndCell(0, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } @@ -12919,7 +15153,7 @@ test "PageList resize (no reflow) less rows trims blank lines erases pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 100, 5, 0); + var s = try init(alloc, .{ .cols = 100, .rows = 5, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -12936,7 +15170,7 @@ test "PageList resize (no reflow) less rows trims blank lines erases pages" { const rac = page.getRowAndCell(0, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } @@ -12952,7 +15186,7 @@ test "PageList resize (no reflow) more rows extends blank lines" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 3, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -12962,7 +15196,7 @@ test "PageList resize (no reflow) more rows extends blank lines" { const rac = page.getRowAndCell(0, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } @@ -12995,7 +15229,7 @@ test "PageList resize (no reflow) more rows contains viewport" { // When the rows are increased we need to make sure that the viewport // doesn't end up below the active area if it's currently in pin mode. - var s = try init(alloc, 5, 5, 1); + var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_size = 1 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); @@ -13024,7 +15258,7 @@ test "PageList resize (no reflow) less cols" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); // Resize @@ -13044,7 +15278,7 @@ test "PageList resize (no reflow) less cols pin in trimmed cols" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); // Put a tracked pin in the history @@ -13073,7 +15307,7 @@ test "PageList resize (no reflow) less cols clears graphemes" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); // Add a grapheme. @@ -13082,7 +15316,7 @@ test "PageList resize (no reflow) less cols clears graphemes" { const rac = page.getRowAndCell(9, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; try page.appendGrapheme(rac.row, rac.cell, 'A'); } @@ -13103,7 +15337,7 @@ test "PageList resize (no reflow) more cols" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 3, 0); + var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_size = 0 }); defer s.deinit(); // Resize @@ -13123,7 +15357,7 @@ test "PageList resize (no reflow) more cols with spacer head" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 3, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 3, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -13134,14 +15368,14 @@ test "PageList resize (no reflow) more cols with spacer head" { rac.row.wrap = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(1, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_head, }; } @@ -13149,7 +15383,7 @@ test "PageList resize (no reflow) more cols with spacer head" { const rac = page.getRowAndCell(0, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '😀' }, + .content = .{ .codepoint = .{ .data = '😀' } }, .wide = .wide, }; } @@ -13157,7 +15391,7 @@ test "PageList resize (no reflow) more cols with spacer head" { const rac = page.getRowAndCell(1, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_tail, }; } @@ -13174,18 +15408,18 @@ test "PageList resize (no reflow) more cols with spacer head" { { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); // try testing.expect(!rac.row.wrap); } { const rac = page.getRowAndCell(1, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); } { const rac = page.getRowAndCell(2, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); } } @@ -13200,7 +15434,7 @@ test "PageList resize (no reflow) grow cols fast path with spacer head" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 3, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_size = 0 }); defer s.deinit(); // Shrink to 5 cols. The page keeps capacity for 10 cols. @@ -13217,14 +15451,14 @@ test "PageList resize (no reflow) grow cols fast path with spacer head" { const rac = page.getRowAndCell(0, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(4, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_head, }; rac.row.wrap = true; @@ -13235,7 +15469,7 @@ test "PageList resize (no reflow) grow cols fast path with spacer head" { const rac = page.getRowAndCell(4, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_head, }; rac.row.wrap = true; @@ -13279,7 +15513,7 @@ test "PageList resize (no reflow) more cols forces less rows per page" { const cols: size.CellCountInt = 5; const rows: size.CellCountInt = 150; try testing.expect((try std_capacity.adjust(.{ .cols = cols })).rows >= rows); - var s = try init(alloc, cols, rows, 0); + var s = try init(alloc, .{ .cols = cols, .rows = rows, .max_size = 0 }); defer s.deinit(); // Then we need to resize our cols so that our rows per page shrinks. @@ -13344,7 +15578,7 @@ test "PageList resize (no reflow) less cols then more cols" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 3, 0); + var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_size = 0 }); defer s.deinit(); // Resize less @@ -13368,7 +15602,7 @@ test "PageList resize (no reflow) less rows and cols" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); // Resize less @@ -13388,7 +15622,7 @@ test "PageList resize less rows and cols cursor at bottom" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, 0); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = 0 }); defer s.deinit(); const cursor_pin = try s.trackPin(s.pin(.{ .active = .{ @@ -13419,7 +15653,7 @@ test "PageList resize less rows and cols cursor near top pushed to scrollback" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Fill every active row with non-blank content so that shrinking rows @@ -13432,7 +15666,7 @@ test "PageList resize less rows and cols cursor near top pushed to scrollback" { const cells = p.node.page().getCells(rac.row); for (cells, 0..) |*cell, x| cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast('A' + (x % 26)) }, + .content = .{ .codepoint = .{ .data = @intCast('A' + (x % 26)) } }, }; } } @@ -13471,7 +15705,7 @@ test "PageList resize (no reflow) more rows and less cols" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); // Resize less @@ -13492,7 +15726,7 @@ test "PageList resize more rows and cols doesn't fit in single std page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); // Resize to a size that requires more than one page to fit our rows. @@ -13511,7 +15745,7 @@ test "PageList resize (no reflow) empty screen" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 5, 0); + var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_size = 0 }); defer s.deinit(); // Resize @@ -13539,7 +15773,7 @@ test "PageList resize (no reflow) more cols forces smaller cap" { try testing.expect(cap2.rows < cap.rows); // Create initial cap, fits in one page - var s = try init(alloc, cap.cols, cap.rows, null); + var s = try init(alloc, .{ .cols = cap.cols, .rows = cap.rows }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -13548,7 +15782,7 @@ test "PageList resize (no reflow) more cols forces smaller cap" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } } @@ -13564,7 +15798,7 @@ test "PageList resize (no reflow) more cols forces smaller cap" { const rac = offset.rowAndCell(); const cells = offset.node.page().getCells(rac.row); try testing.expectEqual(@as(usize, cap2.cols), cells.len); - try testing.expectEqual(@as(u21, 'A'), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cells[0].content.codepoint.data); } } @@ -13572,7 +15806,7 @@ test "PageList resize (no reflow) more rows adds blank rows if cursor at bottom" const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 3, null); + var s = try init(alloc, .{ .cols = 5, .rows = 3 }); defer s.deinit(); // Grow to 5 total rows, simulating 3 active + 2 scrollback @@ -13583,7 +15817,7 @@ test "PageList resize (no reflow) more rows adds blank rows if cursor at bottom" const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -13605,7 +15839,7 @@ test "PageList resize (no reflow) more rows adds blank rows if cursor at bottom" .x = original_cursor.x, .y = original_cursor.y, } }).?; - try testing.expectEqual(@as(u21, 3), get.cell.content.codepoint); + try testing.expectEqual(@as(u21, 3), get.cell.content.codepoint.data); } // Resize @@ -13637,7 +15871,7 @@ test "PageList resize (no reflow) more rows adds blank rows if cursor at bottom" for (0..3) |y| { const get = s.getCell(.{ .active = .{ .y = @intCast(y) } }).?; const expected: u21 = @intCast(y + 2); - try testing.expectEqual(expected, get.cell.content.codepoint); + try testing.expectEqual(expected, get.cell.content.codepoint.data); } } @@ -13645,7 +15879,7 @@ test "PageList resize reflow more cols no wrapped rows" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 3, 0); + var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -13654,7 +15888,7 @@ test "PageList resize reflow more cols no wrapped rows" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } } @@ -13669,7 +15903,7 @@ test "PageList resize reflow more cols no wrapped rows" { const rac = offset.rowAndCell(); const cells = offset.node.page().getCells(rac.row); try testing.expectEqual(@as(usize, 10), cells.len); - try testing.expectEqual(@as(u21, 'A'), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cells[0].content.codepoint.data); } } @@ -13677,7 +15911,7 @@ test "PageList resize reflow more cols wrapped rows" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 4, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 4, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -13694,7 +15928,7 @@ test "PageList resize reflow more cols wrapped rows" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } } @@ -13721,8 +15955,8 @@ test "PageList resize reflow more cols wrapped rows" { const cells = offset.node.page().getCells(rac.row); try testing.expect(!rac.row.wrap); try testing.expectEqual(@as(usize, 4), cells.len); - try testing.expectEqual(@as(u21, 'A'), cells[0].content.codepoint); - try testing.expectEqual(@as(u21, 'A'), cells[2].content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cells[0].content.codepoint.data); + try testing.expectEqual(@as(u21, 'A'), cells[2].content.codepoint.data); } } @@ -13730,7 +15964,7 @@ test "PageList resize reflow invalidates viewport offset cache" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 4, null); + var s = try init(alloc, .{ .cols = 2, .rows = 4 }); defer s.deinit(); try s.growRows(20); @@ -13748,7 +15982,7 @@ test "PageList resize reflow invalidates viewport offset cache" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } } @@ -13790,7 +16024,7 @@ test "PageList resize reflow more cols creates multiple pages" { unreachable; }; - var s = try init(alloc, cap.cols, cap.rows, null); + var s = try init(alloc, .{ .cols = cap.cols, .rows = cap.rows }); defer s.deinit(); // Wrap every other row so every line is wrapped for reflow @@ -13809,7 +16043,7 @@ test "PageList resize reflow more cols creates multiple pages" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } } @@ -13843,7 +16077,7 @@ test "PageList resize reflow more cols wrap across page boundary" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 10, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -13873,7 +16107,7 @@ test "PageList resize reflow more cols wrap across page boundary" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -13888,7 +16122,7 @@ test "PageList resize reflow more cols wrap across page boundary" { const rac = page2.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -13963,10 +16197,10 @@ test "PageList resize reflow more cols wrap across page boundary" { try testing.expect(!row.wrap_continuation); const cells = p.cells(.all); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); - try testing.expectEqual(@as(u21, 1), cells[1].content.codepoint); - try testing.expectEqual(@as(u21, 0), cells[2].content.codepoint); - try testing.expectEqual(@as(u21, 1), cells[3].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); + try testing.expectEqual(@as(u21, 1), cells[1].content.codepoint.data); + try testing.expectEqual(@as(u21, 0), cells[2].content.codepoint.data); + try testing.expectEqual(@as(u21, 1), cells[3].content.codepoint.data); } } @@ -13974,7 +16208,7 @@ test "PageList resize reflow more cols wrap across page boundary cursor in secon const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 10, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -14004,7 +16238,7 @@ test "PageList resize reflow more cols wrap across page boundary cursor in secon const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14019,7 +16253,7 @@ test "PageList resize reflow more cols wrap across page boundary cursor in secon const rac = page2.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14049,10 +16283,10 @@ test "PageList resize reflow more cols wrap across page boundary cursor in secon try testing.expect(!row.wrap); const cells = p2.cells(.all); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); - try testing.expectEqual(@as(u21, 1), cells[1].content.codepoint); - try testing.expectEqual(@as(u21, 0), cells[2].content.codepoint); - try testing.expectEqual(@as(u21, 1), cells[3].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); + try testing.expectEqual(@as(u21, 1), cells[1].content.codepoint.data); + try testing.expectEqual(@as(u21, 0), cells[2].content.codepoint.data); + try testing.expectEqual(@as(u21, 1), cells[3].content.codepoint.data); } } @@ -14060,7 +16294,7 @@ test "PageList resize reflow less cols wrap across page boundary cursor in secon const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 10, null); + var s = try init(alloc, .{ .cols = 5, .rows = 10 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -14090,7 +16324,7 @@ test "PageList resize reflow less cols wrap across page boundary cursor in secon const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14105,7 +16339,7 @@ test "PageList resize reflow less cols wrap across page boundary cursor in secon const rac = page2.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14191,10 +16425,10 @@ test "PageList resize reflow less cols wrap across page boundary cursor in secon try testing.expect(!row.wrap_continuation); const cells = p2.cells(.all); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); - try testing.expectEqual(@as(u21, 1), cells[1].content.codepoint); - try testing.expectEqual(@as(u21, 2), cells[2].content.codepoint); - try testing.expectEqual(@as(u21, 3), cells[3].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); + try testing.expectEqual(@as(u21, 1), cells[1].content.codepoint.data); + try testing.expectEqual(@as(u21, 2), cells[2].content.codepoint.data); + try testing.expectEqual(@as(u21, 3), cells[3].content.codepoint.data); } { // PAGE 0 ROW 7897, ACTIVE 5 @@ -14204,10 +16438,10 @@ test "PageList resize reflow less cols wrap across page boundary cursor in secon try testing.expect(row.wrap_continuation); const cells = p2.cells(.all); - try testing.expectEqual(@as(u21, 4), cells[0].content.codepoint); - try testing.expectEqual(@as(u21, 0), cells[1].content.codepoint); - try testing.expectEqual(@as(u21, 1), cells[2].content.codepoint); - try testing.expectEqual(@as(u21, 2), cells[3].content.codepoint); + try testing.expectEqual(@as(u21, 4), cells[0].content.codepoint.data); + try testing.expectEqual(@as(u21, 0), cells[1].content.codepoint.data); + try testing.expectEqual(@as(u21, 1), cells[2].content.codepoint.data); + try testing.expectEqual(@as(u21, 2), cells[3].content.codepoint.data); } { // PAGE 0 ROW 7898, ACTIVE 6 @@ -14217,8 +16451,8 @@ test "PageList resize reflow less cols wrap across page boundary cursor in secon try testing.expect(row.wrap_continuation); const cells = p2.cells(.all); - try testing.expectEqual(@as(u21, 3), cells[0].content.codepoint); - try testing.expectEqual(@as(u21, 4), cells[1].content.codepoint); + try testing.expectEqual(@as(u21, 3), cells[0].content.codepoint.data); + try testing.expectEqual(@as(u21, 4), cells[1].content.codepoint.data); } { // PAGE 0 ROW 7899, ACTIVE 7 @@ -14239,7 +16473,7 @@ test "PageList resize reflow more cols cursor in wrapped row" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 4, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 4, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -14252,7 +16486,7 @@ test "PageList resize reflow more cols cursor in wrapped row" { const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14265,7 +16499,7 @@ test "PageList resize reflow more cols cursor in wrapped row" { const rac = page.getRowAndCell(x, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14290,7 +16524,7 @@ test "PageList resize reflow more cols cursor in not wrapped row" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 4, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 4, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -14303,7 +16537,7 @@ test "PageList resize reflow more cols cursor in not wrapped row" { const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14316,7 +16550,7 @@ test "PageList resize reflow more cols cursor in not wrapped row" { const rac = page.getRowAndCell(x, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14341,7 +16575,7 @@ test "PageList resize reflow more cols cursor in wrapped row that isn't unwrappe const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 4, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 4, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -14354,7 +16588,7 @@ test "PageList resize reflow more cols cursor in wrapped row that isn't unwrappe const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14368,7 +16602,7 @@ test "PageList resize reflow more cols cursor in wrapped row that isn't unwrappe const rac = page.getRowAndCell(x, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14381,7 +16615,7 @@ test "PageList resize reflow more cols cursor in wrapped row that isn't unwrappe const rac = page.getRowAndCell(x, 2); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -14406,7 +16640,7 @@ test "PageList resize reflow more cols no reflow preserves semantic prompt" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 4, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 4, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -14432,7 +16666,7 @@ test "PageList resize reflow exceeds hyperlink memory forcing capacity increase" const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 10, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -14482,7 +16716,7 @@ test "PageList resize reflow exceeds hyperlink memory forcing capacity increase" rac.row.wrap = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'X' }, + .content = .{ .codepoint = .{ .data = 'X' } }, }; try page.setHyperlink(rac.row, rac.cell, id); try std.testing.expectError( @@ -14506,7 +16740,7 @@ test "PageList resize reflow exceeds hyperlink memory forcing capacity increase" rac.row.wrap_continuation = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'X' }, + .content = .{ .codepoint = .{ .data = 'X' } }, }; try page.setHyperlink(rac.row, rac.cell, id); try std.testing.expectError( @@ -14522,11 +16756,11 @@ test "PageList resize reflow exceeds hyperlink memory forcing capacity increase" try s.resize(.{ .cols = s.cols + 1, .reflow = true }); } -test "PageList resize reflow exceeds grapheme memory forcing capacity increase" { +test "PageList resize reflow hyperlink dupe string alloc chunk rounding" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 10, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -14548,44 +16782,172 @@ test "PageList resize reflow exceeds grapheme memory forcing capacity increase" try std.testing.expectEqual(s.pages.last.?, s.pages.first.?.next); } - // We use almost all grapheme alloc capacity with a grapheme in the final - // row of the first page, and do the same on the first row of the second - // page. We also mark the row as wrapped so that when we resize with more - // cols the row unwraps and we have a single row that requires almost two - // times the base grapheme alloc capacity. + // The string allocator hands out 32-byte chunks and every allocation + // is rounded up to the chunk size independently. Duping a hyperlink + // during reflow allocates the URI and the explicit ID separately, so + // two separate allocations can require one more chunk than a single + // combined allocation of the same total byte length. // - // This forces the reflow to increase capacity. + // We arrange for the reflow target page to have exactly two free + // chunks (64 bytes) remaining when a hyperlink with a 33-byte URI + // (2 chunks) and a 31-byte explicit ID (1 chunk) is reflowed into + // it. The combined byte length (64 bytes -> 2 chunks) fits, but the + // separate allocations (3 chunks) do not, so the reflow must grow + // the string capacity rather than panic or drop the hyperlink. + // + // The two hyperlinked cells are joined as a single wrapped row so + // that they are always reflowed into the same target page. // // +--+ = PAGE 0 // : : - // | X… <- where X is a grapheme which uses almost all the capacity. + // | A… <- A is hyperlinked with all but 64 bytes of string cap. // +--+ // +--+ = PAGE 1 - // …X | <- X here also almost hits grapheme cap. + // …B | <- B is hyperlinked with a 33-byte URI and 31-byte ID. // +--+ - // Almost hit grapheme alloc cap in bottom right of first page. - // Mark the final row as wrapped. + const uri_a = "a" ** (pagepkg.string_bytes_default - 64); + const uri_b = "b" ** 33; + const id_b = "i" ** 31; + + // Hyperlink A in the bottom right of the first page. Mark the final + // row as wrapped. { const page = s.pages.first.?.page(); + const id = try page.insertHyperlink(.{ + .id = .{ .implicit = 0 }, + .uri = uri_a, + }); const rac = page.getRowAndCell(page.size.cols - 1, page.size.rows - 1); rac.row.wrap = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'X' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; - try page.setGraphemes( - rac.row, - rac.cell, - &@as( - [ - @divFloor( - pagepkg.grapheme_bytes_default - 1, - @sizeOf(u21), - ) - ]u21, - @splat('a'), - ), + try page.setHyperlink(rac.row, rac.cell, id); + + // Sanity check the chunk math: the remaining 64 bytes fit as a + // single allocation but not as the two separate allocations that + // inserting (or duping) hyperlink B performs. + const buf = try page.string_alloc.alloc(u8, page.memory, 64); + page.string_alloc.free(page.memory, buf); + try std.testing.expectError( + error.StringsOutOfMemory, + page.insertHyperlink(.{ + .id = .{ .explicit = id_b }, + .uri = uri_b, + }), + ); + } + + // Hyperlink B in the top left of the second page. Mark the first + // row as a wrap continuation. + { + const page = s.pages.last.?.page(); + const id = try page.insertHyperlink(.{ + .id = .{ .explicit = id_b }, + .uri = uri_b, + }); + const rac = page.getRowAndCell(0, 0); + rac.row.wrap_continuation = true; + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 'B' } }, + }; + try page.setHyperlink(rac.row, rac.cell, id); + } + + // Resize to 1 column wider, unwrapping the row. + try s.resize(.{ .cols = s.cols + 1, .reflow = true }); + + // Both hyperlinks must have survived the reflow intact. + var found: usize = 0; + var node_it = s.pages.first; + while (node_it) |node| : (node_it = node.next) { + const page = node.page(); + for (0..page.size.rows) |y| { + for (0..page.size.cols) |x| { + const rac = page.getRowAndCell(x, y); + if (!rac.cell.hyperlink) continue; + found += 1; + + const link_id = page.lookupHyperlink(rac.cell).?; + const entry = page.hyperlink_set.get(page.memory, link_id); + const uri = entry.uri.slice(page.memory); + switch (entry.id) { + .implicit => try testing.expectEqualStrings(uri_a, uri), + .explicit => |slice| { + try testing.expectEqualStrings(uri_b, uri); + try testing.expectEqualStrings( + id_b, + slice.slice(page.memory), + ); + }, + } + } + } + } + try testing.expectEqual(@as(usize, 2), found); +} + +test "PageList resize reflow exceeds grapheme memory forcing capacity increase" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 4, .rows = 10, .max_size = 0 }); + defer s.deinit(); + try testing.expectEqual(@as(usize, 1), s.totalPages()); + + // Grow to the capacity of the first page and add + // one more row so that we have two pages total. + { + const page = s.pages.first.?.page(); + page.pauseIntegrityChecks(true); + for (page.size.rows..page.capacity.rows) |_| { + _ = try s.grow(); + } + page.pauseIntegrityChecks(false); + try testing.expectEqual(@as(usize, 1), s.totalPages()); + try s.growRows(1); + try testing.expectEqual(@as(usize, 2), s.totalPages()); + + // We now have two pages. + try std.testing.expect(s.pages.first.? != s.pages.last.?); + try std.testing.expectEqual(s.pages.last.?, s.pages.first.?.next); + } + + // We use all grapheme alloc capacity with four maximum-sized graphemes on + // each page. The two rows form one wrapped logical line across the page + // boundary, so resizing wider moves all eight graphemes into one page and + // requires almost two times the base grapheme alloc capacity. + // + // This forces the reflow to increase capacity. + // + // +----+ = PAGE 0 + // : : + // |XXXX| <- four capped graphemes in one wrapped row. + // +----+ + // +----+ = PAGE 1 + // |XXXX| <- four more capped graphemes continue the logical line. + // +----+ + + const suffixes: [pagepkg.grapheme_max_len]u21 = @splat('a'); + + // Fill the final row of the first page and mark it as wrapped. + { + const page = s.pages.first.?.page(); + const y = page.size.rows - 1; + const row = page.getRow(y); + row.wrap = true; + + for (0..page.size.cols) |x| { + const rac = page.getRowAndCell(x, y); + rac.cell.* = .init('X'); + try page.setGraphemes(rac.row, rac.cell, &suffixes); + } + try std.testing.expectEqual( + page.grapheme_alloc.capacityBytes(), + page.grapheme_alloc.usedBytes(page.memory), ); try std.testing.expectError( error.OutOfMemory, @@ -14597,29 +16959,17 @@ test "PageList resize reflow exceeds grapheme memory forcing capacity increase" ); } - // Almost hit grapheme alloc cap in top left of second page. - // Mark the first row as a wrap continuation. + // Fill the first row of the second page and mark it as a continuation. { const page = s.pages.last.?.page(); - const rac = page.getRowAndCell(0, 0); - rac.row.wrap = true; - rac.cell.* = .{ - .content_tag = .codepoint, - .content = .{ .codepoint = 'X' }, - }; - try page.setGraphemes( - rac.row, - rac.cell, - &@as( - [ - @divFloor( - pagepkg.grapheme_bytes_default - 1, - @sizeOf(u21), - ) - ]u21, - @splat('a'), - ), - ); + const row = page.getRow(0); + row.wrap_continuation = true; + + for (0..page.size.cols) |x| { + const rac = page.getRowAndCell(x, 0); + rac.cell.* = .init('X'); + try page.setGraphemes(rac.row, rac.cell, &suffixes); + } try std.testing.expectError( error.OutOfMemory, page.grapheme_alloc.alloc( @@ -14638,7 +16988,7 @@ test "PageList resize reflow exceeds style memory forcing capacity increase" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, pagepkg.std_capacity.styles - 1, 10, 0); + var s = try init(alloc, .{ .cols = pagepkg.std_capacity.styles - 1, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -14681,7 +17031,7 @@ test "PageList resize reflow exceeds style memory forcing capacity increase" { rac.row.styled = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'X' }, + .content = .{ .codepoint = .{ .data = 'X' } }, .style_id = id, }; } @@ -14708,7 +17058,7 @@ test "PageList resize reflow exceeds style memory forcing capacity increase" { rac.row.styled = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'X' }, + .content = .{ .codepoint = .{ .data = 'X' } }, .style_id = id, }; } @@ -14722,7 +17072,7 @@ test "PageList resize reflow more cols unwrap wide spacer head" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -14733,14 +17083,14 @@ test "PageList resize reflow more cols unwrap wide spacer head" { rac.row.wrap = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(1, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_head, }; } @@ -14749,7 +17099,7 @@ test "PageList resize reflow more cols unwrap wide spacer head" { rac.row.wrap_continuation = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '😀' }, + .content = .{ .codepoint = .{ .data = '😀' } }, .wide = .wide, }; } @@ -14757,7 +17107,7 @@ test "PageList resize reflow more cols unwrap wide spacer head" { const rac = page.getRowAndCell(1, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_tail, }; } @@ -14774,18 +17124,18 @@ test "PageList resize reflow more cols unwrap wide spacer head" { { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); try testing.expect(!rac.row.wrap); } { const rac = page.getRowAndCell(1, 0); - try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); } { const rac = page.getRowAndCell(2, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); } } @@ -14795,7 +17145,7 @@ test "PageList resize reflow more cols unwrap wide spacer head across two rows" const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 3, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 3, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -14806,14 +17156,14 @@ test "PageList resize reflow more cols unwrap wide spacer head across two rows" rac.row.wrap = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(1, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { @@ -14822,14 +17172,14 @@ test "PageList resize reflow more cols unwrap wide spacer head across two rows" rac.row.wrap = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(1, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_head, }; } @@ -14838,7 +17188,7 @@ test "PageList resize reflow more cols unwrap wide spacer head across two rows" rac.row.wrap_continuation = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '😀' }, + .content = .{ .codepoint = .{ .data = '😀' } }, .wide = .wide, }; } @@ -14846,7 +17196,7 @@ test "PageList resize reflow more cols unwrap wide spacer head across two rows" const rac = page.getRowAndCell(1, 2); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_tail, }; } @@ -14863,33 +17213,33 @@ test "PageList resize reflow more cols unwrap wide spacer head across two rows" { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); try testing.expect(rac.row.wrap); } { const rac = page.getRowAndCell(1, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); } { const rac = page.getRowAndCell(2, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); } { const rac = page.getRowAndCell(3, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_head, rac.cell.wide); } { const rac = page.getRowAndCell(0, 1); - try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); } { const rac = page.getRowAndCell(1, 1); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); } } @@ -14899,7 +17249,7 @@ test "PageList resize reflow more cols unwrap still requires wide spacer head" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 2, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -14910,14 +17260,14 @@ test "PageList resize reflow more cols unwrap still requires wide spacer head" { rac.row.wrap = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(1, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { @@ -14925,7 +17275,7 @@ test "PageList resize reflow more cols unwrap still requires wide spacer head" { rac.row.wrap_continuation = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '😀' }, + .content = .{ .codepoint = .{ .data = '😀' } }, .wide = .wide, }; } @@ -14933,7 +17283,7 @@ test "PageList resize reflow more cols unwrap still requires wide spacer head" { const rac = page.getRowAndCell(1, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_tail, }; } @@ -14950,28 +17300,28 @@ test "PageList resize reflow more cols unwrap still requires wide spacer head" { { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); try testing.expect(rac.row.wrap); } { const rac = page.getRowAndCell(1, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); } { const rac = page.getRowAndCell(2, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_head, rac.cell.wide); } { const rac = page.getRowAndCell(0, 1); - try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); } { const rac = page.getRowAndCell(1, 1); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); } } @@ -14980,7 +17330,7 @@ test "PageList resize reflow less cols no reflow preserves semantic prompt" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 4, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 4, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -14993,7 +17343,7 @@ test "PageList resize reflow less cols no reflow preserves semantic prompt" { const rac = page.getRowAndCell(x, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15023,7 +17373,7 @@ test "PageList resize reflow less cols no reflow preserves semantic prompt on fi const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 4, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 4, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -15049,7 +17399,7 @@ test "PageList resize reflow less cols wrap preserves semantic prompt" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 4, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 4, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -15075,7 +17425,7 @@ test "PageList resize reflow less cols no wrapped rows" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 3, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15086,7 +17436,7 @@ test "PageList resize reflow less cols no wrapped rows" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15104,7 +17454,7 @@ test "PageList resize reflow less cols no wrapped rows" { const rac = offset_copy.rowAndCell(); const cells = offset.node.page().getCells(rac.row); try testing.expectEqual(@as(usize, 5), cells.len); - try testing.expectEqual(@as(u21, @intCast(x)), cells[x].content.codepoint); + try testing.expectEqual(@as(u21, @intCast(x)), cells[x].content.codepoint.data); } } } @@ -15113,7 +17463,7 @@ test "PageList resize reflow less cols wrapped rows" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, null); + var s = try init(alloc, .{ .cols = 4, .rows = 2 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15122,7 +17472,7 @@ test "PageList resize reflow less cols wrapped rows" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15149,7 +17499,7 @@ test "PageList resize reflow less cols wrapped rows" { const cells = offset.node.page().getCells(rac.row); try testing.expect(rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); } { const offset = it.next().?; @@ -15157,7 +17507,7 @@ test "PageList resize reflow less cols wrapped rows" { const cells = offset.node.page().getCells(rac.row); try testing.expect(!rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint.data); } { // First row should be wrapped @@ -15166,7 +17516,7 @@ test "PageList resize reflow less cols wrapped rows" { const cells = offset.node.page().getCells(rac.row); try testing.expect(rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); } { const offset = it.next().?; @@ -15174,7 +17524,7 @@ test "PageList resize reflow less cols wrapped rows" { const cells = offset.node.page().getCells(rac.row); try testing.expect(!rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint.data); } } @@ -15182,7 +17532,7 @@ test "PageList resize reflow less cols wrapped rows with graphemes" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, null); + var s = try init(alloc, .{ .cols = 4, .rows = 2 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -15192,7 +17542,7 @@ test "PageList resize reflow less cols wrapped rows with graphemes" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } @@ -15225,7 +17575,7 @@ test "PageList resize reflow less cols wrapped rows with graphemes" { const cells = offset.node.page().getCells(rac.row); try testing.expect(rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); } { const offset = it.next().?; @@ -15234,7 +17584,7 @@ test "PageList resize reflow less cols wrapped rows with graphemes" { try testing.expect(!rac.row.wrap); try testing.expect(rac.row.grapheme); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint.data); const cps = page.lookupGrapheme(rac.cell).?; try testing.expectEqual(@as(usize, 1), cps.len); @@ -15247,7 +17597,7 @@ test "PageList resize reflow less cols wrapped rows with graphemes" { const cells = offset.node.page().getCells(rac.row); try testing.expect(rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); } { const offset = it.next().?; @@ -15256,7 +17606,7 @@ test "PageList resize reflow less cols wrapped rows with graphemes" { try testing.expect(!rac.row.wrap); try testing.expect(rac.row.grapheme); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint.data); const cps = page.lookupGrapheme(rac.cell).?; try testing.expectEqual(@as(usize, 1), cps.len); @@ -15268,7 +17618,7 @@ test "PageList resize reflow less cols cursor in wrapped row" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, null); + var s = try init(alloc, .{ .cols = 4, .rows = 2 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15277,7 +17627,7 @@ test "PageList resize reflow less cols cursor in wrapped row" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15302,7 +17652,7 @@ test "PageList resize reflow less cols wraps spacer head" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 3, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 3, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -15313,28 +17663,28 @@ test "PageList resize reflow less cols wraps spacer head" { rac.row.wrap = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(1, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(2, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(3, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_head, }; } @@ -15343,7 +17693,7 @@ test "PageList resize reflow less cols wraps spacer head" { rac.row.wrap_continuation = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '😀' }, + .content = .{ .codepoint = .{ .data = '😀' } }, .wide = .wide, }; } @@ -15351,7 +17701,7 @@ test "PageList resize reflow less cols wraps spacer head" { const rac = page.getRowAndCell(1, 1); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_tail, }; } @@ -15368,28 +17718,28 @@ test "PageList resize reflow less cols wraps spacer head" { { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); try testing.expect(rac.row.wrap); } { const rac = page.getRowAndCell(1, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); } { const rac = page.getRowAndCell(2, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); } { const rac = page.getRowAndCell(0, 1); - try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); } { const rac = page.getRowAndCell(1, 1); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); } } @@ -15398,7 +17748,7 @@ test "PageList resize reflow less cols cursor goes to scrollback" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, null); + var s = try init(alloc, .{ .cols = 4, .rows = 2 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15407,7 +17757,7 @@ test "PageList resize reflow less cols cursor goes to scrollback" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15429,7 +17779,7 @@ test "PageList resize reflow less cols cursor in unchanged row" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, null); + var s = try init(alloc, .{ .cols = 4, .rows = 2 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15438,7 +17788,7 @@ test "PageList resize reflow less cols cursor in unchanged row" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15463,7 +17813,7 @@ test "PageList resize reflow less cols cursor in blank cell" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 6, 2, null); + var s = try init(alloc, .{ .cols = 6, .rows = 2 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15472,7 +17822,7 @@ test "PageList resize reflow less cols cursor in blank cell" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15497,7 +17847,7 @@ test "PageList resize reflow less cols cursor in final blank cell" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 6, 2, null); + var s = try init(alloc, .{ .cols = 6, .rows = 2 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15506,7 +17856,7 @@ test "PageList resize reflow less cols cursor in final blank cell" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15531,7 +17881,7 @@ test "PageList resize reflow less cols cursor in wrapped blank cell" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 6, 2, null); + var s = try init(alloc, .{ .cols = 6, .rows = 2 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15540,7 +17890,7 @@ test "PageList resize reflow less cols cursor in wrapped blank cell" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15565,7 +17915,7 @@ test "PageList resize reflow less cols blank lines" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 3, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 3, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15574,7 +17924,7 @@ test "PageList resize reflow less cols blank lines" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15592,7 +17942,7 @@ test "PageList resize reflow less cols blank lines" { const cells = offset.node.page().getCells(rac.row); try testing.expect(rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); } { const offset = it.next().?; @@ -15600,7 +17950,7 @@ test "PageList resize reflow less cols blank lines" { const cells = offset.node.page().getCells(rac.row); try testing.expect(!rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint.data); } } @@ -15608,7 +17958,7 @@ test "PageList resize reflow less cols blank lines between" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 3, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 3, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15617,7 +17967,7 @@ test "PageList resize reflow less cols blank lines between" { const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15626,7 +17976,7 @@ test "PageList resize reflow less cols blank lines between" { const rac = page.getRowAndCell(x, 2); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15648,7 +17998,7 @@ test "PageList resize reflow less cols blank lines between" { const cells = offset.node.page().getCells(rac.row); try testing.expect(rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); } { const offset = it.next().?; @@ -15656,7 +18006,7 @@ test "PageList resize reflow less cols blank lines between" { const cells = offset.node.page().getCells(rac.row); try testing.expect(!rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 2), cells[0].content.codepoint.data); } } @@ -15664,7 +18014,7 @@ test "PageList resize reflow less cols blank lines between no scrollback" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 3, 0); + var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_size = 0 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15672,14 +18022,14 @@ test "PageList resize reflow less cols blank lines between no scrollback" { const rac = page.getRowAndCell(0, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } { const rac = page.getRowAndCell(0, 2); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'C' }, + .content = .{ .codepoint = .{ .data = 'C' } }, }; } @@ -15695,13 +18045,13 @@ test "PageList resize reflow less cols blank lines between no scrollback" { const cells = offset.node.page().getCells(rac.row); try testing.expect(!rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 'A'), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cells[0].content.codepoint.data); } { const offset = it.next().?; const rac = offset.rowAndCell(); const cells = offset.node.page().getCells(rac.row); - try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 0), cells[0].content.codepoint.data); } { const offset = it.next().?; @@ -15709,7 +18059,7 @@ test "PageList resize reflow less cols blank lines between no scrollback" { const cells = offset.node.page().getCells(rac.row); try testing.expect(!rac.row.wrap); try testing.expectEqual(@as(usize, 2), cells.len); - try testing.expectEqual(@as(u21, 'C'), cells[0].content.codepoint); + try testing.expectEqual(@as(u21, 'C'), cells[0].content.codepoint.data); } } @@ -15717,7 +18067,7 @@ test "PageList resize reflow less cols cursor not on last line preserves locatio const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 5, 5, 1); + var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_size = 1 }); defer s.deinit(); try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); @@ -15726,7 +18076,7 @@ test "PageList resize reflow less cols cursor not on last line preserves locatio const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, }; } } @@ -15761,7 +18111,7 @@ test "PageList resize reflow less cols copy style" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 2, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -15775,7 +18125,7 @@ test "PageList resize reflow less cols copy style" { const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x) }, + .content = .{ .codepoint = .{ .data = @intCast(x) } }, .style_id = style_id, }; page.styles.use(page.memory, style_id); @@ -15815,7 +18165,7 @@ test "PageList resize reflow less cols to eliminate a wide char" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 1, 0); + var s = try init(alloc, .{ .cols = 2, .rows = 1, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -15825,7 +18175,7 @@ test "PageList resize reflow less cols to eliminate a wide char" { const rac = page.getRowAndCell(0, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '😀' }, + .content = .{ .codepoint = .{ .data = '😀' } }, .wide = .wide, }; } @@ -15833,7 +18183,7 @@ test "PageList resize reflow less cols to eliminate a wide char" { const rac = page.getRowAndCell(1, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_tail, }; } @@ -15850,7 +18200,7 @@ test "PageList resize reflow less cols to eliminate a wide char" { { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); } } @@ -15860,7 +18210,7 @@ test "PageList resize reflow less cols to wrap a wide char" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 3, 1, 0); + var s = try init(alloc, .{ .cols = 3, .rows = 1, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -15870,14 +18220,14 @@ test "PageList resize reflow less cols to wrap a wide char" { const rac = page.getRowAndCell(0, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'x' }, + .content = .{ .codepoint = .{ .data = 'x' } }, }; } { const rac = page.getRowAndCell(1, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = '😀' }, + .content = .{ .codepoint = .{ .data = '😀' } }, .wide = .wide, }; } @@ -15885,7 +18235,7 @@ test "PageList resize reflow less cols to wrap a wide char" { const rac = page.getRowAndCell(2, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_tail, }; } @@ -15902,128 +18252,492 @@ test "PageList resize reflow less cols to wrap a wide char" { { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); try testing.expect(rac.row.wrap); } { const rac = page.getRowAndCell(1, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_head, rac.cell.wide); } { const rac = page.getRowAndCell(0, 1); - try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); } { const rac = page.getRowAndCell(1, 1); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); } } } -test "PageList resize reflow less cols to wrap a multi-codepoint grapheme with a spacer head" { +test "PageList resize reflow less cols wide char bulk run" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, 0); + var s = try init(alloc, .{ .cols = 8, .rows = 1, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); - // We want to make the screen look like this: - // - // 👨‍👨‍👦‍👦👨‍👨‍👦‍👦 - - // First family emoji at (0, 0) - { - const rac = page.getRowAndCell(0, 0); - rac.cell.* = .{ - .content_tag = .codepoint, - .content = .{ .codepoint = 0x1F468 }, // First codepoint of the grapheme - .wide = .wide, - }; - try page.setGraphemes(rac.row, rac.cell, &.{ - 0x200D, 0x1F468, - 0x200D, 0x1F466, - 0x200D, 0x1F466, - }); - } - { - const rac = page.getRowAndCell(1, 0); - rac.cell.* = .{ - .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, - .wide = .spacer_tail, - }; - } - // Second family emoji at (2, 0) - { - const rac = page.getRowAndCell(2, 0); - rac.cell.* = .{ - .content_tag = .codepoint, - .content = .{ .codepoint = 0x1F468 }, // First codepoint of the grapheme - .wide = .wide, - }; - try page.setGraphemes(rac.row, rac.cell, &.{ - 0x200D, 0x1F468, - 0x200D, 0x1F466, - 0x200D, 0x1F466, - }); - } - { - const rac = page.getRowAndCell(3, 0); - rac.cell.* = .{ - .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, - .wide = .spacer_tail, - }; + // A full row of wide character pairs so the reflow takes + // the bulk run path. + for (0..4) |i| { + { + const rac = page.getRowAndCell(i * 2, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast(0x4E00 + i) } }, + .wide = .wide, + }; + } + { + const rac = page.getRowAndCell(i * 2 + 1, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0 } }, + .wide = .spacer_tail, + }; + } } } - // Resize - try s.resize(.{ .cols = 3, .reflow = true }); - try testing.expectEqual(@as(usize, 3), s.cols); + // Resize to exactly two pairs per row: runs end on the row + // boundary with no spacer heads needed. + try s.resize(.{ .cols = 4, .reflow = true }); + try testing.expectEqual(@as(usize, 4), s.cols); try testing.expectEqual(@as(usize, 2), s.totalRows()); { try testing.expect(s.pages.first == s.pages.last); const page = s.pages.first.?.page(); + for (0..4) |i| { + const y = i / 2; + const x = (i % 2) * 2; + { + const rac = page.getRowAndCell(x, y); + try testing.expectEqual( + @as(u21, @intCast(0x4E00 + i)), + rac.cell.content.codepoint.data, + ); + try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); + } + { + const rac = page.getRowAndCell(x + 1, y); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); + try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); + } + } + { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 0x1F468), rac.cell.content.codepoint); - try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); - - const cps = page.lookupGrapheme(rac.cell).?; - try testing.expectEqual(@as(usize, 6), cps.len); - try testing.expectEqual(@as(u21, 0x200D), cps[0]); - try testing.expectEqual(@as(u21, 0x1F468), cps[1]); - try testing.expectEqual(@as(u21, 0x200D), cps[2]); - try testing.expectEqual(@as(u21, 0x1F466), cps[3]); - try testing.expectEqual(@as(u21, 0x200D), cps[4]); - try testing.expectEqual(@as(u21, 0x1F466), cps[5]); - - // Row should be wrapped try testing.expect(rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); } { - const rac = page.getRowAndCell(1, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + const rac = page.getRowAndCell(0, 1); + try testing.expect(!rac.row.wrap); + try testing.expect(rac.row.wrap_continuation); + } + } +} + +test "PageList resize reflow less cols wide char bulk run odd cols spacer head" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 8, .rows = 1, .max_size = 0 }); + defer s.deinit(); + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + for (0..4) |i| { + { + const rac = page.getRowAndCell(i * 2, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast(0x4E00 + i) } }, + .wide = .wide, + }; + } + { + const rac = page.getRowAndCell(i * 2 + 1, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0 } }, + .wide = .spacer_tail, + }; + } + } + } + + // Resize to an odd number of columns: the bulk run must stop a + // pair short of the row boundary and the slow path inserts a + // spacer head in the final column. + try s.resize(.{ .cols = 5, .reflow = true }); + try testing.expectEqual(@as(usize, 5), s.cols); + try testing.expectEqual(@as(usize, 2), s.totalRows()); + + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + for (0..4) |i| { + const y = i / 2; + const x = (i % 2) * 2; + { + const rac = page.getRowAndCell(x, y); + try testing.expectEqual( + @as(u21, @intCast(0x4E00 + i)), + rac.cell.content.codepoint.data, + ); + try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); + } + { + const rac = page.getRowAndCell(x + 1, y); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); + try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); + } + } + + { + const rac = page.getRowAndCell(4, 0); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); + try testing.expectEqual(pagepkg.Cell.Wide.spacer_head, rac.cell.wide); + try testing.expect(rac.row.wrap); + } + { + const rac = page.getRowAndCell(4, 1); + try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); + try testing.expect(rac.row.wrap_continuation); + } + } +} + +test "PageList resize reflow less cols wide char bulk run mixed narrow round trip" { + const testing = std.testing; + const alloc = testing.allocator; + + // The emoji-in-prose shape: single wide pairs separated by + // narrow cells, which must all share a single bulk run. + const Shape = struct { cp: u21, wide: pagepkg.Cell.Wide }; + const shape: []const Shape = &.{ + .{ .cp = 0x4E00, .wide = .wide }, + .{ .cp = 0, .wide = .spacer_tail }, + .{ .cp = 'x', .wide = .narrow }, + .{ .cp = 0x4E01, .wide = .wide }, + .{ .cp = 0, .wide = .spacer_tail }, + .{ .cp = 'y', .wide = .narrow }, + .{ .cp = 0x4E02, .wide = .wide }, + .{ .cp = 0, .wide = .spacer_tail }, + .{ .cp = 'z', .wide = .narrow }, + }; + + var s = try init(alloc, .{ .cols = 9, .rows = 1, .max_size = 0 }); + defer s.deinit(); + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + for (shape, 0..) |c, x| { + const rac = page.getRowAndCell(x, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = c.cp } }, + .wide = c.wide, + }; + } + } + + // Shrink: the run ends exactly on the row boundary after the + // second narrow cell. + try s.resize(.{ .cols = 6, .reflow = true }); + try testing.expectEqual(@as(usize, 6), s.cols); + try testing.expectEqual(@as(usize, 2), s.totalRows()); + + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + for (shape[0..6], 0..) |c, x| { + const rac = page.getRowAndCell(x, 0); + try testing.expectEqual(c.cp, rac.cell.content.codepoint.data); + try testing.expectEqual(c.wide, rac.cell.wide); + } + for (shape[6..], 0..) |c, x| { + const rac = page.getRowAndCell(x, 1); + try testing.expectEqual(c.cp, rac.cell.content.codepoint.data); + try testing.expectEqual(c.wide, rac.cell.wide); + } + try testing.expect(page.getRowAndCell(0, 0).row.wrap); + try testing.expect(page.getRowAndCell(0, 1).row.wrap_continuation); + } + + // Grow back: the wrapped rows must rejoin into the original + // single-row layout. + try s.resize(.{ .cols = 9, .reflow = true }); + try testing.expectEqual(@as(usize, 9), s.cols); + try testing.expectEqual(@as(usize, 1), s.totalRows()); + + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + for (shape, 0..) |c, x| { + const rac = page.getRowAndCell(x, 0); + try testing.expectEqual(c.cp, rac.cell.content.codepoint.data); + try testing.expectEqual(c.wide, rac.cell.wide); + } + try testing.expect(!page.getRowAndCell(0, 0).row.wrap); + } +} + +test "PageList resize reflow less cols wide char bulk run styled" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 8, .rows = 1, .max_size = 0 }); + defer s.deinit(); + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + // Create a style + const style: stylepkg.Style = .{ .flags = .{ .bold = true } }; + const style_id = try page.styles.add(page.memory, style); + + // Styled pairs: the tail shares the wide cell's style, as + // the print path writes them. + for (0..4) |i| { + { + const rac = page.getRowAndCell(i * 2, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = @intCast(0x4E00 + i) } }, + .wide = .wide, + .style_id = style_id, + }; + page.styles.use(page.memory, style_id); + } + { + const rac = page.getRowAndCell(i * 2 + 1, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0 } }, + .wide = .spacer_tail, + .style_id = style_id, + }; + page.styles.use(page.memory, style_id); + } + } + + // We're over-counted by 1 because `add` implies `use`. + page.styles.release(page.memory, style_id); + } + + // Resize + try s.resize(.{ .cols = 4, .reflow = true }); + try testing.expectEqual(@as(usize, 4), s.cols); + try testing.expectEqual(@as(usize, 2), s.totalRows()); + + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + for (0..2) |y| { + for (0..4) |x| { + const rac = page.getRowAndCell(x, y); + const style_id = rac.cell.style_id; + try testing.expect(style_id != 0); + + const style = page.styles.get(page.memory, style_id); + try testing.expect(style.flags.bold); + try testing.expect(rac.row.styled); + try testing.expectEqual( + if (x % 2 == 0) pagepkg.Cell.Wide.wide else pagepkg.Cell.Wide.spacer_tail, + rac.cell.wide, + ); + } + } + } +} + +test "PageList resize reflow less cols wide char bulk run degenerate spacer tail style" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 4, .rows = 1, .max_size = 0 }); + defer s.deinit(); + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + // A styled wide cell whose tail does NOT share its style. + // This can't come from the print path, but the run scan must + // reject the pair (slow path) rather than rewrite the tail + // to the wide cell's style. + const style: stylepkg.Style = .{ .flags = .{ .bold = true } }; + const style_id = try page.styles.add(page.memory, style); + + { + const rac = page.getRowAndCell(0, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0x4E00 } }, + .wide = .wide, + .style_id = style_id, + }; + } + { + const rac = page.getRowAndCell(1, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0 } }, + .wide = .spacer_tail, + }; + } + { + const rac = page.getRowAndCell(2, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 'x' } }, + }; + } + } + + // Resize + try s.resize(.{ .cols = 3, .reflow = true }); + try testing.expectEqual(@as(usize, 3), s.cols); + try testing.expectEqual(@as(usize, 1), s.totalRows()); + + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + { + const rac = page.getRowAndCell(0, 0); + try testing.expectEqual(@as(u21, 0x4E00), rac.cell.content.codepoint.data); + try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); + try testing.expect(rac.cell.style_id != 0); + const style = page.styles.get(page.memory, rac.cell.style_id); + try testing.expect(style.flags.bold); + } + { + const rac = page.getRowAndCell(1, 0); try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); + try testing.expectEqual(stylepkg.default_id, rac.cell.style_id); } { const rac = page.getRowAndCell(2, 0); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), rac.cell.content.codepoint.data); + try testing.expectEqual(pagepkg.Cell.Wide.narrow, rac.cell.wide); + } + } +} + +test "PageList resize reflow less cols to wrap a multi-codepoint grapheme with a spacer head" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 4, .rows = 2, .max_size = 0 }); + defer s.deinit(); + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + // We want to make the screen look like this: + // + // 👨‍👨‍👦‍👦👨‍👨‍👦‍👦 + + // First family emoji at (0, 0) + { + const rac = page.getRowAndCell(0, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0x1F468 } }, // First codepoint of the grapheme + .wide = .wide, + }; + try page.setGraphemes(rac.row, rac.cell, &.{ + 0x200D, 0x1F468, + 0x200D, 0x1F466, + 0x200D, 0x1F466, + }); + } + { + const rac = page.getRowAndCell(1, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0 } }, + .wide = .spacer_tail, + }; + } + // Second family emoji at (2, 0) + { + const rac = page.getRowAndCell(2, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0x1F468 } }, // First codepoint of the grapheme + .wide = .wide, + }; + try page.setGraphemes(rac.row, rac.cell, &.{ + 0x200D, 0x1F468, + 0x200D, 0x1F466, + 0x200D, 0x1F466, + }); + } + { + const rac = page.getRowAndCell(3, 0); + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = .{ .data = 0 } }, + .wide = .spacer_tail, + }; + } + } + + // Resize + try s.resize(.{ .cols = 3, .reflow = true }); + try testing.expectEqual(@as(usize, 3), s.cols); + try testing.expectEqual(@as(usize, 2), s.totalRows()); + + { + try testing.expect(s.pages.first == s.pages.last); + const page = s.pages.first.?.page(); + + { + const rac = page.getRowAndCell(0, 0); + try testing.expectEqual(@as(u21, 0x1F468), rac.cell.content.codepoint.data); + try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); + + const cps = page.lookupGrapheme(rac.cell).?; + try testing.expectEqual(@as(usize, 6), cps.len); + try testing.expectEqual(@as(u21, 0x200D), cps[0]); + try testing.expectEqual(@as(u21, 0x1F468), cps[1]); + try testing.expectEqual(@as(u21, 0x200D), cps[2]); + try testing.expectEqual(@as(u21, 0x1F466), cps[3]); + try testing.expectEqual(@as(u21, 0x200D), cps[4]); + try testing.expectEqual(@as(u21, 0x1F466), cps[5]); + + // Row should be wrapped + try testing.expect(rac.row.wrap); + } + { + const rac = page.getRowAndCell(1, 0); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); + try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); + } + { + const rac = page.getRowAndCell(2, 0); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_head, rac.cell.wide); } { const rac = page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 0x1F468), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F468), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.wide, rac.cell.wide); const cps = page.lookupGrapheme(rac.cell).?; @@ -16037,7 +18751,7 @@ test "PageList resize reflow less cols to wrap a multi-codepoint grapheme with a } { const rac = page.getRowAndCell(1, 1); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); try testing.expectEqual(pagepkg.Cell.Wide.spacer_tail, rac.cell.wide); } } @@ -16049,7 +18763,7 @@ test "PageList resize reflow less cols copy kitty placeholder" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 2, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -16061,7 +18775,7 @@ test "PageList resize reflow less cols copy kitty placeholder" { rac.row.kitty_virtual_placeholder = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = kitty.graphics.unicode.placeholder }, + .content = .{ .codepoint = .{ .data = kitty.graphics.unicode.placeholder } }, }; } } @@ -16090,7 +18804,7 @@ test "PageList resize reflow more cols clears kitty placeholder" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 2, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -16102,7 +18816,7 @@ test "PageList resize reflow more cols clears kitty placeholder" { rac.row.kitty_virtual_placeholder = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = kitty.graphics.unicode.placeholder }, + .content = .{ .codepoint = .{ .data = kitty.graphics.unicode.placeholder } }, }; } } @@ -16133,7 +18847,7 @@ test "PageList resize reflow wrap moves kitty placeholder" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 2, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 2, .max_size = 0 }); defer s.deinit(); { try testing.expect(s.pages.first == s.pages.last); @@ -16145,7 +18859,7 @@ test "PageList resize reflow wrap moves kitty placeholder" { rac.row.kitty_virtual_placeholder = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = kitty.graphics.unicode.placeholder }, + .content = .{ .codepoint = .{ .data = kitty.graphics.unicode.placeholder } }, }; } } @@ -16172,7 +18886,7 @@ test "PageList reset" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); s.reset(); try testing.expect(s.viewport == .active); @@ -16191,7 +18905,7 @@ test "PageList reset invalidates stale untracked refs even if node memory is reu const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); var stale_nodes: [page_preheat * 4]*List.Node = undefined; @@ -16251,7 +18965,7 @@ test "PageList reset across two pages" { }; // Init - var s = try init(alloc, cap.cols, rows, null); + var s = try init(alloc, .{ .cols = cap.cols, .rows = rows }); defer s.deinit(); s.reset(); try testing.expect(s.viewport == .active); @@ -16263,7 +18977,7 @@ test "PageList reset moves tracked pins and marks them as garbage" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Create a tracked pin into the active area @@ -16286,7 +19000,7 @@ test "PageList clears history" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); try s.growRows(30); s.reset(); @@ -16308,7 +19022,7 @@ test "PageList resize reflow grapheme map capacity exceeded" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 4, 10, 0); + var s = try init(alloc, .{ .cols = 4, .rows = 10, .max_size = 0 }); defer s.deinit(); try testing.expectEqual(@as(usize, 1), s.totalPages()); @@ -16354,7 +19068,7 @@ test "PageList resize reflow grapheme map capacity exceeded" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; try page.appendGrapheme(rac.row, rac.cell, @as(u21, @intCast(0x0301))); } @@ -16368,7 +19082,7 @@ test "PageList resize reflow grapheme map capacity exceeded" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'B' }, + .content = .{ .codepoint = .{ .data = 'B' } }, }; try page.appendGrapheme(rac.row, rac.cell, @as(u21, @intCast(0x0302))); } @@ -16395,7 +19109,7 @@ test "PageList resize grow cols with unwrap fixes viewport pin" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 2, 10, null); + var s = try init(alloc, .{ .cols = 2, .rows = 10 }); defer s.deinit(); // Make sure we have some history, in this case we have 30 rows of history @@ -16416,7 +19130,7 @@ test "PageList resize grow cols with unwrap fixes viewport pin" { for (0..s.cols) |x| { page.getRowAndCell(x, y).cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = .{ .data = 'A' } }, }; } } @@ -16445,7 +19159,7 @@ test "PageList grow reuses non-standard page without leak" { // Create a PageList with 3 * std_size max so we can fit multiple pages // but will still trigger reuse. - var s = try init(alloc, 80, 24, 3 * std_size); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = 3 * std_size }); defer s.deinit(); // Increase the first page capacity to make it non-standard (larger than std_size). @@ -16467,7 +19181,7 @@ test "PageList grow reuses non-standard page without leak" { try testing.expect(s.pages.first != s.pages.last); // Continue growing until we exceed max_size AND the last page is full - while (s.page_size + PagePool.item_size <= s.maxSize() or + while (s.page_size + PagePool.item_size <= s.limits.max(.bytes) or s.pages.last.?.rows() < s.pages.last.?.capacity().rows) { _ = try s.grow(); @@ -16528,7 +19242,7 @@ test "PageList grow non-standard page prune protection" { // This is kind of magic and likely depends on std_size. const rows_count = 600; - var s = try init(alloc, 80, rows_count, std_size); + var s = try init(alloc, .{ .cols = 80, .rows = rows_count, .max_size = std_size }); defer s.deinit(); // Make the first page non-standard @@ -16567,7 +19281,9 @@ test "PageList grow non-standard page prune protection" { // Verify prune path conditions are met try testing.expect(s.pages.first != s.pages.last); - try testing.expect(s.page_size + PagePool.item_size > s.maxSize()); + try testing.expect( + s.page_size + PagePool.item_size > s.limits.max(.bytes), + ); try testing.expect(s.totalRows() >= s.rows); // Verify last page is at capacity (so grow must prune or allocate new) @@ -16591,7 +19307,7 @@ test "PageList resize (no reflow) more cols remaps pins in backfill path" { const cols: size.CellCountInt = 5; const cap = try std_capacity.adjust(.{ .cols = cols }); - var s = try init(alloc, cols, cap.rows, null); + var s = try init(alloc, .{ .cols = cols, .rows = cap.rows }); defer s.deinit(); // Grow until we have two pages. @@ -16622,7 +19338,7 @@ test "PageList resize (no reflow) more cols remaps pins in backfill path" { const marker: u21 = 'X'; tracked.rowAndCell().cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = marker }, + .content = .{ .codepoint = .{ .data = marker } }, }; try s.resize(.{ .cols = new_cols, .reflow = false }); @@ -16642,14 +19358,14 @@ test "PageList resize (no reflow) more cols remaps pins in backfill path" { // Verify the pin still points to the cell with our marker content. const cell = tracked.rowAndCell().cell; try testing.expectEqual(.codepoint, cell.content_tag); - try testing.expectEqual(marker, cell.content.codepoint); + try testing.expectEqual(marker, cell.content.codepoint.data); } test "PageList compact pool page produces exact-size heap page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, 0); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = 0 }); defer s.deinit(); // A freshly created page is pool-owned at std_size. @@ -16678,7 +19394,7 @@ test "PageList compact then grow allocates new page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Compact the only page. It now has no spare row capacity. @@ -16697,7 +19413,7 @@ test "PageList compact then reset frees heap pages" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, 0); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = 0 }); defer s.deinit(); // Compact the only page so the list contains a sub-std_size @@ -16717,7 +19433,7 @@ test "PageList compact then clone" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Write a marker so we can verify contents survive. @@ -16726,7 +19442,7 @@ test "PageList compact then clone" { const rac = node.page().getRowAndCell(1, 2); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'X' }, + .content = .{ .codepoint = .{ .data = 'X' } }, }; } @@ -16745,7 +19461,7 @@ test "PageList compact then clone" { { const node2 = s2.pages.first.?; const rac = node2.page().getRowAndCell(1, 2); - try testing.expectEqual(@as(u21, 'X'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'X'), rac.cell.content.codepoint.data); } } @@ -16753,7 +19469,7 @@ test "PageList compact oversized page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Grow until we have multiple pages @@ -16776,7 +19492,7 @@ test "PageList compact oversized page" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + y * s.cols) }, + .content = .{ .codepoint = .{ .data = @intCast(x + y * s.cols) } }, }; } } @@ -16830,7 +19546,7 @@ test "PageList compact oversized page" { const rac = page.getRowAndCell(x, y); try testing.expectEqual( @as(u21, @intCast(x + y * s.cols)), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, ); } } @@ -16840,7 +19556,7 @@ test "PageList destroyed pool page reuse is zeroed" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, null); + var s = try init(alloc, .{ .cols = 80, .rows = 24 }); defer s.deinit(); // Create a page and scribble over its entire backing memory, @@ -16872,7 +19588,7 @@ test "PageList increaseCapacity from zero-capacity dimensions" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, 0); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = 0 }); defer s.deinit(); // Compact the only page. A plain page has no styled, grapheme, @@ -16906,7 +19622,7 @@ test "PageList compact after increaseCapacity" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 80, 24, 0); + var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_size = 0 }); defer s.deinit(); var node = s.pages.first.?; @@ -16926,7 +19642,7 @@ test "PageList split at middle row" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const page = s.pages.first.?.page(); @@ -16936,7 +19652,7 @@ test "PageList split at middle row" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -16959,13 +19675,13 @@ test "PageList split at middle row" { // Verify content in first page is preserved (rows 0-4 have codepoints 0-4) for (0..5) |y| { const rac = first_page.getRowAndCell(0, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } // Verify content in second page (original rows 5-9, now at y=0-4) for (0..5) |y| { const rac = second_page.getRowAndCell(0, y); - try testing.expectEqual(@as(u21, @intCast(y + 5)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y + 5)), rac.cell.content.codepoint.data); } } @@ -16973,7 +19689,7 @@ test "PageList split at row 0 is no-op" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const page = s.pages.first.?.page(); @@ -16983,7 +19699,7 @@ test "PageList split at row 0 is no-op" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -16999,7 +19715,7 @@ test "PageList split at row 0 is no-op" { try testing.expectEqual(@as(usize, 10), page.size.rows); for (0..10) |y| { const rac = page.getRowAndCell(0, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } } @@ -17007,7 +19723,7 @@ test "PageList split at last row" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const page = s.pages.first.?.page(); @@ -17017,7 +19733,7 @@ test "PageList split at last row" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -17039,7 +19755,7 @@ test "PageList split at last row" { // Verify content in second page (original row 9, now at y=0) const rac = second_page.getRowAndCell(0, 0); - try testing.expectEqual(@as(u21, 9), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 9), rac.cell.content.codepoint.data); } test "PageList split single row page returns OutOfSpace" { @@ -17047,7 +19763,7 @@ test "PageList split single row page returns OutOfSpace" { const alloc = testing.allocator; // Initialize with 1 row - var s = try init(alloc, 10, 1, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 1, .max_size = 0 }); defer s.deinit(); const split_pin: Pin = .{ .node = s.pages.first.?, .y = 0, .x = 0 }; @@ -17060,7 +19776,7 @@ test "PageList split moves tracked pins" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); // Track a pin at row 7 @@ -17083,7 +19799,7 @@ test "PageList split tracked pin before split point unchanged" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const original_node = s.pages.first.?; @@ -17107,7 +19823,7 @@ test "PageList split tracked pin at split point moves to new page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const original_node = s.pages.first.?; @@ -17132,7 +19848,7 @@ test "PageList split multiple tracked pins across regions" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const original_node = s.pages.first.?; @@ -17178,7 +19894,7 @@ test "PageList split tracked viewport_pin in split region moves correctly" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const original_node = s.pages.first.?; @@ -17205,7 +19921,7 @@ test "PageList split middle page preserves linked list order" { const alloc = testing.allocator; // Create a single page with 12 rows - var s = try init(alloc, 10, 12, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 12, .max_size = 0 }); defer s.deinit(); // Split at row 4 to create: page1 (rows 0-3), page2 (rows 4-11) @@ -17254,7 +19970,7 @@ test "PageList split last page makes new page the last" { const alloc = testing.allocator; // Create a single page with 10 rows - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); // Split to create 2 pages first @@ -17285,7 +20001,7 @@ test "PageList split first page keeps original as first" { const alloc = testing.allocator; // Create 2 pages by splitting - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const original_first = s.pages.first.?; @@ -17318,7 +20034,7 @@ test "PageList split preserves wrap flags" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const page = s.pages.first.?.page(); @@ -17372,7 +20088,7 @@ test "PageList split preserves styled cells" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const page = s.pages.first.?.page(); @@ -17385,7 +20101,7 @@ test "PageList split preserves styled cells" { const rac = page.getRowAndCell(0, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'S' }, + .content = .{ .codepoint = .{ .data = 'S' } }, .style_id = style_id, }; rac.row.styled = true; @@ -17410,7 +20126,7 @@ test "PageList split preserves styled cells" { // Verify styled cells are preserved in new page for (0..3) |y| { const rac = second_page.getRowAndCell(0, y); - try testing.expectEqual(@as(u21, 'S'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'S'), rac.cell.content.codepoint.data); try testing.expect(rac.cell.style_id != 0); const got_style = second_page.styles.get(second_page.memory, rac.cell.style_id); @@ -17423,7 +20139,7 @@ test "PageList split preserves grapheme clusters" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const page = s.pages.first.?.page(); @@ -17433,7 +20149,7 @@ test "PageList split preserves grapheme clusters" { const rac = page.getRowAndCell(0, 6); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0x1F468 }, // Man emoji + .content = .{ .codepoint = .{ .data = 0x1F468 } }, // Man emoji }; try page.setGraphemes(rac.row, rac.cell, &.{ 0x200D, // ZWJ @@ -17457,7 +20173,7 @@ test "PageList split preserves grapheme clusters" { // Verify grapheme is preserved in new page (original row 6 is now row 1) { const rac = second_page.getRowAndCell(0, 1); - try testing.expectEqual(@as(u21, 0x1F468), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F468), rac.cell.content.codepoint.data); try testing.expect(rac.row.grapheme); const cps = second_page.lookupGrapheme(rac.cell).?; @@ -17471,7 +20187,7 @@ test "PageList split preserves hyperlinks" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 10, 10, 0); + var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_size = 0 }); defer s.deinit(); const page = s.pages.first.?.page(); @@ -17485,7 +20201,7 @@ test "PageList split preserves hyperlinks" { const rac = page.getRowAndCell(0, 7); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'L' }, + .content = .{ .codepoint = .{ .data = 'L' } }, }; try page.setHyperlink(rac.row, rac.cell, hyperlink_id); } @@ -17506,7 +20222,7 @@ test "PageList split preserves hyperlinks" { // Verify hyperlink is preserved in new page (original row 7 is now row 2) { const rac = second_page.getRowAndCell(0, 2); - try testing.expectEqual(@as(u21, 'L'), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 'L'), rac.cell.content.codepoint.data); try testing.expect(rac.cell.hyperlink); const link_id = second_page.lookupHyperlink(rac.cell).?; @@ -17514,3 +20230,230 @@ test "PageList split preserves hyperlinks" { try testing.expectEqualStrings("https://example.com", link.uri.slice(second_page.memory)); } } + +test "PageList eraseRow recycled row has default metadata" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 5, .rows = 3 }); + defer s.deinit(); + + // Simulate the top row being part of a soft-wrapped, prompt-marked + // line. Erasing it recycles its Row storage as the new blank + // bottom row, which must not retain any of this metadata. + { + const rac = s.getCell(.{ .active = .{} }).?; + rac.row.wrap = true; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt; + } + + try s.eraseRow(.{ .active = .{} }); + + { + const rac = s.getCell(.{ .active = .{ .y = 2 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "PageList eraseRowBounded recycled row has default metadata" { + const testing = std.testing; + const alloc = testing.allocator; + + // A limit smaller than the remaining rows in the page exercises + // the bounded-rotate branch; a larger limit exercises the fallback + // branch that clears the final row after a full rotation. + for ([_]usize{ 1, 10 }) |limit| { + var s = try init(alloc, .{ .cols = 5, .rows = 3 }); + defer s.deinit(); + + { + const rac = s.getCell(.{ .active = .{} }).?; + rac.row.wrap = true; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt; + } + + try s.eraseRowBounded(.{ .active = .{} }, limit); + + const recycled_y = @min(limit, 2); + const rac = s.getCell(.{ .active = .{ .y = @intCast(recycled_y) } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "PageList eraseActive regrown rows have default metadata" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 5, .rows = 3 }); + defer s.deinit(); + + // Mark the rows that will be erased. eraseActive retires their + // storage into unused page capacity and then regrows the active + // area, re-exposing the same Row storage via the grow() fast path. + for (0..2) |y| { + const rac = s.getCell(.{ .active = .{ .y = @intCast(y) } }).?; + rac.row.wrap = true; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt; + } + + s.eraseActive(1); + + for (0..3) |y| { + const rac = s.getCell(.{ .active = .{ .y = @intCast(y) } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "PageList split retired rows have default state" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 5, .rows = 10 }); + defer s.deinit(); + + // Put metadata and a background-colored (non-zero, but text-free) + // cell on a row that the split will move to the new page. The + // retired Row storage on the source page goes back into unused + // capacity that grow() re-exposes without clearing. + { + const rac = s.getCell(.{ .active = .{ .y = 7 } }).?; + rac.row.wrap = true; + rac.row.semantic_prompt = .prompt; + rac.cell.* = .{ + .content_tag = .bg_color_palette, + .content = .{ .color_palette = .{ .data = 42 } }, + }; + } + + const node = s.pages.first.?; + try s.split(s.pin(.{ .active = .{ .y = 5 } }).?); + + // The source page was truncated to 5 rows; peek at the retired + // storage beyond size.rows. + const page = node.page(); + try testing.expectEqual(@as(usize, 5), page.size.rows); + const rows = page.rows.ptr(page.memory.ptr); + for (5..10) |y| { + const row = rows[y]; + try testing.expect(!row.wrap); + try testing.expect(!row.wrap_continuation); + try testing.expectEqual(.none, row.semantic_prompt); + const cells = row.cells.ptr(page.memory.ptr)[0..page.size.cols]; + for (cells) |cell| try testing.expect(cell.isZero()); + } +} + +test "PageList resize trimmed rows have default state" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, .{ .cols = 5, .rows = 5 }); + defer s.deinit(); + + // A trailing blank row has no text, so shrinking rows trims it, + // but it can still carry metadata (e.g. a blank prompt + // continuation line) and background-colored cells. Trimming + // retires the storage into unused capacity that grow() + // re-exposes without clearing. + { + const rac = s.getCell(.{ .active = .{ .y = 4 } }).?; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt_continuation; + rac.cell.* = .{ + .content_tag = .bg_color_palette, + .content = .{ .color_palette = .{ .data = 42 } }, + }; + } + + try s.resize(.{ .rows = 4, .reflow = false }); + try s.resize(.{ .rows = 5, .reflow = false }); + + { + const rac = s.getCell(.{ .active = .{ .y = 4 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + try testing.expect(rac.cell.isZero()); + } +} + +test "PageList memory pool never touches idle page memory" { + const testing = std.testing; + const preheat = page_preheat; + + // Back the page allocator with memory we can inspect. + const backing = try testing.allocator.alignedAlloc( + u8, + .fromByteUnits(std.heap.page_size_min), + preheat * std_size, + ); + defer testing.allocator.free(backing); + var fba: std.heap.FixedBufferAllocator = .init(backing); + + var pool: MemoryPool = try .init(testing.allocator, fba.allocator(), preheat); + defer pool.deinit(); + + // Preheat allocated exactly the items. + try testing.expectEqual(preheat * std_size, fba.end_index); + + // Lay the sentinel down after preheat: allocation itself may write + // (the Allocator interface fills fresh memory with undefined in + // safe builds, which valgrind also tracks). The sentinel must differ + // from Zig's 0xAA undefined pattern so that any write is visible. + const sentinel: u8 = 0x5A; + @memset(backing, sentinel); + + // Every preheated item is handed out untouched and without going + // back to the page allocator, and destroying it doesn't touch it. + var items: [preheat]PagePool.ItemPtr = undefined; + for (&items) |*item| { + item.* = try pool.pages.create(); + try testing.expectEqual(preheat * std_size, fba.end_index); + try testing.expect(std.mem.allEqual(u8, item.*, sentinel)); + } + for (items) |item| pool.pages.destroy(item); + try testing.expect(std.mem.allEqual(u8, backing, sentinel)); +} + +test "PageList memory pool fast path does not allocate" { + const testing = std.testing; + var counting: std.testing.FailingAllocator = .init(testing.allocator, .{}); + + var pool: MemoryPool = try .init( + testing.allocator, + counting.allocator(), + page_preheat, + ); + defer pool.deinit(); + try testing.expectEqual(page_preheat, counting.allocations); + + // Cycle a few thousand pages through the preheated items. As long + // as no more than the preheat are live at once, create is a + // free-list pop and never touches the page allocator. + var items: [page_preheat]PagePool.ItemPtr = undefined; + for (0..1024) |_| { + for (&items) |*item| item.* = try pool.pages.create(); + for (items) |item| pool.pages.destroy(item); + } + try testing.expectEqual(page_preheat, counting.allocations); + try testing.expectEqual(0, counting.deallocations); + + // Going past the preheat allocates the extra items once; they are + // recycled from then on. + var extra: [page_preheat + 2]PagePool.ItemPtr = undefined; + for (0..1024) |_| { + for (&extra) |*item| item.* = try pool.pages.create(); + for (extra) |item| pool.pages.destroy(item); + } + try testing.expectEqual(extra.len, counting.allocations); + try testing.expectEqual(0, counting.deallocations); +} diff --git a/vendor/libghostty-vt/src/terminal/Parser.zig b/vendor/libghostty-vt/src/terminal/Parser.zig index 88e7edf7..34e7e30f 100644 --- a/vendor/libghostty-vt/src/terminal/Parser.zig +++ b/vendor/libghostty-vt/src/terminal/Parser.zig @@ -166,12 +166,18 @@ pub const Action = union(enum) { // of invisible characters we don't want to handle right // now. - // All others do the default behavior + // All others do the default behavior. Note the max + // depth of 1: this is only used for logging so we + // only want the top-level fields. Larger depths + // instantiate a recursive formatter for every nested + // type of every action payload (e.g. the full + // osc.Command union), which costs tens of kilobytes + // of binary size. else => try writer.printValue( "any", .{}, @field(self, u_field.name), - 3, + 1, ), } } @@ -386,10 +392,7 @@ inline fn doAction(self: *Parser, action: TransitionAction, c: u8) ?Action { // We only allow colon or mixed separators for the 'm' command. if (c != 'm' and self.params_sep.count() > 0) { @branchHint(.cold); - log.warn( - "CSI colon or mixed separators only allowed for 'm' command, got: {f}", - .{result}, - ); + warnCsiSepMismatch(result.csi_dispatch); break :csi_dispatch null; } @@ -406,6 +409,20 @@ inline fn doAction(self: *Parser, action: TransitionAction, c: u8) ?Action { }; } +/// Log a warning for a CSI dispatch with colon/mixed separators on a +/// non-'m' command. +/// +/// This is noinline on purpose so that this unlikely (cold) behavior +/// doesn't bloat the hot dispatch path which has been measured to actually +/// affect both binary size and performance due to icache busts. +noinline fn warnCsiSepMismatch(csi: Action.CSI) void { + @branchHint(.cold); + log.warn( + "CSI colon or mixed separators only allowed for 'm' command, got: {f}", + .{csi}, + ); +} + pub inline fn clear(self: *Parser) void { self.intermediates_idx = 0; self.params_idx = 0; diff --git a/vendor/libghostty-vt/src/terminal/Screen.zig b/vendor/libghostty-vt/src/terminal/Screen.zig index 6c4db323..208e276a 100644 --- a/vendor/libghostty-vt/src/terminal/Screen.zig +++ b/vendor/libghostty-vt/src/terminal/Screen.zig @@ -8,6 +8,7 @@ const ansi = @import("ansi.zig"); const charsets = @import("charsets.zig"); const fastmem = @import("../fastmem.zig"); const kitty = @import("kitty.zig"); +const lib = @import("lib.zig"); const sgr = @import("sgr.zig"); const tripwire = @import("../tripwire.zig"); const unicode = @import("../unicode/main.zig"); @@ -16,6 +17,7 @@ const PageList = @import("PageList.zig"); const selection_codepoints = @import("selection_codepoints.zig"); const StringMap = @import("StringMap.zig"); const ScreenFormatter = @import("formatter.zig").ScreenFormatter; +const PinMap = @import("formatter.zig").PinMap; const osc = @import("osc.zig"); const pagepkg = @import("page.zig"); const point = @import("point.zig"); @@ -32,6 +34,9 @@ pub const CursorStyle = @import("cursor.zig").Style; const log = std.log.scoped(.screen); +/// The I/O implementation to use for any operation that requires one. +io: std.Io, + /// The general purpose allocator to use for all memory allocations. /// Unfortunately some screen operations do require allocation. alloc: Allocator, @@ -111,7 +116,12 @@ pub const SemanticPrompt = struct { .click = .none, }; - pub const SemanticClick = union(enum) { + pub const SemanticClickKind = lib.Enum( + lib.target, + &.{ "none", "click_events", "cl" }, + ); + + pub const SemanticClick = union(SemanticClickKind) { none, click_events: osc.semantic_prompt.ClickEvents, cl: osc.semantic_prompt.Click, @@ -250,9 +260,15 @@ pub const Options = struct { cols: size.CellCountInt, rows: size.CellCountInt, - /// The maximum size of scrollback in bytes. Zero means unlimited. Any - /// other value will be clamped to support a minimum of the active area. - max_scrollback: usize = 0, + /// The maximum size of scrollback in bytes. Null is unlimited, zero + /// disables scrollback, and any other value is clamped to support a + /// minimum of the active area. + max_scrollback_bytes: ?usize = 0, + + /// The maximum number of physical scrollback rows, excluding the active + /// area. Null is unlimited. The effective limit permits at least one + /// standard page and only complete historical pages are pruned. + max_scrollback_lines: ?usize = null, /// The total storage limit for Kitty images in bytes for this /// screen. Kitty image storage is per-screen. @@ -273,28 +289,29 @@ pub const Options = struct { pub const default: Options = .{ .cols = 80, .rows = 24, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }; }; /// Initialize a new screen. /// -/// max_scrollback is the amount of scrollback to keep in bytes. This +/// max_scrollback_bytes is the amount of scrollback to keep in bytes. This /// will be rounded UP to the nearest page size because our minimum allocation /// size is that anyways. /// /// If max scrollback is 0, then no scrollback is kept at all. pub fn init( + io: std.Io, alloc: Allocator, opts: Options, ) Allocator.Error!Screen { // Initialize our backing pages. - var pages = try PageList.init( - alloc, - opts.cols, - opts.rows, - opts.max_scrollback, - ); + var pages = try PageList.init(alloc, .{ + .cols = opts.cols, + .rows = opts.rows, + .max_size = opts.max_scrollback_bytes, + .max_lines = opts.max_scrollback_lines, + }); errdefer pages.deinit(); // Create our tracked pin for the cursor. @@ -303,9 +320,10 @@ pub fn init( const page_rac = page_pin.rowAndCell(); var result: Screen = .{ + .io = io, .alloc = alloc, .pages = pages, - .no_scrollback = opts.max_scrollback == 0, + .no_scrollback = opts.max_scrollback_bytes == 0, .cursor = .{ .x = 0, .y = 0, @@ -316,13 +334,12 @@ pub fn init( }; if (comptime build_options.kitty_graphics) { - // This can't fail because the storage is always empty at this point - // and the only fail-able case is that we have to evict images. result.kitty_images.setLimit( + io, alloc, &result, opts.kitty_image_storage_limit, - ) catch unreachable; + ); result.kitty_images.image_limits = opts.kitty_image_loading_limits; } @@ -361,6 +378,16 @@ pub fn assertIntegrity(self: *const Screen) void { ) orelse unreachable; assert(self.cursor.x == pt.active.x); assert(self.cursor.y == pt.active.y); + + // The cursor style and hyperlink if non-zero must reference + // real data in the page the pin is in. + const page: *const Page = self.cursor.page_pin.node.page(); + if (self.cursor.style_id != style.default_id) { + assert(page.styles.refCount(page.memory, self.cursor.style_id) > 0); + } + if (self.cursor.hyperlink_id != 0) { + assert(page.hyperlink_set.refCount(page.memory, self.cursor.hyperlink_id) > 0); + } } } @@ -380,11 +407,16 @@ pub fn reset(self: *Screen) void { self.pages.reset(); // The above reset preserves tracked pins so we can still use - // our cursor pin, which should be at the top-left already. + // our cursor pin, which should be at the top-left already. The + // reset marks every tracked pin as garbage, but we keep using + // this one at its new valid position, so clear the flag: copies + // of the cursor pin (e.g. for Kitty image placements) must not + // be born garbage. const cursor_pin: *PageList.Pin = self.cursor.page_pin; assert(cursor_pin.node == self.pages.pages.first.?); assert(cursor_pin.x == 0); assert(cursor_pin.y == 0); + cursor_pin.garbage = false; const cursor_rac = cursor_pin.rowAndCell(); self.cursor.deinit(self.alloc); self.cursor = .{ @@ -395,8 +427,14 @@ pub fn reset(self: *Screen) void { if (comptime build_options.kitty_graphics) { // Reset kitty graphics storage + const image_limits = self.kitty_images.image_limits; + const total_limit = self.kitty_images.total_limit; self.kitty_images.deinit(self.alloc, self); - self.kitty_images = .{ .dirty = true }; + self.kitty_images = .{ + .dirty = true, + .image_limits = image_limits, + .total_limit = total_limit, + }; } // Reset our basic state @@ -441,6 +479,7 @@ pub fn reset(self: *Screen) void { /// pub fn clone( self: *const Screen, + io: std.Io, alloc: Allocator, top: point.Point, bot: ?point.Point, @@ -565,6 +604,7 @@ pub fn clone( } else null; const result: Screen = .{ + .io = io, .alloc = alloc, .pages = pages, .no_scrollback = self.no_scrollback, @@ -575,6 +615,7 @@ pub fn clone( result.assertIntegrity(); return result; } + pub fn increaseCapacity( self: *Screen, node: *PageList.List.Node, @@ -645,6 +686,74 @@ pub fn increaseCapacity( return new_node; } +/// Clone the cells in columns [x_start, x_end) of a source row into +/// the row at `dst_y` of the given node's page, increasing the node's +/// capacity as necessary to fit the managed memory (styles, +/// hyperlinks, etc.) of the copied cells. +/// +/// This is the Screen-level analog of PageList.cloneRowGrowCapacity: +/// capacity increases are routed through Screen.increaseCapacity so +/// that the cursor's style/hyperlink references are migrated when the +/// destination node is the cursor's page. +/// +/// Since increasing capacity replaces the node in the page list, the +/// (possibly replaced) node is returned and the caller must use it in +/// place of the old node. Tracked pins are updated automatically. The +/// source must NOT be on the given node since the node's page memory +/// may be freed on capacity increase. +/// +/// Callers use this mid-mutation (after rows have been rotated or +/// shifted), so a failure can't be propagated without leaving the +/// page list half-mutated (and corrupt). If the capacity can't be +/// increased (system OOM or the page is already at max capacity), +/// this panics: a crash is better than corruption. +pub fn clonePartialRowGrowCapacity( + self: *Screen, + node: *PageList.List.Node, + dst_y: usize, + src_page: *Page, + src_row: *const Row, + x_start: usize, + x_end: usize, +) *PageList.List.Node { + assert(src_page != node.page()); + + var current = node; + while (true) { + const cur_page: *Page = current.page(); + const cur_rows = cur_page.rows.ptr(cur_page.memory.ptr); + cur_page.clonePartialRowFrom( + src_page, + &cur_rows[dst_y], + src_row, + x_start, + x_end, + ) catch |err| { + // Adjust our page capacity to make room for what we + // didn't have space for and retry the copy. + current = self.increaseCapacity( + current, + PageList.IncreaseCapacity.forCloneError(err), + ) catch |e| switch (e) { + // We can't gracefully recover from either of these + // here: our callers have already rotated or shifted + // rows, so returning an error would leave the page + // list half-mutated (and corrupt), so a crash is + // better. + error.OutOfMemory, + => @panic("increaseCapacity system allocator OOM"), + + error.OutOfSpace, + => @panic("increaseCapacity OutOfSpace"), + }; + + continue; + }; + + return current; + } +} + pub inline fn cursorCellRight(self: *Screen, n: size.CellCountInt) *pagepkg.Cell { assert(self.cursor.x + n < self.pages.cols); const cell: [*]pagepkg.Cell = @ptrCast(self.cursor.page_cell); @@ -775,9 +884,31 @@ pub fn cursorReload(self: *Screen) void { .active, self.cursor.page_pin.*, ) orelse reset: { + // Our cached row/cell pointers may be invalid (that is often + // the reason cursorReload is being called), so refresh them + // from the pin first since cursorChangePin below marks the + // old cursor row as dirty. + const old_rac = self.cursor.page_pin.rowAndCell(); + self.cursor.page_row = old_rac.row; + self.cursor.page_cell = old_rac.cell; + + // The cursor style and hyperlink IDs are only valid within the + // page that the pin points at, so the pin change must go through + // cursorChangePin, which migrates them when the active top-left + // is on a different page. Writing the pin directly here would + // leave the cursor holding IDs that are dead or alias unrelated + // entries on the new page. const pin = self.pages.pin(.{ .active = .{} }).?; - self.cursor.page_pin.* = pin; - break :reset self.pages.pointFromPin(.active, pin).?; + self.cursor.x = 0; // Must be set before cursorChangePin + self.cursor.y = 0; + self.cursorChangePin(pin); + + // cursorChangePin can trigger a page capacity adjustment which + // moves the pin again, so we re-read it to derive our point. + break :reset self.pages.pointFromPin( + .active, + self.cursor.page_pin.*, + ).?; }; self.cursor.x = @intCast(pt.active.x); @@ -785,20 +916,6 @@ pub fn cursorReload(self: *Screen) void { const page_rac = self.cursor.page_pin.rowAndCell(); self.cursor.page_row = page_rac.row; self.cursor.page_cell = page_rac.cell; - - // If we have a style, we need to ensure it is in the page because this - // method may also be called after a page change. - if (self.cursor.style_id != style.default_id) { - self.manualStyleUpdate() catch |err| { - // This failure should not happen because manualStyleUpdate - // handles page splitting, overflow, and more. This should only - // happen if we're out of RAM. In this case, we'll just degrade - // gracefully back to the default style. - log.err("failed to update style on cursor reload err={}", .{err}); - self.cursor.style = .{}; - self.cursor.style_id = 0; - }; - } } /// Scroll the active area and keep the cursor at the bottom of the screen. @@ -824,6 +941,12 @@ pub fn cursorDownScroll(self: *Screen) !void { self.cursor.page_row, page.getCells(self.cursor.page_row), ); + + // The row is a fresh blank row now and must not retain + // metadata (wrap state, semantic prompt) from the + // discarded content. + self.cursor.page_row.reset(); + self.cursorMarkDirty(); } else { // The call to `eraseRow` will move the tracked cursor pin up by one @@ -1035,36 +1158,58 @@ fn cursorScrollAboveRotate( self: *Screen, fresh_node: ?*PageList.List.Node, ) !void { + // A fresh node given to us is always the newly allocated tail of + // the page list (see grow), which is where our iteration starts. + assert(fresh_node == null or fresh_node == self.pages.pages.last); + self.cursorChangePin(self.cursor.page_pin.down(1).?); // Go through each of the pages following our pin, shift all rows - // down by one, and copy the last row of the previous page. + // down by one, and copy the last row of the previous page. We start + // at the tail, which is the only node that can be freshly allocated. var current = self.pages.pages.last.?; - while (current != self.cursor.page_pin.node) : (current = current.prev.?) { + var current_is_fresh = fresh_node != null; + while (current != self.cursor.page_pin.node) : ({ + current = current.prev.?; + current_is_fresh = false; + }) { const prev = current.prev.?; - const prev_page = prev.page(); - const cur_page = current.page(); - const prev_rows = prev_page.rows.ptr(prev_page.memory.ptr); - const cur_rows = cur_page.rows.ptr(cur_page.memory.ptr); // A newly allocated tail has no earlier references to invalidate. - if (fresh_node == null or current != fresh_node.?) { + if (!current_is_fresh) { // Rotating this page moves every cached row coordinate down by one. self.pages.invalidateNodeLayout(current); } // Rotate the pages down: [ 0 1 2 3 ] => [ 3 0 1 2 ] - fastmem.rotateOnceR(Row, cur_rows[0..cur_page.size.rows]); + { + const cur_page = current.page(); + const cur_rows = cur_page.rows.ptr(cur_page.memory.ptr); + fastmem.rotateOnceR(Row, cur_rows[0..cur_page.size.rows]); + } // Copy the last row of the previous page to the top of current. - try cur_page.cloneRowFrom( - prev_page, - &cur_rows[0], - &prev_rows[prev_page.size.rows - 1], - ); + // If the current page doesn't have enough capacity for the + // managed memory of the copied row (styles, hyperlinks, etc.) + // then its capacity is increased and the copy retried, which + // may replace `current` in the page list. Our loop condition + // guarantees `current` is never the cursor page, and `prev` is + // unaffected by the replacement. + { + const prev_page = prev.page(); + const prev_rows = prev_page.rows.ptr(prev_page.memory.ptr); + current = self.clonePartialRowGrowCapacity( + current, + 0, + prev_page, + &prev_rows[prev_page.size.rows - 1], + 0, + self.pages.cols, + ); + } // Mark dirty on the page, since we are dirtying all rows with this. - cur_page.dirty = true; + current.page().dirty = true; } // Our current is our cursor page, we need to rotate down from @@ -1081,6 +1226,11 @@ fn cursorScrollAboveRotate( cur_page.getCells(&cur_rows[self.cursor.page_pin.y]), ); + // The recycled storage becomes the new blank cursor row and must + // not retain metadata (wrap state, semantic prompt) from the row + // whose content was moved to the next page. + cur_rows[self.cursor.page_pin.y].reset(); + // Mark the whole page as dirty. // // Technically we only need to mark from the cursor row to the @@ -1154,6 +1304,11 @@ pub fn cursorScrollRegionUp(self: *Screen, limit: usize) !void { // row with our blank cell, preserving the background color. self.clearCells(page, row, page.getCells(row)); } + + // The row becomes the new blank cursor row after the rotation + // below and must not retain metadata (wrap state, semantic + // prompt) from the discarded content. + row.reset(); } // Rotate the region rows so the now-blank top row moves to the @@ -1366,6 +1521,10 @@ inline fn cursorChangePin(self: *Screen, new: Pin) void { if (self.cursor.hyperlink != null) { const old_page: *Page = self.cursor.page_pin.node.page(); old_page.hyperlink_set.release(old_page.memory, self.cursor.hyperlink_id); + // Zero the ID, it is invalid now and style changes below may + // run integrity checks. We still have self.cursor.hyperlink to + // rebuild this later. + self.cursor.hyperlink_id = 0; } // Update our pin to the new page @@ -1387,8 +1546,9 @@ inline fn cursorChangePin(self: *Screen, new: Pin) void { // On the new page, we need to migrate our hyperlink if (self.cursor.hyperlink) |link| { - // So we don't attempt to free any memory in the replaced page. - self.cursor.hyperlink_id = 0; + // startHyperlink will try to free old hyperlinks, so set this + // to null. We free it ourselves later since we're doing some + // ref-counting shenanigans in this function. self.cursor.hyperlink = null; // Re-add @@ -1789,6 +1949,11 @@ pub fn splitCellBoundary( p_rac.row, p_cells[p_row.node.cols() - 1 ..][0..1], ); + + // `clearCells` does not mark rows dirty, and our + // callers only mark the cursor row, so mark the + // previous row here. + p_row.markDirty(); } } } @@ -1851,50 +2016,80 @@ pub const Resize = struct { prompt_redraw: osc.semantic_prompt.Redraw = .false, }; -/// Resize the screen. The rows or cols can be bigger or smaller. -/// -/// If this returns an error, the screen is left in a likely garbage state. -/// It is very hard to undo this operation without blowing up our memory -/// usage. The only way to recover is to reset the screen. The only way -/// this really fails is if page allocation is required and fails, which -/// probably means the system is in trouble anyways. I'd like to improve this -/// in the future but it is not a priority particularly because this scenario -/// (resize) is difficult. +const resize_tw = tripwire.module(enum { + saved_cursor_pin, + pages, +}, resize); + +/// Resize the screen. The rows or cols can be bigger or smaller. If this +/// returns an error, the screen is unchanged. pub inline fn resize( self: *Screen, opts: Resize, -) !void { +) Allocator.Error!void { + const tw = resize_tw; defer self.assertIntegrity(); - if (comptime build_options.kitty_graphics) { - // No matter what we mark our image state as dirty - self.kitty_images.dirty = true; - } + // We need to insert a tracked pin for our saved cursor so we can + // modify its X/Y for reflow. Do this before changing any state since + // tracking the pin can fail. + const saved_cursor_pin: ?*Pin = saved_cursor: { + const sc = self.saved_cursor orelse break :saved_cursor null; + const pin = self.pages.pin(.{ .active = .{ + .x = sc.x, + .y = sc.y, + } }) orelse break :saved_cursor null; + try tw.check(.saved_cursor_pin); + break :saved_cursor try self.pages.trackPin(pin); + }; + defer if (saved_cursor_pin) |p| self.pages.untrackPin(p); - // Release the cursor style while resizing just - // in case the cursor ends up on a different page. + // A cursor style and hyperlink are partly stored in the page containing + // the cursor. Their IDs only have meaning within that page, and the page + // keeps reference counts for them. Resizing can replace or destroy the + // page, so below we temporarily remove both values from the cursor before + // asking PageList to resize. + // + // Save everything first so we can put the cursor back together afterward, + // or restore it unchanged if the resize fails. We also save the original + // node and serial so after a successful resize we can tell whether that + // page survived and still needs its temporary references released. + const cursor_node = self.cursor.page_pin.node; + const cursor_node_serial = cursor_node.serial; const cursor_style = self.cursor.style; - self.cursor.style = .{}; - self.manualStyleUpdate() catch unreachable; - defer { - // Restore the cursor style. + const cursor_style_id = self.cursor.style_id; + const cursor_hyperlink = self.cursor.hyperlink; + const cursor_hyperlink_id = self.cursor.hyperlink_id; + + // Keep an extra reference to the cursor style and hyperlink while the + // resize is in progress. Releasing the cursor references below therefore + // can't delete either entry, and an error can restore the cursor without + // any allocation. + { + const page = cursor_node.page(); + if (cursor_style_id != style.default_id) { + page.styles.use(page.memory, cursor_style_id); + } + if (cursor_hyperlink_id != 0) { + page.hyperlink_set.use(page.memory, cursor_hyperlink_id); + } + } + errdefer { self.cursor.style = cursor_style; - self.manualStyleUpdate() catch |err| { - // This failure should not happen because manualStyleUpdate - // handles page splitting, overflow, and more. This should only - // happen if we're out of RAM. In this case, we'll just degrade - // gracefully back to the default style. - log.err("failed to update style on cursor reload err={}", .{err}); - self.cursor.style = .{}; - self.cursor.style_id = 0; - }; + self.cursor.style_id = cursor_style_id; + self.cursor.hyperlink = cursor_hyperlink; + self.cursor.hyperlink_id = cursor_hyperlink_id; } + // Release the cursor style while resizing just in case the cursor ends + // up on a different page. + self.cursor.style = .{}; + self.manualStyleUpdate() catch unreachable; + // If we have a hyperlink, release it from the old page // and then we need to re-add it to the new page. This needs // to happen because resize below typically reallocates a // new page so the old hyperlink is invalid. - const hyperlink_ = self.cursor.hyperlink; if (self.cursor.hyperlink_id != 0) { // Note we do NOT use endHyperlink because we want to keep // our allocated self.cursor.hyperlink valid. @@ -1904,73 +2099,8 @@ pub inline fn resize( self.cursor.hyperlink = null; } - // We need to insert a tracked pin for our saved cursor so we can - // modify its X/Y for reflow. - const saved_cursor_pin: ?*Pin = saved_cursor: { - const sc = self.saved_cursor orelse break :saved_cursor null; - const pin = self.pages.pin(.{ .active = .{ - .x = sc.x, - .y = sc.y, - } }) orelse break :saved_cursor null; - break :saved_cursor try self.pages.trackPin(pin); - }; - defer if (saved_cursor_pin) |p| self.pages.untrackPin(p); - - // If our cursor is on a prompt or input line, clear it so the shell can - // redraw it. This works with OSC 133 semantic prompts. - // - // We check cursor.semantic_content rather than page_row.semantic_prompt - // because some shells (e.g., Nu) mark input areas with OSC 133 B but don't - // mark continuation lines with k=s. If the input spans multiple lines and - // continuation lines are unmarked, checking only page_row.semantic_prompt - // would miss them. By checking semantic_content, we assume that if the - // cursor is on anything other than command output, we're at a prompt/input - // line and should clear from there. - if (opts.prompt_redraw != .false and - self.cursor.semantic_content != .output) - prompt: { - switch (opts.prompt_redraw) { - .false => unreachable, - - // For `.last`, only clear the current line where the cursor is. - // For `.true`, clear all prompt lines starting from the beginning. - .last => { - const page = self.cursor.page_pin.node.page(); - const row = self.cursor.page_row; - const cells = page.getCells(row); - self.clearCells(page, row, cells); - }, - - .true => { - const start = start: { - var it = self.cursor.page_pin.promptIterator( - .left_up, - null, - ); - break :start it.next() orelse { - // This should never happen because promptIterator should always - // find a prompt if we already verified our row is some kind of - // prompt. - log.warn("cursor on prompt line but promptIterator found no prompt", .{}); - break :prompt; - }; - }; - - // Clear cells from our start down. We replace it with spaces, - // and do not physically erase the rows (eraseRows) because the - // shell is going to expect this space to be available. - var it = start.rowIterator(.right_down, null); - while (it.next()) |pin| { - const page = pin.node.page(); - const row = pin.rowAndCell().row; - const cells = page.getCells(row); - self.clearCells(page, row, cells); - } - }, - } - } - // Perform the resize operation. + try tw.check(.pages); try self.pages.resize(.{ .rows = opts.rows, .cols = opts.cols, @@ -1982,17 +2112,39 @@ pub inline fn resize( }, }); + // No more failures are possible after this. Enforced by compiler + // because we do NO cleanup of the state below. + errdefer comptime unreachable; + + // Resizing moves image placements, so mark their geometry as dirty. + if (comptime build_options.kitty_graphics) self.kitty_images.dirty = true; + // If we have no scrollback and we shrunk our rows, we must explicitly // erase our history. This is because PageList always keeps at least // a page size of history. - if (self.no_scrollback) { - self.pages.eraseHistory(null); - } + if (self.no_scrollback) self.pages.eraseHistory(null); // If our cursor was updated, we do a full reload so all our cursor // state is correct. self.cursorReload(); + // Clear any redrawable prompt after the fallible resize but before + // restoring the cursor style and hyperlink, so cleared cells retain the + // same default styling they had with the previous ordering. + self.clearPromptForRedraw(opts.prompt_redraw); + + // Restore the cursor style. + self.cursor.style = cursor_style; + self.manualStyleUpdate() catch |err| { + // This failure should not happen because manualStyleUpdate handles + // page splitting, overflow, and more. This should only happen if + // we're out of RAM. In this case, we'll just degrade gracefully back + // to the default style. + log.err("failed to update style on cursor reload err={}", .{err}); + self.cursor.style = .{}; + self.cursor.style_id = 0; + }; + // If we reflowed a saved cursor, update it. if (saved_cursor_pin) |p| { // This should never fail because a non-null saved_cursor_pin @@ -2024,7 +2176,7 @@ pub inline fn resize( } // Fix up our hyperlink if we had one. - if (hyperlink_) |link| { + if (cursor_hyperlink) |link| { self.startHyperlink(link.uri, switch (link.id) { .explicit => |v| v, .implicit => null, @@ -2039,6 +2191,97 @@ pub inline fn resize( link.deinit(self.alloc); self.alloc.destroy(link); } + + // A tracked pin follows its content when PageList moves or replaces a + // page. If the cursor's pin still points to the node we started with, + // that node survived the resize and still owns the temporary style and + // hyperlink references we added above. + // + // Comparing pointers is not enough by itself. PageList keeps a pool of + // nodes, so it can destroy a node and create a replacement at the same + // memory address. Every new use of a node gets a different serial number, + // making the pointer-and-serial pair an O(1) identity check. + const cursor_node_survived = + self.cursor.page_pin.node == cursor_node and + self.cursor.page_pin.node.serial == cursor_node_serial; + if (comptime build_options.slow_runtime_safety) { + assert(cursor_node_survived == + self.pages.nodeIsValid(cursor_node, cursor_node_serial)); + } + + // A surviving node still contains our temporary references, so remove + // them now. If the node was replaced, its destruction removed those + // references along with the rest of the old page so we don't need to + // fix it up. + if (cursor_node_survived) { + const page = cursor_node.page(); + if (cursor_style_id != style.default_id) { + page.styles.release(page.memory, cursor_style_id); + } + if (cursor_hyperlink_id != 0) { + page.hyperlink_set.release(page.memory, cursor_hyperlink_id); + } + } +} + +fn clearPromptForRedraw( + self: *Screen, + redraw: osc.semantic_prompt.Redraw, +) void { + // If our cursor is on a prompt or input line, clear it so the shell can + // redraw it. This works with OSC 133 semantic prompts. We do this after + // the fallible resize so an error leaves the original prompt untouched. + // + // We check cursor.semantic_content rather than page_row.semantic_prompt + // because some shells (e.g., Nu) mark input areas with OSC 133 B but don't + // mark continuation lines with k=s. If the input spans multiple lines and + // continuation lines are unmarked, checking only page_row.semantic_prompt + // would miss them. By checking semantic_content, we assume that if the + // cursor is on anything other than command output, we're at a prompt/input + // line and should clear from there. + if (redraw != .false and + self.cursor.semantic_content != .output) + prompt: { + switch (redraw) { + .false => unreachable, + + // For `.last`, only clear the current line where the cursor is. + // For `.true`, clear all prompt lines starting from the beginning. + .last => { + const page = self.cursor.page_pin.node.page(); + const row = self.cursor.page_row; + const cells = page.getCells(row); + self.clearCells(page, row, cells); + }, + + .true => { + const start = start: { + var it = self.cursor.page_pin.promptIterator( + .left_up, + null, + ); + break :start it.next() orelse { + // This should never happen because promptIterator should always + // find a prompt if we already verified our row is some kind of + // prompt. + log.warn("cursor on prompt line but promptIterator found no prompt", .{}); + break :prompt; + }; + }; + + // Clear cells from our start down. We replace it with spaces, + // and do not physically erase the rows (eraseRows) because the + // shell is going to expect this space to be available. + var it = start.rowIterator(.right_down, null); + while (it.next()) |pin| { + const page = pin.node.page(); + const row = pin.rowAndCell().row; + const cells = page.getCells(row); + self.clearCells(page, row, cells); + } + }, + } + } } /// Set a style attribute for the current cursor. @@ -2684,17 +2927,8 @@ pub const SelectionString = struct { /// If true, trim whitespace around the selection. trim: bool = true, - - /// If non-null, a stringmap will be written here. This will use - /// the same allocator as the call to selectionString. The string will - /// be duplicated here and in the return value so both must be freed. - map: ?*StringMap = null, }; -const selectionString_tw = tripwire.module(enum { - copy_map, -}, selectionString); - /// Returns the raw text associated with a selection. This will unwrap /// soft-wrapped edges. The returned slice is owned by the caller and allocated /// using alloc, not the allocator associated with the screen (unless they match). @@ -2704,6 +2938,32 @@ pub fn selectionString( self: *Screen, alloc: Allocator, opts: SelectionString, +) Allocator.Error![:0]const u8 { + return self.selectionStringImpl(alloc, opts, null); +} + +/// Returns a StringMap associated with a selection. The map contains the +/// selection's raw text and a mapping from each byte to its screen location. +/// +/// The returned map is owned by the caller. +pub fn selectionStringMap( + self: *Screen, + alloc: Allocator, + opts: SelectionString, +) Allocator.Error!StringMap { + var pins: PinMap.Map = .empty; + errdefer pins.deinit(alloc); + return .{ + .string = try self.selectionStringImpl(alloc, opts, &pins), + .map = pins, + }; +} + +fn selectionStringImpl( + self: *Screen, + alloc: Allocator, + opts: SelectionString, + pins: ?*PinMap.Map, ) Allocator.Error![:0]const u8 { // We'll use this as our buffer to build our string. var aw: std.Io.Writer.Allocating = .init(alloc); @@ -2720,33 +2980,15 @@ pub fn selectionString( ); formatter.content = .{ .selection = opts.sel }; - // If we have a string map, we need to set that up. - var pins: std.ArrayList(Pin) = .empty; - defer pins.deinit(alloc); - if (opts.map != null) formatter.pin_map = .{ + if (pins) |map| formatter.pin_map = .{ .alloc = alloc, - .map = &pins, + .map = map, }; // Emit. Since this is an allocating writer, a failed write // just becomes an OOM. formatter.format(&aw.writer) catch return error.OutOfMemory; - - // Build our final text and if we have a string map set that up. - const text = try aw.toOwnedSliceSentinel(0); - errdefer alloc.free(text); - if (opts.map) |map| { - const map_string = try alloc.dupeZ(u8, text); - errdefer alloc.free(map_string); - try selectionString_tw.check(.copy_map); - const map_pins = try pins.toOwnedSlice(alloc); - map.* = .{ - .string = map_string, - .map = map_pins, - }; - } - - return text; + return try aw.toOwnedSliceSentinel(0); } pub const SelectLine = struct { @@ -2894,7 +3136,7 @@ pub fn selectLine(self: *const Screen, opts: SelectLine) ?Selection { const this_whitespace = std.mem.indexOfScalar( u21, whitespace, - cell.content.codepoint, + cell.content.codepoint.data, ) != null; if (this_whitespace) continue; @@ -2916,7 +3158,7 @@ pub fn selectLine(self: *const Screen, opts: SelectLine) ?Selection { const this_whitespace = std.mem.indexOfScalar( u21, whitespace, - cell.content.codepoint, + cell.content.codepoint.data, ) != null; if (this_whitespace) continue; @@ -2948,7 +3190,7 @@ pub fn selectAll(self: *Screen) ?Selection { const this_whitespace = std.mem.indexOfAny( u32, whitespace, - &[_]u32{cell.content.codepoint}, + &[_]u32{cell.content.codepoint.data}, ) != null; if (this_whitespace) continue; @@ -2972,7 +3214,7 @@ pub fn selectAll(self: *Screen) ?Selection { const this_whitespace = std.mem.indexOfAny( u32, whitespace, - &[_]u32{cell.content.codepoint}, + &[_]u32{cell.content.codepoint.data}, ) != null; if (this_whitespace) continue; @@ -3040,7 +3282,7 @@ pub fn selectWord( const expect_boundary = std.mem.indexOfScalar( u21, boundary_codepoints, - start_cell.content.codepoint, + start_cell.content.codepoint.data, ) != null; // Go forwards to find our end boundary @@ -3058,7 +3300,7 @@ pub fn selectWord( const this_boundary = std.mem.indexOfScalar( u21, boundary_codepoints, - cell.content.codepoint, + cell.content.codepoint.data, ) != null; if (this_boundary != expect_boundary) break :end prev; @@ -3095,7 +3337,7 @@ pub fn selectWord( const this_boundary = std.mem.indexOfScalar( u21, boundary_codepoints, - cell.content.codepoint, + cell.content.codepoint.data, ) != null; if (this_boundary != expect_boundary) break :start prev; @@ -3517,7 +3759,7 @@ pub fn testWriteString(self: *Screen, text: []const u8) !void { 1 => { self.cursor.page_cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = c }, + .content = .{ .codepoint = @bitCast(@as(u24, @intCast(c))) }, .style_id = self.cursor.style_id, .protected = self.cursor.protected, .semantic_content = self.cursor.semantic_content, @@ -3539,7 +3781,7 @@ pub fn testWriteString(self: *Screen, text: []const u8) !void { if (self.cursor.x == self.pages.cols - 1) { self.cursor.page_cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_head, .protected = self.cursor.protected, .semantic_content = self.cursor.semantic_content, @@ -3557,7 +3799,7 @@ pub fn testWriteString(self: *Screen, text: []const u8) !void { // Write our wide char self.cursor.page_cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = c }, + .content = .{ .codepoint = .{ .data = c } }, .style_id = self.cursor.style_id, .wide = .wide, .protected = self.cursor.protected, @@ -3571,7 +3813,7 @@ pub fn testWriteString(self: *Screen, text: []const u8) !void { self.cursorRight(1); self.cursor.page_cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .wide = .spacer_tail, .protected = self.cursor.protected, .semantic_content = self.cursor.semantic_content, @@ -3600,11 +3842,48 @@ pub fn testWriteString(self: *Screen, text: []const u8) !void { } } +test "Screen forwards optional scrollback limits" { + const testing = std.testing; + const max_lines: usize = 123; + var s = try init(testing.io, testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_scrollback_bytes = null, + .max_scrollback_lines = max_lines, + }); + defer s.deinit(); + + try testing.expectEqual( + std.math.maxInt(usize), + s.pages.limits.bytes.explicit, + ); + try testing.expectEqual(max_lines, s.pages.limits.lines.explicit); + try testing.expect(!s.no_scrollback); +} + +test "Screen reset cursor pin is not garbage" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); + defer s.deinit(); + try s.testWriteString("hello, world"); + + // The page reset marks every tracked pin garbage but the screen + // keeps using the cursor pin, so it must come back clean: anything + // that copies it (e.g. Kitty image placements) would otherwise be + // born garbage and reaped. + s.reset(); + try testing.expect(!s.cursor.page_pin.garbage); +} + test "Screen read and write" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); try testing.expectEqual(@as(style.Id, 0), s.cursor.style_id); @@ -3617,8 +3896,9 @@ test "Screen read and write" { test "Screen read and write newline" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); try testing.expectEqual(@as(style.Id, 0), s.cursor.style_id); @@ -3631,8 +3911,9 @@ test "Screen read and write newline" { test "Screen read and write scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 2, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 2, .max_scrollback_bytes = 1000 }); defer s.deinit(); try s.testWriteString("hello\nworld\ntest"); @@ -3651,8 +3932,9 @@ test "Screen read and write scrollback" { test "Screen read and write no scrollback small" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 2, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 2, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello\nworld\ntest"); @@ -3671,8 +3953,9 @@ test "Screen read and write no scrollback small" { test "Screen read and write no scrollback large" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 2, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 2, .max_scrollback_bytes = 0 }); defer s.deinit(); for (0..1_000) |i| { @@ -3692,14 +3975,15 @@ test "Screen read and write no scrollback large" { test "Screen cursorCopy x/y" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); s.cursorAbsolute(2, 3); try testing.expect(s.cursor.x == 2); try testing.expect(s.cursor.y == 3); - var s2 = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s2 = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s2.deinit(); try s2.cursorCopy(s.cursor, .{}); try testing.expect(s2.cursor.x == 2); @@ -3716,11 +4000,12 @@ test "Screen cursorCopy x/y" { test "Screen cursorCopy style deref" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); - var s2 = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s2 = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s2.deinit(); const page = s2.cursor.page_pin.node.page(); @@ -3738,11 +4023,12 @@ test "Screen cursorCopy style deref" { test "Screen cursorCopy style deref new page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); - var s2 = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 2048 }); + var s2 = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 2048 }); defer s2.deinit(); // We need to get the cursor on a new page. @@ -3811,12 +4097,13 @@ test "Screen cursorCopy style deref new page" { test "Screen cursorCopy style copy" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.setAttribute(.{ .bold = {} }); - var s2 = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s2 = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s2.deinit(); const page = s2.cursor.page_pin.node.page(); try s2.cursorCopy(s.cursor, .{}); @@ -3827,11 +4114,12 @@ test "Screen cursorCopy style copy" { test "Screen cursorCopy hyperlink deref" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); - var s2 = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s2 = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s2.deinit(); const page = s2.cursor.page_pin.node.page(); @@ -3849,11 +4137,12 @@ test "Screen cursorCopy hyperlink deref" { test "Screen write regrows compacted page capacity" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); @@ -3894,14 +4183,91 @@ test "Screen write regrows compacted page capacity" { try testing.expect(page.graphemeCount() >= 1); } +// The cursor style and hyperlink IDs are only meaningful within the page +// the cursor pin points at. scrollClear can move the active area onto a +// later page while the cursor pin stays with its content on an earlier +// page (now scrollback), so the reset in cursorReload must migrate both +// references to the destination page. It previously replaced the pin +// directly and then released the old style ID on the new page. +test "Screen scrollClear across pages migrates cursor style and hyperlink" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ + .cols = 10, + .rows = 10, + .max_scrollback_bytes = std.math.maxInt(usize), + }); + defer s.deinit(); + + // Fill the first page so the active area spans two pages. + const first_page_size = s.pages.pages.first.?.capacity().rows; + s.pages.pages.first.?.page().pauseIntegrityChecks(true); + for (0..first_page_size - 5) |_| { + try s.testWriteString("\n"); + } + s.pages.pages.first.?.page().pauseIntegrityChecks(false); + try s.testWriteString("1\n2\n3\n4\n5\n6\n7\n8\n9\n10"); + try testing.expect(s.pages.pages.first != s.pages.pages.last); + + // Move the cursor to the top of the active area, which is on the + // first page, and give it a style and a hyperlink there. + s.cursorAbsolute(0, 0); + try testing.expect(s.cursor.page_pin.node == s.pages.pages.first.?); + try s.setAttribute(.{ .bold = {} }); + try s.startHyperlink("https://example.com/", null); + + const old_page: *Page = s.cursor.page_pin.node.page(); + const old_style_id = s.cursor.style_id; + const old_hyperlink_id = s.cursor.hyperlink_id; + try testing.expect(old_style_id != style.default_id); + try testing.expect(old_hyperlink_id != 0); + + // All ten active rows are non-empty, so this moves the active area + // fully onto the second page while the cursor pin stays with its + // old row, which is now scrollback. + try s.scrollClear(); + + // The cursor was moved to the new active top-left on the second + // page with its style and hyperlink references rebuilt there. + const new_page: *Page = s.cursor.page_pin.node.page(); + try testing.expect(new_page != old_page); + try testing.expect(s.cursor.style_id != style.default_id); + try testing.expect(s.cursor.hyperlink_id != 0); + try testing.expect(new_page.styles.refCount( + new_page.memory, + s.cursor.style_id, + ) > 0); + try testing.expect(new_page.hyperlink_set.refCount( + new_page.memory, + s.cursor.hyperlink_id, + ) > 0); + + // The cursor's references on the old page were released. Nothing + // else referenced either entry, so both are dead there now. + try testing.expectEqual(0, old_page.styles.refCount( + old_page.memory, + old_style_id, + )); + try testing.expectEqual(0, old_page.hyperlink_set.refCount( + old_page.memory, + old_hyperlink_id, + )); + + // Printing attaches the migrated style and hyperlink to a cell. + try s.testWriteString("B"); +} + test "Screen cursorCopy hyperlink deref new page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); - var s2 = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 2048 }); + var s2 = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 2048 }); defer s2.deinit(); // We need to get the cursor on a new page. @@ -3970,8 +4336,9 @@ test "Screen cursorCopy hyperlink deref new page" { test "Screen cursorCopy hyperlink copy" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); // Create a hyperlink for the cursor. @@ -3979,7 +4346,7 @@ test "Screen cursorCopy hyperlink copy" { try testing.expectEqual(@as(usize, 1), s.cursor.page_pin.node.page().hyperlink_set.count()); try testing.expect(s.cursor.hyperlink_id != 0); - var s2 = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s2 = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s2.deinit(); const page = s2.cursor.page_pin.node.page(); @@ -3995,8 +4362,9 @@ test "Screen cursorCopy hyperlink copy" { test "Screen cursorCopy hyperlink copy disabled" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); // Create a hyperlink for the cursor. @@ -4004,7 +4372,7 @@ test "Screen cursorCopy hyperlink copy disabled" { try testing.expectEqual(@as(usize, 1), s.cursor.page_pin.node.page().hyperlink_set.count()); try testing.expect(s.cursor.hyperlink_id != 0); - var s2 = try Screen.init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s2 = try Screen.init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s2.deinit(); const page = s2.cursor.page_pin.node.page(); @@ -4020,8 +4388,9 @@ test "Screen cursorCopy hyperlink copy disabled" { test "Screen style basics" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); const page = s.cursor.page_pin.node.page(); try testing.expectEqual(@as(usize, 0), page.styles.count()); @@ -4042,8 +4411,9 @@ test "Screen style basics" { test "Screen style reset to default" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); const page = s.cursor.page_pin.node.page(); try testing.expectEqual(@as(usize, 0), page.styles.count()); @@ -4062,8 +4432,9 @@ test "Screen style reset to default" { test "Screen style reset with unset" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); const page = s.cursor.page_pin.node.page(); try testing.expectEqual(@as(usize, 0), page.styles.count()); @@ -4082,8 +4453,9 @@ test "Screen style reset with unset" { test "Screen clearRows active one line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); try s.testWriteString("hello, world"); @@ -4097,8 +4469,9 @@ test "Screen clearRows active one line" { test "Screen clearRows active multi line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); try s.testWriteString("hello\nworld"); @@ -4113,8 +4486,9 @@ test "Screen clearRows active multi line" { test "Screen clearRows active styled line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); try s.setAttribute(.{ .bold = {} }); @@ -4138,7 +4512,7 @@ test "Screen clearRows active styled line" { test "Screen clearCells empty range" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .default); + var s = try Screen.init(testing.io, testing.allocator, .default); defer s.deinit(); const page = s.cursor.page_pin.node.page(); @@ -4150,10 +4524,10 @@ test "Screen clearCells empty range" { test "Screen clearRows uses stored page width" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 2, .rows = 1, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); @@ -4170,8 +4544,9 @@ test "Screen clearRows uses stored page width" { test "Screen clearRows protected" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); try s.testWriteString("UNPROTECTED"); @@ -4198,8 +4573,9 @@ test "Screen clearRows protected" { test "Screen eraseRows history" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 1000 }); defer s.deinit(); try s.testWriteString("1\n2\n3\n4\n5\n6"); @@ -4232,8 +4608,9 @@ test "Screen eraseRows history" { test "Screen eraseRows history with more lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 1000 }); defer s.deinit(); try s.testWriteString("A\nB\nC\n1\n2\n3\n4\n5\n6"); @@ -4266,8 +4643,9 @@ test "Screen eraseRows history with more lines" { test "Screen eraseRows active partial" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1\n2\n3"); @@ -4294,10 +4672,10 @@ test "Screen eraseRows active partial" { test "Screen: cursorCellEndOfPrev across mixed-width pages" { const testing = std.testing; - var s = try init(testing.allocator, .{ + var s = try init(testing.io, testing.allocator, .{ .cols = 4, .rows = 2, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); @@ -4316,8 +4694,9 @@ test "Screen: cursorCellEndOfPrev across mixed-width pages" { test "Screen: cursorDown across pages preserves style" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); // Scroll down enough to go to another page @@ -4368,8 +4747,9 @@ test "Screen: cursorDown across pages preserves style" { test "Screen: cursorUp across pages preserves style" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); // Scroll down enough to go to another page @@ -4415,8 +4795,9 @@ test "Screen: cursorUp across pages preserves style" { test "Screen: cursorAbsolute across pages preserves style" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); // Scroll down enough to go to another page @@ -4470,8 +4851,9 @@ test "Screen: cursorAbsolute to page with insufficient capacity" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); // Scroll down enough to go to another page @@ -4537,8 +4919,9 @@ test "Screen: cursorAbsolute to page with insufficient capacity" { test "Screen: scrolling" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.setAttribute(.{ .direct_color_bg = .{ .r = 155 } }); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -4579,8 +4962,9 @@ test "Screen: scrolling" { test "Screen: scrolling with a single-row screen no scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 1, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 1, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABCD"); @@ -4599,8 +4983,9 @@ test "Screen: scrolling with a single-row screen no scrollback" { test "Screen: scrolling with a single-row screen with scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 1, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 1, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD"); @@ -4629,8 +5014,9 @@ test "Screen: scrolling with a single-row screen with scrollback" { test "Screen: scrolling across pages preserves style" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.setAttribute(.{ .bold = {} }); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -4658,8 +5044,9 @@ test "Screen: scrolling across pages preserves style" { test "Screen: scroll down from 0" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -4677,8 +5064,9 @@ test "Screen: scroll down from 0" { test "Screen: scrollback various cases" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); try s.cursorDownScroll(); @@ -4758,8 +5146,9 @@ test "Screen: scrollback various cases" { test "Screen: scrollback with multi-row delta" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 3 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH\n6IJKL"); @@ -4784,8 +5173,9 @@ test "Screen: scrollback with multi-row delta" { test "Screen: scrollback empty" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 50 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 50 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); s.scroll(.{ .delta_row = 1 }); @@ -4799,8 +5189,9 @@ test "Screen: scrollback empty" { test "Screen: scrollback doesn't move viewport if not at bottom" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 3 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH"); @@ -4834,8 +5225,9 @@ test "Screen: scrollback doesn't move viewport if not at bottom" { test "Screen: scrolling moves selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -4913,8 +5305,9 @@ test "Screen: scrolling moves selection" { test "Screen: cursorScrollRegionUp simple" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4MNOP\n5QRST"); @@ -4940,8 +5333,9 @@ test "Screen: cursorScrollRegionUp simple" { test "Screen: cursorScrollRegionUp renews page generation" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4MNOP\n5QRST"); s.cursorAbsolute(0, 2); @@ -4956,8 +5350,9 @@ test "Screen: cursorScrollRegionUp renews page generation" { test "Screen: cursorScrollRegionUp moves selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4MNOP\n5QRST"); @@ -4994,8 +5389,9 @@ test "Screen: cursorScrollRegionUp moves selection" { test "Screen: cursorScrollRegionUp region spans pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10 }); defer s.deinit(); // We need to get the cursor to a new page @@ -5058,8 +5454,9 @@ test "Screen: cursorScrollRegionUp region spans pages" { test "Screen: cursorScrollRegionUp region spans pages with background SGR" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10 }); defer s.deinit(); // We need to get the cursor to a new page. See the previous test @@ -5101,8 +5498,9 @@ test "Screen: cursorScrollRegionUp region spans pages with background SGR" { test "Screen: cursorScrollRegionUp with styled erased row" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); // Write a styled row at the top so the erased row has managed @@ -5130,8 +5528,9 @@ test "Screen: cursorScrollRegionUp with styled erased row" { test "Screen: scrolling moves viewport" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n"); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -5155,8 +5554,9 @@ test "Screen: scrolling moves viewport" { test "Screen: scrolling when viewport is pruned" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 215, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 215, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); // Write some to create scrollback and move back into our scrollback. @@ -5181,8 +5581,9 @@ test "Screen: scrolling when viewport is pruned" { test "Screen: scroll and clear full screen" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -5208,8 +5609,9 @@ test "Screen: scroll and clear full screen" { test "Screen: scroll and clear partial screen" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH"); @@ -5235,8 +5637,9 @@ test "Screen: scroll and clear partial screen" { test "Screen: scroll and clear empty screen" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); try s.scrollClear(); { @@ -5254,8 +5657,9 @@ test "Screen: scroll and clear empty screen" { test "Screen: scroll and clear ignore blank lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH"); try s.scrollClear(); @@ -5297,8 +5701,9 @@ test "Screen: scroll and clear ignore blank lines" { test "Screen: scroll above same page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); try s.setAttribute(.{ .direct_color_bg = .{ .r = 155 } }); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -5359,8 +5764,9 @@ test "Screen: scroll above same page" { test "Screen: scroll above same page but cursor on previous page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10 }); defer s.deinit(); // We need to get the cursor to a new page @@ -5446,8 +5852,9 @@ test "Screen: scroll above same page but cursor on previous page" { test "Screen: scroll above same page but cursor on previous page last row" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10 }); defer s.deinit(); // We need to get the cursor to a new page @@ -5536,8 +5943,9 @@ test "Screen: scroll above same page but cursor on previous page last row" { test "Screen: scroll above creates new page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); // We need to get the cursor to a new page @@ -5611,8 +6019,9 @@ test "Screen: scroll above creates new page" { test "Screen: scroll above with cursor on non-final row" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 4, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 4, .max_scrollback_bytes = 10 }); defer s.deinit(); // Get the cursor to be 2 rows above a new page @@ -5688,8 +6097,9 @@ test "Screen: scroll above with cursor on non-final row" { test "Screen: scroll above no scrollback bottom of page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const first_page_size = s.pages.pages.first.?.capacity().rows; @@ -5732,29 +6142,193 @@ test "Screen: scroll above no scrollback bottom of page" { try testing.expectEqualStrings("2EFGH\n\n3IJKL", contents); } { - const list_cell = s.pages.getCell(.{ .active = .{ .x = 0, .y = 1 } }).?; - const cell = list_cell.cell; - try testing.expect(cell.content_tag == .bg_color_rgb); - try testing.expectEqual(Cell.RGB{ - .r = 155, - .g = 0, - .b = 0, - }, cell.content.color_rgb); + const list_cell = s.pages.getCell(.{ .active = .{ .x = 0, .y = 1 } }).?; + const cell = list_cell.cell; + try testing.expect(cell.content_tag == .bg_color_rgb); + try testing.expectEqual(Cell.RGB{ + .r = 155, + .g = 0, + .b = 0, + }, cell.content.color_rgb); + } + + // Page 0 row 1 (active row 0) is dirty because the cursor moved off of it. + try testing.expect(s.pages.isDirty(.{ .active = .{ .x = 0, .y = 0 } })); + // Page 0 row 2 (active row 1) is dirty because it was cleared. + try testing.expect(s.pages.isDirty(.{ .active = .{ .x = 0, .y = 1 } })); + // Page 0 row 3 (active row 2) is dirty because it is new. + try testing.expect(s.pages.isDirty(.{ .active = .{ .x = 0, .y = 2 } })); +} + +test "Screen: scroll above hyperlink-dense row to fresh page" { + // Regression test for https://github.com/ghostty-org/ghostty/discussions/13160 + // + // When a scroll-above operation pushes a row carrying more unique + // hyperlinks than a fresh page's default hyperlink capacity across + // a page boundary, the cross-page row clone must increase the + // destination page's capacity (like insertLines/deleteLines do) + // rather than error out mid-operation, which leaves the page list + // half-mutated and aborts later (e.g. in clearCells). + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10000 }); + defer s.deinit(); + + // Fill the first page so it is exactly full and the cursor is on + // its last row (which is also the bottom row of the active area). + // The next grow() will then allocate a fresh page. + const first_page_rows = s.pages.pages.first.?.capacity().rows; + s.pages.pages.first.?.page().pauseIntegrityChecks(true); + for (0..first_page_rows - 1) |_| try s.testWriteString("\n"); + s.pages.pages.first.?.page().pauseIntegrityChecks(false); + try testing.expect(s.pages.pages.first == s.pages.pages.last); + try testing.expectEqual( + s.pages.pages.first.?.capacity().rows, + s.pages.pages.first.?.page().size.rows, + ); + try testing.expectEqual(s.pages.rows - 1, s.cursor.y); + + // Fill the bottom row with unique hyperlinks: more than a fresh + // page can hold with default hyperlink capacity. + for (0..s.pages.cols) |i| { + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{i}); + try s.startHyperlink(uri, null); + try s.testWriteString("A"); + s.endHyperlink(); + } + try testing.expectEqual( + @as(usize, s.pages.cols), + s.cursor.page_pin.node.page().hyperlink_set.count(), + ); + + // Move the cursor above the bottom row and scroll. The dense row is + // pushed across the page boundary into the freshly allocated page. + s.cursorAbsolute(0, 1); + try s.cursorScrollAbove(); + + // We must have created a second page and the dense row must now be + // the top row of that page. + try testing.expect(s.pages.pages.first != s.pages.pages.last); + + // All hyperlinks must have survived the scroll intact: every cell + // flagged as a hyperlink must resolve to a real entry in its page's + // hyperlink map. A half-applied scroll leaves cells whose hyperlink + // flag is set but that have no map entry, which aborts in + // clearCells later. + var node_: ?*PageList.List.Node = s.pages.pages.first; + while (node_) |node| : (node_ = node.next) { + const page: *Page = node.page(); + page.assertIntegrity(); + for (0..page.size.rows) |y| { + const row = page.getRow(y); + if (!row.hyperlink) continue; + for (page.getCells(row)) |*cell| { + if (!cell.hyperlink) continue; + try testing.expect(page.lookupHyperlink(cell) != null); + } + } + } + { + const last_page: *Page = s.pages.pages.last.?.page(); + try testing.expectEqual( + @as(usize, s.pages.cols), + last_page.hyperlink_set.count(), + ); + } + + // The dense row is still the bottom row of the active area. + for (0..s.pages.cols) |x| { + const list_cell = s.pages.getCell(.{ .active = .{ + .x = @intCast(x), + .y = 4, + } }).?; + try testing.expect(list_cell.cell.hyperlink); + const page: *Page = list_cell.node.page(); + const id = page.lookupHyperlink(list_cell.cell).?; + const link = page.hyperlink_set.get(page.memory, id); + var buf: [64]u8 = undefined; + const expected = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{x}); + try testing.expectEqualStrings(expected, link.uri.slice(page.memory)); + } +} + +test "Screen: scroll above hyperlink-dense row to existing page" { + // Same as the fresh page variant above but the destination page + // already exists (fresh_node == null path in cursorScrollAboveRotate). + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10000 }); + defer s.deinit(); + + // Fill the first page so it is exactly full and the cursor is on + // its last row. + const first_page_rows = s.pages.pages.first.?.capacity().rows; + s.pages.pages.first.?.page().pauseIntegrityChecks(true); + for (0..first_page_rows - 1) |_| try s.testWriteString("\n"); + s.pages.pages.first.?.page().pauseIntegrityChecks(false); + try testing.expectEqual(s.pages.rows - 1, s.cursor.y); + + // Fill the last row of the first page with unique hyperlinks: + // more than a page can hold with default hyperlink capacity. + for (0..s.pages.cols) |i| { + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{i}); + try s.startHyperlink(uri, null); + try s.testWriteString("A"); + s.endHyperlink(); + } + + // Scroll twice so the active area straddles the page boundary: + // the last two active rows are on a second page while the dense + // row remains the last row of the first page. + try s.testWriteString("\n\n"); + try testing.expect(s.pages.pages.first != s.pages.pages.last); + try testing.expect(s.cursor.page_pin.node == s.pages.pages.last.?); + + // Move the cursor to an active row that is still on the first page + // and above the dense row, then scroll. grow() has capacity in the + // last page so no fresh page is allocated, but the dense row still + // crosses the page boundary during the rotate. + s.cursorAbsolute(0, 0); + try testing.expect(s.cursor.page_pin.node == s.pages.pages.first.?); + try s.cursorScrollAbove(); + + // All hyperlinks must have survived the scroll intact: every cell + // flagged as a hyperlink must resolve to a real entry in its page's + // hyperlink map. + var node_: ?*PageList.List.Node = s.pages.pages.first; + while (node_) |node| : (node_ = node.next) { + const page: *Page = node.page(); + page.assertIntegrity(); + for (0..page.size.rows) |y| { + const row = page.getRow(y); + if (!row.hyperlink) continue; + for (page.getCells(row)) |*cell| { + if (!cell.hyperlink) continue; + try testing.expect(page.lookupHyperlink(cell) != null); + } + } + } + { + const last_page: *Page = s.pages.pages.last.?.page(); + try testing.expectEqual( + @as(usize, s.pages.cols), + last_page.hyperlink_set.count(), + ); } - - // Page 0 row 1 (active row 0) is dirty because the cursor moved off of it. - try testing.expect(s.pages.isDirty(.{ .active = .{ .x = 0, .y = 0 } })); - // Page 0 row 2 (active row 1) is dirty because it was cleared. - try testing.expect(s.pages.isDirty(.{ .active = .{ .x = 0, .y = 1 } })); - // Page 0 row 3 (active row 2) is dirty because it is new. - try testing.expect(s.pages.isDirty(.{ .active = .{ .x = 0, .y = 2 } })); } test "Screen: clone" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH"); { @@ -5766,7 +6340,7 @@ test "Screen: clone" { try testing.expectEqual(@as(usize, 1), s.cursor.y); // Clone - var s2 = try s.clone(alloc, .{ .active = .{} }, null); + var s2 = try s.clone(io, alloc, .{ .active = .{} }, null); defer s2.deinit(); { const contents = try s2.dumpStringAlloc(alloc, .{ .active = .{} }); @@ -5795,8 +6369,9 @@ test "Screen: clone" { test "Screen: clone partial" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH"); { @@ -5808,7 +6383,7 @@ test "Screen: clone partial" { try testing.expectEqual(@as(usize, 1), s.cursor.y); // Clone - var s2 = try s.clone(alloc, .{ .active = .{ .y = 1 } }, null); + var s2 = try s.clone(io, alloc, .{ .active = .{ .y = 1 } }, null); defer s2.deinit(); { const contents = try s2.dumpStringAlloc(alloc, .{ .active = .{} }); @@ -5824,8 +6399,9 @@ test "Screen: clone partial" { test "Screen: clone partial cursor out of bounds" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH"); { @@ -5838,6 +6414,7 @@ test "Screen: clone partial cursor out of bounds" { // Clone var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 0 } }, .{ .active = .{ .y = 0 } }, @@ -5857,8 +6434,9 @@ test "Screen: clone partial cursor out of bounds" { test "Screen: clone contains full selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -5871,6 +6449,7 @@ test "Screen: clone contains full selection" { // Clone var s2 = try s.clone( + io, alloc, .{ .active = .{} }, null, @@ -5894,8 +6473,9 @@ test "Screen: clone contains full selection" { test "Screen: clone contains none of selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -5908,6 +6488,7 @@ test "Screen: clone contains none of selection" { // Clone var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 1 } }, null, @@ -5921,8 +6502,9 @@ test "Screen: clone contains none of selection" { test "Screen: clone contains selection start cutoff" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -5935,6 +6517,7 @@ test "Screen: clone contains selection start cutoff" { // Clone var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 1 } }, null, @@ -5958,8 +6541,9 @@ test "Screen: clone contains selection start cutoff" { test "Screen: clone contains selection end cutoff" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -5972,6 +6556,7 @@ test "Screen: clone contains selection end cutoff" { // Clone var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 0 } }, .{ .active = .{ .y = 1 } }, @@ -5995,8 +6580,9 @@ test "Screen: clone contains selection end cutoff" { test "Screen: clone contains selection end cutoff reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); @@ -6009,6 +6595,7 @@ test "Screen: clone contains selection end cutoff reversed" { // Clone var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 0 } }, .{ .active = .{ .y = 1 } }, @@ -6032,8 +6619,9 @@ test "Screen: clone contains selection end cutoff reversed" { test "Screen: clone contains subset of selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 4, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 4, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4ABCD"); @@ -6046,6 +6634,7 @@ test "Screen: clone contains subset of selection" { // Clone var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 1 } }, .{ .active = .{ .y = 2 } }, @@ -6069,8 +6658,9 @@ test "Screen: clone contains subset of selection" { test "Screen: clone clamps clipped selections to mixed-width pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 4, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 4, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const first = s.pages.pages.first.?; @@ -6086,6 +6676,7 @@ test "Screen: clone clamps clipped selections to mixed-width pages" { false, )); var linear = try s.clone( + io, alloc, .{ .screen = .{} }, .{ .screen = .{ .y = 1 } }, @@ -6101,6 +6692,7 @@ test "Screen: clone clamps clipped selections to mixed-width pages" { true, )); var rectangle = try s.clone( + io, alloc, .{ .screen = .{ .y = 1 } }, null, @@ -6114,8 +6706,9 @@ test "Screen: clone clamps clipped selections to mixed-width pages" { test "Screen: clone contains subset of rectangle selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 4, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 4, .max_scrollback_bytes = 1 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4ABCD"); @@ -6128,6 +6721,7 @@ test "Screen: clone contains subset of rectangle selection" { // Clone var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 1 } }, .{ .active = .{ .y = 2 } }, @@ -6153,13 +6747,15 @@ test "Screen: clone contains subset of rectangle selection" { test "Screen: clone basic" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); { var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 1 } }, .{ .active = .{ .y = 1 } }, @@ -6174,6 +6770,7 @@ test "Screen: clone basic" { { var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 1 } }, .{ .active = .{ .y = 2 } }, @@ -6190,12 +6787,14 @@ test "Screen: clone basic" { test "Screen: clone empty viewport" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); { var s2 = try s.clone( + io, alloc, .{ .viewport = .{ .y = 0 } }, .{ .viewport = .{ .y = 0 } }, @@ -6212,13 +6811,15 @@ test "Screen: clone empty viewport" { test "Screen: clone one line viewport" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABC"); { var s2 = try s.clone( + io, alloc, .{ .viewport = .{ .y = 0 } }, .{ .viewport = .{ .y = 0 } }, @@ -6235,12 +6836,14 @@ test "Screen: clone one line viewport" { test "Screen: clone empty active" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); { var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 0 } }, .{ .active = .{ .y = 0 } }, @@ -6257,13 +6860,15 @@ test "Screen: clone empty active" { test "Screen: clone one line active with extra space" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABC"); { var s2 = try s.clone( + io, alloc, .{ .active = .{ .y = 0 } }, null, @@ -6280,8 +6885,9 @@ test "Screen: clone one line active with extra space" { test "Screen: clear history with no history" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 3 }); defer s.deinit(); try s.testWriteString("4ABCD\n5EFGH\n6IJKL"); try testing.expect(s.pages.viewport == .active); @@ -6304,8 +6910,9 @@ test "Screen: clear history with no history" { test "Screen: clear history" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 3 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH\n6IJKL"); try testing.expect(s.pages.viewport == .active); @@ -6338,8 +6945,9 @@ test "Screen: clear history" { test "Screen: clear above cursor" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 3 }); defer s.deinit(); try s.testWriteString("4ABCD\n5EFGH\n6IJKL"); s.clearRows( @@ -6365,8 +6973,9 @@ test "Screen: clear above cursor" { test "Screen: clear above cursor with history" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 3 }); defer s.deinit(); try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n"); try s.testWriteString("4ABCD\n5EFGH\n6IJKL"); @@ -6393,8 +7002,9 @@ test "Screen: clear above cursor with history" { test "Screen: resize (no reflow) more rows" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6411,8 +7021,9 @@ test "Screen: resize (no reflow) more rows" { test "Screen: resize (no reflow) less rows" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6434,8 +7045,9 @@ test "Screen: resize (no reflow) less rows" { test "Screen: resize (no reflow) less rows trims blank lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD"; try s.testWriteString(str); @@ -6469,8 +7081,9 @@ test "Screen: resize (no reflow) less rows trims blank lines" { test "Screen: resize (no reflow) more rows trims blank lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD"; try s.testWriteString(str); @@ -6504,8 +7117,9 @@ test "Screen: resize (no reflow) more rows trims blank lines" { test "Screen: resize (no reflow) more cols" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6521,8 +7135,9 @@ test "Screen: resize (no reflow) more cols" { test "Screen: resize (no reflow) less cols" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6539,8 +7154,9 @@ test "Screen: resize (no reflow) less cols" { test "Screen: resize (no reflow) more rows with scrollback cursor end" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 7, .rows = 3, .max_scrollback = 2 }); + var s = try init(io, alloc, .{ .cols = 7, .rows = 3, .max_scrollback_bytes = 2 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH"; try s.testWriteString(str); @@ -6556,8 +7172,9 @@ test "Screen: resize (no reflow) more rows with scrollback cursor end" { test "Screen: resize (no reflow) less rows with scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 7, .rows = 3, .max_scrollback = 2 }); + var s = try init(io, alloc, .{ .cols = 7, .rows = 3, .max_scrollback_bytes = 2 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH"; try s.testWriteString(str); @@ -6575,8 +7192,9 @@ test "Screen: resize (no reflow) less rows with scrollback" { test "Screen: resize (no reflow) less rows with empty trailing" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1\n2\n3\n4\n5\n6\n7\n8"; try s.testWriteString(str); @@ -6599,8 +7217,9 @@ test "Screen: resize (no reflow) less rows with empty trailing" { test "Screen: resize (no reflow) more rows with soft wrapping" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 2, .rows = 3, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 2, .rows = 3, .max_scrollback_bytes = 3 }); defer s.deinit(); const str = "1A2B\n3C4E\n5F6G"; try s.testWriteString(str); @@ -6640,8 +7259,9 @@ test "Screen: resize (no reflow) more rows with soft wrapping" { test "Screen: resize more rows no scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6667,8 +7287,9 @@ test "Screen: resize more rows no scrollback" { test "Screen: resize more rows with empty scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6694,8 +7315,9 @@ test "Screen: resize more rows with empty scrollback" { test "Screen: resize more rows with populated scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH"; try s.testWriteString(str); @@ -6713,7 +7335,7 @@ test "Screen: resize more rows with populated scrollback" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '4'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '4'), list_cell.cell.content.codepoint.data); } // Resize @@ -6725,7 +7347,7 @@ test "Screen: resize more rows with populated scrollback" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '4'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '4'), list_cell.cell.content.codepoint.data); } { @@ -6739,8 +7361,9 @@ test "Screen: resize more rows with populated scrollback" { test "Screen: resize more cols no reflow" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6768,8 +7391,9 @@ test "Screen: resize more cols no reflow" { test "Screen: resize more cols perfect split" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD2EFGH3IJKL"; try s.testWriteString(str); @@ -6786,8 +7410,9 @@ test "Screen: resize more cols perfect split" { test "Screen: resize (no reflow) more cols with scrollback scrolled up" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1\n2\n3\n4\n5\n6\n7\n8"; try s.testWriteString(str); @@ -6819,8 +7444,9 @@ test "Screen: resize (no reflow) more cols with scrollback scrolled up" { test "Screen: resize (no reflow) less cols with scrollback scrolled up" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1\n2\n3\n4\n5\n6\n7\n8"; try s.testWriteString(str); @@ -6863,8 +7489,9 @@ test "Screen: resize (no reflow) less cols with scrollback scrolled up" { test "Screen: resize more cols no reflow preserves semantic prompt" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); // Set one of the rows to be a prompt @@ -6907,8 +7534,9 @@ test "Screen: resize more cols no reflow preserves semantic prompt" { test "Screen: resize more cols with reflow that fits full width" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6928,7 +7556,7 @@ test "Screen: resize more cols with reflow that fits full width" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '2'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '2'), list_cell.cell.content.codepoint.data); } // Resize and verify we undid the soft wrap because we have space now @@ -6947,8 +7575,9 @@ test "Screen: resize more cols with reflow that fits full width" { test "Screen: resize more cols with reflow that ends in newline" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 6, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 6, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD2EFGH\n3IJKL"; try s.testWriteString(str); @@ -6968,7 +7597,7 @@ test "Screen: resize more cols with reflow that ends in newline" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '3'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '3'), list_cell.cell.content.codepoint.data); } // Resize and verify we undid the soft wrap because we have space now @@ -6985,15 +7614,16 @@ test "Screen: resize more cols with reflow that ends in newline" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '3'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '3'), list_cell.cell.content.codepoint.data); } } test "Screen: resize more cols with reflow that forces more wrapping" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD2EFGH\n3IJKL"; try s.testWriteString(str); @@ -7005,7 +7635,7 @@ test "Screen: resize more cols with reflow that forces more wrapping" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '2'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '2'), list_cell.cell.content.codepoint.data); } // Verify we soft wrapped @@ -7033,8 +7663,9 @@ test "Screen: resize more cols with reflow that forces more wrapping" { test "Screen: resize more cols with reflow that unwraps multiple times" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD2EFGH3IJKL"; try s.testWriteString(str); @@ -7046,7 +7677,7 @@ test "Screen: resize more cols with reflow that unwraps multiple times" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '3'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '3'), list_cell.cell.content.codepoint.data); } // Verify we soft wrapped @@ -7074,8 +7705,9 @@ test "Screen: resize more cols with reflow that unwraps multiple times" { test "Screen: resize more cols with populated scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL\n4ABCD5EFGH"; try s.testWriteString(str); @@ -7093,7 +7725,7 @@ test "Screen: resize more cols with populated scrollback" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '5'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '5'), list_cell.cell.content.codepoint.data); } // Resize @@ -7111,7 +7743,7 @@ test "Screen: resize more cols with populated scrollback" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u21, '5'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, '5'), list_cell.cell.content.codepoint.data); } } @@ -7123,11 +7755,12 @@ test "Screen: resize more cols bounded scrollback keeps viewport valid" { // resize path. A direct PageList resize repro does not hit the same bug. const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ + var s = try init(io, alloc, .{ .cols = 2, .rows = 10, - .max_scrollback = 10_000, + .max_scrollback_bytes = 10_000, }); defer s.deinit(); @@ -7158,7 +7791,7 @@ test "Screen: resize more cols bounded scrollback keeps viewport valid" { for (0..s.pages.cols) |x| { page.getRowAndCell(x, y).cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'A' }, + .content = .{ .codepoint = @bitCast(@as(u24, 'A')) }, }; } } @@ -7198,8 +7831,9 @@ test "Screen: resize more cols bounded scrollback keeps viewport valid" { test "Screen: resize more cols with reflow" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 2, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 2, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1ABC\n2DEF\n3ABC\n4DEF"; try s.testWriteString(str); @@ -7211,7 +7845,7 @@ test "Screen: resize more cols with reflow" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u32, 'E'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u32, 'E'), list_cell.cell.content.codepoint.data); } // Verify we soft wrapped @@ -7236,11 +7870,208 @@ test "Screen: resize more cols with reflow" { try testing.expectEqual(@as(size.CellCountInt, 2), s.cursor.y); } +test "Screen: resize errors preserve state" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + for (std.meta.tags(resize_tw.FailPoint)) |tag| { + const tw = resize_tw; + defer tw.end(.reset) catch unreachable; + + var s = try init(io, alloc, .{ + .cols = 10, + .rows = 3, + .max_scrollback_bytes = 0, + }); + defer s.deinit(); + + s.cursorSetSemanticContent(.{ .prompt = .initial }); + try s.testWriteString("> "); + s.cursorSetSemanticContent(.{ .input = .clear_explicit }); + try s.testWriteString("echo"); + try s.setAttribute(.{ .bold = {} }); + try s.startHyperlink("https://example.com", "resize"); + s.saved_cursor = .{ + .x = 1, + .y = 0, + .style = s.cursor.style, + .protected = s.cursor.protected, + .pending_wrap = s.cursor.pending_wrap, + .origin = false, + .charset = s.charset, + }; + + if (comptime build_options.kitty_graphics) { + s.kitty_images.dirty = false; + } + + // Keep a shallow copy for all non-page state and a byte-for-byte + // copy of the sole page so reference counts and prompt contents are + // covered as well. + try testing.expectEqual(s.pages.pages.first, s.pages.pages.last); + const before = s; + const before_viewport_pin = s.pages.viewport_pin.*; + const before_tracked_pins = s.pages.countTrackedPins(); + const before_page = try alloc.dupe( + u8, + s.pages.pages.first.?.page().memory, + ); + defer alloc.free(before_page); + + tw.errorAlways(tag, error.OutOfMemory); + try testing.expectError(error.OutOfMemory, s.resize(.{ + .cols = 20, + .rows = 4, + .prompt_redraw = .true, + })); + + try testing.expect(std.meta.eql(before.cursor, s.cursor)); + try testing.expect(std.meta.eql(before.saved_cursor, s.saved_cursor)); + try testing.expect(std.meta.eql(before.selection, s.selection)); + try testing.expect(std.meta.eql(before.charset, s.charset)); + try testing.expectEqual(before.protected_mode, s.protected_mode); + try testing.expect(std.meta.eql(before.kitty_keyboard, s.kitty_keyboard)); + try testing.expect(std.meta.eql(before.semantic_prompt, s.semantic_prompt)); + try testing.expectEqual(before.dirty, s.dirty); + try testing.expectEqual(before.pages.pages.first, s.pages.pages.first); + try testing.expectEqual(before.pages.pages.last, s.pages.pages.last); + try testing.expectEqual(before.pages.cols, s.pages.cols); + try testing.expectEqual(before.pages.rows, s.pages.rows); + try testing.expectEqual(before.pages.total_rows, s.pages.total_rows); + try testing.expectEqual(before.pages.viewport, s.pages.viewport); + try testing.expectEqual(before_viewport_pin, s.pages.viewport_pin.*); + try testing.expectEqual(before_tracked_pins, s.pages.countTrackedPins()); + if (std.valgrind.runningOnValgrind() > 0) { + // This assertion deliberately compares the complete raw page, + // including semantically irrelevant struct padding. + std.valgrind.memcheck.makeMemDefined(before_page); + std.valgrind.memcheck.makeMemDefined( + s.pages.pages.first.?.page().memory, + ); + } + try testing.expectEqualSlices( + u8, + before_page, + s.pages.pages.first.?.page().memory, + ); + if (comptime build_options.kitty_graphics) { + try testing.expectEqual( + before.kitty_images.dirty, + s.kitty_images.dirty, + ); + } + } +} + +test "Screen: resize cursor references when node survives" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ + .cols = 5, + .rows = 3, + .max_scrollback_bytes = 1000, + }); + defer s.deinit(); + + try s.setAttribute(.bold); + try s.startHyperlink("https://example.com/", "resize"); + try s.testWriteString("abc"); + + const original_node = s.cursor.page_pin.node; + const original_serial = original_node.serial; + { + const page = original_node.page(); + try testing.expectEqual( + 4, + page.styles.refCount(page.memory, s.cursor.style_id), + ); + try testing.expectEqual( + 4, + page.hyperlink_set.refCount(page.memory, s.cursor.hyperlink_id), + ); + } + + // A row-only resize grows the existing page without replacing the + // cursor's node. The temporary resize references must be released from + // this page after the cursor state is restored. + try s.resize(.{ .cols = 5, .rows = 4 }); + + try testing.expectEqual(original_node, s.cursor.page_pin.node); + try testing.expectEqual(original_serial, s.cursor.page_pin.node.serial); + { + const page = s.cursor.page_pin.node.page(); + try testing.expectEqual( + 4, + page.styles.refCount(page.memory, s.cursor.style_id), + ); + try testing.expectEqual( + 4, + page.hyperlink_set.refCount(page.memory, s.cursor.hyperlink_id), + ); + } +} + +test "Screen: resize cursor references when node is replaced" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ + .cols = 5, + .rows = 3, + .max_scrollback_bytes = 1000, + }); + defer s.deinit(); + + try s.setAttribute(.bold); + try s.startHyperlink("https://example.com/", "resize"); + try s.testWriteString("abc"); + + const original_node = s.cursor.page_pin.node; + const original_serial = original_node.serial; + { + const page = original_node.page(); + try testing.expectEqual( + 4, + page.styles.refCount(page.memory, s.cursor.style_id), + ); + try testing.expectEqual( + 4, + page.hyperlink_set.refCount(page.memory, s.cursor.hyperlink_id), + ); + } + + // A column resize with reflow replaces the page and remaps the tracked + // cursor pin. The old page owns the temporary references, so destroying + // it must account for them without attempting to release them afterward. + try s.resize(.{ .cols = 10, .rows = 3 }); + + try testing.expect( + s.cursor.page_pin.node != original_node or + s.cursor.page_pin.node.serial != original_serial, + ); + { + const page = s.cursor.page_pin.node.page(); + try testing.expectEqual( + 4, + page.styles.refCount(page.memory, s.cursor.style_id), + ); + try testing.expectEqual( + 4, + page.hyperlink_set.refCount(page.memory, s.cursor.hyperlink_id), + ); + } +} + test "Screen: resize more rows and cols with wrapping" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 2, .rows = 4, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 2, .rows = 4, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1A2B\n3C4D"; try s.testWriteString(str); @@ -7272,8 +8103,9 @@ test "Screen: resize more rows and cols with wrapping" { test "Screen: resize less rows no scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -7303,8 +8135,9 @@ test "Screen: resize less rows no scrollback" { test "Screen: resize less rows moving cursor" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -7316,7 +8149,7 @@ test "Screen: resize less rows moving cursor" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u32, 'I'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u32, 'I'), list_cell.cell.content.codepoint.data); } // Resize @@ -7343,8 +8176,9 @@ test "Screen: resize less rows moving cursor" { test "Screen: resize less rows with empty scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -7366,8 +8200,9 @@ test "Screen: resize less rows with empty scrollback" { test "Screen: resize less rows with populated scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH"; try s.testWriteString(str); @@ -7397,8 +8232,9 @@ test "Screen: resize less rows with populated scrollback" { test "Screen: resize less rows with full scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 3 }); defer s.deinit(); const str = "00000\n1ABCD\n2EFGH\n3IJKL\n4ABCD\n5EFGH"; try s.testWriteString(str); @@ -7437,8 +8273,9 @@ test "Screen: resize less rows with full scrollback" { test "Screen: resize less cols no reflow" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1AB\n2EF\n3IJ"; try s.testWriteString(str); @@ -7466,8 +8303,9 @@ test "Screen: resize less cols no reflow" { test "Screen: resize less cols with reflow but row space" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); const str = "1ABCD"; try s.testWriteString(str); @@ -7479,7 +8317,7 @@ test "Screen: resize less cols with reflow but row space" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u32, 'D'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u32, 'D'), list_cell.cell.content.codepoint.data); } try s.resize(.{ .cols = 3, .rows = 3 }); @@ -7504,8 +8342,9 @@ test "Screen: resize less cols with reflow but row space" { test "Screen: resize less cols with reflow with trimmed rows" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "3IJKL\n4ABCD\n5EFGH"; try s.testWriteString(str); @@ -7528,8 +8367,9 @@ test "Screen: resize less cols with reflow with trimmed rows" { test "Screen: resize less cols with reflow with trimmed rows and scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); const str = "3IJKL\n4ABCD\n5EFGH"; try s.testWriteString(str); @@ -7552,8 +8392,9 @@ test "Screen: resize less cols with reflow with trimmed rows and scrollback" { test "Screen: resize less cols with reflow previously wrapped" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "3IJKL4ABCD5EFGH"; try s.testWriteString(str); @@ -7585,8 +8426,9 @@ test "Screen: resize less cols with reflow previously wrapped" { test "Screen: resize less cols with reflow and scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1A\n2B\n3C\n4D\n5E"; try s.testWriteString(str); @@ -7598,7 +8440,7 @@ test "Screen: resize less cols with reflow and scrollback" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u32, 'E'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u32, 'E'), list_cell.cell.content.codepoint.data); } try s.resize(.{ .cols = 3, .rows = 3 }); @@ -7618,8 +8460,9 @@ test "Screen: resize less cols with reflow and scrollback" { test "Screen: resize less cols with reflow previously wrapped and scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 2 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 2 }); defer s.deinit(); const str = "1ABCD2EFGH3IJKL4ABCD5EFGH"; try s.testWriteString(str); @@ -7639,7 +8482,7 @@ test "Screen: resize less cols with reflow previously wrapped and scrollback" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u32, 'H'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u32, 'H'), list_cell.cell.content.codepoint.data); } try s.resize(.{ .cols = 3, .rows = 3 }); @@ -7665,15 +8508,16 @@ test "Screen: resize less cols with reflow previously wrapped and scrollback" { .x = s.cursor.x, .y = s.cursor.y, } }).?; - try testing.expectEqual(@as(u32, 'H'), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u32, 'H'), list_cell.cell.content.codepoint.data); } } test "Screen: resize less cols with scrollback keeps cursor row" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); const str = "1A\n2B\n3C\n4D\n5E"; try s.testWriteString(str); @@ -7701,8 +8545,9 @@ test "Screen: resize less cols with scrollback keeps cursor row" { test "Screen: resize more rows, less cols with reflow with scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 3 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 3 }); defer s.deinit(); const str = "1ABCD\n2EFGH3IJKL\n4MNOP"; try s.testWriteString(str); @@ -7742,8 +8587,9 @@ test "Screen: resize more rows, less cols with reflow with scrollback" { test "Screen: resize more rows then shrink again" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 10 }); defer s.deinit(); const str = "1ABC"; try s.testWriteString(str); @@ -7791,8 +8637,9 @@ test "Screen: resize more rows then shrink again" { test "Screen: resize less cols to eliminate wide char" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 2, .rows = 1, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 2, .rows = 1, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "😀"; try s.testWriteString(str); @@ -7805,7 +8652,7 @@ test "Screen: resize less cols to eliminate wide char" { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; try testing.expectEqual(Cell.Wide.wide, cell.wide); - try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint.data); } // Resize to 1 column can't fit a wide char. So it should be deleted. @@ -7818,7 +8665,7 @@ test "Screen: resize less cols to eliminate wide char" { { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } @@ -7826,8 +8673,9 @@ test "Screen: resize less cols to eliminate wide char" { test "Screen: resize less cols to wrap wide char" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 3, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 3, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "x😀"; try s.testWriteString(str); @@ -7840,7 +8688,7 @@ test "Screen: resize less cols to wrap wide char" { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; try testing.expectEqual(Cell.Wide.wide, cell.wide); - try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint.data); } { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; @@ -7865,8 +8713,9 @@ test "Screen: resize less cols to wrap wide char" { test "Screen: resize less cols to eliminate wide char with row space" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "😀"; try s.testWriteString(str); @@ -7879,7 +8728,7 @@ test "Screen: resize less cols to eliminate wide char with row space" { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; try testing.expectEqual(Cell.Wide.wide, cell.wide); - try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint.data); } { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; @@ -7898,7 +8747,8 @@ test "Screen: resize less cols to eliminate wide char with row space" { test "Screen: resize less cols reflows cursor after wrapped text" { const testing = std.testing; const alloc = testing.allocator; - var s = try Screen.init(alloc, .{ .cols = 50, .rows = 7, .max_scrollback = 0 }); + const io = testing.io; + var s = try Screen.init(io, alloc, .{ .cols = 50, .rows = 7, .max_scrollback_bytes = 0 }); defer s.deinit(); for (0..30) |_| try s.testWriteString("a"); @@ -7915,7 +8765,8 @@ test "Screen: resize less cols reflows cursor after wrapped text" { test "Screen: resize less cols reflows cursor after empty cells" { const testing = std.testing; const alloc = testing.allocator; - var s = try Screen.init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 0 }); + const io = testing.io; + var s = try Screen.init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("abc"); @@ -7933,8 +8784,9 @@ test "Screen: resize less cols reflows cursor after empty cells" { test "Screen: resize more cols with wide spacer head" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 3, .rows = 2, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 3, .rows = 2, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = " 😀"; try s.testWriteString(str); @@ -7974,7 +8826,7 @@ test "Screen: resize more cols with wide spacer head" { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; try testing.expectEqual(Cell.Wide.wide, cell.wide); - try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint.data); } { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 3, .y = 0 } }).?; @@ -7986,8 +8838,9 @@ test "Screen: resize more cols with wide spacer head" { test "Screen: resize more cols with wide spacer head multiple lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 3, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 3, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "xxxyy😀"; try s.testWriteString(str); @@ -8025,7 +8878,7 @@ test "Screen: resize more cols with wide spacer head multiple lines" { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 5, .y = 0 } }).?; const cell = list_cell.cell; try testing.expectEqual(Cell.Wide.wide, cell.wide); - try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint.data); } { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 6, .y = 0 } }).?; @@ -8037,8 +8890,9 @@ test "Screen: resize more cols with wide spacer head multiple lines" { test "Screen: resize more cols requiring a wide spacer head" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 2, .rows = 2, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "xx😀"; try s.testWriteString(str); @@ -8076,7 +8930,7 @@ test "Screen: resize more cols requiring a wide spacer head" { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 0, .y = 1 } }).?; const cell = list_cell.cell; try testing.expectEqual(Cell.Wide.wide, cell.wide); - try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '😀'), cell.content.codepoint.data); } { const list_cell = s.pages.getCell(.{ .screen = .{ .x = 1, .y = 1 } }).?; @@ -8088,8 +8942,9 @@ test "Screen: resize more cols requiring a wide spacer head" { test "Screen: resize more cols with cursor at prompt" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); // zig fmt: off @@ -8128,8 +8983,9 @@ test "Screen: resize more cols with cursor at prompt" { test "Screen: resize more cols with cursor not at prompt" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); // zig fmt: off @@ -8169,8 +9025,9 @@ test "Screen: resize more cols with cursor not at prompt" { test "Screen: resize with prompt_redraw last clears only one line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 4, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 4, .max_scrollback_bytes = 5 }); defer s.deinit(); // zig fmt: off @@ -8208,8 +9065,9 @@ test "Screen: resize with prompt_redraw last clears only one line" { test "Screen: resize with prompt_redraw last multiline prompt clears only last line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 5 }); defer s.deinit(); // Create a 3-line prompt: 1 initial + 2 continuation lines @@ -8248,8 +9106,9 @@ test "Screen: resize with prompt_redraw last multiline prompt clears only last l test "Screen: select untracked" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("ABC DEF\n 123\n456"); @@ -8268,8 +9127,9 @@ test "Screen: select untracked" { test "Screen: select replaces existing pins" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("ABC DEF\n 123\n456"); @@ -8294,8 +9154,9 @@ test "Screen: select replaces existing pins" { test "Screen: reselecting tracked selection preserves its pins" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 2, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 2, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.select(Selection.init( @@ -8311,8 +9172,9 @@ test "Screen: reselecting tracked selection preserves its pins" { test "Screen: selectAll" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); { @@ -8347,8 +9209,9 @@ test "Screen: selectAll" { test "Screen: selectLine" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("ABC DEF\n 123\n456"); @@ -8428,8 +9291,9 @@ test "Screen: selectLine" { test "Screen: selectLine across soft-wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString(" 12 34012 \n 123"); @@ -8454,8 +9318,9 @@ test "Screen: selectLine across soft-wrap" { test "Screen: selectLine across full soft-wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1ABCD2EFGH\n3IJKL"); @@ -8479,8 +9344,9 @@ test "Screen: selectLine across full soft-wrap" { test "Screen: selectLine across soft-wrap ignores blank lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString(" 12 34012 \n 123"); @@ -8539,8 +9405,9 @@ test "Screen: selectLine across soft-wrap ignores blank lines" { test "Screen: selectLine disabled whitespace trimming" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString(" 12 34012 \n 123"); @@ -8588,8 +9455,9 @@ test "Screen: selectLine disabled whitespace trimming" { test "Screen: selectLine with scrollback" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 2, .rows = 3, .max_scrollback = 5 }); + var s = try init(io, alloc, .{ .cols = 2, .rows = 3, .max_scrollback_bytes = 5 }); defer s.deinit(); try s.testWriteString("1A\n2B\n3C\n4D\n5E"); @@ -8632,8 +9500,9 @@ test "Screen: selectLine with scrollback" { test "Screen: selectLine semantic prompt boundary" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("ABCDE\n"); s.cursorSetSemanticContent(.{ .prompt = .initial }); @@ -8682,8 +9551,9 @@ test "Screen: selectLine semantic prompt boundary" { test "Screen: selectLine semantic prompt to input boundary" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Write prompt followed by user input on same row: "$>command" @@ -8731,8 +9601,9 @@ test "Screen: selectLine semantic prompt to input boundary" { test "Screen: selectLine semantic input to output boundary" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Row 0: user input @@ -8776,8 +9647,9 @@ test "Screen: selectLine semantic input to output boundary" { test "Screen: selectLine semantic mid-row boundary" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Single row with output then prompt then input: "out$>cmd" @@ -8844,8 +9716,9 @@ test "Screen: selectLine semantic mid-row boundary" { test "Screen: selectLine semantic boundary soft-wrap with mid-row transition" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Row 0: prompt "$ " + input "cmd" (soft-wraps) @@ -8913,8 +9786,9 @@ test "Screen: selectLine semantic boundary soft-wrap with mid-row transition" { test "Screen: selectLine semantic boundary disabled" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Write prompt followed by input @@ -8945,8 +9819,9 @@ test "Screen: selectLine semantic boundary disabled" { test "Screen: selectLine semantic boundary first cell of row" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Row 0: input that soft-wraps @@ -9001,8 +9876,9 @@ test "Screen: selectLine semantic boundary first cell of row" { test "Screen: selectLine semantic boundary across mixed-width pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 4, .rows = 2, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 4, .rows = 2, .max_scrollback_bytes = 0 }); defer s.deinit(); s.cursorSetSemanticContent(.{ .input = .clear_explicit }); @@ -9032,8 +9908,9 @@ test "Screen: selectLine semantic boundary across mixed-width pages" { test "Screen: selectLine semantic all same content" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // All prompt content that soft-wraps @@ -9066,15 +9943,19 @@ test "Screen: selectLine semantic all same content" { test "Screen: selectWord" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("ABC DEF\n 123\n456"); // Default boundary codepoints for word selection const boundary_codepoints = &[_]u21{ - 0, ' ', '\t', '\'', '"', '│', '`', '|', ':', ';', - ',', '(', ')', '[', ']', '{', '}', '<', '>', '$', + 0, ' ', '\t', '\'', '"', + '│', + '`', '|', ':', ';', ',', + '(', ')', '[', ']', '{', + '}', '<', '>', '$', }; // Outside of active area @@ -9187,15 +10068,19 @@ test "Screen: selectWord" { test "Screen: selectWord across soft-wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString(" 1234012\n 123"); // Default boundary codepoints for word selection const boundary_codepoints = &[_]u21{ - 0, ' ', '\t', '\'', '"', '│', '`', '|', ':', ';', - ',', '(', ')', '[', ']', '{', '}', '<', '>', '$', + 0, ' ', '\t', '\'', '"', + '│', + '`', '|', ':', ';', ',', + '(', ')', '[', ']', '{', + '}', '<', '>', '$', }; { @@ -9259,15 +10144,19 @@ test "Screen: selectWord across soft-wrap" { test "Screen: selectWord whitespace across soft-wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("1 1\n 123"); // Default boundary codepoints for word selection const boundary_codepoints = &[_]u21{ - 0, ' ', '\t', '\'', '"', '│', '`', '|', ':', ';', - ',', '(', ')', '[', ']', '{', '}', '<', '>', '$', + 0, ' ', '\t', '\'', '"', + '│', + '`', '|', ':', ';', ',', + '(', ')', '[', ']', '{', + '}', '<', '>', '$', }; // Going forward @@ -9325,11 +10214,15 @@ test "Screen: selectWord whitespace across soft-wrap" { test "Screen: selectWord with character boundary" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; // Default boundary codepoints for word selection const boundary_codepoints = &[_]u21{ - 0, ' ', '\t', '\'', '"', '│', '`', '|', ':', ';', - ',', '(', ')', '[', ']', '{', '}', '<', '>', '$', + 0, ' ', '\t', '\'', '"', + '│', + '`', '|', ':', ';', ',', + '(', ')', '[', ']', '{', + '}', '<', '>', '$', }; const cases = [_][]const u8{ @@ -9353,7 +10246,7 @@ test "Screen: selectWord with character boundary" { }; for (cases) |case| { - var s = try init(alloc, .{ .cols = 20, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString(case); @@ -9432,8 +10325,9 @@ test "Screen: selectWord with character boundary" { test "Screen: selectOutput" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 15, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 15, .max_scrollback_bytes = 0 }); defer s.deinit(); // Build content with cell-level semantic content: @@ -9528,8 +10422,9 @@ test "Screen: selectOutput" { test "Screen: selectionString basic" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -9553,8 +10448,9 @@ test "Screen: selectionString basic" { test "Screen: selectionString start outside of written area" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -9578,8 +10474,9 @@ test "Screen: selectionString start outside of written area" { test "Screen: selectionString end outside of written area" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -9603,8 +10500,9 @@ test "Screen: selectionString end outside of written area" { test "Screen: selectionString trim space" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1AB \n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -9640,8 +10538,9 @@ test "Screen: selectionString trim space" { test "Screen: selectionString trim empty line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1AB \n\n2EFGH\n3IJKL"; try s.testWriteString(str); @@ -9677,8 +10576,9 @@ test "Screen: selectionString trim empty line" { test "Screen: selectionString soft wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD2EFGH3IJKL"; try s.testWriteString(str); @@ -9702,8 +10602,9 @@ test "Screen: selectionString soft wrap" { test "Screen: selectionString wide char" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1A⚡"; try s.testWriteString(str); @@ -9757,8 +10658,9 @@ test "Screen: selectionString wide char" { test "Screen: selectionString wide char with header" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 3, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABC⚡"; try s.testWriteString(str); @@ -9783,8 +10685,9 @@ test "Screen: selectionString wide char with header" { test "Screen: selectionString empty with soft wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 2, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 2, .max_scrollback_bytes = 0 }); defer s.deinit(); // Let me describe the situation that caused this because this @@ -9816,8 +10719,9 @@ test "Screen: selectionString empty with soft wrap" { test "Screen: selectionString with zero width joiner" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 1, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 1, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "👨‍"; // this has a ZWJ try s.testWriteString(str); @@ -9826,7 +10730,7 @@ test "Screen: selectionString with zero width joiner" { { const pin = s.pages.pin(.{ .screen = .{ .y = 0, .x = 0 } }).?; const cell = pin.rowAndCell().cell; - try testing.expectEqual(@as(u21, 0x1F468), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F468), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.wide, cell.wide); const cps = pin.node.page().lookupGrapheme(cell).?; try testing.expectEqual(@as(usize, 1), cps.len); @@ -9852,8 +10756,9 @@ test "Screen: selectionString with zero width joiner" { test "Screen: selectionString, rectangle, basic" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 30, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 30, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = \\Lorem ipsum dolor @@ -9885,8 +10790,9 @@ test "Screen: selectionString, rectangle, basic" { test "Screen: selectionString, rectangle, w/EOL" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 30, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 30, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = \\Lorem ipsum dolor @@ -9920,8 +10826,9 @@ test "Screen: selectionString, rectangle, w/EOL" { test "Screen: selectionString, rectangle, more complex w/breaks" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 30, .rows = 8, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 30, .rows = 8, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = \\Lorem ipsum dolor @@ -9959,8 +10866,9 @@ test "Screen: selectionString, rectangle, more complex w/breaks" { test "Screen: selectionString multi-page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 2048 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 2048 }); defer s.deinit(); const first_page_size = s.pages.pages.first.?.capacity().rows; @@ -9993,8 +10901,9 @@ test "Screen: selectionString multi-page" { test "Screen: lineIterator" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD\n2EFGH"; try s.testWriteString(str); @@ -10024,8 +10933,9 @@ test "Screen: lineIterator" { test "Screen: lineIterator soft wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD2EFGH\n3ABCD"; try s.testWriteString(str); @@ -10056,8 +10966,9 @@ test "Screen: lineIterator soft wrap" { test "Screen: hyperlink start/end" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try testing.expect(s.cursor.hyperlink_id == 0); { @@ -10083,8 +10994,9 @@ test "Screen: hyperlink start/end" { test "Screen: hyperlink accepts its current values" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.startHyperlink("http://example.com", "current"); @@ -10098,8 +11010,9 @@ test "Screen: hyperlink accepts its current values" { test "Screen: implicit hyperlink ID wraps" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); s.cursor.hyperlink_implicit_id = std.math.maxInt(size.OffsetInt); @@ -10135,8 +11048,9 @@ test "Screen: implicit hyperlink ID wraps" { test "Screen: hyperlink reuse" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try testing.expect(s.cursor.hyperlink_id == 0); @@ -10169,12 +11083,13 @@ test "Screen: hyperlink reuse" { test "Screen: hyperlink cursor state on resize" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; // This test depends on underlying PageList implementation so // it may be invalid one day. It's here to document/verify the // current behavior. - var s = try init(alloc, .{ .cols = 5, .rows = 10, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 5, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); // Start a hyperlink @@ -10204,8 +11119,9 @@ test "Screen: hyperlink cursor state on resize" { test "Screen: cursorSetHyperlink OOM + URI too large for string alloc" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); // Start a hyperlink with a URI that just barely fits in the string alloc. @@ -10238,11 +11154,12 @@ test "Screen: cursorSetHyperlink OOM + URI too large for string alloc" { test "Screen: increaseCapacity cursor style ref count preserved" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); try s.setAttribute(.bold); @@ -10293,11 +11210,12 @@ test "Screen: increaseCapacity cursor style ref count preserved" { test "Screen: increaseCapacity cursor hyperlink ref count preserved" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); try s.startHyperlink("https://example.com/", null); @@ -10336,11 +11254,12 @@ test "Screen: increaseCapacity cursor hyperlink ref count preserved" { test "Screen: increaseCapacity cursor with both style and hyperlink preserved" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); @@ -10401,11 +11320,12 @@ test "Screen: increaseCapacity non-cursor page returns early" { // extra cursor accounting (style/hyperlink re-adding). const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ + var s = try init(io, alloc, .{ .cols = 80, .rows = 24, - .max_scrollback = 10000, + .max_scrollback_bytes = 10000, }); defer s.deinit(); @@ -10486,9 +11406,10 @@ test "Screen: cursorDown to page with insufficient capacity" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; // Small screen to make page boundary crossing easy to set up - var s = try init(alloc, .{ .cols = 10, .rows = 3, .max_scrollback = 1 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 3, .max_scrollback_bytes = 1 }); defer s.deinit(); // Scroll down enough to create a second page @@ -10555,9 +11476,10 @@ test "Screen setAttribute increases capacity when style map is full" { // returns OutOfSpace, manualStyleUpdate will split the page instead. const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; // Use a small screen with multiple rows - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 10 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10 }); defer s.deinit(); // Write content to multiple rows @@ -10604,11 +11526,12 @@ test "Screen setAttribute increases capacity when style map is full" { test "Screen setAttribute splits page on OutOfSpace at max styles" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ + var s = try init(io, alloc, .{ .cols = 10, .rows = 10, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); @@ -10670,42 +11593,12 @@ test "Screen setAttribute splits page on OutOfSpace at max styles" { try testing.expect(page_was_split); } -test "selectionString map allocation failure cleanup" { - // This test verifies that if toOwnedSlice fails when building - // the StringMap, we don't leak the already-allocated map.string. - const testing = std.testing; - const alloc = testing.allocator; - var s = try Screen.init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); - defer s.deinit(); - - try s.testWriteString("hello"); - - // Get a selection - const sel = Selection.init( - s.pages.pin(.{ .active = .{ .x = 0, .y = 0 } }).?, - s.pages.pin(.{ .active = .{ .x = 4, .y = 0 } }).?, - false, - ); - - // Trigger allocation failure on toOwnedSlice - var map: StringMap = undefined; - selectionString_tw.errorAlways(.copy_map, error.OutOfMemory); - const result = s.selectionString(alloc, .{ - .sel = sel, - .map = &map, - }); - try testing.expectError(error.OutOfMemory, result); - try selectionString_tw.end(.reset); - - // If this test passes without memory leaks (when run with testing.allocator), - // it means the errdefer properly cleaned up map.string when toOwnedSlice failed. -} - test "Screen: promptClickMove line right basic" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -10731,8 +11624,9 @@ test "Screen: promptClickMove line right basic" { test "Screen: promptClickMove line right cursor not on input" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -10758,8 +11652,9 @@ test "Screen: promptClickMove line right cursor not on input" { test "Screen: promptClickMove line right click on same position" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -10784,8 +11679,9 @@ test "Screen: promptClickMove line right click on same position" { test "Screen: promptClickMove line right skips non-input cells" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -10816,8 +11712,9 @@ test "Screen: promptClickMove line right skips non-input cells" { test "Screen: promptClickMove line right soft-wrapped line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -10854,8 +11751,9 @@ test "Screen: promptClickMove line right soft-wrapped line" { test "Screen: promptClickMove disabled when click is none" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Click mode is .none by default (disabled) @@ -10880,8 +11778,9 @@ test "Screen: promptClickMove disabled when click is none" { test "Screen: promptClickMove line right stops at hard wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -10912,8 +11811,9 @@ test "Screen: promptClickMove line right stops at hard wrap" { test "Screen: promptClickMove line right stops at non-continuation row" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -10959,8 +11859,9 @@ test "Screen: promptClickMove line right stops at non-continuation row" { test "Screen: promptClickMove line left basic" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -10986,8 +11887,9 @@ test "Screen: promptClickMove line left basic" { test "Screen: promptClickMove line left skips non-input cells" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -11018,8 +11920,9 @@ test "Screen: promptClickMove line left skips non-input cells" { test "Screen: promptClickMove line left soft-wrapped line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -11056,8 +11959,9 @@ test "Screen: promptClickMove line left soft-wrapped line" { test "Screen: promptClickMove line left stops at hard wrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -11088,11 +11992,12 @@ test "Screen: promptClickMove line left stops at hard wrap" { test "Screen: promptClickMove click right of input same line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; // Set up: "> hello" where "> " is prompt and "hello" is input // Clicking to the right of the 'o' should move cursor past the input - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -11118,8 +12023,9 @@ test "Screen: promptClickMove click right of input same line" { test "Screen: promptClickMove click right of input cursor at end" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -11144,8 +12050,9 @@ test "Screen: promptClickMove click right of input cursor at end" { test "Screen: promptClickMove click right of input on lower line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -11173,8 +12080,9 @@ test "Screen: promptClickMove click right of input on lower line" { test "Screen: promptClickMove click right of input cursor at end lower line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -11198,8 +12106,9 @@ test "Screen: promptClickMove click right of input cursor at end lower line" { test "Screen: promptClickMove click right of input cursor on last char" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try init(alloc, .{ .cols = 20, .rows = 5, .max_scrollback = 0 }); + var s = try init(io, alloc, .{ .cols = 20, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); // Enable line click mode @@ -11221,3 +12130,222 @@ test "Screen: promptClickMove click right of input cursor on last char" { try testing.expectEqual(@as(usize, 1), result.right); try testing.expectEqual(@as(usize, 0), result.left); } + +test "Screen: cursorScrollRegionUp recycled row has default metadata" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); + defer s.deinit(); + try s.testWriteString("1ABCD\n2EFGH\n3IJKL\n4MNOP\n5QRST"); + + // Simulate the top region row being part of a soft-wrapped, + // prompt-marked line. Its Row storage is recycled as the new + // blank cursor row and must not retain the metadata. + { + const rac = s.pages.getCell(.{ .active = .{} }).?; + rac.row.wrap = true; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt; + } + + s.cursorAbsolute(1, 2); + try s.cursorScrollRegionUp(2); + + { + const rac = s.pages.getCell(.{ .active = .{ .y = 2 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } + { + const contents = try s.dumpStringAlloc(alloc, .{ .screen = .{} }); + defer alloc.free(contents); + try testing.expectEqualStrings("2EFGH\n3IJKL\n\n4MNOP\n5QRST", contents); + } +} + +test "Screen: cursorScrollRegionUp cross-page recycled row has default metadata" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10 }); + defer s.deinit(); + + // We need to get the active area to span two pages so that the + // scroll region does too, exercising the slow path (eraseRowBounded). + const first_page_size = s.pages.pages.first.?.capacity().rows; + s.pages.pages.first.?.page().pauseIntegrityChecks(true); + for (0..first_page_size - 3) |_| try s.testWriteString("\n"); + s.pages.pages.first.?.page().pauseIntegrityChecks(false); + try s.testWriteString("1A\n2B\n3C\n4D\n5E"); + + // The first row of the last page is the Row storage that ends up + // recycled as the blank region-bottom row: the erased row's + // storage stays on the first page (receiving this row's content + // via clone) while this storage is cleared for the blank row. + { + const rac = s.pages.getCell(.{ .active = .{ .y = 3 } }).?; + rac.row.wrap = true; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt; + } + + // Region rows 1-3 with the cursor on the region bottom, which is + // on the second page while the region top is on the first page. + s.cursorAbsolute(0, 3); + try s.cursorScrollRegionUp(2); + + { + const rac = s.pages.getCell(.{ .active = .{ .y = 3 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } + { + const contents = try s.dumpStringAlloc(alloc, .{ .viewport = .{} }); + defer alloc.free(contents); + try testing.expectEqualStrings("1A\n3C\n4D\n\n5E", contents); + } +} + +test "Screen: cursorScrollAbove cross-page recycled row has default metadata" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 10 }); + defer s.deinit(); + + // Get the cursor page and the last page to differ so that + // cursorScrollAbove takes the cross-page rotate path. + const first_page_size = s.pages.pages.first.?.capacity().rows; + s.pages.pages.first.?.page().pauseIntegrityChecks(true); + for (0..first_page_size - 3) |_| try s.testWriteString("\n"); + s.pages.pages.first.?.page().pauseIntegrityChecks(false); + try s.testWriteString("1A\n2B\n3C\n4D\n5E"); + s.cursorAbsolute(0, 1); + try testing.expect(s.cursor.page_pin.node == s.pages.pages.first.?); + try testing.expect(s.pages.pages.first.?.next != null); + + // The last row of the cursor page is the Row storage that gets + // recycled as the new blank row below the cursor after its content + // is moved down to the next page. + { + const rac = s.pages.getCell(.{ .active = .{ .y = 2 } }).?; + rac.row.wrap = true; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt; + } + + try s.cursorScrollAbove(); + + // One row scrolled into history, so the blank row is at active + // y=1 (just below the cursor's original row). + { + const rac = s.pages.getCell(.{ .active = .{ .y = 1 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } + { + const contents = try s.dumpStringAlloc(alloc, .{ .viewport = .{} }); + defer alloc.free(contents); + try testing.expectEqualStrings("2B\n\n3C\n4D\n5E", contents); + } +} + +test "Screen: cursorDownScroll no scrollback recycled row has default metadata" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 0 }); + defer s.deinit(); + try s.testWriteString("1ABCD\n2EFGH\n3IJKL"); + + { + const rac = s.pages.getCell(.{ .active = .{} }).?; + rac.row.wrap = true; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt; + } + + s.cursorAbsolute(0, 2); + try s.cursorDownScroll(); + + { + const rac = s.pages.getCell(.{ .active = .{ .y = 2 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "Screen: cursorDownScroll single row no scrollback resets metadata" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ .cols = 5, .rows = 1, .max_scrollback_bytes = 0 }); + defer s.deinit(); + try s.testWriteString("1ABCD"); + + { + const rac = s.pages.getCell(.{ .active = .{} }).?; + rac.row.wrap = true; + rac.row.wrap_continuation = true; + rac.row.semantic_prompt = .prompt; + } + + try s.cursorDownScroll(); + + { + const rac = s.pages.getCell(.{ .active = .{} }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "Screen: selectLine does not join lines across a recycled row" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var s = try init(io, alloc, .{ .cols = 6, .rows = 5, .max_scrollback_bytes = 0 }); + defer s.deinit(); + + // A soft-wrapped line across rows 0 and 1: row 0 gets wrap=true. + try s.testWriteString("AAAAAAA"); + + // Scroll a region of rows 0-2 up by one: row 0 is discarded and + // its Row storage recycled as the blank row 2. + s.cursorAbsolute(0, 2); + try s.cursorScrollRegionUp(2); + + // Write unrelated single-line words on the recycled row and below. + s.cursorAbsolute(0, 2); + try s.testWriteString("world"); + s.cursorAbsolute(0, 3); + try s.testWriteString("hello"); + + // Selecting the line "world" must not extend into "hello": these + // are separate hard lines. A stale wrap flag on the recycled row + // would join them. + { + var sel = s.selectLine(.{ .pin = s.pages.pin(.{ .active = .{ + .x = 0, + .y = 2, + } }).? }).?; + defer sel.deinit(&s); + const contents = try s.selectionString(alloc, .{ + .sel = sel, + .trim = false, + }); + defer alloc.free(contents); + try testing.expectEqualStrings("world", contents); + } +} diff --git a/vendor/libghostty-vt/src/terminal/ScreenSet.zig b/vendor/libghostty-vt/src/terminal/ScreenSet.zig index 4d3689e3..28fced98 100644 --- a/vendor/libghostty-vt/src/terminal/ScreenSet.zig +++ b/vendor/libghostty-vt/src/terminal/ScreenSet.zig @@ -30,19 +30,20 @@ active: *Screen, /// All screens that are initialized. all: std.EnumMap(Key, *Screen), -/// Monotonic generation counter for each screen key. This changes whenever a -/// screen is removed so external handles can distinguish a newly initialized -/// screen from stale references into destroyed screen storage. +/// Monotonic generation counter for each screen key. This changes whenever +/// screen storage is removed or replaced so external handles can distinguish a +/// newly initialized screen from stale references into destroyed storage. generations: std.EnumMap(Key, usize), pub fn init( + io: std.Io, alloc: Allocator, opts: Screen.Options, ) Allocator.Error!ScreenSet { // We need to initialize our initial primary screen const screen = try alloc.create(Screen); errdefer alloc.destroy(screen); - screen.* = try .init(alloc, opts); + screen.* = try .init(io, alloc, opts); return .{ .active_key = .primary, .active = screen, @@ -73,6 +74,7 @@ pub fn generation(self: *const ScreenSet, key: Key) usize { /// Get the screen for the given key, initializing it if necessary. pub fn getInit( self: *ScreenSet, + io: std.Io, alloc: Allocator, key: Key, opts: Screen.Options, @@ -80,7 +82,7 @@ pub fn getInit( if (self.get(key)) |screen| return screen; const screen = try alloc.create(Screen); errdefer alloc.destroy(screen); - screen.* = try .init(alloc, opts); + screen.* = try .init(io, alloc, opts); self.all.put(key, screen); return screen; } @@ -108,14 +110,15 @@ pub fn switchTo(self: *ScreenSet, key: Key) void { test ScreenSet { const alloc = testing.allocator; - var set: ScreenSet = try .init(alloc, .default); + const io = testing.io; + var set: ScreenSet = try .init(io, alloc, .default); defer set.deinit(alloc); try testing.expectEqual(.primary, set.active_key); try testing.expectEqual(@as(usize, 0), set.generation(.primary)); try testing.expectEqual(@as(usize, 0), set.generation(.alternate)); // Initialize a secondary screen - _ = try set.getInit(alloc, .alternate, .default); + _ = try set.getInit(io, alloc, .alternate, .default); try testing.expectEqual(@as(usize, 0), set.generation(.alternate)); set.switchTo(.alternate); @@ -124,7 +127,8 @@ test ScreenSet { test "ScreenSet generations" { const alloc = testing.allocator; - var set: ScreenSet = try .init(alloc, .default); + const io = testing.io; + var set: ScreenSet = try .init(io, alloc, .default); defer set.deinit(alloc); try testing.expectEqual(@as(usize, 0), set.generation(.primary)); @@ -135,7 +139,7 @@ test "ScreenSet generations" { try testing.expectEqual(@as(usize, 0), set.generation(.alternate)); // Initializing a screen doesn't change the generation. - _ = try set.getInit(alloc, .alternate, .default); + _ = try set.getInit(io, alloc, .alternate, .default); try testing.expectEqual(@as(usize, 0), set.generation(.alternate)); const alternate_generation = set.generation(.alternate); @@ -144,7 +148,7 @@ test "ScreenSet generations" { // Reinitializing keeps the generation from the last removal, so stale // handles can distinguish the new screen from the destroyed screen. - _ = try set.getInit(alloc, .alternate, .default); + _ = try set.getInit(io, alloc, .alternate, .default); try testing.expectEqual(alternate_generation +% 1, set.generation(.alternate)); try testing.expectEqual(@as(usize, 0), set.generation(.primary)); } diff --git a/vendor/libghostty-vt/src/terminal/Selection.zig b/vendor/libghostty-vt/src/terminal/Selection.zig index be6854e5..398496da 100644 --- a/vendor/libghostty-vt/src/terminal/Selection.zig +++ b/vendor/libghostty-vt/src/terminal/Selection.zig @@ -512,7 +512,7 @@ pub fn adjust( test "Selection: adjust right" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A1234\nB5678\nC1234\nD5678"); @@ -579,7 +579,7 @@ test "Selection: adjust right" { test "Selection: adjust left" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A1234\nB5678\nC1234\nD5678"); @@ -628,7 +628,7 @@ test "Selection: adjust left" { test "Selection: adjust left skips blanks" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A1234\nB5678\nC12\nD56"); @@ -677,7 +677,7 @@ test "Selection: adjust left skips blanks" { test "Selection: adjust up" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A\nB\nC\nD\nE"); @@ -724,7 +724,7 @@ test "Selection: adjust up" { test "Selection: adjust down" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A\nB\nC\nD\nE"); @@ -771,7 +771,7 @@ test "Selection: adjust down" { test "Selection: adjust down with not full screen" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A\nB\nC"); @@ -799,7 +799,7 @@ test "Selection: adjust down with not full screen" { test "Selection: adjust home" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A\nB\nC"); @@ -827,7 +827,7 @@ test "Selection: adjust home" { test "Selection: adjust end with not full screen" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A\nB\nC"); @@ -855,7 +855,7 @@ test "Selection: adjust end with not full screen" { test "Selection: adjust beginning of line" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 8, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 8, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A12 B34\nC12 D34"); @@ -925,7 +925,7 @@ test "Selection: adjust beginning of line" { test "Selection: adjust end of line" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 8, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 8, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("A12 B34\nC12 D34"); @@ -994,8 +994,9 @@ test "Selection: adjust end of line" { test "Selection: order, standard" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 100, .rows = 100, .max_scrollback = 1 }); + var s = try Screen.init(io, alloc, .{ .cols = 100, .rows = 100, .max_scrollback_bytes = 1 }); defer s.deinit(); { @@ -1057,10 +1058,10 @@ test "Selection: order, standard" { test "Selection: rectangle corners clamp across mixed-width pages" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 4, .rows = 2, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); @@ -1084,8 +1085,9 @@ test "Selection: rectangle corners clamp across mixed-width pages" { test "Selection: order, rectangle" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 100, .rows = 100, .max_scrollback = 1 }); + var s = try Screen.init(io, alloc, .{ .cols = 100, .rows = 100, .max_scrollback_bytes = 1 }); defer s.deinit(); // Conventions: @@ -1197,7 +1199,7 @@ test "Selection: order, rectangle" { test "topLeft" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); { // forward @@ -1260,7 +1262,7 @@ test "topLeft" { test "bottomRight" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); { // forward @@ -1323,7 +1325,7 @@ test "bottomRight" { test "ordered" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); { // forward @@ -1404,7 +1406,7 @@ test "ordered" { test "Selection: contains" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .max_scrollback_bytes = 0 }); defer s.deinit(); { const sel = Selection.init( @@ -1450,7 +1452,7 @@ test "Selection: contains" { test "Selection: contains, rectangle" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 15, .rows = 15, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 15, .rows = 15, .max_scrollback_bytes = 0 }); defer s.deinit(); { const sel = Selection.init( @@ -1512,7 +1514,7 @@ test "Selection: contains, rectangle" { test "Selection: containedRow" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); { @@ -1632,10 +1634,10 @@ test "Selection: containedRow" { test "Selection: containedRow clamps mixed-width pages" { const testing = std.testing; - var s = try Screen.init(testing.allocator, .{ + var s = try Screen.init(testing.io, testing.allocator, .{ .cols = 4, .rows = 3, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); diff --git a/vendor/libghostty-vt/src/terminal/SelectionGesture.zig b/vendor/libghostty-vt/src/terminal/SelectionGesture.zig index 2be1e8aa..a842b247 100644 --- a/vendor/libghostty-vt/src/terminal/SelectionGesture.zig +++ b/vendor/libghostty-vt/src/terminal/SelectionGesture.zig @@ -83,13 +83,6 @@ const point = @import("point.zig"); const freestanding_wasm = builtin.target.cpu.arch == .wasm32 and builtin.target.os.tag == .freestanding; -/// Monotonic timestamp type for click-repeat detection. -/// -/// Freestanding wasm cannot reference std.time.Instant because Zig's stdlib -/// Instant type depends on POSIX timespec for that target, so represent the C -/// API nanosecond timestamp directly as a u64 there. -pub const Time = if (freestanding_wasm) u64 else std.time.Instant; - /// The tracked pin of the initial left click along with the screen /// that the pin is part of. left_click_pin: ?*Pin, @@ -100,7 +93,7 @@ left_click_screen_generation: usize, /// The left click time was the last time the left click was done, if the /// caller could provide one. If this is null then we only support single clicks. left_click_count: u3, -left_click_time: ?Time, +left_click_time: ?std.Io.Timestamp, /// The selection behavior chosen for the active left-click gesture. left_click_behavior: Behavior, @@ -232,7 +225,7 @@ pub const Press = struct { /// backwards timestamps reset the repeat sequence. /// This can be null if you're on a system that doesn't support /// time for some reason. In that case, we only support single clicks. - time: ?Time, + time: ?std.Io.Timestamp, /// The cell where the click was. /// @@ -666,14 +659,11 @@ fn pressRepeat( self.left_click_behavior = p.behaviors[self.left_click_count - 1]; } -fn timeSince(time: Time, prev_time: Time) ?u64 { - if (comptime freestanding_wasm) { - if (time < prev_time) return null; - return time - prev_time; - } +fn timeSince(time: std.Io.Timestamp, prev_time: std.Io.Timestamp) ?u64 { + const delta = prev_time.durationTo(time).toNanoseconds(); + if (delta < 0) return null; - if (time.order(prev_time) == .lt) return null; - return time.since(prev_time); + return @as(u64, @intCast(@min(delta, std.math.maxInt(u64)))); } /// Convert a caller-provided floating-point position to a pixel coordinate. @@ -947,7 +937,7 @@ fn untrackPin(self: *SelectionGesture, t: *Terminal) void { screen.pages.untrackPin(pin); } -fn testPress(t: *Terminal, x: u16, y: u32, time: ?std.time.Instant) Press { +fn testPress(t: *Terminal, x: u16, y: u32, time: ?std.Io.Timestamp) Press { return .{ .time = time, .pin = t.screens.active.pages.pin(.{ .active = .{ @@ -962,16 +952,6 @@ fn testPress(t: *Terminal, x: u16, y: u32, time: ?std.time.Instant) Press { }; } -fn testInstant(ns: u64) std.time.Instant { - return switch (builtin.os.tag) { - .windows, .uefi, .wasi => .{ .timestamp = ns }, - else => .{ .timestamp = .{ - .sec = @intCast(ns / std.time.ns_per_s), - .nsec = @intCast(ns % std.time.ns_per_s), - } }, - }; -} - fn testDrag(t: *Terminal, x: u16, y: u32, xpos: f64, ypos: f64) Drag { return .{ .pin = t.screens.active.pages.pin(.{ .active = .{ @@ -1049,7 +1029,7 @@ fn testDragSelection( .padding_left = 5, .screen_height = 110, }; - var screen = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var screen = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer screen.deinit(); const click_pin = screen.pages.pin(.{ @@ -1110,7 +1090,7 @@ fn testDragSelectionIsNull( .padding_left = 5, .screen_height = 110, }; - var screen = try Screen.init(testing.allocator, .{ .cols = 10, .rows = 5, .max_scrollback = 0 }); + var screen = try Screen.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5, .max_scrollback_bytes = 0 }); defer screen.deinit(); const click_pin = screen.pages.pin(.{ @@ -1430,13 +1410,13 @@ test "SelectionGesture rectangle drag selection logic" { } test "SelectionGesture press records initial click" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, testPress(&t, 1, 2, time)); try testing.expectEqual(@as(u3, 1), gesture.left_click_count); @@ -1447,14 +1427,14 @@ test "SelectionGesture press records initial click" { } test "SelectionGesture press returns standard click selections" { - var t = try Terminal.init(testing.allocator, .{ .cols = 20, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 20, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("alpha beta\none two"); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); var event = testPress(&t, 1, 0, time); event.word_boundary_codepoints = &.{ ' ' }; @@ -1474,14 +1454,14 @@ test "SelectionGesture press returns standard click selections" { } test "SelectionGesture press behaviors choose press and drag behavior" { - var t = try Terminal.init(testing.allocator, .{ .cols = 20, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 20, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("alpha beta\none two\nthree four"); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); var event = testPress(&t, 1, 0, time); event.behaviors = &.{ .cell, .line, .word }; event.word_boundary_codepoints = &.{ ' ' }; @@ -1506,7 +1486,7 @@ test "SelectionGesture press behaviors choose press and drag behavior" { } test "SelectionGesture output behavior selects and drags semantic output" { - var t = try Terminal.init(testing.allocator, .{ .cols = 10, .rows = 6 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 10, .rows = 6 }); defer t.deinit(testing.allocator); const screen = t.screens.active; @@ -1522,7 +1502,7 @@ test "SelectionGesture output behavior selects and drags semantic output" { var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var event = testPress(&t, 1, 0, try std.time.Instant.now()); + var event = testPress(&t, 1, 0, std.Io.Timestamp.now(testing.io, .awake)); event.behaviors = &.{ .output, .word, .line }; const press_selection = (try gesture.press(&t, event)).?; @@ -1542,13 +1522,13 @@ test "SelectionGesture output behavior selects and drags semantic output" { } test "SelectionGesture drag returns selection and records autoscroll" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var press_event = testPress(&t, 1, 1, try std.time.Instant.now()); + var press_event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); press_event.xpos = 10; _ = try gesture.press(&t, press_event); @@ -1570,13 +1550,13 @@ test "SelectionGesture drag returns selection and records autoscroll" { } test "SelectionGesture drag clamps unrepresentable positions" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var press_event = testPress(&t, 1, 1, try std.time.Instant.now()); + var press_event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); press_event.xpos = 10; _ = try gesture.press(&t, press_event); @@ -1594,13 +1574,13 @@ test "SelectionGesture drag clamps unrepresentable positions" { } test "SelectionGesture drag saturates overflowing geometry" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); var drag_event = testDrag(&t, 1, 1, 10, 50); drag_event.geometry.columns = std.math.maxInt(u32); drag_event.geometry.cell_width = std.math.maxInt(u32); @@ -1608,13 +1588,13 @@ test "SelectionGesture drag saturates overflowing geometry" { } test "SelectionGesture drag rejects empty geometry" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); var drag_event = testDrag(&t, 1, 1, 10, 50); drag_event.geometry.columns = 0; drag_event.geometry.cell_width = 0; @@ -1622,13 +1602,13 @@ test "SelectionGesture drag rejects empty geometry" { } test "SelectionGesture release clears autoscroll and records drag" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); try testing.expectEqual(false, gesture.left_click_dragged); _ = gesture.drag(&t, testDrag(&t, 1, 1, 10, 1)); @@ -1643,16 +1623,16 @@ test "SelectionGesture release clears autoscroll and records drag" { } test "SelectionGesture release with invalidated click records drag" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); try testing.expectEqual(false, gesture.left_click_dragged); - _ = try t.screens.getInit(testing.allocator, .alternate, .{ + _ = try t.screens.getInit(testing.io, testing.allocator, .alternate, .{ .cols = t.cols, .rows = t.rows, }); @@ -1664,13 +1644,13 @@ test "SelectionGesture release with invalidated click records drag" { } test "SelectionGesture same-cell threshold selection records drag" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var press_event = testPress(&t, 1, 1, try std.time.Instant.now()); + var press_event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); press_event.xpos = 10; _ = try gesture.press(&t, press_event); try testing.expectEqual(false, gesture.left_click_dragged); @@ -1685,7 +1665,7 @@ test "SelectionGesture same-cell threshold selection records drag" { } test "SelectionGesture drag without press returns null" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; @@ -1696,13 +1676,13 @@ test "SelectionGesture drag without press returns null" { } test "SelectionGesture drag autoscroll edge boundaries" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var press_event = testPress(&t, 1, 1, try std.time.Instant.now()); + var press_event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); press_event.xpos = 10; _ = try gesture.press(&t, press_event); @@ -1720,13 +1700,13 @@ test "SelectionGesture drag autoscroll edge boundaries" { } test "SelectionGesture autoscroll tick scrolls and continues drag" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var press_event = testPress(&t, 1, 1, try std.time.Instant.now()); + var press_event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); press_event.xpos = 10; _ = try gesture.press(&t, press_event); @@ -1744,7 +1724,7 @@ test "SelectionGesture autoscroll tick scrolls and continues drag" { } test "SelectionGesture autoscroll tick resolves drag pin after scrolling" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 3, .max_scrollback = 10 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 3, .max_scrollback_bytes = 10 }); defer t.deinit(testing.allocator); try t.printString("1111\n2222\n3333\n4444\n5555"); t.scrollViewport(.{ .delta = -2 }); @@ -1752,7 +1732,7 @@ test "SelectionGesture autoscroll tick resolves drag pin after scrolling" { var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var press_event = testPress(&t, 1, 1, try std.time.Instant.now()); + var press_event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); press_event.xpos = 10; _ = try gesture.press(&t, press_event); @@ -1773,20 +1753,20 @@ test "SelectionGesture autoscroll tick resolves drag pin after scrolling" { } test "SelectionGesture autoscroll tick stops with invalidated click" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var press_event = testPress(&t, 1, 1, try std.time.Instant.now()); + var press_event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); press_event.xpos = 10; _ = try gesture.press(&t, press_event); _ = gesture.drag(&t, testDrag(&t, 2, 1, 20, 1)); try testing.expectEqual(.up, gesture.left_drag_autoscroll); - _ = try t.screens.getInit(testing.allocator, .alternate, .{ + _ = try t.screens.getInit(testing.io, testing.allocator, .alternate, .{ .cols = t.cols, .rows = t.rows, }); @@ -1798,14 +1778,14 @@ test "SelectionGesture autoscroll tick stops with invalidated click" { } test "SelectionGesture deep press selects word and consumes drag" { - var t = try Terminal.init(testing.allocator, .{ .cols = 20, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 20, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("alpha beta"); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - _ = try gesture.press(&t, testPress(&t, 1, 0, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 0, std.Io.Timestamp.now(testing.io, .awake))); _ = gesture.drag(&t, testDrag(&t, 1, 0, 10, 1)); try testing.expectEqual(.up, gesture.left_drag_autoscroll); @@ -1819,7 +1799,7 @@ test "SelectionGesture deep press selects word and consumes drag" { false, ), sel); try testing.expectEqual(@as(u3, 0), gesture.left_click_count); - try testing.expectEqual(@as(?std.time.Instant, null), gesture.left_click_time); + try testing.expectEqual(@as(?std.Io.Timestamp, null), gesture.left_click_time); try testing.expectEqual(true, gesture.left_click_dragged); try testing.expectEqual(.none, gesture.left_drag_autoscroll); try testing.expect(gesture.left_click_pin == null); @@ -1830,20 +1810,20 @@ test "SelectionGesture deep press selects word and consumes drag" { } test "SelectionGesture drag with invalidated click returns null" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var press_event = testPress(&t, 1, 1, try std.time.Instant.now()); + var press_event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); press_event.xpos = 10; _ = try gesture.press(&t, press_event); _ = gesture.drag(&t, testDrag(&t, 2, 1, 20, 1)); try testing.expectEqual(.up, gesture.left_drag_autoscroll); - _ = try t.screens.getInit(testing.allocator, .alternate, .{ + _ = try t.screens.getInit(testing.io, testing.allocator, .alternate, .{ .cols = t.cols, .rows = t.rows, }); @@ -1854,14 +1834,14 @@ test "SelectionGesture drag with invalidated click returns null" { } test "SelectionGesture double-click drag selects by word" { - var t = try Terminal.init(testing.allocator, .{ .cols = 20, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 20, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("alpha beta gamma"); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, testPress(&t, 1, 0, time)); _ = try gesture.press(&t, testPress(&t, 1, 0, time)); @@ -1877,14 +1857,14 @@ test "SelectionGesture double-click drag selects by word" { } test "SelectionGesture double-click drag selects by word backwards" { - var t = try Terminal.init(testing.allocator, .{ .cols = 20, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 20, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("alpha beta gamma"); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, testPress(&t, 7, 0, time)); _ = try gesture.press(&t, testPress(&t, 7, 0, time)); @@ -1900,14 +1880,14 @@ test "SelectionGesture double-click drag selects by word backwards" { } test "SelectionGesture double-click drag on empty cell selects nearest word" { - var t = try Terminal.init(testing.allocator, .{ .cols = 20, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 20, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("alpha beta"); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, testPress(&t, 1, 0, time)); _ = try gesture.press(&t, testPress(&t, 1, 0, time)); @@ -1923,14 +1903,14 @@ test "SelectionGesture double-click drag on empty cell selects nearest word" { } test "SelectionGesture triple-click drag selects by line" { - var t = try Terminal.init(testing.allocator, .{ .cols = 20, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 20, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("alpha beta\none two\nthree four"); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, testPress(&t, 1, 0, time)); _ = try gesture.press(&t, testPress(&t, 1, 0, time)); _ = try gesture.press(&t, testPress(&t, 1, 0, time)); @@ -1945,14 +1925,14 @@ test "SelectionGesture triple-click drag selects by line" { } test "SelectionGesture triple-click drag selects by line backwards" { - var t = try Terminal.init(testing.allocator, .{ .cols = 20, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 20, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("alpha beta\none two\nthree four"); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, testPress(&t, 2, 2, time)); _ = try gesture.press(&t, testPress(&t, 2, 2, time)); _ = try gesture.press(&t, testPress(&t, 2, 2, time)); @@ -1967,13 +1947,13 @@ test "SelectionGesture triple-click drag selects by line backwards" { } test "SelectionGesture repeat increments click count" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, testPress(&t, 1, 1, time)); _ = try gesture.press(&t, testPress(&t, 1, 1, time)); @@ -1981,54 +1961,54 @@ test "SelectionGesture repeat increments click count" { } test "SelectionGesture repeat clamps at triple click" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); for (0..4) |_| _ = try gesture.press(&t, testPress(&t, 1, 1, time)); try testing.expectEqual(@as(u3, 3), gesture.left_click_count); } test "SelectionGesture null initial time stays single click" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); _ = try gesture.press(&t, testPress(&t, 1, 1, null)); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); try testing.expectEqual(@as(u3, 1), gesture.left_click_count); try testing.expect(gesture.left_click_time != null); } test "SelectionGesture null repeat time stays single click" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); _ = try gesture.press(&t, testPress(&t, 1, 1, null)); try testing.expectEqual(@as(u3, 1), gesture.left_click_count); - try testing.expectEqual(@as(?std.time.Instant, null), gesture.left_click_time); + try testing.expectEqual(@as(?std.Io.Timestamp, null), gesture.left_click_time); } test "SelectionGesture distant press resets click count" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, testPress(&t, 1, 1, time)); _ = try gesture.press(&t, testPress(&t, 4, 1, time)); @@ -2037,51 +2017,52 @@ test "SelectionGesture distant press resets click count" { } test "SelectionGesture expired repeat resets click count" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - var event = testPress(&t, 1, 1, try std.time.Instant.now()); + var event = testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake)); event.repeat_interval = 0; _ = try gesture.press(&t, event); - std.Thread.sleep(std.time.ns_per_ms); - event.time = try std.time.Instant.now(); + try std.Io.sleep(testing.io, std.Io.Duration.fromMilliseconds(1), .awake); + event.time = std.Io.Timestamp.now(testing.io, .awake); _ = try gesture.press(&t, event); try testing.expectEqual(@as(u3, 1), gesture.left_click_count); } test "SelectionGesture backwards repeat time resets click count" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const earlier = testInstant(std.time.ns_per_s); - const later = testInstant(2 * std.time.ns_per_s); + const now: std.Io.Timestamp = .now(testing.io, .awake); + const earlier = now.addDuration(.fromSeconds(1)); + const later = now.addDuration(.fromSeconds(2)); _ = try gesture.press(&t, testPress(&t, 1, 1, later)); _ = try gesture.press(&t, testPress(&t, 1, 1, earlier)); try testing.expectEqual(@as(u3, 1), gesture.left_click_count); - try testing.expectEqual(.eq, gesture.left_click_time.?.order(earlier)); + try testing.expectEqualDeep(earlier, gesture.left_click_time.?); } test "SelectionGesture screen switch resets click count" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - const time = try std.time.Instant.now(); + const time = std.Io.Timestamp.now(testing.io, .awake); const primary_tracked = t.screens.active.pages.countTrackedPins(); _ = try gesture.press(&t, testPress(&t, 1, 1, time)); - _ = try t.screens.getInit(testing.allocator, .alternate, .{ + _ = try t.screens.getInit(testing.io, testing.allocator, .alternate, .{ .cols = t.cols, .rows = t.rows, }); @@ -2094,34 +2075,34 @@ test "SelectionGesture screen switch resets click count" { } test "SelectionGesture removed screen resets without untracking stale pin" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; defer gesture.deinit(&t); - _ = try t.screens.getInit(testing.allocator, .alternate, .{ + _ = try t.screens.getInit(testing.io, testing.allocator, .alternate, .{ .cols = t.cols, .rows = t.rows, }); t.screens.switchTo(.alternate); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); t.screens.switchTo(.primary); t.screens.remove(testing.allocator, .alternate); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); try testing.expectEqual(@as(u3, 1), gesture.left_click_count); try testing.expectEqual(.primary, gesture.left_click_screen); } test "SelectionGesture deinit untracks pin" { - var t = try Terminal.init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try Terminal.init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); var gesture: SelectionGesture = .init; const tracked = t.screens.active.pages.countTrackedPins(); - _ = try gesture.press(&t, testPress(&t, 1, 1, try std.time.Instant.now())); + _ = try gesture.press(&t, testPress(&t, 1, 1, std.Io.Timestamp.now(testing.io, .awake))); try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins()); gesture.deinit(&t); diff --git a/vendor/libghostty-vt/src/terminal/StringMap.zig b/vendor/libghostty-vt/src/terminal/StringMap.zig index 18dd7b19..7b8aac06 100644 --- a/vendor/libghostty-vt/src/terminal/StringMap.zig +++ b/vendor/libghostty-vt/src/terminal/StringMap.zig @@ -6,9 +6,9 @@ const std = @import("std"); const build_options = @import("terminal_options"); const oni = @import("oniguruma"); const point = @import("point.zig"); +const PinMap = @import("formatter.zig").PinMap; const Selection = @import("Selection.zig"); const Screen = @import("Screen.zig"); -const Pin = @import("PageList.zig").Pin; const Allocator = std.mem.Allocator; // Retry budget for StringMap regex searches. @@ -18,11 +18,15 @@ const Allocator = std.mem.Allocator; const oni_search_retry_limit = 100_000; string: [:0]const u8, -map: []Pin, + +/// Mapping of string byte offsets to pins. See PinMap for the +/// storage details. +map: PinMap.Map, pub fn deinit(self: StringMap, alloc: Allocator) void { alloc.free(self.string); - alloc.free(self.map); + var map = self.map; + map.deinit(alloc); } /// Returns an iterator that yields the next match of the given regex. @@ -106,8 +110,8 @@ pub const Match = struct { pub fn selection(self: Match) Selection { const start_idx: usize = @intCast(self.region.starts()[0]); const end_idx: usize = @intCast(self.region.ends()[0] - 1); - const start_pt = self.map.map[self.offset + start_idx]; - const end_pt = self.map.map[self.offset + end_idx]; + const start_pt = self.map.map.get(self.offset + start_idx).?; + const end_pt = self.map.map.get(self.offset + end_idx).?; return .init(start_pt, end_pt, false); } }; @@ -117,6 +121,7 @@ test "StringMap searchIterator" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; // Initialize our regex try oni.testing.ensureInit(); @@ -130,7 +135,7 @@ test "StringMap searchIterator" { defer re.deinit(); // Initialize our screen - var s = try Screen.init(alloc, .{ .cols = 5, .rows = 5, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 5, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); const str = "1ABCD2EFGH\n3IJKL"; try s.testWriteString(str); @@ -140,13 +145,10 @@ test "StringMap searchIterator" { .y = 1, } }).?, }).?; - var map: StringMap = undefined; - const sel_str = try s.selectionString(alloc, .{ + const map = try s.selectionStringMap(alloc, .{ .sel = line, .trim = false, - .map = &map, }); - alloc.free(sel_str); defer map.deinit(alloc); // Get our iterator @@ -174,6 +176,7 @@ test "StringMap searchIterator URL detection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; const url = @import("../config/url.zig"); // Initialize URL regex @@ -188,7 +191,7 @@ test "StringMap searchIterator URL detection" { defer re.deinit(); // Initialize our screen with text containing a URL - var s = try Screen.init(alloc, .{ .cols = 40, .rows = 5, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 40, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello https://example.com/path world"); @@ -199,13 +202,10 @@ test "StringMap searchIterator URL detection" { .y = 0, } }).?, }).?; - var map: StringMap = undefined; - const sel_str = try s.selectionString(alloc, .{ + const map = try s.selectionStringMap(alloc, .{ .sel = line, .trim = false, - .map = &map, }); - alloc.free(sel_str); defer map.deinit(alloc); // Search for URL match @@ -235,6 +235,7 @@ test "StringMap searchIterator URL with click position" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; const url = @import("../config/url.zig"); // Initialize URL regex @@ -249,7 +250,7 @@ test "StringMap searchIterator URL with click position" { defer re.deinit(); // Initialize our screen with text containing a URL - var s = try Screen.init(alloc, .{ .cols = 40, .rows = 5, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 40, .rows = 5, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello https://example.com world"); @@ -263,13 +264,10 @@ test "StringMap searchIterator URL with click position" { const line = s.selectLine(.{ .pin = click_pin, }).?; - var map: StringMap = undefined; - const sel_str = try s.selectionString(alloc, .{ + const map = try s.selectionStringMap(alloc, .{ .sel = line, .trim = false, - .map = &map, }); - alloc.free(sel_str); defer map.deinit(alloc); // Search for URL match and verify click position is within URL diff --git a/vendor/libghostty-vt/src/terminal/Tabstops.zig b/vendor/libghostty-vt/src/terminal/Tabstops.zig index 8dbe6789..fb1d7ae6 100644 --- a/vendor/libghostty-vt/src/terminal/Tabstops.zig +++ b/vendor/libghostty-vt/src/terminal/Tabstops.zig @@ -14,7 +14,6 @@ const tripwire = @import("../tripwire.zig"); const Allocator = std.mem.Allocator; const testing = std.testing; const assert = @import("../quirks.zig").inlineAssert; -const fastmem = @import("../fastmem.zig"); /// Unit is the type we use per tabstop unit (see file docs). const Unit = u8; @@ -94,13 +93,13 @@ pub fn unset(self: *Tabstops, col: usize) void { const i = entry(col); const idx = index(col); if (i < prealloc_count) { - self.prealloc_stops[i] ^= masks[idx]; + self.prealloc_stops[i] &= ~masks[idx]; return; } const dynamic_i = i - prealloc_count; assert(dynamic_i < self.dynamic_stops.len); - self.dynamic_stops[dynamic_i] ^= masks[idx]; + self.dynamic_stops[dynamic_i] &= ~masks[idx]; } /// Get the value of a tabstop at a specific column. The columns are 0-indexed. @@ -138,24 +137,21 @@ pub fn resize( return; } - // What we need in the dynamic size - const size = cols - prealloc_columns; - if (size < self.dynamic_stops.len) { + // Number of units needed beyond the preallocated columns. + const dynamic_count = std.math.divCeil( + usize, + cols - prealloc_columns, + unit_bits, + ) catch unreachable; + if (dynamic_count <= self.dynamic_stops.len) { self.cols = cols; return; } - // Note: we can probably try to realloc here but I'm not sure it matters. try tw.check(.dynamic_alloc); - const new = try alloc.alloc(Unit, size); - errdefer comptime unreachable; - @memset(new, 0); - if (self.dynamic_stops.len > 0) { - fastmem.copy(Unit, new, self.dynamic_stops); - alloc.free(self.dynamic_stops); - } - - self.dynamic_stops = new; + const old_len = self.dynamic_stops.len; + self.dynamic_stops = try alloc.realloc(self.dynamic_stops, dynamic_count); + @memset(self.dynamic_stops[old_len..], 0); self.cols = cols; } @@ -202,25 +198,48 @@ test "Tabstops: basic" { try testing.expect(t.get(4)); t.unset(4); try testing.expect(!t.get(4)); + + // Unsetting a column with no tabstop should be a no-op, not a toggle. + t.unset(4); + try testing.expect(!t.get(4)); } test "Tabstops: dynamic allocations" { var t: Tabstops = .{}; defer t.deinit(testing.allocator); - // Grow the capacity by 2. + // Grow by less than one unit to verify the allocation rounds up. const cap = t.capacity(); - try t.resize(testing.allocator, cap * 2); + try t.resize(testing.allocator, cap + 5); + try testing.expectEqual(cap + unit_bits, t.capacity()); // Set something that was out of range of the first - t.set(cap + 5); - try testing.expect(t.get(cap + 5)); - try testing.expect(!t.get(cap + 4)); + t.set(cap + 4); + try testing.expect(t.get(cap + 4)); + try testing.expect(!t.get(cap + 3)); + + // Unsetting a column with no tabstop should be a no-op, not a toggle. + t.unset(cap + 3); + try testing.expect(!t.get(cap + 3)); + + // Growing again preserves existing stops and clears the new unit. + try t.resize(testing.allocator, cap + unit_bits + 1); + try testing.expect(t.get(cap + 4)); + try testing.expect(!t.get(cap + unit_bits)); // Prealloc still works try testing.expect(!t.get(5)); } +test "Tabstops: resize to existing capacity does not allocate" { + var backing: [prealloc_count]Unit = undefined; + var fixed = std.heap.FixedBufferAllocator.init(&backing); + var t: Tabstops = .{}; + + try t.resize(fixed.allocator(), prealloc_columns * 2); + try t.resize(fixed.allocator(), prealloc_columns * 2); +} + test "Tabstops: interval" { var t: Tabstops = try init(testing.allocator, 80, 4); defer t.deinit(testing.allocator); diff --git a/vendor/libghostty-vt/src/terminal/Terminal.zig b/vendor/libghostty-vt/src/terminal/Terminal.zig index 08920127..e9cc3b8e 100644 --- a/vendor/libghostty-vt/src/terminal/Terminal.zig +++ b/vendor/libghostty-vt/src/terminal/Terminal.zig @@ -7,8 +7,10 @@ const std = @import("std"); const build_options = @import("terminal_options"); const lib = @import("lib.zig"); const assert = @import("../quirks.zig").inlineAssert; +const tripwire = @import("../tripwire.zig"); const testing = std.testing; const Allocator = std.mem.Allocator; +const simd = @import("../simd/main.zig"); const unicode = @import("../unicode/main.zig"); const uucode = @import("uucode"); @@ -80,12 +82,21 @@ previous_char: ?u21 = null, /// The modes that this terminal currently has active. modes: modespkg.ModeState = .{}, +/// Terminal-level cursor state. +cursor: Cursor = .{}, + /// The most recently set mouse shape for the terminal. mouse_shape: mouse.Shape = .text, /// Per-session Glyph Protocol registrations. glyph_glossary: glyph.Glossary = .empty, +/// Kitty drag and drop protocol (OSC 72) state. Allocated when a client +/// registers to accept drops (t=a) and freed when it unregisters (t=A), +/// so a terminal that never runs a drag and drop aware program pays +/// nothing for it. Non-null means a client currently accepts drops. +kitty_dnd: ?*kitty.dnd.State = null, + /// These are just a packed set of flags we may set on the terminal. flags: packed struct { // This supports a Kitty extension where programs using semantic @@ -112,6 +123,10 @@ flags: packed struct { /// True if the window is focused. focused: bool = true, + /// True if the terminal view may be visible. Unknown visibility is + /// represented as visible so callers behave conservatively. + visible: bool = true, + /// True if the terminal is in a password entry mode. This is set /// to true based on termios state. This is set /// to true based on termios state. @@ -235,16 +250,42 @@ pub const ScrollingRegion = struct { right: size.CellCountInt, }; +/// Terminal-level cursor state shared by all screens. +pub const Cursor = struct { + /// Whether the current cursor appearance follows the configured defaults. + is_default: bool = true, + + /// Configured style restored by DECSCUSR default and RIS. + default_style: Screen.CursorStyle = .block, + + /// Configured blink restored by DECSCUSR default and RIS. Null selects + /// the terminal emulator default, which is blinking. + default_blink: ?bool = false, +}; + pub const Options = struct { cols: size.CellCountInt, rows: size.CellCountInt, - max_scrollback: usize = 10_000, + + /// The maximum size of scrollback in bytes. Null is unlimited and zero + /// disables scrollback. + max_scrollback_bytes: ?usize = 10_000, + + /// The maximum number of physical scrollback rows, excluding the active + /// area. Null is unlimited. The effective limit permits at least one + /// standard page and only complete historical pages are pruned. + max_scrollback_lines: ?usize = null, + colors: Colors = .default, /// The default mode state. When the terminal gets a reset, it /// will revert back to this state. default_modes: modespkg.ModePacked = .{}, + /// Cursor state restored by DECSCUSR default and RIS. + default_cursor_style: Screen.CursorStyle = .block, + default_cursor_blink: ?bool = false, + /// The total storage limit for Kitty images in bytes. Has no effect /// if kitty images are disabled at build-time. kitty_image_storage_limit: usize = switch (build_options.artifact) { @@ -268,22 +309,24 @@ pub const Options = struct { /// Initialize a new terminal. pub fn init( + io_impl: std.Io, alloc: Allocator, opts: Options, ) !Terminal { const cols = opts.cols; const rows = opts.rows; - var screen_set: ScreenSet = try .init(alloc, .{ + var screen_set: ScreenSet = try .init(io_impl, alloc, .{ .cols = cols, .rows = rows, - .max_scrollback = opts.max_scrollback, + .max_scrollback_bytes = opts.max_scrollback_bytes, + .max_scrollback_lines = opts.max_scrollback_lines, .kitty_image_storage_limit = opts.kitty_image_storage_limit, .kitty_image_loading_limits = opts.kitty_image_loading_limits, }); errdefer screen_set.deinit(alloc); - return .{ + var result: Terminal = .{ .cols = cols, .rows = rows, .screens = screen_set, @@ -301,15 +344,23 @@ pub fn init( .values = opts.default_modes, .default = opts.default_modes, }, + .cursor = .{ + .default_style = opts.default_cursor_style, + .default_blink = opts.default_cursor_blink, + }, }; + result.setCursorStyle(.default); + return result; } pub fn deinit(self: *Terminal, alloc: Allocator) void { self.tabstops.deinit(alloc); self.screens.deinit(alloc); + self.colors.palette.deinit(alloc); self.pwd.deinit(alloc); self.title.deinit(alloc); self.glyph_glossary.deinit(alloc); + if (self.kitty_dnd) |dnd| dnd.destroy(alloc); self.* = undefined; } @@ -328,7 +379,10 @@ pub fn deinit(self: *Terminal, alloc: Allocator) void { /// for handling escape sequences split across write boundaries), you /// must store and reuse the returned stream. pub fn vtStream(self: *Terminal) Stream { - return .initAlloc(self.gpa(), self.vtHandler()); + return Stream.init(.{ + .allocator = self.gpa(), + .handler = self.vtHandler(), + }); } /// This is the handler-side only for vtStream. @@ -336,11 +390,96 @@ pub fn vtHandler(self: *Terminal) Stream.Handler { return .init(self); } +/// Change the cursor's current shape and blink behavior. +/// +/// The terminal parser uses this for DECSCUSR (`CSI Ps SP q`), but the behavior +/// is general: `.default` selects the configured defaults, while any other +/// value selects a concrete appearance until it is changed again or reset. +pub fn setCursorStyle(self: *Terminal, value: ansi.CursorStyle) void { + // Remember whether future configuration changes should update the visible + // cursor. An explicit appearance must remain in effect until the program + // selects the default again. + self.cursor.is_default = value == .default; + + // Convert the request into the concrete values used by the renderer and + // terminal mode state. A null default blink means the emulator default. + self.modes.set(.cursor_blinking, switch (value) { + .default => self.cursor.default_blink orelse true, + .steady_block, .steady_bar, .steady_underline => false, + .blinking_block, .blinking_bar, .blinking_underline => true, + }); + self.screens.active.cursor.cursor_style = switch (value) { + .default => self.cursor.default_style, + .blinking_block, .steady_block => .block, + .blinking_bar, .steady_bar => .bar, + .blinking_underline, .steady_underline => .underline, + }; +} + +/// Change the default cursor shape. +/// +/// If the cursor currently follows its defaults, the visible shape changes +/// immediately. Otherwise the new shape is saved for the next reset or default +/// selection, such as DECSCUSR `CSI 0 SP q`. +pub fn setDefaultCursorStyle( + self: *Terminal, + configured_style: Screen.CursorStyle, +) void { + // Always retain the new default, even while an explicit appearance is + // active, so a later reset or default request can restore it. + self.cursor.default_style = configured_style; + + // Do not overwrite an appearance explicitly selected by the program. + if (self.cursor.is_default) self.setCursorStyle(.default); +} + +/// Change the default cursor blink behavior. +/// +/// Null selects the terminal emulator default (blinking). Like the default +/// shape, this is applied immediately only when the cursor currently follows +/// its defaults; otherwise it is saved for the next reset or default selection. +pub fn setDefaultCursorBlink(self: *Terminal, blink: ?bool) void { + // Keep the configured value separate from the currently resolved mode so + // null can continue to mean "use the emulator default." + self.cursor.default_blink = blink; + + // Do not overwrite blink behavior explicitly selected by the program. + if (self.cursor.is_default) self.setCursorStyle(.default); +} + +/// The I/O implementation we should use for this terminal. +pub fn io(self: *Terminal) std.Io { + return self.screens.active.io; +} + /// The general allocator we should use for this terminal. pub fn gpa(self: *Terminal) Allocator { return self.screens.active.alloc; } +/// Change the primary screen's maximum scrollback allocation in bytes. +/// +/// Null removes the byte limit and zero disables scrollback. Disabling +/// scrollback also immediately erases retained history and changes future +/// scrolling to use the no-scrollback path. The alternate screen is +/// intentionally unaffected because it never retains scrollback. +pub fn setScrollbackMaxBytes(self: *Terminal, max: ?usize) void { + const primary = self.screens.get(.primary).?; + primary.pages.setMaxBytes(max); + primary.no_scrollback = max == 0; + + if (primary.no_scrollback) primary.eraseHistory(null); +} + +/// Change the primary screen's maximum number of physical scrollback lines. +/// +/// Null removes the line limit. The alternate screen is intentionally +/// unaffected because it never retains scrollback. +pub fn setScrollbackMaxLines(self: *Terminal, max: ?usize) void { + const primary = self.screens.get(.primary).?; + primary.pages.setMaxLines(max); +} + /// Print UTF-8 encoded string to the terminal. pub fn printString(self: *Terminal, str: []const u8) !void { const view = try std.unicode.Utf8View.init(str); @@ -359,9 +498,26 @@ pub fn printString(self: *Terminal, str: []const u8) !void { /// Print the previous printed character a repeated amount of times. pub fn printRepeat(self: *Terminal, count_req: usize) !void { - if (self.previous_char) |c| { - const count = @max(count_req, 1); - for (0..count) |_| try self.print(c); + const c = self.previous_char orelse return; + var remaining = @max(count_req, 1); + + // Print the repeated codepoint in slices so that eligible runs + // take the batched printSlice fast path. printSlice is semantically + // identical to calling print per codepoint: ineligible characters + // or terminal states (insert mode, grapheme clustering, hyperlinks, + // etc.) fall back to the per-codepoint print() path internally. + // + // The buffer is filled with a runtime-bounded loop rather than + // `= @splat(c)`: a comptime-known 4096-element splat gets fully + // unrolled into ~33KB of consecutive stores (LLVM won't re-roll + // or vectorize it, see quirks_memset.zig), and it would fill the + // whole buffer even for the typical small repeat counts. + var buf: [4096]u32 = undefined; + for (buf[0..@min(remaining, buf.len)]) |*cp| cp.* = c; + while (remaining > 0) { + const n = @min(remaining, buf.len); + try self.printSlice(buf[0..n]); + remaining -= n; } } @@ -381,11 +537,55 @@ pub fn printRepeat(self: *Terminal, count_req: usize) !void { /// slower per-codepoint path. They're less common and this is optimized /// for the aforementioned cases. pub fn printSlice(self: *Terminal, cps: []const u32) !void { + // Check if we can do the fast path up front. If we can't + // we need to go back to scalar `print`. + const fast = fast: { + // Only the main display is supported. + if (self.status_display != .main) break :fast false; + + // Modes that require per-codepoint handling in print(). + // Wraparound is required (its the default) so that our + // row-fill logic below can assume soft-wrap semantics. Insert + // mode shifts cells per print. + if (self.modes.get(.insert)) break :fast false; + if (!self.modes.get(.wraparound)) break :fast false; + + // Charset must map ASCII as-is (true unless a DEC special + // charset is actively invoked, which is rare). + const screen: *Screen = self.screens.active; + if (screen.charset.single_shift != null) break :fast false; + switch (screen.charset.charsets.get(screen.charset.gl)) { + .utf8, .ascii => {}, + else => break :fast false, + } + + // Hyperlinks require per-cell map bookkeeping. + if (screen.cursor.hyperlink_id != 0) break :fast false; + + break :fast true; + }; + if (!fast) { + for (cps) |cp| try self.print(@intCast(cp)); + return; + } + + const grapheme_cluster = self.modes.get(.grapheme_cluster); + + // When grapheme clustering is enabled and a left margin is set, + // print() consults the cell left of the margin after wrapping, + // which we can't reason about here. Restrict the fast path to + // the [0x10, 0xFF] range in that case (those never cluster). + const allow_unicode = !grapheme_cluster or self.scrolling_region.left == 0; + var i: usize = 0; while (i < cps.len) { // Try the fast-path print first. This will return the number of // codepoints it consumed. - const consumed = try self.printSliceFast(cps[i..]); + const consumed = try self.printSliceFast( + cps[i..], + grapheme_cluster, + allow_unicode, + ); if (consumed > 0) { i += consumed; continue; @@ -407,32 +607,16 @@ pub fn printSlice(self: *Terminal, cps: []const u32) !void { /// /// The fast path handles runs of narrow (width 1) and wide (width 2) /// codepoints being written to simple cells. Everything else (zero -/// width codepoints, grapheme cluster continuations, insert mode, -/// charset mapping, hyperlinks, complex cells, etc.) is rejected so -/// `print` can handle it with full generality. -fn printSliceFast(self: *Terminal, cps: []const u32) !usize { - // Only the main display is supported. - if (self.status_display != .main) return 0; - - // Modes that require per-codepoint handling in print(). Wraparound - // is required (its the default) so that our row-fill logic below can - // assume soft-wrap semantics. Insert mode shifts cells per print. - if (self.modes.get(.insert)) return 0; - if (!self.modes.get(.wraparound)) return 0; - +/// width codepoints, grapheme cluster continuations, complex cells, +/// etc.) is rejected so `print` can handle it with full generality. +fn printSliceFast( + self: *Terminal, + cps: []const u32, + grapheme_cluster: bool, + allow_unicode: bool, +) !usize { const screen: *Screen = self.screens.active; - // Charset must map ASCII as-is (true unless a DEC special charset - // is actively invoked, which is rare). - if (screen.charset.single_shift != null) return 0; - switch (screen.charset.charsets.get(screen.charset.gl)) { - .utf8, .ascii => {}, - else => return 0, - } - - // Hyperlinks require per-cell map bookkeeping. - if (screen.cursor.hyperlink_id != 0) return 0; - // Codepoints in [0x10, 0xFF] are always narrow (width 1, matching // the c <= 0xFF fast path in print) and can never interact with // grapheme clustering (which requires a codepoint > 0xFF). @@ -443,13 +627,6 @@ fn printSliceFast(self: *Terminal, cps: []const u32) !usize { // 2027) is enabled, if they are a grapheme break from the // previously printed codepoint (so print would never attach them // to the previous cell). - const grapheme_cluster = self.modes.get(.grapheme_cluster); - - // When grapheme clustering is enabled and a left margin is set, - // print() consults the cell left of the margin after wrapping, - // which we can't reason about here. Restrict the fast path to - // the [0x10, 0xFF] range in that case (those never cluster). - const allow_unicode = !grapheme_cluster or self.scrolling_region.left == 0; // Codepoints in [0x10, 0xFF] are always narrow: print() // hardcodes width 1 for c <= 0xFF (no width table lookup). @@ -478,14 +655,46 @@ fn printSliceFast(self: *Terminal, cps: []const u32) !usize { } // The first codepoint requires care when grapheme clustering is - // enabled: print() examines the previous *cell* which can hold - // state (grapheme data) that we can't cheaply reason about here. - // Note this includes the pending-wrap state: print() may attach - // to the pending cell *instead of wrapping*. We only take the - // first codepoint if the cursor is at column zero with no pending - // wrap, where print() skips clustering entirely. - if (grapheme_cluster) { - if (screen.cursor.pending_wrap or screen.cursor.x != 0) return 0; + // enabled: print() may attach it to the previous *cell* instead + // of writing a new one. Take the first codepoint only when we can + // determine — computing exactly what print() would — that it + // starts a new cluster. At column zero with no pending wrap, + // print() skips clustering entirely. Otherwise resolve the + // previous cell the way print() does and check for a break. + // + // Note the pending-wrap rejection: print() may attach to the + // pending cell *instead of wrapping*, which we can't model here. + if (grapheme_cluster and screen.cursor.x != 0) gate: { + if (screen.cursor.pending_wrap) return 0; + + // Resolve the content cell to our left exactly like print(): + // if the immediate left cell is a wide spacer tail, the + // content lives one further left. (A spacer tail can never + // be at column zero — its wide half would have to be in the + // previous row — so the second cursorCellLeft is in bounds.) + const immediate = screen.cursorCellLeft(1); + const prev: *Cell = switch (immediate.wide) { + .spacer_tail => screen.cursorCellLeft(2), + else => immediate, + }; + + // An empty previous cell is necessarily a grapheme break. + if (prev.codepoint() == 0) break :gate; + + // Grapheme data on the previous cell requires the full + // cluster state machine replay; only print() can do that. + if (prev.hasGrapheme()) return 0; + + // A simple single-codepoint previous cell: print() would run + // exactly this break check from the default state. + var state: uucode.grapheme.BreakState = .default; + if (!unicode.graphemeBreak( + prev.content.codepoint.data, + @intCast(cp0), + &state, + )) return 0; + } else if (grapheme_cluster) { + if (screen.cursor.pending_wrap) return 0; } // The width lookup is a runtime value while printSliceFill is @@ -540,6 +749,85 @@ inline fn printSliceEligible(cp: u32, comptime width: PrintSliceWidth) bool { }); } +/// Store a run of narrow codepoint cells built from a bit template: +/// for each `idx` in `[from, to)`, `cells[idx]` is assigned the bits +/// `template_bits | (cps[idx] << cp_shift)`. +/// +/// The template is a complete Cell (content tag, style, wide state, +/// etc. already baked in by the caller) whose codepoint content bits +/// are zero. Since Cell is a packed struct(u64), OR-ing a codepoint +/// into the content field's bit position yields a finished cell as a +/// single integer, keeping the loop pure data movement: no per-cell +/// field assignments and no branches. +/// +/// This loop is manually vectorized: Zig 0.16 (LLVM 21) no longer +/// auto-vectorizes the scalar form the way Zig 0.15 (LLVM 20) did. +inline fn printSliceStoreRun( + cells: [*]Cell, + cps: [*]const u32, + from: usize, + to: usize, + template_bits: u64, +) void { + // The bit position of the `content` field within the packed + // Cell. A codepoint occupies the low bits of `content`, so + // shifting a codepoint left by this amount places it exactly + // where `.content = .{ .codepoint = .{ .data = cp } }` would. + const cp_shift = @bitOffsetOf(Cell, "content"); + + // Since codepoints are OR'd into the content field rather than + // assigned, any nonzero content bits in the template would + // corrupt the stored codepoints. + const content_mask: u64 = comptime mask: { + const bits = @bitSizeOf(@FieldType(Cell, "content")); + break :mask ((1 << bits) - 1) << cp_shift; + }; + assert(template_bits & content_mask == 0); + + var idx = from; + + // Vectorized bulk of the run. + if (simd.lanes(u64)) |lanes| { + // u64 due to backing integer of Cell + const V = @Vector(lanes, u64); + + // The template and shift amount are loop-invariant, so + // broadcast them to every lane once up front. + const template: V = @splat(template_bits); + const shift: @Vector( + lanes, + std.math.Log2Int(u64), + ) = @splat(cp_shift); + + while (idx + lanes <= to) : (idx += lanes) { + // Load `lanes` decoded codepoints... + const narrow: @Vector(lanes, u32) = cps[idx..][0..lanes].*; + + // ...widen each u32 lane to the u64 cell size... + const wide: V = @intCast(narrow); + + // ...shift each codepoint into the content field's bit + // position and merge with the template, producing + // `lanes` finished cells (coerced from vector to array + // so they can be bitcast for the store below)... + const bits: [lanes]u64 = template | (wide << shift); + + // ...and store them contiguously. Cell is a packed + // struct(u64) so an array of u64 bit patterns has + // identical layout to an array of cells. + cells[idx..][0..lanes].* = @bitCast(bits); + } + } + + // Scalar tail: the final `< lanes` cells of the run, or the + // entire run on targets without SIMD. Note `idx` carries over + // from the vector loop above. This is the same computation as + // the vector body, one cell at a time. + while (idx < to) : (idx += 1) { + cells[idx] = @bitCast(template_bits | (@as(u64, cps[idx]) << cp_shift)); + } +} + /// The row-filling portion of the printSlice fast path, specialized by /// width class. The first codepoint must already be validated by the /// caller (printSliceFast). @@ -581,19 +869,20 @@ fn printSliceFill( // Anything else (including eligible unicode) proceeds via // the scalar loop below. if (comptime width == .narrow) { - const lanes = 8; - const V = @Vector(lanes, u32); - const lo: V = @splat(0x10); - const hi: V = @splat(0xFF); - while (idx + lanes <= cps.len) { - const v: V = cps[idx..][0..lanes].*; - const in_range = (v >= lo) & (v <= hi); - if (!@reduce(.And, in_range)) { - const bits: std.meta.Int(.unsigned, lanes) = @bitCast(in_range); - idx += @ctz(~bits); - break; + if (simd.lanes(u32)) |lanes| { + const V = @Vector(lanes, u32); + const lo: V = @splat(0x10); + const hi: V = @splat(0xFF); + while (idx + lanes <= cps.len) { + const v: V = cps[idx..][0..lanes].*; + const in_range = (v >= lo) & (v <= hi); + if (!@reduce(.And, in_range)) { + const bits: std.meta.Int(.unsigned, lanes) = @bitCast(in_range); + idx += @ctz(~bits); + break; + } + idx += lanes; } - idx += lanes; } } @@ -650,7 +939,7 @@ fn printSliceFill( const style_id = cursor.style_id; const template: Cell = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, .style_id = style_id, .wide = .narrow, .protected = cursor.protected, @@ -727,12 +1016,38 @@ fn printSliceFill( // We can only write whole (wide, spacer) pairs. const pair_end = k + (simple - k) / 2 * 2; var idx = k; + + // Manually vectorized for the same reason as + // printSliceStoreRun: build a vector of wide cells, + // interleave with spacer tails, and store (wide, + // spacer) pairs several at a time. + if (simd.lanes(u64)) |lanes| { + const pair_lanes = lanes / 2; + const Vp = @Vector(pair_lanes, u64); + const wide_v: Vp = @splat(wide_bits); + const spacer_v: Vp = @splat(spacer_bits); + const shift_v: @Vector( + pair_lanes, + std.math.Log2Int(u64), + ) = @splat(cp_shift); + while (idx + 2 * pair_lanes <= pair_end) : (idx += 2 * pair_lanes) { + const narrow: @Vector(pair_lanes, u32) = + cps[printed + idx / 2 ..][0..pair_lanes].*; + const wides: Vp = wide_v | (@as(Vp, @intCast(narrow)) << shift_v); + const inter: [2 * pair_lanes]u64 = std.simd.interlace(.{ + wides, + spacer_v, + }); + cells[idx..][0 .. 2 * pair_lanes].* = @bitCast(inter); + } + } while (idx < pair_end) : (idx += 2) { cells[idx] = @bitCast( wide_bits | (@as(u64, cps[printed + idx / 2]) << cp_shift), ); cells[idx + 1] = @bitCast(spacer_bits); } + // If the simple run ended mid-pair we stop at the pair // boundary and handle the offending cell below. k = pair_end; @@ -742,11 +1057,13 @@ fn printSliceFill( simple = pair_end; } } else { - for (k..simple) |idx| { - cells[idx] = @bitCast( - template_bits | (@as(u64, cps[printed + idx]) << cp_shift), - ); - } + printSliceStoreRun( + cells, + cps.ptr + printed, + k, + simple, + template_bits, + ); k = simple; } if (k >= cell_count) break; @@ -798,11 +1115,13 @@ fn printSliceFill( page.styles.useMultiple(page.memory, style_id, @intCast(n)); } - for (k..m) |idx| { - cells[idx] = @bitCast( - template_bits | (@as(u64, cps[printed + idx]) << cp_shift), - ); - } + printSliceStoreRun( + cells, + cps.ptr + printed, + k, + m, + template_bits, + ); k = m; continue :fill; } @@ -945,7 +1264,7 @@ pub fn print(self: *Terminal, c: u21) !void { // necessarily a grapheme break. if (prev.cell.codepoint() == 0) break :grapheme; - var previous_codepoint: u21 = prev.cell.content.codepoint; + var previous_codepoint: u21 = prev.cell.content.codepoint.data; const grapheme_break = brk: { var state: uucode.grapheme.BreakState = .default; if (prev.cell.hasGrapheme()) { @@ -992,13 +1311,13 @@ pub fn print(self: *Terminal, c: u21) !void { const row_wrap = right_limit == self.cols; if (row_wrap) self.screens.active.cursor.page_row.wrap = true; - const prev_cp = prev.cell.content.codepoint; + const prev_cp = prev.cell.content.codepoint.data; if (prev.cell.hasGrapheme()) { // This is like printCell but without clearing the // grapheme data from the cell, so we can move it // later. prev.cell.wide = if (row_wrap) .spacer_head else .narrow; - prev.cell.content.codepoint = 0; + prev.cell.content.codepoint.data = 0; try self.printWrap(); self.printCell(prev_cp, .wide); @@ -1012,14 +1331,21 @@ pub fn print(self: *Terminal, c: u21) !void { const old_rac = old_pin.rowAndCell(); if (new_pin.node == old_pin.node) { - new_pin.node.page().moveGrapheme(prev.cell, new_rac.cell); - prev.cell.content_tag = .codepoint; + new_pin.node.page().moveGrapheme(old_rac.cell, new_rac.cell); + old_rac.cell.content_tag = .codepoint; new_rac.cell.content_tag = .codepoint_grapheme; new_rac.row.grapheme = true; } else { const cps = old_pin.node.page().lookupGrapheme(old_rac.cell).?; for (cps) |cp| { - try self.screens.active.appendGrapheme(new_rac.cell, cp); + // appendGrapheme can grow the cursor + // page, so read the destination from + // the cursor each time rather than + // holding a pointer across the call. + try self.screens.active.appendGrapheme( + self.screens.active.cursor.page_cell, + cp, + ); } old_pin.node.page().clearGrapheme(old_rac.cell); } @@ -1029,7 +1355,7 @@ pub fn print(self: *Terminal, c: u21) !void { // Point prev.cell to our new previous cell that // we'll be appending graphemes to - prev.cell = new_rac.cell; + prev.cell = self.screens.active.cursor.page_cell; } else { self.printCell( 0, @@ -1048,8 +1374,30 @@ pub fn print(self: *Terminal, c: u21) !void { // Write our spacer, since prev.cell is now wide self.screens.active.cursorRight(1); + + // Writing the spacer can grow the page to make room for + // the cursor hyperlink. Growing replaces the page, which + // invalidates `prev.cell`. Record the page identity first + // so the common case where nothing grows stays free. + // + // A pointer comparison alone isn't enough: pages are + // pooled, so a replacement can reuse the same address. + // The serial makes the pair a unique identity. + const spacer_node = self.screens.active.cursor.page_pin.node; + const spacer_serial = spacer_node.serial; + self.printCell(0, .spacer_tail); + if (self.screens.active.cursor.page_pin.node != spacer_node or + self.screens.active.cursor.page_pin.node.serial != spacer_serial) + { + @branchHint(.unlikely); + + // The cursor is on the spacer tail we just wrote, so + // the wide cell we append to is the one to its left. + prev.cell = self.screens.active.cursorCellLeft(1); + } + // Move the cursor again so we're beyond our spacer if (self.screens.active.cursor.x == right_limit - 1) { self.screens.active.cursor.pending_wrap = true; @@ -1063,28 +1411,24 @@ pub fn print(self: *Terminal, c: u21) !void { if (prev.cell.wide != .wide) break :narrow; prev.cell.wide = .narrow; - // Remove the wide spacer tail - const cell = self.screens.active.cursorCellLeft(prev.left - 1); - cell.wide = .narrow; - - // Back track the cursor so that we don't end up with - // an extra space after the character. Since xterm is - // not VS aware, it cannot be used as a reference for - // this behavior; but it does follow the principle of - // least surprise, and also matches the behavior that - // can be observed in Kitty, which is one of the only - // other VS aware terminals. - if (self.screens.active.cursor.x == right_limit - 1) { - // If we're already at the right edge, we stay - // here and set the pending wrap to false since - // when we pend a wrap, we only move our cursor once - // even for wide chars (tests verify). - self.screens.active.cursor.pending_wrap = false; - } else { - // Otherwise, move back. - self.screens.active.cursorLeft(1); + // Remove the wide spacer tail. The previous cell may be + // under the cursor, so locate the tail from the wide base + // rather than by subtracting from the cursor distance. + const prev_x = self.screens.active.cursor.x - prev.left; + if (prev_x < self.cols - 1) { + const cells: [*]Cell = @ptrCast(prev.cell); + cells[1].wide = .narrow; } + // Place the cursor one cell after the now-narrow base, + // clamped to the right edge. Usually this moves the cursor + // back from after the old tail, but saved cursor state or + // changed margins can leave it directly on the base. + self.screens.active.cursor.pending_wrap = false; + self.screens.active.cursorHorizontalAbsolute( + @min(prev_x + 1, right_limit - 1), + ); + break :narrow; }, @@ -1151,7 +1495,7 @@ pub fn print(self: *Terminal, c: u21) !void { // If this is a emoji variation selector, prev must be an emoji if (c == 0xFE0F or c == 0xFE0E) { - const prev_props = unicode.table.get(prev.content.codepoint); + const prev_props = unicode.table.get(prev.content.codepoint.data); const emoji = prev_props.grapheme_break == .extended_pictographic; if (!emoji) return; } @@ -1311,7 +1655,7 @@ fn printCell( // So integrity checks pass. We fix this up later so we don't // need to do this without safety checks. - if (comptime std.debug.runtime_safety) { + if (comptime build_options.slow_runtime_safety) { cell.wide = .narrow; } @@ -1364,7 +1708,7 @@ fn printCell( // Write cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = c }, + .content = .{ .codepoint = .{ .data = c } }, .style_id = self.screens.active.cursor.style_id, .wide = wide, .protected = self.screens.active.cursor.protected, @@ -1397,7 +1741,11 @@ fn printCell( self.screens.active.cursorSetHyperlink() catch |err| { @branchHint(.unlikely); log.warn("error reallocating for more hyperlink space, ignoring hyperlink err={}", .{err}); - assert(!cell.hyperlink); + + // A partially successful grow can replace the page even when the + // call fails, so `cell` may be stale here. The cursor pointers are + // always reloaded, so read the cell through the cursor. + assert(!self.screens.active.cursor.page_cell.hyperlink); }; } else if (had_hyperlink) { // If the previous cell had a hyperlink then we need to clear it. @@ -2040,11 +2388,25 @@ pub fn index(self: *Terminal) !void { (!screen.no_scrollback or self.scrolling_region.bottom == 0)) { + // If a bottom margin is set, kitty image placements may + // need adjusting around the scroll. The rare placements- + // present case is handled out of line so this hot path + // only pays a count check (a load from a cache line we + // already write, above). + if (comptime build_options.kitty_graphics) { + if (screen.kitty_images.placements.count() != 0) { + @branchHint(.unlikely); + try self.indexScrollWithImages(.window_shift); + return; + } + } + try screen.cursorScrollAbove(); return; } // Slow path for left and right scrolling region margins. + // scrollUp handles the kitty image adjustment itself. if (self.scrolling_region.left != 0 or self.scrolling_region.right != self.cols - 1) { @@ -2052,6 +2414,16 @@ pub fn index(self: *Terminal) !void { return; } + // Kitty image placements may need adjusting around the scroll; + // handled out of line like the scrollback path above. + if (comptime build_options.kitty_graphics) { + if (screen.kitty_images.placements.count() != 0) { + @branchHint(.unlikely); + try self.indexScrollWithImages(.in_place); + return; + } + } + // Otherwise use a fast path function to efficiently scroll // the contents of the scrolling region. try screen.cursorScrollRegionUp( @@ -2067,6 +2439,69 @@ pub fn index(self: *Terminal) !void { } } +/// The operation when we have Kitty image placements during index. +/// Split out of index() so its hot paths don't carry the adjustment +/// state in their stack frame, which measurably slows the scroll +/// hot path. +fn indexScrollWithImages( + self: *Terminal, + comptime op: kitty.graphics.ImageStorage.ScrollOp, +) !void { + var kitty_scroll = self.kittyScrollMarginsBegin(-1, op); + defer if (kitty_scroll) |*state| state.end(); + switch (op) { + .window_shift => try self.screens.active.cursorScrollAbove(), + .in_place => try self.screens.active.cursorScrollRegionUp( + self.scrolling_region.bottom - self.scrolling_region.top, + ), + } +} + +// Handle when Kitty graphics is disabled. +const KittyScrollMargins = if (build_options.kitty_graphics) + kitty.graphics.ImageStorage.ScrollMargins +else + struct { + pub inline fn end(self: *@This()) void { + _ = self; + } + }; + +/// Begin adjusting kitty image placements for a scroll of the +/// scrolling region by delta rows (negative moves content up). If +/// adjustment is needed this returns state whose end() must be called +/// after the scroll's row operations complete (see +/// ImageStorage.scrollMarginsBegin for why this is two phases). This +/// returns null when the scrolling region is the full screen, because +/// placements then follow their anchored rows via pin tracking which +/// matches kitty's marginless behavior. +/// +/// Callers must comptime-gate on build_options.kitty_graphics and +/// check that placements exist before calling, which keeps the cost +/// on the hot scroll paths cheap. +fn kittyScrollMarginsBegin( + self: *Terminal, + delta: isize, + op: kitty.graphics.ImageStorage.ScrollOp, +) ?kitty.graphics.ImageStorage.ScrollMargins { + @branchHint(.cold); + + // Full-screen scrolls need no adjustment: placements follow their + // anchored rows (possibly into the scrollback) via pin tracking. + if (self.scrolling_region.top == 0 and + self.scrolling_region.bottom == self.rows - 1 and + self.scrolling_region.left == 0 and + self.scrolling_region.right == self.cols - 1) return null; + + const screen: *Screen = self.screens.active; + return screen.kitty_images.scrollMarginsBegin( + self.io(), + self, + delta, + op, + ); +} + /// Move the cursor to the previous line in the scrolling region, possibly /// scrolling. /// @@ -2192,6 +2627,24 @@ pub fn scrollDown(self: *Terminal, count: usize) void { self.screens.active.cursor.pending_wrap = old_wrap; } + // If margins are set and kitty image placements exist, they need + // adjusting around the scroll. Note this wraps scrollDown and NOT + // insertLines: kitty scrolls images for SD/RI but leaves them + // alone for IL/DL. + var kitty_scroll: ?KittyScrollMargins = null; + defer if (kitty_scroll) |*state| state.end(); + if (comptime build_options.kitty_graphics) { + if (self.screens.active.kitty_images.placements.count() != 0) { + @branchHint(.unlikely); + const region_height: usize = + @as(usize, self.scrolling_region.bottom - self.scrolling_region.top) + 1; + kitty_scroll = self.kittyScrollMarginsBegin( + @intCast(@min(count, region_height)), + .in_place, + ); + } + } + // Move to the top of the scroll region self.screens.active.cursorAbsolute(self.scrolling_region.left, self.scrolling_region.top); self.insertLines(count); @@ -2214,6 +2667,35 @@ pub fn scrollUp(self: *Terminal, count: usize) !void { self.screens.active.cursor.pending_wrap = old_wrap; } + // If margins are set and kitty image placements exist, they need + // adjusting around the scroll. Note this wraps scrollUp and NOT + // deleteLines: kitty scrolls images for SU/IND but leaves them + // alone for IL/DL. + var kitty_scroll: ?KittyScrollMargins = null; + defer if (kitty_scroll) |*state| state.end(); + if (comptime build_options.kitty_graphics) { + if (self.screens.active.kitty_images.placements.count() != 0) { + @branchHint(.unlikely); + + // The op must mirror the branch below: the scrollback path + // shifts the active window while the deleteLines path + // moves rows in place. + const region_height: usize = + @as(usize, self.scrolling_region.bottom - self.scrolling_region.top) + 1; + kitty_scroll = self.kittyScrollMarginsBegin( + -@as(isize, @intCast(@min(count, region_height))), + if (self.scrolling_region.top == 0 and + self.scrolling_region.left == 0 and + self.scrolling_region.right == self.cols - 1 and + (!self.screens.active.no_scrollback or + self.scrolling_region.bottom == self.rows - 1)) + .window_shift + else + .in_place, + ); + } + } + // If our scroll region is at the top and we have no left/right // margins then we move the scrolled out text into the scrollback. // @@ -2283,7 +2765,7 @@ pub const ScrollViewport = union(Tag) { @This(), // Padding: largest variant is isize (8 bytes on 64-bit). // Use [2]u64 (16 bytes) for future expansion. - [2]u64, + .{ .padding = [2]u64 }, ); pub const C = c_union.C; pub const CValue = c_union.CValue; @@ -2412,7 +2894,7 @@ fn rowWillBeShifted( page.clearGrapheme(wide_cell); page.updateRowGraphemeFlag(row); } - wide_cell.content.codepoint = 0; + wide_cell.content.codepoint = .{ .data = 0 }; wide_cell.wide = .narrow; left_cell.wide = .narrow; } @@ -2423,7 +2905,7 @@ fn rowWillBeShifted( page.clearGrapheme(right_cell); page.updateRowGraphemeFlag(row); } - right_cell.content.codepoint = 0; + right_cell.content.codepoint.data = 0; right_cell.wide = .narrow; tail_cell.wide = .narrow; } @@ -2558,60 +3040,19 @@ pub fn insertLines(self: *Terminal, count: usize) void { // If our page doesn't match, then we need to do a copy from // one page to another. This is the slow path. if (src_p.node != dst_p.node) { - dst_p.node.page().clonePartialRowFrom( + // The copy may replace the destination node in order + // to increase its capacity. Our pins are tracked so + // they update automatically; we can discard the + // replacement because the remainder of this iteration + // only accesses rows through the pins. + _ = self.screens.active.clonePartialRowGrowCapacity( + dst_p.node, + dst_p.y, src_p.node.page(), - dst_row, src_row, self.scrolling_region.left, self.scrolling_region.right + 1, - ) catch |err| { - // Adjust our page capacity to make - // room for we didn't have space for - _ = self.screens.active.increaseCapacity( - dst_p.node, - switch (err) { - // Rehash the sets - error.StyleSetNeedsRehash, - error.HyperlinkSetNeedsRehash, - => null, - - // Increase style memory - error.StyleSetOutOfMemory, - => .styles, - - // Increase string memory - error.StringAllocOutOfMemory, - => .string_bytes, - - // Increase hyperlink memory - error.HyperlinkSetOutOfMemory, - error.HyperlinkMapOutOfMemory, - => .hyperlink_bytes, - - // Increase grapheme memory - error.GraphemeMapOutOfMemory, - error.GraphemeAllocOutOfMemory, - => .grapheme_bytes, - }, - ) catch |e| switch (e) { - // System OOM. We have no way to recover from this - // currently. We should probably change insertLines - // to raise an error here. - error.OutOfMemory, - => @panic("increaseCapacity system allocator OOM"), - - // The page can't accommodate the managed memory required - // for this operation. We previously just corrupted - // memory here so a crash is better. The right long - // term solution is to allocate a new page here - // move this row to the new page, and start over. - error.OutOfSpace, - => @panic("increaseCapacity OutOfSpace"), - }; - - // Continue the loop to try handling this row again. - continue; - }; + ); } else { if (!left_right) { // Swap the src/dst cells. This ensures that our dst gets the @@ -2646,6 +3087,14 @@ pub fn insertLines(self: *Terminal, count: usize) void { cur_row, cells[self.scrolling_region.left .. self.scrolling_region.right + 1], ); + + // With a full-width scroll region the entire row is a + // fresh blank row: reset the metadata so nothing (wrap + // state, semantic prompt) is retained from the row whose + // storage it recycles. With left/right margins the row + // keeps content outside the margins so the metadata is + // preserved, matching the shift case above. + if (!left_right) cur_row.reset(); } // Mark the row as dirty @@ -2759,53 +3208,19 @@ pub fn deleteLines(self: *Terminal, count: usize) void { // If our page doesn't match, then we need to do a copy from // one page to another. This is the slow path. if (src_p.node != dst_p.node) { - dst_p.node.page().clonePartialRowFrom( + // The copy may replace the destination node in order + // to increase its capacity. Our pins are tracked so + // they update automatically; we can discard the + // replacement because the remainder of this iteration + // only accesses rows through the pins. + _ = self.screens.active.clonePartialRowGrowCapacity( + dst_p.node, + dst_p.y, src_p.node.page(), - dst_row, src_row, self.scrolling_region.left, self.scrolling_region.right + 1, - ) catch |err| { - // Adjust our page capacity to make - // room for we didn't have space for - _ = self.screens.active.increaseCapacity( - dst_p.node, - switch (err) { - // Rehash the sets - error.StyleSetNeedsRehash, - error.HyperlinkSetNeedsRehash, - => null, - - // Increase style memory - error.StyleSetOutOfMemory, - => .styles, - - // Increase string memory - error.StringAllocOutOfMemory, - => .string_bytes, - - // Increase hyperlink memory - error.HyperlinkSetOutOfMemory, - error.HyperlinkMapOutOfMemory, - => .hyperlink_bytes, - - // Increase grapheme memory - error.GraphemeMapOutOfMemory, - error.GraphemeAllocOutOfMemory, - => .grapheme_bytes, - }, - ) catch |e| switch (e) { - // See insertLines - error.OutOfMemory, - => @panic("increaseCapacity system allocator OOM"), - - error.OutOfSpace, - => @panic("increaseCapacity OutOfSpace"), - }; - - // Continue the loop to try handling this row again. - continue; - }; + ); } else { if (!left_right) { // Swap the src/dst cells. This ensures that our dst gets the @@ -2840,6 +3255,14 @@ pub fn deleteLines(self: *Terminal, count: usize) void { cur_row, cells[self.scrolling_region.left .. self.scrolling_region.right + 1], ); + + // With a full-width scroll region the entire row is a + // fresh blank row: reset the metadata so nothing (wrap + // state, semantic prompt) is retained from the row whose + // storage it recycles. With left/right margins the row + // keeps content outside the margins so the metadata is + // preserved, matching the shift case above. + if (!left_right) cur_row.reset(); } // Mark the row as dirty @@ -3095,9 +3518,14 @@ pub fn eraseLine( break :left .{ 0, x + 1 }; }, - // Note that it seems like complete should reset the soft-wrap - // state of the line but in xterm it does not. - .complete => .{ 0, self.cols }, + .complete => complete: { + // Xterm preserves this flag for EL2, but it also doesn't reflow + // rows when resizing. Since we do, the erased row must no longer + // continue onto the next row. + self.screens.active.cursorResetWrap(); + + break :complete .{ 0, self.cols }; + }, else => { log.err("unimplemented erase line mode: {}", .{mode}); @@ -3164,11 +3592,12 @@ pub fn eraseDisplay( self.screens.active.cursor.pending_wrap = false; if (comptime build_options.kitty_graphics) { - // Clear all Kitty graphics state for this screen - self.screens.active.kitty_images.delete( + // Clear only placements still visible after moving the active + // area into scrollback. + self.screens.active.kitty_images.clearScreen( + self.io(), self.screens.active.alloc, self, - .{ .all = true }, ); } }, @@ -3221,11 +3650,12 @@ pub fn eraseDisplay( self.screens.active.cursor.pending_wrap = false; if (comptime build_options.kitty_graphics) { - // Clear all Kitty graphics state for this screen - self.screens.active.kitty_images.delete( + // ED2 clears visible placements but preserves graphics that + // are wholly in scrollback. + self.screens.active.kitty_images.clearScreen( + self.io(), self.screens.active.alloc, self, - .{ .all = true }, ); } @@ -3315,7 +3745,7 @@ pub fn decaln(self: *Terminal) !void { const cells = cells_multi[0..page.size.cols]; @memset(cells, .{ .content_tag = .codepoint, - .content = .{ .codepoint = 'E' }, + .content = .{ .codepoint = .{ .data = 'E' } }, .style_id = self.screens.active.cursor.style_id, // DECALN does not respect protected state. Verified with xterm. @@ -3353,10 +3783,11 @@ pub fn decaln(self: *Terminal) !void { /// undefined. pub fn kittyGraphics( self: *Terminal, + io_impl: std.Io, alloc: Allocator, cmd: *kitty.graphics.Command, ) ?kitty.graphics.Response { - return kitty.graphics.execute(alloc, self, cmd); + return kitty.graphics.execute(io_impl, alloc, self, cmd); } /// Execute a Glyph Protocol APC command against this terminal's per-session @@ -3380,12 +3811,12 @@ pub fn setKittyGraphicsSizeLimit( self: *Terminal, alloc: Allocator, limit: usize, -) !void { +) void { if (comptime !build_options.kitty_graphics) return; var it = self.screens.all.iterator(); while (it.next()) |entry| { const screen: *Screen = entry.value.*; - try screen.kitty_images.setLimit(alloc, screen, limit); + screen.kitty_images.setLimit(self.io(), alloc, screen, limit); } } @@ -3414,58 +3845,69 @@ pub fn setAttribute(self: *Terminal, attr: sgr.Attribute) !void { /// Boolean attributes are printed first, followed by foreground color, then /// background color. Each attribute is separated by a semicolon. pub fn printAttributes(self: *Terminal, buf: []u8) ![]const u8 { - var stream = std.io.fixedBufferStream(buf); - const writer = stream.writer(); + var writer: std.Io.Writer = .fixed(buf); // The SGR response always starts with a 0. See https://vt100.net/docs/vt510-rm/DECRPSS try writer.writeByte('0'); const pen = self.screens.active.cursor.style; - var attrs: [8]u8 = @splat(0); + var attrs: [9]u8 = @splat(0); var i: usize = 0; if (pen.flags.bold) { - attrs[i] = '1'; + attrs[i] = 1; i += 1; } if (pen.flags.faint) { - attrs[i] = '2'; + attrs[i] = 2; i += 1; } if (pen.flags.italic) { - attrs[i] = '3'; + attrs[i] = 3; i += 1; } if (pen.flags.underline != .none) { - attrs[i] = '4'; + attrs[i] = 4; + i += 1; + } + + if (pen.flags.overline) { + attrs[i] = 53; i += 1; } if (pen.flags.blink) { - attrs[i] = '5'; + attrs[i] = 5; i += 1; } if (pen.flags.inverse) { - attrs[i] = '7'; + attrs[i] = 7; i += 1; } if (pen.flags.invisible) { - attrs[i] = '8'; + attrs[i] = 8; i += 1; } if (pen.flags.strikethrough) { - attrs[i] = '9'; + attrs[i] = 9; i += 1; } - for (attrs[0..i]) |c| { - try writer.print(";{c}", .{c}); + for (attrs[0..i]) |attr| { + // Preserve underline styles. Kind of a hack to special case 4 + // here but its easier than changing how we do all attributes. + if (attr == 4 and pen.flags.underline != .single) { + try writer.print(";4:{}", .{@intFromEnum(pen.flags.underline)}); + continue; + } + + try writer.print(";{}", .{attr}); } switch (pen.fg_color) { @@ -3490,7 +3932,7 @@ pub fn printAttributes(self: *Terminal, buf: []u8) ![]const u8 { .rgb => |rgb| try writer.print(";48:2::{[r]}:{[g]}:{[b]}", rgb), } - return stream.getWritten(); + return writer.buffered(); } /// The modes for DECCOLM. @@ -3519,86 +3961,666 @@ pub fn deccolm(self: *Terminal, alloc: Allocator, mode: DeccolmMode) !void { self.modes.set(.@"132_column", mode == .@"132_cols"); // Resize to the requested size - try self.resize( - alloc, - switch (mode) { + try self.resize(alloc, .{ + .cols = switch (mode) { .@"132_cols" => 132, .@"80_cols" => 80, }, - self.rows, - ); + .rows = self.rows, + }); // Erase our display and move our cursor. self.eraseDisplay(.complete, false); self.setCursorPos(1, 1); } +/// A terminal resize expressed in cells with optional per-cell pixel +/// geometry. It is highly recommended that callers supply cell geometry +/// but a terminal can technically function without it (but some reports +/// like certain mouse reporting modes and Kitty image protocol will +/// not be functional). +/// +/// Cell pixel dimensions must already be scaled for the current display DPI. +pub const Resize = struct { + cols: size.CellCountInt, + rows: size.CellCountInt, + cell_size_px: ?struct { + width: u32, + height: u32, + } = null, +}; + +pub const ResizeError = error{ + /// Resize requires allocation + OutOfMemory, + + /// Input value was invalid, such as a 0-sized dimension. + InvalidValue, +}; + +const resize_tw = tripwire.module(enum { + tabstops, + primary_screen, + alternate_screen, + alternate_screen_init, +}, resize); + /// Resize the underlying terminal. +/// +/// This has follow-on impacts: +/// +/// - If the column count changes, tabstops are reset. +/// - The scroll region is always reset +/// - Synchronized output mode is reset for every successful resize. +/// +/// This handles errors gracefully and recovers the terminal back to +/// a clean usable state. +/// +/// The only error handling edge case is in the highly exceptional scenario +/// where the primary screen can be resized but the alternate screen cannot. +/// In this scenario, we attempt to clear the alt screen at the desired +/// new size. If that fails, we unconditionally deallocate the alt screen +/// and move to primary screen. This can break terminal programs but it +/// requires a really particular scenario where memory exists for one +/// but not the other and we do our best. pub fn resize( self: *Terminal, alloc: Allocator, - cols: size.CellCountInt, - rows: size.CellCountInt, -) !void { - // If our cols/rows didn't change then we're done - if (self.cols == cols and self.rows == rows) return; - - // Resize our tabstops - if (self.cols != cols) { - const tabstops: Tabstops = try .init( + opts: Resize, +) ResizeError!void { + const tw = resize_tw; + + // Screen and scrolling-region invariants require non-zero dimensions. + // Validate before changing any terminal state. + if (opts.cols == 0 or opts.rows == 0) return error.InvalidValue; + + // Pixel geometry and synchronized output are updated on every valid + // resize attempt, including one that doesn't change the grid dimensions. + // Save their old values so later allocation failures can roll them back. + const old_width_px = self.width_px; + const old_height_px = self.height_px; + const old_synchronized_output = self.modes.get(.synchronized_output); + errdefer { + self.width_px = old_width_px; + self.height_px = old_height_px; + self.modes.set(.synchronized_output, old_synchronized_output); + } + + // If our pixel geometry was set, then we set it even if our rows/cols + // didn't change. + if (opts.cell_size_px) |cell_size| { + self.width_px = std.math.mul( + u32, + opts.cols, + cell_size.width, + ) catch std.math.maxInt(u32); + self.height_px = std.math.mul( + u32, + opts.rows, + cell_size.height, + ) catch std.math.maxInt(u32); + } + + self.modes.set(.synchronized_output, false); + + // If our cols/rows didn't change, skip grid work but still apply pixels. + if (self.cols == opts.cols and self.rows == opts.rows) return; + + // Build replacement tabstops without touching the current table. Keep + // ownership here until every fallible resize operation has succeeded. + var new_tabstops: ?Tabstops = null; + errdefer if (new_tabstops) |*v| v.deinit(alloc); + if (self.cols != opts.cols) { + try tw.check(.tabstops); + new_tabstops = try .init( alloc, - cols, + opts.cols, TABSTOP_INTERVAL, ); - self.tabstops.deinit(alloc); - self.tabstops = tabstops; } - // Resize primary screen, which supports reflow + // Resize primary screen, which supports reflow. We do this first + // because the cleanup situation is a lot better if this succeeds + // and alt fails than the reverse. + try tw.check(.primary_screen); const primary = self.screens.get(.primary).?; try primary.resize(.{ - .cols = cols, - .rows = rows, + .cols = opts.cols, + .rows = opts.rows, .reflow = self.modes.get(.wraparound), .prompt_redraw = self.flags.shell_redraws_prompt, }); - // Alternate screen, if it exists, doesn't reflow - if (self.screens.get(.alternate)) |alt| try alt.resize(.{ - .cols = cols, - .rows = rows, - .reflow = false, - }); + // Alternate screen, if it exists, doesn't reflow. The primary resize + // above can't be losslessly undone, so if the alternate resize fails we + // replace it with an empty screen at the requested size. If that + // also fails, we fall back to the primary screen. + if (self.screens.get(.alternate)) |alt| alt: { + const err: ResizeError = resize: { + tw.check(.alternate_screen) catch |err| break :resize err; + alt.resize(.{ + .cols = opts.cols, + .rows = opts.rows, + .reflow = false, + }) catch |err| break :resize err; + + // Resize succeeded. + break :alt; + }; + + log.warn("alternate screen resize failed, replacing it err={}", .{err}); + + // If the alternate screen isn't active, then we just free it + // and move on. It'll be reallocated when it gets reinitialized lazily. + // In this case, we just lose the prior data if the terminal program + // expected it to be saved. + if (self.screens.active_key != .alternate) { + self.screens.remove(alloc, .alternate); + break :alt; + } + + // The alt screen is active, so we temporarily switch to primary + // so we can safely remove the alt and recreate it blank. This loses + // the data but hopefully keeps us on the alt screen. + const charset = alt.charset; + self.screens.switchTo(.primary); + self.screens.remove(alloc, .alternate); + + // Replace the alt screen with an empty version. If this fails + // we just go back to the primary screen. Not great, but best + // we can do. + tw.check(.alternate_screen_init) catch break :alt; + const replacement = self.screens.getInit( + self.io(), + alloc, + .alternate, + .{ + .cols = opts.cols, + .rows = opts.rows, + .max_scrollback_bytes = 0, + .kitty_image_storage_limit = if (comptime build_options.kitty_graphics) + primary.kitty_images.total_limit + else + 0, + .kitty_image_loading_limits = if (comptime build_options.kitty_graphics) + primary.kitty_images.image_limits + else {}, + }, + ) catch |init_err| { + log.warn( + "alternate screen replacement failed, falling back to primary err={}", + .{init_err}, + ); + break :alt; + }; + + replacement.charset = charset; + self.screens.switchTo(.alternate); + } + + // No more failures are allowed after this point because the screens have + // committed their new sizes and the remaining Terminal state must follow. + errdefer comptime unreachable; + + // All fallible work is complete. Replace the old tabstop table only now. + if (new_tabstops) |v| { + self.tabstops.deinit(alloc); + self.tabstops = v; + new_tabstops = null; + } // Whenever we resize we just mark it as a screen clear self.flags.dirty.clear = true; // Set our size - self.cols = cols; - self.rows = rows; + self.cols = opts.cols; + self.rows = opts.rows; // Reset the scrolling region self.scrolling_region = .{ .top = 0, - .bottom = rows - 1, + .bottom = opts.rows - 1, .left = 0, - .right = cols - 1, + .right = opts.cols - 1, }; } +test "Terminal forwards optional scrollback limits" { + const max_lines: usize = 123; + var t = try init(testing.io, testing.allocator, .{ + .cols = 80, + .rows = 24, + .max_scrollback_bytes = null, + .max_scrollback_lines = max_lines, + }); + defer t.deinit(testing.allocator); + + try testing.expectEqual( + std.math.maxInt(usize), + t.screens.active.pages.limits.bytes.explicit, + ); + try testing.expectEqual( + max_lines, + t.screens.active.pages.limits.lines.explicit, + ); +} + +test "Terminal setScrollbackMaxBytes" { + var t = try init(testing.io, testing.allocator, .{ + .cols = 80, + .rows = 3, + .max_scrollback_bytes = null, + }); + defer t.deinit(testing.allocator); + + const primary = t.screens.get(.primary).?; + const page_rows: usize = primary.pages.pages.first.?.capacity().rows; + + // Build several complete pages of history so lowering the byte limit has + // existing allocations to prune immediately. + for (0..4 * page_rows) |_| try t.linefeed(); + const old_page_size = primary.pages.page_size; + t.setScrollbackMaxBytes(1); + try testing.expectEqual(@as(usize, 1), primary.pages.limits.bytes.explicit); + try testing.expect(primary.pages.page_size < old_page_size); + try testing.expect( + primary.pages.page_size <= primary.pages.limits.max(.bytes), + ); + try testing.expect(!primary.no_scrollback); + + // Zero switches Screen behavior as well as PageList accounting, discards + // all retained history, and prevents future linefeeds from recreating it. + t.setScrollbackMaxBytes(0); + try testing.expectEqual(@as(usize, 0), primary.pages.limits.bytes.explicit); + try testing.expect(primary.no_scrollback); + try testing.expectEqual( + @as(usize, primary.pages.rows), + primary.pages.total_rows, + ); + try testing.expect(primary.pages.viewport == .active); + + for (0..page_rows) |_| try t.linefeed(); + try testing.expectEqual( + @as(usize, primary.pages.rows), + primary.pages.total_rows, + ); + + // Re-enabling unlimited scrollback affects subsequent output. + t.setScrollbackMaxBytes(null); + try testing.expectEqual( + std.math.maxInt(usize), + primary.pages.limits.bytes.explicit, + ); + try testing.expect(!primary.no_scrollback); + for (0..page_rows) |_| try t.linefeed(); + try testing.expect(primary.pages.total_rows > primary.pages.rows); +} + +test "Terminal setScrollbackMaxLines" { + var t = try init(testing.io, testing.allocator, .{ + .cols = 80, + .rows = 3, + .max_scrollback_bytes = null, + .max_scrollback_lines = null, + }); + defer t.deinit(testing.allocator); + + const primary = t.screens.get(.primary).?; + const page_rows: usize = primary.pages.pages.first.?.capacity().rows; + + for (0..4 * page_rows) |_| try t.linefeed(); + const old_total_rows = primary.pages.total_rows; + t.setScrollbackMaxLines(page_rows); + try testing.expectEqual( + page_rows, + primary.pages.limits.lines.explicit, + ); + try testing.expect(primary.pages.total_rows < old_total_rows); + try testing.expect( + !primary.pages.limits.exceeded(&primary.pages, .lines), + ); + + const limited_total_rows = primary.pages.total_rows; + t.setScrollbackMaxLines(null); + try testing.expectEqual( + std.math.maxInt(usize), + primary.pages.limits.lines.explicit, + ); + try testing.expectEqual(limited_total_rows, primary.pages.total_rows); + for (0..3 * page_rows) |_| try t.linefeed(); + try testing.expect( + primary.pages.total_rows - primary.pages.rows > page_rows, + ); +} + +test "Terminal setScrollback only affects primary screen" { + var t = try init(testing.io, testing.allocator, .{ + .cols = 80, + .rows = 3, + }); + defer t.deinit(testing.allocator); + + _ = try t.switchScreen(.alternate); + const primary = t.screens.get(.primary).?; + const alternate = t.screens.get(.alternate).?; + + t.setScrollbackMaxBytes(123); + t.setScrollbackMaxLines(456); + + try testing.expectEqual( + @as(usize, 123), + primary.pages.limits.bytes.explicit, + ); + try testing.expectEqual( + @as(usize, 456), + primary.pages.limits.lines.explicit, + ); + try testing.expect(!primary.no_scrollback); + + try testing.expectEqual( + @as(usize, 0), + alternate.pages.limits.bytes.explicit, + ); + try testing.expectEqual( + std.math.maxInt(usize), + alternate.pages.limits.lines.explicit, + ); + try testing.expect(alternate.no_scrollback); + try testing.expectEqual(alternate, t.screens.active); +} + +test "Terminal: resize resets synchronized output" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); + defer t.deinit(alloc); + + t.modes.set(.synchronized_output, true); + try t.resize(alloc, .{ .cols = 10, .rows = 5 }); + try testing.expect(!t.modes.get(.synchronized_output)); +} + +test "Terminal: resize rejects zero dimensions before mutation" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); + defer t.deinit(alloc); + + t.width_px = 100; + t.height_px = 100; + t.flags.dirty.clear = false; + + try testing.expectError(error.InvalidValue, t.resize(alloc, .{ + .cols = 0, + .rows = 5, + .cell_size_px = .{ .width = 9, .height = 18 }, + })); + try testing.expectError(error.InvalidValue, t.resize(alloc, .{ + .cols = 10, + .rows = 0, + .cell_size_px = .{ .width = 9, .height = 18 }, + })); + + try testing.expectEqual(@as(size.CellCountInt, 10), t.cols); + try testing.expectEqual(@as(size.CellCountInt, 5), t.rows); + try testing.expectEqual(@as(u32, 100), t.width_px); + try testing.expectEqual(@as(u32, 100), t.height_px); + try testing.expect(!t.flags.dirty.clear); +} + +test "Terminal: resize preserves pixel dimensions when omitted" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); + defer t.deinit(alloc); + + t.width_px = 90; + t.height_px = 90; + try t.resize(alloc, .{ .cols = 20, .rows = 10 }); + + try testing.expectEqual(@as(u32, 90), t.width_px); + try testing.expectEqual(@as(u32, 90), t.height_px); +} + +test "Terminal: resize updates pixels without changing cell dimensions" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); + defer t.deinit(alloc); + + try t.resize(alloc, .{ + .cols = 10, + .rows = 5, + .cell_size_px = .{ .width = 9, .height = 18 }, + }); + + try testing.expectEqual(@as(u32, 90), t.width_px); + try testing.expectEqual(@as(u32, 90), t.height_px); +} + +test "Terminal: resize pixel dimensions saturate" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 3 }); + defer t.deinit(alloc); + + try t.resize(alloc, .{ + .cols = 2, + .rows = 3, + .cell_size_px = .{ + .width = std.math.maxInt(u32), + .height = std.math.maxInt(u32), + }, + }); + + try testing.expectEqual(std.math.maxInt(u32), t.width_px); + try testing.expectEqual(std.math.maxInt(u32), t.height_px); +} + test "Terminal: resize preserves tabstops on allocation failure" { var failing = testing.FailingAllocator.init(testing.allocator, .{}); const alloc = failing.allocator(); - var t = try init(alloc, .{ .cols = 10, .rows = 1 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 1 }); defer t.deinit(alloc); failing.fail_index = failing.alloc_index; - try testing.expectError(error.OutOfMemory, t.resize(alloc, 513, 1)); + try testing.expectError(error.OutOfMemory, t.resize(alloc, .{ + .cols = 513, + .rows = 1, + })); try testing.expectEqual(@as(size.CellCountInt, 10), t.cols); try testing.expect(t.tabstops.get(8)); } +test "Terminal: resize failure paths preserve consistent state" { + const alloc = testing.allocator; + const io_impl = testing.io; + + for ([_]resize_tw.FailPoint{ + .tabstops, + .primary_screen, + .alternate_screen, + }) |tag| { + const tw = resize_tw; + defer tw.end(.reset) catch unreachable; + + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 3 }); + defer t.deinit(alloc); + + try t.printString("primary"); + _ = try t.switchScreen(.alternate); + try t.printString("alternate"); + _ = try t.switchScreen(.primary); + + t.width_px = 100; + t.height_px = 50; + t.modes.set(.synchronized_output, true); + t.flags.dirty.clear = false; + t.scrolling_region = .{ + .top = 1, + .bottom = 2, + .left = 1, + .right = 8, + }; + t.tabstops.unset(8); + t.tabstops.set(3); + + const primary = t.screens.get(.primary).?; + const alternate = t.screens.get(.alternate).?; + const alternate_generation = t.screens.generation(.alternate); + try testing.expectEqual(primary.pages.pages.first, primary.pages.pages.last); + try testing.expectEqual(alternate.pages.pages.first, alternate.pages.pages.last); + + const before = t; + const before_primary = primary.*; + const before_alternate = alternate.*; + const before_primary_page = try alloc.dupe( + u8, + primary.pages.pages.first.?.page().memory, + ); + defer alloc.free(before_primary_page); + const before_alternate_page = try alloc.dupe( + u8, + alternate.pages.pages.first.?.page().memory, + ); + defer alloc.free(before_alternate_page); + tw.errorAlways(tag, error.OutOfMemory); + + // A failure after the primary screen has resized is recovered by + // dropping the inactive alternate and completing the resize. This + // also proves the alternate tripwire is after the primary resize + // rather than acting as a preflight check. + if (tag == .alternate_screen) { + try t.resize(alloc, .{ + .cols = 513, + .rows = 4, + .cell_size_px = .{ .width = 9, .height = 18 }, + }); + + try testing.expectEqual(@as(size.CellCountInt, 513), t.cols); + try testing.expectEqual(@as(size.CellCountInt, 4), t.rows); + try testing.expectEqual(@as(u32, 4617), t.width_px); + try testing.expectEqual(@as(u32, 72), t.height_px); + try testing.expect(!t.modes.get(.synchronized_output)); + try testing.expect(t.flags.dirty.clear); + try testing.expectEqual(@as(size.CellCountInt, 513), primary.pages.cols); + try testing.expectEqual(@as(size.CellCountInt, 4), primary.pages.rows); + try testing.expectEqual(@as(?*Screen, null), t.screens.get(.alternate)); + try testing.expectEqual( + alternate_generation +% 1, + t.screens.generation(.alternate), + ); + try testing.expectEqual(.primary, t.screens.active_key); + try testing.expectEqual(primary, t.screens.active); + try testing.expect(t.tabstops.get(8)); + try testing.expect(!t.tabstops.get(3)); + + // The alternate is recreated lazily at the terminal's new size. + _ = try t.switchScreen(.alternate); + const replacement = t.screens.get(.alternate).?; + try testing.expectEqual(@as(size.CellCountInt, 513), replacement.pages.cols); + try testing.expectEqual(@as(size.CellCountInt, 4), replacement.pages.rows); + try testing.expect(replacement.pages.getCell(.{ .active = .{} }).?.cell.isEmpty()); + continue; + } + + try testing.expectError(error.OutOfMemory, t.resize(alloc, .{ + .cols = 513, + .rows = 4, + .cell_size_px = .{ .width = 9, .height = 18 }, + })); + + try testing.expectEqual(before.width_px, t.width_px); + try testing.expectEqual(before.height_px, t.height_px); + try testing.expect(std.meta.eql(before.modes, t.modes)); + try testing.expectEqual(before.cols, t.cols); + try testing.expectEqual(before.rows, t.rows); + try testing.expectEqual(before.scrolling_region, t.scrolling_region); + try testing.expectEqual(before.flags, t.flags); + try testing.expect(std.meta.eql(before.tabstops, t.tabstops)); + try testing.expectEqual(before.screens.active_key, t.screens.active_key); + try testing.expectEqual(before.screens.active, t.screens.active); + + try testing.expectEqual(before_primary.pages.cols, primary.pages.cols); + try testing.expectEqual(before_primary.pages.rows, primary.pages.rows); + try testing.expectEqual( + before_primary.pages.total_rows, + primary.pages.total_rows, + ); + try testing.expect(std.meta.eql(before_primary.cursor, primary.cursor)); + try testing.expectEqualSlices( + u8, + before_primary_page, + primary.pages.pages.first.?.page().memory, + ); + + try testing.expectEqual(before_alternate.pages.cols, alternate.pages.cols); + try testing.expectEqual(before_alternate.pages.rows, alternate.pages.rows); + try testing.expectEqual( + before_alternate.pages.total_rows, + alternate.pages.total_rows, + ); + try testing.expect(std.meta.eql(before_alternate.cursor, alternate.cursor)); + try testing.expectEqualSlices( + u8, + before_alternate_page, + alternate.pages.pages.first.?.page().memory, + ); + } +} + +test "Terminal: alternate resize failure replaces active alternate screen" { + const alloc = testing.allocator; + const io_impl = testing.io; + const tw = resize_tw; + defer tw.end(.reset) catch unreachable; + + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 3 }); + defer t.deinit(alloc); + + _ = try t.switchScreen(.alternate); + try testing.expectEqual(.alternate, t.screens.active_key); + t.screens.active.charset.gl = .G1; + try t.printString("alternate"); + const generation = t.screens.generation(.alternate); + + tw.errorAlways(.alternate_screen, error.OutOfMemory); + try t.resize(alloc, .{ .cols = 20, .rows = 4 }); + + const alternate = t.screens.get(.alternate).?; + try testing.expectEqual(.alternate, t.screens.active_key); + try testing.expectEqual(alternate, t.screens.active); + try testing.expectEqual(@as(size.CellCountInt, 20), alternate.pages.cols); + try testing.expectEqual(@as(size.CellCountInt, 4), alternate.pages.rows); + try testing.expect(alternate.pages.getCell(.{ .active = .{} }).?.cell.isEmpty()); + try testing.expectEqual(.G1, alternate.charset.gl); + try testing.expectEqual(generation +% 1, t.screens.generation(.alternate)); +} + +test "Terminal: alternate resize replacement failure falls back to primary" { + const alloc = testing.allocator; + const io_impl = testing.io; + const tw = resize_tw; + defer tw.end(.reset) catch unreachable; + + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 3 }); + defer t.deinit(alloc); + + _ = try t.switchScreen(.alternate); + tw.errorAlways(.alternate_screen, error.OutOfMemory); + tw.errorAlways(.alternate_screen_init, error.OutOfMemory); + try t.resize(alloc, .{ .cols = 20, .rows = 4 }); + + const primary = t.screens.get(.primary).?; + try testing.expectEqual(@as(?*Screen, null), t.screens.get(.alternate)); + try testing.expectEqual(.primary, t.screens.active_key); + try testing.expectEqual(primary, t.screens.active); + try testing.expectEqual(@as(size.CellCountInt, 20), primary.pages.cols); + try testing.expectEqual(@as(size.CellCountInt, 4), primary.pages.rows); +} + /// Set the pwd for the terminal. pub fn setPwd(self: *Terminal, pwd: []const u8) !void { if (pwd.len == 0) { @@ -3625,7 +4647,8 @@ pub fn getPwd(self: *const Terminal) ?[:0]const u8 { test "Terminal: setPwd preserves a sentinel on allocation failure" { var failing = testing.FailingAllocator.init(testing.allocator, .{}); const alloc = failing.allocator(); - var t = try init(alloc, .{ .cols = 5, .rows = 1 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 1 }); defer t.deinit(alloc); try t.pwd.ensureTotalCapacityPrecise(alloc, 3); @@ -3635,7 +4658,7 @@ test "Terminal: setPwd preserves a sentinel on allocation failure" { } test "Terminal: setPwd accepts its current value" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 1 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 1 }); defer t.deinit(testing.allocator); try t.setPwd("file:///tmp"); @@ -3669,7 +4692,8 @@ pub fn getTitle(self: *const Terminal) ?[:0]const u8 { test "Terminal: setTitle preserves a sentinel on allocation failure" { var failing = testing.FailingAllocator.init(testing.allocator, .{}); const alloc = failing.allocator(); - var t = try init(alloc, .{ .cols = 5, .rows = 1 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 1 }); defer t.deinit(alloc); try t.title.ensureTotalCapacityPrecise(alloc, 5); @@ -3679,7 +4703,7 @@ test "Terminal: setTitle preserves a sentinel on allocation failure" { } test "Terminal: setTitle accepts its current value" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 1 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 1 }); defer t.deinit(testing.allocator); try t.setTitle("Ghostty"); @@ -3715,13 +4739,14 @@ pub fn switchScreen(self: *Terminal, key: ScreenSet.Key) !?*Screen { const new = self.screens.get(key) orelse new: { const primary = self.screens.get(.primary).?; break :new try self.screens.getInit( + old.io, old.alloc, key, .{ .cols = self.cols, .rows = self.rows, - .max_scrollback = switch (key) { - .primary => primary.pages.explicit_max_size, + .max_scrollback_bytes = switch (key) { + .primary => primary.pages.limits.bytes.explicit, .alternate => 0, }, @@ -3894,13 +4919,21 @@ pub fn fullReset(self: *Terminal) void { self.screens.active.reset(); // Rest our basic state + const visible = self.flags.visible; self.modes.reset(); - self.flags = .{}; + self.flags = .{ + // Visibility belongs to the view rather than terminal state, so a + // terminal reset must not make a hidden view potentially visible. + .visible = visible, + }; self.tabstops.reset(TABSTOP_INTERVAL); self.previous_char = null; self.pwd.clearRetainingCapacity(); self.title.clearRetainingCapacity(); self.glyph_glossary.clearAndFree(self.gpa()); + // A reset only interrupts an in-progress chunked OSC 72 command; + // drag and drop registration survives, matching kitty. + if (self.kitty_dnd) |dnd| dnd.chunking = .{}; self.status_display = .main; self.scrolling_region = .{ .top = 0, @@ -3908,6 +4941,7 @@ pub fn fullReset(self: *Terminal) void { .left = 0, .right = self.cols - 1, }; + self.setCursorStyle(.default); // Always mark dirty so we redraw everything self.flags.dirty.clear = true; @@ -3925,7 +4959,8 @@ fn clearDirty(t: *Terminal) void { test "Terminal: setCursorPos saturates overflowing origin offsets" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); t.scrolling_region = .{ @@ -3943,7 +4978,8 @@ test "Terminal: setCursorPos saturates overflowing origin offsets" { test "Terminal: input with no control characters" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 40, .rows = 40 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 40, .rows = 40 }); defer t.deinit(alloc); // Basic grid writing @@ -3963,7 +4999,8 @@ test "Terminal: input with no control characters" { test "Terminal: input with basic wraparound" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 40 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 40 }); defer t.deinit(alloc); // Basic grid writing @@ -3980,7 +5017,8 @@ test "Terminal: input with basic wraparound" { test "Terminal: input with basic wraparound dirty" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 40 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 40 }); defer t.deinit(alloc); for ("hello") |c| try t.print(c); @@ -3995,7 +5033,8 @@ test "Terminal: input with basic wraparound dirty" { test "Terminal: input that forces scroll" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 1, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 1, .rows = 5 }); defer t.deinit(alloc); // Basic grid writing @@ -4011,7 +5050,8 @@ test "Terminal: input that forces scroll" { test "Terminal: input unique style per cell" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 30, .rows = 30 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 30, .rows = 30 }); defer t.deinit(alloc); for (0..t.rows) |y| { @@ -4030,7 +5070,8 @@ test "Terminal: input unique style per cell" { test "Terminal: input glitch text" { const glitch = @embedFile("res/glitch.txt"); const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 30, .rows = 30 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 30, .rows = 30 }); defer t.deinit(alloc); // Get our initial grapheme capacity. @@ -4052,7 +5093,7 @@ test "Terminal: input glitch text" { } test "Terminal: zero-width character at start" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // This used to crash the terminal. This is not allowed so we should @@ -4068,7 +5109,7 @@ test "Terminal: zero-width character at start" { // https://github.com/ghostty-org/ghostty/pull/12581 test "Terminal: zero-width character attaches to pending wrap cell" { - var t = try init(testing.allocator, .{ .cols = 2, .rows = 2 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 2, .rows = 2 }); defer t.deinit(testing.allocator); // Disable grapheme clustering to exercise the fallback path. @@ -4083,9 +5124,32 @@ test "Terminal: zero-width character attaches to pending wrap cell" { try testing.expectEqualStrings("xå̲", str); } +test "Terminal: caps zero-width codepoints attached to one cell" { + var t = try init(testing.io, testing.allocator, .{ .cols = 2, .rows = 2 }); + defer t.deinit(testing.allocator); + + t.modes.set(.grapheme_cluster, false); + try t.print('A'); + + const initial_capacity = t.screens.active.cursor.page_pin.node.capacity().grapheme_bytes; + for (0..pagepkg.grapheme_max_len * 4) |_| try t.print(0x0301); + + const list_cell = t.screens.active.pages.getCell(.{ + .screen = .{ .x = 0, .y = 0 }, + }).?; + try testing.expectEqual( + @as(usize, pagepkg.grapheme_max_len), + list_cell.node.page().lookupGrapheme(list_cell.cell).?.len, + ); + try testing.expectEqual( + initial_capacity, + list_cell.node.capacity().grapheme_bytes, + ); +} + // https://github.com/mitchellh/ghostty/issues/1400 test "Terminal: print single very long line" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // This would crash for issue 1400. So the assertion here is @@ -4094,7 +5158,7 @@ test "Terminal: print single very long line" { } test "Terminal: print wide char" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); try t.print(0x1F600); // Smiley face @@ -4104,7 +5168,7 @@ test "Terminal: print wide char" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F600), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F600), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.wide, cell.wide); } { @@ -4117,7 +5181,7 @@ test "Terminal: print wide char" { } test "Terminal: print wide char at edge creates spacer head" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); t.setCursorPos(1, 10); @@ -4133,7 +5197,7 @@ test "Terminal: print wide char at edge creates spacer head" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 1 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F600), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F600), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.wide, cell.wide); } { @@ -4152,7 +5216,8 @@ test "Terminal: print wide char at edge creates spacer head" { test "Terminal: print wide char with 1-column width" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 1, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 1, .rows = 2 }); defer t.deinit(alloc); try t.print('😀'); // 0x1F600 @@ -4162,7 +5227,7 @@ test "Terminal: print wide char with 1-column width" { } test "Terminal: print wide char in single-width terminal" { - var t = try init(testing.allocator, .{ .cols = 1, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 1, .rows = 80 }); defer t.deinit(testing.allocator); try t.print(0x1F600); // Smiley face @@ -4173,7 +5238,7 @@ test "Terminal: print wide char in single-width terminal" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } @@ -4181,7 +5246,7 @@ test "Terminal: print wide char in single-width terminal" { } test "Terminal: print over wide char at 0,0" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); try t.print(0x1F600); // Smiley face @@ -4194,13 +5259,13 @@ test "Terminal: print over wide char at 0,0" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } @@ -4216,7 +5281,8 @@ test "Terminal: print over wide char at col 0 corrupts previous row" { // when that cell is a .spacer_tail rather than a .spacer_head. This // orphans the .wide cell at cols-2. const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 3 }); defer t.deinit(alloc); // Fill rows 0 and 1 with wide chars (5 per row on a 10-col terminal). @@ -4247,7 +5313,7 @@ test "Terminal: print over wide char at col 0 corrupts previous row" { } test "Terminal: print over wide spacer tail" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); try t.print('橋'); @@ -4257,13 +5323,13 @@ test "Terminal: print over wide spacer tail" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'X'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'X'), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } @@ -4277,7 +5343,7 @@ test "Terminal: print over wide spacer tail" { } test "Terminal: print over wide char with bold" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); try t.setAttribute(.{ .bold = {} }); @@ -4303,7 +5369,7 @@ test "Terminal: print over wide char with bold" { } test "Terminal: print over wide char with bg color" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); try t.setAttribute(.{ .direct_color_bg = .{ @@ -4333,7 +5399,7 @@ test "Terminal: print over wide char with bg color" { } test "Terminal: print multicodepoint grapheme, disabled mode 2027" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // https://github.com/mitchellh/ghostty/issues/289 @@ -4353,7 +5419,7 @@ test "Terminal: print multicodepoint grapheme, disabled mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F468), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F468), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -4362,7 +5428,7 @@ test "Terminal: print multicodepoint grapheme, disabled mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); try testing.expect(list_cell.node.page().lookupGrapheme(cell) == null); @@ -4370,7 +5436,7 @@ test "Terminal: print multicodepoint grapheme, disabled mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F469), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F469), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -4379,7 +5445,7 @@ test "Terminal: print multicodepoint grapheme, disabled mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 3, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); try testing.expect(list_cell.node.page().lookupGrapheme(cell) == null); @@ -4387,7 +5453,7 @@ test "Terminal: print multicodepoint grapheme, disabled mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F467), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F467), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); try testing.expect(list_cell.node.page().lookupGrapheme(cell) == null); @@ -4395,7 +5461,7 @@ test "Terminal: print multicodepoint grapheme, disabled mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 5, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); try testing.expect(list_cell.node.page().lookupGrapheme(cell) == null); @@ -4405,7 +5471,7 @@ test "Terminal: print multicodepoint grapheme, disabled mode 2027" { } test "Terminal: enabling grapheme mode handles stored breaks" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 1 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 1 }); defer t.deinit(testing.allocator); t.modes.set(.grapheme_cluster, false); @@ -4425,7 +5491,7 @@ test "Terminal: enabling grapheme mode handles stored breaks" { // cluster or string end. This keeps the streaming printer's cursor advance // in sync with the buffered measurement API for representative clusters. fn expectGraphemeWidthParity(cps: []const u21) !void { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 5 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 5 }); defer t.deinit(testing.allocator); t.modes.set(.grapheme_cluster, true); @@ -4459,7 +5525,7 @@ test "Terminal: graphemeWidth parity" { } test "Terminal: VS16 doesn't make character with 2027 disabled" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Disable grapheme clustering @@ -4477,7 +5543,7 @@ test "Terminal: VS16 doesn't make character with 2027 disabled" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x2764), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x2764), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -4486,7 +5552,7 @@ test "Terminal: VS16 doesn't make character with 2027 disabled" { } test "Terminal: ignored VS16 doesn't mark dirty" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Disable grapheme clustering @@ -4501,7 +5567,7 @@ test "Terminal: ignored VS16 doesn't mark dirty" { } test "Terminal: print invalid VS16 non-grapheme" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // https://github.com/mitchellh/ghostty/issues/1482 @@ -4517,19 +5583,19 @@ test "Terminal: print invalid VS16 non-grapheme" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'x'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); } } test "Terminal: invalid VS16 doesn't mark dirty" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Disable grapheme clustering @@ -4545,7 +5611,7 @@ test "Terminal: invalid VS16 doesn't mark dirty" { // https://github.com/ghostty-org/ghostty/pull/12596 test "Terminal: variation selectors apply to preceding codepoint" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 5 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 5 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4559,13 +5625,13 @@ test "Terminal: variation selectors apply to preceding codepoint" { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F3F4), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F3F4), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{ 0x200D, 0x2620, 0xFE0F }, list_cell.node.page().lookupGrapheme(cell).?); } test "Terminal: print multicodepoint grapheme, mode 2027" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4591,7 +5657,7 @@ test "Terminal: print multicodepoint grapheme, mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F468), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F468), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -4600,14 +5666,14 @@ test "Terminal: print multicodepoint grapheme, mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); } } test "Terminal: keypad sequence VS15" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4629,14 +5695,14 @@ test "Terminal: keypad sequence VS15" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x23), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x23), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: keypad sequence VS16" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4658,14 +5724,14 @@ test "Terminal: keypad sequence VS16" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x23), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x23), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); } } test "Terminal: Fitzpatrick skin tone next valid base" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4687,14 +5753,14 @@ test "Terminal: Fitzpatrick skin tone next valid base" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F44B), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F44B), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); } } test "Terminal: Fitzpatrick skin tone next to non-base" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4718,28 +5784,28 @@ test "Terminal: Fitzpatrick skin tone next to non-base" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x22), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x22), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F3FF), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F3FF), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 3, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x22), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x22), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: multicodepoint grapheme marks dirty on every codepoint" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4768,7 +5834,7 @@ test "Terminal: multicodepoint grapheme marks dirty on every codepoint" { } test "Terminal: VS15 to make narrow character" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4799,7 +5865,7 @@ test "Terminal: VS15 to make narrow character" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x2614), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x2614), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -4808,7 +5874,7 @@ test "Terminal: VS15 to make narrow character" { } test "Terminal: VS15 on already narrow emoji" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4834,7 +5900,7 @@ test "Terminal: VS15 on already narrow emoji" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x26C8), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x26C8), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -4843,7 +5909,7 @@ test "Terminal: VS15 on already narrow emoji" { } test "Terminal: print invalid VS15 following emoji is wide" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4861,20 +5927,20 @@ test "Terminal: print invalid VS15 following emoji is wide" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, '\u{1F9E0}'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '\u{1F9E0}'), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); } } test "Terminal: print invalid VS15 in emoji ZWJ sequence" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4894,7 +5960,7 @@ test "Terminal: print invalid VS15 in emoji ZWJ sequence" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, '\u{1F469}'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '\u{1F469}'), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{ '\u{200D}', '\u{1F466}' }, list_cell.node.page().lookupGrapheme(cell).?); try testing.expectEqual(Cell.Wide.wide, cell.wide); @@ -4902,13 +5968,13 @@ test "Terminal: print invalid VS15 in emoji ZWJ sequence" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); } } test "Terminal: VS15 to make narrow character with pending wrap" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 4 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 4 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4945,7 +6011,7 @@ test "Terminal: VS15 to make narrow character with pending wrap" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x2614), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x2614), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -4955,16 +6021,78 @@ test "Terminal: VS15 to make narrow character with pending wrap" { // VS15 should not affect the previous grapheme { const lemon_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?.cell; - try testing.expectEqual(@as(u21, 0x1F34B), lemon_cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F34B), lemon_cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.wide, lemon_cell.wide); const spacer_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?.cell; - try testing.expectEqual(@as(u21, 0), spacer_cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), spacer_cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.spacer_tail, spacer_cell.wide); } } +test "Terminal: VS15 narrows wide cell under cursor with wraparound disabled" { + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); + defer t.deinit(testing.allocator); + + t.modes.set(.grapheme_cluster, true); + t.modes.set(.wraparound, false); + + // First create a wide cell spanning columns 4 and 5. + t.setCursorPos(1, 4); + try t.print(0x2614); + + // Make column 4 the right margin and put the cursor on the wide base. + // With wraparound disabled, grapheme lookup selects the cell under the + // cursor when it has content. + t.modes.set(.enable_left_and_right_margin, true); + t.setLeftAndRightMargin(1, 4); + t.setCursorPos(1, 4); + try t.print(0xFE0E); + + try testing.expectEqual(@as(usize, 3), t.screens.active.cursor.x); + try testing.expect(!t.screens.active.cursor.pending_wrap); + const base = t.screens.active.pages.getCell(.{ .screen = .{ .x = 3, .y = 0 } }).?.cell; + try testing.expectEqual(Cell.Wide.narrow, base.wide); + try testing.expect(base.hasGrapheme()); + const tail = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?.cell; + try testing.expectEqual(Cell.Wide.narrow, tail.wide); +} + +test "Terminal: VS15 narrows wide cell under restored pending cursor" { + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); + defer t.deinit(testing.allocator); + + t.modes.set(.grapheme_cluster, true); + t.modes.set(.enable_left_and_right_margin, true); + t.setLeftAndRightMargin(1, 4); + + // Save a pending-wrap cursor at column 4. + t.setCursorPos(1, 4); + try t.print('X'); + try testing.expect(t.screens.active.cursor.pending_wrap); + t.saveCursor(); + + // Widen the margin and replace that cell with a wide character. + t.setLeftAndRightMargin(1, 5); + t.setCursorPos(1, 4); + try t.print(0x2614); + + // Restoring also restores pending_wrap, so grapheme lookup selects the + // wide base under the cursor rather than its spacer tail. + t.restoreCursor(); + try testing.expect(t.screens.active.cursor.pending_wrap); + try t.print(0xFE0E); + + try testing.expectEqual(@as(usize, 4), t.screens.active.cursor.x); + try testing.expect(!t.screens.active.cursor.pending_wrap); + const base = t.screens.active.pages.getCell(.{ .screen = .{ .x = 3, .y = 0 } }).?.cell; + try testing.expectEqual(Cell.Wide.narrow, base.wide); + try testing.expect(base.hasGrapheme()); + const tail = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?.cell; + try testing.expectEqual(Cell.Wide.narrow, tail.wide); +} + test "Terminal: VS16 to make wide character on next line" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 3 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 3 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -4989,7 +6117,7 @@ test "Terminal: VS16 to make wide character on next line" { // Previous cell turns into spacer_head const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_head, cell.wide); } @@ -4997,7 +6125,7 @@ test "Terminal: VS16 to make wide character on next line" { // '#' cell is wide const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 1 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, '#'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '#'), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{0xFE0F}, list_cell.node.page().lookupGrapheme(cell).?); try testing.expectEqual(Cell.Wide.wide, cell.wide); @@ -5006,7 +6134,7 @@ test "Terminal: VS16 to make wide character on next line" { // spacer_tail const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 1 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); } @@ -5015,7 +6143,7 @@ test "Terminal: VS16 to make wide character on next line" { test "Terminal: VS16 to make wide character on next line with hyperlink" { // Regression test for the crash fixed in print's grapheme `.wide` path: // writing a spacer_head at the screen edge before row.wrap was set. - var t = try init(testing.allocator, .{ .rows = 5, .cols = 3 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 3 }); defer t.deinit(testing.allocator); // Enable grapheme clustering and activate a hyperlink so printCell @@ -5039,33 +6167,133 @@ test "Terminal: VS16 to make wide character on next line with hyperlink" { // Previous cell turns into spacer_head and remains hyperlinked. const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.spacer_head, cell.wide); try testing.expect(cell.hyperlink); try testing.expect(list_cell.row.wrap); } { - // '#' cell is now wide and still hyperlinked. - const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 1 } }).?; + // '#' cell is now wide and still hyperlinked. + const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 1 } }).?; + const cell = list_cell.cell; + try testing.expectEqual(@as(u21, '#'), cell.content.codepoint.data); + try testing.expect(cell.hasGrapheme()); + try testing.expectEqualSlices(u21, &.{0xFE0F}, list_cell.node.page().lookupGrapheme(cell).?); + try testing.expectEqual(Cell.Wide.wide, cell.wide); + try testing.expect(cell.hyperlink); + } + { + // spacer_tail inherits hyperlink as part of the same grapheme cell. + const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 1 } }).?; + const cell = list_cell.cell; + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); + try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); + try testing.expect(cell.hyperlink); + } +} + +test "Terminal: VS16 widening when the spacer tail grows the page" { + // Regression test for a stale cell pointer in print's grapheme `.wide` + // path: writing the spacer tail can grow the page to fit the hyperlink, + // which replaces the page and invalidates the pointer to the wide cell. + var t = try init(testing.io, testing.allocator, .{ .rows = 10, .cols = 20 }); + defer t.deinit(testing.allocator); + + t.modes.set(.grapheme_cluster, true); + try t.screens.active.startHyperlink("http://example.com", null); + + // Fill the page hyperlink map until a single slot is left. The '#' below + // takes that slot so the spacer tail is what forces the page to grow. + while (true) { + const page = t.screens.active.cursor.page_pin.node.page(); + const map = page.hyperlink_map.map(page.memory); + if (map.maxLoad() - map.count() == 1) break; + try t.print('x'); + } + + const x = t.screens.active.cursor.x; + const y = t.screens.active.cursor.y; + try t.print('#'); + + // Without the fix this crashed appending to a freed page. + try t.print(0xFE0F); + + { + // '#' is wide and carries the VS16 grapheme. + const list_cell = t.screens.active.pages.getCell(.{ .active = .{ + .x = x, + .y = y, + } }).?; + const cell = list_cell.cell; + try testing.expectEqual(@as(u21, '#'), cell.content.codepoint.data); + try testing.expectEqual(Cell.Wide.wide, cell.wide); + try testing.expect(cell.hasGrapheme()); + try testing.expectEqualSlices( + u21, + &.{0xFE0F}, + list_cell.node.page().lookupGrapheme(cell).?, + ); + } + { + const list_cell = t.screens.active.pages.getCell(.{ .active = .{ + .x = x + 1, + .y = y, + } }).?; + try testing.expectEqual(Cell.Wide.spacer_tail, list_cell.cell.wide); + } +} + +test "Terminal: grapheme transfer when widening wraps to the next line" { + // Covers print's grapheme `.wide` path where the previous cell already + // holds grapheme data and has to be moved to the wrapped row. + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 3 }); + defer t.deinit(testing.allocator); + + t.modes.set(.grapheme_cluster, true); + t.cursorRight(2); + + // A narrow emoji, then ZWJ, then a second emoji. The ZWJ attaches + // without changing the width, so the cell has grapheme data by the time + // the second emoji widens it. + try t.print(0x263A); + try t.print(0x200D); + try t.print(0x2764); + + { + // The old cell becomes a spacer head on the wrapped row. + const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ + .x = 2, + .y = 0, + } }).?; + try testing.expectEqual(Cell.Wide.spacer_head, list_cell.cell.wide); + try testing.expect(list_cell.row.wrap); + } + { + // The grapheme moved with the base codepoint. + const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ + .x = 0, + .y = 1, + } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, '#'), cell.content.codepoint); - try testing.expect(cell.hasGrapheme()); - try testing.expectEqualSlices(u21, &.{0xFE0F}, list_cell.node.page().lookupGrapheme(cell).?); + try testing.expectEqual(@as(u21, 0x263A), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.wide, cell.wide); - try testing.expect(cell.hyperlink); + try testing.expectEqualSlices( + u21, + &.{ 0x200D, 0x2764 }, + list_cell.node.page().lookupGrapheme(cell).?, + ); } { - // spacer_tail inherits hyperlink as part of the same grapheme cell. - const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 1 } }).?; - const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); - try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); - try testing.expect(cell.hyperlink); + const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ + .x = 1, + .y = 1, + } }).?; + try testing.expectEqual(Cell.Wide.spacer_tail, list_cell.cell.wide); } } test "Terminal: VS16 to make wide character with pending wrap" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 3 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 3 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5086,7 +6314,7 @@ test "Terminal: VS16 to make wide character with pending wrap" { // '#' cell is wide const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, '#'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '#'), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{0xFE0F}, list_cell.node.page().lookupGrapheme(cell).?); try testing.expectEqual(Cell.Wide.wide, cell.wide); @@ -5095,14 +6323,14 @@ test "Terminal: VS16 to make wide character with pending wrap" { // spacer_tail const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); } } test "Terminal: VS16 to make wide character with mode 2027" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5124,7 +6352,7 @@ test "Terminal: VS16 to make wide character with mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x2764), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x2764), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -5133,7 +6361,7 @@ test "Terminal: VS16 to make wide character with mode 2027" { } test "Terminal: VS16 repeated with mode 2027" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5155,7 +6383,7 @@ test "Terminal: VS16 repeated with mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x2764), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x2764), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -5164,7 +6392,7 @@ test "Terminal: VS16 repeated with mode 2027" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x2764), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x2764), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.wide, cell.wide); const cps = list_cell.node.page().lookupGrapheme(cell).?; @@ -5173,7 +6401,7 @@ test "Terminal: VS16 repeated with mode 2027" { } test "Terminal: print invalid VS16 grapheme" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5192,20 +6420,20 @@ test "Terminal: print invalid VS16 grapheme" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'x'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: print invalid VS16 with second char" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5225,7 +6453,7 @@ test "Terminal: print invalid VS16 with second char" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'x'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'x'), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } @@ -5233,14 +6461,14 @@ test "Terminal: print invalid VS16 with second char" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'y'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'y'), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: print grapheme ò (o with nonspacing mark) should be narrow" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5258,7 +6486,7 @@ test "Terminal: print grapheme ò (o with nonspacing mark) should be narrow" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'o'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'o'), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{0x0300}, list_cell.node.page().lookupGrapheme(cell).?); try testing.expectEqual(Cell.Wide.narrow, cell.wide); @@ -5266,7 +6494,7 @@ test "Terminal: print grapheme ò (o with nonspacing mark) should be narrow" { } test "Terminal: print Devanagari grapheme should be wide" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5287,7 +6515,7 @@ test "Terminal: print Devanagari grapheme should be wide" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x0915), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x0915), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{ 0x094D, 0x200D, 0x0937 }, list_cell.node.page().lookupGrapheme(cell).?); try testing.expectEqual(Cell.Wide.wide, cell.wide); @@ -5300,7 +6528,7 @@ test "Terminal: print Devanagari grapheme should be wide" { } test "Terminal: print Devanagari grapheme should be wide on next line" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 3 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 3 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5327,7 +6555,7 @@ test "Terminal: print Devanagari grapheme should be wide on next line" { // Previous cell turns into spacer_head const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_head, cell.wide); } @@ -5335,7 +6563,7 @@ test "Terminal: print Devanagari grapheme should be wide on next line" { // Devanagari grapheme is wide const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 1 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x0915), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x0915), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{ 0x094D, 0x200D, 0x0937 }, list_cell.node.page().lookupGrapheme(cell).?); try testing.expectEqual(Cell.Wide.wide, cell.wide); @@ -5350,7 +6578,7 @@ test "Terminal: print Devanagari grapheme should be wide on next line" { test "Terminal: print Devanagari grapheme should be wide on next page" { const rows = pagepkg.std_capacity.rows; const cols = pagepkg.std_capacity.cols; - var t = try init(testing.allocator, .{ .rows = rows, .cols = cols }); + var t = try init(testing.io, testing.allocator, .{ .rows = rows, .cols = cols }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5386,7 +6614,7 @@ test "Terminal: print Devanagari grapheme should be wide on next page" { // Previous cell turns into spacer_head const list_cell = t.screens.active.pages.getCell(.{ .active = .{ .x = cols - 1, .y = rows - 2 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.spacer_head, cell.wide); } @@ -5394,7 +6622,7 @@ test "Terminal: print Devanagari grapheme should be wide on next page" { // Devanagari grapheme is wide const list_cell = t.screens.active.pages.getCell(.{ .active = .{ .x = 0, .y = rows - 1 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x0915), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x0915), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{ 0x094D, 0x200D, 0x0937 }, list_cell.node.page().lookupGrapheme(cell).?); try testing.expectEqual(Cell.Wide.wide, cell.wide); @@ -5407,7 +6635,7 @@ test "Terminal: print Devanagari grapheme should be wide on next page" { } test "Terminal: print invalid VS16 with second char (combining)" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5427,7 +6655,7 @@ test "Terminal: print invalid VS16 with second char (combining)" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'n'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'n'), cell.content.codepoint.data); try testing.expect(cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{'\u{0303}'}, list_cell.node.page().lookupGrapheme(cell).?); try testing.expectEqual(Cell.Wide.narrow, cell.wide); @@ -5435,13 +6663,13 @@ test "Terminal: print invalid VS16 with second char (combining)" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: overwrite grapheme should clear grapheme data" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5465,14 +6693,14 @@ test "Terminal: overwrite grapheme should clear grapheme data" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: overwrite multicodepoint grapheme clears grapheme data" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5512,7 +6740,7 @@ test "Terminal: overwrite multicodepoint grapheme clears grapheme data" { } test "Terminal: overwrite multicodepoint grapheme tail clears grapheme data" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); // Enable grapheme clustering @@ -5551,7 +6779,8 @@ test "Terminal: overwrite multicodepoint grapheme tail clears grapheme data" { test "Terminal: print breaks valid grapheme cluster with Prepend + ASCII for speed" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -5578,7 +6807,7 @@ test "Terminal: print breaks valid grapheme cluster with Prepend + ASCII for spe { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x0600), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x0600), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); // This is what we'd expect if we did break correctly: //try testing.expect(cell.hasGrapheme()); @@ -5588,16 +6817,16 @@ test "Terminal: print breaks valid grapheme cluster with Prepend + ASCII for spe { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, '1'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '1'), cell.content.codepoint.data); // This is what we'd expect if we did break correctly: - //try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + //try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expect(!cell.hasGrapheme()); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: print writes to bottom if scrolled" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 2 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 2 }); defer t.deinit(testing.allocator); // Basic grid writing @@ -5639,7 +6868,7 @@ test "Terminal: print writes to bottom if scrolled" { } test "Terminal: print charset" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // G1 should have no effect @@ -5668,7 +6897,7 @@ test "Terminal: print charset" { } test "Terminal: print charset outside of ASCII" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // G1 should have no effect @@ -5693,7 +6922,7 @@ test "Terminal: print charset outside of ASCII" { } test "Terminal: print invoke charset" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); t.configureCharset(.G1, .dec_special); @@ -5717,7 +6946,7 @@ test "Terminal: print invoke charset" { } test "Terminal: print invoke charset single" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); t.configureCharset(.G1, .dec_special); @@ -5737,7 +6966,7 @@ test "Terminal: print invoke charset single" { test "Terminal: print kitty unicode placeholder" { if (comptime !build_options.kitty_graphics) return error.SkipZigTest; - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); try t.print(kitty.graphics.unicode.placeholder); @@ -5747,7 +6976,7 @@ test "Terminal: print kitty unicode placeholder" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, kitty.graphics.unicode.placeholder), cell.content.codepoint); + try testing.expectEqual(@as(u21, kitty.graphics.unicode.placeholder), cell.content.codepoint.data); try testing.expect(list_cell.row.kitty_virtual_placeholder); } @@ -5755,7 +6984,7 @@ test "Terminal: print kitty unicode placeholder" { } test "Terminal: soft wrap" { - var t = try init(testing.allocator, .{ .cols = 3, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 3, .rows = 80 }); defer t.deinit(testing.allocator); // Basic grid writing @@ -5770,7 +6999,7 @@ test "Terminal: soft wrap" { } test "Terminal: soft wrap with semantic prompt" { - var t = try init(testing.allocator, .{ .cols = 3, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 3, .rows = 80 }); defer t.deinit(testing.allocator); // Mark our prompt. @@ -5791,7 +7020,7 @@ test "Terminal: soft wrap with semantic prompt" { } test "Terminal: disabled wraparound with wide char and one space" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); t.modes.set(.wraparound, false); @@ -5814,7 +7043,7 @@ test "Terminal: disabled wraparound with wide char and one space" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } @@ -5823,7 +7052,7 @@ test "Terminal: disabled wraparound with wide char and one space" { } test "Terminal: disabled wraparound with wide char and no space" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); t.modes.set(.wraparound, false); @@ -5845,7 +7074,7 @@ test "Terminal: disabled wraparound with wide char and no space" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } @@ -5854,7 +7083,7 @@ test "Terminal: disabled wraparound with wide char and no space" { } test "Terminal: disabled wraparound with wide grapheme and half space" { - var t = try init(testing.allocator, .{ .rows = 5, .cols = 5 }); + var t = try init(testing.io, testing.allocator, .{ .rows = 5, .cols = 5 }); defer t.deinit(testing.allocator); t.modes.set(.grapheme_cluster, true); @@ -5878,7 +7107,7 @@ test "Terminal: disabled wraparound with wide grapheme and half space" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, '❤'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '❤'), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } @@ -5887,7 +7116,7 @@ test "Terminal: disabled wraparound with wide grapheme and half space" { } test "Terminal: print right margin wrap" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 5 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("123456789"); @@ -5910,7 +7139,7 @@ test "Terminal: print right margin wrap" { } test "Terminal: print right margin wrap dirty tracking" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 5 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("123456789"); @@ -5939,7 +7168,7 @@ test "Terminal: print right margin wrap dirty tracking" { } test "Terminal: print right margin outside" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 5 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("123456789"); @@ -5959,7 +7188,7 @@ test "Terminal: print right margin outside" { } test "Terminal: print right margin outside wrap" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 5 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5 }); defer t.deinit(testing.allocator); try t.printString("123456789"); @@ -5976,7 +7205,7 @@ test "Terminal: print right margin outside wrap" { } test "Terminal: print wide char at right margin does not create spacer head" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); t.modes.set(.enable_left_and_right_margin, true); @@ -5993,7 +7222,7 @@ test "Terminal: print wide char at right margin does not create spacer head" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); const row = list_cell.row; @@ -6002,7 +7231,7 @@ test "Terminal: print wide char at right margin does not create spacer head" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 2, .y = 1 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x1F600), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x1F600), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.wide, cell.wide); } { @@ -6013,7 +7242,7 @@ test "Terminal: print wide char at right margin does not create spacer head" { } test "Terminal: print with hyperlink" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Setup our hyperlink and print @@ -6038,7 +7267,7 @@ test "Terminal: print with hyperlink" { } test "Terminal: print over cell with same hyperlink" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Setup our hyperlink and print @@ -6065,7 +7294,7 @@ test "Terminal: print over cell with same hyperlink" { } test "Terminal: print and end hyperlink" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Setup our hyperlink and print @@ -6102,7 +7331,7 @@ test "Terminal: print and end hyperlink" { } test "Terminal: print and change hyperlink" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Setup our hyperlink and print @@ -6137,7 +7366,7 @@ test "Terminal: print and change hyperlink" { } test "Terminal: overwrite hyperlink" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Setup our hyperlink and print @@ -6170,7 +7399,7 @@ test "Terminal: overwrite hyperlink" { // flag. The integrity check inside setHyperlink (or increaseCapacity) // sees the unwrapped spacer head and panics. Found via fuzzing. test "Terminal: print wide char at right edge with hyperlink" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 5 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5 }); defer t.deinit(testing.allocator); try t.screens.active.startHyperlink("http://example.com", null); @@ -6197,7 +7426,7 @@ test "Terminal: print wide char at right edge with hyperlink" { // Row 1, col 0: the wide char with hyperlink { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 1 } }).?; - try testing.expectEqual(@as(u21, 0x4E2D), list_cell.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x4E2D), list_cell.cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.wide, list_cell.cell.wide); try testing.expect(list_cell.cell.hyperlink); } @@ -6209,8 +7438,87 @@ test "Terminal: print wide char at right edge with hyperlink" { } } +// A cursor style or hyperlink ID is an index into a set stored in the +// page memory of the page the cursor pin points at. When scrollClear +// (here via ED 22, kitty's scroll_complete) pushes the active area onto +// a later page while the cursor pin is still on an earlier one, +// cursorReload must migrate the cursor's style and hyperlink references +// to the destination page. It previously replaced the pin directly, +// leaving the cursor holding an ID that was dead or aliased an unrelated +// entry on the new page, and the next print attached a live cell to it. +// Found via fuzzing. +test "Terminal: scrollClear across pages keeps cursor hyperlink refs page-local" { + const alloc = testing.allocator; + + // Minimized from a 774-byte AFL fuzz input. Reading it: + // + // A print, so REP has something to repeat + // ESC [ 48111 b REP, filling the page and spilling onto a second + // ESC ] 8 ; ; 0x93 OSC 8; the C1 byte terminates the OSC and makes + // the URI non-empty, so a hyperlink starts + // ESC [ 11 A CUU, moving the cursor back onto the first page + // ESC [ 22 J ED 22, i.e. scroll_complete -> Screen.scrollClear + // B print, which attaches the cursor hyperlink + // ESC ] 8 ; ; ESC OSC 8 with an empty URI, ending the hyperlink + // + // The grid must be wide enough to fill a page from a single REP, so + // this does not reproduce at 80x24. + const input = "A\x1b[48111b\x1b]8;;\x93\x1b[11A\x1b[22JB\x1b]8;;\x1b"; + + var t = try init(testing.io, alloc, .{ .cols = 200, .rows = 50 }); + defer t.deinit(alloc); + + { + var s = t.vtStream(); + defer s.deinit(); + s.nextSlice(input); + } + + // With slow runtime safety on, the page integrity checks during the + // stream above already catch the bug. Verify the ref counts explicitly + // as well so this test is meaningful with runtime safety off: every + // cell holding a hyperlink ID owns a reference, so a count below the + // number of holding cells means a live cell points at an entry that + // was already freed. + var node_ = t.screens.active.pages.pages.first; + while (node_) |node| : (node_ = node.next) { + const page = node.page(); + const cap = page.hyperlink_set.layout.cap; + if (cap == 0) continue; + + const holders = try alloc.alloc(u32, cap); + defer alloc.free(holders); + @memset(holders, 0); + + for (page.rows.ptr(page.memory)[0..page.size.rows]) |*row| { + if (!row.hyperlink) continue; + for (row.cells.ptr(page.memory)[0..page.size.cols]) |*cell| { + if (!cell.hyperlink) continue; + const id = page.lookupHyperlink(cell) orelse continue; + if (id < cap) holders[id] += 1; + } + } + + for (holders, 0..) |held, id| { + if (held == 0) continue; + const refs = page.hyperlink_set.refCount(page.memory, @intCast(id)); + try testing.expect(refs >= held); + } + } + + // If the cursor still has an active hyperlink, its own extra + // reference must live on the cursor's page. + const cursor = &t.screens.active.cursor; + if (cursor.hyperlink_id != 0) { + const page = cursor.page_pin.node.page(); + try testing.expect( + page.hyperlink_set.refCount(page.memory, cursor.hyperlink_id) > 0, + ); + } +} + test "Terminal: linefeed and carriage return" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Print and CR. @@ -6238,7 +7546,7 @@ test "Terminal: linefeed and carriage return" { } test "Terminal: linefeed unsets pending wrap" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 80 }); defer t.deinit(testing.allocator); // Basic grid writing @@ -6252,7 +7560,7 @@ test "Terminal: linefeed unsets pending wrap" { } test "Terminal: linefeed mode automatic carriage return" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); // Basic grid writing @@ -6268,7 +7576,7 @@ test "Terminal: linefeed mode automatic carriage return" { } test "Terminal: carriage return unsets pending wrap" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 80 }); defer t.deinit(testing.allocator); // Basic grid writing @@ -6279,7 +7587,7 @@ test "Terminal: carriage return unsets pending wrap" { } test "Terminal: carriage return origin mode moves to left margin" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 80 }); defer t.deinit(testing.allocator); t.modes.set(.origin, true); @@ -6290,7 +7598,7 @@ test "Terminal: carriage return origin mode moves to left margin" { } test "Terminal: carriage return left of left margin moves to zero" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 80 }); defer t.deinit(testing.allocator); t.screens.active.cursor.x = 1; @@ -6300,7 +7608,7 @@ test "Terminal: carriage return left of left margin moves to zero" { } test "Terminal: carriage return right of left margin moves to left margin" { - var t = try init(testing.allocator, .{ .cols = 5, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 5, .rows = 80 }); defer t.deinit(testing.allocator); t.screens.active.cursor.x = 3; @@ -6310,7 +7618,7 @@ test "Terminal: carriage return right of left margin moves to left margin" { } test "Terminal: backspace" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // BS @@ -6328,7 +7636,8 @@ test "Terminal: backspace" { test "Terminal: horizontal tabs" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 20, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 20, .rows = 5 }); defer t.deinit(alloc); // HT @@ -6349,7 +7658,8 @@ test "Terminal: horizontal tabs" { test "Terminal: horizontal tabs starting on tabstop" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 20, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 20, .rows = 5 }); defer t.deinit(alloc); t.setCursorPos(t.screens.active.cursor.y, 9); @@ -6367,7 +7677,8 @@ test "Terminal: horizontal tabs starting on tabstop" { test "Terminal: horizontal tabs with right margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 20, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 20, .rows = 5 }); defer t.deinit(alloc); t.scrolling_region.left = 2; @@ -6386,7 +7697,8 @@ test "Terminal: horizontal tabs with right margin" { test "Terminal: horizontal tabs back" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 20, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 20, .rows = 5 }); defer t.deinit(alloc); // Edge of screen @@ -6409,7 +7721,8 @@ test "Terminal: horizontal tabs back" { test "Terminal: horizontal tabs back starting on tabstop" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 20, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 20, .rows = 5 }); defer t.deinit(alloc); t.setCursorPos(t.screens.active.cursor.y, 9); @@ -6427,7 +7740,8 @@ test "Terminal: horizontal tabs back starting on tabstop" { test "Terminal: horizontal tabs with left margin in origin mode" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 20, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 20, .rows = 5 }); defer t.deinit(alloc); t.modes.set(.origin, true); @@ -6447,7 +7761,8 @@ test "Terminal: horizontal tabs with left margin in origin mode" { test "Terminal: horizontal tab back with cursor before left margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 20, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 20, .rows = 5 }); defer t.deinit(alloc); t.modes.set(.origin, true); @@ -6467,7 +7782,8 @@ test "Terminal: horizontal tab back with cursor before left margin" { test "Terminal: cursorPos resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -6485,7 +7801,8 @@ test "Terminal: cursorPos resets wrap" { test "Terminal: cursorPos off the screen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setCursorPos(500, 500); @@ -6500,7 +7817,8 @@ test "Terminal: cursorPos off the screen" { test "Terminal: cursorPos relative to origin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.scrolling_region.top = 2; @@ -6518,7 +7836,8 @@ test "Terminal: cursorPos relative to origin" { test "Terminal: cursorPos relative to origin with left/right" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.scrolling_region.top = 2; @@ -6538,7 +7857,8 @@ test "Terminal: cursorPos relative to origin with left/right" { test "Terminal: cursorPos limits with full scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.scrolling_region.top = 2; @@ -6558,7 +7878,7 @@ test "Terminal: cursorPos limits with full scroll region" { // Probably outdated, but dates back to the original terminal implementation. test "Terminal: setCursorPos (original test)" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.x); @@ -6611,7 +7931,8 @@ test "Terminal: setCursorPos (original test)" { test "Terminal: setTopAndBottomMargin simple" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6641,7 +7962,8 @@ test "Terminal: setTopAndBottomMargin simple" { test "Terminal: setTopAndBottomMargin top only" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6671,7 +7993,8 @@ test "Terminal: setTopAndBottomMargin top only" { test "Terminal: setTopAndBottomMargin top and bottom" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6699,7 +8022,8 @@ test "Terminal: setTopAndBottomMargin top and bottom" { test "Terminal: setTopAndBottomMargin top equal to bottom" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6728,7 +8052,8 @@ test "Terminal: setTopAndBottomMargin top equal to bottom" { test "Terminal: setLeftAndRightMargin simple" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6756,7 +8081,8 @@ test "Terminal: setLeftAndRightMargin simple" { test "Terminal: setLeftAndRightMargin left only" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6789,7 +8115,8 @@ test "Terminal: setLeftAndRightMargin left only" { test "Terminal: setLeftAndRightMargin left and right" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6820,7 +8147,8 @@ test "Terminal: setLeftAndRightMargin left and right" { test "Terminal: setLeftAndRightMargin left equal right" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6851,7 +8179,8 @@ test "Terminal: setLeftAndRightMargin left equal right" { test "Terminal: setLeftAndRightMargin mode 69 unset" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6882,7 +8211,8 @@ test "Terminal: setLeftAndRightMargin mode 69 unset" { test "Terminal: insertLines simple" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6914,7 +8244,8 @@ test "Terminal: insertLines simple" { test "Terminal: insertLines colors with bg color" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6955,7 +8286,8 @@ test "Terminal: insertLines colors with bg color" { test "Terminal: insertLines handles style refs" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -6989,7 +8321,8 @@ test "Terminal: insertLines handles style refs" { test "Terminal: insertLines outside of scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -7018,7 +8351,8 @@ test "Terminal: insertLines outside of scroll region" { test "Terminal: insertLines top/bottom scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -7051,7 +8385,8 @@ test "Terminal: insertLines top/bottom scroll region" { test "Terminal: insertLines across page boundary marks all shifted rows dirty" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 10, .max_scrollback = 1024 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 10, .max_scrollback_bytes = 1024 }); defer t.deinit(alloc); const first_page = t.screens.active.pages.pages.first.?; @@ -7099,9 +8434,96 @@ test "Terminal: insertLines across page boundary marks all shifted rows dirty" { } } +test "Terminal: insertLines hyperlink-dense row crosses page boundary" { + // Regression test for the cross-page copy of insertLines: when the + // shifted row carries more unique hyperlinks than the destination + // page's hyperlink capacity, the copy must increase the destination + // page's capacity and retry rather than corrupting the page list. + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 10, .max_scrollback_bytes = 1024 }); + defer t.deinit(alloc); + + const pages = &t.screens.active.pages; + + // Fill the first page so it is exactly full, then two more rows so + // the second page holds the last two active rows (y=3 and y=4). + const first_page_rows = pages.pages.first.?.capacity().rows; + for (0..first_page_rows + 1) |_| try t.linefeed(); + try testing.expect(pages.pages.first != pages.pages.last); + try testing.expectEqual(@as(usize, 2), pages.pages.last.?.rows()); + + // Marker rows so we can verify the shift afterwards. + t.setCursorPos(1, 1); + try t.printString("0"); + t.setCursorPos(2, 1); + try t.printString("1"); + t.setCursorPos(4, 1); + try t.printString("3"); + t.setCursorPos(5, 1); + try t.printString("4"); + + // Fill the last row of the first page (active y=2) with unique + // hyperlinks: more than the second page can hold with its default + // hyperlink capacity. Writing them grows the first page's capacity + // as needed; the second page keeps its default capacity. + t.setCursorPos(3, 1); + for (0..10) |i| { + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{i}); + try t.screens.active.startHyperlink(uri, null); + try t.print(@intCast('A' + i)); + t.screens.active.endHyperlink(); + } + { + const pin = pages.pin(.{ .active = .{ .y = 2 } }).?; + try testing.expectEqual(pages.pages.first.?, pin.node); + try testing.expectEqual(pin.node.rows() - 1, @as(usize, pin.y)); + } + try testing.expect(pages.pages.last.?.page().hyperlink_set.layout.cap < 10); + + // Insert a line at the top: every row shifts down by one and the + // dense row crosses the page boundary into the second page. + t.setCursorPos(1, 1); + t.insertLines(1); + + { + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("\n0\n1\nABCDEFGHIJ\n3", str); + } + + // The second page's hyperlink capacity had to grow to receive the + // row, proving the capacity-retry path ran. + try testing.expect(pages.pages.last.?.page().hyperlink_set.layout.cap >= 10); + + // Every cell of the dense row must still resolve to a real + // hyperlink entry with the correct URI. A half-applied shift + // leaves cells whose hyperlink flag is set but that have no map + // entry, which aborts in clearCells later. + for (0..10) |x| { + const list_cell = pages.getCell(.{ .active = .{ + .x = @intCast(x), + .y = 3, + } }).?; + try testing.expect(list_cell.cell.hyperlink); + const page: *Page = list_cell.node.page(); + const id = page.lookupHyperlink(list_cell.cell).?; + const link = page.hyperlink_set.get(page.memory, id); + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{x}); + try testing.expectEqualStrings(uri, link.uri.slice(page.memory)); + } + + // All pages must pass integrity checks. + var node_: ?*PageList.List.Node = pages.pages.first; + while (node_) |node| : (node_ = node.next) node.page().assertIntegrity(); +} + test "Terminal: insertLines (legacy test)" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); // Initial value @@ -7134,7 +8556,8 @@ test "Terminal: insertLines (legacy test)" { test "Terminal: insertLines zero" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); // This should do nothing @@ -7144,7 +8567,8 @@ test "Terminal: insertLines zero" { test "Terminal: insertLines with scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 6 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 6 }); defer t.deinit(alloc); // Initial value @@ -7183,7 +8607,8 @@ test "Terminal: insertLines with scroll region" { test "Terminal: insertLines more than remaining" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); // Initial value @@ -7221,7 +8646,8 @@ test "Terminal: insertLines more than remaining" { test "Terminal: insertLines resets pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -7239,7 +8665,8 @@ test "Terminal: insertLines resets pending wrap" { test "Terminal: insertLines resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 3, .cols = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); try t.print('1'); @@ -7265,7 +8692,8 @@ test "Terminal: insertLines resets wrap" { test "Terminal: insertLines multi-codepoint graphemes" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Disable grapheme clustering @@ -7297,7 +8725,8 @@ test "Terminal: insertLines multi-codepoint graphemes" { test "Terminal: insertLines left/right scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -7328,7 +8757,8 @@ test "Terminal: insertLines left/right scroll region" { test "Terminal: scrollUp simple" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -7361,7 +8791,8 @@ test "Terminal: scrollUp simple" { test "Terminal: scrollUp moves hyperlink" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -7412,7 +8843,8 @@ test "Terminal: scrollUp moves hyperlink" { test "Terminal: scrollUp clears hyperlink" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.screens.active.startHyperlink("http://example.com", null); @@ -7449,7 +8881,8 @@ test "Terminal: scrollUp clears hyperlink" { test "Terminal: scrollUp top/bottom scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -7479,7 +8912,8 @@ test "Terminal: scrollUp top/bottom scroll region" { test "Terminal: scrollUp left/right scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -7512,7 +8946,8 @@ test "Terminal: scrollUp left/right scroll region" { test "Terminal: scrollUp left/right scroll region hyperlink" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -7618,7 +9053,8 @@ test "Terminal: scrollUp left/right scroll region hyperlink" { test "Terminal: scrollUp preserves pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setCursorPos(1, 5); @@ -7639,7 +9075,8 @@ test "Terminal: scrollUp preserves pending wrap" { test "Terminal: scrollUp full top/bottom region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("top"); @@ -7663,7 +9100,8 @@ test "Terminal: scrollUp full top/bottom region" { test "Terminal: scrollUp full top/bottomleft/right scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("top"); @@ -7694,7 +9132,8 @@ test "Terminal: scrollUp creates scrollback in primary screen" { // When in primary screen with full-width scroll region at top, // scrollUp (CSI S) should push lines into scrollback like xterm. const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5, .max_scrollback = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5, .max_scrollback_bytes = 10 }); defer t.deinit(alloc); // Fill the screen with content @@ -7737,10 +9176,11 @@ test "Terminal: scrollUp creates scrollback in primary screen" { } } -test "Terminal: scrollUp with max_scrollback zero" { - // When max_scrollback is 0, scrollUp should still work but not retain history +test "Terminal: scrollUp with max_scrollback_bytes zero" { + // When max_scrollback_bytes is 0, scrollUp should still work but not retain history const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5, .max_scrollback = 0 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5, .max_scrollback_bytes = 0 }); defer t.deinit(alloc); try t.printString("AAAAA"); @@ -7769,10 +9209,11 @@ test "Terminal: scrollUp with max_scrollback zero" { } } -test "Terminal: scrollUp with max_scrollback zero and top margin" { - // When max_scrollback is 0 and top margin is set, should use deleteLines path +test "Terminal: scrollUp with max_scrollback_bytes zero and top margin" { + // When max_scrollback_bytes is 0 and top margin is set, should use deleteLines path const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5, .max_scrollback = 0 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5, .max_scrollback_bytes = 0 }); defer t.deinit(alloc); try t.printString("AAAAA"); @@ -7799,10 +9240,11 @@ test "Terminal: scrollUp with max_scrollback zero and top margin" { } } -test "Terminal: scrollUp with max_scrollback zero and left/right margin" { - // When max_scrollback is 0 with left/right margins, uses deleteLines path +test "Terminal: scrollUp with max_scrollback_bytes zero and left/right margin" { + // When max_scrollback_bytes is 0 with left/right margins, uses deleteLines path const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 10, .max_scrollback = 0 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 10, .max_scrollback_bytes = 0 }); defer t.deinit(alloc); try t.printString("AAAAABBBBB"); @@ -7829,7 +9271,8 @@ test "Terminal: scrollUp with max_scrollback zero and left/right margin" { test "Terminal: scrollDown simple" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -7861,7 +9304,8 @@ test "Terminal: scrollDown simple" { test "Terminal: scrollDown hyperlink moves" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.screens.active.startHyperlink("http://example.com", null); @@ -7912,7 +9356,8 @@ test "Terminal: scrollDown hyperlink moves" { test "Terminal: scrollDown outside of scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -7947,7 +9392,8 @@ test "Terminal: scrollDown outside of scroll region" { test "Terminal: scrollDown left/right scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -7981,7 +9427,8 @@ test "Terminal: scrollDown left/right scroll region" { test "Terminal: scrollDown left/right scroll region hyperlink" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.screens.active.startHyperlink("http://example.com", null); @@ -8087,7 +9534,8 @@ test "Terminal: scrollDown left/right scroll region hyperlink" { test "Terminal: scrollDown outside of left/right scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -8121,7 +9569,8 @@ test "Terminal: scrollDown outside of left/right scroll region" { test "Terminal: scrollDown preserves pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 10 }); defer t.deinit(alloc); t.setCursorPos(1, 5); @@ -8142,7 +9591,8 @@ test "Terminal: scrollDown preserves pending wrap" { test "Terminal: eraseChars simple operation" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -8163,7 +9613,8 @@ test "Terminal: eraseChars simple operation" { test "Terminal: eraseChars minimum one" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -8182,7 +9633,8 @@ test "Terminal: eraseChars minimum one" { test "Terminal: eraseChars beyond screen edge" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for (" ABC") |c| try t.print(c); @@ -8198,7 +9650,8 @@ test "Terminal: eraseChars beyond screen edge" { test "Terminal: eraseChars wide character" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.print('橋'); @@ -8216,7 +9669,8 @@ test "Terminal: eraseChars wide character" { test "Terminal: eraseChars resets pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -8234,7 +9688,8 @@ test "Terminal: eraseChars resets pending wrap" { test "Terminal: eraseChars resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE123") |c| try t.print(c); @@ -8264,7 +9719,8 @@ test "Terminal: eraseChars resets wrap" { test "Terminal: eraseChars preserves background sgr" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -8303,7 +9759,8 @@ test "Terminal: eraseChars preserves background sgr" { test "Terminal: eraseChars handles refcounted styles" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.setAttribute(.{ .bold = {} }); @@ -8325,7 +9782,8 @@ test "Terminal: eraseChars handles refcounted styles" { test "Terminal: eraseChars protected attributes respected with iso" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -8342,7 +9800,8 @@ test "Terminal: eraseChars protected attributes respected with iso" { test "Terminal: eraseChars protected attributes ignored with dec most recent" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -8361,7 +9820,8 @@ test "Terminal: eraseChars protected attributes ignored with dec most recent" { test "Terminal: eraseChars protected attributes ignored with dec set" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.dec); @@ -8378,7 +9838,8 @@ test "Terminal: eraseChars protected attributes ignored with dec set" { test "Terminal: eraseChars wide char boundary conditions" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 1, .cols = 8 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 1, .cols = 8 }); defer t.deinit(alloc); try t.printString("😀a😀b😀"); @@ -8401,7 +9862,8 @@ test "Terminal: eraseChars wide char boundary conditions" { test "Terminal: eraseChars wide char splits proper cell boundaries" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 1, .cols = 30 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 1, .cols = 30 }); defer t.deinit(alloc); // This is a test for a bug: https://github.com/ghostty-org/ghostty/issues/2817 @@ -8431,7 +9893,8 @@ test "Terminal: eraseChars wide char splits proper cell boundaries" { test "Terminal: eraseChars wide char wrap boundary conditions" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 3, .cols = 8 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 3, .cols = 8 }); defer t.deinit(alloc); try t.printString(".......😀abcde😀......"); @@ -8460,9 +9923,42 @@ test "Terminal: eraseChars wide char wrap boundary conditions" { } } +test "Terminal: eraseChars clearing wrapped wide char marks spacer head row dirty" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 3, .cols = 5 }); + defer t.deinit(alloc); + + // The wide char doesn't fit so it wraps, leaving a spacer head at + // the end of the first row. + try t.printString("ABCD字"); + { + const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?; + try testing.expectEqual(Cell.Wide.spacer_head, list_cell.cell.wide); + try testing.expect(list_cell.row.wrap); + } + + t.setCursorPos(2, 1); + t.clearDirty(); + t.eraseChars(1); + t.screens.active.cursor.page_pin.node.page().assertIntegrity(); + + // Erasing the wide char also clears the spacer head on the previous + // row, so that row must be dirty too. + try testing.expect(t.isDirty(.{ .screen = .{ .x = 0, .y = 0 } })); + try testing.expect(t.isDirty(.{ .screen = .{ .x = 0, .y = 1 } })); + try testing.expect(!t.isDirty(.{ .screen = .{ .x = 0, .y = 2 } })); + + { + const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 4, .y = 0 } }).?; + try testing.expectEqual(Cell.Wide.narrow, list_cell.cell.wide); + } +} + test "Terminal: reverseIndex" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); // Initial value @@ -8489,7 +9985,8 @@ test "Terminal: reverseIndex" { test "Terminal: reverseIndex from the top" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -8522,7 +10019,8 @@ test "Terminal: reverseIndex from the top" { test "Terminal: reverseIndex top of scrolling region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 10 }); defer t.deinit(alloc); // Initial value @@ -8555,7 +10053,8 @@ test "Terminal: reverseIndex top of scrolling region" { test "Terminal: reverseIndex top of screen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -8576,7 +10075,8 @@ test "Terminal: reverseIndex top of screen" { test "Terminal: reverseIndex not top of screen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -8597,7 +10097,8 @@ test "Terminal: reverseIndex not top of screen" { test "Terminal: reverseIndex top/bottom margins" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -8618,7 +10119,8 @@ test "Terminal: reverseIndex top/bottom margins" { test "Terminal: reverseIndex outside top/bottom margins" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -8639,7 +10141,8 @@ test "Terminal: reverseIndex outside top/bottom margins" { test "Terminal: reverseIndex left/right margins" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -8661,7 +10164,8 @@ test "Terminal: reverseIndex left/right margins" { test "Terminal: reverseIndex outside left/right margins" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -8683,7 +10187,8 @@ test "Terminal: reverseIndex outside left/right margins" { test "Terminal: index" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); try t.index(); @@ -8701,7 +10206,8 @@ test "Terminal: index" { test "Terminal: index from the bottom" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); t.setCursorPos(5, 1); @@ -8724,7 +10230,8 @@ test "Terminal: index from the bottom" { test "Terminal: index scrolling with hyperlink" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); t.setCursorPos(5, 1); @@ -8769,7 +10276,8 @@ test "Terminal: index scrolling with hyperlink" { test "Terminal: index outside of scrolling region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); try testing.expectEqual(@as(usize, 0), t.screens.active.cursor.y); @@ -8780,7 +10288,8 @@ test "Terminal: index outside of scrolling region" { test "Terminal: index from the bottom outside of scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 2); @@ -8800,7 +10309,8 @@ test "Terminal: index from the bottom outside of scroll region" { test "Terminal: index no scroll region, top of screen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -8820,7 +10330,8 @@ test "Terminal: index no scroll region, top of screen" { test "Terminal: index bottom of primary screen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setCursorPos(5, 1); @@ -8841,7 +10352,8 @@ test "Terminal: index bottom of primary screen" { test "Terminal: index bottom of primary screen background sgr" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setCursorPos(5, 1); @@ -8874,7 +10386,8 @@ test "Terminal: index bottom of primary screen background sgr" { test "Terminal: index inside scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -8895,7 +10408,8 @@ test "Terminal: index inside scroll region" { test "Terminal: index bottom of scroll region with hyperlinks" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 2); @@ -8943,7 +10457,8 @@ test "Terminal: index bottom of scroll region with hyperlinks" { test "Terminal: index bottom of scroll region clear hyperlinks" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5, .max_scrollback = 0 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5, .max_scrollback_bytes = 0 }); defer t.deinit(alloc); t.setTopAndBottomMargin(2, 3); @@ -8982,7 +10497,8 @@ test "Terminal: index bottom of scroll region clear hyperlinks" { test "Terminal: index bottom of scroll region with background SGR" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -9019,7 +10535,8 @@ test "Terminal: index bottom of scroll region with background SGR" { test "Terminal: index bottom of primary screen with scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -9047,7 +10564,8 @@ test "Terminal: index bottom of primary screen with scroll region" { test "Terminal: index outside left/right margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -9071,7 +10589,8 @@ test "Terminal: index outside left/right margin" { test "Terminal: index inside left/right margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); try t.printString("AAAAAA"); @@ -9105,7 +10624,8 @@ test "Terminal: index inside left/right margin" { test "Terminal: index bottom of scroll region creates scrollback" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -9130,7 +10650,8 @@ test "Terminal: index bottom of scroll region creates scrollback" { test "Terminal: index bottom of scroll region no scrollback" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5, .max_scrollback = 0 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5, .max_scrollback_bytes = 0 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -9151,7 +10672,8 @@ test "Terminal: index bottom of scroll region no scrollback" { test "Terminal: index bottom of scroll region blank line preserves SGR" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -9192,7 +10714,8 @@ test "Terminal: index bottom of scroll region blank line preserves SGR" { test "Terminal: index bottom of scroll region with top margin and background SGR" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("1\n2\n3\n4\n5"); @@ -9232,7 +10755,8 @@ test "Terminal: index bottom of scroll region with top margin and background SGR test "Terminal: index bottom of alt screen full region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 3, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 3, .cols = 5 }); defer t.deinit(alloc); try t.switchScreenMode(.@"1049", true); @@ -9265,7 +10789,8 @@ test "Terminal: index bottom of alt screen full region" { test "Terminal: index bottom of alt screen top region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.switchScreenMode(.@"1049", true); @@ -9292,7 +10817,8 @@ test "Terminal: index bottom of alt screen top region" { test "Terminal: scrollUp top region no scrollback" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5, .max_scrollback = 0 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5, .max_scrollback_bytes = 0 }); defer t.deinit(alloc); try t.printString("A\nB\nC\nD\nE"); @@ -9314,7 +10840,8 @@ test "Terminal: scrollUp top region no scrollback" { test "Terminal: cursorUp basic" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setCursorPos(3, 1); @@ -9331,7 +10858,8 @@ test "Terminal: cursorUp basic" { test "Terminal: cursorUp below top scroll margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(2, 4); @@ -9349,7 +10877,8 @@ test "Terminal: cursorUp below top scroll margin" { test "Terminal: cursorUp above top scroll margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(3, 5); @@ -9368,7 +10897,8 @@ test "Terminal: cursorUp above top scroll margin" { test "Terminal: cursorUp resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -9386,7 +10916,8 @@ test "Terminal: cursorUp resets wrap" { test "Terminal: cursorLeft no wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -9404,7 +10935,8 @@ test "Terminal: cursorLeft no wrap" { test "Terminal: cursorLeft unsets pending wrap state" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -9422,7 +10954,8 @@ test "Terminal: cursorLeft unsets pending wrap state" { test "Terminal: cursorLeft unsets pending wrap state with longer jump" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -9440,7 +10973,8 @@ test "Terminal: cursorLeft unsets pending wrap state with longer jump" { test "Terminal: cursorLeft reverse wrap with pending wrap state" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9461,7 +10995,8 @@ test "Terminal: cursorLeft reverse wrap with pending wrap state" { test "Terminal: cursorLeft reverse wrap extended with pending wrap state" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9482,7 +11017,8 @@ test "Terminal: cursorLeft reverse wrap extended with pending wrap state" { test "Terminal: cursorLeft reverse wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9502,7 +11038,8 @@ test "Terminal: cursorLeft reverse wrap" { test "Terminal: cursorLeft reverse wrap with no soft wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9524,7 +11061,8 @@ test "Terminal: cursorLeft reverse wrap with no soft wrap" { test "Terminal: cursorLeft reverse wrap before left margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9542,7 +11080,8 @@ test "Terminal: cursorLeft reverse wrap before left margin" { test "Terminal: cursorLeft extended reverse wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9564,7 +11103,8 @@ test "Terminal: cursorLeft extended reverse wrap" { test "Terminal: cursorLeft extended reverse wrap bottom wraparound" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9586,7 +11126,8 @@ test "Terminal: cursorLeft extended reverse wrap bottom wraparound" { test "Terminal: cursorLeft extended reverse wrap is priority if both set" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9609,7 +11150,8 @@ test "Terminal: cursorLeft extended reverse wrap is priority if both set" { test "Terminal: cursorLeft extended reverse wrap above top scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9625,7 +11167,8 @@ test "Terminal: cursorLeft extended reverse wrap above top scroll region" { test "Terminal: cursorLeft reverse wrap on first row" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -9641,7 +11184,8 @@ test "Terminal: cursorLeft reverse wrap on first row" { test "Terminal: cursorDown basic" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -9657,7 +11201,8 @@ test "Terminal: cursorDown basic" { test "Terminal: cursorDown above bottom scroll margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -9674,7 +11219,8 @@ test "Terminal: cursorDown above bottom scroll margin" { test "Terminal: cursorDown below bottom scroll margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setTopAndBottomMargin(1, 3); @@ -9692,7 +11238,8 @@ test "Terminal: cursorDown below bottom scroll margin" { test "Terminal: cursorDown resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -9710,7 +11257,8 @@ test "Terminal: cursorDown resets wrap" { test "Terminal: cursorRight resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -9728,7 +11276,8 @@ test "Terminal: cursorRight resets wrap" { test "Terminal: cursorRight to the edge of screen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.cursorRight(100); @@ -9743,7 +11292,8 @@ test "Terminal: cursorRight to the edge of screen" { test "Terminal: cursorRight left of right margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.scrolling_region.right = 2; @@ -9759,7 +11309,8 @@ test "Terminal: cursorRight left of right margin" { test "Terminal: cursorRight right of right margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.scrolling_region.right = 2; @@ -9776,7 +11327,8 @@ test "Terminal: cursorRight right of right margin" { test "Terminal: deleteLines simple" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -9808,7 +11360,8 @@ test "Terminal: deleteLines simple" { test "Terminal: deleteLines colors with bg color" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("ABC"); @@ -9849,7 +11402,8 @@ test "Terminal: deleteLines colors with bg color" { test "Terminal: deleteLines across page boundary marks all shifted rows dirty" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 10, .max_scrollback = 1024 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 10, .max_scrollback_bytes = 1024 }); defer t.deinit(alloc); const first_page = t.screens.active.pages.pages.first.?; @@ -9897,9 +11451,101 @@ test "Terminal: deleteLines across page boundary marks all shifted rows dirty" { } } +test "Terminal: deleteLines hyperlink-dense row crosses page boundary" { + // Regression test for the cross-page copy of deleteLines: when the + // shifted row carries more unique hyperlinks than the destination + // page's hyperlink capacity, the copy must increase the destination + // page's capacity and retry rather than corrupting the page list. + // + // This is the mirror of the insertLines variant: the dense row + // starts as the first row of the second page and is pulled up into + // the first page, which also happens to be the cursor's page so + // this exercises the cursor accounting of the capacity increase. + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 10, .max_scrollback_bytes = 1024 }); + defer t.deinit(alloc); + + const pages = &t.screens.active.pages; + + // Fill the first page so it is exactly full, then two more rows so + // the second page holds the last two active rows (y=3 and y=4). + const first_page_rows = pages.pages.first.?.capacity().rows; + for (0..first_page_rows + 1) |_| try t.linefeed(); + try testing.expect(pages.pages.first != pages.pages.last); + try testing.expectEqual(@as(usize, 2), pages.pages.last.?.rows()); + + // Marker rows so we can verify the shift afterwards. + t.setCursorPos(1, 1); + try t.printString("0"); + t.setCursorPos(2, 1); + try t.printString("1"); + t.setCursorPos(3, 1); + try t.printString("2"); + t.setCursorPos(5, 1); + try t.printString("4"); + + // Fill the first row of the second page (active y=3) with unique + // hyperlinks: more than the first page can hold with its default + // hyperlink capacity. Writing them grows the second page's + // capacity as needed; the first page keeps its default capacity. + t.setCursorPos(4, 1); + for (0..10) |i| { + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{i}); + try t.screens.active.startHyperlink(uri, null); + try t.print(@intCast('A' + i)); + t.screens.active.endHyperlink(); + } + { + const pin = pages.pin(.{ .active = .{ .y = 3 } }).?; + try testing.expectEqual(pages.pages.last.?, pin.node); + try testing.expectEqual(@as(usize, 0), @as(usize, pin.y)); + } + try testing.expect(pages.pages.first.?.page().hyperlink_set.layout.cap < 10); + + // Delete the top line: every row shifts up by one and the dense + // row crosses the page boundary into the first page. + t.setCursorPos(1, 1); + t.deleteLines(1); + + { + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("1\n2\nABCDEFGHIJ\n4", str); + } + + // The first page's hyperlink capacity had to grow to receive the + // row, proving the capacity-retry path ran. + try testing.expect(pages.pages.first.?.page().hyperlink_set.layout.cap >= 10); + + // Every cell of the dense row must still resolve to a real + // hyperlink entry with the correct URI. A half-applied shift + // leaves cells whose hyperlink flag is set but that have no map + // entry, which aborts in clearCells later. + for (0..10) |x| { + const list_cell = pages.getCell(.{ .active = .{ + .x = @intCast(x), + .y = 2, + } }).?; + try testing.expect(list_cell.cell.hyperlink); + const page: *Page = list_cell.node.page(); + const id = page.lookupHyperlink(list_cell.cell).?; + const link = page.hyperlink_set.get(page.memory, id); + var buf: [64]u8 = undefined; + const uri = try std.fmt.bufPrint(&buf, "http://example.com/{d}", .{x}); + try testing.expectEqualStrings(uri, link.uri.slice(page.memory)); + } + + // All pages must pass integrity checks. + var node_: ?*PageList.List.Node = pages.pages.first; + while (node_) |node| : (node_ = node.next) node.page().assertIntegrity(); +} + test "Terminal: deleteLines (legacy)" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 80, .rows = 80 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 80, .rows = 80 }); defer t.deinit(alloc); // Initial value @@ -9934,7 +11580,8 @@ test "Terminal: deleteLines (legacy)" { test "Terminal: deleteLines with scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 80, .rows = 80 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 80, .rows = 80 }); defer t.deinit(alloc); // Initial value @@ -9977,7 +11624,8 @@ test "Terminal: deleteLines with scroll region" { test "Terminal: deleteLines with scroll region, large count" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 80, .rows = 80 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 80, .rows = 80 }); defer t.deinit(alloc); // Initial value @@ -10020,7 +11668,8 @@ test "Terminal: deleteLines with scroll region, large count" { test "Terminal: deleteLines with scroll region, cursor outside of region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 80, .rows = 80 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 80, .rows = 80 }); defer t.deinit(alloc); // Initial value @@ -10055,7 +11704,8 @@ test "Terminal: deleteLines with scroll region, cursor outside of region" { test "Terminal: deleteLines resets pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -10073,7 +11723,8 @@ test "Terminal: deleteLines resets pending wrap" { test "Terminal: deleteLines resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 3, .cols = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); try t.print('1'); @@ -10104,7 +11755,8 @@ test "Terminal: deleteLines resets wrap" { test "Terminal: deleteLines left/right scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -10136,7 +11788,8 @@ test "Terminal: deleteLines left/right scroll region" { test "Terminal: deleteLines left/right scroll region from top" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -10167,7 +11820,8 @@ test "Terminal: deleteLines left/right scroll region from top" { test "Terminal: deleteLines left/right scroll region high count" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -10199,7 +11853,8 @@ test "Terminal: deleteLines left/right scroll region high count" { test "Terminal: deleteLines wide character spacer head" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); defer t.deinit(alloc); // Initial value @@ -10235,7 +11890,8 @@ test "Terminal: deleteLines wide character spacer head" { test "Terminal: deleteLines wide character spacer head left scroll margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); defer t.deinit(alloc); // Initial value @@ -10276,7 +11932,8 @@ test "Terminal: deleteLines wide character spacer head left scroll margin" { test "Terminal: deleteLines wide character spacer head right scroll margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); defer t.deinit(alloc); // Initial value @@ -10317,7 +11974,8 @@ test "Terminal: deleteLines wide character spacer head right scroll margin" { test "Terminal: deleteLines wide character spacer head left and right scroll margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); defer t.deinit(alloc); // Initial value @@ -10359,7 +12017,8 @@ test "Terminal: deleteLines wide character spacer head left and right scroll mar test "Terminal: deleteLines wide character spacer head left (< 2) and right scroll margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); defer t.deinit(alloc); // Initial value @@ -10401,7 +12060,8 @@ test "Terminal: deleteLines wide character spacer head left (< 2) and right scro test "Terminal: deleteLines wide characters split by left/right scroll region boundaries" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); // Initial value @@ -10436,7 +12096,8 @@ test "Terminal: deleteLines wide characters split by left/right scroll region bo test "Terminal: deleteLines zero" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 5 }); defer t.deinit(alloc); // This should do nothing @@ -10446,7 +12107,8 @@ test "Terminal: deleteLines zero" { test "Terminal: default style is empty" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -10454,14 +12116,15 @@ test "Terminal: default style is empty" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint.data); try testing.expectEqual(@as(style.Id, 0), cell.style_id); } } test "Terminal: bold style" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.setAttribute(.{ .bold = {} }); @@ -10470,7 +12133,7 @@ test "Terminal: bold style" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'A'), cell.content.codepoint.data); try testing.expect(cell.style_id != 0); const page = t.screens.active.cursor.page_pin.node.page(); try testing.expect(page.styles.refCount(page.memory, t.screens.active.cursor.style_id) > 1); @@ -10479,7 +12142,8 @@ test "Terminal: bold style" { test "Terminal: garbage collect overwritten" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.setAttribute(.{ .bold = {} }); @@ -10491,7 +12155,7 @@ test "Terminal: garbage collect overwritten" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'B'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'B'), cell.content.codepoint.data); try testing.expect(cell.style_id == 0); } @@ -10502,7 +12166,8 @@ test "Terminal: garbage collect overwritten" { test "Terminal: do not garbage collect old styles in use" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.setAttribute(.{ .bold = {} }); @@ -10513,7 +12178,7 @@ test "Terminal: do not garbage collect old styles in use" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 'B'), cell.content.codepoint); + try testing.expectEqual(@as(u21, 'B'), cell.content.codepoint.data); try testing.expect(cell.style_id == 0); } @@ -10524,7 +12189,8 @@ test "Terminal: do not garbage collect old styles in use" { test "Terminal: print with style marks the row as styled" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.setAttribute(.{ .bold = {} }); @@ -10540,7 +12206,8 @@ test "Terminal: print with style marks the row as styled" { test "Terminal: DECALN" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 2 }); defer t.deinit(alloc); // Initial value @@ -10567,7 +12234,8 @@ test "Terminal: DECALN" { test "Terminal: decaln reset margins" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 3 }); defer t.deinit(alloc); // Initial value @@ -10585,7 +12253,8 @@ test "Terminal: decaln reset margins" { test "Terminal: decaln preserves color" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 3 }); defer t.deinit(alloc); // Initial value @@ -10614,7 +12283,8 @@ test "Terminal: decaln preserves color" { test "Terminal: DECALN resets graphemes with protected mode" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 3 }); defer t.deinit(alloc); // Add protected mode. A previous version of DECALN accidentally preserved @@ -10650,7 +12320,8 @@ test "Terminal: DECALN resets graphemes with protected mode" { test "Terminal: insertBlanks zero" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); try t.print('A'); @@ -10671,7 +12342,8 @@ test "Terminal: insertBlanks" { // NOTE: this is not verified with conformance tests, so these // tests might actually be verifying wrong behavior. const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); try t.print('A'); @@ -10695,7 +12367,8 @@ test "Terminal: insertBlanks pushes off end" { // NOTE: this is not verified with conformance tests, so these // tests might actually be verifying wrong behavior. const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 2 }); defer t.deinit(alloc); try t.print('A'); @@ -10718,7 +12391,8 @@ test "Terminal: insertBlanks more than size" { // NOTE: this is not verified with conformance tests, so these // tests might actually be verifying wrong behavior. const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 2 }); defer t.deinit(alloc); try t.print('A'); @@ -10739,7 +12413,8 @@ test "Terminal: insertBlanks more than size" { test "Terminal: insertBlanks no scroll region, fits" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -10758,7 +12433,8 @@ test "Terminal: insertBlanks no scroll region, fits" { test "Terminal: insertBlanks preserves background sgr" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -10788,7 +12464,8 @@ test "Terminal: insertBlanks preserves background sgr" { test "Terminal: insertBlanks shift off screen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 10 }); defer t.deinit(alloc); for (" ABC") |c| try t.print(c); @@ -10807,7 +12484,8 @@ test "Terminal: insertBlanks shift off screen" { test "Terminal: insertBlanks split multi-cell character" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 10 }); defer t.deinit(alloc); for ("123") |c| try t.print(c); @@ -10826,7 +12504,8 @@ test "Terminal: insertBlanks split multi-cell character" { test "Terminal: insertBlanks inside left/right scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); t.scrolling_region.left = 2; @@ -10849,7 +12528,8 @@ test "Terminal: insertBlanks inside left/right scroll region" { test "Terminal: insertBlanks outside left/right scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 6, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 6, .rows = 10 }); defer t.deinit(alloc); t.setCursorPos(1, 4); @@ -10872,7 +12552,8 @@ test "Terminal: insertBlanks outside left/right scroll region" { test "Terminal: insertBlanks left/right scroll region large count" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); t.modes.set(.origin, true); @@ -10893,7 +12574,8 @@ test "Terminal: insertBlanks left/right scroll region large count" { test "Terminal: insertBlanks deleting graphemes" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Disable grapheme clustering @@ -10929,7 +12611,8 @@ test "Terminal: insertBlanks deleting graphemes" { test "Terminal: insertBlanks shift graphemes" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Enable grapheme clustering @@ -10965,7 +12648,8 @@ test "Terminal: insertBlanks shift graphemes" { test "Terminal: insertBlanks split multi-cell character from tail" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 10 }); defer t.deinit(alloc); try t.printString("橋123"); @@ -10989,7 +12673,8 @@ test "Terminal: insertBlanks shifts hyperlinks" { // link should be preserved, blanks should not be linked const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 2 }); defer t.deinit(alloc); try t.screens.active.startHyperlink("http://example.com", null); @@ -11030,7 +12715,8 @@ test "Terminal: insertBlanks shifts hyperlinks" { test "Terminal: insertBlanks pushes hyperlink off end completely" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 2 }); defer t.deinit(alloc); try t.screens.active.startHyperlink("http://example.com", null); @@ -11066,7 +12752,8 @@ test "Terminal: insertBlanks wide char straddling right margin" { // away via swapCells but leaves the orphaned spacer_tail in place, // causing a page integrity violation. const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Fill row: A B C D 橋 _ _ _ _ _ @@ -11101,7 +12788,8 @@ test "Terminal: insertBlanks wide char spacer_tail orphaned beyond right margin" // is left behind, causing a page integrity violation: // "spacer tail not following wide" const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Fill cols 0–9 with wide chars: 中中中中中 @@ -11131,7 +12819,8 @@ test "Terminal: insertBlanks wide char spacer_tail orphaned beyond right margin" test "Terminal: insert mode with space" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 2 }); defer t.deinit(alloc); for ("hello") |c| try t.print(c); @@ -11148,7 +12837,8 @@ test "Terminal: insert mode with space" { test "Terminal: insert mode doesn't wrap pushed characters" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); for ("hello") |c| try t.print(c); @@ -11165,7 +12855,8 @@ test "Terminal: insert mode doesn't wrap pushed characters" { test "Terminal: insert mode does nothing at the end of the line" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); for ("hello") |c| try t.print(c); @@ -11181,7 +12872,8 @@ test "Terminal: insert mode does nothing at the end of the line" { test "Terminal: insert mode with wide characters" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); for ("hello") |c| try t.print(c); @@ -11198,7 +12890,8 @@ test "Terminal: insert mode with wide characters" { test "Terminal: insert mode with wide characters at end" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); for ("well") |c| try t.print(c); @@ -11214,7 +12907,8 @@ test "Terminal: insert mode with wide characters at end" { test "Terminal: insert mode pushing off wide character" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); for ("123") |c| try t.print(c); @@ -11232,7 +12926,8 @@ test "Terminal: insert mode pushing off wide character" { test "Terminal: deleteChars" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -11251,7 +12946,8 @@ test "Terminal: deleteChars" { test "Terminal: deleteChars zero count" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -11270,7 +12966,8 @@ test "Terminal: deleteChars zero count" { test "Terminal: deleteChars more than half" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -11289,7 +12986,8 @@ test "Terminal: deleteChars more than half" { test "Terminal: deleteChars more than line width" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -11308,7 +13006,8 @@ test "Terminal: deleteChars more than line width" { test "Terminal: deleteChars should shift left" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -11327,7 +13026,8 @@ test "Terminal: deleteChars should shift left" { test "Terminal: deleteChars resets pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -11345,7 +13045,8 @@ test "Terminal: deleteChars resets pending wrap" { test "Terminal: deleteChars resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE123") |c| try t.print(c); @@ -11374,7 +13075,8 @@ test "Terminal: deleteChars resets wrap" { test "Terminal: deleteChars simple operation" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -11393,7 +13095,8 @@ test "Terminal: deleteChars simple operation" { test "Terminal: deleteChars preserves background sgr" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); for ("ABC123") |c| try t.print(c); @@ -11426,7 +13129,8 @@ test "Terminal: deleteChars preserves background sgr" { test "Terminal: deleteChars outside scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 6, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 6, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -11447,7 +13151,8 @@ test "Terminal: deleteChars outside scroll region" { test "Terminal: deleteChars inside scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 6, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 6, .rows = 10 }); defer t.deinit(alloc); try t.printString("ABC123"); @@ -11468,7 +13173,8 @@ test "Terminal: deleteChars inside scroll region" { test "Terminal: deleteChars split wide character from spacer tail" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 6, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 6, .rows = 10 }); defer t.deinit(alloc); try t.printString("A橋123"); @@ -11484,7 +13190,8 @@ test "Terminal: deleteChars split wide character from spacer tail" { test "Terminal: deleteChars split wide character from wide" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 6, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 6, .rows = 10 }); defer t.deinit(alloc); try t.printString("橋123"); @@ -11494,20 +13201,21 @@ test "Terminal: deleteChars split wide character from wide" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, '1'), cell.content.codepoint); + try testing.expectEqual(@as(u21, '1'), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: deleteChars split wide character from end" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 6, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 6, .rows = 10 }); defer t.deinit(alloc); try t.printString("A橋123"); @@ -11517,20 +13225,21 @@ test "Terminal: deleteChars split wide character from end" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 0, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0x6A4B), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0x6A4B), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.wide, cell.wide); } { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 1, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.spacer_tail, cell.wide); } } test "Terminal: deleteChars with a spacer head at the end" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 10 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 10 }); defer t.deinit(alloc); try t.printString("0123橋123"); @@ -11548,14 +13257,15 @@ test "Terminal: deleteChars with a spacer head at the end" { { const list_cell = t.screens.active.pages.getCell(.{ .screen = .{ .x = 3, .y = 0 } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u21, 0), cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), cell.content.codepoint.data); try testing.expectEqual(Cell.Wide.narrow, cell.wide); } } test "Terminal: deleteChars split wide character tail" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setCursorPos(1, t.cols - 1); @@ -11573,7 +13283,8 @@ test "Terminal: deleteChars split wide character tail" { test "Terminal: deleteChars wide char boundary conditions" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 1, .cols = 8 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 1, .cols = 8 }); defer t.deinit(alloc); // EXPLANATION(qwerasd): @@ -11637,7 +13348,8 @@ test "Terminal: deleteChars wide char boundary conditions" { test "Terminal: deleteChars wide char wrap boundary conditions" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 3, .cols = 8 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 3, .cols = 8 }); defer t.deinit(alloc); // EXPLANATION(qwerasd): @@ -11677,9 +13389,16 @@ test "Terminal: deleteChars wide char wrap boundary conditions" { } t.setCursorPos(2, 2); + t.clearDirty(); t.deleteChars(3); t.screens.active.cursor.page_pin.node.page().assertIntegrity(); + // Deleting the wide char also clears the spacer head on the previous + // row, so that row must be dirty too. + try testing.expect(t.isDirty(.{ .screen = .{ .x = 0, .y = 0 } })); + try testing.expect(t.isDirty(.{ .screen = .{ .x = 0, .y = 1 } })); + try testing.expect(!t.isDirty(.{ .screen = .{ .x = 0, .y = 2 } })); + { const str = try t.plainString(alloc); defer testing.allocator.free(str); @@ -11693,7 +13412,8 @@ test "Terminal: deleteChars wide char wrap boundary conditions" { test "Terminal: deleteChars wide char across right margin" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 3, .cols = 8 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 3, .cols = 8 }); defer t.deinit(alloc); // scroll region @@ -11736,7 +13456,8 @@ test "Terminal: deleteChars wide char across right margin" { test "Terminal: saveCursor" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 3 }); defer t.deinit(alloc); try t.setAttribute(.{ .bold = {} }); @@ -11754,7 +13475,8 @@ test "Terminal: saveCursor" { test "Terminal: saveCursor position" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); t.setCursorPos(1, 5); @@ -11774,7 +13496,8 @@ test "Terminal: saveCursor position" { test "Terminal: saveCursor pending wrap state" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setCursorPos(1, 5); @@ -11794,7 +13517,8 @@ test "Terminal: saveCursor pending wrap state" { test "Terminal: saveCursor origin mode" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); t.modes.set(.origin, true); @@ -11814,12 +13538,13 @@ test "Terminal: saveCursor origin mode" { test "Terminal: saveCursor resize" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); t.setCursorPos(1, 10); t.saveCursor(); - try t.resize(alloc, 5, 5); + try t.resize(alloc, .{ .cols = 5, .rows = 5 }); t.restoreCursor(); try t.print('X'); @@ -11832,7 +13557,8 @@ test "Terminal: saveCursor resize" { test "Terminal: saveCursor protected pen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -11847,7 +13573,8 @@ test "Terminal: saveCursor protected pen" { test "Terminal: saveCursor doesn't modify hyperlink state" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 3 }); defer t.deinit(alloc); try t.screens.active.startHyperlink("http://example.com", null); @@ -11863,9 +13590,10 @@ test "Terminal: restoreCursor uses default style on OutOfSpace" { // manualStyleUpdate fails with OutOfSpace (can't split a 1-row page // and styles are at max capacity). const alloc = testing.allocator; + const io_impl = testing.io; // Use a single row so the page can't be split - var t = try init(alloc, .{ .cols = 10, .rows = 1 }); + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 1 }); defer t.deinit(alloc); // Set a style and save the cursor @@ -11917,7 +13645,8 @@ test "Terminal: restoreCursor uses default style on OutOfSpace" { test "Terminal: setProtectedMode" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 3 }); defer t.deinit(alloc); try testing.expect(!t.screens.active.cursor.protected); @@ -11933,7 +13662,8 @@ test "Terminal: setProtectedMode" { test "Terminal: eraseLine simple erase right" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -11951,7 +13681,8 @@ test "Terminal: eraseLine simple erase right" { test "Terminal: eraseLine resets pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -11969,7 +13700,8 @@ test "Terminal: eraseLine resets pending wrap" { test "Terminal: eraseLine resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE123") |c| try t.print(c); @@ -11996,7 +13728,8 @@ test "Terminal: eraseLine resets wrap" { test "Terminal: eraseLine right preserves background sgr" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -12029,7 +13762,8 @@ test "Terminal: eraseLine right preserves background sgr" { test "Terminal: eraseLine right wide character" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); for ("AB") |c| try t.print(c); @@ -12049,7 +13783,8 @@ test "Terminal: eraseLine right wide character" { test "Terminal: eraseLine right protected attributes respected with iso" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -12068,7 +13803,8 @@ test "Terminal: eraseLine right protected attributes respected with iso" { test "Terminal: eraseLine right protected attributes ignored with dec most recent" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -12089,7 +13825,8 @@ test "Terminal: eraseLine right protected attributes ignored with dec most recen test "Terminal: eraseLine right protected attributes ignored with dec set" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.dec); @@ -12108,7 +13845,8 @@ test "Terminal: eraseLine right protected attributes ignored with dec set" { test "Terminal: eraseLine right protected requested" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); for ("12345678") |c| try t.print(c); @@ -12129,7 +13867,8 @@ test "Terminal: eraseLine right protected requested" { test "Terminal: eraseLine simple erase left" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -12147,7 +13886,8 @@ test "Terminal: eraseLine simple erase left" { test "Terminal: eraseLine left resets wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -12167,7 +13907,8 @@ test "Terminal: eraseLine left resets wrap" { test "Terminal: eraseLine left preserves background sgr" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -12200,7 +13941,8 @@ test "Terminal: eraseLine left preserves background sgr" { test "Terminal: eraseLine left wide character" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); for ("AB") |c| try t.print(c); @@ -12220,7 +13962,8 @@ test "Terminal: eraseLine left wide character" { test "Terminal: eraseLine left protected attributes respected with iso" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -12239,7 +13982,8 @@ test "Terminal: eraseLine left protected attributes respected with iso" { test "Terminal: eraseLine left protected attributes ignored with dec most recent" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -12260,7 +14004,8 @@ test "Terminal: eraseLine left protected attributes ignored with dec most recent test "Terminal: eraseLine left protected attributes ignored with dec set" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.dec); @@ -12279,7 +14024,8 @@ test "Terminal: eraseLine left protected attributes ignored with dec set" { test "Terminal: eraseLine left protected requested" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); for ("123456789") |c| try t.print(c); @@ -12300,7 +14046,8 @@ test "Terminal: eraseLine left protected requested" { test "Terminal: eraseLine complete preserves background sgr" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -12331,9 +14078,39 @@ test "Terminal: eraseLine complete preserves background sgr" { } } +test "Terminal: eraseLine complete resets wrap" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + for ("ABCDE123") |c| try t.print(c); + { + const list_cell = t.screens.active.pages.getCell(.{ .active = .{ .x = 0, .y = 0 } }).?; + try testing.expect(list_cell.row.wrap); + } + + t.setCursorPos(1, 1); + t.eraseLine(.complete, false); + + { + const list_cell = t.screens.active.pages.getCell(.{ .active = .{ .x = 0, .y = 0 } }).?; + try testing.expect(!list_cell.row.wrap); + } + try t.print('X'); + try t.resize(alloc, .{ .rows = 5, .cols = 10 }); + + { + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("X\n123", str); + } +} + test "Terminal: eraseLine complete protected attributes respected with iso" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -12352,7 +14129,8 @@ test "Terminal: eraseLine complete protected attributes respected with iso" { test "Terminal: eraseLine complete protected attributes ignored with dec most recent" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -12373,7 +14151,8 @@ test "Terminal: eraseLine complete protected attributes ignored with dec most re test "Terminal: eraseLine complete protected attributes ignored with dec set" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.dec); @@ -12392,7 +14171,8 @@ test "Terminal: eraseLine complete protected attributes ignored with dec set" { test "Terminal: eraseLine complete protected requested" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); for ("123456789") |c| try t.print(c); @@ -12413,7 +14193,8 @@ test "Terminal: eraseLine complete protected requested" { test "Terminal: tabClear single" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 30, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 30, .rows = 5 }); defer t.deinit(alloc); t.horizontalTab(); @@ -12426,7 +14207,8 @@ test "Terminal: tabClear single" { test "Terminal: tabClear all" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 30, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 30, .rows = 5 }); defer t.deinit(alloc); t.tabClear(.all); @@ -12438,7 +14220,8 @@ test "Terminal: tabClear all" { test "Terminal: printRepeat simple" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("A"); @@ -12454,7 +14237,8 @@ test "Terminal: printRepeat simple" { test "Terminal: printRepeat wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString(" A"); @@ -12470,7 +14254,8 @@ test "Terminal: printRepeat wrap" { test "Terminal: printRepeat no previous character" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printRepeat(1); @@ -12485,7 +14270,8 @@ test "Terminal: printRepeat no previous character" { test "Terminal: printSlice simple ascii" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 3 }); defer t.deinit(alloc); try t.printSlice(&.{ 'h', 'e', 'l', 'l', 'o' }); @@ -12502,7 +14288,8 @@ test "Terminal: printSlice simple ascii" { test "Terminal: printSlice wraps and scrolls" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); // 12 chars: fills row 1 (5), row 2 (5), wraps+scrolls, 2 more. @@ -12519,7 +14306,8 @@ test "Terminal: printSlice wraps and scrolls" { test "Terminal: printSlice pending wrap state" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 5, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 2 }); defer t.deinit(alloc); try t.printSlice(&.{ 'a', 'b', 'c', 'd', 'e' }); @@ -12538,18 +14326,19 @@ test "Terminal: printSlice pending wrap state" { /// the other using printSlice() with random chunking, verifying that /// the results are identical. fn testPrintSliceDifferential( + io_impl: std.Io, alloc: Allocator, rand: std.Random, ops: usize, cols: size.CellCountInt, rows: size.CellCountInt, ) !void { - var t1 = try init(alloc, .{ + var t1 = try init(io_impl, alloc, .{ .cols = cols, .rows = rows, }); defer t1.deinit(alloc); - var t2 = try init(alloc, .{ + var t2 = try init(io_impl, alloc, .{ .cols = cols, .rows = rows, }); @@ -12558,11 +14347,12 @@ fn testPrintSliceDifferential( // Alphabet of interesting codepoints: ascii, latin-1, combining // marks, CJK (wide), emoji (wide), ZWJ, variation selectors. const alphabet = [_]u21{ - 'a', 'b', 'Z', '0', ' ', 0x10, 0x1F, 0x7F, - 'é', 0xFF, 0x301, 0x4E00, 0x4E01, 0x1F600, 0x200D, 0xFE0F, - 'x', 'y', 0x1F9D1, 0x0308, 0xAD, 0x3042, 0xAC00, 'q', - 'r', 's', 't', 'u', 'v', 'w', '1', '2', - 0x1F1E6, 0x1F1E7, 0x1100, 0x1161, 0x11A8, 0x200C, 0x0430, 0x03B1, + 'a', 'b', 'Z', '0', ' ', 0x10, 0x1F, 0x7F, + 'é', + 0xFF, 0x301, 0x4E00, 0x4E01, 0x1F600, 0x200D, 0xFE0F, 'x', + 'y', 0x1F9D1, 0x0308, 0xAD, 0x3042, 0xAC00, 'q', 'r', + 's', 't', 'u', 'v', 'w', '1', '2', 0x1F1E6, + 0x1F1E7, 0x1100, 0x1161, 0x11A8, 0x200C, 0x0430, 0x03B1, }; var cps_buf: [64]u32 = undefined; @@ -12700,20 +14490,22 @@ fn testPrintSliceDifferential( test "Terminal: printSlice differential fuzz vs print" { const alloc = testing.allocator; + const io_impl = testing.io; // Multiple seeds and terminal sizes for coverage, including a // tiny terminal to stress wrap/scroll edge cases. var prng = std.Random.DefaultPrng.init(0xC0FFEE); const rand = prng.random(); - try testPrintSliceDifferential(alloc, rand, 500, 80, 24); - try testPrintSliceDifferential(alloc, rand, 500, 10, 4); - try testPrintSliceDifferential(alloc, rand, 500, 5, 2); - try testPrintSliceDifferential(alloc, rand, 200, 2, 2); + try testPrintSliceDifferential(io_impl, alloc, rand, 500, 80, 24); + try testPrintSliceDifferential(io_impl, alloc, rand, 500, 10, 4); + try testPrintSliceDifferential(io_impl, alloc, rand, 500, 5, 2); + try testPrintSliceDifferential(io_impl, alloc, rand, 200, 2, 2); } test "Terminal: printAttributes" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); var storage: [64]u8 = undefined; @@ -12742,20 +14534,31 @@ test "Terminal: printAttributes" { try t.setAttribute(.inverse); try t.setAttribute(.invisible); try t.setAttribute(.strikethrough); + try t.setAttribute(.overline); try t.setAttribute(.{ .direct_color_fg = .{ .r = 100, .g = 200, .b = 255 } }); try t.setAttribute(.{ .direct_color_bg = .{ .r = 101, .g = 102, .b = 103 } }); defer t.setAttribute(.unset) catch unreachable; const buf = try t.printAttributes(&storage); - try testing.expectEqualStrings("0;1;2;3;4;5;7;8;9;38:2::100:200:255;48:2::101:102:103", buf); + try testing.expectEqualStrings("0;1;2;3;4;53;5;7;8;9;38:2::100:200:255;48:2::101:102:103", buf); } - { - try t.setAttribute(.{ .underline = .single }); - defer t.setAttribute(.unset) catch unreachable; + const Case = struct { + underline: sgr.Attribute.Underline, + expected: []const u8, + }; + for ([_]Case{ + .{ .underline = .single, .expected = "0;4" }, + .{ .underline = .double, .expected = "0;4:2" }, + .{ .underline = .curly, .expected = "0;4:3" }, + .{ .underline = .dotted, .expected = "0;4:4" }, + .{ .underline = .dashed, .expected = "0;4:5" }, + }) |case| { + try t.setAttribute(.{ .underline = case.underline }); const buf = try t.printAttributes(&storage); - try testing.expectEqualStrings("0;4", buf); + try testing.expectEqualStrings(case.expected, buf); } + try t.setAttribute(.unset); { const buf = try t.printAttributes(&storage); try testing.expectEqualStrings("0", buf); @@ -12764,7 +14567,8 @@ test "Terminal: printAttributes" { test "Terminal: eraseDisplay simple erase below" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -12792,7 +14596,8 @@ test "Terminal: eraseDisplay simple erase below" { test "Terminal: eraseDisplay erase below preserves SGR bg" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -12832,7 +14637,8 @@ test "Terminal: eraseDisplay erase below preserves SGR bg" { test "Terminal: eraseDisplay below split multi-cell" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("AB橋C"); @@ -12854,7 +14660,8 @@ test "Terminal: eraseDisplay below split multi-cell" { test "Terminal: eraseDisplay below protected attributes respected with iso" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -12877,7 +14684,8 @@ test "Terminal: eraseDisplay below protected attributes respected with iso" { test "Terminal: eraseDisplay below protected attributes ignored with dec most recent" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -12902,7 +14710,8 @@ test "Terminal: eraseDisplay below protected attributes ignored with dec most re test "Terminal: eraseDisplay below protected attributes ignored with dec set" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.dec); @@ -12925,7 +14734,8 @@ test "Terminal: eraseDisplay below protected attributes ignored with dec set" { test "Terminal: eraseDisplay below protected attributes respected with force" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.dec); @@ -12948,7 +14758,8 @@ test "Terminal: eraseDisplay below protected attributes respected with force" { test "Terminal: eraseDisplay simple erase above" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -12975,7 +14786,8 @@ test "Terminal: eraseDisplay simple erase above" { test "Terminal: eraseDisplay erase above preserves SGR bg" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABC") |c| try t.print(c); @@ -13015,7 +14827,8 @@ test "Terminal: eraseDisplay erase above preserves SGR bg" { test "Terminal: eraseDisplay above split multi-cell" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.printString("AB橋C"); @@ -13037,7 +14850,8 @@ test "Terminal: eraseDisplay above split multi-cell" { test "Terminal: eraseDisplay above protected attributes respected with iso" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -13060,7 +14874,8 @@ test "Terminal: eraseDisplay above protected attributes respected with iso" { test "Terminal: eraseDisplay above protected attributes ignored with dec most recent" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.iso); @@ -13085,7 +14900,8 @@ test "Terminal: eraseDisplay above protected attributes ignored with dec most re test "Terminal: eraseDisplay above protected attributes ignored with dec set" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.dec); @@ -13108,7 +14924,8 @@ test "Terminal: eraseDisplay above protected attributes ignored with dec set" { test "Terminal: eraseDisplay above protected attributes respected with force" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.setProtectedMode(.dec); @@ -13131,7 +14948,8 @@ test "Terminal: eraseDisplay above protected attributes respected with force" { test "Terminal: eraseDisplay protected complete" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -13159,7 +14977,8 @@ test "Terminal: eraseDisplay protected complete" { test "Terminal: eraseDisplay protected below" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -13181,7 +15000,8 @@ test "Terminal: eraseDisplay protected below" { test "Terminal: eraseDisplay scroll complete" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); try t.print('A'); @@ -13198,7 +15018,8 @@ test "Terminal: eraseDisplay scroll complete" { test "Terminal: eraseDisplay protected above" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 3 }); defer t.deinit(alloc); try t.print('A'); @@ -13220,7 +15041,8 @@ test "Terminal: eraseDisplay protected above" { test "Terminal: eraseDisplay complete preserves cursor" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Set our cursur @@ -13237,7 +15059,8 @@ test "Terminal: eraseDisplay complete preserves cursor" { test "Terminal: semantic prompt" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Prompt @@ -13281,7 +15104,8 @@ test "Terminal: semantic prompt" { test "Terminal: semantic prompt continuations" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Prompt @@ -13331,7 +15155,8 @@ test "Terminal: index in prompt mode marks new row as prompt continuation" { // a newline, assume the new row is a prompt continuation (since Fish // doesn't emit OSC133 k=s markers for continuation lines). const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Start a prompt @@ -13368,7 +15193,8 @@ test "Terminal: index in input mode does not mark new row as prompt" { // Input mode should NOT trigger prompt continuation on newline // (only prompt mode does, not input mode) const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Start a prompt then switch to input @@ -13397,7 +15223,8 @@ test "Terminal: index in input mode does not mark new row as prompt" { test "Terminal: index in output mode does not mark new row as prompt" { // Output mode should NOT trigger prompt continuation const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Complete prompt cycle: prompt -> input -> output @@ -13426,7 +15253,8 @@ test "Terminal: OSC133C at x=0 on prompt row clears prompt mark" { // then immediately sends OSC133C (start output) at column 0, we // should clear the prompt continuation mark we just set. const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Start a prompt @@ -13463,7 +15291,8 @@ test "Terminal: OSC133C at x=0 on prompt row clears prompt mark" { test "Terminal: OSC133C at x>0 on prompt row does not clear prompt mark" { // If we're not at column 0, we shouldn't clear the prompt mark const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Start a prompt on a row @@ -13505,7 +15334,8 @@ test "Terminal: OSC133C at x>0 on prompt row does not clear prompt mark" { test "Terminal: multiple newlines in prompt mode marks all rows" { // Multiple newlines should each mark their row as prompt continuation const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Start a prompt @@ -13548,7 +15378,8 @@ test "Terminal: multiple newlines in prompt mode marks all rows" { test "Terminal: OSC133A click_events=1 sets click to click_events" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Verify default state is none @@ -13565,7 +15396,8 @@ test "Terminal: OSC133A click_events=1 sets click to click_events" { test "Terminal: OSC133A click_events=2 sets click to click_events (relative)" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // Verify default state is none @@ -13582,7 +15414,8 @@ test "Terminal: OSC133A click_events=2 sets click to click_events (relative)" { test "Terminal: OSC133A click_events=0 does not set click_events" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // OSC 133;A with click_events=0 @@ -13597,7 +15430,8 @@ test "Terminal: OSC133A click_events=0 does not set click_events" { test "Terminal: OSC133A cl option sets click to cl value" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // OSC 133;A with cl=m (multiple) @@ -13611,7 +15445,8 @@ test "Terminal: OSC133A cl option sets click to cl value" { test "Terminal: OSC133A cl=line sets click to line" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); try t.semanticPrompt(.{ @@ -13624,7 +15459,8 @@ test "Terminal: OSC133A cl=line sets click to line" { test "Terminal: OSC133A click_events=1 takes priority over cl" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // OSC 133;A with both click_events=1 and cl=m @@ -13639,7 +15475,8 @@ test "Terminal: OSC133A click_events=1 takes priority over cl" { test "Terminal: OSC133A click_events=0 falls back to cl" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // OSC 133;A with click_events=0 and cl=v @@ -13654,7 +15491,8 @@ test "Terminal: OSC133A click_events=0 falls back to cl" { test "Terminal: OSC133A no click options leaves click as none" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); // OSC 133;A with no click-related options @@ -13668,7 +15506,8 @@ test "Terminal: OSC133A no click options leaves click as none" { test "Terminal: cursorIsAtPrompt" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 10, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 3 }); defer t.deinit(alloc); try testing.expect(!t.cursorIsAtPrompt()); @@ -13697,7 +15536,8 @@ test "Terminal: cursorIsAtPrompt" { test "Terminal: cursorIsAtPrompt alternate screen" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 2 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 2 }); defer t.deinit(alloc); try testing.expect(!t.cursorIsAtPrompt()); @@ -13711,8 +15551,66 @@ test "Terminal: cursorIsAtPrompt alternate screen" { try testing.expect(!t.cursorIsAtPrompt()); } +test "Terminal: cursor defaults update current default cursor" { + var t = try init(testing.io, testing.allocator, .{ + .cols = 10, + .rows = 10, + .default_cursor_style = .bar, + .default_cursor_blink = true, + }); + defer t.deinit(testing.allocator); + + // Initialization applies the configured defaults. + try testing.expect(t.cursor.is_default); + try testing.expectEqual(.bar, t.screens.active.cursor.cursor_style); + try testing.expect(t.modes.get(.cursor_blinking)); + + // Configuration changes are immediately visible while the cursor still + // follows its defaults. + t.setDefaultCursorStyle(.underline); + t.setDefaultCursorBlink(false); + try testing.expect(t.cursor.is_default); + try testing.expectEqual(.underline, t.screens.active.cursor.cursor_style); + try testing.expect(!t.modes.get(.cursor_blinking)); + + // Null restores the terminal emulator's blinking default. + t.setDefaultCursorBlink(null); + try testing.expect(t.modes.get(.cursor_blinking)); +} + +test "Terminal: cursor defaults do not override explicit cursor" { + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); + defer t.deinit(testing.allocator); + + t.setCursorStyle(.blinking_bar); + try testing.expect(!t.cursor.is_default); + try testing.expectEqual(.bar, t.screens.active.cursor.cursor_style); + try testing.expect(t.modes.get(.cursor_blinking)); + + // New defaults are retained without replacing the explicit appearance. + t.setDefaultCursorStyle(.underline); + t.setDefaultCursorBlink(false); + try testing.expectEqual(.underline, t.cursor.default_style); + try testing.expectEqual(false, t.cursor.default_blink); + try testing.expectEqual(.bar, t.screens.active.cursor.cursor_style); + try testing.expect(t.modes.get(.cursor_blinking)); + + // Selecting the default applies the values that changed above. + t.setCursorStyle(.default); + try testing.expect(t.cursor.is_default); + try testing.expectEqual(.underline, t.screens.active.cursor.cursor_style); + try testing.expect(!t.modes.get(.cursor_blinking)); + + // A full reset also leaves the cursor on the configured defaults. + t.setCursorStyle(.steady_block); + t.fullReset(); + try testing.expect(t.cursor.is_default); + try testing.expectEqual(.underline, t.screens.active.cursor.cursor_style); + try testing.expect(!t.modes.get(.cursor_blinking)); +} + test "Terminal: fullReset with a non-empty pen" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); try t.setAttribute(.{ .direct_color_fg = .{ .r = 0xFF, .g = 0, .b = 0x7F } }); @@ -13734,7 +15632,7 @@ test "Terminal: fullReset with a non-empty pen" { } test "Terminal: fullReset hyperlink" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); try t.screens.active.startHyperlink("http://example.com", null); @@ -13743,7 +15641,7 @@ test "Terminal: fullReset hyperlink" { } test "Terminal: fullReset with a non-empty saved cursor" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); try t.setAttribute(.{ .direct_color_fg = .{ .r = 0xFF, .g = 0, .b = 0x7F } }); @@ -13764,7 +15662,7 @@ test "Terminal: fullReset with a non-empty saved cursor" { } test "Terminal: fullReset origin mode" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); t.setCursorPos(3, 5); @@ -13778,7 +15676,7 @@ test "Terminal: fullReset origin mode" { } test "Terminal: fullReset status display" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); t.status_display = .status_line; @@ -13786,9 +15684,37 @@ test "Terminal: fullReset status display" { try testing.expect(t.status_display == .main); } +test "Terminal: fullReset preserves kitty graphics limits" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + + const alloc = testing.allocator; + const temp_dir = "/tmp/ghostty-kitty-images"; + + var t = try init(testing.io, alloc, .{ .cols = 10, .rows = 10 }); + defer t.deinit(alloc); + + t.setKittyGraphicsLoadingLimits(.allWithTempDir(temp_dir)); + for ([_]usize{ 1234, 0 }) |total_limit| { + t.setKittyGraphicsSizeLimit(alloc, total_limit); + t.fullReset(); + + const storage = &t.screens.active.kitty_images; + try testing.expectEqual(total_limit, storage.total_limit); + try testing.expect(storage.image_limits.file); + try testing.expect(storage.image_limits.shared_memory); + switch (storage.image_limits.temporary_file) { + .enabled => |value| try testing.expectEqualStrings( + temp_dir, + value.directory, + ), + .disabled => return error.TestUnexpectedResult, + } + } +} + // https://github.com/mitchellh/ghostty/issues/1607 test "Terminal: fullReset clears alt screen kitty keyboard state" { - var t = try init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); try t.switchScreenMode(.@"1049", true); @@ -13806,7 +15732,7 @@ test "Terminal: fullReset clears alt screen kitty keyboard state" { } test "Terminal: fullReset default modes" { - var t = try init(testing.allocator, .{ + var t = try init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10, .default_modes = .{ .grapheme_cluster = true }, @@ -13818,7 +15744,7 @@ test "Terminal: fullReset default modes" { } test "Terminal: fullReset tracked pins" { - var t = try init(testing.allocator, .{ .cols = 80, .rows = 80 }); + var t = try init(testing.io, testing.allocator, .{ .cols = 80, .rows = 80 }); defer t.deinit(testing.allocator); // Create a tracked pin @@ -13832,12 +15758,13 @@ test "Terminal: fullReset tracked pins" { // this test around to ensure we don't regress at multiple layers. test "Terminal: resize less cols with wide char then print" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 3, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 3, .rows = 3 }); defer t.deinit(alloc); try t.print('x'); try t.print('😀'); // 0x1F600 - try t.resize(alloc, 2, 3); + try t.resize(alloc, .{ .cols = 2, .rows = 3 }); t.setCursorPos(1, 2); try t.print('😀'); // 0x1F600 } @@ -13846,27 +15773,29 @@ test "Terminal: resize less cols with wide char then print" { // This was found via fuzzing so its highly specific. test "Terminal: resize with left and right margin set" { const alloc = testing.allocator; + const io_impl = testing.io; const cols = 70; const rows = 23; - var t = try init(alloc, .{ .cols = cols, .rows = rows }); + var t = try init(io_impl, alloc, .{ .cols = cols, .rows = rows }); defer t.deinit(alloc); t.modes.set(.enable_left_and_right_margin, true); try t.print('0'); t.modes.set(.enable_mode_3, true); - try t.resize(alloc, cols, rows); + try t.resize(alloc, .{ .cols = cols, .rows = rows }); t.setLeftAndRightMargin(2, 0); try t.printRepeat(1850); _ = t.modes.restore(.enable_mode_3); - try t.resize(alloc, cols, rows); + try t.resize(alloc, .{ .cols = cols, .rows = rows }); } // https://github.com/mitchellh/ghostty/issues/1343 test "Terminal: resize with wraparound off" { const alloc = testing.allocator; + const io_impl = testing.io; const cols = 4; const rows = 2; - var t = try init(alloc, .{ .cols = cols, .rows = rows }); + var t = try init(io_impl, alloc, .{ .cols = cols, .rows = rows }); defer t.deinit(alloc); t.modes.set(.wraparound, false); @@ -13875,7 +15804,7 @@ test "Terminal: resize with wraparound off" { try t.print('2'); try t.print('3'); const new_cols = 2; - try t.resize(alloc, new_cols, rows); + try t.resize(alloc, .{ .cols = new_cols, .rows = rows }); const str = try t.plainString(testing.allocator); defer testing.allocator.free(str); @@ -13884,9 +15813,10 @@ test "Terminal: resize with wraparound off" { test "Terminal: resize with wraparound on" { const alloc = testing.allocator; + const io_impl = testing.io; const cols = 4; const rows = 2; - var t = try init(alloc, .{ .cols = cols, .rows = rows }); + var t = try init(io_impl, alloc, .{ .cols = cols, .rows = rows }); defer t.deinit(alloc); t.modes.set(.wraparound, true); @@ -13895,7 +15825,7 @@ test "Terminal: resize with wraparound on" { try t.print('2'); try t.print('3'); const new_cols = 2; - try t.resize(alloc, new_cols, rows); + try t.resize(alloc, .{ .cols = new_cols, .rows = rows }); const str = try t.plainString(testing.allocator); defer testing.allocator.free(str); @@ -13904,7 +15834,8 @@ test "Terminal: resize with wraparound on" { test "Terminal: resize with high unique style per cell" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 30, .rows = 30 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 30, .rows = 30 }); defer t.deinit(alloc); for (0..t.rows) |y| { @@ -13919,12 +15850,13 @@ test "Terminal: resize with high unique style per cell" { } } - try t.resize(alloc, 60, 30); + try t.resize(alloc, .{ .cols = 60, .rows = 30 }); } test "Terminal: resize with high unique style per cell with wrapping" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 30, .rows = 30 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 30, .rows = 30 }); defer t.deinit(alloc); const cell_count: u16 = @intCast(t.rows * t.cols); @@ -13940,12 +15872,13 @@ test "Terminal: resize with high unique style per cell with wrapping" { try t.print('x'); } - try t.resize(alloc, 60, 30); + try t.resize(alloc, .{ .cols = 60, .rows = 30 }); } test "Terminal: resize with reflow and saved cursor" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 3 }); defer t.deinit(alloc); try t.printString("1A2B"); t.setCursorPos(2, 2); @@ -13955,7 +15888,7 @@ test "Terminal: resize with reflow and saved cursor" { .y = t.screens.active.cursor.y, } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u32, 'B'), cell.content.codepoint); + try testing.expectEqual(@as(u32, 'B'), cell.content.codepoint.data); } { @@ -13965,7 +15898,7 @@ test "Terminal: resize with reflow and saved cursor" { } t.saveCursor(); - try t.resize(alloc, 5, 3); + try t.resize(alloc, .{ .cols = 5, .rows = 3 }); t.restoreCursor(); { @@ -13981,13 +15914,14 @@ test "Terminal: resize with reflow and saved cursor" { .y = t.screens.active.cursor.y, } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u32, 'B'), cell.content.codepoint); + try testing.expectEqual(@as(u32, 'B'), cell.content.codepoint.data); } } test "Terminal: resize with reflow and saved cursor pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 2, .rows = 3 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 2, .rows = 3 }); defer t.deinit(alloc); try t.printString("1A2B"); { @@ -13996,7 +15930,7 @@ test "Terminal: resize with reflow and saved cursor pending wrap" { .y = t.screens.active.cursor.y, } }).?; const cell = list_cell.cell; - try testing.expectEqual(@as(u32, 'B'), cell.content.codepoint); + try testing.expectEqual(@as(u32, 'B'), cell.content.codepoint.data); } { @@ -14006,7 +15940,7 @@ test "Terminal: resize with reflow and saved cursor pending wrap" { } t.saveCursor(); - try t.resize(alloc, 5, 3); + try t.resize(alloc, .{ .cols = 5, .rows = 3 }); t.restoreCursor(); { @@ -14026,7 +15960,8 @@ test "Terminal: resize with reflow and saved cursor pending wrap" { test "Terminal: DECCOLM without DEC mode 40" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.@"132_column", true); @@ -14038,7 +15973,8 @@ test "Terminal: DECCOLM without DEC mode 40" { test "Terminal: DECCOLM unset" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.enable_mode_3, true); @@ -14049,7 +15985,8 @@ test "Terminal: DECCOLM unset" { test "Terminal: DECCOLM resets pending wrap" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); for ("ABCDE") |c| try t.print(c); @@ -14064,7 +16001,8 @@ test "Terminal: DECCOLM resets pending wrap" { test "Terminal: DECCOLM preserves SGR bg" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); try t.setAttribute(.{ .direct_color_bg = .{ @@ -14088,7 +16026,8 @@ test "Terminal: DECCOLM preserves SGR bg" { test "Terminal: DECCOLM resets scroll region" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.enable_left_and_right_margin, true); @@ -14107,7 +16046,8 @@ test "Terminal: DECCOLM resets scroll region" { test "Terminal: mode 47 alt screen plain" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Print on primary screen @@ -14158,7 +16098,8 @@ test "Terminal: mode 47 alt screen plain" { test "Terminal: mode 47 copies cursor both directions" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Color our cursor red @@ -14194,7 +16135,8 @@ test "Terminal: mode 47 copies cursor both directions" { test "Terminal: mode 1047 alt screen plain" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Print on primary screen @@ -14245,7 +16187,8 @@ test "Terminal: mode 1047 alt screen plain" { test "Terminal: mode 1047 copies cursor both directions" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Color our cursor red @@ -14281,7 +16224,8 @@ test "Terminal: mode 1047 copies cursor both directions" { test "Terminal: mode 1049 alt screen plain" { const alloc = testing.allocator; - var t = try init(alloc, .{ .rows = 5, .cols = 5 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Print on primary screen @@ -14346,7 +16290,8 @@ test "Terminal: mode 1049 alt screen plain" { // characters straddling the right margin boundary leave orphaned spacer_tails. test "Terminal: deleteLines wide char at right margin with full clear" { const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 80, .rows = 24 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 80, .rows = 24 }); defer t.deinit(alloc); // Place a wide character at col 39 (1-indexed) on several rows. @@ -14368,8 +16313,11 @@ test "Terminal: deleteLines wide char at right margin with full clear" { } test "Terminal: glyph APC stores session glossary entries" { + if (comptime !build_options.glyph_protocol) return error.SkipZigTest; + const alloc = testing.allocator; - var t = try init(alloc, .{ .cols = 80, .rows = 24 }); + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 80, .rows = 24 }); defer t.deinit(alloc); var register_parser = glyph.CommandParser.init(alloc, 1024 * 1024); @@ -14398,3 +16346,161 @@ test "Terminal: glyph APC stores session glossary entries" { t.fullReset(); try testing.expect(!t.glyph_glossary.contains(0xE0A0)); } + +test "Terminal: scroll region linefeed recycled row has default metadata" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 5 }); + defer t.deinit(alloc); + + // A soft-wrapped line across rows 0-2 so that row 1 has both wrap + // flags set. Mark row 1 as a prompt as well (OSC 133 A would). + for (0..12) |_| try t.print('A'); + t.screens.active.pages.getCell( + .{ .active = .{ .y = 1 } }, + ).?.row.semantic_prompt = .prompt; + + // DECSTBM rows 2-4, cursor to the region bottom, and linefeed: + // row 1 is discarded and its Row storage recycled as the new + // blank region-bottom row. + t.setTopAndBottomMargin(2, 4); + t.setCursorPos(4, 1); + try t.linefeed(); + + { + const rac = t.screens.active.pages.getCell(.{ .active = .{ .y = 3 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "Terminal: alt screen scroll up recycled row has default metadata" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); + defer t.deinit(alloc); + + try t.switchScreenMode(.@"1049", true); + + // A soft-wrapped line across rows 0-1 and a prompt mark on row 0. + for (0..7) |_| try t.print('A'); + t.screens.active.pages.getCell( + .{ .active = .{} }, + ).?.row.semantic_prompt = .prompt; + + // Scroll up: with no scrollback, row 0 is discarded and its Row + // storage recycled as the new blank bottom row. + try t.scrollUp(1); + + { + const rac = t.screens.active.pages.getCell(.{ .active = .{ .y = 2 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "Terminal: insertLines count over region blanks row metadata" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 5 }); + defer t.deinit(alloc); + + // A soft-wrapped line across rows 0-2 so that row 1 has both wrap + // flags set, plus a prompt mark on row 1. + for (0..12) |_| try t.print('A'); + t.screens.active.pages.getCell( + .{ .active = .{ .y = 1 } }, + ).?.row.semantic_prompt = .prompt; + + // Insert more lines than remain in the region: every row from the + // cursor to the region bottom is blanked in place, with no shifts. + t.setCursorPos(2, 1); + t.insertLines(10); + + for (1..5) |y| { + const rac = t.screens.active.pages.getCell( + .{ .active = .{ .y = @intCast(y) } }, + ).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "Terminal: deleteLines count over region blanks row metadata" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 5 }); + defer t.deinit(alloc); + + for (0..12) |_| try t.print('A'); + t.screens.active.pages.getCell( + .{ .active = .{ .y = 1 } }, + ).?.row.semantic_prompt = .prompt; + + t.setCursorPos(2, 1); + t.deleteLines(10); + + for (1..5) |y| { + const rac = t.screens.active.pages.getCell( + .{ .active = .{ .y = @intCast(y) } }, + ).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "Terminal: deleteLines blank row does not retain semantic prompt" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 5, .rows = 3 }); + defer t.deinit(alloc); + + // Mark row 0 as a prompt row, then delete it. The blank row that + // appears at the region bottom reuses the deleted row's storage + // and must not read as a prompt (e.g. for prompt navigation). + try t.print('$'); + t.screens.active.pages.getCell( + .{ .active = .{} }, + ).?.row.semantic_prompt = .prompt; + + t.setCursorPos(1, 1); + t.deleteLines(1); + + { + const rac = t.screens.active.pages.getCell(.{ .active = .{ .y = 2 } }).?; + try testing.expect(!rac.row.wrap); + try testing.expect(!rac.row.wrap_continuation); + try testing.expectEqual(.none, rac.row.semantic_prompt); + } +} + +test "Terminal: eraseDisplay complete ignores stale prompt on recycled row" { + const alloc = testing.allocator; + const io_impl = testing.io; + var t = try init(io_impl, alloc, .{ .cols = 10, .rows = 3 }); + defer t.deinit(alloc); + + // Screen content that must NOT enter the scrollback on a clear. + try t.printString("hello"); + + // Mark row 1 as a prompt row and then discard it with a region + // scroll, recycling its storage as the blank bottom row. + t.screens.active.pages.getCell( + .{ .active = .{ .y = 1 } }, + ).?.row.semantic_prompt = .prompt; + t.setTopAndBottomMargin(2, 3); + t.setCursorPos(3, 1); + try t.linefeed(); + t.setTopAndBottomMargin(0, 0); + + // ED2: since no prompt is on screen, this must NOT take the + // scroll-and-clear path that pushes content into scrollback. A + // stale prompt flag on the recycled blank bottom row would. + t.eraseDisplay(.complete, false); + + try testing.expectEqual(t.screens.active.pages.rows, t.screens.active.pages.total_rows); +} diff --git a/vendor/libghostty-vt/src/terminal/ansi.zig b/vendor/libghostty-vt/src/terminal/ansi.zig index 4e777b7c..4e28995d 100644 --- a/vendor/libghostty-vt/src/terminal/ansi.zig +++ b/vendor/libghostty-vt/src/terminal/ansi.zig @@ -99,8 +99,11 @@ pub const ModifyKeyFormat = lib.Enum( /// The protection modes that can be set for the terminal. See DECSCA and /// ESC V, W. -pub const ProtectedMode = enum { - off, - iso, // ESC V, W - dec, // CSI Ps " q -}; +pub const ProtectedMode = lib.Enum( + lib.target, + &.{ + "off", + "iso", // ESC V, W + "dec", // CSI Ps " q + }, +); diff --git a/vendor/libghostty-vt/src/terminal/apc.zig b/vendor/libghostty-vt/src/terminal/apc.zig index 121f906a..1ae70f5f 100644 --- a/vendor/libghostty-vt/src/terminal/apc.zig +++ b/vendor/libghostty-vt/src/terminal/apc.zig @@ -13,6 +13,10 @@ const log = std.log.scoped(.terminal_apc); pub const Handler = struct { state: State = .inactive, + /// Maximum content bytes retained for unsupported APC identifiers. Zero + /// drops and ignores unknown APC values. + unknown_max_bytes: usize = 0, + /// Maximum bytes each APC protocol can buffer. This is to prevent /// malicious input from causing us to allocate too much memory. /// If you want to be lazy and set a single value for all protocols, @@ -23,8 +27,7 @@ pub const Handler = struct { }), /// Protocols recognized by this APC handler. When a protocol is absent, - /// matching APC sequences are ignored so callers see the same behavior as - /// an unsupported protocol: no command execution and no response. + /// matching APC sequences are ignored and are not reported as unknown. enabled: std.EnumSet(Protocol) = .initFull(), pub fn deinit(self: *Handler) void { @@ -50,51 +53,74 @@ pub const Handler = struct { // recognize it so there is no need to store the data in memory. .ignore => return, + // Unsupported APC content is retained only when enabled. + .unknown => |*unknown| unknown.append(&.{byte}), + // We identify the APC command by the first byte. .identify => |*id| id: { // Kitty graphics is detected immediately on the `G` byte, // since commands begin immediately after with no termination // character after the 'G'. - if (comptime build_options.kitty_graphics) { - if (id.len == 0 and - byte == 'G' and - self.enabled.contains(.kitty)) - { - self.state = .{ .kitty = .init( - alloc, - self.max_bytes.get(.kitty) orelse - Protocol.defaultMaxBytes(.kitty), - ) }; - break :id; + if (id.len == 0 and byte == 'G') { + if (comptime build_options.kitty_graphics) { + if (self.enabled.contains(.kitty)) { + self.state = .{ .kitty = .init( + alloc, + self.max_bytes.get(.kitty) orelse + Protocol.defaultMaxBytes(.kitty), + ) }; + } else { + self.state = .ignore; + } + } else { + self.state = .ignore; } + break :id; } // If we hit `;` then identify... if (byte == ';') { const str = id.buf[0..id.len]; - if (std.mem.eql(u8, str, "25a1") and - self.enabled.contains(.glyph)) - { - self.state = .{ .glyph = .init( - alloc, - self.max_bytes.get(.glyph) orelse - Protocol.defaultMaxBytes(.glyph), - ) }; + if (std.mem.eql(u8, str, glyph.identifier)) { + if (comptime build_options.glyph_protocol) { + if (self.enabled.contains(.glyph)) { + self.state = .{ .glyph = .init( + alloc, + self.max_bytes.get(.glyph) orelse + Protocol.defaultMaxBytes(.glyph), + ) }; + } else { + self.state = .ignore; + } + } else { + self.state = .ignore; + } } else { - self.state = .ignore; + self.beginUnknown(alloc, str, &.{byte}); } break :id; } - // If we're out of space to buffer then we're done. + // If we're out of identification space, the identifier is + // unsupported. Preserve the buffered prefix before replacing + // the identify union state. if (id.len >= id.buf.len) { - self.state = .ignore; + self.beginUnknown(alloc, id.buf[0..id.len], &.{byte}); break :id; } + const expected_idx: usize = id.len; id.buf[id.len] = byte; id.len += 1; + + // Once the buffered input is no longer a prefix of a known + // protocol, it is an unsupported identifier. + if (self.unknown_max_bytes > 0 and + byte != glyph.identifier[expected_idx]) + { + self.beginUnknown(alloc, id.buf[0..id.len], &.{}); + } }, .kitty => |*p| if (comptime build_options.kitty_graphics) { @@ -105,12 +131,35 @@ pub const Handler = struct { }; } else unreachable, - .glyph => |*p| p.feed(byte) catch |err| { - log.warn("glyph protocol error: {}", .{err}); - p.deinit(); - self.state = .ignore; - }, + .glyph => |*p| if (comptime build_options.glyph_protocol) { + p.feed(byte) catch |err| { + log.warn("glyph protocol error: {}", .{err}); + p.deinit(); + self.state = .ignore; + }; + } else unreachable, + } + } + + /// Transition from protocol identification to bounded unknown capture. + fn beginUnknown( + self: *Handler, + alloc: Allocator, + prefix: []const u8, + suffix: []const u8, + ) void { + const max_bytes = self.unknown_max_bytes; + if (max_bytes == 0) { + self.state = .ignore; + return; } + + // Build the replacement before overwriting identify because prefix + // points into that union field. + var unknown: UnknownBuilder = .init(alloc, max_bytes); + unknown.append(prefix); + unknown.append(suffix); + self.state = .{ .unknown = unknown }; } /// Feed a slice of bytes to the handler. This is equivalent to @@ -126,6 +175,12 @@ pub const Handler = struct { // We're ignoring this APC command; drop the whole slice. .ignore => return, + // We're capturing an unknown APC command, so store it. + .unknown => |*unknown| { + unknown.append(rem); + return; + }, + // Identification consumes at most a few bytes; step // through them one at a time until the state changes. .identify => { @@ -142,18 +197,20 @@ pub const Handler = struct { return; } else unreachable, - .glyph => |*p| { + .glyph => |*p| if (comptime build_options.glyph_protocol) { p.feedSlice(rem) catch |err| { log.warn("glyph protocol error: {}", .{err}); p.deinit(); self.state = .ignore; }; return; - }, + } else unreachable, } } } + /// Complete the current APC. The caller owns a returned result and must + /// call `Command.deinit` with the allocator used while feeding the APC. pub fn end(self: *Handler) ?Command { defer { self.state.deinit(); @@ -163,11 +220,12 @@ pub const Handler = struct { return switch (self.state) { .inactive => unreachable, .ignore, .identify => null, + .unknown => |*unknown| .{ .unknown = unknown.toOwned() }, .kitty => |*p| kitty: { if (comptime !build_options.kitty_graphics) unreachable; // Use the same allocator that was used to create the parser. - const alloc = p.arena.child_allocator; + const alloc = p.alloc; const command = p.complete(alloc) catch |err| { log.warn("kitty graphics protocol error: {}", .{err}); break :kitty null; @@ -177,6 +235,8 @@ pub const Handler = struct { }, .glyph => |*p| glyph_cmd: { + if (comptime !build_options.glyph_protocol) unreachable; + const command = p.complete(p.alloc) catch |err| { log.warn("glyph protocol error: {}", .{err}); break :glyph_cmd null; @@ -207,7 +267,7 @@ pub const State = union(enum) { /// identify: struct { len: u3 = 0, - buf: [4]u8 = undefined, + buf: [glyph.identifier.len]u8 = undefined, }, /// Kitty graphics protocol @@ -217,12 +277,24 @@ pub const State = union(enum) { void, /// Glyph protocol - glyph: glyph.CommandParser, + glyph: if (build_options.glyph_protocol) + glyph.CommandParser + else + void, + + /// An unsupported APC retained for the optional unknown callback. + /// Keep this after recognized protocol states so their tag values and + /// generated dispatch stay stable when unknown capture is unused. + unknown: UnknownBuilder, pub fn deinit(self: *State) void { switch (self.*) { .inactive, .ignore, .identify => {}, - .glyph => |*v| v.deinit(), + .unknown => |*v| v.deinit(), + .glyph => |*v| if (comptime build_options.glyph_protocol) + v.deinit() + else + unreachable, .kitty => |*v| if (comptime build_options.kitty_graphics) v.deinit() else @@ -231,6 +303,95 @@ pub const State = union(enum) { } }; +/// UnknownBuilder is responsible for accumulating bytes for an +/// unidentified APC command if unknown capture is enabled. +const UnknownBuilder = struct { + data: std.ArrayList(u8) = .empty, + alloc: Allocator, + max_bytes: usize, + truncated: bool = false, + + fn init(alloc: Allocator, max_bytes: usize) UnknownBuilder { + return .{ + .alloc = alloc, + .max_bytes = max_bytes, + }; + } + + fn deinit(self: *UnknownBuilder) void { + self.data.deinit(self.alloc); + self.data = .empty; + } + + // Append some bytes to the unknown capture. This flags as truncated + // if allocation fails or we reach our byte limit, therefore + // it can't fail. + fn append(self: *UnknownBuilder, bytes: []const u8) void { + if (bytes.len == 0) return; + const current = self.data.items.len; + + // Determine how many bytes we can store in this append and + // if it is less than our input, then we have to note we're + // truncating. + const retained = @min(bytes.len, self.max_bytes -| current); + if (retained < bytes.len) self.truncated = true; + + // If we require more bytes than our capacity allows then we + // need to grow. + const required = current + retained; + if (required > self.data.capacity) { + const capacity = @min( + self.max_bytes, + @max(required, @max(self.data.capacity *| 2, 1)), + ); + self.data.ensureTotalCapacityPrecise( + self.alloc, + capacity, + ) catch { + self.truncated = true; + return; + }; + } + + self.data.appendSliceAssumeCapacity(bytes[0..retained]); + } + + /// Convert the current capture state to an Unknown where allocator + /// ownership shifts to Unknown. Removes any accumulated unknown + /// capture in this struct. + /// + /// This can't fail because if there is an allocator issue we return + /// an empty truncate-flagged Unknown. + fn toOwned(self: *UnknownBuilder) Unknown { + // toOwnedSlice allows us to reuse data but this makes error + // handling a little simpler. + const content = self.data.toOwnedSlice(self.alloc) catch { + self.data.deinit(self.alloc); + self.data = .empty; + return .{ + .content = self.data.items, + .truncated = true, + }; + }; + + return .{ + .content = content, + .truncated = self.truncated, + }; + } +}; + +/// An unsupported APC returned by `Handler.end`. +pub const Unknown = struct { + content: []u8, + truncated: bool, + + pub fn deinit(self: *Unknown, alloc: Allocator) void { + if (self.content.len > 0) alloc.free(self.content); + self.* = undefined; + } +}; + /// Possible APC command types. pub const Protocol = enum { kitty, @@ -248,16 +409,32 @@ pub const Protocol = enum { .glyph => 1 * 1024 * 1024, }; } + + /// Return the largest default buffer limit across every APC protocol. + /// Consumers that must retain any unfinished APC can derive their limit + /// here instead of duplicating a particular protocol's current default. + pub fn maxDefaultBytes() usize { + var result: usize = 0; + for (std.enums.values(Protocol)) |protocol| { + result = @max(result, protocol.defaultMaxBytes()); + } + return result; + } }; -/// Possible APC commands. -pub const Command = union(Protocol) { +/// A recognized or unsupported APC command. +pub const Command = union(enum) { kitty: if (build_options.kitty_graphics) kitty_gfx.Command else void, - glyph: glyph.Request, + glyph: if (build_options.glyph_protocol) + glyph.Request + else + void, + + unknown: Unknown, pub fn deinit(self: *Command, alloc: Allocator) void { switch (self.*) { @@ -266,7 +443,12 @@ pub const Command = union(Protocol) { else unreachable, - .glyph => |*v| v.deinit(alloc), + .glyph => |*v| if (comptime build_options.glyph_protocol) + v.deinit(alloc) + else + unreachable, + + .unknown => |*v| v.deinit(alloc), } } }; @@ -281,6 +463,64 @@ test "unknown APC command" { try testing.expect(h.end() == null); } +test "capture unknown APC command" { + const testing = std.testing; + const alloc = testing.allocator; + + var h: Handler = .{ .unknown_max_bytes = 5 }; + defer h.deinit(); + h.start(); + h.feedSlice(alloc, "abcd;payload"); + + var result = h.end().?; + defer result.deinit(alloc); + const unknown = &result.unknown; + try testing.expectEqualStrings("abcd;", unknown.content); + try testing.expect(unknown.truncated); +} + +test "capture short unknown APC command" { + const testing = std.testing; + const alloc = testing.allocator; + + var h: Handler = .{ .unknown_max_bytes = 16 }; + defer h.deinit(); + h.start(); + h.feed(alloc, 'X'); + + var result = h.end().?; + const unknown = &result.unknown; + try testing.expectEqualStrings("X", unknown.content); + try testing.expect(!unknown.truncated); + result.deinit(alloc); + + h.unknown_max_bytes = 1; + h.start(); + h.feedSlice(alloc, "XYZ"); + result = h.end().?; + const truncated = &result.unknown; + try testing.expectEqualStrings("X", truncated.content); + try testing.expect(truncated.truncated); + result.deinit(alloc); +} + +test "disabled known APC protocol is not unknown" { + const testing = std.testing; + const alloc = testing.allocator; + + var h: Handler = .{ .unknown_max_bytes = 64 }; + defer h.deinit(); + h.enable(.glyph, false); + h.start(); + h.feedSlice(alloc, "25a1;q;cp=E0A0"); + try testing.expect(h.end() == null); + + // An incomplete known protocol identifier is malformed, not unknown. + h.start(); + h.feedSlice(alloc, "25a"); + try testing.expect(h.end() == null); +} + test "garbage Kitty command" { if (comptime !build_options.kitty_graphics) return error.SkipZigTest; @@ -363,9 +603,9 @@ test "valid Kitty command" { const input = "Gf=24,s=10,v=20,hello=world"; for (input) |c| h.feed(alloc, c); - var cmd = h.end().?; - defer cmd.deinit(alloc); - try testing.expect(cmd == .kitty); + var result = h.end().?; + defer result.deinit(alloc); + try testing.expect(result == .kitty); } test "identify with unrecognized command" { @@ -418,6 +658,8 @@ test "garbage glyph command" { } test "valid glyph command" { + if (comptime !build_options.glyph_protocol) return error.SkipZigTest; + const testing = std.testing; const alloc = testing.allocator; @@ -425,10 +667,10 @@ test "valid glyph command" { h.start(); for ("25a1;q;cp=E0A0") |c| h.feed(alloc, c); - var cmd = h.end().?; - defer cmd.deinit(alloc); - try testing.expect(cmd == .glyph); - try testing.expect(cmd.glyph == .query); + var result = h.end().?; + defer result.deinit(alloc); + try testing.expect(result == .glyph); + try testing.expect(result.glyph == .query); } test "feedSlice valid Kitty command" { @@ -441,12 +683,12 @@ test "feedSlice valid Kitty command" { h.start(); h.feedSlice(alloc, "Gf=24,s=10,v=20;aGVsbG8="); - var cmd = h.end().?; - defer cmd.deinit(alloc); - try testing.expect(cmd == .kitty); + var result = h.end().?; + defer result.deinit(alloc); + try testing.expect(result == .kitty); // The payload is base64-decoded by the parser on completion. - try testing.expectEqualStrings("hello", cmd.kitty.data); + try testing.expectEqualStrings("hello", result.kitty.data); } test "feedSlice identify split across slices" { @@ -461,12 +703,12 @@ test "feedSlice identify split across slices" { h.feedSlice(alloc, "f=24,s=10,"); h.feedSlice(alloc, "v=20;aGVsbG8="); - var cmd = h.end().?; - defer cmd.deinit(alloc); - try testing.expect(cmd == .kitty); + var result = h.end().?; + defer result.deinit(alloc); + try testing.expect(result == .kitty); // The payload is base64-decoded by the parser on completion. - try testing.expectEqualStrings("hello", cmd.kitty.data); + try testing.expectEqualStrings("hello", result.kitty.data); } test "feedSlice unknown APC command is ignored" { @@ -482,6 +724,8 @@ test "feedSlice unknown APC command is ignored" { } test "feedSlice valid glyph command" { + if (comptime !build_options.glyph_protocol) return error.SkipZigTest; + const testing = std.testing; const alloc = testing.allocator; @@ -489,10 +733,10 @@ test "feedSlice valid glyph command" { h.start(); h.feedSlice(alloc, "25a1;q;cp=E0A0"); - var cmd = h.end().?; - defer cmd.deinit(alloc); - try testing.expect(cmd == .glyph); - try testing.expect(cmd.glyph == .query); + var result = h.end().?; + defer result.deinit(alloc); + try testing.expect(result == .glyph); + try testing.expect(result.glyph == .query); } test "feedSlice kitty max bytes exceeded" { diff --git a/vendor/libghostty-vt/src/terminal/apc/glyph.zig b/vendor/libghostty-vt/src/terminal/apc/glyph.zig index 727e1ab4..e86ee9f8 100644 --- a/vendor/libghostty-vt/src/terminal/apc/glyph.zig +++ b/vendor/libghostty-vt/src/terminal/apc/glyph.zig @@ -133,9 +133,7 @@ //! ## Glossary Capacity //! //! Each session holds at most 1024 registrations keyed by codepoint. -//! Registrations live for the session duration. A 1025th registration -//! evicts the oldest entry (FIFO). Sessions are isolated: two tabs may -//! independently register the same codepoint. +//! Each allocation made by the `glyf` decoder is limited to 64 KiB. //! //! ## Security: PUA-Only Restriction //! @@ -150,6 +148,9 @@ const std = @import("std"); +/// APC identifier for the glyph protocol. +pub const identifier = "25a1"; + pub const request = @import("glyph/request.zig"); pub const response = @import("glyph/response.zig"); pub const execute = @import("glyph/execute.zig").execute; diff --git a/vendor/libghostty-vt/src/terminal/apc/glyph/Glossary.zig b/vendor/libghostty-vt/src/terminal/apc/glyph/Glossary.zig index 28458dbb..7200d162 100644 --- a/vendor/libghostty-vt/src/terminal/apc/glyph/Glossary.zig +++ b/vendor/libghostty-vt/src/terminal/apc/glyph/Glossary.zig @@ -20,6 +20,11 @@ const Constraint = FontGlyph.RenderOptions.Constraint; /// Defined by the specification. pub const max_entries = 1024; +/// Maximum size of any allocation made while decoding a glyf registration. +/// This is limited by the dynamic allocations not the struct size so the +/// actual size allowed is slightly larger, but worst case only by 10KB. +pub const max_glyf_allocation = 64 * 1024; // 64 KiB + /// An empty glossary with no registered glyphs. pub const empty: Glossary = .{ .entries = .empty }; @@ -170,7 +175,7 @@ pub const Entry = struct { // Decode the payload into some usable glyph format for // future rasterization. const glyph: Glyph = switch (fmt) { - .glyf => .{ .glyf = try req.decodeGlyfPayload(alloc) }, + .glyf => .{ .glyf = try req.decodeGlyfPayload(alloc, max_glyf_allocation) }, .colrv0, .colrv1 => return error.UnsupportedFormat, }; @@ -273,6 +278,47 @@ fn testParseRegister(alloc: Allocator, data: []const u8) !RegisterReq { // and three on-curve points. const test_triangle_glyf_payload = "AAEAZABkA4QDhAACAAABAQEB9P5wAyADhPzgAAA="; +fn testRepeatedPointRegisterReq( + alloc: Allocator, + end_points: []const u16, +) !RegisterReq { + assert(end_points.len > 0); + + var decoded: std.ArrayList(u8) = .empty; + defer decoded.deinit(alloc); + + // Simple glyph header with zero bounds. + const contour_count: u16 = @intCast(end_points.len); + try decoded.appendSlice(alloc, &.{ @truncate(contour_count >> 8), @truncate(contour_count) }); + try decoded.appendNTimes(alloc, 0, 8); + for (end_points) |end_point| try decoded.appendSlice( + alloc, + &.{ @truncate(end_point >> 8), @truncate(end_point) }, + ); + try decoded.appendSlice(alloc, &.{ 0x00, 0x00 }); // No instructions. + + // Maximum-length runs describe on-curve, zero-delta points compactly. + var remaining: usize = @as(usize, end_points[end_points.len - 1]) + 1; + while (remaining > 0) { + const run = @min(remaining, 256); + if (run == 1) { + try decoded.append(alloc, 0x31); + } else { + try decoded.appendSlice(alloc, &.{ 0x39, @intCast(run - 1) }); + } + remaining -= run; + } + + const Encoder = std.base64.standard.Encoder; + const payload = try alloc.alloc(u8, Encoder.calcSize(decoded.items.len)); + defer alloc.free(payload); + const encoded = Encoder.encode(payload, decoded.items); + + const command = try std.fmt.allocPrint(alloc, "r;cp=e000;{s}", .{encoded}); + defer alloc.free(command); + return try testParseRegister(alloc, command); +} + fn testRegisterReq(alloc: Allocator, cp: u21) !RegisterReq { const data = try std.fmt.allocPrint( alloc, @@ -330,6 +376,31 @@ test "Entry init rejects invalid register payload" { try testing.expectError(error.MalformedPayload, Entry.init(alloc, req)); } +test "Entry init rejects glyf allocation over limit" { + const testing = std.testing; + const alloc = testing.allocator; + + // 5,462 points require a 65,544-byte points allocation. + const req = try testRepeatedPointRegisterReq(alloc, &.{5461}); + defer alloc.free(req.raw); + + try testing.expectError(error.PayloadTooLarge, Entry.init(alloc, req)); +} + +test "Entry init accepts largest glyf points allocation under limit" { + const testing = std.testing; + const alloc = testing.allocator; + + // 5,461 points require a 65,532-byte points allocation. + const req = try testRepeatedPointRegisterReq(alloc, &.{ 0, 5460 }); + defer alloc.free(req.raw); + + var entry = try Entry.init(alloc, req); + defer entry.deinit(alloc); + try testing.expectEqual(@as(usize, 5461), entry.glyph.glyf.points.len); + try testing.expectEqual(@as(usize, 2), entry.glyph.glyf.contours.len); +} + test "Glossary register overwrites and moves entry to newest position" { const testing = std.testing; const alloc = testing.allocator; diff --git a/vendor/libghostty-vt/src/terminal/apc/glyph/request.zig b/vendor/libghostty-vt/src/terminal/apc/glyph/request.zig index 71a2dd4b..6b6d7ccd 100644 --- a/vendor/libghostty-vt/src/terminal/apc/glyph/request.zig +++ b/vendor/libghostty-vt/src/terminal/apc/glyph/request.zig @@ -1,7 +1,9 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const assert = std.debug.assert; +const LimitedAllocator = @import("../../../datastruct/main.zig").LimitedAllocator; const Glyf = @import("../../../font/opentype/glyf.zig").Glyf; +const fraction = @import("../../fraction.zig"); /// Maximum decoded glyph payload size accepted by the protocol. /// This is documented in the spec. @@ -271,7 +273,11 @@ pub const Request = union(enum) { }; /// Decode this request's base64 glyf payload into an owned outline. - pub fn decodeGlyfPayload(self: Register, alloc: Allocator) DecodeError!Glyf.Outline { + pub fn decodeGlyfPayload( + self: Register, + alloc: Allocator, + max_allocation_bytes: usize, + ) DecodeError!Glyf.Outline { // Prep base64 decoding, initial validation. const Decoder = std.base64.standard.Decoder; const payload_bytes = self.payload(); @@ -299,12 +305,21 @@ pub const Request = union(enum) { // decode call below. Glyf.Entry.decode returns an owned Outline, so // it is safe to free `data` before returning that outline. const glyf_entry = Glyf.Entry.init(data) catch return error.MalformedPayload; - return glyf_entry.decode(alloc) catch |err| switch (err) { - error.OutOfMemory => error.OutOfMemory, + + // The points slice is the dominant decoded allocation. Limiting + // every allocation made by the decoder prevents a compact record + // from expanding into an unbounded persistent outline. + var limited: LimitedAllocator = .init(alloc, max_allocation_bytes); + return glyf_entry.decode(limited.allocator()) catch |err| switch (err) { + error.OutOfMemory => if (limited.limit_exceeded) + error.PayloadTooLarge + else + error.OutOfMemory, // Unsupported fields error.CompositeNotSupported => error.CompositeUnsupported, error.InstructionsNotSupported => error.HintingUnsupported, // Various semantic issues + error.ReadFailed, error.EndOfStream, error.EndPointsOutOfOrder, error.TooManyPoints, @@ -562,9 +577,7 @@ pub const Pad = struct { /// Top/bottom fractions are relative to cell height; left/right fractions /// are relative to render span width. fn parseFraction(value: []const u8) ?f64 { - const result = std.fmt.parseFloat(f64, value) catch return null; - if (!(result >= 0 and result <= 1)) return null; - return result; + return fraction.parse(value); } }; @@ -789,7 +802,10 @@ test "register decodes glyf payload" { var cmd = try testParse(testing.allocator, "r;cp=e0a0;AAAAAAAAAAAAAA=="); defer cmd.deinit(testing.allocator); - var outline = try cmd.register.decodeGlyfPayload(testing.allocator); + var outline = try cmd.register.decodeGlyfPayload( + testing.allocator, + std.math.maxInt(usize), + ); defer outline.deinit(testing.allocator); try testing.expectEqual(@as(usize, 0), outline.points.len); @@ -802,7 +818,10 @@ test "register rejects malformed glyf payload" { var cmd = try testParse(testing.allocator, "r;cp=e0a0;%%%not-base64%%%"); defer cmd.deinit(testing.allocator); - try testing.expectError(error.MalformedPayload, cmd.register.decodeGlyfPayload(testing.allocator)); + try testing.expectError( + error.MalformedPayload, + cmd.register.decodeGlyfPayload(testing.allocator, std.math.maxInt(usize)), + ); } test "register response without payload" { diff --git a/vendor/libghostty-vt/src/terminal/bitmap_allocator.zig b/vendor/libghostty-vt/src/terminal/bitmap_allocator.zig index 787f0ace..eed0bdd8 100644 --- a/vendor/libghostty-vt/src/terminal/bitmap_allocator.zig +++ b/vendor/libghostty-vt/src/terminal/bitmap_allocator.zig @@ -38,12 +38,16 @@ pub fn BitmapAllocator(comptime chunk_size: comptime_int) type { pub const bitmap_bit_size = @bitSizeOf(u64); /// The bitmap of available chunks. Each bit represents a chunk. A - /// 1 means the chunk is free and a 0 means it's used. We use 1 - /// for free since it makes it very slightly faster to find free - /// chunks. + /// 0 means the chunk is free and a 1 means it's used, so an + /// all-zero bitmap is a fully free allocator. bitmap: Offset(u64), bitmap_count: usize, + /// Lowest bitmap word index that may contain a free bit; words + /// below it are fully allocated, so alloc scans can start here + /// instead of at zero. + search_start: usize = 0, + /// The contiguous buffer of chunks. chunks: Offset(u8), @@ -51,13 +55,23 @@ pub fn BitmapAllocator(comptime chunk_size: comptime_int) type { pub fn init(buf: OffsetBuf, l: Layout) Self { assert(base_align.check(@intFromPtr(buf.start()))); - // Initialize our bitmaps to all 1s to note that all chunks are free. + // Clear our bitmaps to note that all chunks are free. const bitmap = buf.member(u64, l.bitmap_start); - const bitmap_ptr = bitmap.ptr(buf); - @memset(bitmap_ptr[0..l.bitmap_count], std.math.maxInt(u64)); + @memset(bitmap.ptr(buf)[0..l.bitmap_count], 0); + + return initAssumeZeroed(buf, l); + } + /// Initialize the allocator map over memory that the caller + /// guarantees is already zero-filled. + /// + /// This writes nothing to the buffer: an all-zero bitmap already + /// marks every chunk as free. Behavior is undefined if the bitmap + /// region is not zero. + pub fn initAssumeZeroed(buf: OffsetBuf, l: Layout) Self { + assert(base_align.check(@intFromPtr(buf.start()))); return .{ - .bitmap = bitmap, + .bitmap = buf.member(u64, l.bitmap_start), .bitmap_count = l.bitmap_count, .chunks = buf.member(u8, l.chunks_start), }; @@ -95,9 +109,25 @@ pub fn BitmapAllocator(comptime chunk_size: comptime_int) type { return error.OutOfMemory; // Find the index of the free chunk. This also marks it as used. + // Words below search_start have no free bits, so no free span + // can start in or cross them and it is safe to skip them. const bitmaps = self.bitmap.ptr(base); - const idx = findFreeChunks(bitmaps[0..self.bitmap_count], chunk_count) orelse + const start = @min(self.search_start, self.bitmap_count); + const rel = findFreeChunks(bitmaps[start..self.bitmap_count], chunk_count) orelse return error.OutOfMemory; + const idx = start * bitmap_bit_size + rel; + + // Advance past any words the allocation just filled so the + // next scan starts at the first word with a free bit. + self.search_start = start: { + var new_start = start; + while (new_start < self.bitmap_count and + bitmaps[new_start] == std.math.maxInt(u64)) + { + new_start += 1; + } + break :start new_start; + }; const chunks = self.chunks.ptr(base); const ptr: [*]T = @ptrCast(@alignCast(&chunks[idx * chunk_size])); @@ -116,6 +146,12 @@ pub fn BitmapAllocator(comptime chunk_size: comptime_int) type { const chunks = self.chunks.ptr(base); const chunk_idx = @divExact(@intFromPtr(slice.ptr) - @intFromPtr(chunks), chunk_size); + // The freed word gains free bits, so scans must not skip it. + self.search_start = @min( + self.search_start, + @divFloor(chunk_idx, bitmap_bit_size), + ); + const bitmaps = self.bitmap.ptr(base); // Current bitmap index. @@ -130,20 +166,20 @@ pub fn BitmapAllocator(comptime chunk_size: comptime_int) type { // Number of bits we need to mark in this bitmap. const bits = @min(rem, 64 - bit); - bitmaps[i] |= ~@as(u64, 0) >> @intCast(64 - bits) << @intCast(bit); + bitmaps[i] &= ~(~@as(u64, 0) >> @intCast(64 - bits) << @intCast(bit)); rem -= bits; } // Mark any full bitmaps worth of bits that need to be marked. i += 1; while (rem > 64) : (i += 1) { - bitmaps[i] = std.math.maxInt(u64); + bitmaps[i] = 0; rem -= 64; } // Mark any bits at the start of this last bitmap if it needs it. if (rem > 0) { - bitmaps[i] |= ~@as(u64, 0) >> @intCast(64 - rem); + bitmaps[i] &= ~(~@as(u64, 0) >> @intCast(64 - rem)); } } @@ -155,10 +191,9 @@ pub fn BitmapAllocator(comptime chunk_size: comptime_int) type { /// Returns the number of bytes currently in use. pub fn usedBytes(self: Self, base: anytype) usize { const bitmaps = self.bitmap.ptr(base); - var free_chunks: usize = 0; - for (bitmaps[0..self.bitmap_count]) |bitmap| free_chunks += @popCount(bitmap); - const total_chunks = self.bitmap_count * bitmap_bit_size; - return (total_chunks - free_chunks) * chunk_size; + var used_chunks: usize = 0; + for (bitmaps[0..self.bitmap_count]) |bitmap| used_chunks += @popCount(bitmap); + return used_chunks * chunk_size; } /// For testing only. @@ -177,7 +212,7 @@ pub fn BitmapAllocator(comptime chunk_size: comptime_int) type { for (chunk_idx..chunk_idx + chunk_count) |i| { const bitmap = @divFloor(i, bitmap_bit_size); const bit = i % bitmap_bit_size; - if (bitmaps[bitmap] & (@as(u64, 1) << @intCast(bit)) != 0) { + if (bitmaps[bitmap] & (@as(u64, 1) << @intCast(bit)) == 0) { return false; } } @@ -252,7 +287,7 @@ fn findFreeChunks(bitmaps: []u64, n: usize) ?usize { var i: usize = 0; search: while (i < bitmaps.len) { // Number of chunks available at the end of this bitmap. - const prefix = @clz(~bitmaps[i]); + const prefix = @clz(bitmaps[i]); // If there are no chunks available at the end of this bitmap // then we can't start in it, so we'll try the next one. @@ -275,7 +310,7 @@ fn findFreeChunks(bitmaps: []u64, n: usize) ?usize { // There's more than 64 remaining chunks and this bitmap has // content in it, so we try starting again with this bitmap. - if (bitmaps[i] != std.math.maxInt(u64)) continue :search; + if (bitmaps[i] != 0) continue :search; // This bitmap is completely free, we can subtract 64 from // our remaining number. @@ -284,17 +319,17 @@ fn findFreeChunks(bitmaps: []u64, n: usize) ?usize { // If the number of available chunks at the start of this bitmap // is less than the remaining required, we have to try again. - if (@ctz(~bitmaps[i]) < rem) continue; + if (@ctz(bitmaps[i]) < rem) continue; const suffix = (n - prefix) % 64; - // Found! Mark everything between our start and end as full. - bitmaps[start_bitmap] ^= ~@as(u64, 0) >> @intCast(start_bit) << @intCast(start_bit); + // Found! Mark everything between our start and end as used. + bitmaps[start_bitmap] |= ~@as(u64, 0) >> @intCast(start_bit) << @intCast(start_bit); const full_bitmaps = @divFloor(n - prefix - suffix, 64); for (bitmaps[start_bitmap + 1 ..][0..full_bitmaps]) |*bitmap| { - bitmap.* = 0; + bitmap.* = std.math.maxInt(u64); } - if (suffix > 0) bitmaps[i] ^= ~@as(u64, 0) >> @intCast(64 - suffix); + if (suffix > 0) bitmaps[i] |= ~@as(u64, 0) >> @intCast(64 - suffix); return start_bitmap * 64 + start_bit; } @@ -314,8 +349,10 @@ fn findFreeChunks(bitmaps: []u64, n: usize) ?usize { // = 000001000000010000 // ^ ^ // In this example there are 2 places with at least 4 sequential 1s. - var shifted: u64 = bitmap.*; - for (1..n) |i| shifted &= bitmap.* >> @intCast(i); + // Work on the inverted word so that free chunks are 1 bits. + const free = ~bitmap.*; + var shifted: u64 = free; + for (1..n) |i| shifted &= free >> @intCast(i); // If we have zero then we have no matches if (shifted == 0) continue; @@ -326,7 +363,7 @@ fn findFreeChunks(bitmaps: []u64, n: usize) ?usize { // Calculate the mask so we can mark it as used const mask = (@as(u64, std.math.maxInt(u64)) >> @intCast(64 - n)) << @intCast(bit); - bitmap.* ^= mask; + bitmap.* |= mask; return (idx * 64) + bit; } @@ -338,12 +375,12 @@ test "findFreeChunks single found" { const testing = std.testing; var bitmaps = [_]u64{ - 0b10000000_00000000_00000000_00000000_00000000_00000000_00001110_00000000, + 0b01111111_11111111_11111111_11111111_11111111_11111111_11110001_11111111, }; const idx = findFreeChunks(&bitmaps, 2).?; try testing.expectEqual(@as(usize, 9), idx); try testing.expectEqual( - 0b10000000_00000000_00000000_00000000_00000000_00000000_00001000_00000000, + 0b01111111_11111111_11111111_11111111_11111111_11111111_11110111_11111111, bitmaps[0], ); } @@ -351,7 +388,7 @@ test "findFreeChunks single found" { test "findFreeChunks single not found" { const testing = std.testing; - var bitmaps = [_]u64{0b10000111_00000000_00000000_00000000_00000000_00000000_00000000_00000000}; + var bitmaps = [_]u64{0b01111000_11111111_11111111_11111111_11111111_11111111_11111111_11111111}; const idx = findFreeChunks(&bitmaps, 4); try testing.expect(idx == null); } @@ -360,13 +397,13 @@ test "findFreeChunks multiple found" { const testing = std.testing; var bitmaps = [_]u64{ - 0b10000111_00000000_00000000_00000000_00000000_00000000_00000000_01110000, - 0b10000000_00111110_00000000_00000000_00000000_00000000_00111110_00000000, + 0b01111000_11111111_11111111_11111111_11111111_11111111_11111111_10001111, + 0b01111111_11000001_11111111_11111111_11111111_11111111_11000001_11111111, }; const idx = findFreeChunks(&bitmaps, 4).?; try testing.expectEqual(@as(usize, 73), idx); try testing.expectEqual( - 0b10000000_00111110_00000000_00000000_00000000_00000000_00100000_00000000, + 0b01111111_11000001_11111111_11111111_11111111_11111111_11011111_11111111, bitmaps[1], ); } @@ -375,11 +412,11 @@ test "findFreeChunks exactly 64 chunks" { const testing = std.testing; var bitmaps = [_]u64{ - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, }; const idx = findFreeChunks(&bitmaps, 64).?; try testing.expectEqual( - 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, bitmaps[0], ); try testing.expectEqual(@as(usize, 0), idx); @@ -389,16 +426,16 @@ test "findFreeChunks larger than 64 chunks" { const testing = std.testing; var bitmaps = [_]u64{ - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, }; const idx = findFreeChunks(&bitmaps, 65).?; try testing.expectEqual( - 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, bitmaps[0], ); try testing.expectEqual( - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111110, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000001, bitmaps[1], ); try testing.expectEqual(@as(usize, 0), idx); @@ -408,21 +445,21 @@ test "findFreeChunks larger than 64 chunks not at beginning" { const testing = std.testing; var bitmaps = [_]u64{ - 0b11111111_00000000_00000000_00000000_00000000_00000000_00000000_00000000, - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, + 0b00000000_11111111_11111111_11111111_11111111_11111111_11111111_11111111, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, }; const idx = findFreeChunks(&bitmaps, 65).?; try testing.expectEqual( - 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, bitmaps[0], ); try testing.expectEqual( - 0b11111110_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b00000001_11111111_11111111_11111111_11111111_11111111_11111111_11111111, bitmaps[1], ); try testing.expectEqual( - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, bitmaps[2], ); try testing.expectEqual(@as(usize, 56), idx); @@ -432,16 +469,16 @@ test "findFreeChunks larger than 64 chunks exact" { const testing = std.testing; var bitmaps = [_]u64{ - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, - 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, }; const idx = findFreeChunks(&bitmaps, 128).?; try testing.expectEqual( - 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, bitmaps[0], ); try testing.expectEqual( - 0b00000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, + 0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111, bitmaps[1], ); try testing.expectEqual(@as(usize, 0), idx); @@ -654,11 +691,95 @@ test "BitmapAllocator alloc and free one bitmap" { // All of our bitmaps should be free. try testing.expectEqualSlices( u64, - &@as([3]u64, @splat(~@as(u64, 0))), + &@as([3]u64, @splat(0)), bm.bitmap.ptr(buf)[0..3], ); } +test "BitmapAllocator search hint skips full words" { + const Alloc = BitmapAllocator(1); + // Capacity such that we'll have 3 bitmaps. + const cap = Alloc.bitmap_bit_size * 3; + + const testing = std.testing; + const alloc = testing.allocator; + const layout = Alloc.layout(cap); + const buf = try alloc.alignedAlloc(u8, Alloc.base_align, layout.total_size); + defer alloc.free(buf); + + var bm = Alloc.init(.init(buf), layout); + try testing.expectEqual(@as(usize, 0), bm.search_start); + + // Fill the first bitmap word exactly with single-chunk allocations. + var first: []u8 = undefined; + for (0..Alloc.bitmap_bit_size) |i| { + const slice = try bm.alloc(u8, buf, 1); + if (i == 0) first = slice; + } + + // The first word is exhausted so scans start at the second word. + try testing.expectEqual(@as(usize, 1), bm.search_start); + + // The next allocation is the first chunk of the second word, so + // the skipped-word index arithmetic must still yield the right + // chunk address. + const next = try bm.alloc(u8, buf, 1); + try testing.expectEqual( + @intFromPtr(first.ptr) + Alloc.bitmap_bit_size, + @intFromPtr(next.ptr), + ); + + // Freeing a chunk in the first word lowers the hint so the freed + // space is found again. + bm.free(buf, first); + try testing.expectEqual(@as(usize, 0), bm.search_start); + const again = try bm.alloc(u8, buf, 1); + try testing.expectEqual(@intFromPtr(first.ptr), @intFromPtr(again.ptr)); +} + +test "BitmapAllocator search hint does not skip partial words" { + const Alloc = BitmapAllocator(1); + // Capacity such that we'll have 2 bitmaps. + const cap = Alloc.bitmap_bit_size * 2; + + const testing = std.testing; + const alloc = testing.allocator; + const layout = Alloc.layout(cap); + const buf = try alloc.alignedAlloc(u8, Alloc.base_align, layout.total_size); + defer alloc.free(buf); + + var bm = Alloc.init(.init(buf), layout); + + // Allocate all but 4 chunks of the first word. + var first: []u8 = undefined; + for (0..Alloc.bitmap_bit_size - 4) |i| { + const slice = try bm.alloc(u8, buf, 1); + if (i == 0) first = slice; + } + + // An 8-chunk run can't fit the 4 remaining bits (small runs never + // span words), so it comes from the second word — but the hint + // must stay at the first word, which still has free bits. + const big = try bm.alloc(u8, buf, 8); + try testing.expectEqual( + @intFromPtr(first.ptr) + Alloc.bitmap_bit_size, + @intFromPtr(big.ptr), + ); + try testing.expectEqual(@as(usize, 0), bm.search_start); + + // A 4-chunk run fits the first word's remaining bits exactly; a + // hint that skipped the partial word would wrongly place this in + // the second word (or report OutOfMemory once that filled). + const small = try bm.alloc(u8, buf, 4); + try testing.expectEqual( + @intFromPtr(first.ptr) + Alloc.bitmap_bit_size - 4, + @intFromPtr(small.ptr), + ); + + // Now the first word is full and the hint advances past it. + try testing.expectEqual(@as(usize, 1), bm.search_start); +} + test "BitmapAllocator alloc and free half bitmap" { const Alloc = BitmapAllocator(1); // Capacity such that we'll have 3 bitmaps. @@ -691,7 +812,7 @@ test "BitmapAllocator alloc and free half bitmap" { // All of our bitmaps should be free. try testing.expectEqualSlices( u64, - &@as([3]u64, @splat(~@as(u64, 0))), + &@as([3]u64, @splat(0)), bm.bitmap.ptr(buf)[0..3], ); } @@ -746,7 +867,7 @@ test "BitmapAllocator alloc and free two half bitmaps" { // All of our bitmaps should be free. try testing.expectEqualSlices( u64, - &@as([3]u64, @splat(~@as(u64, 0))), + &@as([3]u64, @splat(0)), bm.bitmap.ptr(buf)[0..3], ); } @@ -783,7 +904,7 @@ test "BitmapAllocator alloc and free 1.5 bitmaps" { // All of our bitmaps should be free. try testing.expectEqualSlices( u64, - &@as([3]u64, @splat(~@as(u64, 0))), + &@as([3]u64, @splat(0)), bm.bitmap.ptr(buf)[0..3], ); } @@ -838,7 +959,7 @@ test "BitmapAllocator alloc and free two 1.5 bitmaps" { // All of our bitmaps should be free. try testing.expectEqualSlices( u64, - &@as([3]u64, @splat(~@as(u64, 0))), + &@as([3]u64, @splat(0)), bm.bitmap.ptr(buf)[0..3], ); } @@ -895,7 +1016,7 @@ test "BitmapAllocator alloc and free 1.5 bitmaps offset by 0.75" { // All of our bitmaps should be free. try testing.expectEqualSlices( u64, - &@as([3]u64, @splat(~@as(u64, 0))), + &@as([3]u64, @splat(0)), bm.bitmap.ptr(buf)[0..3], ); } @@ -973,7 +1094,7 @@ test "BitmapAllocator alloc and free three 0.75 bitmaps" { // All of our bitmaps should be free. try testing.expectEqualSlices( u64, - &@as([3]u64, @splat(~@as(u64, 0))), + &@as([3]u64, @splat(0)), bm.bitmap.ptr(buf)[0..3], ); } @@ -1052,7 +1173,7 @@ test "BitmapAllocator alloc and free two 1.5 bitmaps offset 0.75" { // All of our bitmaps should be free. try testing.expectEqualSlices( u64, - &@as([4]u64, @splat(~@as(u64, 0))), + &@as([4]u64, @splat(0)), bm.bitmap.ptr(buf)[0..4], ); } diff --git a/vendor/libghostty-vt/src/terminal/build_options.zig b/vendor/libghostty-vt/src/terminal/build_options.zig index 136e0f10..f16cbf42 100644 --- a/vendor/libghostty-vt/src/terminal/build_options.zig +++ b/vendor/libghostty-vt/src/terminal/build_options.zig @@ -36,9 +36,246 @@ pub const Options = struct { /// Options. c_abi: bool, + /// Optional feature gates, all enabled by default. See Features. + features: Features = .{}, + /// The version of the application. version: std.SemanticVersion, + /// Optional features for lib artifacts. Disabling a feature removes + /// its C API exports and any stream-integrated handling from the + /// build, primarily so size-conscious embedders (e.g. wasm) can trim + /// the binary. The Ghostty application itself always builds with + /// every feature enabled. + /// + /// Field names are the accepted tokens for `-Dvt-features`, which + /// acts like `-Dcpu`: comma-separated modifications applied on top + /// of the default (all enabled) set. `+feature` and `feature` + /// enable, `-feature` disables. The special name `all` refers to + /// every feature, so `-Dvt-features=-all,+render-state` builds only + /// the render state API. Hyphens in names are interchangeable with + /// underscores. Fields are emitted verbatim as bool options on the + /// `terminal_options` module. + pub const Features = packed struct { + /// Terminal state serialization: encode a full terminal (screen + /// contents, scrollback, cursor, styles, modes, etc.) to a + /// compact binary format and decode it back into a live + /// terminal. Used for session persistence, window restoration, + /// and moving terminal state between processes. + /// + /// C API: `ghostty_snapshot_*` and, together with + /// `grid_introspection`, `ghostty_tracked_grid_ref_snapshot`. + snapshot: bool = true, + + /// Textual export of terminal contents as plain text, VT + /// (replayable escape sequences), or HTML, with optional extras + /// such as cursor position, palette, modes, and tabstops. Used + /// for copy-to-clipboard, dump-to-file, and debugging. + /// + /// C API: `ghostty_formatter_*` and, together with `selection`, + /// `ghostty_terminal_selection_format_*`. + formatter: bool = true, + + /// Text selection: select word/line/output/all, selection + /// ordering, adjustment, and containment math, and the + /// click/drag gesture state machine (single/double/triple + /// click behaviors). Selections are expressed in grid + /// references, so enabling this also keeps the untracked grid + /// reference constructors even if `grid_introspection` is + /// disabled. + /// + /// C API: `ghostty_terminal_select_*`, + /// `ghostty_terminal_selection_*`, `ghostty_selection_gesture_*`. + selection: bool = true, + + /// Terminal search: find matches for a string in the active + /// area and scrollback (ASCII case-insensitive), with results + /// that survive primary/alternate screen switches, resize, and + /// scrollback pruning, plus match selection with wrap-around + /// and viewport scrolling. Used to implement find bars. + /// + /// C API: `ghostty_search_*`. + search: bool = true, + + /// The render state API: a coherent, update-in-place view of + /// the visible screen (rows, cells, styles, cursor, colors, + /// palette) designed to drive a renderer at frame rates. This + /// is the primary read path for embedders that draw the + /// terminal. + /// + /// C API: `ghostty_render_state_*`. + render_state: bool = true, + + /// Encoding of host input events into the byte sequences a + /// terminal application expects: keyboard input (including the + /// Kitty keyboard protocol), mouse reporting, focus reporting, + /// and (bracketed) paste. Any interactive embedder needs this; + /// a read-only viewer does not. + /// + /// C API: `ghostty_key_event_*`, `ghostty_key_encoder_*`, + /// `ghostty_mouse_event_*`, `ghostty_mouse_encoder_*`, + /// `ghostty_focus_encode`, `ghostty_paste_*`. + input_encode: bool = true, + + /// Color utilities: luminance, perceived luminance, and + /// contrast math (e.g. for minimum-contrast rendering), color + /// string parsing (`#rrggbb`, X11 color names, XParseColor + /// `rgb:`/`rgbi:` device specs), the default 256-color palette + /// and palette generation from configurable bases, and the X11 + /// color name table itself. + /// + /// C API: `ghostty_color_*`. + color: bool = true, + + /// Direct inspection of grid contents at a position via grid + /// references: cell and row data getters (codepoints, + /// graphemes, style, hyperlink URI, wrap state, ...) plus + /// tracked grid references that stay valid across terminal + /// updates. Used for hyperlink hover/open, accessibility, and + /// tests; deliberately not built for render loops (use + /// `render_state` for that). + /// + /// C API: `ghostty_grid_ref_*`, `ghostty_tracked_grid_ref_*`, + /// `ghostty_cell_get*`, `ghostty_row_get*`, and (shared with + /// `selection`) `ghostty_terminal_grid_ref` / + /// `ghostty_terminal_point_from_grid_ref`. + grid_introspection: bool = true, + + /// The APC glyph protocol: a Ghostty extension that lets + /// terminal applications register custom font glyphs (glyf + /// outlines) for private-use-area codepoints. Embedders that + /// don't render registered glyphs can disable this; the + /// sequences are still consumed and safely ignored. + /// + /// No C API surface of its own; this gates the stream handling + /// and glossary storage. + glyph_protocol: bool = true, + + /// The Kitty graphics protocol: APC command parsing, image + /// transmission and storage (PNG decoding via the sys + /// interface), and placement tracking, plus the read APIs a + /// renderer uses to draw placed images. Disabled sequences are + /// still consumed and safely ignored. + /// + /// This requires the ability to get timestamps from the OS, so + /// it is always disabled on freestanding targets (e.g. + /// wasm32-freestanding) regardless of this setting. + /// + /// C API: `ghostty_kitty_graphics_*`. + kitty_graphics: bool = true, + + pub fn parse(list: []const u8) error{UnknownFeature}!Features { + // Modifications apply on top of the default set. + var result: Features = .{}; + + var it = std.mem.splitAny(u8, list, ", "); + while (it.next()) |raw| { + if (raw.len == 0) continue; + + var name = raw; + var enable = true; + switch (name[0]) { + '+' => name = name[1..], + '-' => { + enable = false; + name = name[1..]; + }, + else => {}, + } + + // A dangling sign with no name is malformed. + if (name.len == 0) return error.UnknownFeature; + + // The special name "all" refers to every feature. + if (std.mem.eql(u8, name, "all")) { + inline for (@typeInfo(Features).@"struct".fields) |field| { + @field(result, field.name) = enable; + } + continue; + } + + var found = false; + inline for (@typeInfo(Features).@"struct".fields) |field| { + if (eqlName(name, field.name)) { + @field(result, field.name) = enable; + found = true; + } + } + if (!found) return error.UnknownFeature; + } + + return result; + } + + /// Feature name equality where '-' in the input matches '_' in + /// the field name. + fn eqlName(input: []const u8, name: []const u8) bool { + if (input.len != name.len) return false; + for (input, name) |a, b| { + const norm: u8 = if (a == '-') '_' else a; + if (norm != b) return false; + } + return true; + } + + test "parse" { + const testing = std.testing; + + const none: Features = @bitCast( + @as(@typeInfo(Features).@"struct".backing_integer.?, 0), + ); + + // Empty input keeps the defaults. + try testing.expectEqual(Features{}, try Features.parse("")); + + // All three token forms, comma separated. + try testing.expectEqual( + Features{ .snapshot = false }, + try Features.parse("-snapshot"), + ); + try testing.expectEqual( + Features{ .snapshot = false, .formatter = false }, + try Features.parse("-snapshot,+render_state,-formatter,color"), + ); + + // The special "all" name refers to every feature. + try testing.expectEqual(none, try Features.parse("-all")); + try testing.expectEqual(Features{}, try Features.parse("-all,+all")); + var render_only = none; + render_only.render_state = true; + try testing.expectEqual( + render_only, + try Features.parse("-all,+render-state"), + ); + + // Hyphens are interchangeable with underscores. + try testing.expectEqual( + Features{ .input_encode = false, .grid_introspection = false }, + try Features.parse("-input-encode,-grid_introspection"), + ); + + // Later tokens win. + try testing.expectEqual( + Features{}, + try Features.parse("-snapshot,+snapshot"), + ); + + // Unknown names and dangling signs are errors. + try testing.expectError(error.UnknownFeature, Features.parse("bogus")); + try testing.expectError(error.UnknownFeature, Features.parse("-snapshot,-")); + } + }; + + /// Whether the Kitty graphics feature is effectively enabled for + /// the given target. Kitty graphics requires the ability to get + /// timestamps and there is no way to do that on freestanding + /// targets, so it is always disabled there regardless of the + /// feature setting. + pub fn kittyGraphics(self: Options, target: std.Target) bool { + if (target.os.tag == .freestanding) return false; + return self.features.kitty_graphics; + } + /// Add the required build options for the terminal module. /// /// The memory referenced by self is expected to stick around (it isn't @@ -55,25 +292,23 @@ pub const Options = struct { opts.addOption(bool, "simd", self.simd); opts.addOption(bool, "slow_runtime_safety", self.slow_runtime_safety); - // Kitty graphics is almost always true. This used to be conditional on - // some other factors but we've since generalized the implementation - // to support optional PNG decoding, OS capabilities like filesystems, - // etc. So its safe to always enable it and just have the - // implementation deal with unsupported features as needed. - // - // We disable it on wasm32-freestanding because we at the least - // require the ability to get timestamps and there is no way to - // do that with freestanding targets. - const target = m.resolved_target.?.result; - opts.addOption( - bool, - "kitty_graphics", - !(target.cpu.arch == .wasm32 and target.os.tag == .freestanding), - ); - // These are synthesized based on other options. opts.addOption(bool, "tmux_control_mode", self.oniguruma); + // Feature gates, emitted as flat bools (e.g. `options.snapshot`). + const target = m.resolved_target.?.result; + inline for (@typeInfo(Features).@"struct".fields) |field| { + var value = @field(self.features, field.name); + + // Kitty graphics is force-disabled on some targets; see + // kittyGraphics for details. + if (comptime std.mem.eql(u8, field.name, "kitty_graphics")) { + value = self.kittyGraphics(target); + } + + opts.addOption(bool, field.name, value); + } + // Version information. opts.addOption( []const u8, @@ -92,3 +327,7 @@ pub const Options = struct { m.addOptions("terminal_options", opts); } }; + +test { + _ = Options.Features; +} diff --git a/vendor/libghostty-vt/src/terminal/c/allocator.zig b/vendor/libghostty-vt/src/terminal/c/allocator.zig index ca698f3c..c6732375 100644 --- a/vendor/libghostty-vt/src/terminal/c/allocator.zig +++ b/vendor/libghostty-vt/src/terminal/c/allocator.zig @@ -7,11 +7,13 @@ const CAllocator = lib.alloc.Allocator; /// (or the default allocator if NULL). /// /// Returns a pointer to the allocated buffer, or NULL if the -/// allocation failed. +/// allocation failed or `len` is zero. pub fn alloc( alloc_: ?*const CAllocator, len: usize, ) callconv(lib.calling_conv) ?[*]u8 { + // Zig's empty allocation pointer is not safe to expose to foreign runtimes. + if (len == 0) return null; const allocator = lib.alloc.default(alloc_); const buf = allocator.alloc(u8, len) catch return null; return buf.ptr; @@ -47,6 +49,7 @@ test "alloc with null allocator" { test "alloc zero length" { const ptr = alloc(&lib.alloc.test_allocator, 0); defer free(&lib.alloc.test_allocator, ptr, 0); + try testing.expectEqual(null, ptr); } test "free null pointer" { diff --git a/vendor/libghostty-vt/src/terminal/c/build_info.zig b/vendor/libghostty-vt/src/terminal/c/build_info.zig index 8e0cd4d6..a186163a 100644 --- a/vendor/libghostty-vt/src/terminal/c/build_info.zig +++ b/vendor/libghostty-vt/src/terminal/c/build_info.zig @@ -45,7 +45,7 @@ pub fn get( out: ?*anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(BuildInfo, @intFromEnum(data)) catch { + _ = std.enums.fromInt(BuildInfo, @intFromEnum(data)) orelse { log.warn("build_info invalid data value={d}", .{@intFromEnum(data)}); return .invalid_value; }; diff --git a/vendor/libghostty-vt/src/terminal/c/cell.zig b/vendor/libghostty-vt/src/terminal/c/cell.zig index b59567df..8444cfd9 100644 --- a/vendor/libghostty-vt/src/terminal/c/cell.zig +++ b/vendor/libghostty-vt/src/terminal/c/cell.zig @@ -10,6 +10,12 @@ const Result = @import("result.zig").Result; /// C: GhosttyCell pub const CCell = u64; +/// C: GhosttyCellsView +pub const CellsView = extern struct { + ptr: ?[*]const CCell, + len: usize, +}; + /// C: GhosttyCellContentTag pub const ContentTag = enum(c_int) { codepoint = 0, @@ -105,7 +111,7 @@ pub fn get( out: ?*anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(CellData, @intFromEnum(data)) catch { + _ = std.enums.fromInt(CellData, @intFromEnum(data)) orelse { return .invalid_value; }; } @@ -158,7 +164,7 @@ fn getTyped( .has_hyperlink => out.* = cell.hyperlink, .protected => out.* = cell.protected, .semantic_content => out.* = @enumFromInt(@intFromEnum(cell.semantic_content)), - .color_palette => out.* = cell.content.color_palette, + .color_palette => out.* = cell.content.color_palette.data, .color_rgb => { const rgb = cell.content.color_rgb; out.* = .{ .r = rgb.r, .g = rgb.g, .b = rgb.b }; diff --git a/vendor/libghostty-vt/src/terminal/c/formatter.zig b/vendor/libghostty-vt/src/terminal/c/formatter.zig index 5b4504c8..4ab41cd4 100644 --- a/vendor/libghostty-vt/src/terminal/c/formatter.zig +++ b/vendor/libghostty-vt/src/terminal/c/formatter.zig @@ -2,6 +2,7 @@ const std = @import("std"); const testing = std.testing; const lib = @import("../lib.zig"); const CAllocator = lib.alloc.Allocator; +const io_c = @import("io.zig"); const terminal_c = @import("terminal.zig"); const grid_ref = @import("grid_ref.zig"); const selection_c = @import("selection.zig"); @@ -160,6 +161,32 @@ fn terminal_new_( return ptr; } +/// Format directly to a synchronous C writer callback. +pub fn format( + formatter_: Formatter, + writer: io_c.Writer, +) callconv(lib.calling_conv) Result { + const wrapper = formatter_ orelse return .invalid_value; + if (!writer.valid()) return .invalid_value; + + // Batch the formatter's many small writes into few callback calls. The + // trailing flush upholds the API contract that success means the + // callback has accepted every byte. + var buffer: [io_c.WriterAdapter.recommended_buffer_len]u8 = undefined; + var adapter: io_c.WriterAdapter = .initBuffered(writer, &buffer); + format_: { + switch (wrapper.kind) { + .terminal => |*t| t.format(adapter.writer()) catch break :format_, + } + adapter.writer().flush() catch break :format_; + return .success; + } + + if (adapter.invalid_write) return .limit_exceeded; + if (adapter.callback_failed) return .io_error; + return .io_error; +} + pub fn format_buf( formatter_: Formatter, out_: ?[*]u8, @@ -207,7 +234,9 @@ pub fn format_alloc( } const buf = aw.toOwnedSlice() catch return .out_of_memory; - out_ptr.* = buf.ptr; + // Empty Zig slices may contain sentinel pointers that foreign runtimes + // reject even when the length is zero (for example Go's stack scanner). + out_ptr.* = if (buf.len == 0) null else buf.ptr; out_len.* = buf.len; return .success; } @@ -223,7 +252,8 @@ test "terminal_new/free" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -253,12 +283,54 @@ test "free null" { free(null); } +test "format_alloc empty output" { + const failing: CAllocator = .fromZig(&std.mem.Allocator.failing); + var t: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &t, + 100, + 40, + )); + defer terminal_c.free(t); + + // Match a zero-initialized C options struct, including trim = false. + const opts: TerminalOptions = std.mem.zeroInit(TerminalOptions, .{}); + var f: Formatter = null; + try testing.expectEqual(Result.success, terminal_new( + &lib.alloc.test_allocator, + &f, + t, + opts, + )); + defer free(f); + + for ([_]?*const CAllocator{ null, &failing }) |allocator| { + var sentinel: u8 = 0; + var ptr: ?[*]u8 = @ptrCast(&sentinel); + var len: usize = 123; + try testing.expectEqual(Result.success, format_alloc(f, allocator, &ptr, &len)); + defer @import("allocator.zig").free(allocator, ptr, len); + try testing.expectEqual(@as(usize, 0), len); + try testing.expectEqual(null, ptr); + } + + // Reusing the same formatter must still allocate and return real output. + terminal_c.vt_write(t, "hello", 5); + var ptr: ?[*]u8 = null; + var len: usize = 0; + try testing.expectEqual(Result.success, format_alloc(f, null, &ptr, &len)); + defer @import("allocator.zig").free(null, ptr, len); + try testing.expectEqualStrings("hello", ptr.?[0..len]); +} + test "format plain" { var t: terminal_c.Terminal = null; try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -279,12 +351,103 @@ test "format plain" { try testing.expectEqualStrings("Hello", buf[0..written]); } +test "format streams to writer" { + var t: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer terminal_c.free(t); + + terminal_c.vt_write(t, "Hello\r\nWorld", 12); + + var f: Formatter = null; + try testing.expectEqual(Result.success, terminal_new( + &lib.alloc.test_allocator, + &f, + t, + .{ .emit = .plain, .unwrap = false, .trim = true, .extra = .{ .palette = false, .modes = false, .scrolling_region = false, .tabstops = false, .pwd = false, .keyboard = false, .screen = .{ .cursor = false, .style = false, .hyperlink = false, .protection = false, .kitty_keyboard = false, .charsets = false } } }, + )); + defer free(f); + + const Context = struct { + destination: []u8, + offset: usize = 0, + calls: usize = 0, + + fn write( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + if (len > self.destination.len - self.offset) return false; + @memcpy(self.destination[self.offset..][0..len], data[0..len]); + self.offset += len; + self.calls += 1; + return true; + } + }; + + var buf: [1024]u8 = undefined; + var context: Context = .{ .destination = &buf }; + try testing.expectEqual(Result.success, format(f, .{ + .write = &Context.write, + .userdata = &context, + })); + try testing.expect(context.calls > 0); + try testing.expectEqualStrings("Hello\nWorld", buf[0..context.offset]); +} + +test "format writer errors" { + const FailWriter = struct { + fn write( + _: ?*anyopaque, + _: [*]const u8, + _: usize, + ) callconv(lib.calling_conv) bool { + return false; + } + }; + + try testing.expectEqual(Result.invalid_value, format(null, .{ + .write = &FailWriter.write, + })); + + var t: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer terminal_c.free(t); + terminal_c.vt_write(t, "Hello", 5); + + var f: Formatter = null; + try testing.expectEqual(Result.success, terminal_new( + &lib.alloc.test_allocator, + &f, + t, + .{ .emit = .plain, .unwrap = false, .trim = true, .extra = .{ .palette = false, .modes = false, .scrolling_region = false, .tabstops = false, .pwd = false, .keyboard = false, .screen = .{ .cursor = false, .style = false, .hyperlink = false, .protection = false, .kitty_keyboard = false, .charsets = false } } }, + )); + defer free(f); + + try testing.expectEqual(Result.invalid_value, format(f, .{})); + try testing.expectEqual(Result.io_error, format(f, .{ + .write = &FailWriter.write, + })); +} + test "format reflects terminal changes" { var t: terminal_c.Terminal = null; try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -316,7 +479,8 @@ test "format null returns required size" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -348,7 +512,8 @@ test "format buffer too small" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -381,7 +546,8 @@ test "format vt" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -408,7 +574,8 @@ test "format plain with selection" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -452,7 +619,8 @@ test "format html" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); diff --git a/vendor/libghostty-vt/src/terminal/c/grid_ref.zig b/vendor/libghostty-vt/src/terminal/c/grid_ref.zig index 946ca8a1..20f49be9 100644 --- a/vendor/libghostty-vt/src/terminal/c/grid_ref.zig +++ b/vendor/libghostty-vt/src/terminal/c/grid_ref.zig @@ -205,7 +205,8 @@ test "grid_ref_hyperlink_uri no hyperlink" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -228,7 +229,8 @@ test "grid_ref_hyperlink_uri with hyperlink" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(terminal); diff --git a/vendor/libghostty-vt/src/terminal/c/grid_ref_tracked.zig b/vendor/libghostty-vt/src/terminal/c/grid_ref_tracked.zig index 60091715..475a8e43 100644 --- a/vendor/libghostty-vt/src/terminal/c/grid_ref_tracked.zig +++ b/vendor/libghostty-vt/src/terminal/c/grid_ref_tracked.zig @@ -96,7 +96,8 @@ test "tracked_grid_ref snapshots after terminal scroll" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -128,7 +129,8 @@ test "tracked_grid_ref reports no value after reset" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -154,7 +156,8 @@ test "tracked_grid_ref reports no value after alternate screen reset" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -194,7 +197,8 @@ test "tracked_grid_ref reports no value after terminal free" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); terminal_c.vt_write(terminal, "A", 1); diff --git a/vendor/libghostty-vt/src/terminal/c/io.zig b/vendor/libghostty-vt/src/terminal/c/io.zig new file mode 100644 index 00000000..76e7451d --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/c/io.zig @@ -0,0 +1,774 @@ +//! C byte-stream callbacks and adapters for `std.Io`. +//! +//! The public callbacks deliberately expose a smaller contract than Zig's I/O +//! interfaces: reads are synchronous progress/EOF/error, and writes accept an +//! entire slice or fail. These adapters translate that contract while keeping +//! enough state to map failures back to distinct C result codes. + +const std = @import("std"); +const assert = std.debug.assert; +const lib = @import("../lib.zig"); + +/// C: GhosttyReaderFn +pub const ReaderFn = *const fn ( + userdata: ?*anyopaque, + buffer: [*]u8, + capacity: usize, + out_read: *usize, +) callconv(lib.calling_conv) bool; + +/// C: GhosttyWriterFn +pub const WriterFn = *const fn ( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, +) callconv(lib.calling_conv) bool; + +/// C: GhosttyReader +pub const Reader = extern struct { + read: ?ReaderFn = null, + userdata: ?*anyopaque = null, + + pub fn valid(self: Reader) bool { + return self.read != null; + } +}; + +/// C: GhosttyWriter +pub const Writer = extern struct { + write: ?WriterFn = null, + userdata: ?*anyopaque = null, + + pub fn valid(self: Writer) bool { + return self.write != null; + } +}; + +/// C: GhosttyMimeReaderFn +pub const MimeReaderFn = *const fn ( + userdata: ?*anyopaque, + mime: lib.String, + writer: Writer, +) callconv(lib.calling_conv) bool; + +/// C: GhosttyMimeReader +pub const MimeReader = extern struct { + read: ?MimeReaderFn = null, + userdata: ?*anyopaque = null, + + pub fn valid(self: MimeReader) bool { + return self.read != null; + } +}; + +/// Adapts a `GhosttyReader` to `std.Io.Reader`. +/// +/// The adapter must have a stable address while `interface` is in use. Its +/// interface starts unbuffered so `init` can safely return it by value. A +/// one-byte buffer is attached lazily when an operation such as `peekByte` +/// requires buffering. This also prevents reads beyond a requested framing +/// boundary. +pub const ReaderAdapter = struct { + /// Copy of the C callback pair. The pointed-to userdata remains borrowed. + source: Reader, + + /// Zig-facing interface. Its vtable recovers this enclosing adapter with + /// `@fieldParentPtr`, which is why the adapter's address must remain stable. + interface: std.Io.Reader, + + /// Bytes successfully obtained from the callback. + offset: usize = 0, + + /// The callback returned false. This distinguishes an I/O failure from + /// the callback returning true with zero bytes at end-of-file. + callback_failed: bool = false, + + /// The callback was NULL, returned too many bytes, or overflowed offset. + invalid_read: bool = false, + + /// The callback returned true with zero bytes. EOF is permanent. + eof: bool = false, + + /// `std.Io.Reader` needs backing storage for `peekByte`. One byte is + /// intentional: it satisfies that operation without allowing the adapter + /// to pull a second byte across a snapshot checkpoint or FINISH boundary. + peek_buffer: [1]u8 = undefined, + + /// Scratch space used only when the destination writer has no writable + /// buffer of its own. 4 KiB amortizes callback overhead for streaming + /// operations while keeping every adapter's fixed allocation modest. The + /// caller's `limit` always truncates this slice, so the size cannot cause + /// framing read-ahead. + transfer_buffer: [4096]u8 = undefined, + + pub fn init(source: Reader) ReaderAdapter { + return .{ + .source = source, + .interface = .{ + .vtable = &.{ + .stream = stream, + .rebase = rebase, + }, + // `rebase` attaches peek_buffer after the adapter reaches its + // stable destination address. + .buffer = &.{}, + .seek = 0, + .end = 0, + }, + }; + } + + pub fn reader(self: *ReaderAdapter) *std.Io.Reader { + // Calling this only after the adapter reaches its final address makes + // the parent-pointer recovery in `stream` and `rebase` valid. + return &self.interface; + } + + /// Perform exactly one C callback invocation into caller-selected storage. + /// This is the single place that interprets the public callback contract. + fn readInto(self: *ReaderAdapter, destination: []u8) std.Io.Reader.Error!usize { + assert(destination.len > 0); + + // Failure and EOF are sticky. In particular, a callback that reports + // EOF is never polled again because the C API defines it as permanent. + if (self.callback_failed or self.invalid_read) return error.ReadFailed; + if (self.eof) return error.EndOfStream; + + // A missing callback is an invalid C value rather than external I/O + // failure, so record it separately for the public result mapper. + const read_fn = self.source.read orelse { + self.invalid_read = true; + return error.ReadFailed; + }; + + // Initialize this ourselves: a callback returning false is not + // required to initialize out_read. + var read_len: usize = 0; + if (!read_fn( + self.source.userdata, + destination.ptr, + destination.len, + &read_len, + )) { + self.callback_failed = true; + return error.ReadFailed; + } + + // Treat a callback that violates the capacity contract as an invalid + // reader without constructing an out-of-bounds slice. + if (read_len > destination.len) { + self.invalid_read = true; + return error.ReadFailed; + } + // Successful zero-length reads are the C representation of EOF. + if (read_len == 0) { + self.eof = true; + return error.EndOfStream; + } + + // Offset counts only bytes actually supplied by successful callbacks. + // Saturating or wrapping would make SOURCE_OFFSET untrustworthy. + self.offset = std.math.add(usize, self.offset, read_len) catch { + self.invalid_read = true; + return error.ReadFailed; + }; + return read_len; + } + + /// `stream` is the fundamental read primitive in `std.Io.Reader`'s + /// vtable. It moves at most `limit` bytes from the C source to the Zig + /// writer and reports the number transferred. Higher-level operations + /// such as `readSliceAll` and `peekByte` are implemented in terms of it. + fn stream( + reader_: *std.Io.Reader, + writer: *std.Io.Writer, + limit: std.Io.Limit, + ) std.Io.Reader.StreamError!usize { + // A zero limit is a successful no-op and must not invoke user code. + if (limit == .nothing) return 0; + + // The std.Io callback receives only the embedded interface pointer. + // Recover the adapter to reach the C callback and accounting flags. + const self: *ReaderAdapter = @alignCast(@fieldParentPtr( + "interface", + reader_, + )); + + // Use the destination's storage directly when possible. This is the + // normal path for readSlice operations and for filling peek_buffer. + const direct = limit.slice(writer.unusedCapacitySlice()); + if (direct.len > 0) { + const read_len = try self.readInto(direct); + // `readInto` initialized these bytes outside the Writer API, so + // explicitly publish them to the destination. + writer.advance(read_len); + return read_len; + } + + // An unbuffered Writer has no direct destination storage. Read into + // bounded adapter storage and then let its drain implementation take + // ownership of the complete slice. + const transfer = limit.slice(&self.transfer_buffer); + assert(transfer.len > 0); + const read_len = try self.readInto(transfer); + // writeAll either transfers this complete callback result or reports + // failure; returning a partial count would violate stream's contract. + try writer.writeAll(transfer[0..read_len]); + return read_len; + } + + /// Give buffered reader operations contiguous storage on first demand. + /// Initialization cannot point at `peek_buffer` because `init` returns the + /// adapter by value and its final address is not known until afterward. + fn rebase( + reader_: *std.Io.Reader, + capacity: usize, + ) std.Io.Reader.RebaseError!void { + const self: *ReaderAdapter = @alignCast(@fieldParentPtr( + "interface", + reader_, + )); + + if (reader_.buffer.len == 0) { + // No operation could have buffered bytes before storage existed. + assert(reader_.seek == 0); + assert(reader_.end == 0); + reader_.buffer = &self.peek_buffer; + } + + // Like other std.Io.Reader implementations, contiguous peek capacity + // is limited by the reader's declared buffer capacity. + assert(capacity <= reader_.buffer.len); + // Avoid the default memmove when the unread tail already has room. + if (reader_.buffer.len - reader_.seek >= capacity) return; + return std.Io.Reader.defaultRebase(reader_, capacity); + } +}; + +/// Adapts a `GhosttyWriter` to a `std.Io.Writer`. +/// +/// The adapter may be given a buffer via `initBuffered` so that the many +/// small writes typical of streaming producers (e.g. the formatter) are +/// batched into far fewer C callback invocations. A buffered adapter's owner +/// must call `flush` on the interface before reporting success so that the +/// public contract holds: success means the C callback has already accepted +/// every byte. +pub const WriterAdapter = struct { + /// Copy of the C callback pair. The pointed-to userdata remains borrowed. + destination: Writer, + + /// Zig-facing interface whose drain vtable points back to this adapter. + interface: std.Io.Writer, + + /// Bytes accepted by successful callback invocations. Buffered bytes are + /// counted only once a drain or flush hands them to the callback. + offset: usize = 0, + + /// The callback returned false. + callback_failed: bool = false, + + /// The callback was NULL or offset accounting overflowed. + invalid_write: bool = false, + + /// The buffer length used by the C API entry points that stream through + /// a callback. 4 KiB amortizes callback overhead while keeping every + /// adapter's stack footprint modest, mirroring ReaderAdapter's + /// transfer_buffer sizing. + pub const recommended_buffer_len = 4096; + + /// An unbuffered adapter: every Zig write reaches the callback + /// immediately and no flush is required before returning. + pub fn init(destination: Writer) WriterAdapter { + return initBuffered(destination, &.{}); + } + + /// A buffered adapter. The buffer must outlive the adapter and remain + /// at a stable address (it is referenced, not copied). The owner must + /// flush the interface before treating the operation as successful. + pub fn initBuffered(destination: Writer, buffer: []u8) WriterAdapter { + return .{ + .destination = destination, + .interface = .{ + .vtable = &.{ .drain = drain }, + .buffer = buffer, + .end = 0, + }, + }; + } + + pub fn writer(self: *WriterAdapter) *std.Io.Writer { + // As with ReaderAdapter, the returned interface borrows this stable + // enclosing address for parent-pointer recovery in the vtable. + return &self.interface; + } + + /// Submit one ordinary slice through the all-or-nothing C write callback. + fn writeAll(self: *WriterAdapter, data: []const u8) std.Io.Writer.Error!void { + // Empty writes carry no information and should not call foreign code. + if (data.len == 0) return; + + // Make failures sticky so a higher-level Zig retry cannot produce a + // misleading second callback after the output is already partial. + if (self.callback_failed or self.invalid_write) return error.WriteFailed; + + // Missing callbacks are invalid arguments; false callback returns are + // external I/O errors. Preserve that distinction for the C wrapper. + const write_fn = self.destination.write orelse { + self.invalid_write = true; + return error.WriteFailed; + }; + if (!write_fn(self.destination.userdata, data.ptr, data.len)) { + self.callback_failed = true; + return error.WriteFailed; + } + + // Count bytes only after the callback accepts the complete slice. + self.offset = std.math.add(usize, self.offset, data.len) catch { + self.invalid_write = true; + return error.WriteFailed; + }; + } + + /// Write one slice, batching through the interface buffer when one is + /// attached. Slices that fit are parked in the buffer (a later drain or + /// flush hands them to the callback); anything larger goes to the + /// callback directly after the buffer is emptied to preserve ordering. + fn writeSlice( + self: *WriterAdapter, + writer_: *std.Io.Writer, + slice: []const u8, + ) std.Io.Writer.Error!void { + const buffer = writer_.buffer; + if (slice.len <= buffer.len - writer_.end) { + @memcpy(buffer[writer_.end..][0..slice.len], slice); + writer_.end += slice.len; + return; + } + + if (writer_.end > 0) { + try self.writeAll(buffer[0..writer_.end]); + writer_.end = 0; + } + + if (slice.len <= buffer.len) { + @memcpy(buffer[0..slice.len], slice); + writer_.end = slice.len; + } else { + try self.writeAll(slice); + } + } + + /// Implement the sole primitive required by a std.Io.Writer. + /// + /// Zig represents a vector write as ordinary slices followed by the last + /// slice repeated `splat` times. The C callback has no vector form, so + /// this method expands that representation, batching through the + /// interface buffer when one is attached, and returns the total logical + /// byte count consumed from `data`. Bytes parked in the buffer count as + /// consumed; `std.Io.Writer.flush` drains them via this same method. + fn drain( + writer_: *std.Io.Writer, + data: []const []const u8, + splat: usize, + ) std.Io.Writer.Error!usize { + assert(data.len > 0); // The final element is always the splat pattern. + + // The vtable receives the embedded Writer rather than our adapter. + const self: *WriterAdapter = @alignCast(@fieldParentPtr( + "interface", + writer_, + )); + + // Buffered bytes must reach the destination before any of `data`. + if (writer_.end > 0) { + try self.writeAll(writer_.buffer[0..writer_.end]); + writer_.end = 0; + } + + var consumed: usize = 0; + // Every element except the last is written exactly once. + for (data[0 .. data.len - 1]) |slice| { + try self.writeSlice(writer_, slice); + consumed = std.math.add(usize, consumed, slice.len) catch { + self.invalid_write = true; + return error.WriteFailed; + }; + } + + // std.Io uses the final element as a compact repeated pattern. A + // splat of zero means the final element is not part of this write. + const pattern = data[data.len - 1]; + if (pattern.len == 1 and writer_.buffer.len > 0) { + // Single-byte splats (e.g. runs of blank cells) are memset + // into the buffer in bulk rather than repeated one write at + // a time. + const buffer = writer_.buffer; + var remaining = splat; + while (remaining > 0) { + if (writer_.end == buffer.len) { + try self.writeAll(buffer); + writer_.end = 0; + } + const n = @min(remaining, buffer.len - writer_.end); + @memset(buffer[writer_.end..][0..n], pattern[0]); + writer_.end += n; + remaining -= n; + } + consumed = std.math.add(usize, consumed, splat) catch { + self.invalid_write = true; + return error.WriteFailed; + }; + } else for (0..splat) |_| { + try self.writeSlice(writer_, pattern); + consumed = std.math.add(usize, consumed, pattern.len) catch { + self.invalid_write = true; + return error.WriteFailed; + }; + } + return consumed; + } +}; + +test "C reader and writer layouts keep callback first" { + try std.testing.expectEqual(@as(usize, 0), @offsetOf(Reader, "read")); + try std.testing.expectEqual(@sizeOf(?ReaderFn), @offsetOf(Reader, "userdata")); + try std.testing.expectEqual(@sizeOf(?ReaderFn) + @sizeOf(?*anyopaque), @sizeOf(Reader)); + + try std.testing.expectEqual(@as(usize, 0), @offsetOf(Writer, "write")); + try std.testing.expectEqual(@as(usize, 0), @offsetOf(MimeReader, "read")); + try std.testing.expectEqual(@sizeOf(?MimeReaderFn), @offsetOf(MimeReader, "userdata")); + try std.testing.expectEqual(@sizeOf(?WriterFn), @offsetOf(Writer, "userdata")); + try std.testing.expectEqual(@sizeOf(?WriterFn) + @sizeOf(?*anyopaque), @sizeOf(Writer)); +} + +test "ReaderAdapter supports short reads and permanent EOF" { + const Context = struct { + data: []const u8, + offset: usize = 0, + calls: usize = 0, + + fn read( + userdata: ?*anyopaque, + buffer: [*]u8, + capacity: usize, + out_read: *usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + self.calls += 1; + const remaining = self.data[self.offset..]; + const len = @min(remaining.len, capacity, 2); + @memcpy(buffer[0..len], remaining[0..len]); + self.offset += len; + out_read.* = len; + return true; + } + }; + + var context: Context = .{ .data = "abcdef" }; + var adapter: ReaderAdapter = .init(.{ + .read = &Context.read, + .userdata = &context, + }); + + var actual: [6]u8 = undefined; + try adapter.interface.readSliceAll(&actual); + try std.testing.expectEqualStrings("abcdef", &actual); + try std.testing.expectEqual(@as(usize, 6), adapter.offset); + try std.testing.expect(!adapter.eof); + + try std.testing.expectError(error.EndOfStream, adapter.interface.takeByte()); + try std.testing.expect(adapter.eof); + try std.testing.expect(!adapter.callback_failed); + + const calls_at_eof = context.calls; + try std.testing.expectError(error.EndOfStream, adapter.interface.takeByte()); + try std.testing.expectEqual(calls_at_eof, context.calls); +} + +test "ReaderAdapter peek reads only the requested byte" { + const Context = struct { + data: []const u8, + offset: usize = 0, + last_capacity: usize = 0, + + fn read( + userdata: ?*anyopaque, + buffer: [*]u8, + capacity: usize, + out_read: *usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + self.last_capacity = capacity; + const remaining = self.data[self.offset..]; + const len = @min(remaining.len, capacity); + @memcpy(buffer[0..len], remaining[0..len]); + self.offset += len; + out_read.* = len; + return true; + } + }; + + var context: Context = .{ .data = "ab" }; + var adapter: ReaderAdapter = .init(.{ + .read = &Context.read, + .userdata = &context, + }); + + try std.testing.expectEqual(@as(u8, 'a'), try adapter.interface.peekByte()); + try std.testing.expectEqual(@as(usize, 1), context.last_capacity); + try std.testing.expectEqual(@as(usize, 1), adapter.offset); + try std.testing.expectEqual(@as(u8, 'a'), try adapter.interface.takeByte()); + try std.testing.expectEqual(@as(usize, 1), adapter.offset); +} + +test "ReaderAdapter distinguishes callback failure and invalid length" { + const Failing = struct { + fn read( + _: ?*anyopaque, + _: [*]u8, + _: usize, + _: *usize, + ) callconv(lib.calling_conv) bool { + return false; + } + }; + var failing: ReaderAdapter = .init(.{ .read = &Failing.read }); + try std.testing.expectError(error.ReadFailed, failing.interface.takeByte()); + try std.testing.expect(failing.callback_failed); + try std.testing.expect(!failing.eof); + try std.testing.expect(!failing.invalid_read); + + const Oversized = struct { + fn read( + _: ?*anyopaque, + _: [*]u8, + capacity: usize, + out_read: *usize, + ) callconv(lib.calling_conv) bool { + out_read.* = capacity + 1; + return true; + } + }; + var oversized: ReaderAdapter = .init(.{ .read = &Oversized.read }); + try std.testing.expectError(error.ReadFailed, oversized.interface.takeByte()); + try std.testing.expect(!oversized.callback_failed); + try std.testing.expect(!oversized.eof); + try std.testing.expect(oversized.invalid_read); +} + +test "ReaderAdapter rejects a null callback" { + var adapter: ReaderAdapter = .init(.{}); + try std.testing.expectError(error.ReadFailed, adapter.interface.takeByte()); + try std.testing.expect(adapter.invalid_read); + try std.testing.expect(!adapter.callback_failed); +} + +test "WriterAdapter writes vectors and splats" { + const Context = struct { + data: [32]u8 = undefined, + len: usize = 0, + + fn write( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + @memcpy(self.data[self.len..][0..len], data[0..len]); + self.len += len; + return true; + } + }; + + var context: Context = .{}; + var adapter: WriterAdapter = .init(.{ + .write = &Context.write, + .userdata = &context, + }); + + var vectors: [2][]const u8 = .{ "ab", "cd" }; + try adapter.interface.writeVecAll(&vectors); + var repeated: [1][]const u8 = .{"!"}; + try adapter.interface.writeSplatAll(&repeated, 2); + + try std.testing.expectEqualStrings("abcd!!", context.data[0..context.len]); + try std.testing.expectEqual(@as(usize, 6), adapter.offset); + try std.testing.expect(!adapter.callback_failed); +} + +test "WriterAdapter buffered batches small writes" { + const Context = struct { + data: [256]u8 = undefined, + len: usize = 0, + calls: usize = 0, + + fn write( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + @memcpy(self.data[self.len..][0..len], data[0..len]); + self.len += len; + self.calls += 1; + return true; + } + }; + + var context: Context = .{}; + var buffer: [64]u8 = undefined; + var adapter: WriterAdapter = .initBuffered(.{ + .write = &Context.write, + .userdata = &context, + }, &buffer); + + // Many small writes fit in the buffer: no callback yet. + for (0..16) |_| try adapter.interface.writeAll("ab"); + try std.testing.expectEqual(@as(usize, 0), context.calls); + + // Flush delivers everything in a single call. + try adapter.interface.flush(); + try std.testing.expectEqual(@as(usize, 1), context.calls); + try std.testing.expectEqualStrings( + "ab" ** 16, + context.data[0..context.len], + ); + try std.testing.expectEqual(@as(usize, 32), adapter.offset); +} + +test "WriterAdapter buffered splats and large writes" { + const Context = struct { + data: [256]u8 = undefined, + len: usize = 0, + calls: usize = 0, + + fn write( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + @memcpy(self.data[self.len..][0..len], data[0..len]); + self.len += len; + self.calls += 1; + return true; + } + }; + + var context: Context = .{}; + var buffer: [16]u8 = undefined; + var adapter: WriterAdapter = .initBuffered(.{ + .write = &Context.write, + .userdata = &context, + }, &buffer); + + // A splat larger than the buffer is chunked, not one call per byte. + try adapter.interface.splatByteAll(' ', 40); + // A write larger than the buffer flushes and goes to the callback + // directly. + try adapter.interface.writeAll("0123456789abcdef0"); // 17 > 16 + try adapter.interface.writeAll("xy"); + try adapter.interface.flush(); + + try std.testing.expectEqualStrings( + " " ** 40 ++ "0123456789abcdef0" ++ "xy", + context.data[0..context.len], + ); + try std.testing.expectEqual( + @as(usize, context.len), + adapter.offset, + ); + // Chunked delivery: far fewer calls than bytes. + try std.testing.expect(context.calls <= 6); +} + +test "WriterAdapter makes callback failure sticky" { + const Context = struct { + calls: usize = 0, + + fn write( + userdata: ?*anyopaque, + _: [*]const u8, + _: usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + self.calls += 1; + return false; + } + }; + + var context: Context = .{}; + var adapter: WriterAdapter = .init(.{ + .write = &Context.write, + .userdata = &context, + }); + try std.testing.expectError(error.WriteFailed, adapter.interface.writeAll("x")); + try std.testing.expect(adapter.callback_failed); + try std.testing.expectEqual(@as(usize, 0), adapter.offset); + + try std.testing.expectError(error.WriteFailed, adapter.interface.writeAll("y")); + try std.testing.expectEqual(@as(usize, 1), context.calls); +} + +test "WriterAdapter rejects a null callback" { + var adapter: WriterAdapter = .init(.{}); + try std.testing.expectError(error.WriteFailed, adapter.interface.writeAll("x")); + try std.testing.expect(adapter.invalid_write); + try std.testing.expect(!adapter.callback_failed); +} + +test "ReaderAdapter streams into an unbuffered WriterAdapter" { + const ReadContext = struct { + data: []const u8, + offset: usize = 0, + + fn read( + userdata: ?*anyopaque, + buffer: [*]u8, + capacity: usize, + out_read: *usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + const remaining = self.data[self.offset..]; + const len = @min(remaining.len, capacity); + @memcpy(buffer[0..len], remaining[0..len]); + self.offset += len; + out_read.* = len; + return true; + } + }; + const WriteContext = struct { + data: [8]u8 = undefined, + len: usize = 0, + + fn write( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + @memcpy(self.data[self.len..][0..len], data[0..len]); + self.len += len; + return true; + } + }; + + var read_context: ReadContext = .{ .data = "stream" }; + var reader: ReaderAdapter = .init(.{ + .read = &ReadContext.read, + .userdata = &read_context, + }); + var write_context: WriteContext = .{}; + var writer: WriterAdapter = .init(.{ + .write = &WriteContext.write, + .userdata = &write_context, + }); + + try reader.interface.streamExact(&writer.interface, "stream".len); + try std.testing.expectEqualStrings( + "stream", + write_context.data[0..write_context.len], + ); + try std.testing.expectEqual(@as(usize, "stream".len), reader.offset); + try std.testing.expectEqual(@as(usize, "stream".len), writer.offset); +} diff --git a/vendor/libghostty-vt/src/terminal/c/key_encode.zig b/vendor/libghostty-vt/src/terminal/c/key_encode.zig index f5d459f0..f0aec36e 100644 --- a/vendor/libghostty-vt/src/terminal/c/key_encode.zig +++ b/vendor/libghostty-vt/src/terminal/c/key_encode.zig @@ -80,7 +80,7 @@ pub fn setopt( value: ?*const anyopaque, ) callconv(lib.calling_conv) void { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Option, @intFromEnum(option)) catch { + _ = std.enums.fromInt(Option, @intFromEnum(option)) orelse { log.warn("setopt invalid option value={d}", .{@intFromEnum(option)}); return; }; @@ -113,7 +113,7 @@ fn setoptTyped( }, .macos_option_as_alt => { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(OptionAsAlt, @intFromEnum(value.*)) catch { + _ = std.enums.fromInt(OptionAsAlt, @intFromEnum(value.*)) orelse { log.warn("setopt invalid OptionAsAlt value={d}", .{@intFromEnum(value.*)}); return; }; @@ -257,7 +257,8 @@ test "setopt_from_terminal" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -283,7 +284,8 @@ test "setopt_from_terminal null" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); setopt_from_terminal(null, t); diff --git a/vendor/libghostty-vt/src/terminal/c/key_event.zig b/vendor/libghostty-vt/src/terminal/c/key_event.zig index 1feac9ac..b9deecc6 100644 --- a/vendor/libghostty-vt/src/terminal/c/key_event.zig +++ b/vendor/libghostty-vt/src/terminal/c/key_event.zig @@ -38,7 +38,7 @@ pub fn free(event_: Event) callconv(lib.calling_conv) void { pub fn set_action(event_: Event, action: key.Action) callconv(lib.calling_conv) void { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(key.Action, @intFromEnum(action)) catch { + _ = std.enums.fromInt(key.Action, @intFromEnum(action)) orelse { log.warn("set_action invalid action value={d}", .{@intFromEnum(action)}); return; }; @@ -55,7 +55,7 @@ pub fn get_action(event_: Event) callconv(lib.calling_conv) key.Action { pub fn set_key(event_: Event, k: key.Key) callconv(lib.calling_conv) void { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(key.Key, @intFromEnum(k)) catch { + _ = std.enums.fromInt(key.Key, @intFromEnum(k)) orelse { log.warn("set_key invalid key value={d}", .{@intFromEnum(k)}); return; }; diff --git a/vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig b/vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig index 13a675cc..2bd5e862 100644 --- a/vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig +++ b/vendor/libghostty-vt/src/terminal/c/kitty_graphics.zig @@ -10,6 +10,7 @@ const grid_ref = @import("grid_ref.zig"); const selection_c = @import("selection.zig"); const terminal_c = @import("terminal.zig"); const Terminal = @import("../Terminal.zig"); +const PageList = @import("../PageList.zig"); const Result = @import("result.zig").Result; /// C: GhosttyKittyGraphics @@ -261,8 +262,11 @@ fn imageGetTyped( .height => out.* = image.height, .format => out.* = image.format, .compression => out.* = image.compression, - .data_ptr => out.* = image.data.ptr, - .data_len => out.* = image.data.len, + // For animated images this is the current animation frame's + // data; the image generation changes whenever the current + // frame does, so generation-keyed caches stay coherent. + .data_ptr => out.* = (image.renderData().bytes() orelse return .no_value).ptr, + .data_len => out.* = image.renderData().len(), .generation => out.* = image.generation, } @@ -301,7 +305,7 @@ pub fn placement_iterator_set( if (comptime !build_options.kitty_graphics) return .no_value; if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(PlacementIteratorOption, @intFromEnum(option)) catch { + _ = std.enums.fromInt(PlacementIteratorOption, @intFromEnum(option)) orelse { return .invalid_value; }; } @@ -516,16 +520,12 @@ pub fn placement_source_rect( const entry = iter.entry orelse return .invalid_value; const p = entry.value_ptr; - // Apply "0 = full image dimension" convention, then clamp to image bounds. - const x = @min(p.source_x, image.width); - const y = @min(p.source_y, image.height); - const w = @min(if (p.source_width > 0) p.source_width else image.width, image.width - x); - const h = @min(if (p.source_height > 0) p.source_height else image.height, image.height - y); + const source = p.sourceRect(image.*); - out_x.* = x; - out_y.* = y; - out_width.* = w; - out_height.* = h; + out_x.* = source.x; + out_y.* = source.y; + out_width.* = source.width; + out_height.* = source.height; return .success; } @@ -576,12 +576,11 @@ pub fn placement_render_info( out.viewport_row = vp.row; out.viewport_visible = vp.visible; - const x = @min(p.source_x, image.width); - const y = @min(p.source_y, image.height); - out.source_x = x; - out.source_y = y; - out.source_width = @min(if (p.source_width > 0) p.source_width else image.width, image.width - x); - out.source_height = @min(if (p.source_height > 0) p.source_height else image.height, image.height - y); + const source = p.sourceRect(image.*); + out.source_x = source.x; + out.source_y = source.y; + out.source_width = source.width; + out.source_height = source.height; return .success; } @@ -595,9 +594,9 @@ pub fn placement_render_info( /// the placement's origin has scrolled above the top of the viewport. /// /// A placement is considered not visible if it is a virtual (unicode -/// placeholder) placement, or if it is fully off-screen (its bottom -/// edge is above the viewport or its top edge is at or below the -/// viewport's last row). +/// placeholder) placement, its tracked content has been pruned, or it is +/// fully off-screen (its bottom edge is above the viewport or its top edge is +/// at or below the viewport's last row). fn computeViewportPos( p: *const kitty_storage.ImageStorage.Placement, image: *const Image, @@ -605,11 +604,37 @@ fn computeViewportPos( ) struct { col: i32, row: i32, visible: bool } { // Virtual placements use unicode placeholders and don't have a // screen position — they are rendered inline by the text layout. - const pin = switch (p.location) { - .pin => |pin| pin, + // Relative placements are anchored at the root of their parent + // chain, offset by the accumulated chain offsets. A chain rooted + // at a virtual placement has no resolvable position here: its + // origin is the parent's placeholder cells, which only a renderer + // scanning the screen can locate. + const origin: struct { + pin: *const PageList.Pin, + col_offset: i32 = 0, + row_offset: i32 = 0, + } = switch (p.location) { + .pin => |pin| .{ .pin = pin }, .virtual => return .{ .col = 0, .row = 0, .visible = false }, + .relative => |rel| origin: { + const storage = &t.screens.active.kitty_images; + const chain = storage.resolveChain(rel) orelse + return .{ .col = 0, .row = 0, .visible = false }; + switch (chain.root.location) { + .pin => |root_pin| break :origin .{ + .pin = root_pin, + .col_offset = chain.horizontal_offset, + .row_offset = chain.vertical_offset, + }, + .virtual => return .{ .col = 0, .row = 0, .visible = false }, + .relative => unreachable, // resolveChain roots are never relative + } + }, }; + const pin = origin.pin; + if (pin.garbage) return .{ .col = 0, .row = 0, .visible = false }; + // Convert both the placement's pin and the viewport's top-left // corner to screen-absolute coordinates so we can subtract them // to get viewport-relative coordinates. @@ -622,23 +647,30 @@ fn computeViewportPos( // Subtracting viewport origin from the pin gives us viewport- // relative coordinates. The row can be negative when the - // placement has partially scrolled above the viewport. - const vp_row: i32 = @as(i32, @intCast(pin_screen.screen.y)) - - @as(i32, @intCast(vp_screen.screen.y)); - const vp_col: i32 = @intCast(pin_screen.screen.x); - - // A placement is invisible if its bottom edge (row + height) - // is above the viewport, or its top edge is at or below the - // viewport's last row. + // placement has partially scrolled above the viewport, and both + // can be negative for relative placements with negative offsets. + const vp_row: i32 = (@as(i32, @intCast(pin_screen.screen.y)) - + @as(i32, @intCast(vp_screen.screen.y))) +| origin.row_offset; + const vp_col: i32 = @as(i32, @intCast(pin_screen.screen.x)) +| + origin.col_offset; + + // A placement is invisible if its bottom edge (row + height) is + // above the viewport, or its top edge is at or below the viewport's + // last row. The same applies horizontally: a pin's column is always + // in bounds, but a relative placement's offsets can push it fully + // off either side. const grid_size = p.gridSize(image.*, t); - const rows_i32: i32 = @intCast(grid_size.rows); - const term_rows: i32 = @intCast(t.rows); - const visible = vp_row + rows_i32 > 0 and vp_row < term_rows; + const bottom_row = @as(i64, vp_row) + @as(i64, grid_size.rows); + const right_col = @as(i64, vp_col) + @as(i64, grid_size.cols); + const visible = bottom_row > 0 and vp_row < @as(i32, t.rows) and + right_col > 0 and vp_col < @as(i32, t.cols); return .{ .col = vp_col, .row = vp_row, .visible = visible }; } test "placement_iterator new/free" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + var iter: PlacementIterator = null; try testing.expectEqual(Result.success, placement_iterator_new( &lib.alloc.test_allocator, @@ -659,7 +691,8 @@ test "placement_iterator next on empty storage" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -688,7 +721,8 @@ test "placement_iterator get before next returns invalid" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -719,7 +753,8 @@ test "placement_iterator with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -772,7 +807,8 @@ test "placement_iterator with multiple placements" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -829,7 +865,8 @@ test "placement_iterator_set layer filter" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -919,7 +956,8 @@ test "image_get_handle returns null for missing id" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -940,7 +978,8 @@ test "image_get_handle and image_get with transmitted image" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -983,6 +1022,57 @@ test "image_get_handle and image_get with transmitted image" { try testing.expect(data_len > 0); } +test "image_get exposes pending metadata without a data pointer" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + + var t: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer terminal_c.free(t); + + var graphics: KittyGraphics = undefined; + try testing.expectEqual(Result.success, terminal_c.get( + t, + .kitty_graphics, + @ptrCast(&graphics), + )); + + const alloc = lib.alloc.default(&lib.alloc.test_allocator); + const pending = try graphics.addPendingImage(testing.io, alloc, terminal_c.zigTerminal(t).?.screens.active, .{ + .id = 42, + .number = 7, + .width = 1, + .height = 4, + .format = .rgb, + .data = .{ .pending = 12 }, + }); + + const img = image_get_handle(graphics, 42); + try testing.expect(img != null); + + var number: u32 = 0; + try testing.expectEqual(Result.success, image_get(img, .number, @ptrCast(&number))); + try testing.expectEqual(@as(u32, 7), number); + + var data_len: usize = 0; + try testing.expectEqual(Result.success, image_get(img, .data_len, @ptrCast(&data_len))); + try testing.expectEqual(@as(usize, 12), data_len); + + var data_ptr: [*]const u8 = undefined; + try testing.expectEqual(Result.no_value, image_get(img, .data_ptr, @ptrCast(&data_ptr))); + + const pixels = try alloc.dupe(u8, "*" ** 12); + try testing.expect(pending.complete(graphics, testing.io, pixels)); + const completed_img = image_get_handle(graphics, 42); + try testing.expect(completed_img != null); + try testing.expectEqual(Result.success, image_get(completed_img, .data_ptr, @ptrCast(&data_ptr))); + try testing.expectEqualSlices(u8, pixels, data_ptr[0..data_len]); +} + test "placement_rect with transmit and display" { if (comptime !build_options.kitty_graphics) return error.SkipZigTest; @@ -990,7 +1080,8 @@ test "placement_rect with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1051,7 +1142,8 @@ test "placement_pixel_size with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1106,7 +1198,8 @@ test "placement_grid_size with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1160,7 +1253,8 @@ test "placement_viewport_pos with transmit and display" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1205,7 +1299,8 @@ test "placement_viewport_pos fully off-screen above" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 100 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1244,7 +1339,8 @@ test "placement_viewport_pos top off-screen" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 100 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1287,7 +1383,8 @@ test "placement_viewport_pos bottom off-screen" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 0 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1327,7 +1424,8 @@ test "placement_viewport_pos top and bottom off-screen" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 100 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1378,7 +1476,8 @@ test "placement_source_rect defaults to full image" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1417,7 +1516,8 @@ test "placement_source_rect with explicit source rect" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1461,7 +1561,8 @@ test "placement_source_rect clamps to image bounds" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1496,6 +1597,15 @@ test "placement_source_rect clamps to image bounds" { try testing.expectEqual(3, y); try testing.expectEqual(1, w); try testing.expectEqual(1, h); + + var pixel_width: u32 = undefined; + var pixel_height: u32 = undefined; + try testing.expectEqual( + Result.success, + placement_pixel_size(iter, img, t, &pixel_width, &pixel_height), + ); + try testing.expectEqual(1, pixel_width); + try testing.expectEqual(1, pixel_height); } test "placement_source_rect null args return invalid_value" { @@ -1522,7 +1632,8 @@ test "placement_render_info returns all fields" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1552,6 +1663,53 @@ test "placement_render_info returns all fields" { try testing.expectEqual(0, ri.source_y); try testing.expectEqual(1, ri.source_width); try testing.expectEqual(2, ri.source_height); + + const entry = iter.?.entry.?; + const pin = switch (entry.value_ptr.location) { + .pin => |pin| pin, + .virtual, .relative => unreachable, + }; + pin.garbage = true; + + ri = .{}; + try testing.expectEqual(Result.success, placement_render_info(iter, img, t, &ri)); + try testing.expect(!ri.viewport_visible); + + var rect: selection_c.CSelection = undefined; + try testing.expectEqual(Result.no_value, placement_rect(iter, img, t, &rect)); +} + +test "placement_render_info handles maximum grid dimensions" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + + var t: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer terminal_c.free(t); + try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); + + const cmd = "\x1b_Ga=T,t=d,f=24,i=1,p=1,s=1,v=2,c=1,r=4294967295,C=1;////////\x1b\\"; + terminal_c.vt_write(t, cmd.ptr, cmd.len); + + var graphics: KittyGraphics = undefined; + try testing.expectEqual(Result.success, terminal_c.get(t, .kitty_graphics, @ptrCast(&graphics))); + const img = image_get_handle(graphics, 1); + try testing.expect(img != null); + + var iter: PlacementIterator = null; + try testing.expectEqual(Result.success, placement_iterator_new(&lib.alloc.test_allocator, &iter)); + defer placement_iterator_free(iter); + try testing.expectEqual(Result.success, get(graphics, .placement_iterator, @ptrCast(&iter))); + try testing.expect(placement_iterator_next(iter)); + + var ri: PlacementRenderInfo = .{}; + try testing.expectEqual(Result.success, placement_render_info(iter, img, t, &ri)); + try testing.expect(ri.viewport_visible); + try testing.expectEqual(std.math.maxInt(u32), ri.grid_rows); } test "placement_render_info off-screen sets viewport_visible false" { @@ -1561,7 +1719,8 @@ test "placement_render_info off-screen sets viewport_visible false" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 5, .max_scrollback = 100 }, + 80, + 5, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 5, 10, 20)); @@ -1610,7 +1769,8 @@ test "image_get_multi success" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1664,7 +1824,8 @@ test "placement_get_multi success" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); try testing.expectEqual(Result.success, terminal_c.resize(t, 80, 24, 10, 20)); @@ -1722,7 +1883,8 @@ test "storage generation via get" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1774,7 +1936,8 @@ test "image generation detects same-sized retransmission" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1835,7 +1998,8 @@ test "image generation via image_get_multi" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1871,7 +2035,8 @@ test "image compression and format always report decoded data" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -1917,7 +2082,8 @@ test "generation never recurs across resets and screen switches" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); diff --git a/vendor/libghostty-vt/src/terminal/c/main.zig b/vendor/libghostty-vt/src/terminal/c/main.zig index 37dc5768..45b4dc47 100644 --- a/vendor/libghostty-vt/src/terminal/c/main.zig +++ b/vendor/libghostty-vt/src/terminal/c/main.zig @@ -10,6 +10,7 @@ pub const focus = @import("focus.zig"); pub const formatter = @import("formatter.zig"); pub const grid_ref = @import("grid_ref.zig"); pub const grid_ref_tracked = @import("grid_ref_tracked.zig"); +pub const io = @import("io.zig"); pub const kitty_graphics = @import("kitty_graphics.zig"); pub const kitty_graphics_get = kitty_graphics.get; pub const kitty_graphics_image = kitty_graphics.image_get_handle; @@ -39,8 +40,10 @@ pub const mouse_event = @import("mouse_event.zig"); pub const mouse_encode = @import("mouse_encode.zig"); pub const paste = @import("paste.zig"); pub const row = @import("row.zig"); +pub const search = @import("search.zig"); pub const sgr = @import("sgr.zig"); pub const size_report = @import("size_report.zig"); +pub const snapshot = @import("snapshot.zig"); pub const style = @import("style.zig"); pub const sys = @import("sys.zig"); pub const terminal = @import("terminal.zig"); @@ -76,6 +79,7 @@ pub const focus_encode = focus.encode; pub const mode_report_encode = modes.report_encode; pub const formatter_terminal_new = formatter.terminal_new; +pub const formatter_format = formatter.format; pub const formatter_format_buf = formatter.format_buf; pub const formatter_format_alloc = formatter.format_alloc; pub const formatter_free = formatter.free; @@ -85,12 +89,13 @@ pub const render_state_free = render.free; pub const render_state_update = render.update; pub const render_state_begin_update = render.begin_update; pub const render_state_end_update = render.end_update; +pub const render_state_clean = render.clean; pub const render_state_get = render.get; pub const render_state_get_multi = render.get_multi; pub const render_state_set = render.set; -pub const render_state_colors_get = render.colors_get; pub const render_state_row_iterator_new = render.row_iterator_new; pub const render_state_row_iterator_next = render.row_iterator_next; +pub const render_state_row_iterator_next_dirty = render.row_iterator_next_dirty; pub const render_state_row_get = render.row_get; pub const render_state_row_get_multi = render.row_get_multi; pub const render_state_row_set = render.row_set; @@ -111,8 +116,6 @@ pub const sgr_unknown_full = sgr.unknown_full; pub const sgr_unknown_partial = sgr.unknown_partial; pub const sgr_attribute_tag = sgr.attribute_tag; pub const sgr_attribute_value = sgr.attribute_value; -pub const wasm_alloc_sgr_attribute = sgr.wasm_alloc_attribute; -pub const wasm_free_sgr_attribute = sgr.wasm_free_attribute; pub const key_event_new = key_event.new; pub const key_event_free = key_event.free; @@ -158,6 +161,7 @@ pub const mouse_encoder_encode = mouse_encode.encode; pub const paste_is_safe = paste.is_safe; pub const paste_encode = paste.encode; +pub const terminal_paste = paste.terminal_paste; pub const alloc_alloc = allocator.alloc; pub const alloc_free = allocator.free; @@ -182,13 +186,15 @@ pub const terminal_reset = terminal.reset; pub const terminal_resize = terminal.resize; pub const terminal_set = terminal.set; pub const terminal_vt_write = terminal.vt_write; +pub const terminal_vt_write_until_ground = terminal.vt_write_until_ground; pub const terminal_scroll_viewport = terminal.scroll_viewport; pub const terminal_compression_activity = terminal.compression_activity; pub const terminal_compress = terminal.compress; -pub const terminal_mode_get = terminal.mode_get; -pub const terminal_mode_set = terminal.mode_set; pub const terminal_get = terminal.get; pub const terminal_get_multi = terminal.get_multi; +pub const terminal_continuation_write = terminal.continuation_write; +pub const terminal_continuation_buf = terminal.continuation_buf; +pub const terminal_continuation_alloc = terminal.continuation_alloc; pub const terminal_select_word = selection.word; pub const terminal_select_word_between = selection.word_between; pub const terminal_select_line = selection.line; @@ -210,10 +216,31 @@ pub const selection_gesture_get_multi = selection_gesture.get_multi; pub const selection_gesture_event_new = selection_gesture.event_new; pub const selection_gesture_event_free = selection_gesture.event_free; pub const selection_gesture_event_set = selection_gesture.event_set; +pub const search_new = search.new; +pub const search_free = search.free; +pub const search_tick = search.tick; +pub const search_feed = search.feed; +pub const search_run = search.run; +pub const search_set = search.set; +pub const search_get = search.get; +pub const search_get_multi = search.get_multi; pub const terminal_grid_ref = terminal.grid_ref; pub const terminal_grid_ref_track = terminal.grid_ref_track; pub const terminal_point_from_grid_ref = terminal.point_from_grid_ref; +pub const snapshot_encode = snapshot.encode; +pub const snapshot_encode_buf = snapshot.encode_buf; +pub const snapshot_encode_alloc = snapshot.encode_alloc; +pub const snapshot_decoder_new = snapshot.decoder_new; +pub const snapshot_decoder_new_buf = snapshot.decoder_new_buf; +pub const snapshot_decoder_free = snapshot.decoder_free; +pub const snapshot_decoder_set = snapshot.decoder_set; +pub const snapshot_decoder_get = snapshot.decoder_get; +pub const snapshot_decoder_get_multi = snapshot.decoder_get_multi; +pub const snapshot_decoder_ready = snapshot.decoder_ready; +pub const snapshot_decoder_next = snapshot.decoder_next; +pub const snapshot_decoder_decode = snapshot.decoder_decode; + pub const type_json = types.get_json; pub const unicode_codepoint_width = unicode.codepoint_width; @@ -238,6 +265,7 @@ test { _ = color_scheme; _ = grid_ref; _ = grid_ref_tracked; + _ = io; _ = kitty_graphics; _ = row; _ = focus; @@ -252,8 +280,10 @@ test { _ = mouse_event; _ = mouse_encode; _ = paste; + _ = search; _ = sgr; _ = size_report; + _ = snapshot; _ = style; _ = sys; _ = terminal; diff --git a/vendor/libghostty-vt/src/terminal/c/modes.zig b/vendor/libghostty-vt/src/terminal/c/modes.zig index 449ce63e..b249b2a5 100644 --- a/vendor/libghostty-vt/src/terminal/c/modes.zig +++ b/vendor/libghostty-vt/src/terminal/c/modes.zig @@ -8,10 +8,10 @@ pub const ReportState = enum(c_int) { _, fn toZig(self: ReportState) ?modes.Report.State { - return std.meta.intToEnum( + return std.enums.fromInt( modes.Report.State, @intFromEnum(self), - ) catch null; + ); } }; diff --git a/vendor/libghostty-vt/src/terminal/c/mouse_encode.zig b/vendor/libghostty-vt/src/terminal/c/mouse_encode.zig index 331dd606..2586a57a 100644 --- a/vendor/libghostty-vt/src/terminal/c/mouse_encode.zig +++ b/vendor/libghostty-vt/src/terminal/c/mouse_encode.zig @@ -113,7 +113,7 @@ pub fn setopt( value: ?*const anyopaque, ) callconv(lib.calling_conv) void { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Option, @intFromEnum(option)) catch { + _ = std.enums.fromInt(Option, @intFromEnum(option)) orelse { log.warn("setopt invalid option value={d}", .{@intFromEnum(option)}); return; }; @@ -137,7 +137,7 @@ fn setoptTyped( switch (option) { .event => { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(TrackingMode, @intFromEnum(value.*)) catch { + _ = std.enums.fromInt(TrackingMode, @intFromEnum(value.*)) orelse { log.warn("setopt invalid TrackingMode value={d}", .{@intFromEnum(value.*)}); return; }; @@ -149,7 +149,7 @@ fn setoptTyped( .format => { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Format, @intFromEnum(value.*)) catch { + _ = std.enums.fromInt(Format, @intFromEnum(value.*)) orelse { log.warn("setopt invalid Format value={d}", .{@intFromEnum(value.*)}); return; }; @@ -324,7 +324,8 @@ test "setopt_from_terminal" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); @@ -346,7 +347,8 @@ test "setopt_from_terminal null" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer terminal_c.free(t); setopt_from_terminal(null, t); diff --git a/vendor/libghostty-vt/src/terminal/c/mouse_event.zig b/vendor/libghostty-vt/src/terminal/c/mouse_event.zig index b3c19dd3..b97b4661 100644 --- a/vendor/libghostty-vt/src/terminal/c/mouse_event.zig +++ b/vendor/libghostty-vt/src/terminal/c/mouse_event.zig @@ -51,7 +51,7 @@ pub fn free(event_: Event) callconv(lib.calling_conv) void { pub fn set_action(event_: Event, action: Action) callconv(lib.calling_conv) void { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Action, @intFromEnum(action)) catch { + _ = std.enums.fromInt(Action, @intFromEnum(action)) orelse { log.warn("set_action invalid action value={d}", .{@intFromEnum(action)}); return; }; @@ -66,7 +66,7 @@ pub fn get_action(event_: Event) callconv(lib.calling_conv) Action { pub fn set_button(event_: Event, button: Button) callconv(lib.calling_conv) void { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Button, @intFromEnum(button)) catch { + _ = std.enums.fromInt(Button, @intFromEnum(button)) orelse { log.warn("set_button invalid button value={d}", .{@intFromEnum(button)}); return; }; diff --git a/vendor/libghostty-vt/src/terminal/c/osc.zig b/vendor/libghostty-vt/src/terminal/c/osc.zig index 7ba32300..2ecc9c97 100644 --- a/vendor/libghostty-vt/src/terminal/c/osc.zig +++ b/vendor/libghostty-vt/src/terminal/c/osc.zig @@ -12,6 +12,9 @@ pub const Parser = ?*osc.Parser; /// C: GhosttyOscCommand pub const Command = ?*osc.Command; +/// C: GhosttyOscCommandType +pub const CommandType = osc.Command.Key; + pub fn new( alloc_: ?*const CAllocator, result: *Parser, @@ -44,7 +47,7 @@ pub fn end(parser_: Parser, terminator: u8) callconv(lib.calling_conv) Command { return parser_.?.end(terminator); } -pub fn commandType(command_: Command) callconv(lib.calling_conv) osc.Command.Key { +pub fn commandType(command_: Command) callconv(lib.calling_conv) CommandType { const command = command_ orelse return .invalid; return command.*; } @@ -69,7 +72,7 @@ pub fn commandData( out: ?*anyopaque, ) callconv(lib.calling_conv) bool { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(CommandData, @intFromEnum(data)) catch { + _ = std.enums.fromInt(CommandData, @intFromEnum(data)) orelse { log.warn("commandData invalid data value={d}", .{@intFromEnum(data)}); return false; }; diff --git a/vendor/libghostty-vt/src/terminal/c/paste.zig b/vendor/libghostty-vt/src/terminal/c/paste.zig index bce6a565..8dfa649c 100644 --- a/vendor/libghostty-vt/src/terminal/c/paste.zig +++ b/vendor/libghostty-vt/src/terminal/c/paste.zig @@ -1,8 +1,130 @@ const std = @import("std"); const lib = @import("../lib.zig"); const paste = @import("../../input/paste.zig"); +const terminal_paste_pkg = @import("../paste.zig"); +const clipboard = @import("../clipboard.zig"); +const io = @import("io.zig"); +const terminal_c = @import("terminal.zig"); +const Terminal = terminal_c.Terminal; +const ClipboardContent = terminal_c.ClipboardContent; +const ClipboardRead = terminal_c.ClipboardRead; +const ClipboardReadReply = terminal_c.ClipboardReadReply; const Result = @import("result.zig").Result; +/// Why a paste happened. The flat C form of the Zig tagged union +/// (terminal.paste.Source); the location rides alongside in +/// GhosttyPaste and only applies to a clipboard paste. +/// +/// C: GhosttyPasteSource +pub const Source = lib.Enum(lib.target, &.{ + "clipboard", + "text", +}); + +/// A paste of clipboard contents into the terminal. Sized struct. +/// +/// C: GhosttyPaste +pub const Request = extern struct { + size: usize = @sizeOf(Request), + location: clipboard.Location, + source: Source, + mimes: ?[*]const lib.String, + mimes_len: usize, + reader: io.MimeReader, + allow_unsafe: bool, +}; + +pub fn terminal_paste( + terminal_: Terminal, + req_: ?*const Request, + out_written: ?*bool, +) callconv(lib.calling_conv) Result { + const wrapper = terminal_ orelse return .invalid_value; + const req = req_ orelse return .invalid_value; + + // Every field is required; a smaller size is a caller from a + // different ABI version than any this struct has had. + if (req.size < @sizeOf(Request)) return .invalid_value; + + // The handler always has a write_pty trampoline that no-ops without + // a C callback, so the "nothing can be written" check is ours. + if (wrapper.effects.write_pty == null) return .invalid_value; + + const c_mimes: []const lib.String = if (req.mimes) |ptr| + ptr[0..req.mimes_len] + else + &.{}; + + // Only a paste with something to read needs a reader. + if (c_mimes.len > 0 and !req.reader.valid()) return .invalid_value; + + // A paste carries a handful of representations, so keep the common + // case allocation-free. + var sfa = std.heap.stackFallback(256, wrapper.terminal.gpa()); + const alloc = sfa.get(); + const mimes = alloc.alloc([]const u8, c_mimes.len) catch return .out_of_memory; + defer alloc.free(mimes); + for (mimes, c_mimes) |*mime, c_mime| mime.* = c_mime.ptr[0..c_mime.len]; + + const written = wrapper.stream.handler.paste(.{ + .source = switch (req.source) { + .clipboard => .{ .clipboard = req.location }, + .text => .text, + }, + .contents = .{ .reader = .{ + .mimes = mimes, + .read = .{ .ctx = @constCast(req), .read_fn = &readTrampoline }, + } }, + .allow_unsafe = req.allow_unsafe, + }) catch |err| return switch (err) { + error.UnsafePaste => .rejected, + error.NoWritePty => .invalid_value, + error.OutOfMemory => .out_of_memory, + error.ReadFailed, + error.EntropyUnavailable, + error.Canceled, + => .io_error, + }; + if (out_written) |ptr| ptr.* = written; + return .success; +} + +/// The sink handed to the C read callback: a GhosttyWriter over the +/// Zig sink, remembering whether the sink itself failed so that can be +/// told apart from the callback failing to read. +const Sink = struct { + writer: *std.Io.Writer, + write_failed: bool = false, + + fn write( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) bool { + const self: *Sink = @ptrCast(@alignCast(userdata.?)); + self.writer.writeAll(data[0..len]) catch { + self.write_failed = true; + return false; + }; + return true; + } +}; + +fn readTrampoline( + ctx: ?*anyopaque, + mime: []const u8, + writer: *std.Io.Writer, +) clipboard.MimeReader.Error!void { + const req: *const Request = @ptrCast(@alignCast(ctx.?)); + var sink: Sink = .{ .writer = writer }; + if (!req.reader.read.?(req.reader.userdata, .init(mime), .{ + .write = &Sink.write, + .userdata = &sink, + })) { + return if (sink.write_failed) error.WriteFailed else error.ReadFailed; + } +} + pub fn is_safe(data: ?[*]const u8, len: usize) callconv(lib.calling_conv) bool { const slice: []const u8 = if (data) |v| v[0..len] else &.{}; return paste.isSafe(slice); @@ -127,3 +249,273 @@ test "is_safe with null empty data" { const testing = std.testing; try testing.expect(is_safe(null, 0)); } + +/// Capture state for the terminal_paste tests: every pty write and the +/// clipboard reads that follow a paste event. +const TerminalPasteCapture = struct { + var written: [1024]u8 = undefined; + var written_len: usize = 0; + var write_count: usize = 0; + var read_count: usize = 0; + var last_read_granted: bool = false; + + fn reset() void { + written_len = 0; + write_count = 0; + read_count = 0; + last_read_granted = false; + } + + fn writePty(_: Terminal, _: ?*anyopaque, ptr: [*]const u8, len: usize) callconv(lib.calling_conv) void { + @memcpy(written[written_len..][0..len], ptr[0..len]); + written_len += len; + write_count += 1; + } + + fn clipboardRead(_: Terminal, _: ?*anyopaque, request: *const ClipboardRead) callconv(lib.calling_conv) void { + read_count += 1; + last_read_granted = request.granted; + const contents = [_]ClipboardContent{.{ + .mime = .init(@as([]const u8, "text/plain")), + .data = .init(@as([]const u8, "Ghostty")), + }}; + request.reply(request, &.{ + .size = @sizeOf(ClipboardReadReply), + .result = .success, + .contents = &contents, + .contents_len = contents.len, + .available = null, + .available_len = 0, + .remember = false, + }); + } + + fn writtenSlice() []const u8 { + return written[0..written_len]; + } + + /// The representations a test paste serves: the MIME list for the + /// request and the data the read callback streams, in pieces. + const Contents = struct { + mimes: [2]lib.String = undefined, + data: [2][]const u8 = undefined, + len: usize = 0, + reads: [2]usize = @splat(0), + fail: bool = false, + + fn init(entries: []const struct { []const u8, []const u8 }) Contents { + var self: Contents = .{}; + for (entries) |entry| { + self.mimes[self.len] = .init(entry[0]); + self.data[self.len] = entry[1]; + self.len += 1; + } + return self; + } + + fn request(self: *Contents) Request { + return .{ + .location = .standard, + .source = .clipboard, + .mimes = &self.mimes, + .mimes_len = self.len, + .reader = .{ .read = &read, .userdata = self }, + .allow_unsafe = false, + }; + } + + fn read(userdata: ?*anyopaque, mime: lib.String, writer: io.Writer) callconv(lib.calling_conv) bool { + const self: *Contents = @ptrCast(@alignCast(userdata.?)); + // The mime is the exact string from the request's list, so + // identifying it by pointer works. + const index: usize = for (self.mimes[0..self.len], 0..) |m, i| { + if (m.ptr == mime.ptr and m.len == mime.len) break i; + } else return false; + self.reads[index] += 1; + if (self.fail) return false; + const data = self.data[index]; + var offset: usize = 0; + while (offset < data.len) { + const n = @min(3, data.len - offset); + if (!writer.write.?(writer.userdata, data[offset..].ptr, n)) return false; + offset += n; + } + return true; + } + }; +}; + +test "terminal_paste null handling" { + const testing = std.testing; + const S = TerminalPasteCapture; + + var t: Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new(&lib.alloc.test_allocator, &t, 80, 24)); + defer terminal_c.free(t); + try testing.expectEqual(Result.success, terminal_c.set(t, .write_pty, @ptrCast(&S.writePty))); + + var written: bool = true; + var contents: S.Contents = .init(&.{.{ "text/plain", "hello" }}); + const req = contents.request(); + try testing.expectEqual(Result.invalid_value, terminal_paste(null, &req, &written)); + try testing.expectEqual(Result.invalid_value, terminal_paste(t, null, &written)); + try testing.expect(written); + + // A size smaller than the struct is rejected. + var small = req; + small.size = @sizeOf(usize); + try testing.expectEqual(Result.invalid_value, terminal_paste(t, &small, &written)); + + // MIME types without a reader are rejected; none at all is fine. + var unreadable = req; + unreadable.reader.read = null; + try testing.expectEqual(Result.invalid_value, terminal_paste(t, &unreadable, &written)); + unreadable.mimes = null; + unreadable.mimes_len = 0; + try testing.expectEqual(Result.success, terminal_paste(t, &unreadable, &written)); + try testing.expect(!written); +} + +test "terminal_paste without write_pty is invalid" { + const testing = std.testing; + const S = TerminalPasteCapture; + S.reset(); + + var t: Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new(&lib.alloc.test_allocator, &t, 80, 24)); + defer terminal_c.free(t); + + var contents: S.Contents = .init(&.{.{ "text/plain", "hello" }}); + const req = contents.request(); + try testing.expectEqual(Result.invalid_value, terminal_paste(t, &req, null)); +} + +test "terminal_paste text and unsafe" { + const testing = std.testing; + const S = TerminalPasteCapture; + S.reset(); + + var t: Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new(&lib.alloc.test_allocator, &t, 80, 24)); + defer terminal_c.free(t); + try testing.expectEqual(Result.success, terminal_c.set(t, .write_pty, @ptrCast(&S.writePty))); + + // Plain text, NULL out_written pointer is fine. The text is read + // once, the image never. + var contents: S.Contents = .init(&.{ + .{ "image/png", "\x89PNG" }, + .{ "text/plain", "hel\x1blo" }, + }); + const req = contents.request(); + try testing.expectEqual(Result.success, terminal_paste(t, &req, null)); + try testing.expectEqualStrings("hel lo", S.writtenSlice()); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqual(@as(usize, 0), contents.reads[0]); + try testing.expectEqual(@as(usize, 1), contents.reads[1]); + + // Unsafe is refused with nothing written, then allowed. + S.reset(); + var written: bool = false; + var unsafe_contents: S.Contents = .init(&.{.{ "text/plain", "rm -rf /\n" }}); + var unsafe = unsafe_contents.request(); + try testing.expectEqual(Result.rejected, terminal_paste(t, &unsafe, &written)); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expect(!written); + try testing.expectEqual(@as(usize, 1), unsafe_contents.reads[0]); + + unsafe.allow_unsafe = true; + try testing.expectEqual(Result.success, terminal_paste(t, &unsafe, &written)); + try testing.expect(written); + try testing.expectEqualStrings("rm -rf /\r", S.writtenSlice()); + try testing.expectEqual(@as(usize, 2), unsafe_contents.reads[0]); + + // Bracketed paste mode frames the text through the real mode path. + S.reset(); + const decset = "\x1b[?2004h"; + terminal_c.vt_write(t, decset, decset.len); + try testing.expectEqual(Result.success, terminal_paste(t, &req, &written)); + try testing.expect(written); + try testing.expectEqualStrings("\x1b[200~hel lo\x1b[201~", S.writtenSlice()); + + // No text representation writes nothing and reads nothing. + S.reset(); + var image: S.Contents = .init(&.{.{ "image/png", "\x89PNG" }}); + const image_req = image.request(); + try testing.expectEqual(Result.success, terminal_paste(t, &image_req, &written)); + try testing.expect(!written); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqual(@as(usize, 0), image.reads[0]); + + // A failing reader is an I/O error. + S.reset(); + contents.fail = true; + try testing.expectEqual(Result.io_error, terminal_paste(t, &req, &written)); + try testing.expectEqual(@as(usize, 0), S.write_count); +} + +test "terminal_paste event" { + const testing = std.testing; + const S = TerminalPasteCapture; + S.reset(); + + var t: Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new(&lib.alloc.test_allocator, &t, 80, 24)); + defer terminal_c.free(t); + try testing.expectEqual(Result.success, terminal_c.set(t, .write_pty, @ptrCast(&S.writePty))); + t.?.terminal.modes.set(.kitty_paste_events, true); + + // Without a clipboard_read callback the paste stays text. + var written: bool = false; + var contents: S.Contents = .init(&.{ + .{ "text/plain", "secret" }, + .{ "image/png", "\x89PNG" }, + }); + var req = contents.request(); + req.location = .primary; + try testing.expectEqual(Result.success, terminal_paste(t, &req, &written)); + try testing.expect(written); + try testing.expectEqualStrings("secret", S.writtenSlice()); + + // With one, an event is sent listing every MIME type and no data + // is read, let alone written. + S.reset(); + contents.reads = @splat(0); + try testing.expectEqual(Result.success, terminal_c.set(t, .clipboard_read, @ptrCast(&S.clipboardRead))); + try testing.expectEqual(Result.success, terminal_paste(t, &req, &written)); + try testing.expect(written); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqual(@as(usize, 3), std.mem.count(u8, S.writtenSlice(), "\x1b]5522;")); + try testing.expect(std.mem.startsWith(u8, S.writtenSlice(), "\x1b]5522;type=read:status=OK:loc=primary:pw=")); + try testing.expect(std.mem.indexOf(u8, S.writtenSlice(), "secret") == null); + try testing.expect(std.mem.indexOf(u8, S.writtenSlice(), ";dGV4dC9wbGFpbiBpbWFnZS9wbmcK\x1b\\") != null); + try testing.expectEqual(@as(usize, 0), contents.reads[0]); + try testing.expectEqual(@as(usize, 0), contents.reads[1]); + + // The program's read with the event password is granted once. + const ok_prefix = "\x1b]5522;type=read:status=OK:loc=primary:pw="; + const pw_end = std.mem.indexOfPos(u8, S.writtenSlice(), ok_prefix.len, "\x1b\\").?; + var read_buf: [256]u8 = undefined; + const read = try std.fmt.bufPrint( + &read_buf, + "\x1b]5522;type=read:pw={s}:name=UGFzdGUgZXZlbnQ=;dGV4dC9wbGFpbg==\x1b\\", + .{S.writtenSlice()[ok_prefix.len..pw_end]}, + ); + S.reset(); + terminal_c.vt_write(t, read.ptr, read.len); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expect(S.last_read_granted); + try testing.expect(std.mem.indexOf(u8, S.writtenSlice(), ";R2hvc3R0eQ==\x1b\\") != null); + + S.reset(); + terminal_c.vt_write(t, read.ptr, read.len); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expect(!S.last_read_granted); + + // Text sources never become events. + S.reset(); + var ime = req; + ime.source = .text; + try testing.expectEqual(Result.success, terminal_paste(t, &ime, &written)); + try testing.expect(written); + try testing.expectEqualStrings("secret", S.writtenSlice()); +} diff --git a/vendor/libghostty-vt/src/terminal/c/render.zig b/vendor/libghostty-vt/src/terminal/c/render.zig index 6cf78ec5..c108456f 100644 --- a/vendor/libghostty-vt/src/terminal/c/render.zig +++ b/vendor/libghostty-vt/src/terminal/c/render.zig @@ -12,6 +12,7 @@ const terminal_c = @import("terminal.zig"); const ZigTerminal = @import("../Terminal.zig"); const renderpkg = @import("../render.zig"); const Result = @import("result.zig").Result; +const cell_c = @import("cell.zig"); const row = @import("row.zig"); const style_c = @import("style.zig"); @@ -22,11 +23,18 @@ const RenderStateWrapper = struct { state: renderpkg.RenderState = .empty, }; +/// The "before the first element" position for the iterator wrappers +/// below. Represented as a sentinel index rather than an optional because +/// optional codegens into something that is less efficient than this. +const position_none = std.math.maxInt(usize); + const RowIteratorWrapper = struct { alloc: std.mem.Allocator, - /// The current index (also y value) into the row list. - y: ?size.CellCountInt, + /// The current index (also y value) into the row list, or + /// `position_none` if iteration hasn't started. Always validate + /// against `raws.len` before use. + y: usize, /// These are the raw pointers into the render state data. raws: []const page.Row, @@ -34,6 +42,10 @@ const RowIteratorWrapper = struct { selection: []const ?[2]size.CellCountInt, dirty: []bool, + /// The global dirty state from the render state that populated this + /// iterator. This has the same borrowed lifetime as the row slices. + state_dirty: *const Dirty, + /// The color palette from the render state, needed to resolve /// palette-indexed background colors on cells. palette: *const colorpkg.Palette, @@ -41,7 +53,12 @@ const RowIteratorWrapper = struct { const RowCellsWrapper = struct { alloc: std.mem.Allocator, - x: ?size.CellCountInt, + + /// The current index (also x value) into the cell list, or + /// `position_none` if iteration hasn't started. Always validate + /// against `raws.len` before use. + x: usize, + raws: []const page.Cell, graphemes: []const []const u21, styles: []const Style, @@ -87,6 +104,19 @@ pub const CursorVisualStyle = enum(c_int) { } }; +/// C: GhosttyRenderStateCursor +pub const Cursor = extern struct { + size: usize = @sizeOf(Cursor), + viewport_has_value: bool, + viewport_x: u16, + viewport_y: u16, + wide_tail: bool, + visible: bool, + blinking: bool, + password_input: bool, + visual_style: CursorVisualStyle, +}; + /// C: GhosttyRenderStateData pub const Data = enum(c_int) { invalid = 0, @@ -107,6 +137,8 @@ pub const Data = enum(c_int) { cursor_viewport_x = 15, cursor_viewport_y = 16, cursor_viewport_wide_tail = 17, + cursor = 18, + colors = 19, /// Output type expected for querying the data of the given kind. pub fn OutType(comptime self: Data) type { @@ -122,6 +154,8 @@ pub const Data = enum(c_int) { .cursor_visible, .cursor_blinking, .cursor_password_input => bool, .cursor_viewport_has_value, .cursor_viewport_wide_tail => bool, .cursor_viewport_x, .cursor_viewport_y => size.CellCountInt, + .cursor => Cursor, + .colors => Colors, }; } }; @@ -207,26 +241,21 @@ pub fn end_update( return .success; } +pub fn clean( + state_: RenderState, +) callconv(lib.calling_conv) Result { + const state = state_ orelse return .invalid_value; + state.state.clean(); + return .success; +} + pub fn get( state_: RenderState, data: Data, out: ?*anyopaque, ) callconv(lib.calling_conv) Result { - if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Data, @intFromEnum(data)) catch { - log.warn("render_state_get invalid data value={d}", .{@intFromEnum(data)}); - return .invalid_value; - }; - } - - return switch (data) { - .invalid => .invalid_value, - inline else => |comptime_data| getTyped( - state_, - comptime_data, - @ptrCast(@alignCast(out)), - ), - }; + const state = state_ orelse return .invalid_value; + return getDispatch(state, data, out); } pub fn get_multi( @@ -239,8 +268,16 @@ pub fn get_multi( const k = keys orelse return .invalid_value; const v = values orelse return .invalid_value; + // Unwrap the handle once for the whole batch rather than per key. + // A null handle fails on the first key, matching the per-call + // behavior. + const state: ?*RenderStateWrapper = state_; + for (0..count) |i| { - const result = get(state_, k[i], v[i]); + const result = if (state) |s| + getDispatch(s, k[i], v[i]) + else + Result.invalid_value; if (result != .success) { if (out_written) |w| w.* = i; return result; @@ -250,12 +287,34 @@ pub fn get_multi( return .success; } +inline fn getDispatch( + state: *RenderStateWrapper, + data: Data, + out: ?*anyopaque, +) Result { + if (comptime std.debug.runtime_safety) { + _ = std.enums.fromInt(Data, @intFromEnum(data)) orelse { + log.warn("render_state_get invalid data value={d}", .{@intFromEnum(data)}); + return .invalid_value; + }; + } + + const out_ptr = out orelse return .invalid_value; + return switch (data) { + .invalid => .invalid_value, + inline else => |comptime_data| getTyped( + state, + comptime_data, + @ptrCast(@alignCast(out_ptr)), + ), + }; +} + fn getTyped( - state_: RenderState, + state: *RenderStateWrapper, comptime data: Data, out: *data.OutType(), ) Result { - const state = state_ orelse return .invalid_value; switch (data) { .invalid => return .invalid_value, .cols => out.* = state.state.cols, @@ -266,11 +325,12 @@ fn getTyped( const row_data = state.state.row_data.slice(); it.* = .{ .alloc = it.alloc, - .y = null, + .y = position_none, .raws = row_data.items(.raw), .cells = row_data.items(.cells), .selection = row_data.items(.selection), .dirty = row_data.items(.dirty), + .state_dirty = &state.state.dirty, .palette = &state.state.colors.palette, }; }, @@ -299,6 +359,8 @@ fn getTyped( const vp = state.state.cursor.viewport orelse return .invalid_value; out.* = vp.wide_tail; }, + .cursor => return writeCursor(state, out), + .colors => return writeColors(state, out), } return .success; @@ -310,7 +372,7 @@ pub fn set( value: ?*const anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(SetOption, @intFromEnum(option)) catch { + _ = std.enums.fromInt(SetOption, @intFromEnum(option)) orelse { log.warn("render_state_set invalid option value={d}", .{@intFromEnum(option)}); return .invalid_value; }; @@ -338,16 +400,91 @@ fn setTyped( return .success; } -pub fn colors_get( - state_: RenderState, - out_colors_: ?*Colors, -) callconv(lib.calling_conv) Result { - const state = state_ orelse return .invalid_value; - const out_colors = out_colors_ orelse return .invalid_value; +fn writeCursor( + state: *RenderStateWrapper, + out_cursor: *Cursor, +) Result { + const out_size = out_cursor.size; + if (out_size < @sizeOf(usize)) return .invalid_value; + + const cursor = &state.state.cursor; + if (lib.structSizedFieldFits( + Cursor, + out_size, + "viewport_has_value", + )) { + out_cursor.viewport_has_value = cursor.viewport != null; + } + + if (cursor.viewport) |viewport| { + if (lib.structSizedFieldFits( + Cursor, + out_size, + "viewport_x", + )) { + out_cursor.viewport_x = viewport.x; + } + + if (lib.structSizedFieldFits( + Cursor, + out_size, + "viewport_y", + )) { + out_cursor.viewport_y = viewport.y; + } + + if (lib.structSizedFieldFits( + Cursor, + out_size, + "wide_tail", + )) { + out_cursor.wide_tail = viewport.wide_tail; + } + } + + if (lib.structSizedFieldFits( + Cursor, + out_size, + "visible", + )) { + out_cursor.visible = cursor.visible; + } + + if (lib.structSizedFieldFits( + Cursor, + out_size, + "blinking", + )) { + out_cursor.blinking = cursor.blinking; + } + + if (lib.structSizedFieldFits( + Cursor, + out_size, + "password_input", + )) { + out_cursor.password_input = cursor.password_input; + } + + if (lib.structSizedFieldFits( + Cursor, + out_size, + "visual_style", + )) { + out_cursor.visual_style = CursorVisualStyle.fromCursorStyle(cursor.visual_style); + } + + return .success; +} + +fn writeColors( + state: *RenderStateWrapper, + out_colors: *Colors, +) Result { const out_size = out_colors.size; if (out_size < @sizeOf(usize)) return .invalid_value; - const colors = state.state.colors; + const colors = &state.state.colors; if (lib.structSizedFieldFits( Colors, out_size, @@ -387,9 +524,10 @@ pub fn colors_get( if (out_size > palette_offset) { const available = out_size - palette_offset; const max_entries = @min(colors.palette.len, available / @sizeOf(colorpkg.RGB.C)); - for (0..max_entries) |i| { - out_colors.palette[i] = colors.palette[i].cval(); - } + colorpkg.paletteCvalSlice( + colors.palette[0..max_entries], + out_colors.palette[0..max_entries], + ); } } @@ -412,6 +550,7 @@ pub fn row_iterator_new( .cells = undefined, .selection = undefined, .dirty = undefined, + .state_dirty = undefined, .palette = undefined, }; result.* = ptr; @@ -426,12 +565,43 @@ pub fn row_iterator_free(iterator_: RowIterator) callconv(lib.calling_conv) void pub fn row_iterator_next(iterator_: RowIterator) callconv(lib.calling_conv) bool { const it = iterator_ orelse return false; - const next_y: size.CellCountInt = if (it.y) |y| y + 1 else 0; + // The none sentinel wraps to zero. + const next_y = it.y +% 1; if (next_y >= it.raws.len) return false; it.y = next_y; return true; } +pub fn row_iterator_next_dirty( + iterator_: RowIterator, + out_y: ?*size.CellCountInt, +) callconv(lib.calling_conv) bool { + const it = iterator_ orelse return false; + const y_out = out_y orelse return false; + + switch (it.state_dirty.*) { + .false => return false, + .full => { + // The none sentinel wraps to zero. + const next_y = it.y +% 1; + if (next_y >= it.raws.len) return false; + it.y = next_y; + y_out.* = @intCast(next_y); + return true; + }, + .partial => { + var next_y = it.y +% 1; + while (next_y < it.raws.len) : (next_y += 1) { + if (!it.dirty[next_y]) continue; + it.y = next_y; + y_out.* = @intCast(next_y); + return true; + } + return false; + }, + } +} + pub fn row_cells_new( alloc_: ?*const CAllocator, result: *RowCells, @@ -456,7 +626,8 @@ pub fn row_cells_new( pub fn row_cells_next(cells_: RowCells) callconv(lib.calling_conv) bool { const cells = cells_ orelse return false; - const next_x: size.CellCountInt = if (cells.x) |x| x + 1 else 0; + // The none sentinel wraps to zero. + const next_x = cells.x +% 1; if (next_x >= cells.raws.len) return false; cells.x = next_x; return true; @@ -508,22 +679,10 @@ pub fn row_cells_get( data: RowCellsData, out: ?*anyopaque, ) callconv(lib.calling_conv) Result { - if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(RowCellsData, @intFromEnum(data)) catch { - log.warn("render_state_row_cells_get invalid data value={d}", .{@intFromEnum(data)}); - return .invalid_value; - }; - } - if (out == null) return .invalid_value; - - return switch (data) { - .invalid => .invalid_value, - inline else => |comptime_data| rowCellsGetTyped( - cells_, - comptime_data, - @ptrCast(@alignCast(out)), - ), - }; + const cells = cells_ orelse return .invalid_value; + const x = cells.x; + if (x >= cells.raws.len) return .invalid_value; + return rowCellsGetDispatch(cells, x, data, out); } pub fn row_cells_get_multi( @@ -536,8 +695,21 @@ pub fn row_cells_get_multi( const k = keys orelse return .invalid_value; const v = values orelse return .invalid_value; + // Unwrap the handle and position once for the whole batch rather + // than per key. An invalid handle/position fails on the first + // key, matching the per-call behavior. + const unwrapped: ?struct { *RowCellsWrapper, usize } = valid: { + const cells = cells_ orelse break :valid null; + const x = cells.x; + if (x >= cells.raws.len) break :valid null; + break :valid .{ cells, x }; + }; + for (0..count) |i| { - const result = row_cells_get(cells_, k[i], v[i]); + const result = if (unwrapped) |u| + rowCellsGetDispatch(u[0], u[1], k[i], v[i]) + else + Result.invalid_value; if (result != .success) { if (out_written) |w| w.* = i; return result; @@ -547,21 +719,46 @@ pub fn row_cells_get_multi( return .success; } -fn rowCellsGetTyped( - cells_: RowCells, +inline fn rowCellsGetDispatch( + cells: *const RowCellsWrapper, + x: usize, + data: RowCellsData, + out: ?*anyopaque, +) Result { + if (comptime std.debug.runtime_safety) { + _ = std.enums.fromInt(RowCellsData, @intFromEnum(data)) orelse { + log.warn("render_state_row_cells_get invalid data value={d}", .{@intFromEnum(data)}); + return .invalid_value; + }; + } + if (out == null) return .invalid_value; + + return switch (data) { + .invalid => .invalid_value, + inline else => |comptime_data| rowCellsGetTypedInner( + cells, + x, + comptime_data, + @ptrCast(@alignCast(out)), + ), + }; +} + +fn rowCellsGetTypedInner( + cells: *const RowCellsWrapper, + x: usize, comptime data: RowCellsData, out: *data.OutType(), ) Result { - const cells = cells_ orelse return .invalid_value; - const x = cells.x orelse return .invalid_value; const cell = cells.raws[x]; switch (data) { .invalid => return .invalid_value, .raw => out.* = cell.cval(), - .style => out.* = if (cell.hasStyling()) - style_c.Style.fromStyle(cells.styles[x]) - else - style_c.Style.fromStyle(.{}), + .style => if (cell.hasStyling()) { + style_c.Style.write(cells.styles[x], out); + } else { + out.* = style_c.Style.default; + }, .graphemes_len => { if (!cell.hasText()) { out.* = 0; @@ -580,15 +777,37 @@ fn rowCellsGetTyped( } }, .bg_color => { - const s: Style = if (cell.hasStyling()) cells.styles[x] else .{}; - const bg = s.bg(&cell, cells.palette) orelse return .invalid_value; - out.* = bg.cval(); + // Avoid copying the full struct when only partial changes happen. + switch (cell.content_tag) { + .bg_color_palette => { + out.* = cells.palette[cell.content.color_palette.data].cval(); + }, + + .bg_color_rgb => { + const rgb = cell.content.color_rgb; + out.* = .{ .r = rgb.r, .g = rgb.g, .b = rgb.b }; + }, + + .codepoint, + .codepoint_grapheme, + => { + // The default style has no background. + if (!cell.hasStyling()) return .invalid_value; + switch (cells.styles[x].bg_color) { + .none => return .invalid_value, + .palette => |idx| out.* = cells.palette[idx].cval(), + .rgb => |rgb| out.* = rgb.cval(), + } + }, + } }, .fg_color => { - const s: Style = if (cell.hasStyling()) cells.styles[x] else .{}; - if (s.fg_color == .none) return .invalid_value; - const fg = s.fg(.{ .default = .{}, .palette = cells.palette }); - out.* = fg.cval(); + if (!cell.hasStyling()) return .invalid_value; + switch (cells.styles[x].fg_color) { + .none => return .invalid_value, + .palette => |idx| out.* = cells.palette[idx].cval(), + .rgb => |rgb| out.* = rgb.cval(), + } }, .selected => out.* = if (cells.selection) |sel| x >= sel[0] and x <= sel[1] @@ -606,11 +825,32 @@ fn rowCellsGetGraphemesUtf8( extra: []const u21, out: *lib.Buffer, ) Result { - out.len = 0; + if (!cell.hasText()) { + out.len = 0; + return .success; + } + + const first = cell.codepoint(); + + // Fast path: a single ASCII codepoint written to an adequately + // sized buffer. This is the overwhelmingly common case for + // terminal content. + if (first < 0x80 and extra.len == 0) { + @branchHint(.likely); + if (out.ptr) |ptr| { + if (out.cap >= 1) { + ptr[0] = @intCast(first); + out.len = 1; + return .success; + } + } + out.len = 1; + return .out_of_space; + } - if (!cell.hasText()) return .success; + out.len = 0; - var needed: usize = std.unicode.utf8CodepointSequenceLength(cell.codepoint()) catch + var needed: usize = std.unicode.utf8CodepointSequenceLength(first) catch return .invalid_value; for (extra) |cp| { needed += std.unicode.utf8CodepointSequenceLength(cp) catch @@ -622,7 +862,7 @@ fn rowCellsGetGraphemesUtf8( const buf = out.ptr.?[0..out.cap]; var i: usize = 0; - i += std.unicode.utf8Encode(cell.codepoint(), buf[i..]) catch + i += std.unicode.utf8Encode(first, buf[i..]) catch return .invalid_value; for (extra) |cp| { i += std.unicode.utf8Encode(cp, buf[i..]) catch @@ -640,6 +880,7 @@ pub const RowData = enum(c_int) { raw = 2, cells = 3, selection = 4, + cells_raw = 5, /// Output type expected for querying the data of the given kind. pub fn OutType(comptime self: RowData) type { @@ -649,6 +890,7 @@ pub const RowData = enum(c_int) { .raw => row.CRow, .cells => RowCells, .selection => RowSelection, + .cells_raw => cell_c.CellsView, }; } }; @@ -670,21 +912,10 @@ pub fn row_get( data: RowData, out: ?*anyopaque, ) callconv(lib.calling_conv) Result { - if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(RowData, @intFromEnum(data)) catch { - log.warn("render_state_row_get invalid data value={d}", .{@intFromEnum(data)}); - return .invalid_value; - }; - } - - return switch (data) { - .invalid => .invalid_value, - inline else => |comptime_data| rowGetTyped( - iterator_, - comptime_data, - @ptrCast(@alignCast(out)), - ), - }; + const it = iterator_ orelse return .invalid_value; + const y = it.y; + if (y >= it.raws.len) return .invalid_value; + return rowGetDispatch(it, y, data, out); } pub fn row_get_multi( @@ -697,8 +928,21 @@ pub fn row_get_multi( const k = keys orelse return .invalid_value; const v = values orelse return .invalid_value; + // Unwrap the handle and position once for the whole batch rather + // than per key. An invalid handle/position fails on the first + // key, matching the per-call behavior. + const unwrapped: ?struct { *RowIteratorWrapper, usize } = valid: { + const it = iterator_ orelse break :valid null; + const y = it.y; + if (y >= it.raws.len) break :valid null; + break :valid .{ it, y }; + }; + for (0..count) |i| { - const result = row_get(iterator_, k[i], v[i]); + const result = if (unwrapped) |u| + rowGetDispatch(u[0], u[1], k[i], v[i]) + else + Result.invalid_value; if (result != .success) { if (out_written) |w| w.* = i; return result; @@ -708,13 +952,36 @@ pub fn row_get_multi( return .success; } +inline fn rowGetDispatch( + it: *RowIteratorWrapper, + y: usize, + data: RowData, + out: ?*anyopaque, +) Result { + if (comptime std.debug.runtime_safety) { + _ = std.enums.fromInt(RowData, @intFromEnum(data)) orelse { + log.warn("render_state_row_get invalid data value={d}", .{@intFromEnum(data)}); + return .invalid_value; + }; + } + + return switch (data) { + .invalid => .invalid_value, + inline else => |comptime_data| rowGetTyped( + it, + y, + comptime_data, + @ptrCast(@alignCast(out)), + ), + }; +} + fn rowGetTyped( - iterator_: RowIterator, + it: *RowIteratorWrapper, + y: usize, comptime data: RowData, out: *data.OutType(), ) Result { - const it = iterator_ orelse return .invalid_value; - const y = it.y orelse return .invalid_value; switch (data) { .invalid => return .invalid_value, .dirty => out.* = it.dirty[y], @@ -724,7 +991,7 @@ fn rowGetTyped( const cell_data = it.cells[y].slice(); cells.* = .{ .alloc = cells.alloc, - .x = null, + .x = position_none, .raws = cell_data.items(.raw), .graphemes = cell_data.items(.grapheme), .styles = cell_data.items(.style), @@ -740,6 +1007,13 @@ fn rowGetTyped( out.start_x = sel[0]; out.end_x = sel[1]; }, + .cells_raw => { + const raws: []const page.Cell = it.cells[y].items(.raw); + out.* = .{ + .ptr = @ptrCast(raws.ptr), + .len = raws.len, + }; + }, } return .success; @@ -751,7 +1025,7 @@ pub fn row_set( value: ?*const anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(RowOption, @intFromEnum(option)) catch { + _ = std.enums.fromInt(RowOption, @intFromEnum(option)) orelse { log.warn("render_state_row_set invalid option value={d}", .{@intFromEnum(option)}); return .invalid_value; }; @@ -772,7 +1046,8 @@ fn rowSetTyped( value: *const option.InType(), ) Result { const it = iterator_ orelse return .invalid_value; - const y = it.y orelse return .invalid_value; + const y = it.y; + if (y >= it.raws.len) return .invalid_value; switch (option) { .dirty => it.dirty[y] = value.*, } @@ -827,11 +1102,8 @@ test "render: begin/end update" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 10, - .rows = 3, - .max_scrollback = 10_000, - }, + 10, + 3, )); defer terminal_c.free(terminal); @@ -864,6 +1136,14 @@ test "render: begin/end update" { test "render: get invalid value" { var cols: size.CellCountInt = 0; try testing.expectEqual(Result.invalid_value, get(null, .cols, @ptrCast(&cols))); + + var state: RenderState = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &state, + )); + defer free(state); + try testing.expectEqual(Result.invalid_value, get(state, .cols, null)); } test "render: get invalid data" { @@ -877,7 +1157,7 @@ test "render: get invalid data" { try testing.expectEqual(Result.invalid_value, get(state, .invalid, null)); } -test "render: colors get invalid value" { +test "render: aggregate get invalid value" { var state: RenderState = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, @@ -888,11 +1168,20 @@ test "render: colors get invalid value" { var colors: Colors = std.mem.zeroes(Colors); colors.size = @sizeOf(Colors); - try testing.expectEqual(Result.invalid_value, colors_get(null, &colors)); - try testing.expectEqual(Result.invalid_value, colors_get(state, null)); + try testing.expectEqual(Result.invalid_value, get(null, .colors, &colors)); + try testing.expectEqual(Result.invalid_value, get(state, .colors, null)); colors.size = @sizeOf(usize) - 1; - try testing.expectEqual(Result.invalid_value, colors_get(state, &colors)); + try testing.expectEqual(Result.invalid_value, get(state, .colors, &colors)); + + var cursor: Cursor = std.mem.zeroes(Cursor); + cursor.size = @sizeOf(Cursor); + + try testing.expectEqual(Result.invalid_value, get(null, .cursor, &cursor)); + try testing.expectEqual(Result.invalid_value, get(state, .cursor, null)); + + cursor.size = @sizeOf(usize) - 1; + try testing.expectEqual(Result.invalid_value, get(state, .cursor, &cursor)); } test "render: get/set dirty invalid value" { @@ -925,6 +1214,42 @@ test "render: get/set dirty" { try testing.expectEqual(Dirty.full, dirty); } +test "render: clean" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 3, + )); + defer terminal_c.free(terminal); + + var state: RenderState = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &state, + )); + defer free(state); + + try testing.expectEqual(Result.invalid_value, clean(null)); + try testing.expectEqual(Result.success, update(state, terminal)); + try testing.expectEqual(Dirty.full, state.?.state.dirty); + + try testing.expectEqual(Result.success, clean(state)); + try testing.expectEqual(Dirty.false, state.?.state.dirty); + for (state.?.state.row_data.items(.dirty)) |dirty| { + try testing.expect(!dirty); + } + + // Cleaning is idempotent, and an unchanged update remains clean. + try testing.expectEqual(Result.success, clean(state)); + try testing.expectEqual(Result.success, update(state, terminal)); + try testing.expectEqual(Dirty.false, state.?.state.dirty); + for (state.?.state.row_data.items(.dirty)) |dirty| { + try testing.expect(!dirty); + } +} + test "render: set null value" { var state: RenderState = null; try testing.expectEqual(Result.success, new( @@ -952,11 +1277,8 @@ test "render: row iterator new/free" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -983,7 +1305,7 @@ test "render: row iterator new/free" { const iterator_ptr = iterator.?; const row_data = state.?.state.row_data.slice(); - try testing.expectEqual(@as(?size.CellCountInt, null), iterator_ptr.y); + try testing.expectEqual(position_none, iterator_ptr.y); try testing.expectEqual(row_data.items(.raw).len, iterator_ptr.raws.len); try testing.expectEqual(row_data.items(.cells).len, iterator_ptr.cells.len); try testing.expectEqual(row_data.items(.selection).len, iterator_ptr.selection.len); @@ -1008,11 +1330,8 @@ test "render: row get invalid data" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1047,11 +1366,8 @@ test "render: row set before iteration" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1081,11 +1397,8 @@ test "render: row get before iteration" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1115,11 +1428,8 @@ test "render: row get/set dirty" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1173,11 +1483,8 @@ test "render: row get selection" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 10, - .rows = 3, - .max_scrollback = 10_000, - }, + 10, + 3, )); defer terminal_c.free(terminal); @@ -1222,16 +1529,75 @@ test "render: row get selection" { try testing.expectEqual(Result.no_value, row_get(it, .selection, @ptrCast(&sel))); } +test "render: row get cells_raw" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 3, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "AB", 2); + + var state: RenderState = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &state, + )); + defer free(state); + + try testing.expectEqual(Result.success, update(state, terminal)); + + var it: RowIterator = null; + try testing.expectEqual(Result.success, row_iterator_new( + &lib.alloc.test_allocator, + &it, + )); + defer row_iterator_free(it); + + try testing.expectEqual(Result.success, get(state, .row_iterator, @ptrCast(&it))); + + // Not positioned on a row yet. + var view: cell_c.CellsView = undefined; + try testing.expectEqual(Result.invalid_value, row_get(it, .cells_raw, @ptrCast(&view))); + + try testing.expect(row_iterator_next(it)); + try testing.expectEqual(Result.success, row_get(it, .cells_raw, @ptrCast(&view))); + try testing.expectEqual(@as(usize, 10), view.len); + + // The view must match the per-cell raw reads. + var cells: RowCells = null; + try testing.expectEqual(Result.success, row_cells_new( + &lib.alloc.test_allocator, + &cells, + )); + defer row_cells_free(cells); + try testing.expectEqual(Result.success, row_get(it, .cells, @ptrCast(&cells))); + const ptr = view.ptr.?; + var x: u16 = 0; + while (x < view.len) : (x += 1) { + var raw: page.Cell.C = undefined; + try testing.expectEqual(Result.success, row_cells_select(cells, x)); + try testing.expectEqual(Result.success, row_cells_get(cells, .raw, @ptrCast(&raw))); + try testing.expectEqual(raw, ptr[x]); + } + + // Contents sanity: first two cells hold our text. + const first: page.Cell = @bitCast(ptr[0]); + const second: page.Cell = @bitCast(ptr[1]); + try testing.expectEqual(@as(u21, 'A'), first.codepoint()); + try testing.expectEqual(@as(u21, 'B'), second.codepoint()); +} + test "render: row cells get selected" { var terminal: terminal_c.Terminal = null; try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 10, - .rows = 3, - .max_scrollback = 10_000, - }, + 10, + 3, )); defer terminal_c.free(terminal); @@ -1310,11 +1676,8 @@ test "render: row cells get has_styling" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 10, - .rows = 3, - .max_scrollback = 10_000, - }, + 10, + 3, )); defer terminal_c.free(terminal); @@ -1390,7 +1753,8 @@ test "render: row cells get graphemes utf8" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 10, .rows = 3, .max_scrollback = 10_000 }, + 10, + 3, )); defer terminal_c.free(terminal); @@ -1447,7 +1811,8 @@ test "render: row cells get graphemes utf8" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 10, .rows = 3, .max_scrollback = 10_000 }, + 10, + 3, )); defer terminal_c.free(terminal); @@ -1493,11 +1858,8 @@ test "render: row iterator next" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1526,16 +1888,89 @@ test "render: row iterator next" { } try testing.expect(row_iterator_next(iterator)); - try testing.expectEqual(@as(?size.CellCountInt, 0), iterator.?.y); + try testing.expectEqual(@as(usize, 0), iterator.?.y); var i: size.CellCountInt = 1; while (i < rows) : (i += 1) { try testing.expect(row_iterator_next(iterator)); - try testing.expectEqual(@as(?size.CellCountInt, i), iterator.?.y); + try testing.expectEqual(i, iterator.?.y); } try testing.expect(!row_iterator_next(iterator)); - try testing.expectEqual(@as(?size.CellCountInt, rows - 1), iterator.?.y); + try testing.expectEqual(@as(usize, rows - 1), iterator.?.y); +} + +test "render: row iterator next dirty" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + var state: RenderState = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &state, + )); + defer free(state); + try testing.expectEqual(Result.success, update(state, terminal)); + + var iterator: RowIterator = null; + try testing.expectEqual(Result.success, row_iterator_new( + &lib.alloc.test_allocator, + &iterator, + )); + defer row_iterator_free(iterator); + + // Invalid arguments neither write the output nor advance the iterator. + try testing.expectEqual(Result.success, get(state, .row_iterator, @ptrCast(&iterator))); + var out_y: size.CellCountInt = 0xCAFE; + try testing.expect(!row_iterator_next_dirty(null, &out_y)); + try testing.expectEqual(@as(size.CellCountInt, 0xCAFE), out_y); + try testing.expect(!row_iterator_next_dirty(iterator, null)); + try testing.expectEqual(position_none, iterator.?.y); + + // A full redraw returns every row, even if its row flag was cleared. + @memset(state.?.state.row_data.items(.dirty), false); + var expected_y: size.CellCountInt = 0; + while (row_iterator_next_dirty(iterator, &out_y)) : (expected_y += 1) { + try testing.expectEqual(expected_y, out_y); + } + try testing.expectEqual(@as(size.CellCountInt, 4), expected_y); + try testing.expectEqual(@as(size.CellCountInt, 3), out_y); + + // A partial redraw skips clean rows without consuming dirty flags. + const dirty = state.?.state.row_data.items(.dirty); + state.?.state.dirty = .partial; + @memset(dirty, false); + dirty[1] = true; + dirty[3] = true; + try testing.expectEqual(Result.success, get(state, .row_iterator, @ptrCast(&iterator))); + try testing.expect(row_iterator_next_dirty(iterator, &out_y)); + try testing.expectEqual(@as(size.CellCountInt, 1), out_y); + try testing.expect(row_iterator_next_dirty(iterator, &out_y)); + try testing.expectEqual(@as(size.CellCountInt, 3), out_y); + try testing.expect(!row_iterator_next_dirty(iterator, &out_y)); + try testing.expectEqual(@as(size.CellCountInt, 3), out_y); + try testing.expect(dirty[1]); + try testing.expect(dirty[3]); + + // The global clean state is authoritative, even if stale row flags exist. + state.?.state.dirty = .false; + try testing.expectEqual(Result.success, get(state, .row_iterator, @ptrCast(&iterator))); + out_y = 0xCAFE; + try testing.expect(!row_iterator_next_dirty(iterator, &out_y)); + try testing.expectEqual(@as(size.CellCountInt, 0xCAFE), out_y); + + // Existing iterators observe cleaning through their borrowed state. + state.?.state.dirty = .partial; + dirty[1] = true; + try testing.expectEqual(Result.success, get(state, .row_iterator, @ptrCast(&iterator))); + try testing.expectEqual(Result.success, clean(state)); + try testing.expect(!row_iterator_next_dirty(iterator, &out_y)); } test "render: update" { @@ -1543,11 +1978,8 @@ test "render: update" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1571,16 +2003,13 @@ test "render: update" { try testing.expectEqual(@as(size.CellCountInt, 24), rows_val); } -test "render: colors get" { +test "render: colors data get" { var terminal: terminal_c.Terminal = null; try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1595,7 +2024,7 @@ test "render: colors get" { var colors: Colors = std.mem.zeroes(Colors); colors.size = @sizeOf(Colors); - try testing.expectEqual(Result.success, colors_get(state, &colors)); + try testing.expectEqual(Result.success, get(state, .colors, &colors)); const state_colors = &state.?.state.colors; try testing.expectEqual(state_colors.background.cval(), colors.background); @@ -1613,16 +2042,128 @@ test "render: colors get" { } } +test "render: cursor data get matches scalar getters" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 80, + 24, + )); + defer terminal_c.free(terminal); + + var state: RenderState = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &state, + )); + defer free(state); + + try testing.expectEqual(Result.success, update(state, terminal)); + + var cursor: Cursor = std.mem.zeroes(Cursor); + cursor.size = @sizeOf(Cursor); + try testing.expectEqual(Result.success, get(state, .cursor, &cursor)); + + var viewport_has_value: bool = undefined; + var viewport_x: u16 = undefined; + var viewport_y: u16 = undefined; + var wide_tail: bool = undefined; + var visible: bool = undefined; + var blinking: bool = undefined; + var password_input: bool = undefined; + var visual_style: CursorVisualStyle = undefined; + try testing.expectEqual(Result.success, get(state, .cursor_viewport_has_value, &viewport_has_value)); + try testing.expectEqual(Result.success, get(state, .cursor_viewport_x, &viewport_x)); + try testing.expectEqual(Result.success, get(state, .cursor_viewport_y, &viewport_y)); + try testing.expectEqual(Result.success, get(state, .cursor_viewport_wide_tail, &wide_tail)); + try testing.expectEqual(Result.success, get(state, .cursor_visible, &visible)); + try testing.expectEqual(Result.success, get(state, .cursor_blinking, &blinking)); + try testing.expectEqual(Result.success, get(state, .cursor_password_input, &password_input)); + try testing.expectEqual(Result.success, get(state, .cursor_visual_style, &visual_style)); + + try testing.expectEqual(viewport_has_value, cursor.viewport_has_value); + try testing.expectEqual(viewport_x, cursor.viewport_x); + try testing.expectEqual(viewport_y, cursor.viewport_y); + try testing.expectEqual(wide_tail, cursor.wide_tail); + try testing.expectEqual(visible, cursor.visible); + try testing.expectEqual(blinking, cursor.blinking); + try testing.expectEqual(password_input, cursor.password_input); + try testing.expectEqual(visual_style, cursor.visual_style); +} + +test "render: cursor data get without viewport" { + var state: RenderState = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &state, + )); + defer free(state); + + state.?.state.cursor.viewport = null; + state.?.state.cursor.visible = true; + state.?.state.cursor.blinking = true; + state.?.state.cursor.password_input = true; + state.?.state.cursor.visual_style = .underline; + + var cursor: Cursor = std.mem.zeroes(Cursor); + cursor.size = @sizeOf(Cursor); + cursor.viewport_x = 0xAAAA; + cursor.viewport_y = 0xBBBB; + cursor.wide_tail = true; + try testing.expectEqual(Result.success, get(state, .cursor, &cursor)); + + try testing.expect(!cursor.viewport_has_value); + try testing.expectEqual(@as(u16, 0xAAAA), cursor.viewport_x); + try testing.expectEqual(@as(u16, 0xBBBB), cursor.viewport_y); + try testing.expect(cursor.wide_tail); + try testing.expect(cursor.visible); + try testing.expect(cursor.blinking); + try testing.expect(cursor.password_input); + try testing.expectEqual(CursorVisualStyle.underline, cursor.visual_style); +} + +test "render: cursor data get supports truncated sized struct" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 80, + 24, + )); + defer terminal_c.free(terminal); + + var state: RenderState = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &state, + )); + defer free(state); + + try testing.expectEqual(Result.success, update(state, terminal)); + const expected = state.?.state.cursor; + const viewport = expected.viewport.?; + + var cursor: Cursor = std.mem.zeroes(Cursor); + cursor.size = @offsetOf(Cursor, "viewport_y") + @sizeOf(u16); + cursor.wide_tail = !viewport.wide_tail; + cursor.visible = !expected.visible; + try testing.expectEqual(Result.success, get(state, .cursor, &cursor)); + + try testing.expect(cursor.viewport_has_value); + try testing.expectEqual(viewport.x, cursor.viewport_x); + try testing.expectEqual(viewport.y, cursor.viewport_y); + try testing.expectEqual(!viewport.wide_tail, cursor.wide_tail); + try testing.expectEqual(!expected.visible, cursor.visible); +} + test "render: row cells bg_color no background" { var terminal: terminal_c.Terminal = null; try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1668,11 +2209,8 @@ test "render: row cells bg_color from style" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1720,11 +2258,8 @@ test "render: row cells bg_color from content tag" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1774,11 +2309,8 @@ test "render: row cells fg_color no foreground" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1824,11 +2356,8 @@ test "render: row cells fg_color from style" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1871,16 +2400,13 @@ test "render: row cells fg_color from style" { try testing.expectEqual(@as(u8, 30), fg.b); } -test "render: colors get supports truncated sized struct" { +test "render: colors data get supports truncated sized struct" { var terminal: terminal_c.Terminal = null; try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1898,7 +2424,7 @@ test "render: colors get supports truncated sized struct" { for (&colors.palette) |*entry| entry.* = sentinel; colors.size = @offsetOf(Colors, "palette") + @sizeOf(colorpkg.RGB.C) * 2; - try testing.expectEqual(Result.success, colors_get(state, &colors)); + try testing.expectEqual(Result.success, get(state, .colors, &colors)); const state_colors = &state.?.state.colors; try testing.expectEqual(state_colors.palette[0].cval(), colors.palette[0]); @@ -1911,7 +2437,8 @@ test "render: get_multi success" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1947,7 +2474,8 @@ test "render: row_get_multi success" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(terminal); @@ -1990,7 +2518,8 @@ test "render: row_cells_get_multi success" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(terminal); diff --git a/vendor/libghostty-vt/src/terminal/c/result.zig b/vendor/libghostty-vt/src/terminal/c/result.zig index 32866328..174e973b 100644 --- a/vendor/libghostty-vt/src/terminal/c/result.zig +++ b/vendor/libghostty-vt/src/terminal/c/result.zig @@ -5,4 +5,7 @@ pub const Result = enum(c_int) { invalid_value = -2, out_of_space = -3, no_value = -4, + io_error = -5, + limit_exceeded = -6, + rejected = -7, }; diff --git a/vendor/libghostty-vt/src/terminal/c/row.zig b/vendor/libghostty-vt/src/terminal/c/row.zig index 66c398c8..136d6835 100644 --- a/vendor/libghostty-vt/src/terminal/c/row.zig +++ b/vendor/libghostty-vt/src/terminal/c/row.zig @@ -68,7 +68,7 @@ pub fn get( out: ?*anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(RowData, @intFromEnum(data)) catch { + _ = std.enums.fromInt(RowData, @intFromEnum(data)) orelse { return .invalid_value; }; } diff --git a/vendor/libghostty-vt/src/terminal/c/search.zig b/vendor/libghostty-vt/src/terminal/c/search.zig new file mode 100644 index 00000000..c5137891 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/c/search.zig @@ -0,0 +1,1009 @@ +const std = @import("std"); +const testing = std.testing; +const lib = @import("../lib.zig"); +const CAllocator = lib.alloc.Allocator; +const searchpkg = @import("../search.zig"); +const TerminalSearch = searchpkg.Terminal; +const FlattenedHighlight = @import("../highlight.zig").Flattened; +const selection_c = @import("selection.zig"); +const terminal_c = @import("terminal.zig"); +const Result = @import("result.zig").Result; + +const log = std.log.scoped(.search_c); + +/// C: GhosttySearch +pub const Search = ?*SearchWrapper; + +pub const SearchWrapper = struct { + alloc: std.mem.Allocator, + + /// The terminal this search is bound to. This is borrowed, so the + /// search never frees it. If the terminal is freed first, it sets + /// this to null to detach the search: calls that need the terminal + /// then fail cleanly and free releases only search-owned memory. + terminal: terminal_c.Terminal, + + /// The search state. Null when no needle is set: the search + /// starts idle and returns to idle when the needle is cleared. + search: ?TerminalSearch = null, + + /// The scroll policy applied by the select options. Persistent + /// across selects, set via the select_scroll option. + select_scroll: TerminalSearch.SelectScroll = .if_needed, + + /// The Zig terminal this search is bound to, or null if the + /// terminal was freed before the search. + fn zigTerminal(self: *const SearchWrapper) ?*terminal_c.ZigTerminal { + const terminal_wrapper = self.terminal orelse return null; + return terminal_wrapper.terminal; + } + + /// The status of the search. A search with no needle reports + /// complete since there is nothing to look for. + fn status(self: *SearchWrapper) Status { + const s = if (self.search) |*s| s else return .complete; + return .fromZig(s.status()); + } +}; + +/// C: GhosttySearchStatus +pub const Status = enum(c_int) { + running = 0, + feed_required = 1, + complete = 2, + + fn fromZig(status: TerminalSearch.Status) Status { + return switch (status) { + .running => .running, + .feed_required => .feed_required, + .complete => .complete, + }; + } +}; + +/// C: GhosttySearchScroll +pub const Scroll = enum(c_int) { + if_needed = 0, + none = 1, + + fn toZig(self: Scroll) TerminalSearch.SelectScroll { + return switch (self) { + .if_needed => .if_needed, + .none => .none, + }; + } + + fn fromZig(scroll: TerminalSearch.SelectScroll) Scroll { + return switch (scroll) { + .if_needed => .if_needed, + .none => .none, + }; + } +}; + +/// C: GhosttySearchData +pub const Data = enum(c_int) { + status = 0, + needle = 1, + total_matches = 2, + selected_index = 3, + selected_match = 4, + matches = 5, + viewport_matches = 6, + select_scroll = 7, + + pub fn OutType(comptime self: Data) type { + return switch (self) { + .status => Status, + .needle => lib.String, + .total_matches => usize, + .selected_index => usize, + .selected_match => selection_c.CSelection, + .matches, .viewport_matches => selection_c.CSelectionBuffer, + .select_scroll => Scroll, + }; + } +}; + +/// C: GhosttySearchOption +pub const Option = enum(c_int) { + needle = 0, + select_next = 1, + select_prev = 2, + select_scroll = 3, + + pub fn Type(comptime self: Option) type { + return switch (self) { + .needle => lib.String, + // The value must be NULL. Reserved for future use. + .select_next, .select_prev => void, + .select_scroll => Scroll, + }; + } +}; + +pub fn new( + alloc_: ?*const CAllocator, + out_search: ?*Search, + terminal: terminal_c.Terminal, +) callconv(lib.calling_conv) Result { + const out = out_search orelse return .invalid_value; + out.* = null; + + const terminal_wrapper = terminal orelse return .invalid_value; + const alloc = lib.alloc.default(alloc_); + const wrapper = alloc.create(SearchWrapper) catch return .out_of_memory; + wrapper.* = .{ + .alloc = alloc, + .terminal = terminal_wrapper, + }; + + // Store the search in the terminal so that when the terminal is + // freed the search can be detached safely. + terminal_wrapper.searches.putNoClobber( + terminal_wrapper.terminal.gpa(), + wrapper, + {}, + ) catch { + alloc.destroy(wrapper); + return .out_of_memory; + }; + + out.* = wrapper; + return .success; +} + +pub fn free(search_: Search) callconv(lib.calling_conv) void { + const wrapper = search_ orelse return; + if (wrapper.terminal) |terminal_wrapper| { + _ = terminal_wrapper.searches.swapRemove(wrapper); + if (wrapper.search) |*s| s.deinit(terminal_wrapper.terminal); + } else { + // The terminal was freed first. Tracked state died with it, + // so only search-owned memory is freed. + if (wrapper.search) |*s| s.deinit(null); + } + const alloc = wrapper.alloc; + alloc.destroy(wrapper); +} + +pub fn tick( + search_: Search, + out_status: ?*Status, +) callconv(lib.calling_conv) Result { + const wrapper = search_ orelse return .invalid_value; + if (wrapper.search) |*s| _ = s.tick(); + if (out_status) |out| out.* = wrapper.status(); + return .success; +} + +pub fn feed(search_: Search) callconv(lib.calling_conv) Result { + const wrapper = search_ orelse return .invalid_value; + const t = wrapper.zigTerminal() orelse return .invalid_value; + const s = if (wrapper.search) |*s| s else return .success; + + // The C API has no renderer cooperation to know whether the active + // area changed, so it is always re-scanned. This is correct without + // any dirty tracking and cheap because the active area search was + // built for exactly this. + s.feed(t, true); + return .success; +} + +pub fn run(search_: Search) callconv(lib.calling_conv) Result { + const wrapper = search_ orelse return .invalid_value; + const t = wrapper.zigTerminal() orelse return .invalid_value; + const s = if (wrapper.search) |*s| s else return .success; + + // Always start with a feed: complete only means caught up as of + // the last feed, so run doubles as the "terminal changed, catch + // up" convenience for one-shot embedders. + s.feed(t, true); + while (true) { + switch (s.status()) { + .complete => return .success, + .feed_required => s.feed(t, true), + .running => _ = s.tick(), + } + } +} + +pub fn set( + search_: Search, + option: Option, + value: ?*const anyopaque, +) callconv(lib.calling_conv) Result { + if (comptime std.debug.runtime_safety) { + _ = std.enums.fromInt(Option, @intFromEnum(option)) orelse { + log.warn("search_set invalid option value={d}", .{@intFromEnum(option)}); + return .invalid_value; + }; + } + + return switch (option) { + inline else => |comptime_option| setTyped( + search_, + comptime_option, + if (value) |ptr| @ptrCast(@alignCast(ptr)) else null, + ), + }; +} + +fn setTyped( + search_: Search, + comptime option: Option, + value: ?*const option.Type(), +) Result { + const wrapper = search_ orelse return .invalid_value; + switch (option) { + .needle => { + // The needle touches the terminal (replacing or clearing + // an active search releases tracked pins through it), so + // it requires the terminal to still be alive. + const t = wrapper.zigTerminal() orelse return .invalid_value; + + // NULL and empty both clear the needle, returning the + // search to idle. This matches the internal engine, where + // an empty needle stops the search. + const v = value orelse return clearNeedle(wrapper, t); + if (v.len == 0) return clearNeedle(wrapper, t); + if (@intFromPtr(v.ptr) == 0) return .invalid_value; + const bytes = v.ptr[0..v.len]; + + // Setting the current needle again keeps existing results, + // using the same ASCII case-insensitive comparison as + // matching. This is what a find bar wants on resubmit. + if (wrapper.search) |*s| { + if (std.ascii.eqlIgnoreCase(s.needle(), bytes)) { + return .success; + } + } + + // Create the replacement before dropping the current + // search so an allocation failure keeps existing state. + const replacement = TerminalSearch.init( + wrapper.alloc, + bytes, + ) catch return .out_of_memory; + if (wrapper.search) |*s| s.deinit(t); + wrapper.search = replacement; + }, + + .select_next, .select_prev => { + // The value is reserved for future use and must be NULL. + if (value != null) return .invalid_value; + + const t = wrapper.zigTerminal() orelse return .invalid_value; + const s = if (wrapper.search) |*s| s else return .no_value; + const selected = s.select( + t, + switch (option) { + .select_next => .next, + .select_prev => .prev, + else => comptime unreachable, + }, + wrapper.select_scroll, + ) catch return .out_of_memory; + if (!selected) return .no_value; + }, + + .select_scroll => { + const v = value orelse { + wrapper.select_scroll = .if_needed; + return .success; + }; + const scroll = std.enums.fromInt(Scroll, @intFromEnum(v.*)) orelse + return .invalid_value; + wrapper.select_scroll = scroll.toZig(); + }, + } + + return .success; +} + +fn clearNeedle(wrapper: *SearchWrapper, t: *terminal_c.ZigTerminal) Result { + if (wrapper.search) |*s| { + s.deinit(t); + wrapper.search = null; + } + return .success; +} + +pub fn get( + search_: Search, + data: Data, + value: ?*anyopaque, +) callconv(lib.calling_conv) Result { + if (comptime std.debug.runtime_safety) { + _ = std.enums.fromInt(Data, @intFromEnum(data)) orelse { + log.warn("search_get invalid data value={d}", .{@intFromEnum(data)}); + return .invalid_value; + }; + } + + const out_ptr = value orelse return .invalid_value; + return switch (data) { + inline else => |comptime_data| getTyped( + search_, + comptime_data, + @ptrCast(@alignCast(out_ptr)), + ), + }; +} + +pub fn get_multi( + search_: Search, + count: usize, + keys: ?[*]const Data, + values: ?[*]?*anyopaque, + out_written: ?*usize, +) callconv(lib.calling_conv) Result { + const k = keys orelse return .invalid_value; + const v = values orelse return .invalid_value; + + for (0..count) |i| { + const result = get(search_, k[i], v[i]); + if (result != .success) { + if (out_written) |w| w.* = i; + return result; + } + } + if (out_written) |w| w.* = count; + return .success; +} + +fn getTyped( + search_: Search, + comptime data: Data, + out: *data.OutType(), +) Result { + const wrapper = search_ orelse return .invalid_value; + + switch (data) { + .status => out.* = wrapper.status(), + + .needle => { + const s = if (wrapper.search) |*s| s else return .no_value; + out.* = .init(s.needle()); + }, + + .total_matches => out.* = total: { + const s = if (wrapper.search) |*s| s else break :total 0; + const ss = s.activeScreenSearch() orelse break :total 0; + break :total ss.matchesLen(); + }, + + .selected_index => { + const s = if (wrapper.search) |*s| s else return .no_value; + const ss = s.activeScreenSearch() orelse return .no_value; + const selected = ss.selected orelse return .no_value; + out.* = selected.idx; + }, + + .selected_match => { + const s = if (wrapper.search) |*s| s else return .no_value; + const ss = s.activeScreenSearch() orelse return .no_value; + const hl = ss.selectedMatch() orelse return .no_value; + out.* = selectionFromHighlight(hl); + }, + + .matches => { + const ss = if (wrapper.search) |*s| + s.activeScreenSearch() + else + null; + const total = if (ss) |v| v.matchesLen() else 0; + if (out.cap < total) { + out.len = total; + return .out_of_space; + } + if (total > 0) { + const dst = (out.ptr orelse return .invalid_value)[0..total]; + for (dst, 0..) |*d, i| { + d.* = selectionFromHighlight(ss.?.matchAt(i).?); + } + } + out.len = total; + }, + + .viewport_matches => { + const matches: []const FlattenedHighlight = if (wrapper.search) |*s| + s.viewportMatches() catch return .out_of_memory + else + &.{}; + if (out.cap < matches.len) { + out.len = matches.len; + return .out_of_space; + } + if (matches.len > 0) { + const dst = (out.ptr orelse return .invalid_value)[0..matches.len]; + for (dst, matches) |*d, hl| d.* = selectionFromHighlight(hl); + } + out.len = matches.len; + }, + + .select_scroll => out.* = .fromZig(wrapper.select_scroll), + } + + return .success; +} + +fn selectionFromHighlight(hl: FlattenedHighlight) selection_c.CSelection { + const untracked = hl.untracked(); + return .{ + .start = .fromPin(untracked.start), + .end = .fromPin(untracked.end), + .rectangle = false, + }; +} + +fn testString(str: []const u8) lib.String { + return .init(str); +} + +fn testNewSearch(terminal: terminal_c.Terminal, needle_str: []const u8) !Search { + var search: Search = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &search, + terminal, + )); + errdefer free(search); + const needle_value = testString(needle_str); + try testing.expectEqual(Result.success, set(search, .needle, &needle_value)); + return search; +} + +test "search lifecycle and run to complete" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz\r\nBuzz\r\nFizz\r\nBang", 22); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + + // Right after the needle is set the search must report + // feed_required, not complete. The internal completion check is + // trivially true before the search has seen the terminal. + var status: Status = .complete; + try testing.expectEqual(Result.success, get(search, .status, &status)); + try testing.expectEqual(Status.feed_required, status); + + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, get(search, .status, &status)); + try testing.expectEqual(Status.complete, status); + + var total: usize = 0; + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 2), total); + + // The needle is borrowed and matches what we searched for. + var needle: lib.String = undefined; + try testing.expectEqual(Result.success, get(search, .needle, &needle)); + try testing.expectEqualStrings("Fizz", needle.ptr[0..needle.len]); +} + +test "search tick reports feed required before first feed" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + + var status: Status = .complete; + try testing.expectEqual(Result.success, tick(search, &status)); + try testing.expectEqual(Status.feed_required, status); + + // NULL status out is allowed. + try testing.expectEqual(Result.success, tick(search, null)); +} + +test "search feed after write updates results" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz", 4); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + + try testing.expectEqual(Result.success, run(search)); + var total: usize = 0; + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 1), total); + + // Write another match: the caller-driven feed is the "terminal + // changed" signal. + terminal_c.vt_write(terminal, "\r\nFizz", 6); + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 2), total); +} + +test "search alt screen flip and return retains results" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz\r\nFizz", 10); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + + try testing.expectEqual(Result.success, run(search)); + var total: usize = 0; + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 2), total); + + // Flip to the alternate screen: reads now reflect that screen. + terminal_c.vt_write(terminal, "\x1b[?1049h", 8); + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 0), total); + + terminal_c.vt_write(terminal, "Fizz", 4); + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 1), total); + + // Return to the primary screen: results are retained and restored. + terminal_c.vt_write(terminal, "\x1b[?1049l", 8); + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 2), total); +} + +test "search select wraps in both directions" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz\r\nBuzz\r\nFizz", 16); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + try testing.expectEqual(Result.success, run(search)); + + // No selection yet. + var idx: usize = 999; + try testing.expectEqual(Result.no_value, get(search, .selected_index, &idx)); + + // Next: newest match first (index 0), then older (1), then wrap + // back to 0. + try testing.expectEqual(Result.success, set(search, .select_next, null)); + try testing.expectEqual(Result.success, get(search, .selected_index, &idx)); + try testing.expectEqual(@as(usize, 0), idx); + + var match: selection_c.CSelection = undefined; + try testing.expectEqual(Result.success, get(search, .selected_match, &match)); + try testing.expect(match.start.toPin() != null); + + try testing.expectEqual(Result.success, set(search, .select_next, null)); + try testing.expectEqual(Result.success, get(search, .selected_index, &idx)); + try testing.expectEqual(@as(usize, 1), idx); + + try testing.expectEqual(Result.success, set(search, .select_next, null)); + try testing.expectEqual(Result.success, get(search, .selected_index, &idx)); + try testing.expectEqual(@as(usize, 0), idx); + + // Prev: wrap backward to the oldest match. + try testing.expectEqual(Result.success, set(search, .select_prev, null)); + try testing.expectEqual(Result.success, get(search, .selected_index, &idx)); + try testing.expectEqual(@as(usize, 1), idx); +} + +test "search select with no matches returns no value" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + + try testing.expectEqual(Result.no_value, set(search, .select_next, null)); + try testing.expectEqual(Result.no_value, set(search, .select_prev, null)); +} + +test "search select rejects a non-null reserved value" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + + const bogus: c_int = 0; + try testing.expectEqual(Result.invalid_value, set(search, .select_next, &bogus)); + try testing.expectEqual(Result.invalid_value, set(search, .select_prev, &bogus)); +} + +test "search select scroll option" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + + var scroll: Scroll = .none; + try testing.expectEqual(Result.success, get(search, .select_scroll, &scroll)); + try testing.expectEqual(Scroll.if_needed, scroll); + + const none: Scroll = .none; + try testing.expectEqual(Result.success, set(search, .select_scroll, &none)); + try testing.expectEqual(Result.success, get(search, .select_scroll, &scroll)); + try testing.expectEqual(Scroll.none, scroll); + + // NULL resets to the default. + try testing.expectEqual(Result.success, set(search, .select_scroll, null)); + try testing.expectEqual(Result.success, get(search, .select_scroll, &scroll)); + try testing.expectEqual(Scroll.if_needed, scroll); + + // Invalid enum values are rejected. + const bogus: c_int = 42; + try testing.expectEqual(Result.invalid_value, set(search, .select_scroll, @ptrCast(&bogus))); +} + +test "search selection dropped when reset prunes results" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz\r\nBuzz", 10); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, set(search, .select_next, null)); + + // A selection survives unrelated writes... + terminal_c.vt_write(terminal, "\r\nBang", 6); + try testing.expectEqual(Result.success, run(search)); + var match: selection_c.CSelection = undefined; + try testing.expectEqual(Result.success, get(search, .selected_match, &match)); + + // ...but a full reset prunes every result, dropping the selection. + terminal_c.vt_write(terminal, "\x1bc", 2); + try testing.expectEqual(Result.success, run(search)); + var total: usize = 999; + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 0), total); + try testing.expectEqual(Result.no_value, get(search, .selected_match, &match)); +} + +test "search matches buffer semantics" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz\r\nBuzz\r\nFizz", 16); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + try testing.expectEqual(Result.success, run(search)); + + // NULL ptr with cap 0 queries the required capacity. + var buf: selection_c.CSelectionBuffer = .{}; + try testing.expectEqual(Result.out_of_space, get(search, .matches, &buf)); + try testing.expectEqual(@as(usize, 2), buf.len); + + // Too-small buffers report the required capacity. + var one: [1]selection_c.CSelection = undefined; + buf = .{ .ptr = &one, .cap = one.len }; + try testing.expectEqual(Result.out_of_space, get(search, .matches, &buf)); + try testing.expectEqual(@as(usize, 2), buf.len); + + // A large-enough buffer is filled newest to oldest. + var storage: [4]selection_c.CSelection = undefined; + buf = .{ .ptr = &storage, .cap = storage.len }; + try testing.expectEqual(Result.success, get(search, .matches, &buf)); + try testing.expectEqual(@as(usize, 2), buf.len); + const first = storage[0].start.toPin().?; + const second = storage[1].start.toPin().?; + try testing.expect(first.y != second.y or first.node != second.node); + + // Viewport matches use the same conventions and are cached across + // reads. + buf = .{}; + try testing.expectEqual(Result.out_of_space, get(search, .viewport_matches, &buf)); + try testing.expectEqual(@as(usize, 2), buf.len); + buf = .{ .ptr = &storage, .cap = storage.len }; + try testing.expectEqual(Result.success, get(search, .viewport_matches, &buf)); + try testing.expectEqual(@as(usize, 2), buf.len); + buf = .{ .ptr = &storage, .cap = storage.len }; + try testing.expectEqual(Result.success, get(search, .viewport_matches, &buf)); + try testing.expectEqual(@as(usize, 2), buf.len); +} + +test "search get_multi returns first failing index" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz", 4); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + try testing.expectEqual(Result.success, run(search)); + + // No selection: selected_index fails at index 1. + const keys = [_]Data{ .total_matches, .selected_index, .status }; + var total: usize = 0; + var idx: usize = 0; + var status: Status = .running; + var values = [_]?*anyopaque{ &total, &idx, &status }; + var written: usize = 999; + try testing.expectEqual(Result.no_value, get_multi( + search, + keys.len, + &keys, + &values, + &written, + )); + try testing.expectEqual(@as(usize, 1), written); + try testing.expectEqual(@as(usize, 1), total); + + // After selecting, the whole batch succeeds. + try testing.expectEqual(Result.success, set(search, .select_next, null)); + try testing.expectEqual(Result.success, get_multi( + search, + keys.len, + &keys, + &values, + &written, + )); + try testing.expectEqual(keys.len, written); + try testing.expectEqual(@as(usize, 0), idx); +} + +test "search new validates options" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + var search: Search = null; + + // NULL out and NULL terminal are invalid. + try testing.expectEqual(Result.invalid_value, new( + &lib.alloc.test_allocator, + null, + terminal, + )); + try testing.expectEqual(Result.invalid_value, new( + &lib.alloc.test_allocator, + &search, + null, + )); + try testing.expect(search == null); +} + +test "search without a needle is idle" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz", 4); + + var search: Search = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &search, + terminal, + )); + defer free(search); + + // With nothing to look for, the search is complete and empty, and + // driving it is a harmless no-op. + var status: Status = .running; + try testing.expectEqual(Result.success, get(search, .status, &status)); + try testing.expectEqual(Status.complete, status); + try testing.expectEqual(Result.success, feed(search)); + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, tick(search, &status)); + try testing.expectEqual(Status.complete, status); + + var needle_out: lib.String = undefined; + try testing.expectEqual(Result.no_value, get(search, .needle, &needle_out)); + var total: usize = 999; + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 0), total); + try testing.expectEqual(Result.no_value, set(search, .select_next, null)); + + var buf: selection_c.CSelectionBuffer = .{}; + try testing.expectEqual(Result.success, get(search, .matches, &buf)); + try testing.expectEqual(@as(usize, 0), buf.len); + buf = .{}; + try testing.expectEqual(Result.success, get(search, .viewport_matches, &buf)); + try testing.expectEqual(@as(usize, 0), buf.len); + + // Clearing an already-clear needle is fine. + try testing.expectEqual(Result.success, set(search, .needle, null)); +} + +test "search needle change and clear" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + terminal_c.vt_write(terminal, "Fizz\r\nBuzz", 10); + + const search: Search = try testNewSearch(terminal, "Fizz"); + defer free(search); + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, set(search, .select_next, null)); + + var total: usize = 0; + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 1), total); + + // Setting the same needle again (any ASCII case) keeps existing + // results and the selection. + const same = testString("fIZZ"); + try testing.expectEqual(Result.success, set(search, .needle, &same)); + var needle_out: lib.String = undefined; + try testing.expectEqual(Result.success, get(search, .needle, &needle_out)); + try testing.expectEqualStrings("Fizz", needle_out.ptr[0..needle_out.len]); + var idx: usize = 999; + try testing.expectEqual(Result.success, get(search, .selected_index, &idx)); + try testing.expectEqual(@as(usize, 0), idx); + + // Changing the needle restarts the search from scratch. + const changed = testString("Buzz"); + try testing.expectEqual(Result.success, set(search, .needle, &changed)); + var status: Status = .complete; + try testing.expectEqual(Result.success, get(search, .status, &status)); + try testing.expectEqual(Status.feed_required, status); + try testing.expectEqual(Result.no_value, get(search, .selected_index, &idx)); + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 1), total); + try testing.expectEqual(Result.success, get(search, .needle, &needle_out)); + try testing.expectEqualStrings("Buzz", needle_out.ptr[0..needle_out.len]); + + // An empty needle clears the search, same as NULL. + const empty = testString(""); + try testing.expectEqual(Result.success, set(search, .needle, &empty)); + try testing.expectEqual(Result.no_value, get(search, .needle, &needle_out)); + try testing.expectEqual(Result.success, get(search, .status, &status)); + try testing.expectEqual(Status.complete, status); + try testing.expectEqual(Result.success, get(search, .total_matches, &total)); + try testing.expectEqual(@as(usize, 0), total); +} + +test "search free after terminal free" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + + terminal_c.vt_write(terminal, "Fizz\r\nFizz", 10); + + const search: Search = try testNewSearch(terminal, "Fizz"); + + // Run and select so the search holds tracked pins within the + // terminal's page storage. + try testing.expectEqual(Result.success, run(search)); + try testing.expectEqual(Result.success, set(search, .select_next, null)); + + // Free the terminal first. The search detaches: calls that need + // the terminal fail cleanly instead of touching freed memory. + terminal_c.free(terminal); + try testing.expectEqual(Result.invalid_value, feed(search)); + try testing.expectEqual(Result.invalid_value, run(search)); + try testing.expectEqual(Result.invalid_value, set(search, .select_next, null)); + const needle_value = testString("Buzz"); + try testing.expectEqual(Result.invalid_value, set(search, .needle, &needle_value)); + + // Reads that only touch search-owned state still answer. + var needle_out: lib.String = undefined; + try testing.expectEqual(Result.success, get(search, .needle, &needle_out)); + try testing.expectEqualStrings("Fizz", needle_out.ptr[0..needle_out.len]); + const scroll: Scroll = .none; + try testing.expectEqual(Result.success, set(search, .select_scroll, &scroll)); + + // The search can still be freed, releasing only its own memory. + free(search); +} + +test "search freed before terminal detaches from the registry" { + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &terminal, + 10, + 4, + )); + defer terminal_c.free(terminal); + + // Create two searches and free one while the terminal is alive. + // The freed search must be unregistered so the later terminal free + // only detaches the survivor. + const a: Search = try testNewSearch(terminal, "Fizz"); + const b: Search = try testNewSearch(terminal, "Fizz"); + defer free(b); + + try testing.expectEqual(Result.success, run(a)); + free(a); + try testing.expectEqual(Result.success, run(b)); +} + +test "search free null" { + free(null); +} diff --git a/vendor/libghostty-vt/src/terminal/c/selection.zig b/vendor/libghostty-vt/src/terminal/c/selection.zig index cb574ecc..2830149f 100644 --- a/vendor/libghostty-vt/src/terminal/c/selection.zig +++ b/vendor/libghostty-vt/src/terminal/c/selection.zig @@ -38,6 +38,18 @@ pub const CSelection = extern struct { } }; +/// C: GhosttySelectionBuffer +/// +/// A caller-provided buffer of selections. This follows the same +/// conventions as GhosttyBuffer: ptr may be NULL with cap 0 to query +/// the required capacity, and len is set to the entries written on +/// success or to the required capacity on GHOSTTY_OUT_OF_SPACE. +pub const CSelectionBuffer = extern struct { + ptr: ?[*]CSelection = null, + cap: usize = 0, + len: usize = 0, +}; + /// C: GhosttyTerminalSelectWordOptions pub const SelectWordOptions = extern struct { size: usize = @sizeOf(SelectWordOptions), @@ -234,7 +246,8 @@ pub fn format_alloc( }; const buf = aw.toOwnedSlice() catch return .out_of_memory; - out_ptr.* = buf.ptr; + // Do not expose Zig's empty slice sentinel through the C ABI. + out_ptr.* = if (buf.len == 0) null else buf.ptr; out_len.* = buf.len; return .success; } @@ -253,7 +266,7 @@ fn selectionFormatter( opts: FormatOptions, ) error{ InvalidValue, NoValue }!formatterpkg.TerminalFormatter { if (opts.size < @sizeOf(FormatOptions)) return error.InvalidValue; - _ = std.meta.intToEnum(Format, @intFromEnum(opts.emit)) catch + _ = std.enums.fromInt(Format, @intFromEnum(opts.emit)) orelse return error.InvalidValue; const sel = if (opts.selection) |sel| @@ -308,7 +321,7 @@ pub fn adjust( adjustment: Selection.Adjustment, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Selection.Adjustment, @intFromEnum(adjustment)) catch { + _ = std.enums.fromInt(Selection.Adjustment, @intFromEnum(adjustment)) orelse { log.warn("terminal_selection_adjust invalid adjustment value={d}", .{@intFromEnum(adjustment)}); return .invalid_value; }; @@ -343,7 +356,7 @@ pub fn ordered( out_selection: ?*CSelection, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Selection.Order, @intFromEnum(desired)) catch { + _ = std.enums.fromInt(Selection.Order, @intFromEnum(desired)) orelse { log.warn("terminal_selection_ordered invalid desired value={d}", .{@intFromEnum(desired)}); return .invalid_value; }; @@ -392,12 +405,44 @@ pub fn equal( return .success; } +test "selection_format_alloc empty output" { + const failing: CAllocator = .fromZig(&std.mem.Allocator.failing); + var t: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer terminal_c.free(t); + + var ref: grid_ref.CGridRef = .{}; + try testing.expectEqual(Result.success, terminal_c.grid_ref(t, .{ + .tag = .active, + .value = .{ .active = .{ .x = 0, .y = 0 } }, + }, &ref)); + const sel: CSelection = .{ .start = ref, .end = ref }; + try testing.expectEqual(Result.success, terminal_c.set(t, .selection, &sel)); + + var ptr: ?[*]u8 = null; + var len: usize = 123; + try testing.expectEqual(Result.success, format_alloc(t, &failing, .{ + .emit = .plain, + .unwrap = true, + .trim = true, + }, &ptr, &len)); + defer @import("allocator.zig").free(&failing, ptr, len); + try testing.expectEqual(@as(usize, 0), len); + try testing.expectEqual(null, ptr); +} + test "selection_format_alloc uses active selection" { var t: terminal_c.Terminal = null; try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -457,7 +502,8 @@ test "selection_format_buf uses provided selection" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); @@ -513,7 +559,8 @@ test "selection_format_alloc returns no_value without active selection" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer terminal_c.free(t); diff --git a/vendor/libghostty-vt/src/terminal/c/selection_gesture.zig b/vendor/libghostty-vt/src/terminal/c/selection_gesture.zig index cc56da3c..46253057 100644 --- a/vendor/libghostty-vt/src/terminal/c/selection_gesture.zig +++ b/vendor/libghostty-vt/src/terminal/c/selection_gesture.zig @@ -241,7 +241,7 @@ pub fn event_new( event_type: EventType, ) callconv(lib.calling_conv) Result { const out = out_event orelse return .invalid_value; - _ = std.meta.intToEnum(EventType, @intFromEnum(event_type)) catch + _ = std.enums.fromInt(EventType, @intFromEnum(event_type)) orelse return .invalid_value; const alloc = lib.alloc.default(alloc_); @@ -343,7 +343,7 @@ pub fn event_set( value: ?*const anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(EventOption, @intFromEnum(option)) catch { + _ = std.enums.fromInt(EventOption, @intFromEnum(option)) orelse { log.warn("selection_gesture_event_set invalid option value={d}", .{@intFromEnum(option)}); return .invalid_value; }; @@ -365,7 +365,7 @@ pub fn get( out: ?*anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Data, @intFromEnum(data)) catch { + _ = std.enums.fromInt(Data, @intFromEnum(data)) orelse { log.warn("selection_gesture_get invalid data value={d}", .{@intFromEnum(data)}); return .invalid_value; }; @@ -738,24 +738,12 @@ fn clearWordBoundaryCodepoints(event: *EventWrapper, target: *[]const u21) void target.* = &selection_codepoints.default_word_boundaries; } -fn instantFromNs(ns: u64) SelectionGesture.Time { - if (comptime builtin.target.cpu.arch == .wasm32 and - builtin.target.os.tag == .freestanding) - { - return ns; - } - - return switch (builtin.os.tag) { - .windows, .uefi, .wasi => .{ .timestamp = ns }, - else => .{ .timestamp = .{ - .sec = @intCast(ns / std.time.ns_per_s), - .nsec = @intCast(ns % std.time.ns_per_s), - } }, - }; +fn instantFromNs(ns: u64) std.Io.Timestamp { + return .fromNanoseconds(ns); } fn validBehavior(behavior: Behavior) bool { - _ = std.meta.intToEnum(Behavior, @intFromEnum(behavior)) catch return false; + _ = std.enums.fromInt(Behavior, @intFromEnum(behavior)) orelse return false; return true; } @@ -764,7 +752,8 @@ test "selection gesture lifecycle and get" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -800,7 +789,8 @@ test "selection gesture get_multi" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -844,7 +834,8 @@ test "selection gesture get_multi returns first failing index" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -956,7 +947,8 @@ test "selection gesture event applies press" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1003,7 +995,8 @@ test "selection gesture event press requires ref" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1031,7 +1024,8 @@ test "selection gesture event null output still reports no selection" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1065,7 +1059,8 @@ test "selection gesture event applies release" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1116,7 +1111,8 @@ test "selection gesture release without ref marks dragged" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1163,7 +1159,8 @@ test "selection gesture event applies drag" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1235,7 +1232,8 @@ test "selection gesture drag requires ref and geometry" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1279,7 +1277,8 @@ test "selection gesture event applies autoscroll tick" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1362,7 +1361,8 @@ test "selection gesture autoscroll tick requires viewport and geometry" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1401,7 +1401,8 @@ test "selection gesture event applies deep press" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); @@ -1456,7 +1457,8 @@ test "selection gesture deep press without active anchor returns no value" { try testing.expectEqual(Result.success, terminal_c.new( &lib.alloc.test_allocator, &terminal, - .{ .cols = 5, .rows = 2, .max_scrollback = 10_000 }, + 5, + 2, )); defer terminal_c.free(terminal); diff --git a/vendor/libghostty-vt/src/terminal/c/sgr.zig b/vendor/libghostty-vt/src/terminal/c/sgr.zig index b0ef23d0..bb8830cb 100644 --- a/vendor/libghostty-vt/src/terminal/c/sgr.zig +++ b/vendor/libghostty-vt/src/terminal/c/sgr.zig @@ -126,17 +126,6 @@ pub fn attribute_value( return &attr.value; } -pub fn wasm_alloc_attribute() callconv(lib.calling_conv) *sgr.Attribute.C { - const alloc = std.heap.wasm_allocator; - const ptr = alloc.create(sgr.Attribute.C) catch @panic("out of memory"); - return ptr; -} - -pub fn wasm_free_attribute(attr: *sgr.Attribute.C) callconv(lib.calling_conv) void { - const alloc = std.heap.wasm_allocator; - alloc.destroy(attr); -} - test "alloc" { var p: Parser = undefined; try testing.expectEqual(Result.success, new( diff --git a/vendor/libghostty-vt/src/terminal/c/snapshot.zig b/vendor/libghostty-vt/src/terminal/c/snapshot.zig new file mode 100644 index 00000000..13d5f2b9 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/c/snapshot.zig @@ -0,0 +1,1527 @@ +//! C ABI wrappers for snapshot encoding and incremental restoration. + +const std = @import("std"); +const testing = std.testing; +const lib = @import("../lib.zig"); +const CAllocator = lib.alloc.Allocator; +const io_c = @import("io.zig"); +const Result = @import("result.zig").Result; +const snapshot_core = @import("../snapshot/main.zig"); +const apc = @import("../apc.zig"); + +/// Snapshot readers accept the largest default APC payload. Deriving this from +/// the APC protocols keeps snapshot validation in sync as protocols change. +/// Opt-in decoded-terminal retention reuses this configured validation bound +/// as its runtime tracking limit. +const default_max_continuation_bytes = apc.Protocol.maxDefaultBytes(); +const terminal_c = @import("terminal.zig"); + +/// C: GhosttySnapshotDecoderOption. +/// +/// Options configure validation and returned-terminal policy before snapshot +/// decoding begins. +pub const DecoderOption = enum(c_int) { + max_continuation_bytes = 0, + retain_continuation = 1, + _, +}; + +/// C: GhosttySnapshotDecoderData. +/// +/// Data keys select typed values exposed by the decoder getter APIs. +pub const DecoderData = enum(c_int) { + invalid = 0, + max_continuation_bytes = 1, + source_offset = 2, + history_rows_primary = 3, + history_rows_alternate = 4, + progress_screen = 5, + progress_rows = 6, + progress_remaining = 7, + retain_continuation = 8, + _, + + /// Return the Zig type stored through the C API output pointer for a key. + fn OutType(comptime self: DecoderData) type { + return switch (self) { + .invalid => void, + .max_continuation_bytes, .source_offset => usize, + .history_rows_primary, .history_rows_alternate => u64, + .progress_screen => terminal_c.TerminalScreen, + .progress_rows => usize, + .progress_remaining => u32, + .retain_continuation => bool, + _ => void, + }; + } +}; + +/// Owns the source adapter and all state needed by an opaque C decoder handle. +const DecoderWrapper = struct { + /// A callback or borrowed-buffer source with uniform reader access. + const Source = union(enum) { + callback: io_c.ReaderAdapter, + buffer: std.Io.Reader, + + /// Return the stable `std.Io.Reader` used by the snapshot decoder. + fn reader(self: *Source) *std.Io.Reader { + return switch (self.*) { + .callback => |*value| &value.interface, + .buffer => |*value| value, + }; + } + + /// Return the number of source bytes consumed without reading ahead. + fn offset(self: *const Source) usize { + return switch (self.*) { + .callback => |*value| value.offset, + .buffer => |*value| value.seek, + }; + } + }; + + /// READY metadata that remains queryable through FINISH or a later error. + const Metadata = struct { + history_rows: std.EnumMap(terminal_c.TerminalScreen, u64), + }; + + /// Decoder lifecycle and the data available in each phase. + const State = union(enum) { + configuring, + + history: struct { + terminal: terminal_c.Terminal, + metadata: Metadata, + progress: ?snapshot_core.Decoder.Progress = null, + }, + + finished: Metadata, + + /// READY failures have no metadata. Failures from `next` retain the + /// READY metadata even though decoding cannot continue. + failed: ?Metadata, + }; + + alloc: std.mem.Allocator, + source: Source, + decoder: snapshot_core.Decoder, + state: State, + max_continuation_bytes: usize, + retain_continuation: bool, +}; + +/// C: GhosttySnapshotDecoder, an opaque nullable decoder handle. +pub const Decoder = ?*DecoderWrapper; + +/// Create a decoder backed by a synchronous C reader callback. +pub fn decoder_new( + alloc_: ?*const CAllocator, + out_: ?*Decoder, + reader: io_c.Reader, +) callconv(lib.calling_conv) Result { + // Validate C-owned output and callback pointers before allocating anything. + const out = out_ orelse return .invalid_value; + out.* = null; + if (reader.read == null) return .invalid_value; + + // Store the adapter inline so its std.Io.Reader has a stable address. + return decoderNewSource(alloc_, out, .{ + .callback = io_c.ReaderAdapter.init(reader), + }); +} + +/// Create a decoder backed by immutable, caller-owned bytes. +pub fn decoder_new_buf( + alloc_: ?*const CAllocator, + out_: ?*Decoder, + ptr: ?[*]const u8, + len: usize, +) callconv(lib.calling_conv) Result { + // Normalize the C pointer and length into a borrowed Zig slice. + const out = out_ orelse return .invalid_value; + out.* = null; + const bytes: []const u8 = if (ptr) |value| + value[0..len] + else if (len == 0) + &.{} + else + return .invalid_value; + + // The fixed reader records its current seek position as the source offset. + return decoderNewSource(alloc_, out, .{ + .buffer = .fixed(bytes), + }); +} + +/// Allocate and initialize a decoder around either supported source type. +fn decoderNewSource( + alloc_: ?*const CAllocator, + out: *Decoder, + source: DecoderWrapper.Source, +) Result { + // Allocate first so the source and its embedded reader never move again. + const alloc = lib.alloc.default(alloc_); + const wrapper = alloc.create(DecoderWrapper) catch return .out_of_memory; + errdefer alloc.destroy(wrapper); + + // Initialize the core decoder only after its reader has a stable address. + wrapper.* = undefined; + wrapper.alloc = alloc; + wrapper.source = source; + wrapper.state = .configuring; + wrapper.max_continuation_bytes = default_max_continuation_bytes; + wrapper.retain_continuation = false; + wrapper.decoder = .init(wrapper.source.reader()); + out.* = wrapper; + return .success; +} + +/// Destroy a decoder without taking ownership of any returned terminal. +pub fn decoder_free(decoder_: Decoder) callconv(lib.calling_conv) void { + const decoder = decoder_ orelse return; + const alloc = decoder.alloc; + alloc.destroy(decoder); +} + +/// Set a decoder option while the decoder is still configuring. +pub fn decoder_set( + decoder_: Decoder, + option: DecoderOption, + value_: ?*const anyopaque, +) callconv(lib.calling_conv) Result { + // Options freeze as soon as any decoding attempt starts. + const decoder = decoder_ orelse return .invalid_value; + switch (decoder.state) { + .configuring => {}, + else => return .invalid_value, + } + + // Interpret the erased C pointer according to the selected option. + const value = value_ orelse return .invalid_value; + switch (option) { + .max_continuation_bytes => decoder.max_continuation_bytes = + @as(*const usize, @ptrCast(@alignCast(value))).*, + .retain_continuation => decoder.retain_continuation = + @as(*const bool, @ptrCast(@alignCast(value))).*, + _ => return .invalid_value, + } + return .success; +} + +/// Read one typed decoder value through the type-erased C API. +pub fn decoder_get( + decoder_: Decoder, + data: DecoderData, + out: ?*anyopaque, +) callconv(lib.calling_conv) Result { + // Enumerate known keys so each branch recovers the correct output type. + return switch (data) { + inline .invalid, + .max_continuation_bytes, + .source_offset, + .history_rows_primary, + .history_rows_alternate, + .progress_screen, + .progress_rows, + .progress_remaining, + .retain_continuation, + => |comptime_data| decoderGetTyped( + decoder_, + comptime_data, + @ptrCast(@alignCast(out)), + ), + _ => .invalid_value, + }; +} + +/// Implement a getter after the key has selected its concrete output type. +fn decoderGetTyped( + decoder_: Decoder, + comptime data: DecoderData, + out: *data.OutType(), +) Result { + const decoder = decoder_ orelse return .invalid_value; + switch (data) { + // Decoder-wide configuration and source state. + .invalid => return .invalid_value, + .max_continuation_bytes => { + if (decoderFailed(decoder)) return .no_value; + out.* = decoder.max_continuation_bytes; + }, + .source_offset => { + if (decoderFailed(decoder)) return .no_value; + out.* = decoder.source.offset(); + }, + .retain_continuation => { + if (decoderFailed(decoder)) return .no_value; + out.* = decoder.retain_continuation; + }, + + // READY metadata remains available after history decoding completes. + .history_rows_primary => { + const metadata = decoderMetadata(decoder) orelse return .no_value; + out.* = metadata.history_rows.get(.primary) orelse return .no_value; + }, + .history_rows_alternate => { + const metadata = decoderMetadata(decoder) orelse return .no_value; + out.* = metadata.history_rows.get(.alternate) orelse + return .no_value; + }, + + // Progress describes only the most recent successful `next` call. + .progress_screen => { + const progress = decoderProgress(decoder) orelse return .no_value; + out.* = progress.key; + }, + .progress_rows => { + const progress = decoderProgress(decoder) orelse return .no_value; + out.* = progress.rows; + }, + .progress_remaining => { + const progress = decoderProgress(decoder) orelse return .no_value; + out.* = progress.remaining; + }, + else => return .invalid_value, + } + return .success; +} + +/// Return whether a consuming operation has permanently failed. +fn decoderFailed(decoder: *const DecoderWrapper) bool { + return switch (decoder.state) { + .failed => true, + else => false, + }; +} + +/// Return READY metadata in every state where it remains meaningful. +fn decoderMetadata( + decoder: *const DecoderWrapper, +) ?DecoderWrapper.Metadata { + return switch (decoder.state) { + .history => |value| value.metadata, + .finished => |value| value, + .failed => |value| value, + .configuring => null, + }; +} + +/// Return page progress only during incremental history decoding. +fn decoderProgress( + decoder: *const DecoderWrapper, +) ?snapshot_core.Decoder.Progress { + return switch (decoder.state) { + .history => |value| value.progress, + else => null, + }; +} + +/// Query several typed decoder values in order, stopping at the first error. +pub fn decoder_get_multi( + decoder_: Decoder, + count: usize, + keys_: ?[*]const DecoderData, + values_: ?[*]?*anyopaque, + out_written: ?*usize, +) callconv(lib.calling_conv) Result { + // Invalid arrays and first-key failures both report zero completed writes. + if (out_written) |written| written.* = 0; + const keys = keys_ orelse return .invalid_value; + const values = values_ orelse return .invalid_value; + + // Reuse the single-key path so availability rules remain identical. + for (0..count) |i| { + const result = decoder_get(decoder_, keys[i], values[i]); + if (result != .success) { + if (out_written) |written| written.* = i; + return result; + } + } + if (out_written) |written| written.* = count; + return .success; +} + +/// Decode through READY and return a renderable terminal for incremental use. +pub fn decoder_ready( + decoder_: Decoder, + out_: ?*terminal_c.Terminal, +) callconv(lib.calling_conv) Result { + // Validate the output and require an untouched decoder lifecycle. + const out = out_ orelse return .invalid_value; + out.* = null; + const decoder = decoder_ orelse return .invalid_value; + switch (decoder.state) { + .configuring => {}, + else => return .invalid_value, + } + // Decode READY and transfer the decoded state into a C-owned terminal. + const ready = decoderReadyTerminal(decoder) catch |err| { + decoder.state = .{ .failed = null }; + return decoderMapError(decoder, err); + }; + + // Keep only the terminal handle and queryable metadata for page decoding. + decoder.state = .{ .history = .{ + .terminal = ready.terminal, + .metadata = ready.metadata, + } }; + out.* = ready.terminal; + return .success; +} + +/// Decode and apply exactly one history page, or validate FINISH. +pub fn decoder_next( + decoder_: Decoder, +) callconv(lib.calling_conv) Result { + // FINISH is idempotent; every other non-history state rejects `next`. + const decoder = decoder_ orelse return .invalid_value; + switch (decoder.state) { + .finished => return .no_value, + .history => {}, + else => return .invalid_value, + } + + // Resolve the terminal retained by READY and clear stale page progress. + const history = &decoder.state.history; + const terminal = history.terminal orelse return .invalid_value; + const native = terminal_c.zigTerminal(terminal).?; + history.progress = null; + + // Consume one validated record and preserve READY metadata on failure. + const progress = decoder.decoder.next(decoder.alloc, native) catch |err| { + const metadata = history.metadata; + decoder.state = .{ .failed = metadata }; + return decoderMapError(decoder, err); + }; + + // Publish page progress, or transition permanently to validated FINISH. + if (progress) |value| { + history.progress = value; + return .success; + } + + const metadata = history.metadata; + decoder.state = .{ .finished = metadata }; + return .no_value; +} + +/// Decode and validate the complete snapshot transactionally. +pub fn decoder_decode( + decoder_: Decoder, + out_: ?*terminal_c.Terminal, +) callconv(lib.calling_conv) Result { + // Validate the output and require an untouched decoder lifecycle. + const out = out_ orelse return .invalid_value; + out.* = null; + const decoder = decoder_ orelse return .invalid_value; + switch (decoder.state) { + .configuring => {}, + else => return .invalid_value, + } + // Build the terminal from the renderable READY prefix. + const ready = decoderReadyTerminal(decoder) catch |err| { + decoder.state = .{ .failed = null }; + return decoderMapError(decoder, err); + }; + const native = terminal_c.zigTerminal(ready.terminal).?; + + // Apply every history page and require a valid FINISH record. + while (true) { + const progress = decoder.decoder.next(decoder.alloc, native) catch |err| { + terminal_c.free(ready.terminal); + decoder.state = .{ .failed = ready.metadata }; + return decoderMapError(decoder, err); + }; + if (progress == null) break; + } + + // Publish the terminal only after the entire snapshot succeeds. + decoder.state = .{ .finished = ready.metadata }; + out.* = ready.terminal; + return .success; +} + +/// Values produced while moving a core READY result into a C terminal. +const ReadyTerminal = struct { + terminal: terminal_c.Terminal, + metadata: DecoderWrapper.Metadata, +}; + +/// Decode READY with terminal-owned I/O and construct the C terminal. +fn decoderReadyTerminal(decoder: *DecoderWrapper) anyerror!ReadyTerminal { + const io: terminal_c.Io = .init; + var decoded = try decoder.decoder.ready( + decoder.alloc, + io.io(), + .{ .max_continuation_bytes = decoder.max_continuation_bytes }, + ); + defer decoded.deinit(decoder.alloc); + + // Copy small query metadata before `fromDecoded` consumes the core result. + const metadata: DecoderWrapper.Metadata = .{ + .history_rows = decoded.history_rows, + }; + + // Move terminal state and I/O into their final C-owned allocation. + const terminal = try terminal_c.fromDecoded( + decoder.alloc, + io, + &decoded, + if (decoder.retain_continuation) + decoder.max_continuation_bytes + else + 0, + ); + return .{ .terminal = terminal, .metadata = metadata }; +} + +/// Map decoder and source-adapter failures to public C result codes. +fn decoderMapError(decoder: *DecoderWrapper, err: anyerror) Result { + // Callback protocol failures are more specific than the core read error. + switch (decoder.source) { + .callback => |adapter| { + if (adapter.invalid_read) return .invalid_value; + if (adapter.callback_failed) return .io_error; + }, + .buffer => {}, + } + + // A successful zero-byte read is clean EOF by contract. Reaching it before + // the required marker therefore means truncated snapshot data, not an + // external I/O failure, and deliberately maps to invalid_value below. + return switch (err) { + error.OutOfMemory => .out_of_memory, + error.ContinuationLimitExceeded => .limit_exceeded, + error.ContinuationDisabled, + error.ContinuationUnavailable, + error.DecoderNotReady, + error.DecoderFailed, + => .invalid_value, + error.InvalidContinuation => .invalid_value, + else => .invalid_value, + }; +} + +/// Export the allocator-owned continuation required by snapshot encoding. +fn continuationOwned(terminal: terminal_c.Terminal) struct { + result: Result, + bytes: []u8, +} { + // The terminal allocator also owns the temporary continuation copy. + const native = terminal_c.zigTerminal(terminal) orelse return .{ + .result = .invalid_value, + .bytes = &.{}, + }; + const alloc = native.gpa(); + + // Snapshotting can prove an untracked ground stream needs no replay bytes; + // only non-ground streams require tracking to have retained their prefix. + const bytes = terminal_c.continuationAllocIo( + terminal, + alloc, + true, + ) catch |err| { + return .{ + .result = switch (err) { + error.InvalidValue => .invalid_value, + error.OutOfMemory => .out_of_memory, + error.ContinuationDisabled, + error.ContinuationUnavailable, + => .invalid_value, + }, + .bytes = &.{}, + }; + }; + return .{ .result = .success, .bytes = bytes }; +} + +/// Convert exported bytes to the core snapshot continuation representation. +fn continuationValue(bytes: []const u8) snapshot_core.Continuation { + return if (bytes.len == 0) .ground else .{ .bytes = bytes }; +} + +/// Encode a terminal snapshot to a synchronous C writer callback. +pub fn encode( + terminal: terminal_c.Terminal, + writer: io_c.Writer, +) callconv(lib.calling_conv) Result { + // Validate both handles and preflight the terminal continuation. + const native = terminal_c.zigTerminal(terminal) orelse return .invalid_value; + if (writer.write == null) return .invalid_value; + const continuation = continuationOwned(terminal); + if (continuation.result != .success) return continuation.result; + defer native.gpa().free(continuation.bytes); + + // Stream the snapshot through the callback adapter, batching the + // encoder's writes into few callback invocations. + var buffer: [io_c.WriterAdapter.recommended_buffer_len]u8 = undefined; + var adapter: io_c.WriterAdapter = .initBuffered(writer, &buffer); + snapshot_core.encode( + native.gpa(), + &adapter.interface, + native, + .{ .continuation = continuationValue(continuation.bytes) }, + ) catch |err| return mapEncodeError(err, &adapter); + adapter.interface.flush() catch + return mapEncodeError(error.WriteFailed, &adapter); + return .success; +} + +/// Encode a terminal snapshot into a caller-provided fixed buffer. +pub fn encode_buf( + terminal: terminal_c.Terminal, + buf: ?[*]u8, + buf_len: usize, + out_written_: ?*usize, +) callconv(lib.calling_conv) Result { + // Validate output storage and preflight the terminal continuation. + const out_written = out_written_ orelse return .invalid_value; + out_written.* = 0; + if (buf == null and buf_len != 0) return .invalid_value; + const native = terminal_c.zigTerminal(terminal) orelse return .invalid_value; + const continuation = continuationOwned(terminal); + if (continuation.result != .success) return continuation.result; + defer native.gpa().free(continuation.bytes); + + // A null size query becomes a zero-capacity fixed writer and follows the + // same WriteFailed counting path as every other undersized destination. + const destination: []u8 = if (buf) |ptr| ptr[0..buf_len] else &.{}; + var writer: std.Io.Writer = .fixed(destination); + snapshot_core.encode( + native.gpa(), + &writer, + native, + .{ .continuation = continuationValue(continuation.bytes) }, + ) catch |err| switch (err) { + error.WriteFailed => { + // The fixed writer may already contain a valid snapshot prefix. + // Repeat only on this failure path to report the exact capacity. + var counter: std.Io.Writer.Discarding = .init(&.{}); + snapshot_core.encode( + native.gpa(), + &counter.writer, + native, + .{ .continuation = continuationValue(continuation.bytes) }, + ) catch |count_err| return mapEncodeError(count_err, null); + out_written.* = std.math.cast(usize, counter.count) orelse + return .limit_exceeded; + return .out_of_space; + }, + else => return mapEncodeError(err, null), + }; + out_written.* = writer.end; + return .success; +} + +/// Encode a terminal snapshot into a newly allocated C-owned buffer. +pub fn encode_alloc( + terminal: terminal_c.Terminal, + alloc_: ?*const CAllocator, + out_ptr_: ?*?[*]u8, + out_len_: ?*usize, +) callconv(lib.calling_conv) Result { + // Initialize outputs before validating terminal state or allocating bytes. + const out_ptr = out_ptr_ orelse return .invalid_value; + const out_len = out_len_ orelse return .invalid_value; + out_ptr.* = null; + out_len.* = 0; + + // Preflight continuation state before building an allocating writer. + const native = terminal_c.zigTerminal(terminal) orelse return .invalid_value; + const continuation = continuationOwned(terminal); + if (continuation.result != .success) return continuation.result; + defer native.gpa().free(continuation.bytes); + + // Encode once, then transfer ownership of the writer's completed slice. + const alloc = lib.alloc.default(alloc_); + var writer: std.Io.Writer.Allocating = .init(alloc); + defer writer.deinit(); + snapshot_core.encode( + native.gpa(), + &writer.writer, + native, + .{ .continuation = continuationValue(continuation.bytes) }, + ) catch |err| return mapEncodeError(err, null); + const bytes = writer.toOwnedSlice() catch return .out_of_memory; + out_ptr.* = bytes.ptr; + out_len.* = bytes.len; + return .success; +} + +/// Map core encoder and callback-adapter failures to public result codes. +fn mapEncodeError( + err: anyerror, + adapter: ?*const io_c.WriterAdapter, +) Result { + // Callback validation happens before encoding, so invalid_write can only + // mean output accounting overflow; callback_failed is external I/O. + if (adapter) |value| { + if (value.invalid_write) return .limit_exceeded; + if (value.callback_failed) return .io_error; + } + + // Fixed and allocating writers surface only core allocation or size errors. + return switch (err) { + error.OutOfMemory, error.WriteFailed => .out_of_memory, + error.Overflow, + error.PayloadTooLarge, + error.PageCountOverflow, + error.ScrollbackLimitOverflow, + error.StringTooLong, + => .limit_exceeded, + else => .invalid_value, + }; +} + +/// Enable continuation tracking for tests that encode C terminal snapshots. +fn testEnableContinuation(terminal: terminal_c.Terminal) !void { + const limit: usize = default_max_continuation_bytes; + try testing.expectEqual(Result.success, terminal_c.set( + terminal, + .continuation_max_bytes, + &limit, + )); +} + +test "decoder option and empty source" { + var decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new_buf( + &lib.alloc.test_allocator, + &decoder, + null, + 0, + )); + defer decoder_free(decoder); + + var limit: usize = 0; + try testing.expectEqual(Result.success, decoder_get( + decoder, + .max_continuation_bytes, + &limit, + )); + try testing.expectEqual(default_max_continuation_bytes, limit); + + limit = 1234; + try testing.expectEqual(Result.success, decoder_set( + decoder, + .max_continuation_bytes, + &limit, + )); + limit = 0; + try testing.expectEqual(Result.success, decoder_get( + decoder, + .max_continuation_bytes, + &limit, + )); + try testing.expectEqual(1234, limit); + + var retain = true; + try testing.expectEqual(Result.success, decoder_get( + decoder, + .retain_continuation, + &retain, + )); + try testing.expect(!retain); + retain = true; + try testing.expectEqual(Result.success, decoder_set( + decoder, + .retain_continuation, + &retain, + )); + retain = false; + try testing.expectEqual(Result.success, decoder_get( + decoder, + .retain_continuation, + &retain, + )); + try testing.expect(retain); + + var written: usize = 99; + try testing.expectEqual(Result.invalid_value, decoder_get_multi( + decoder, + 1, + null, + null, + &written, + )); + try testing.expectEqual(@as(usize, 0), written); + + var terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.invalid_value, decoder_decode( + decoder, + &terminal, + )); + try testing.expectEqual(null, terminal); +} + +test "snapshot C API full round trip restores continuation" { + var source: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &source, + 20, + 4, + )); + defer terminal_c.free(source); + try testEnableContinuation(source); + terminal_c.vt_write(source, "hello\x1b[31", "hello\x1b[31".len); + + var encoded_ptr: ?[*]u8 = null; + var encoded_len: usize = 0; + try testing.expectEqual(Result.success, encode_alloc( + source, + &lib.alloc.test_allocator, + &encoded_ptr, + &encoded_len, + )); + const encoded = encoded_ptr.?[0..encoded_len]; + defer lib.alloc.default(&lib.alloc.test_allocator).free(encoded); + + var decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new_buf( + &lib.alloc.test_allocator, + &decoder, + encoded.ptr, + encoded.len, + )); + defer decoder_free(decoder); + + var restored: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, decoder_decode(decoder, &restored)); + defer terminal_c.free(restored); + + const source_text = try terminal_c.zigTerminal(source).?.plainString(testing.allocator); + defer testing.allocator.free(source_text); + const restored_text = try terminal_c.zigTerminal(restored).?.plainString(testing.allocator); + defer testing.allocator.free(restored_text); + try testing.expectEqualStrings(source_text, restored_text); + + var restored_limit: usize = std.math.maxInt(usize); + try testing.expectEqual(Result.success, terminal_c.get( + restored, + .continuation_max_bytes, + &restored_limit, + )); + try testing.expectEqual(@as(usize, 0), restored_limit); + + // Parser state is restored, but its tracking policy is not. The terminal + // cannot be snapshotted again until the restored sequence reaches ground. + var restored_required: usize = 0; + try testing.expectEqual(Result.invalid_value, encode_buf( + restored, + null, + 0, + &restored_required, + )); + terminal_c.vt_write(restored, "m", 1); + try testing.expectEqual(Result.out_of_space, encode_buf( + restored, + null, + 0, + &restored_required, + )); + try testing.expect(restored_required > 0); + + var offset: usize = 0; + try testing.expectEqual(Result.success, decoder_get( + decoder, + .source_offset, + &offset, + )); + try testing.expectEqual(encoded.len, offset); + try testing.expectEqual(Result.no_value, decoder_next(decoder)); + + var retained_decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new_buf( + &lib.alloc.test_allocator, + &retained_decoder, + encoded.ptr, + encoded.len, + )); + defer if (retained_decoder != null) decoder_free(retained_decoder); + const retained_limit: usize = 1024; + try testing.expectEqual(Result.success, decoder_set( + retained_decoder, + .max_continuation_bytes, + &retained_limit, + )); + const retain = true; + try testing.expectEqual(Result.success, decoder_set( + retained_decoder, + .retain_continuation, + &retain, + )); + + // The returned tracker owns the exact decoded continuation, so the + // decoder can be freed before callers export those terminal-owned bytes. + var retained: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, decoder_decode( + retained_decoder, + &retained, + )); + decoder_free(retained_decoder); + retained_decoder = null; + defer terminal_c.free(retained); + + var retained_terminal_limit: usize = 0; + try testing.expectEqual(Result.success, terminal_c.get( + retained, + .continuation_max_bytes, + &retained_terminal_limit, + )); + try testing.expectEqual(retained_limit, retained_terminal_limit); + var continuation_buf: [16]u8 = undefined; + var continuation_len: usize = 0; + try testing.expectEqual(Result.success, terminal_c.continuation_buf( + retained, + &continuation_buf, + continuation_buf.len, + &continuation_len, + )); + try testing.expectEqualStrings( + "\x1b[31", + continuation_buf[0..continuation_len], + ); + + // Disabling tracking releases the exported prefix without changing the + // unfinished parser state. The final byte still completes the SGR. + const disabled: usize = 0; + try testing.expectEqual(Result.success, terminal_c.set( + retained, + .continuation_max_bytes, + &disabled, + )); + try testing.expectEqual(Result.invalid_value, terminal_c.continuation_buf( + retained, + &continuation_buf, + continuation_buf.len, + &continuation_len, + )); + terminal_c.vt_write(retained, "m", 1); + var retained_ground = false; + try testing.expectEqual(Result.success, terminal_c.get( + retained, + .vt_ground, + &retained_ground, + )); + try testing.expect(retained_ground); + + var limited: Decoder = null; + try testing.expectEqual(Result.success, decoder_new_buf( + &lib.alloc.test_allocator, + &limited, + encoded.ptr, + encoded.len, + )); + defer decoder_free(limited); + const limit: usize = 3; + try testing.expectEqual(Result.success, decoder_set( + limited, + .max_continuation_bytes, + &limit, + )); + var rejected: terminal_c.Terminal = null; + try testing.expectEqual(Result.limit_exceeded, decoder_decode( + limited, + &rejected, + )); + try testing.expectEqual(null, rejected); + try testing.expectEqual(Result.invalid_value, decoder_set( + limited, + .max_continuation_bytes, + &limit, + )); + var failed_offset: usize = 0; + try testing.expectEqual(Result.no_value, decoder_get( + limited, + .source_offset, + &failed_offset, + )); + + try testing.expectEqual(Result.success, terminal_c.set( + source, + .continuation_max_bytes, + &disabled, + )); + var required: usize = 0; + try testing.expectEqual(Result.invalid_value, encode_buf( + source, + null, + 0, + &required, + )); +} + +test "snapshot decoder retains every supported continuation class" { + const cases = [_]struct { + name: []const u8, + prefix: []const u8, + suffix: []const u8, + }{ + .{ .name = "ground", .prefix = "", .suffix = "X" }, + .{ .name = "CSI", .prefix = "\x1b[31", .suffix = "mX" }, + .{ .name = "OSC", .prefix = "\x1b]2;title", .suffix = "\x07" }, + .{ .name = "DCS", .prefix = "\x1bP$qm", .suffix = "\x1b\\" }, + .{ .name = "APC", .prefix = "\x1b_Ga=q;payload", .suffix = "\x1b\\" }, + .{ .name = "UTF-8", .prefix = "\xF0\x9F", .suffix = "\x98\x84" }, + }; + const limit: usize = 4096; + const disabled: usize = 0; + const retain = true; + + for (cases) |case| { + errdefer std.log.err("retained continuation case failed: {s}", .{case.name}); + + var source: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &source, + 20, + 4, + )); + defer terminal_c.free(source); + try testing.expectEqual(Result.success, terminal_c.set( + source, + .continuation_max_bytes, + &limit, + )); + terminal_c.vt_write(source, case.prefix.ptr, case.prefix.len); + + var snapshot_ptr: ?[*]u8 = null; + var snapshot_len: usize = 0; + try testing.expectEqual(Result.success, encode_alloc( + source, + &lib.alloc.test_allocator, + &snapshot_ptr, + &snapshot_len, + )); + const snapshot = snapshot_ptr.?[0..snapshot_len]; + defer lib.alloc.default(&lib.alloc.test_allocator).free(snapshot); + + var decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new_buf( + &lib.alloc.test_allocator, + &decoder, + snapshot.ptr, + snapshot.len, + )); + defer if (decoder != null) decoder_free(decoder); + try testing.expectEqual(Result.success, decoder_set( + decoder, + .max_continuation_bytes, + &limit, + )); + try testing.expectEqual(Result.success, decoder_set( + decoder, + .retain_continuation, + &retain, + )); + + var restored: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, decoder_decode( + decoder, + &restored, + )); + defer terminal_c.free(restored); + decoder_free(decoder); + decoder = null; + + var restored_limit: usize = 0; + try testing.expectEqual(Result.success, terminal_c.get( + restored, + .continuation_max_bytes, + &restored_limit, + )); + try testing.expectEqual(limit, restored_limit); + + var continuation: [4096]u8 = undefined; + var continuation_len: usize = 0; + try testing.expectEqual(Result.success, terminal_c.continuation_buf( + restored, + &continuation, + continuation.len, + &continuation_len, + )); + try testing.expectEqualStrings( + case.prefix, + continuation[0..continuation_len], + ); + + // Releasing the retained bytes must not reset the parser. Both the + // source and replica should reach the same fully serializable state + // after consuming the suffix that followed the snapshot cut. + try testing.expectEqual(Result.success, terminal_c.set( + restored, + .continuation_max_bytes, + &disabled, + )); + try testing.expectEqual(Result.invalid_value, terminal_c.continuation_buf( + restored, + &continuation, + continuation.len, + &continuation_len, + )); + terminal_c.vt_write(source, case.suffix.ptr, case.suffix.len); + terminal_c.vt_write(restored, case.suffix.ptr, case.suffix.len); + + var source_ground = false; + var restored_ground = false; + try testing.expectEqual(Result.success, terminal_c.get( + source, + .vt_ground, + &source_ground, + )); + try testing.expectEqual(Result.success, terminal_c.get( + restored, + .vt_ground, + &restored_ground, + )); + try testing.expect(source_ground); + try testing.expect(restored_ground); + + var source_final_ptr: ?[*]u8 = null; + var source_final_len: usize = 0; + try testing.expectEqual(Result.success, encode_alloc( + source, + &lib.alloc.test_allocator, + &source_final_ptr, + &source_final_len, + )); + const source_final = source_final_ptr.?[0..source_final_len]; + defer lib.alloc.default(&lib.alloc.test_allocator).free(source_final); + + var restored_final_ptr: ?[*]u8 = null; + var restored_final_len: usize = 0; + try testing.expectEqual(Result.success, encode_alloc( + restored, + &lib.alloc.test_allocator, + &restored_final_ptr, + &restored_final_len, + )); + const restored_final = restored_final_ptr.?[0..restored_final_len]; + defer lib.alloc.default(&lib.alloc.test_allocator).free(restored_final); + try testing.expectEqualSlices(u8, source_final, restored_final); + } +} + +test "snapshot encoding accepts an untracked ground terminal" { + var source: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &source, + 10, + 3, + )); + defer terminal_c.free(source); + + var limit: usize = std.math.maxInt(usize); + try testing.expectEqual(Result.success, terminal_c.get( + source, + .continuation_max_bytes, + &limit, + )); + try testing.expectEqual(@as(usize, 0), limit); + + terminal_c.vt_write(source, "ground", "ground".len); + var required: usize = 0; + try testing.expectEqual(Result.out_of_space, encode_buf( + source, + null, + 0, + &required, + )); + try testing.expect(required > 0); + + // Once untracked input leaves ground, the missing prefix is unknowable. + terminal_c.vt_write(source, "\x1b[31", "\x1b[31".len); + try testing.expectEqual(Result.invalid_value, encode_buf( + source, + null, + 0, + &required, + )); +} + +test "snapshot callbacks stop at FINISH and map I/O failures" { + var source: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &source, + 10, + 3, + )); + defer terminal_c.free(source); + try testEnableContinuation(source); + terminal_c.vt_write(source, "callback", "callback".len); + + var required: usize = 0; + try testing.expectEqual(Result.out_of_space, encode_buf( + source, + null, + 0, + &required, + )); + const buffer_encoded = try testing.allocator.alloc(u8, required); + defer testing.allocator.free(buffer_encoded); + var buffer_written: usize = 0; + try testing.expectEqual(Result.success, encode_buf( + source, + buffer_encoded.ptr, + buffer_encoded.len, + &buffer_written, + )); + try testing.expectEqual(required, buffer_written); + + // A non-null undersized buffer takes the fixed-first path. It may contain + // the prefix written before capacity exhaustion while still reporting the + // exact complete size. + var short: [10]u8 = @splat(0xAA); + var short_required: usize = 0; + try testing.expectEqual(Result.out_of_space, encode_buf( + source, + &short, + short.len, + &short_required, + )); + try testing.expectEqual(required, short_required); + try testing.expectEqualStrings("GHOSTSNP", short[0..8]); + + const encoded = try testing.allocator.alloc(u8, required); + defer testing.allocator.free(encoded); + + const WriteContext = struct { + destination: []u8, + offset: usize = 0, + + fn write( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + if (len > self.destination.len - self.offset) return false; + @memcpy(self.destination[self.offset..][0..len], data[0..len]); + self.offset += len; + return true; + } + }; + var write_context: WriteContext = .{ .destination = encoded }; + try testing.expectEqual(Result.success, encode(source, .{ + .write = &WriteContext.write, + .userdata = &write_context, + })); + try testing.expectEqual(required, write_context.offset); + try testing.expectEqualSlices(u8, buffer_encoded, encoded); + + var failing = testing.FailingAllocator.init(testing.allocator, .{ + .fail_index = 0, + }); + const failing_zig = failing.allocator(); + const failing_c: CAllocator = .fromZig(&failing_zig); + var failed_ptr: ?[*]u8 = null; + var failed_len: usize = 99; + try testing.expectEqual(Result.out_of_memory, encode_alloc( + source, + &failing_c, + &failed_ptr, + &failed_len, + )); + try testing.expectEqual(null, failed_ptr); + try testing.expectEqual(@as(usize, 0), failed_len); + + const FailWriter = struct { + fn write(_: ?*anyopaque, _: [*]const u8, _: usize) callconv(lib.calling_conv) bool { + return false; + } + }; + try testing.expectEqual(Result.io_error, encode(source, .{ + .write = &FailWriter.write, + })); + + const combined = try testing.allocator.alloc(u8, encoded.len + 4); + defer testing.allocator.free(combined); + @memcpy(combined[0..encoded.len], encoded); + @memcpy(combined[encoded.len..], "tail"); + + const ReadContext = struct { + source: []const u8, + offset: usize = 0, + + fn read( + userdata: ?*anyopaque, + destination: [*]u8, + capacity: usize, + out_read: *usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + const remaining = self.source[self.offset..]; + const len = @min(remaining.len, capacity, 3); + @memcpy(destination[0..len], remaining[0..len]); + self.offset += len; + out_read.* = len; + return true; + } + }; + var read_context: ReadContext = .{ .source = combined }; + var decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new( + &lib.alloc.test_allocator, + &decoder, + .{ .read = &ReadContext.read, .userdata = &read_context }, + )); + defer decoder_free(decoder); + var restored: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, decoder_decode(decoder, &restored)); + defer terminal_c.free(restored); + try testing.expectEqual(encoded.len, read_context.offset); + try testing.expectEqualStrings("tail", read_context.source[read_context.offset..]); + + const FailReader = struct { + fn read( + _: ?*anyopaque, + _: [*]u8, + _: usize, + _: *usize, + ) callconv(lib.calling_conv) bool { + return false; + } + }; + var failed_decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new( + &lib.alloc.test_allocator, + &failed_decoder, + .{ .read = &FailReader.read }, + )); + defer decoder_free(failed_decoder); + var failed_terminal: terminal_c.Terminal = null; + try testing.expectEqual(Result.io_error, decoder_decode( + failed_decoder, + &failed_terminal, + )); + + const EofReader = struct { + fn read( + _: ?*anyopaque, + _: [*]u8, + _: usize, + out_read: *usize, + ) callconv(lib.calling_conv) bool { + out_read.* = 0; + return true; + } + }; + var eof_decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new( + &lib.alloc.test_allocator, + &eof_decoder, + .{ .read = &EofReader.read }, + )); + defer decoder_free(eof_decoder); + // Successful EOF before READY/FINISH is truncated snapshot data. It is + // intentionally INVALID_VALUE; only a false callback is IO_ERROR. + try testing.expectEqual(Result.invalid_value, decoder_decode( + eof_decoder, + &failed_terminal, + )); + + const InvalidReader = struct { + fn read( + _: ?*anyopaque, + _: [*]u8, + capacity: usize, + out_read: *usize, + ) callconv(lib.calling_conv) bool { + out_read.* = capacity + 1; + return true; + } + }; + var invalid_decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new( + &lib.alloc.test_allocator, + &invalid_decoder, + .{ .read = &InvalidReader.read }, + )); + defer decoder_free(invalid_decoder); + try testing.expectEqual(Result.invalid_value, decoder_decode( + invalid_decoder, + &failed_terminal, + )); +} + +test "snapshot incremental decoder exposes READY and page progress" { + var source: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, terminal_c.new( + &lib.alloc.test_allocator, + &source, + 215, + 2, + )); + defer terminal_c.free(source); + try testEnableContinuation(source); + // Keep multiple complete pages. The ordinary C-terminal byte limit keeps + // at least one page but can evict older ones before snapshot encoding. + try testing.expectEqual(Result.success, terminal_c.set( + source, + .scrollback_max_bytes, + null, + )); + const first_page_rows = terminal_c.zigTerminal(source).?.screens + .get(.primary).?.pages.pages.first.?.capacity().rows; + for (0..@as(usize, first_page_rows) * 3) |_| + terminal_c.vt_write(source, "x\r\n", 3); + const ready_continuation = "\x1b]2;ready"; + terminal_c.vt_write( + source, + ready_continuation, + ready_continuation.len, + ); + + var encoded_ptr: ?[*]u8 = null; + var encoded_len: usize = 0; + try testing.expectEqual(Result.success, encode_alloc( + source, + &lib.alloc.test_allocator, + &encoded_ptr, + &encoded_len, + )); + const encoded = encoded_ptr.?[0..encoded_len]; + defer lib.alloc.default(&lib.alloc.test_allocator).free(encoded); + + var decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new_buf( + &lib.alloc.test_allocator, + &decoder, + encoded.ptr, + encoded.len, + )); + defer decoder_free(decoder); + const retained_limit: usize = 1024; + try testing.expectEqual(Result.success, decoder_set( + decoder, + .max_continuation_bytes, + &retained_limit, + )); + const retain = true; + try testing.expectEqual(Result.success, decoder_set( + decoder, + .retain_continuation, + &retain, + )); + + var restored: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, decoder_ready(decoder, &restored)); + defer terminal_c.free(restored); + + // READY applies the same retention policy as one-shot decode and makes + // the unfinished snapshot continuation immediately exportable. + var restored_limit: usize = 0; + try testing.expectEqual(Result.success, terminal_c.get( + restored, + .continuation_max_bytes, + &restored_limit, + )); + try testing.expectEqual(retained_limit, restored_limit); + var ready_buf: [32]u8 = undefined; + var ready_len: usize = 0; + try testing.expectEqual(Result.success, terminal_c.continuation_buf( + restored, + &ready_buf, + ready_buf.len, + &ready_len, + )); + try testing.expectEqualStrings( + ready_continuation, + ready_buf[0..ready_len], + ); + + var history_rows: u64 = 0; + try testing.expectEqual(Result.success, decoder_get( + decoder, + .history_rows_primary, + &history_rows, + )); + try testing.expect(history_rows > 0); + + var page_count: usize = 0; + while (true) { + const result = decoder_next(decoder); + if (result == .no_value) break; + try testing.expectEqual(Result.success, result); + page_count += 1; + + var screen: terminal_c.TerminalScreen = undefined; + var rows: usize = 0; + var remaining: u32 = 0; + const keys = [_]DecoderData{ + .progress_screen, + .progress_rows, + .progress_remaining, + }; + const values = [_]?*anyopaque{ &screen, &rows, &remaining }; + var written: usize = 0; + try testing.expectEqual(Result.success, decoder_get_multi( + decoder, + keys.len, + &keys, + @constCast(&values), + &written, + )); + try testing.expectEqual(keys.len, written); + try testing.expectEqual(terminal_c.TerminalScreen.primary, screen); + try testing.expect(rows > 0); + } + try testing.expect(page_count > 0); + var rows: usize = 0; + try testing.expectEqual(Result.no_value, decoder_get( + decoder, + .progress_rows, + &rows, + )); + try testing.expectEqual(Result.no_value, decoder_next(decoder)); + + var source_total: usize = 0; + var restored_total: usize = 0; + try testing.expectEqual(Result.success, terminal_c.get( + source, + .total_rows, + &source_total, + )); + try testing.expectEqual(Result.success, terminal_c.get( + restored, + .total_rows, + &restored_total, + )); + try testing.expectEqual(source_total, restored_total); + + var dropped_decoder: Decoder = null; + try testing.expectEqual(Result.success, decoder_new_buf( + &lib.alloc.test_allocator, + &dropped_decoder, + encoded.ptr, + encoded.len, + )); + defer decoder_free(dropped_decoder); + var dropped: terminal_c.Terminal = null; + try testing.expectEqual(Result.success, decoder_ready( + dropped_decoder, + &dropped, + )); + defer terminal_c.free(dropped); + try testing.expectEqual(Result.success, terminal_c.resize( + dropped, + 214, + 2, + 0, + 0, + )); + try testing.expectEqual(Result.success, decoder_next(dropped_decoder)); + var dropped_rows: usize = 1; + try testing.expectEqual(Result.success, decoder_get( + dropped_decoder, + .progress_rows, + &dropped_rows, + )); + try testing.expectEqual(@as(usize, 0), dropped_rows); + while (decoder_next(dropped_decoder) == .success) {} + try testing.expectEqual(Result.no_value, decoder_next(dropped_decoder)); +} diff --git a/vendor/libghostty-vt/src/terminal/c/style.zig b/vendor/libghostty-vt/src/terminal/c/style.zig index 82729fa9..bea4d074 100644 --- a/vendor/libghostty-vt/src/terminal/c/style.zig +++ b/vendor/libghostty-vt/src/terminal/c/style.zig @@ -25,20 +25,32 @@ pub const Color = extern struct { tag: ColorTag, value: ColorValue, + // fromColor converts the tag as a plain integer, which requires + // the internal and C tag values to line up. + comptime { + const Tag = std.meta.Tag(style.Style.Color); + for (@typeInfo(Tag).@"enum".fields) |f| { + assert(f.value == @intFromEnum(@field(ColorTag, f.name))); + } + } + pub fn fromColor(c: style.Style.Color) Color { - return switch (c) { - .none => .{ - .tag = .none, - .value = .{ ._padding = 0 }, - }, - .palette => |idx| .{ - .tag = .palette, - .value = .{ .palette = idx }, - }, - .rgb => |rgb| .{ - .tag = .rgb, - .value = .{ .rgb = rgb.cval() }, - }, + // The value is built as a single integer rather than through + // the per-variant union fields: the C union layout puts + // .palette at byte 0 and .rgb at bytes 0-2, which is exactly + // the internal payload representation, so each variant is the + // payload zero-extended to the u64 union backing. This keeps + // the conversion to two stores (tag + value) per color, which + // matters for the per-cell style reads in the render state + // API. + const value: u64 = switch (c) { + .none => 0, + .palette => |idx| idx, + .rgb => |rgb| @as(u24, @bitCast(rgb)), + }; + return .{ + .tag = @enumFromInt(@intFromEnum(std.meta.activeTag(c))), + .value = .{ ._padding = value }, }; } }; @@ -59,27 +71,83 @@ pub const Style = extern struct { overline: bool, underline: c_int, - pub fn fromStyle(s: style.Style) Style { - return .{ - .fg_color = .fromColor(s.fg_color), - .bg_color = .fromColor(s.bg_color), - .underline_color = .fromColor(s.underline_color), - .bold = s.flags.bold, - .italic = s.flags.italic, - .faint = s.flags.faint, - .blink = s.flags.blink, - .inverse = s.flags.inverse, - .invisible = s.flags.invisible, - .strikethrough = s.flags.strikethrough, - .overline = s.flags.overline, - .underline = @intFromEnum(s.flags.underline), + /// The default (empty) style in C representation. C + pub const default: Style = .{ + .fg_color = .{ .tag = .none, .value = .{ ._padding = 0 } }, + .bg_color = .{ .tag = .none, .value = .{ ._padding = 0 } }, + .underline_color = .{ .tag = .none, .value = .{ ._padding = 0 } }, + .bold = false, + .italic = false, + .faint = false, + .blink = false, + .inverse = false, + .invisible = false, + .strikethrough = false, + .overline = false, + .underline = 0, + }; + + // fromStyle writes the eight bool fields as one 8-byte store, + // which requires them to be consecutive and to match the low + // eight flag bits in order. + comptime { + const flag_names = [8][:0]const u8{ + "bold", "italic", "faint", "blink", + "inverse", "invisible", "strikethrough", "overline", }; + const base = @offsetOf(Style, "bold"); + for (flag_names, 0..) |name, i| { + assert(@offsetOf(Style, name) == base + i); + assert(@bitOffsetOf(@FieldType(style.Style, "flags"), name) == i); + } + } + + /// Write the C representation of the given style directly through + /// the out pointer. The hot per-cell style read in the render + /// state API uses this rather than `out.* = fromStyle(s)`: the + /// by-value form materializes a stack temporary plus a + /// @sizeOf(Style)-byte copy that LLVM does not elide. + pub fn write(s: style.Style, out: *Style) void { + out.size = @sizeOf(Style); + out.fg_color = .fromColor(s.fg_color); + out.bg_color = .fromColor(s.bg_color); + out.underline_color = .fromColor(s.underline_color); + + // I know this looks scary, but this results in truly measurable + // performance improvements due to how often styles are written + // especially in render loops. We vectorize our 8-bit to 8-byte + // write. + // + // We have to align(1) because we're ptrCasting a u8 pointer. + @as(*align(1) u64, @ptrCast(&out.bold)).* = bytes: { + // Spread the low eight flag bits (the eight bool fields, in + // order) into one byte each and write them with a single + // 8-byte store. + const flag_bits: u8 = @truncate(@as(u16, @bitCast(s.flags))); + const masks: @Vector(8, u8) = .{ 1, 2, 4, 8, 16, 32, 64, 128 }; + const hits = (@as(@Vector(8, u8), @splat(flag_bits)) & masks) == masks; + const bytes: @Vector(8, u8) = @select( + u8, + hits, + @as(@Vector(8, u8), @splat(1)), + @as(@Vector(8, u8), @splat(0)), + ); + break :bytes @bitCast(bytes); + }; + + out.underline = @intFromEnum(s.flags.underline); + } + + pub fn fromStyle(s: style.Style) Style { + var result: Style = undefined; + write(s, &result); + return result; } }; /// Returns the default style. pub fn default_style(result: *Style) callconv(lib.calling_conv) void { - result.* = .fromStyle(.{}); + result.* = .default; assert(result.size == @sizeOf(Style)); } diff --git a/vendor/libghostty-vt/src/terminal/c/sys.zig b/vendor/libghostty-vt/src/terminal/c/sys.zig index c4b2b17f..f622e8fd 100644 --- a/vendor/libghostty-vt/src/terminal/c/sys.zig +++ b/vendor/libghostty-vt/src/terminal/c/sys.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const lib = @import("../lib.zig"); const CAllocator = lib.alloc.Allocator; const terminal_sys = @import("../sys.zig"); +const stderr = @import("../../os/stderr.zig"); const Result = @import("result.zig").Result; /// C: GhosttySysImage @@ -22,6 +23,13 @@ pub const DecodePngFn = *const fn ( *Image, ) callconv(lib.calling_conv) bool; +/// C: GhosttySysRandomSecureFn +pub const RandomSecureFn = *const fn ( + ?*anyopaque, + [*]u8, + usize, +) callconv(lib.calling_conv) bool; + /// C: GhosttySysLogLevel pub const LogLevel = enum(c_int) { @"error" = 0, @@ -54,12 +62,14 @@ pub const Option = enum(c_int) { userdata = 0, decode_png = 1, log = 2, + random_secure = 3, pub fn InType(comptime self: Option) type { return switch (self) { .userdata => ?*const anyopaque, .decode_png => ?DecodePngFn, .log => ?LogFn, + .random_secure => ?RandomSecureFn, }; } }; @@ -70,6 +80,7 @@ const Global = struct { userdata: ?*anyopaque = null, decode_png: ?DecodePngFn = null, log: ?LogFn = null, + random_secure: ?RandomSecureFn = null, }; /// Global state for the C sys interface. @@ -86,7 +97,7 @@ fn decodePngWrapper( const c_alloc = CAllocator.fromZig(&alloc); var out: Image = undefined; - if (!func(global.userdata, &c_alloc, data.ptr, data.len, &out)) return error.InvalidData; + if (!func(global.userdata, &c_alloc, lib.String.init(data).ptr, data.len, &out)) return error.InvalidData; const result_data = out.data orelse return error.InvalidData; @@ -97,12 +108,18 @@ fn decodePngWrapper( }; } +/// Zig-compatible wrapper that calls through to the stored C callback. +fn randomSecureWrapper(buffer: []u8) terminal_sys.RandomSecureError!void { + const func = global.random_secure orelse return error.EntropyUnavailable; + if (!func(global.userdata, buffer.ptr, buffer.len)) return error.EntropyUnavailable; +} + pub fn set( option: Option, value: ?*const anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Option, @intFromEnum(option)) catch { + _ = std.enums.fromInt(Option, @intFromEnum(option)) orelse { return .invalid_value; }; } @@ -126,6 +143,10 @@ fn setTyped( terminal_sys.decode_png = if (value != null) &decodePngWrapper else null; }, .log => global.log = value, + .random_secure => { + global.random_secure = value; + terminal_sys.random_secure = if (value != null) &randomSecureWrapper else null; + }, } return .success; } @@ -146,37 +167,30 @@ fn emitLog(level: LogLevel, scope: []const u8, message: []const u8) void { /// Emits logs in chunks. Almost all logs will be less than the chunk size /// but this allows emitting larger logs without heap allocation. const LogEmitter = struct { + const buffer_size = 2048; + c_level: LogLevel, scope_text: []const u8, - buf: [2048]u8 = undefined, - pos: usize = 0, - - fn write(self: *@This(), bytes: []const u8) error{}!usize { - var remaining = bytes; - while (remaining.len > 0) { - const space = self.buf.len - self.pos; - if (space == 0) { - self.flush(); - continue; - } - - const n = @min(remaining.len, space); - @memcpy(self.buf[self.pos..][0..n], remaining[0..n]); - self.pos += n; - remaining = remaining[n..]; - } + writer: std.Io.Writer, - return bytes.len; - } - - fn flush(self: *@This()) void { - if (self.pos == 0) return; - emitLog( - self.c_level, - self.scope_text, - self.buf[0..self.pos], - ); - self.pos = 0; + fn drain(w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize { + switch (data.len) { + 0 => unreachable, + 1 => if (splat == 0) return 0, + else => {}, + } + const self: *@This() = @fieldParentPtr("writer", w); + if (w.end > 0) { + emitLog( + self.c_level, + self.scope_text, + w.buffer[0..w.end], + ); + } + const len: usize = @min(data[0].len, w.buffer.len); + @memcpy(w.buffer[0..len], data[0][0..len]); + w.end = len; + return len; } }; @@ -197,18 +211,20 @@ pub fn logFn( const scope_text: []const u8 = if (scope == .default) "" else @tagName(scope); const c_level = LogLevel.fromStd(level); - var ctx: LogEmitter = .{ + var log_emitter_buffer: [LogEmitter.buffer_size]u8 = undefined; + var log_emitter: LogEmitter = .{ .c_level = c_level, .scope_text = scope_text, + .writer = .{ + .buffer = &log_emitter_buffer, + .vtable = &.{ + .drain = LogEmitter.drain, + }, + }, }; - const writer: std.io.GenericWriter( - *LogEmitter, - error{}, - LogEmitter.write, - ) = .{ .context = &ctx }; - - nosuspend writer.print(format, args) catch {}; - ctx.flush(); + + nosuspend log_emitter.writer.print(format, args) catch {}; + log_emitter.writer.flush() catch {}; } /// Built-in log callback that writes to stderr. @@ -216,8 +232,12 @@ pub fn logFn( /// Formats each message as "[level](scope): message\n". Can be passed /// directly to ghostty_sys_set(GHOSTTY_SYS_OPT_LOG, &ghostty_sys_log_stderr). /// -/// Uses std.debug.lockStderrWriter for thread-safe, mutex-protected output. -/// On freestanding/wasm targets this is a no-op (no stderr available). +/// Each log line is emitted with a single raw write to stderr, which keeps +/// concurrent log lines from interleaving. We intentionally avoid +/// `std.debug.lockStderr` because it routes through `std.Options.debug_io` +/// and would keep the entire `std.Io.Threaded` vtable alive in the binary +/// (see `os/stderr.zig`). On freestanding/wasm targets this is a no-op +/// (no stderr available). pub fn logStderr( _: ?*anyopaque, level: LogLevel, @@ -238,16 +258,32 @@ pub fn logStderr( .debug => "debug", }; - var buffer: [64]u8 = undefined; - const writer = std.debug.lockStderrWriter(&buffer); - defer std.debug.unlockStderrWriter(); - nosuspend { - if (scope.len > 0) { - writer.print("[{s}]({s}): {s}\n", .{ level_text, scope, message }) catch {}; - } else { - writer.print("[{s}]: {s}\n", .{ level_text, message }) catch {}; - } + // Large enough for a full logFn chunk plus the level/scope prefix. + var buffer: [LogEmitter.buffer_size + 128]u8 = undefined; + const line: ?[]const u8 = if (scope.len > 0) + std.fmt.bufPrint(&buffer, "[{s}]({s}): {s}\n", .{ level_text, scope, message }) catch null + else + std.fmt.bufPrint(&buffer, "[{s}]: {s}\n", .{ level_text, message }) catch null; + if (line) |v| { + stderr.write(v); + return; + } + + // The line didn't fit in our buffer (an embedder called us directly + // with a very large message). Fall back to writing the pieces + // separately; interleaving with other threads is possible here but + // this is a best-effort diagnostic path. + stderr.write("["); + stderr.write(level_text); + stderr.write("]"); + if (scope.len > 0) { + stderr.write("("); + stderr.write(scope); + stderr.write(")"); } + stderr.write(": "); + stderr.write(message); + stderr.write("\n"); } test "set decode_png with null clears" { diff --git a/vendor/libghostty-vt/src/terminal/c/terminal.zig b/vendor/libghostty-vt/src/terminal/c/terminal.zig index 0f95fcf1..73097573 100644 --- a/vendor/libghostty-vt/src/terminal/c/terminal.zig +++ b/vendor/libghostty-vt/src/terminal/c/terminal.zig @@ -1,9 +1,12 @@ +const builtin = @import("builtin"); const std = @import("std"); const testing = std.testing; const build_options = @import("terminal_options"); const lib = @import("../lib.zig"); const CAllocator = lib.alloc.Allocator; pub const ZigTerminal = @import("../Terminal.zig"); +const Action = @import("../stream.zig").Action; +const osc = @import("../osc.zig"); const Stream = @import("../stream_terminal.zig").Stream; const Screen = @import("../Screen.zig"); const ScreenSet = @import("../ScreenSet.zig"); @@ -21,24 +24,73 @@ const cell_c = @import("cell.zig"); const row_c = @import("row.zig"); const grid_ref_c = @import("grid_ref.zig"); const grid_ref_tracked_c = @import("grid_ref_tracked.zig"); +const search_c = @import("search.zig"); const selection_c = @import("selection.zig"); const style_c = @import("style.zig"); const color = @import("../color.zig"); const clipboard = @import("../clipboard.zig"); +const kitty_clipboard = @import("../kitty/clipboard.zig"); +const c_io = @import("io.zig"); +const snapshot_core = @import("../snapshot/main.zig"); const Result = @import("result.zig").Result; +const assert = @import("../../quirks.zig").inlineAssert; const Handler = @import("../stream_terminal.zig").Handler; +const max_path_bytes = if (builtin.os.tag == .freestanding) 4096 else std.fs.max_path_bytes; + const log = std.log.scoped(.terminal_c); +/// C terminals do not retain replay bytes unless the embedding application +/// opts in through `GHOSTTY_TERMINAL_OPT_CONTINUATION_MAX_BYTES`. +pub const default_continuation_max_bytes: usize = 0; + +/// Owns the `std.Io` implementation retained by every C terminal. +/// +/// Snapshot decoding creates this before the native terminal exists and +/// transfers it into the final C wrapper after READY. +/// +/// This is TinyIo on every target: it is stateless and supports exactly +/// the operations the terminal needs at a fraction of the code size of +/// `std.Io.Threaded` (see lib/TinyIo.zig), on POSIX and Windows alike. +/// On the remaining targets (e.g. freestanding wasm) TinyIo degrades to +/// `std.Io.failing`, which is correct: they have no filesystem. +pub const Io = struct { + impl: lib.TinyIo, + + pub const init: Io = .{ .impl = .init }; + + /// Return the value passed to native terminal construction and decoding. + pub fn io(self: Io) std.Io { + return self.impl.io(); + } +}; + /// Wrapper around ZigTerminal that tracks additional state for C API usage, /// such as the persistent VT stream needed to handle escape sequences split /// across multiple vt_write calls. const TerminalWrapper = struct { terminal: *ZigTerminal, + /// C construction has no I/O argument, so the wrapper retains the owner + /// created by `new` or transferred from snapshot decoding until `free`. + io: Io, + /// Allocator-owned copy of the temporary directory path for some + /// operations (e.g. kitty graphics). This is only allocated once the + /// embedder sets the option. + tmp_dir_path: ?[]u8 = null, + /// The terminfo name reported for XTGETTCAP "TN". The stream handler holds + /// a slice into this. + terminfo_name_buf: [Handler.max_terminfo_name_bytes]u8, stream: Stream, effects: Effects = .{}, tracked_grid_refs: std.AutoArrayHashMapUnmanaged(*grid_ref_tracked_c.TrackedGridRef, void) = .{}, + searches: std.AutoArrayHashMapUnmanaged(*search_c.SearchWrapper, void) = .{}, + + /// Fetches a `TerminalWrapper` reference from a `Handler`. + fn fromHandler(handler: *Handler) *TerminalWrapper { + const stream_ptr: *Stream = @fieldParentPtr("handler", handler); + return @alignCast(@fieldParentPtr("stream", stream_ptr)); + } }; /// A single MIME representation in a clipboard write. @@ -50,6 +102,8 @@ pub const ClipboardContent = extern struct { }; /// A protocol-neutral request to replace or clear clipboard contents. +/// The embedder answers by calling `reply` with the request before the +/// callback returns. /// /// C: GhosttyClipboardWrite pub const ClipboardWrite = extern struct { @@ -57,23 +111,152 @@ pub const ClipboardWrite = extern struct { location: clipboard.Location, contents: ?[*]const ClipboardContent, contents_len: usize, + name: lib.String, + granted: bool, + can_remember: bool, + /// Terminal-owned reply state; opaque to the embedder. + ctx: *const anyopaque, + reply: ClipboardWriteReplyFn, +}; + +/// The reply to a clipboard write request. +/// +/// C: GhosttyClipboardWriteReply +pub const ClipboardWriteReply = extern struct { + size: usize, + result: clipboard.Write.Status, + remember: bool, +}; + +/// C function pointer type for replying to a clipboard write. +/// +/// C: GhosttyClipboardWriteReplyFn +pub const ClipboardWriteReplyFn = *const fn (*const ClipboardWrite, *const ClipboardWriteReply) callconv(lib.calling_conv) void; + +/// The reply to a clipboard read request. +/// +/// C: GhosttyClipboardReadReply +pub const ClipboardReadReply = extern struct { + size: usize, + result: clipboard.Read.Status, + contents: ?[*]const ClipboardContent, + contents_len: usize, + available: ?[*]const lib.String, + available_len: usize, + remember: bool, +}; + +/// A synchronous request to read clipboard contents. The embedder answers +/// by calling `reply` with the request before the callback returns. +/// +/// C: GhosttyClipboardRead +pub const ClipboardRead = extern struct { + size: usize, + location: clipboard.Location, + mimes: ?[*]const lib.String, + mimes_len: usize, + list: bool, + name: lib.String, + granted: bool, + can_remember: bool, + /// Terminal-owned reply state; opaque to the embedder. + ctx: *const anyopaque, + reply: ClipboardReadReplyFn, +}; + +/// C function pointer type for replying to a clipboard read. +/// +/// C: GhosttyClipboardReadReplyFn +pub const ClipboardReadReplyFn = *const fn (*const ClipboardRead, *const ClipboardReadReply) callconv(lib.calling_conv) void; + +/// A request to show a desktop notification. +/// +/// C: GhosttyTerminalDesktopNotification +pub const DesktopNotification = extern struct { + size: usize, + title: lib.String, + body: lib.String, +}; + +/// C: GhosttyTerminalProgressState +pub const ProgressState = osc.Command.ProgressReport.State; + +/// A progress report emitted by the running program. +/// +/// C: GhosttyTerminalProgressReport +pub const ProgressReport = extern struct { + size: usize, + state: ProgressState, + progress: i8, +}; + +/// A borrowed unsupported string sequence. +/// +/// C: GhosttyTerminalUnknownStringSequence +pub const UnknownStringSequence = extern struct { + truncated: bool, + content: lib.String, +}; + +/// An unsupported terminal sequence reported to the C callback. +/// +/// C: GhosttyTerminalUnknownSequence +pub const UnknownSequence = union(Tag) { + apc: UnknownStringSequence, + + /// C: GhosttyTerminalUnknownSequenceTag + pub const Tag = lib.Enum(lib.target, &.{"apc"}); + + const c_union = lib.TaggedUnion( + lib.target, + @This(), + // A future borrowed CSI payload may need parameter, separator, and + // intermediate arrays. Reserve 128 bytes so that representation and + // other structured sequence types can be added without an ABI break. + .{ .padding = [16]u64 }, + ); + pub const C = c_union.C; + pub const CValue = c_union.CValue; + pub const cval = c_union.cval; +}; + +/// A terminal mode and boolean value used for mode configuration. +/// +/// C: GhosttyTerminalModeConfig +pub const ModeConfig = extern struct { + mode: modes.ModeTag.Backing, + value: bool, + + fn toMode(self: ModeConfig) ?modes.Mode { + const tag: modes.ModeTag = @bitCast(self.mode); + return modes.modeFromInt(tag.value, tag.ansi); + } }; -/// C callback state for terminal effects. Trampolines are always -/// installed on the stream handler; they check these fields and -/// no-op when the corresponding callback is null. +/// C callback state for terminal effects. Most trampolines are always +/// installed on the stream handler; they check these fields and no-op when +/// the corresponding callback is null. The unknown-sequence and +/// clipboard trampolines are installed dynamically to preserve their +/// null fast paths (for clipboard_write, a null Zig-level effect makes +/// Kitty clipboard writes fail up front instead of spooling a +/// transaction that can never commit; for clipboard_read it keeps +/// reads denied). const Effects = struct { userdata: ?*anyopaque = null, write_pty: ?WritePtyFn = null, bell: ?BellFn = null, color_scheme: ?ColorSchemeFn = null, + desktop_notification: ?DesktopNotificationFn = null, device_attributes_cb: ?DeviceAttributesFn = null, enquiry: ?EnquiryFn = null, xtversion: ?XtversionFn = null, title_changed: ?TitleChangedFn = null, pwd_changed: ?PwdChangedFn = null, + progress_report: ?ProgressReportFn = null, size_cb: ?SizeFn = null, clipboard_write: ?ClipboardWriteFn = null, + clipboard_read: ?ClipboardReadFn = null, + unknown_sequence: ?UnknownSequenceFn = null, /// Scratch buffer for DA1 feature codes. The device attributes /// trampoline converts C feature codes into this buffer and returns @@ -105,8 +288,18 @@ const Effects = struct { pub const XtversionFn = *const fn (Terminal, ?*anyopaque) callconv(lib.calling_conv) lib.String; /// C function pointer type for the clipboard_write callback. The request - /// and its contents are borrowed and only valid for the callback duration. - pub const ClipboardWriteFn = *const fn (Terminal, ?*anyopaque, *const ClipboardWrite) callconv(lib.calling_conv) clipboard.WriteResult; + /// is borrowed for the callback duration and must be answered through + /// its reply function before the callback returns. + pub const ClipboardWriteFn = *const fn (Terminal, ?*anyopaque, *const ClipboardWrite) callconv(lib.calling_conv) void; + + /// C function pointer type for the clipboard_read callback. The request + /// is borrowed for the callback duration and must be answered through + /// its reply function before the callback returns. + pub const ClipboardReadFn = *const fn (Terminal, ?*anyopaque, *const ClipboardRead) callconv(lib.calling_conv) void; + + /// C function pointer type for the desktop_notification callback. The + /// request and its strings are borrowed for the callback duration. + pub const DesktopNotificationFn = *const fn (Terminal, ?*anyopaque, *const DesktopNotification) callconv(lib.calling_conv) void; /// C function pointer type for the title_changed callback. pub const TitleChangedFn = *const fn (Terminal, ?*anyopaque) callconv(lib.calling_conv) void; @@ -114,9 +307,17 @@ const Effects = struct { /// C function pointer type for the pwd_changed callback. pub const PwdChangedFn = *const fn (Terminal, ?*anyopaque) callconv(lib.calling_conv) void; - /// C function pointer type for the size callback. - /// Returns true and fills out_size if size is available, - /// or returns false to silently ignore the query. + /// C function pointer type for the progress_report callback. + pub const ProgressReportFn = *const fn (Terminal, ?*anyopaque, *const ProgressReport) callconv(lib.calling_conv) void; + + /// C function pointer type for the unknown_sequence callback. The request + /// and its content are borrowed for the callback duration. + pub const UnknownSequenceFn = *const fn (Terminal, ?*anyopaque, *const UnknownSequence.C) callconv(lib.calling_conv) void; + + /// C function pointer type for the size callback. Used by XTWINOPS queries + /// and VT-driven mode 2048 enable reports. Returns true and fills out_size + /// if size is available, or false to suppress the XTWINOPS response or + /// mode 2048 report. pub const SizeFn = *const fn (Terminal, ?*anyopaque, *size_report.Size) callconv(lib.calling_conv) bool; /// C function pointer type for the device_attributes callback. @@ -148,24 +349,27 @@ const Effects = struct { }; }; - fn writePtyTrampoline(handler: *Handler, data: [:0]const u8) void { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + fn writePtyTrampoline(handler: *Handler, data: []const u8) void { + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.write_pty orelse return; func(@ptrCast(wrapper), wrapper.effects.userdata, data.ptr, data.len); } fn bellTrampoline(handler: *Handler) void { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.bell orelse return; func(@ptrCast(wrapper), wrapper.effects.userdata); } - fn clipboardWriteTrampoline(handler: *Handler, write: clipboard.Write) clipboard.WriteResult { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); - const func = wrapper.effects.clipboard_write orelse return .unsupported; + /// Opaque context behind ClipboardWrite.ctx for the reply trampoline. + const ClipboardWriteCtx = struct { + write: clipboard.Write, + }; + + fn clipboardWriteTrampoline(handler: *Handler, write: clipboard.Write) void { + const wrapper = TerminalWrapper.fromHandler(handler); + const func = wrapper.effects.clipboard_write orelse + return write.reply(.unsupported); // Most protocols currently produce one representation, so keep that // path allocation-free while supporting arbitrary multi-MIME writes. @@ -174,35 +378,158 @@ const Effects = struct { stack_contents[0..write.contents.len] else wrapper.terminal.gpa().alloc(ClipboardContent, write.contents.len) catch - return .io_error; + return write.reply(.io_error); defer if (write.contents.len > stack_contents.len) wrapper.terminal.gpa().free(contents); for (contents, write.contents) |*c_content, content| { c_content.* = .{ - .mime = .{ - .ptr = content.mime.ptr, - .len = content.mime.len, - }, - .data = .{ - .ptr = content.data.ptr, - .len = content.data.len, - }, + .mime = .init(content.mime), + .data = .init(content.data), }; } + const ctx: ClipboardWriteCtx = .{ .write = write }; const request: ClipboardWrite = .{ .size = @sizeOf(ClipboardWrite), .location = write.location, .contents = if (contents.len > 0) contents.ptr else null, .contents_len = contents.len, + .name = .init(write.name), + .granted = write.granted, + .can_remember = write.can_remember, + .ctx = &ctx, + .reply = &clipboardWriteReplyTrampoline, + }; + func(@ptrCast(wrapper), wrapper.effects.userdata, &request); + } + + fn clipboardWriteReplyTrampoline( + request: *const ClipboardWrite, + reply: *const ClipboardWriteReply, + ) callconv(lib.calling_conv) void { + const ctx: *const ClipboardWriteCtx = @ptrCast(@alignCast(request.ctx)); + const write = ctx.write; + switch (reply.result) { + .success => write.reply(.{ .success = .{ .remember = reply.remember } }), + .denied => write.reply(.denied), + .busy => write.reply(.busy), + .invalid_data => write.reply(.invalid_data), + .io_error => write.reply(.io_error), + .unsupported, _ => write.reply(.unsupported), + } + } + + /// Opaque context behind ClipboardRead.ctx for the reply trampoline. + const ClipboardReadCtx = struct { + read: clipboard.Read, + wrapper: *TerminalWrapper, + }; + + fn clipboardReadTrampoline(handler: *Handler, read: clipboard.Read) void { + const wrapper = TerminalWrapper.fromHandler(handler); + const func = wrapper.effects.clipboard_read orelse return; + + // Requests carry a handful of MIME types, so keep the common case + // allocation-free. On OOM the request goes unanswered and the + // handler replies with an empty clipboard. + var sfa = std.heap.stackFallback(128, wrapper.terminal.gpa()); + const alloc = sfa.get(); + const mimes = alloc.alloc(lib.String, read.mimes.len) catch { + log.warn("out of memory converting clipboard read request", .{}); + return; + }; + defer alloc.free(mimes); + for (mimes, read.mimes) |*c_mime, mime| c_mime.* = .init(mime); + + const ctx: ClipboardReadCtx = .{ .read = read, .wrapper = wrapper }; + const request: ClipboardRead = .{ + .size = @sizeOf(ClipboardRead), + .location = read.location, + .mimes = if (mimes.len > 0) mimes.ptr else null, + .mimes_len = mimes.len, + .list = read.list, + .name = .init(read.name), + .granted = read.granted, + .can_remember = read.can_remember, + .ctx = &ctx, + .reply = &clipboardReadReplyTrampoline, + }; + func(@ptrCast(wrapper), wrapper.effects.userdata, &request); + } + + fn clipboardReadReplyTrampoline( + request: *const ClipboardRead, + reply: *const ClipboardReadReply, + ) callconv(lib.calling_conv) void { + const ctx: *const ClipboardReadCtx = @ptrCast(@alignCast(request.ctx)); + const read = ctx.read; + switch (reply.result) { + .success => {}, + .denied => return read.reply(.denied), + .busy => return read.reply(.busy), + .io_error => return read.reply(.io_error), + .unsupported, _ => return read.reply(.unsupported), + } + + const c_contents: []const ClipboardContent = if (reply.contents) |ptr| + ptr[0..reply.contents_len] + else + &.{}; + const c_available: []const lib.String = if (reply.available) |ptr| + ptr[0..reply.available_len] + else + &.{}; + + // Most replies carry one representation, so keep that path + // allocation-free while supporting arbitrary multi-MIME replies. + // On OOM we don't reply and the handler answers with an empty + // clipboard. + var sfa = std.heap.stackFallback(256, ctx.wrapper.terminal.gpa()); + const alloc = sfa.get(); + const contents = alloc.alloc(clipboard.Content, c_contents.len) catch { + log.warn("out of memory converting clipboard read reply", .{}); + return; + }; + defer alloc.free(contents); + for (contents, c_contents) |*content, c_content| { + content.* = .{ + .mime = c_content.mime.ptr[0..c_content.mime.len], + .data = c_content.data.ptr[0..c_content.data.len], + }; + } + const available = alloc.alloc([]const u8, c_available.len) catch { + log.warn("out of memory converting clipboard read reply", .{}); + return; + }; + defer alloc.free(available); + for (available, c_available) |*mime, c_mime| { + mime.* = c_mime.ptr[0..c_mime.len]; + } + + read.reply(.{ .success = .{ + .contents = contents, + .available = available, + .remember = reply.remember, + } }); + } + + fn desktopNotificationTrampoline( + handler: *Handler, + notification: Action.ShowDesktopNotification, + ) void { + const wrapper = TerminalWrapper.fromHandler(handler); + const func = wrapper.effects.desktop_notification orelse return; + const request: DesktopNotification = .{ + .size = @sizeOf(DesktopNotification), + .title = .init(notification.title), + .body = .init(notification.body), }; - return func(@ptrCast(wrapper), wrapper.effects.userdata, &request); + func(@ptrCast(wrapper), wrapper.effects.userdata, &request); } fn colorSchemeTrampoline(handler: *Handler) ?device_status.ColorScheme { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.color_scheme orelse return null; var scheme: device_status.ColorScheme = undefined; if (func(@ptrCast(wrapper), wrapper.effects.userdata, &scheme)) return scheme; @@ -210,8 +537,7 @@ const Effects = struct { } fn deviceAttributesTrampoline(handler: *Handler) device_attributes.Attributes { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.device_attributes_cb orelse return .{}; // Get our attributes from the callback. @@ -241,8 +567,7 @@ const Effects = struct { } fn enquiryTrampoline(handler: *Handler) []const u8 { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.enquiry orelse return ""; const result = func(@ptrCast(wrapper), wrapper.effects.userdata); if (result.len == 0) return ""; @@ -250,8 +575,7 @@ const Effects = struct { } fn xtversionTrampoline(handler: *Handler) []const u8 { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.xtversion orelse return ""; const result = func(@ptrCast(wrapper), wrapper.effects.userdata); if (result.len == 0) return ""; @@ -259,22 +583,50 @@ const Effects = struct { } fn titleChangedTrampoline(handler: *Handler) void { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.title_changed orelse return; func(@ptrCast(wrapper), wrapper.effects.userdata); } fn pwdChangedTrampoline(handler: *Handler) void { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.pwd_changed orelse return; func(@ptrCast(wrapper), wrapper.effects.userdata); } + fn progressReportTrampoline( + handler: *Handler, + report: osc.Command.ProgressReport, + ) void { + const wrapper = TerminalWrapper.fromHandler(handler); + const func = wrapper.effects.progress_report orelse return; + const c_report: ProgressReport = .{ + .size = @sizeOf(ProgressReport), + .state = @enumFromInt(@intFromEnum(report.state)), + .progress = if (report.progress) |value| @intCast(value) else -1, + }; + func(@ptrCast(wrapper), wrapper.effects.userdata, &c_report); + } + + fn unknownSequenceTrampoline( + handler: *Handler, + sequence: Handler.UnknownSequence, + ) void { + const wrapper = TerminalWrapper.fromHandler(handler); + const func = wrapper.effects.unknown_sequence orelse return; + const value = UnknownSequence.cval(switch (sequence) { + .apc => |apc_value| .{ + .apc = .{ + .truncated = apc_value.truncated, + .content = .init(apc_value.content), + }, + }, + }); + func(@ptrCast(wrapper), wrapper.effects.userdata, &value); + } + fn sizeTrampoline(handler: *Handler) ?size_report.Size { - const stream_ptr: *Stream = @fieldParentPtr("handler", handler); - const wrapper: *TerminalWrapper = @fieldParentPtr("stream", stream_ptr); + const wrapper = TerminalWrapper.fromHandler(handler); const func = wrapper.effects.size_cb orelse return null; var s: size_report.Size = undefined; if (func(@ptrCast(wrapper), wrapper.effects.userdata, &s)) return s; @@ -295,13 +647,150 @@ pub fn zigTerminal(terminal_: Terminal) ?*ZigTerminal { return (terminal_ orelse return null).terminal; } -/// C: GhosttyTerminalOptions -pub const Options = extern struct { - cols: size.CellCountInt, - rows: size.CellCountInt, - max_scrollback: usize, +/// Attach the persistent C stream and I/O owner to a heap-stable terminal. +/// The caller retains ownership of both inputs if wrapper allocation fails. +fn wrap( + alloc: std.mem.Allocator, + t: *ZigTerminal, + io: Io, + continuation_max_bytes: usize, +) error{OutOfMemory}!Terminal { + const wrapper = alloc.create(TerminalWrapper) catch + return error.OutOfMemory; + + // Trampolines are always installed so setting C callbacks later takes + // effect immediately. + var handler: Stream.Handler = t.vtHandler(); + handler.effects = .{ + .write_pty = &Effects.writePtyTrampoline, + .bell = &Effects.bellTrampoline, + .color_scheme = &Effects.colorSchemeTrampoline, + .desktop_notification = &Effects.desktopNotificationTrampoline, + .drag_and_drop = null, + .device_attributes = &Effects.deviceAttributesTrampoline, + .enquiry = &Effects.enquiryTrampoline, + .xtversion = &Effects.xtversionTrampoline, + .title_changed = &Effects.titleChangedTrampoline, + .pwd_changed = &Effects.pwdChangedTrampoline, + .progress_report = &Effects.progressReportTrampoline, + .size = &Effects.sizeTrampoline, + + // Installed dynamically when the callback is set; see Effects. + .clipboard_write = null, + .clipboard_read = null, + }; + + wrapper.* = .{ + .terminal = t, + .io = io, + .terminfo_name_buf = undefined, + .stream = Stream.init(.{ + .allocator = alloc, + .handler = handler, + .continuation_max_bytes = continuation_max_bytes, + }), + }; + return wrapper; +} + +pub const RestoreContinuationError = error{ + OutOfMemory, + ContinuationDisabled, + ContinuationUnavailable, + InvalidContinuation, +}; + +/// Replay a decoded continuation exactly once into a newly created C terminal +/// and verify that the persistent stream exports the identical canonical +/// bytes. The terminal remains valid on error and may be freed normally. +pub fn restoreContinuation( + terminal_: Terminal, + continuation: []const u8, +) RestoreContinuationError!void { + const wrapper = terminal_ orelse return error.InvalidContinuation; + if (continuation.len > 0) wrapper.stream.nextSlice(continuation); + + var exported: std.Io.Writer.Allocating = .init(wrapper.terminal.gpa()); + defer exported.deinit(); + wrapper.stream.writeContinuation(&exported.writer) catch |err| switch (err) { + error.WriteFailed => return error.OutOfMemory, + error.ContinuationDisabled => return error.ContinuationDisabled, + error.ContinuationUnavailable => return error.ContinuationUnavailable, + }; + if (!std.mem.eql(u8, continuation, exported.written())) { + return error.InvalidContinuation; + } +} + +pub const FromDecodedError = error{ + OutOfMemory, + InvalidContinuation, }; +/// Transfer a core snapshot result into a caller-owned C terminal. +/// +/// `io` is the owner the decoded terminal was built with and is retained +/// by the returned wrapper. The decoded terminal is transferred only +/// after its final heap address has been allocated; its +/// continuation remains in `decoded` and is replayed before returning. +/// `continuation_max_bytes` selects the returned terminal's tracking policy: +/// zero uses a temporary exact-size tracker and restores the ordinary C +/// default before returning, while a nonzero value leaves tracking enabled +/// with that limit. +pub fn fromDecoded( + alloc: std.mem.Allocator, + io: Io, + decoded: *snapshot_core.Decoded, + continuation_max_bytes: usize, +) FromDecodedError!Terminal { + const native = alloc.create(ZigTerminal) catch + return error.OutOfMemory; + native.* = decoded.toOwned(); + + const continuation = switch (decoded.continuation) { + .ground => "", + .bytes => |bytes| bytes, + }; + assert(continuation_max_bytes == 0 or + continuation.len <= continuation_max_bytes); + + // Without opt-in retention, non-ground state needs tracking only long + // enough to verify that replay reconstructed the exact canonical + // continuation. With retention, even ground state needs a tracker so its + // continuation can be exported successfully as an empty slice. + const tracker_max_bytes = if (continuation_max_bytes > 0) + continuation_max_bytes + else + continuation.len; + const terminal = wrap(alloc, native, io, tracker_max_bytes) catch |err| { + native.deinit(alloc); + alloc.destroy(native); + return err; + }; + errdefer free(terminal); + + if (continuation.len > 0) { + restoreContinuation(terminal, continuation) catch |err| return switch (err) { + // The decoded bytes fit this fresh tracker's cap, so losing them + // while replaying can only be an allocation failure. + error.OutOfMemory, + error.ContinuationUnavailable, + => error.OutOfMemory, + error.ContinuationDisabled, + error.InvalidContinuation, + => error.InvalidContinuation, + }; + } + + // Unless the decoder opted into retention, restore the same disabled + // tracking default used by newly created C terminals. Disabling tracking + // does not alter the parser or UTF-8 state reconstructed above. + if (continuation_max_bytes == 0) { + setContinuationMaxBytes(terminal.?, default_continuation_max_bytes); + } + return terminal; +} + const NewError = error{ InvalidValue, OutOfMemory, @@ -310,9 +799,10 @@ const NewError = error{ pub fn new( alloc_: ?*const CAllocator, result: *Terminal, - opts: Options, + cols: size.CellCountInt, + rows: size.CellCountInt, ) callconv(lib.calling_conv) Result { - result.* = new_(alloc_, opts) catch |err| { + result.* = new_(alloc_, cols, rows) catch |err| { result.* = null; return switch (err) { error.InvalidValue => .invalid_value, @@ -325,26 +815,27 @@ pub fn new( fn new_( alloc_: ?*const CAllocator, - opts: Options, -) NewError!*TerminalWrapper { - if (opts.cols == 0 or opts.rows == 0) return error.InvalidValue; + cols: size.CellCountInt, + rows: size.CellCountInt, +) NewError!Terminal { + if (cols == 0 or rows == 0) return error.InvalidValue; const alloc = lib.alloc.default(alloc_); const t = alloc.create(ZigTerminal) catch return error.OutOfMemory; errdefer alloc.destroy(t); - const wrapper = alloc.create(TerminalWrapper) catch - return error.OutOfMemory; - errdefer alloc.destroy(wrapper); + const io: Io = .init; // Setup our terminal - t.* = try .init(alloc, .{ - .cols = opts.cols, - .rows = opts.rows, - .max_scrollback = opts.max_scrollback, - .default_modes = .{ .grapheme_cluster = true }, - }); + t.* = try .init( + io.io(), + alloc, + .{ + .cols = cols, + .rows = rows, + }, + ); errdefer t.deinit(alloc); // libghostty-vt embedders don't necessarily install Ghostty's shell @@ -352,28 +843,12 @@ fn new_( // Shells can still opt in with OSC 133;A;redraw=1. t.flags.shell_redraws_prompt = .false; - // Setup our stream with trampolines always installed so that - // setting C callbacks at any time takes effect immediately. - var handler: Stream.Handler = t.vtHandler(); - handler.effects = .{ - .write_pty = &Effects.writePtyTrampoline, - .bell = &Effects.bellTrampoline, - .color_scheme = &Effects.colorSchemeTrampoline, - .device_attributes = &Effects.deviceAttributesTrampoline, - .enquiry = &Effects.enquiryTrampoline, - .xtversion = &Effects.xtversionTrampoline, - .title_changed = &Effects.titleChangedTrampoline, - .pwd_changed = &Effects.pwdChangedTrampoline, - .size = &Effects.sizeTrampoline, - .clipboard_write = &Effects.clipboardWriteTrampoline, - }; - - wrapper.* = .{ - .terminal = t, - .stream = .initAlloc(alloc, handler), - }; - - return wrapper; + return try wrap( + alloc, + t, + io, + default_continuation_max_bytes, + ); } pub fn vt_write( @@ -385,6 +860,256 @@ pub fn vt_write( wrapper.stream.nextSlice(ptr[0..len]); } +pub fn vt_write_until_ground( + terminal_: Terminal, + ptr_: ?[*]const u8, + len: usize, + out_consumed_: ?*usize, +) callconv(lib.calling_conv) Result { + const out_consumed = out_consumed_ orelse return .invalid_value; + out_consumed.* = 0; + + const wrapper = terminal_ orelse return .invalid_value; + const input: []const u8 = if (ptr_) |ptr| + ptr[0..len] + else if (len == 0) + "" + else + return .invalid_value; + + if (wrapper.stream.nextSliceUntilGround(input)) |consumed| { + out_consumed.* = consumed; + return .success; + } + + out_consumed.* = len; + return .no_value; +} + +pub const ContinuationWriteError = error{ + InvalidValue, + WriteFailed, + ContinuationDisabled, + ContinuationUnavailable, +}; + +/// Write the exact replay-safe continuation for a C terminal to a Zig writer. +/// Snapshot encoding uses this helper so it can preflight continuation state +/// without converting its allocator or writer through the C ABI. +pub fn continuationWriteIo( + terminal_: Terminal, + writer: *std.Io.Writer, +) ContinuationWriteError!void { + // Keep handle validation here so the three public output forms and the + // snapshot encoder all use exactly the same continuation preflight. + const wrapper = terminal_ orelse return error.InvalidValue; + + // Stream owns the tracker and distinguishes disabled tracking from a + // tracker that lost bytes after allocation failure or limit overflow. + try wrapper.stream.writeContinuation(writer); +} + +pub const ContinuationAllocError = error{ + InvalidValue, + OutOfMemory, + ContinuationDisabled, + ContinuationUnavailable, +}; + +/// Return an allocator-owned copy of the terminal's replay-safe continuation. +/// The caller owns the returned slice and must free it with `alloc`. +/// +/// If `allow_untracked_ground` is true, disabled tracking is accepted only +/// when the stream is provably at ground and an owned empty slice is returned. +pub fn continuationAllocIo( + terminal_: Terminal, + alloc: std.mem.Allocator, + allow_untracked_ground: bool, +) ContinuationAllocError![]u8 { + const wrapper = terminal_ orelse return error.InvalidValue; + if (allow_untracked_ground and + wrapper.stream.continuation == null and + wrapper.stream.ground()) + { + return alloc.dupe(u8, "") catch error.OutOfMemory; + } + + // The allocating writer gives snapshot encoding and the public allocation + // API one common way to obtain an exact owned continuation. + var aw: std.Io.Writer.Allocating = .init(alloc); + defer aw.deinit(); + + // A write failure from an Allocating writer can only be allocation failure; + // no external callback participates in this path. + continuationWriteIo(terminal_, &aw.writer) catch |err| switch (err) { + error.InvalidValue => return error.InvalidValue, + error.WriteFailed => return error.OutOfMemory, + error.ContinuationDisabled => return error.ContinuationDisabled, + error.ContinuationUnavailable => return error.ContinuationUnavailable, + }; + + // Transfer the buffer out before the deferred writer cleanup runs. + return aw.toOwnedSlice() catch error.OutOfMemory; +} + +/// Map errors that are intrinsic to continuation export. Public callback +/// failures receive finer classification in `continuation_write` below. +fn continuationErrorResult(err: ContinuationWriteError) Result { + return switch (err) { + error.InvalidValue => .invalid_value, + error.WriteFailed => .io_error, + error.ContinuationDisabled => .invalid_value, + error.ContinuationUnavailable => .invalid_value, + }; +} + +pub fn continuation_write( + terminal_: Terminal, + writer: c_io.Writer, +) callconv(lib.calling_conv) Result { + // Reject the missing callback before invoking the common helper so this is + // classified as a bad argument rather than a write failure. + if (writer.write == null) return .invalid_value; + + // The callback was validated above, so invalid_write can only mean output + // accounting overflow. Keep that distinct from callback rejection. + var buffer: [c_io.WriterAdapter.recommended_buffer_len]u8 = undefined; + var adapter: c_io.WriterAdapter = .initBuffered(writer, &buffer); + write: { + continuationWriteIo(terminal_, &adapter.interface) catch |err| switch (err) { + error.WriteFailed => break :write, + else => return continuationErrorResult(err), + }; + adapter.interface.flush() catch break :write; + return .success; + } + + if (adapter.invalid_write) return .limit_exceeded; + if (adapter.callback_failed) return .io_error; + return continuationErrorResult(error.WriteFailed); +} + +pub fn continuation_buf( + terminal_: Terminal, + out_: ?[*]u8, + out_len: usize, + out_written_: ?*usize, +) callconv(lib.calling_conv) Result { + const out_written = out_written_ orelse return .invalid_value; + // All failure paths leave deterministic output metadata. + out_written.* = 0; + if (out_ == null and out_len != 0) return .invalid_value; + + if (out_ == null) { + // A null/zero destination is the explicit size-query form. Discarding + // runs the real exporter, so disabled or unavailable tracking is still + // detected before a required length is reported. + var discarding: std.Io.Writer.Discarding = .init(&.{}); + continuationWriteIo(terminal_, &discarding.writer) catch |err| + return continuationErrorResult(err); + out_written.* = @intCast(discarding.count); + return .out_of_space; + } + + // Fixed writers report WriteFailed when capacity is exhausted. The stream + // exporter itself remains all-or-nothing from the API's perspective. + var writer: std.Io.Writer = .fixed(out_.?[0..out_len]); + continuationWriteIo(terminal_, &writer) catch |err| switch (err) { + error.WriteFailed => { + // Re-run against a counter to return the full required capacity, + // not merely the prefix that fit in the caller's buffer. + var discarding: std.Io.Writer.Discarding = .init(&.{}); + continuationWriteIo(terminal_, &discarding.writer) catch |count_err| + return continuationErrorResult(count_err); + out_written.* = @intCast(discarding.count); + return .out_of_space; + }, + else => return continuationErrorResult(err), + }; + + // `end` is the initialized prefix of the fixed destination. + out_written.* = writer.end; + return .success; +} + +pub fn continuation_alloc( + terminal_: Terminal, + alloc_: ?*const CAllocator, + out_ptr_: ?*?[*]u8, + out_len_: ?*usize, +) callconv(lib.calling_conv) Result { + const out_ptr = out_ptr_ orelse return .invalid_value; + const out_len = out_len_ orelse return .invalid_value; + // Make ownership unambiguous even if validation or allocation fails. + out_ptr.* = null; + out_len.* = 0; + + // Resolve NULL to libghostty-vt's default allocator before entering the + // shared Zig allocation path. + const bytes = continuationAllocIo( + terminal_, + lib.alloc.default(alloc_), + false, + ) catch |err| return switch (err) { + error.InvalidValue => .invalid_value, + error.OutOfMemory => .out_of_memory, + error.ContinuationDisabled => .invalid_value, + error.ContinuationUnavailable => .invalid_value, + }; + + // Ownership crosses the ABI here; callers release this exact pointer and + // length with ghostty_free and the same allocator selection. + // An idle parser has no continuation. Never export the empty Zig slice's + // sentinel pointer to a foreign runtime. + out_ptr.* = if (bytes.len == 0) null else bytes.ptr; + out_len.* = bytes.len; + return .success; +} + +fn continuationMaxBytes(wrapper: *const TerminalWrapper) usize { + // Absence of a tracker is the public representation of disabled tracking. + return if (wrapper.stream.continuation) |tracker| + tracker.max_bytes + else + 0; +} + +/// Change continuation tracking policy without disturbing normal VT parser +/// state. Bytes which were not retained while disabled or after exceeding an +/// earlier cap cannot be reconstructed: export remains unavailable until a +/// later feed reaches ground or contains a new replay start. +fn setContinuationMaxBytes(wrapper: *TerminalWrapper, max_bytes: usize) void { + if (max_bytes == 0) { + // Disabling releases retained bytes immediately. Parser and UTF-8 state + // continue normally; only future replay/export information is lost. + if (wrapper.stream.continuation) |*tracker| tracker.deinit(); + wrapper.stream.continuation = null; + return; + } + + if (wrapper.stream.continuation) |*tracker| { + // Changing a live cap preserves retained bytes when they still fit. + tracker.max_bytes = max_bytes; + if (tracker.bytes.items.len > max_bytes) { + // Once a retained prefix is discarded it cannot be reconstructed + // from parser state alone. Mark it broken until Stream observes a + // ground state or a new replay-safe sequence start. + tracker.bytes.clearRetainingCapacity(); + tracker.broken = true; + } + return; + } + + // Enabling from zero starts an empty tracker owned by the terminal's + // allocator. It can immediately track only if no earlier bytes are needed. + wrapper.stream.continuation = .init(wrapper.terminal.gpa(), max_bytes); + if (!wrapper.stream.ground()) { + // The parser was already mid-sequence while tracking was disabled, so + // exporting now would omit an unknown prefix. + wrapper.stream.continuation.?.broken = true; + } +} + pub fn compression_activity( terminal_: Terminal, out_activity_: ?*u64, @@ -402,9 +1127,7 @@ pub fn compress( ) callconv(lib.calling_conv) Result { const t: *ZigTerminal = (terminal_ orelse return .invalid_value).terminal; const out_result = out_result_ orelse return .invalid_value; - const mode = std.meta.intToEnum(CompressionMode, mode_) catch - return .invalid_value; - + const mode = std.enums.fromInt(CompressionMode, mode_) orelse return .invalid_value; out_result.* = t.compress(mode); return .success; } @@ -438,6 +1161,19 @@ pub const Option = enum(c_int) { glyph_protocol = 24, pwd_changed = 25, clipboard_write = 26, + scrollback_max_bytes = 27, + scrollback_max_lines = 28, + desktop_notification = 29, + progress_report = 30, + continuation_max_bytes = 31, + title_report = 32, + mode_default = 33, + mode = 34, + unknown_sequence = 35, + unknown_max_bytes = 36, + terminfo_name = 37, + clipboard_read = 38, + clipboard_write_max_bytes = 39, /// Input type expected for setting the option. pub fn InType(comptime self: Option) type { @@ -446,26 +1182,39 @@ pub const Option = enum(c_int) { .write_pty => ?Effects.WritePtyFn, .bell => ?Effects.BellFn, .color_scheme => ?Effects.ColorSchemeFn, + .desktop_notification => ?Effects.DesktopNotificationFn, .device_attributes => ?Effects.DeviceAttributesFn, .enquiry => ?Effects.EnquiryFn, .xtversion => ?Effects.XtversionFn, .title_changed => ?Effects.TitleChangedFn, .pwd_changed => ?Effects.PwdChangedFn, + .progress_report => ?Effects.ProgressReportFn, .size_cb => ?Effects.SizeFn, .clipboard_write => ?Effects.ClipboardWriteFn, - .title, .pwd => ?*const lib.String, + .clipboard_read => ?Effects.ClipboardReadFn, + .unknown_sequence => ?Effects.UnknownSequenceFn, + .title, .pwd, .terminfo_name => ?*const lib.String, .color_foreground, .color_background, .color_cursor => ?*const color.RGB.C, .color_palette => ?*const color.PaletteC, .kitty_image_storage_limit => ?*const u64, .kitty_image_medium_file, - .kitty_image_medium_temp_file, .kitty_image_medium_shared_mem, .glyph_protocol, + .title_report, => ?*const bool, - .apc_max_bytes, .apc_max_bytes_kitty => ?*const usize, + .kitty_image_medium_temp_file => ?*const lib.String, + .apc_max_bytes, + .apc_max_bytes_kitty, + .scrollback_max_bytes, + .scrollback_max_lines, + .continuation_max_bytes, + .unknown_max_bytes, + .clipboard_write_max_bytes, + => ?*const usize, .selection => ?*const selection_c.CSelection, .default_cursor_style => ?*const TerminalCursorStyle, .default_cursor_blink => ?*const bool, + .mode, .mode_default => ?*const ModeConfig, }; } }; @@ -476,7 +1225,7 @@ pub fn set( value: ?*const anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(Option, @intFromEnum(option)) catch { + _ = std.enums.fromInt(Option, @intFromEnum(option)) orelse { log.warn("terminal_set invalid option value={d}", .{@intFromEnum(option)}); return .invalid_value; }; @@ -503,13 +1252,39 @@ fn setTyped( .write_pty => wrapper.effects.write_pty = value, .bell => wrapper.effects.bell = value, .color_scheme => wrapper.effects.color_scheme = value, + .desktop_notification => wrapper.effects.desktop_notification = value, .device_attributes => wrapper.effects.device_attributes_cb = value, .enquiry => wrapper.effects.enquiry = value, .xtversion => wrapper.effects.xtversion = value, .title_changed => wrapper.effects.title_changed = value, .pwd_changed => wrapper.effects.pwd_changed = value, + .progress_report => wrapper.effects.progress_report = value, .size_cb => wrapper.effects.size_cb = value, - .clipboard_write => wrapper.effects.clipboard_write = value, + .clipboard_write => { + wrapper.effects.clipboard_write = value; + wrapper.stream.handler.effects.clipboard_write = if (value != null) + &Effects.clipboardWriteTrampoline + else + null; + }, + .clipboard_read => { + wrapper.effects.clipboard_read = value; + wrapper.stream.handler.effects.clipboard_read = if (value != null) + &Effects.clipboardReadTrampoline + else + null; + }, + .unknown_sequence => { + wrapper.effects.unknown_sequence = value; + wrapper.stream.handler.unknown_sequence = if (value != null) + &Effects.unknownSequenceTrampoline + else + null; + }, + .title_report => wrapper.stream.handler.title_report = if (value) |ptr| + ptr.* + else + false, .title => { const str = if (value) |v| v.ptr[0..v.len] else ""; wrapper.terminal.setTitle(str) catch return .out_of_memory; @@ -518,7 +1293,16 @@ fn setTyped( const str = if (value) |v| v.ptr[0..v.len] else ""; wrapper.terminal.setPwd(str) catch return .out_of_memory; }, - .color_foreground => { + .terminfo_name => { + const str = if (value) |v| v.ptr[0..v.len] else ""; + if (str.len > wrapper.terminfo_name_buf.len) return .invalid_value; + @memcpy(wrapper.terminfo_name_buf[0..str.len], str); + wrapper.stream.handler.terminfo_name = if (str.len > 0) + wrapper.terminfo_name_buf[0..str.len] + else + null; + }, + .color_foreground => { wrapper.terminal.colors.foreground.default = if (value) |v| .fromC(v.*) else null; wrapper.terminal.flags.dirty.palette = true; }, @@ -532,8 +1316,9 @@ fn setTyped( }, .color_palette => { wrapper.terminal.colors.palette.changeDefault( + wrapper.terminal.gpa(), if (value) |v| color.paletteZval(v) else color.default, - ); + ) catch return .out_of_memory; wrapper.terminal.flags.dirty.palette = true; }, .kitty_image_storage_limit => { @@ -542,11 +1327,10 @@ fn setTyped( var it = wrapper.terminal.screens.all.iterator(); while (it.next()) |entry| { const screen = entry.value.*; - screen.kitty_images.setLimit(screen.alloc, screen, limit) catch return .out_of_memory; + screen.kitty_images.setLimit(screen.io, screen.alloc, screen, limit); } }, .kitty_image_medium_file, - .kitty_image_medium_temp_file, .kitty_image_medium_shared_mem, => { if (comptime !build_options.kitty_graphics) return .success; @@ -556,12 +1340,40 @@ fn setTyped( const screen = entry.value.*; switch (option) { .kitty_image_medium_file => screen.kitty_images.image_limits.file = val, - .kitty_image_medium_temp_file => screen.kitty_images.image_limits.temporary_file = val, .kitty_image_medium_shared_mem => screen.kitty_images.image_limits.shared_memory = val, else => unreachable, } } }, + .kitty_image_medium_temp_file => { + if (comptime !build_options.kitty_graphics) return .success; + const alloc = wrapper.terminal.gpa(); + if (value) |v| { + if (v.len > max_path_bytes) return .out_of_memory; + const path = alloc.dupe(u8, v.ptr[0..v.len]) catch + return .out_of_memory; + var it = wrapper.terminal.screens.all.iterator(); + while (it.next()) |entry| { + const screen = entry.value.*; + screen.kitty_images.image_limits.temporary_file = .{ + .enabled = .{ .directory = path }, + }; + } + + // Every screen points at the new copy now so the previous + // one can be released. + if (wrapper.tmp_dir_path) |old| alloc.free(old); + wrapper.tmp_dir_path = path; + } else { + var it = wrapper.terminal.screens.all.iterator(); + while (it.next()) |entry| { + const screen = entry.value.*; + screen.kitty_images.image_limits.temporary_file = .disabled; + } + if (wrapper.tmp_dir_path) |old| alloc.free(old); + wrapper.tmp_dir_path = null; + } + }, .apc_max_bytes => { wrapper.stream.handler.apc_handler.max_bytes = if (value) |ptr| .initFull(ptr.*) @@ -590,16 +1402,36 @@ fn setTyped( }, .default_cursor_style => { const style = (if (value) |ptr| ptr.* else TerminalCursorStyle.block).toZig() orelse return .invalid_value; - wrapper.stream.handler.default_cursor_style = style; - if (wrapper.stream.handler.default_cursor) { - wrapper.terminal.screens.active.cursor.cursor_style = style; - } + wrapper.terminal.setDefaultCursorStyle(style); }, .default_cursor_blink => { const blink = if (value) |ptr| ptr.* else false; - wrapper.stream.handler.default_cursor_blink = blink; - if (wrapper.stream.handler.default_cursor) { - wrapper.terminal.modes.set(.cursor_blinking, blink); + wrapper.terminal.setDefaultCursorBlink(blink); + }, + .scrollback_max_bytes => wrapper.terminal.setScrollbackMaxBytes( + if (value) |ptr| ptr.* else null, + ), + .scrollback_max_lines => wrapper.terminal.setScrollbackMaxLines( + if (value) |ptr| ptr.* else null, + ), + .continuation_max_bytes => setContinuationMaxBytes( + wrapper, + if (value) |ptr| ptr.* else default_continuation_max_bytes, + ), + .unknown_max_bytes => wrapper.stream.handler.apc_handler.unknown_max_bytes = + if (value) |ptr| ptr.* else 0, + .clipboard_write_max_bytes => wrapper.stream.handler.kitty_clipboard_write_max_bytes = + if (value) |ptr| ptr.* else kitty_clipboard.max_write_size, + .mode, .mode_default => { + const config = (value orelse return .invalid_value).*; + const mode = config.toMode() orelse return .invalid_value; + switch (option) { + .mode => wrapper.terminal.modes.set(mode, config.value), + .mode_default => { + if (!modes.defaultConfigurable(mode)) return .invalid_value; + wrapper.terminal.modes.setDefault(mode, config.value); + }, + else => unreachable, } }, } @@ -652,34 +1484,17 @@ pub fn resize( cell_height_px: u32, ) callconv(lib.calling_conv) Result { const wrapper = terminal_ orelse return .invalid_value; - const t = wrapper.terminal; - if (cols == 0 or rows == 0) return .invalid_value; - t.resize(t.gpa(), cols, rows) catch return .out_of_memory; - - // Update pixel sizes - t.width_px = std.math.mul(u32, cols, cell_width_px) catch std.math.maxInt(u32); - t.height_px = std.math.mul(u32, rows, cell_height_px) catch std.math.maxInt(u32); - - // Disable synchronized output mode so that we show changes - // immediately for a resize. This is allowed by the spec. - t.modes.set(.synchronized_output, false); - - // If we have in-band size reporting enabled, send a report. - if (t.modes.get(.in_band_size_reports)) in_band: { - const func = wrapper.effects.write_pty orelse break :in_band; - - var buf: [1024]u8 = undefined; - var writer: std.Io.Writer = .fixed(&buf); - size_report.encode(&writer, .mode_2048, .{ - .rows = rows, - .columns = cols, - .cell_width = cell_width_px, - .cell_height = cell_height_px, - }) catch break :in_band; - - const data = writer.buffered(); - func(@ptrCast(wrapper), wrapper.effects.userdata, data.ptr, data.len); - } + wrapper.stream.handler.resize(.{ + .cols = cols, + .rows = rows, + .cell_size_px = .{ + .width = cell_width_px, + .height = cell_height_px, + }, + }) catch |err| return switch (err) { + error.InvalidValue => .invalid_value, + error.OutOfMemory => .out_of_memory, + }; return .success; } @@ -689,30 +1504,6 @@ pub fn reset(terminal_: Terminal) callconv(lib.calling_conv) void { t.fullReset(); } -pub fn mode_get( - terminal_: Terminal, - tag: modes.ModeTag.Backing, - out_value: *bool, -) callconv(lib.calling_conv) Result { - const t: *ZigTerminal = (terminal_ orelse return .invalid_value).terminal; - const mode_tag: modes.ModeTag = @bitCast(tag); - const mode = modes.modeFromInt(mode_tag.value, mode_tag.ansi) orelse return .invalid_value; - out_value.* = t.modes.get(mode); - return .success; -} - -pub fn mode_set( - terminal_: Terminal, - tag: modes.ModeTag.Backing, - value: bool, -) callconv(lib.calling_conv) Result { - const t: *ZigTerminal = (terminal_ orelse return .invalid_value).terminal; - const mode_tag: modes.ModeTag = @bitCast(tag); - const mode = modes.modeFromInt(mode_tag.value, mode_tag.ansi) orelse return .invalid_value; - t.modes.set(mode, value); - return .success; -} - /// C: GhosttyKittyGraphics pub const KittyGraphics = kitty_gfx_c.KittyGraphics; @@ -757,7 +1548,15 @@ pub const TerminalData = enum(c_int) { kitty_graphics = 30, selection = 31, viewport_active = 32, - modify_other_keys = 33, + vt_processing_error = 33, + scrollback_max_bytes = 34, + scrollback_max_lines = 35, + continuation_max_bytes = 36, + mode = 37, + vt_ground = 38, + cursor_at_prompt = 39, + clipboard_write_max_bytes = 40, + modify_other_keys = 41, /// Output type expected for querying the data of the given kind. pub fn OutType(comptime self: TerminalData) type { @@ -768,6 +1567,9 @@ pub const TerminalData = enum(c_int) { .cursor_visible, .mouse_tracking, .viewport_active, + .vt_processing_error, + .vt_ground, + .cursor_at_prompt, .modify_other_keys, => bool, .active_screen => TerminalScreen, @@ -775,7 +1577,13 @@ pub const TerminalData = enum(c_int) { .scrollbar => TerminalScrollbar, .cursor_style => style_c.Style, .title, .pwd => lib.String, - .total_rows, .scrollback_rows => usize, + .total_rows, + .scrollback_rows, + .scrollback_max_bytes, + .scrollback_max_lines, + .continuation_max_bytes, + .clipboard_write_max_bytes, + => usize, .width_px, .height_px => u32, .color_foreground, .color_background, @@ -787,11 +1595,12 @@ pub const TerminalData = enum(c_int) { .color_palette, .color_palette_default => color.PaletteC, .kitty_image_storage_limit => u64, .kitty_image_medium_file, - .kitty_image_medium_temp_file, .kitty_image_medium_shared_mem, => bool, + .kitty_image_medium_temp_file => lib.String, .kitty_graphics => KittyGraphics, .selection => selection_c.CSelection, + .mode => ModeConfig, }; } }; @@ -802,18 +1611,20 @@ pub fn get( out: ?*anyopaque, ) callconv(lib.calling_conv) Result { if (comptime std.debug.runtime_safety) { - _ = std.meta.intToEnum(TerminalData, @intFromEnum(data)) catch { + _ = std.enums.fromInt(TerminalData, @intFromEnum(data)) orelse { log.warn("terminal_get invalid data value={d}", .{@intFromEnum(data)}); return .invalid_value; }; } + const out_ptr = out orelse return .invalid_value; + return switch (data) { .invalid => .invalid_value, inline else => |comptime_data| getTyped( terminal_, comptime_data, - @ptrCast(@alignCast(out)), + @ptrCast(@alignCast(out_ptr)), ), }; } @@ -844,7 +1655,8 @@ fn getTyped( comptime data: TerminalData, out: *data.OutType(), ) Result { - const t: *ZigTerminal = (terminal_ orelse return .invalid_value).terminal; + const wrapper = terminal_ orelse return .invalid_value; + const t: *ZigTerminal = wrapper.terminal; switch (data) { .invalid => return .invalid_value, .cols => out.* = t.cols, @@ -880,7 +1692,7 @@ fn getTyped( .color_background_default => out.* = (t.colors.background.default orelse return .no_value).cval(), .color_cursor_default => out.* = (t.colors.cursor.default orelse return .no_value).cval(), .color_palette => out.* = color.paletteCval(&t.colors.palette.current), - .color_palette_default => out.* = color.paletteCval(&t.colors.palette.original), + .color_palette_default => out.* = color.paletteCval(t.colors.palette.original), .kitty_image_storage_limit => { if (comptime !build_options.kitty_graphics) return .no_value; out.* = @intCast(t.screens.active.kitty_images.total_limit); @@ -891,7 +1703,11 @@ fn getTyped( }, .kitty_image_medium_temp_file => { if (comptime !build_options.kitty_graphics) return .no_value; - out.* = t.screens.active.kitty_images.image_limits.temporary_file; + const dir = switch (t.screens.active.kitty_images.image_limits.temporary_file) { + .enabled => |d| d.directory, + .disabled => "", + }; + out.* = .init(dir); }, .kitty_image_medium_shared_mem => { if (comptime !build_options.kitty_graphics) return .no_value; @@ -906,6 +1722,25 @@ fn getTyped( ), .viewport_active => out.* = t.screens.active.pages.viewport == .active, .modify_other_keys => out.* = t.flags.modify_other_keys_2, + .vt_processing_error => out.* = wrapper.stream.handler.semantic_failure, + .vt_ground => out.* = wrapper.stream.ground(), + .scrollback_max_bytes => { + const max = t.screens.get(.primary).?.pages.limits.bytes.explicit; + if (max == std.math.maxInt(usize)) return .no_value; + out.* = max; + }, + .scrollback_max_lines => { + const max = t.screens.get(.primary).?.pages.limits.lines.explicit; + if (max == std.math.maxInt(usize)) return .no_value; + out.* = max; + }, + .continuation_max_bytes => out.* = continuationMaxBytes(wrapper), + .clipboard_write_max_bytes => out.* = wrapper.stream.handler.kitty_clipboard_write_max_bytes, + .mode => { + const mode = out.toMode() orelse return .invalid_value; + out.value = t.modes.get(mode); + }, + .cursor_at_prompt => out.* = t.cursorIsAtPrompt(), } return .success; @@ -988,22 +1823,30 @@ pub fn free(terminal_: Terminal) callconv(lib.calling_conv) void { for (wrapper.tracked_grid_refs.keys()) |ref| ref.terminal = null; wrapper.tracked_grid_refs.deinit(alloc); + for (wrapper.searches.keys()) |search| search.terminal = null; + wrapper.searches.deinit(alloc); wrapper.stream.deinit(); t.deinit(alloc); + if (wrapper.tmp_dir_path) |path| alloc.free(path); alloc.destroy(t); alloc.destroy(wrapper); } +fn testEnableContinuation(terminal: Terminal) !void { + const limit: usize = 1024; + try testing.expectEqual( + Result.success, + set(terminal, .continuation_max_bytes, &limit), + ); +} + test "new/free" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); try testing.expect(t != null); @@ -1016,26 +1859,392 @@ test "new invalid value" { try testing.expectEqual(Result.invalid_value, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 0, - .rows = 24, - .max_scrollback = 10_000, - }, + 0, + 24, )); try testing.expect(t == null); try testing.expectEqual(Result.invalid_value, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 0, - .max_scrollback = 10_000, - }, + 80, + 0, )); try testing.expect(t == null); } +test "continuation option and data" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + var value: usize = 0; + try testing.expectEqual( + Result.success, + get(t, .continuation_max_bytes, @ptrCast(&value)), + ); + try testing.expectEqual(default_continuation_max_bytes, value); + + const custom: usize = 1234; + try testing.expectEqual( + Result.success, + set(t, .continuation_max_bytes, &custom), + ); + try testing.expectEqual( + Result.success, + get(t, .continuation_max_bytes, @ptrCast(&value)), + ); + try testing.expectEqual(custom, value); + + try testing.expectEqual( + Result.success, + set(t, .continuation_max_bytes, null), + ); + try testing.expectEqual( + Result.success, + get(t, .continuation_max_bytes, @ptrCast(&value)), + ); + try testing.expectEqual(default_continuation_max_bytes, value); + + const disabled: usize = 0; + try testing.expectEqual( + Result.success, + set(t, .continuation_max_bytes, &disabled), + ); + try testing.expectEqual( + Result.success, + get(t, .continuation_max_bytes, @ptrCast(&value)), + ); + try testing.expectEqual(@as(usize, 0), value); + + var written: usize = 99; + try testing.expectEqual( + Result.invalid_value, + continuation_buf(t, null, 0, &written), + ); + try testing.expectEqual(@as(usize, 0), written); +} + +test "continuation buffer and allocator export exact suffix" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + try testEnableContinuation(t); + + vt_write(t, "A\x1b[31", 5); + + var required: usize = 0; + try testing.expectEqual( + Result.out_of_space, + continuation_buf(t, null, 0, &required), + ); + try testing.expectEqual(@as(usize, 4), required); + + var short: [2]u8 = undefined; + try testing.expectEqual( + Result.out_of_space, + continuation_buf(t, &short, short.len, &required), + ); + try testing.expectEqual(@as(usize, 4), required); + + var buf: [8]u8 = undefined; + var written: usize = 0; + try testing.expectEqual( + Result.success, + continuation_buf(t, &buf, buf.len, &written), + ); + try testing.expectEqualStrings("\x1b[31", buf[0..written]); + + var out_ptr: ?[*]u8 = null; + var out_len: usize = 0; + try testing.expectEqual(Result.success, continuation_alloc( + t, + &lib.alloc.test_allocator, + &out_ptr, + &out_len, + )); + const allocated = (out_ptr orelse return error.TestExpectedEqual)[0..out_len]; + defer lib.alloc.default(&lib.alloc.test_allocator).free(allocated); + try testing.expectEqualStrings("\x1b[31", allocated); + + vt_write(t, "m", 1); + try testing.expectEqual( + Result.out_of_space, + continuation_buf(t, null, 0, &required), + ); + try testing.expectEqual(@as(usize, 0), required); + + // Completing the sequence must return an empty, allocation-free result. + const failing: CAllocator = .fromZig(&std.mem.Allocator.failing); + try testing.expectEqual(Result.success, continuation_alloc(t, &failing, &out_ptr, &out_len)); + defer @import("allocator.zig").free(&failing, out_ptr, out_len); + try testing.expectEqual(null, out_ptr); + try testing.expectEqual(@as(usize, 0), out_len); +} + +test "continuation export tracks split UTF-8" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + try testEnableContinuation(t); + + const prefix = [_]u8{ 0xF0, 0x9F }; + vt_write(t, &prefix, prefix.len); + + var buf: [4]u8 = undefined; + var written: usize = 0; + try testing.expectEqual( + Result.success, + continuation_buf(t, &buf, buf.len, &written), + ); + try testing.expectEqualSlices(u8, &prefix, buf[0..written]); + + const suffix = [_]u8{ 0x98, 0x80 }; + vt_write(t, &suffix, suffix.len); + try testing.expectEqual( + Result.out_of_space, + continuation_buf(t, null, 0, &written), + ); + try testing.expectEqual(@as(usize, 0), written); +} + +test "continuation callback writer reports success and failure" { + const Sink = struct { + bytes: [8]u8 = undefined, + len: usize = 0, + + fn write( + userdata: ?*anyopaque, + data: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) bool { + const self: *@This() = @ptrCast(@alignCast(userdata.?)); + if (len > self.bytes.len - self.len) return false; + @memcpy(self.bytes[self.len..][0..len], data[0..len]); + self.len += len; + return true; + } + + fn fail( + _: ?*anyopaque, + _: [*]const u8, + _: usize, + ) callconv(lib.calling_conv) bool { + return false; + } + }; + + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + try testEnableContinuation(t); + vt_write(t, "\x1b[31", 4); + + var sink: Sink = .{}; + try testing.expectEqual(Result.success, continuation_write(t, .{ + .write = &Sink.write, + .userdata = &sink, + })); + try testing.expectEqualStrings("\x1b[31", sink.bytes[0..sink.len]); + + try testing.expectEqual(Result.io_error, continuation_write(t, .{ + .write = &Sink.fail, + })); + try testing.expectEqual( + Result.invalid_value, + continuation_write(t, .{}), + ); + try testing.expectEqual(Result.invalid_value, continuation_write(null, .{ + .write = &Sink.fail, + })); +} + +test "continuation runtime reconfiguration recovers safely" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const enough: usize = 8; + try testing.expectEqual( + Result.success, + set(t, .continuation_max_bytes, &enough), + ); + vt_write(t, "\x1b[123", 5); + const too_small: usize = 2; + try testing.expectEqual( + Result.success, + set(t, .continuation_max_bytes, &too_small), + ); + + var written: usize = 99; + try testing.expectEqual( + Result.invalid_value, + continuation_buf(t, null, 0, &written), + ); + try testing.expectEqual(@as(usize, 0), written); + + // Raising the limit cannot reconstruct discarded bytes, but a new ESC is + // a complete replay start and repairs tracking without first grounding. + try testing.expectEqual( + Result.success, + set(t, .continuation_max_bytes, &enough), + ); + vt_write(t, "\x1b[", 2); + var buf: [8]u8 = undefined; + try testing.expectEqual( + Result.success, + continuation_buf(t, &buf, buf.len, &written), + ); + try testing.expectEqualStrings("\x1b[", buf[0..written]); + + const disabled: usize = 0; + try testing.expectEqual( + Result.success, + set(t, .continuation_max_bytes, &disabled), + ); + try testing.expectEqual( + Result.success, + set(t, .continuation_max_bytes, &enough), + ); + try testing.expectEqual( + Result.invalid_value, + continuation_buf(t, null, 0, &written), + ); + + // Completing the sequence reaches ground and resets the broken marker. + vt_write(t, "m", 1); + try testing.expectEqual( + Result.out_of_space, + continuation_buf(t, null, 0, &written), + ); + try testing.expectEqual(@as(usize, 0), written); +} + +test "continuation internal allocation and restoration" { + var source: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &source, + 80, + 24, + )); + defer free(source); + try testEnableContinuation(source); + vt_write(source, "\x1b[31", 4); + + const alloc = lib.alloc.default(&lib.alloc.test_allocator); + const bytes = try continuationAllocIo(source, alloc, false); + defer alloc.free(bytes); + try testing.expectEqualStrings("\x1b[31", bytes); + + var restored: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &restored, + 80, + 24, + )); + defer free(restored); + try testEnableContinuation(restored); + try restoreContinuation(restored, bytes); + + const reexported = try continuationAllocIo(restored, alloc, false); + defer alloc.free(reexported); + try testing.expectEqualStrings(bytes, reexported); + + var invalid: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &invalid, + 80, + 24, + )); + defer free(invalid); + try testEnableContinuation(invalid); + try testing.expectError( + error.InvalidContinuation, + restoreContinuation(invalid, "A"), + ); +} + +test "set scrollback limits" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 3, + )); + defer free(t); + + const primary = t.?.terminal.screens.get(.primary).?; + + const max_bytes: usize = 0; + try testing.expectEqual( + Result.success, + set(t, .scrollback_max_bytes, &max_bytes), + ); + try testing.expectEqual( + @as(usize, 0), + primary.pages.limits.bytes.explicit, + ); + try testing.expect(primary.no_scrollback); + + try testing.expectEqual( + Result.success, + set(t, .scrollback_max_bytes, null), + ); + try testing.expectEqual( + std.math.maxInt(usize), + primary.pages.limits.bytes.explicit, + ); + try testing.expect(!primary.no_scrollback); + + const max_lines: usize = 12; + try testing.expectEqual( + Result.success, + set(t, .scrollback_max_lines, &max_lines), + ); + try testing.expectEqual( + max_lines, + primary.pages.limits.lines.explicit, + ); + + try testing.expectEqual( + Result.success, + set(t, .scrollback_max_lines, null), + ); + try testing.expectEqual( + std.math.maxInt(usize), + primary.pages.limits.lines.explicit, + ); +} + test "free null" { free(null); } @@ -1045,11 +2254,8 @@ test "scroll_viewport" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 5, - .rows = 2, - .max_scrollback = 10_000, - }, + 5, + 2, )); defer free(t); @@ -1107,11 +2313,8 @@ test "scroll_viewport row" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 5, - .rows = 2, - .max_scrollback = 10_000, - }, + 5, + 2, )); defer free(t); @@ -1167,11 +2370,8 @@ test "scroll_viewport row alt screen" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 5, - .rows = 2, - .max_scrollback = 10_000, - }, + 5, + 2, )); defer free(t); @@ -1220,11 +2420,8 @@ test "compression invalid arguments" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000_000, - }, + 80, + 24, )); defer free(t); @@ -1247,11 +2444,8 @@ test "compression activity and incremental scheduling" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000_000, - }, + 80, + 24, )); defer free(t); @@ -1314,11 +2508,8 @@ test "reset" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer free(t); @@ -1339,11 +2530,8 @@ test "resize" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer free(t); @@ -1361,11 +2549,8 @@ test "resize invalid value" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer free(t); @@ -1378,11 +2563,8 @@ test "resize shrinks both axes with cursor at bottom" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1390,112 +2572,359 @@ test "resize shrinks both axes with cursor at bottom" { const move = "\x1b[24;1H"; vt_write(t, move, move.len); - // Shrink both axes; pre-resize cursor.y sits past the new bottom row. - // Previously this underflowed in PageList.resizeCols. - try testing.expectEqual(Result.success, resize(t, 79, 23, 8, 16)); - try testing.expectEqual(79, t.?.terminal.cols); - try testing.expectEqual(23, t.?.terminal.rows); + // Shrink both axes; pre-resize cursor.y sits past the new bottom row. + // Previously this underflowed in PageList.resizeCols. + try testing.expectEqual(Result.success, resize(t, 79, 23, 8, 16)); + try testing.expectEqual(79, t.?.terminal.cols); + try testing.expectEqual(23, t.?.terminal.rows); +} + +test "set and get mode" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + // DEC mode 25 (cursor_visible) defaults to true + const cursor_visible: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 25, .ansi = false }); + var config: ModeConfig = .{ .mode = cursor_visible, .value = undefined }; + try testing.expectEqual(Result.success, get(t, .mode, @ptrCast(&config))); + try testing.expect(config.value); + + // Set it to false + config.value = false; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); + try testing.expectEqual(Result.success, get(t, .mode, @ptrCast(&config))); + try testing.expect(!config.value); + + // ANSI mode 4 (insert) defaults to false + const insert: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 4, .ansi = true }); + config.mode = insert; + try testing.expectEqual(Result.success, get(t, .mode, @ptrCast(&config))); + try testing.expect(!config.value); + + config.value = true; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); + try testing.expectEqual(Result.success, get(t, .mode, @ptrCast(&config))); + try testing.expect(config.value); +} + +test "set mode default updates current and reset value" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const tag: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ + .value = 2027, + .ansi = false, + }); + var config: ModeConfig = .{ .mode = tag, .value = true }; + try testing.expectEqual(Result.success, set( + t, + .mode_default, + @ptrCast(&config), + )); + try testing.expect(t.?.terminal.modes.get(.grapheme_cluster)); + try testing.expect(t.?.terminal.modes.default.grapheme_cluster); + + config.value = false; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); + try testing.expect(!t.?.terminal.modes.get(.grapheme_cluster)); + try testing.expect(t.?.terminal.modes.default.grapheme_cluster); + + // Setting the default also unconditionally replaces the current value. + config.value = true; + try testing.expectEqual(Result.success, set( + t, + .mode_default, + @ptrCast(&config), + )); + try testing.expect(t.?.terminal.modes.get(.grapheme_cluster)); + + config.value = false; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); + vt_write(t, "\x1bc", 2); + try testing.expect(t.?.terminal.modes.get(.grapheme_cluster)); + + config.value = false; + try testing.expectEqual(Result.success, set( + t, + .mode_default, + @ptrCast(&config), + )); + try testing.expect(!t.?.terminal.modes.get(.grapheme_cluster)); + try testing.expect(!t.?.terminal.modes.default.grapheme_cluster); +} + +test "set mode default validates configuration" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + try testing.expectEqual(Result.invalid_value, set( + t, + .mode_default, + null, + )); + + var config: ModeConfig = .{ + .mode = @bitCast(modes.ModeTag{ .value = 9999, .ansi = false }), + .value = true, + }; + try testing.expectEqual(Result.invalid_value, set( + t, + .mode_default, + @ptrCast(&config), + )); + + config.mode = @bitCast(modes.ModeTag{ .value = 1047, .ansi = false }); + try testing.expectEqual(Result.invalid_value, set( + t, + .mode_default, + @ptrCast(&config), + )); +} + +test "set and get mode null" { + const tag: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 25, .ansi = false }); + var config: ModeConfig = .{ .mode = tag, .value = true }; + try testing.expectEqual(Result.invalid_value, set(null, .mode, @ptrCast(&config))); + try testing.expectEqual(Result.invalid_value, get(null, .mode, @ptrCast(&config))); +} + +test "set and get mode validate configuration" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + try testing.expectEqual(Result.invalid_value, set(t, .mode, null)); + try testing.expectEqual(Result.invalid_value, get(t, .mode, null)); + + var config: ModeConfig = .{ + .mode = @bitCast(modes.ModeTag{ .value = 9999, .ansi = false }), + .value = true, + }; + try testing.expectEqual(Result.invalid_value, set(t, .mode, @ptrCast(&config))); + try testing.expectEqual(Result.invalid_value, get(t, .mode, @ptrCast(&config))); +} + +test "vt_write" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + vt_write(t, "Hello", 5); + + const str = try t.?.terminal.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("Hello", str); } -test "mode_get and mode_set" { +test "vt_write_until_ground result contract" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); - var value: bool = undefined; - - // DEC mode 25 (cursor_visible) defaults to true - const cursor_visible: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 25, .ansi = false }); - try testing.expectEqual(Result.success, mode_get(t, cursor_visible, &value)); - try testing.expect(value); + // Input remains untouched when the stream is already at ground. + var consumed: usize = 99; + const untouched = "unprocessed"; + try testing.expectEqual( + Result.success, + vt_write_until_ground(t, untouched, untouched.len, &consumed), + ); + try testing.expectEqual(@as(usize, 0), consumed); - // Set it to false - try testing.expectEqual(Result.success, mode_set(t, cursor_visible, false)); - try testing.expectEqual(Result.success, mode_get(t, cursor_visible, &value)); - try testing.expect(!value); + // Complete a split CSI and stop before inspecting the printable suffix. + vt_write(t, "\x1b[31", 4); + const input = "mABC\x1b["; + try testing.expectEqual( + Result.success, + vt_write_until_ground(t, input, input.len, &consumed), + ); + try testing.expectEqual(@as(usize, 1), consumed); + try testing.expect(t.?.stream.ground()); - // ANSI mode 4 (insert) defaults to false - const insert: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 4, .ansi = true }); - try testing.expectEqual(Result.success, mode_get(t, insert, &value)); - try testing.expect(!value); + var str = try t.?.terminal.plainString(testing.allocator); + try testing.expectEqualStrings("", str); + testing.allocator.free(str); - try testing.expectEqual(Result.success, mode_set(t, insert, true)); - try testing.expectEqual(Result.success, mode_get(t, insert, &value)); - try testing.expect(value); -} + // The untouched suffix can be processed after work at the boundary. + vt_write(t, input.ptr + consumed, input.len - consumed); + str = try t.?.terminal.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("ABC", str); + try testing.expect(!t.?.stream.ground()); -test "mode_get null" { - var value: bool = undefined; - const tag: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 25, .ansi = false }); - try testing.expectEqual(Result.invalid_value, mode_get(null, tag, &value)); -} + // Exhausting input while unfinished is distinct from reaching ground on + // the final byte, even though both consume the entire input. + try testing.expectEqual( + Result.no_value, + vt_write_until_ground(t, "123", 3, &consumed), + ); + try testing.expectEqual(@as(usize, 3), consumed); + try testing.expect(!t.?.stream.ground()); -test "mode_set null" { - const tag: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 25, .ansi = false }); - try testing.expectEqual(Result.invalid_value, mode_set(null, tag, true)); + try testing.expectEqual( + Result.success, + vt_write_until_ground(t, "m", 1, &consumed), + ); + try testing.expectEqual(@as(usize, 1), consumed); + try testing.expect(t.?.stream.ground()); } -test "mode_get unknown mode" { +test "vt_write_until_ground handles UTF-8 and abort boundaries" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); - var value: bool = undefined; - const unknown: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 9999, .ansi = false }); - try testing.expectEqual(Result.invalid_value, mode_get(t, unknown, &value)); + var ground: bool = false; + try testing.expectEqual( + Result.success, + get(t, .vt_ground, @ptrCast(&ground)), + ); + try testing.expect(ground); + + vt_write(t, &.{0xF0}, 1); + try testing.expectEqual( + Result.success, + get(t, .vt_ground, @ptrCast(&ground)), + ); + try testing.expect(!ground); + + const utf8_suffix = [_]u8{ 0x9F, 0x98, 0x84 }; + var consumed: usize = 99; + try testing.expectEqual( + Result.success, + vt_write_until_ground(t, &utf8_suffix, utf8_suffix.len, &consumed), + ); + try testing.expectEqual(utf8_suffix.len, consumed); + try testing.expect(t.?.stream.ground()); + + // A malformed continuation resets the decoder and reaches ground after + // processing the retry byte. + vt_write(t, &.{ 0xE0, 0xA0 }, 2); + try testing.expectEqual( + Result.success, + vt_write_until_ground(t, "A", 1, &consumed), + ); + try testing.expectEqual(@as(usize, 1), consumed); + try testing.expect(t.?.stream.ground()); + + vt_write(t, "\x1b[123", 5); + try testing.expectEqual( + Result.success, + vt_write_until_ground(t, &.{0x18}, 1, &consumed), + ); + try testing.expectEqual(@as(usize, 1), consumed); + try testing.expect(t.?.stream.ground()); } -test "mode_set unknown mode" { +test "vt_write_until_ground invokes effects only for consumed input" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); - const unknown: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 9999, .ansi = false }); - try testing.expectEqual(Result.invalid_value, mode_set(t, unknown, true)); + const S = struct { + var bell_count: usize = 0; + + fn bell(_: Terminal, _: ?*anyopaque) callconv(lib.calling_conv) void { + bell_count += 1; + } + }; + S.bell_count = 0; + try testing.expectEqual(Result.success, set(t, .bell, @ptrCast(&S.bell))); + + vt_write(t, "\x1b[31", 4); + const input = "m\x07"; + var consumed: usize = 99; + try testing.expectEqual( + Result.success, + vt_write_until_ground(t, input, input.len, &consumed), + ); + try testing.expectEqual(@as(usize, 1), consumed); + try testing.expectEqual(@as(usize, 0), S.bell_count); + + vt_write(t, input.ptr + consumed, input.len - consumed); + try testing.expectEqual(@as(usize, 1), S.bell_count); } -test "vt_write" { +test "vt_write_until_ground validates arguments" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer free(t); - vt_write(t, "Hello", 5); + var consumed: usize = 99; + try testing.expectEqual( + Result.invalid_value, + vt_write_until_ground(null, "x", 1, &consumed), + ); + try testing.expectEqual(@as(usize, 0), consumed); - const str = try t.?.terminal.plainString(testing.allocator); - defer testing.allocator.free(str); - try testing.expectEqualStrings("Hello", str); + consumed = 99; + try testing.expectEqual( + Result.invalid_value, + vt_write_until_ground(t, null, 1, &consumed), + ); + try testing.expectEqual(@as(usize, 0), consumed); + + try testing.expectEqual( + Result.invalid_value, + vt_write_until_ground(t, "", 0, null), + ); + + // NULL represents a valid empty slice. While unfinished it consumes zero + // bytes and reports that no ground boundary was found. + vt_write(t, "\x1b[", 2); + consumed = 99; + try testing.expectEqual( + Result.no_value, + vt_write_until_ground(t, null, 0, &consumed), + ); + try testing.expectEqual(@as(usize, 0), consumed); } test "vt_write split escape sequence" { @@ -1503,11 +2932,8 @@ test "vt_write split escape sequence" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer free(t); @@ -1528,11 +2954,8 @@ test "vt_write split combining mark after base at right edge" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 2, - .rows = 2, - .max_scrollback = 0, - }, + 2, + 2, )); defer free(t); @@ -1551,11 +2974,8 @@ test "get cols and rows" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1572,11 +2992,8 @@ test "get cursor position" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1590,6 +3007,51 @@ test "get cursor position" { try testing.expectEqual(0, y); } +test "get vt_processing_error" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + var processing_error: bool = true; + try testing.expectEqual(Result.success, get( + t, + .vt_processing_error, + @ptrCast(&processing_error), + )); + try testing.expect(!processing_error); + + // Force a non-graceful terminal-owned update failure through the public + // VT write path. + { + const alloc = t.?.terminal.screens.active.alloc; + t.?.terminal.screens.active.alloc = testing.failing_allocator; + defer t.?.terminal.screens.active.alloc = alloc; + + const input = "\x1B]2;unavailable\x1B\\"; + vt_write(t, input, input.len); + } + + try testing.expectEqual(Result.success, get( + t, + .vt_processing_error, + @ptrCast(&processing_error), + )); + try testing.expect(processing_error); + + reset(t); + try testing.expectEqual(Result.success, get( + t, + .vt_processing_error, + @ptrCast(&processing_error), + )); + try testing.expect(processing_error); +} + test "get null" { var cols: size.CellCountInt = undefined; try testing.expectEqual(Result.invalid_value, get(null, .cols, @ptrCast(&cols))); @@ -1600,11 +3062,8 @@ test "get cursor_visible" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1613,22 +3072,47 @@ test "get cursor_visible" { try testing.expect(visible); // DEC mode 25 controls cursor visibility - const cursor_visible_mode: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 25, .ansi = false }); - try testing.expectEqual(Result.success, mode_set(t, cursor_visible_mode, false)); + var config: ModeConfig = .{ + .mode = @bitCast(modes.ModeTag{ .value = 25, .ansi = false }), + .value = false, + }; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); try testing.expectEqual(Result.success, get(t, .cursor_visible, @ptrCast(&visible))); try testing.expect(!visible); } +test "get cursor_at_prompt" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + var at_prompt: bool = undefined; + try testing.expectEqual(Result.success, get(t, .cursor_at_prompt, @ptrCast(&at_prompt))); + try testing.expect(!at_prompt); + + const prompt_start = "\x1b]133;A\x07"; + vt_write(t, prompt_start, prompt_start.len); + try testing.expectEqual(Result.success, get(t, .cursor_at_prompt, @ptrCast(&at_prompt))); + try testing.expect(at_prompt); + + const alternate_screen = "\x1b[?1049h"; + vt_write(t, alternate_screen, alternate_screen.len); + try testing.expectEqual(Result.success, get(t, .cursor_at_prompt, @ptrCast(&at_prompt))); + try testing.expect(!at_prompt); +} + test "get active_screen" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1642,11 +3126,8 @@ test "get kitty_keyboard_flags" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1663,15 +3144,7 @@ test "get kitty_keyboard_flags" { test "get modify_other_keys" { var t: Terminal = null; - try testing.expectEqual(Result.success, new( - &lib.alloc.test_allocator, - &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, - )); + try testing.expectEqual(Result.success, new(&lib.alloc.test_allocator, &t, 80, 24)); defer free(t); var enabled: bool = undefined; @@ -1692,11 +3165,8 @@ test "get mouse_tracking" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1705,34 +3175,50 @@ test "get mouse_tracking" { try testing.expect(!tracking); // Enable X10 mouse (DEC mode 9) - const x10_mode: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 9, .ansi = false }); - try testing.expectEqual(Result.success, mode_set(t, x10_mode, true)); + var config: ModeConfig = .{ + .mode = @bitCast(modes.ModeTag{ .value = 9, .ansi = false }), + .value = true, + }; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); try testing.expectEqual(Result.success, get(t, .mouse_tracking, @ptrCast(&tracking))); try testing.expect(tracking); // Disable X10, enable normal mouse (DEC mode 1000) - try testing.expectEqual(Result.success, mode_set(t, x10_mode, false)); - const normal_mode: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 1000, .ansi = false }); - try testing.expectEqual(Result.success, mode_set(t, normal_mode, true)); + config.value = false; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); + config = .{ + .mode = @bitCast(modes.ModeTag{ .value = 1000, .ansi = false }), + .value = true, + }; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); try testing.expectEqual(Result.success, get(t, .mouse_tracking, @ptrCast(&tracking))); try testing.expect(tracking); // Disable normal, enable button mouse (DEC mode 1002) - try testing.expectEqual(Result.success, mode_set(t, normal_mode, false)); - const button_mode: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 1002, .ansi = false }); - try testing.expectEqual(Result.success, mode_set(t, button_mode, true)); + config.value = false; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); + config = .{ + .mode = @bitCast(modes.ModeTag{ .value = 1002, .ansi = false }), + .value = true, + }; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); try testing.expectEqual(Result.success, get(t, .mouse_tracking, @ptrCast(&tracking))); try testing.expect(tracking); // Disable button, enable any mouse (DEC mode 1003) - try testing.expectEqual(Result.success, mode_set(t, button_mode, false)); - const any_mode: modes.ModeTag.Backing = @bitCast(modes.ModeTag{ .value = 1003, .ansi = false }); - try testing.expectEqual(Result.success, mode_set(t, any_mode, true)); + config.value = false; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); + config = .{ + .mode = @bitCast(modes.ModeTag{ .value = 1003, .ansi = false }), + .value = true, + }; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); try testing.expectEqual(Result.success, get(t, .mouse_tracking, @ptrCast(&tracking))); try testing.expect(tracking); // Disable all - should be false again - try testing.expectEqual(Result.success, mode_set(t, any_mode, false)); + config.value = false; + try testing.expectEqual(Result.success, set(t, .mode, @ptrCast(&config))); try testing.expectEqual(Result.success, get(t, .mouse_tracking, @ptrCast(&tracking))); try testing.expect(!tracking); } @@ -1742,11 +3228,8 @@ test "get total_rows" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 10_000, - }, + 80, + 24, )); defer free(t); @@ -1760,11 +3243,8 @@ test "get scrollback_rows" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 3, - .max_scrollback = 10_000, - }, + 80, + 3, )); defer free(t); @@ -1779,16 +3259,87 @@ test "get scrollback_rows" { try testing.expectEqual(@as(usize, 2), scrollback); } +test "get configured scrollback limits" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 3, + )); + defer free(t); + + var value: usize = undefined; + try testing.expectEqual( + Result.success, + get(t, .scrollback_max_bytes, @ptrCast(&value)), + ); + try testing.expectEqual(@as(usize, 10_000), value); + try testing.expectEqual( + Result.no_value, + get(t, .scrollback_max_lines, @ptrCast(&value)), + ); + + const max_bytes: usize = 0; + const max_lines: usize = 12; + try testing.expectEqual( + Result.success, + set(t, .scrollback_max_bytes, &max_bytes), + ); + try testing.expectEqual( + Result.success, + set(t, .scrollback_max_lines, &max_lines), + ); + try testing.expectEqual( + Result.success, + get(t, .scrollback_max_bytes, @ptrCast(&value)), + ); + try testing.expectEqual(max_bytes, value); + try testing.expectEqual( + Result.success, + get(t, .scrollback_max_lines, @ptrCast(&value)), + ); + try testing.expectEqual(max_lines, value); + + // The configured limits belong to the primary screen and remain + // readable while an alternate screen is active. + vt_write(t, "\x1b[?1049h", 8); + try testing.expectEqual( + Result.success, + get(t, .scrollback_max_bytes, @ptrCast(&value)), + ); + try testing.expectEqual(max_bytes, value); + try testing.expectEqual( + Result.success, + get(t, .scrollback_max_lines, @ptrCast(&value)), + ); + try testing.expectEqual(max_lines, value); + + try testing.expectEqual( + Result.success, + set(t, .scrollback_max_bytes, null), + ); + try testing.expectEqual( + Result.success, + set(t, .scrollback_max_lines, null), + ); + try testing.expectEqual( + Result.no_value, + get(t, .scrollback_max_bytes, @ptrCast(&value)), + ); + try testing.expectEqual( + Result.no_value, + get(t, .scrollback_max_lines, @ptrCast(&value)), + ); +} + test "get invalid" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1800,11 +3351,8 @@ test "set default cursor style and blink" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1832,6 +3380,12 @@ test "set default cursor style and blink" { vt_write(t, "\x1b[0 q", 5); try testing.expectEqual(Screen.CursorStyle.underline, t.?.terminal.screens.active.cursor.cursor_style); try testing.expect(t.?.terminal.modes.get(.cursor_blinking)); + + // RIS also restores cursor defaults from Terminal-owned state. + vt_write(t, "\x1b[2 q", 5); + reset(t); + try testing.expectEqual(Screen.CursorStyle.underline, t.?.terminal.screens.active.cursor.cursor_style); + try testing.expect(t.?.terminal.modes.get(.cursor_blinking)); } test "set and get selection" { @@ -1839,11 +3393,8 @@ test "set and get selection" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1887,11 +3438,8 @@ test "selection derivation helpers" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -1977,11 +3525,8 @@ test "selection_adjust mutates snapshot end" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2025,11 +3570,8 @@ test "selection_order and selection_ordered" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2078,11 +3620,8 @@ test "selection_contains" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2136,11 +3675,8 @@ test "selection_equal" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2148,11 +3684,8 @@ test "selection_equal" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &other_t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(other_t); @@ -2225,11 +3758,8 @@ test "selection_order invalid values" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2243,11 +3773,8 @@ test "grid_ref" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2281,7 +3808,8 @@ test "point_from_grid_ref roundtrip active" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer free(t); @@ -2306,7 +3834,8 @@ test "point_from_grid_ref roundtrip viewport" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 10_000 }, + 80, + 24, )); defer free(t); @@ -2329,7 +3858,8 @@ test "point_from_grid_ref history ref to active returns no_value" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 4, .max_scrollback = 10_000 }, + 80, + 4, )); defer free(t); @@ -2364,7 +3894,8 @@ test "point_from_grid_ref null node" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer free(t); @@ -2377,11 +3908,8 @@ test "set write_pty callback" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2408,11 +3936,43 @@ test "set write_pty callback" { try testing.expectEqual(Result.success, set(t, .userdata, @ptrCast(&sentinel))); try testing.expectEqual(Result.success, set(t, .write_pty, @ptrCast(&S.writePty))); - // DECRQM for wraparound mode (mode 7, set by default) should trigger write_pty - vt_write(t, "\x1B[?7$p", 6); - try testing.expect(S.last_data != null); - try testing.expectEqualStrings("\x1B[?7;1$y", S.last_data.?); - try testing.expectEqual(@as(?*anyopaque, @ptrCast(&sentinel)), S.last_userdata); + // DECRQM for wraparound mode (mode 7, set by default) should trigger write_pty + vt_write(t, "\x1B[?7$p", 6); + try testing.expect(S.last_data != null); + try testing.expectEqualStrings("\x1B[?7;1$y", S.last_data.?); + try testing.expectEqual(@as(?*anyopaque, @ptrCast(&sentinel)), S.last_userdata); +} + +test "write_pty receives DECRQSS response" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var last_data: ?[]u8 = null; + + fn deinit() void { + if (last_data) |data| testing.allocator.free(data); + last_data = null; + } + + fn writePty(_: Terminal, _: ?*anyopaque, ptr: [*]const u8, len: usize) callconv(lib.calling_conv) void { + if (last_data) |data| testing.allocator.free(data); + last_data = testing.allocator.dupe(u8, ptr[0..len]) catch @panic("OOM"); + } + }; + defer S.deinit(); + + try testing.expectEqual(Result.success, set(t, .write_pty, @ptrCast(&S.writePty))); + + const query = "\x1B[1m\x1BP$qm\x1B\\"; + vt_write(t, query, query.len); + try testing.expectEqualStrings("\x1BP1$r0;1m\x1B\\", S.last_data.?); } test "write_pty receives OSC color query response" { @@ -2420,11 +3980,8 @@ test "write_pty receives OSC color query response" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2460,11 +4017,8 @@ test "set write_pty without callback ignores queries" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2477,11 +4031,8 @@ test "set write_pty null clears callback" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2506,11 +4057,8 @@ test "set bell callback" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2546,11 +4094,8 @@ test "bell without callback is silent" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2563,11 +4108,8 @@ test "set enquiry callback" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2605,11 +4147,8 @@ test "enquiry without callback is silent" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2622,11 +4161,8 @@ test "set xtversion callback" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2665,11 +4201,8 @@ test "xtversion without callback reports default" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2696,16 +4229,80 @@ test "xtversion without callback reports default" { try testing.expectEqualStrings("\x1BP>|libghostty\x1B\\", S.last_data.?); } +test "set terminfo_name option" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var last_data: ?[]u8 = null; + + fn deinit() void { + if (last_data) |d| testing.allocator.free(d); + last_data = null; + } + + fn writePty(_: Terminal, _: ?*anyopaque, ptr: [*]const u8, len: usize) callconv(lib.calling_conv) void { + if (last_data) |d| testing.allocator.free(d); + last_data = testing.allocator.dupe(u8, ptr[0..len]) catch @panic("OOM"); + } + }; + defer S.deinit(); + + try testing.expectEqual(Result.success, set(t, .write_pty, @ptrCast(&S.writePty))); + + // While no name is set the query goes unanswered; other capabilities + // are still served from the static map. + const query = "\x1BP+q" ++ std.fmt.bytesToHex("TN", .upper) ++ "\x1B\\"; + vt_write(t, query, query.len); + try testing.expect(S.last_data == null); + const co_query = "\x1BP+q" ++ std.fmt.bytesToHex("Co", .upper) ++ "\x1B\\"; + vt_write(t, co_query, co_query.len); + try testing.expectEqualStrings( + "\x1BP1+r" ++ std.fmt.bytesToHex("Co", .upper) ++ "=" ++ + std.fmt.bytesToHex("256", .upper) ++ "\x1B\\", + S.last_data.?, + ); + S.deinit(); + + // The name is copied, so the caller's buffer can go away afterwards. + var name: [14]u8 = "xterm-256color".*; + const value: lib.String = .{ .ptr = &name, .len = name.len }; + try testing.expectEqual(Result.success, set(t, .terminfo_name, @ptrCast(&value))); + @memset(&name, 'z'); + + vt_write(t, query, query.len); + try testing.expect(S.last_data != null); + try testing.expectEqualStrings( + "\x1BP1+r" ++ std.fmt.bytesToHex("TN", .upper) ++ "=" ++ + std.fmt.bytesToHex("xterm-256color", .upper) ++ "\x1B\\", + S.last_data.?, + ); + + // Clearing with NULL leaves the query unanswered again. + S.deinit(); + try testing.expectEqual(Result.success, set(t, .terminfo_name, null)); + vt_write(t, query, query.len); + try testing.expect(S.last_data == null); + + // Names beyond the maximum are rejected rather than truncated. + const long: [Handler.max_terminfo_name_bytes + 1]u8 = @splat('a'); + const long_value: lib.String = .{ .ptr = &long, .len = long.len }; + try testing.expectEqual(Result.invalid_value, set(t, .terminfo_name, @ptrCast(&long_value))); +} + test "set title_changed callback" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2740,11 +4337,8 @@ test "title_changed without callback is silent" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2752,16 +4346,280 @@ test "title_changed without callback is silent" { vt_write(t, "\x1B]2;Hello\x1B\\", 10); } +test "kitty_image_medium_temp_file empty output" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + var t: Terminal = null; + try testing.expectEqual(Result.success, new(&lib.alloc.test_allocator, &t, 80, 24)); + defer free(t); + + const empty: lib.String = .{ .ptr = "", .len = 0 }; + try testing.expectEqual(Result.success, set(t, .kitty_image_medium_temp_file, &empty)); + var result: lib.String = undefined; + try testing.expectEqual(Result.success, get(t, .kitty_image_medium_temp_file, &result)); + try testing.expectEqual(@as(usize, 0), result.len); + try testing.expectEqual(@as([*]const u8, ""), result.ptr); +} + +test "set desktop_notification callback" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var count: usize = 0; + var last_userdata: ?*anyopaque = null; + var last_size: usize = 0; + var title: [64]u8 = undefined; + var title_len: usize = 0; + var body: [64]u8 = undefined; + var body_len: usize = 0; + + fn desktopNotification( + _: Terminal, + ud: ?*anyopaque, + notification: *const DesktopNotification, + ) callconv(lib.calling_conv) void { + count += 1; + last_userdata = ud; + last_size = notification.size; + title_len = notification.title.len; + body_len = notification.body.len; + @memcpy(title[0..title_len], notification.title.ptr[0..title_len]); + @memcpy(body[0..body_len], notification.body.ptr[0..body_len]); + } + }; + S.count = 0; + S.last_userdata = null; + S.last_size = 0; + S.title_len = 0; + S.body_len = 0; + + var sentinel: u8 = 99; + try testing.expectEqual(Result.success, set(t, .userdata, @ptrCast(&sentinel))); + try testing.expectEqual(Result.success, set( + t, + .desktop_notification, + @ptrCast(&S.desktopNotification), + )); + + // Split OSC 777 across writes to exercise the persistent VT parser. + const seq_a = "\x1B]777;notify;Codex;"; + const seq_b = "Needs attention\x1B\\"; + vt_write(t, seq_a, seq_a.len); + try testing.expectEqual(@as(usize, 0), S.count); + vt_write(t, seq_b, seq_b.len); + try testing.expectEqual(@as(usize, 1), S.count); + try testing.expectEqual(@as(?*anyopaque, @ptrCast(&sentinel)), S.last_userdata); + try testing.expectEqual(@sizeOf(DesktopNotification), S.last_size); + try testing.expectEqualStrings("Codex", S.title[0..S.title_len]); + try testing.expectEqualStrings("Needs attention", S.body[0..S.body_len]); + + // OSC 9 has no title and preserves its body. + const seq_c = "\x1B]9;Build complete\x07"; + vt_write(t, seq_c, seq_c.len); + try testing.expectEqual(@as(usize, 2), S.count); + try testing.expectEqualStrings("", S.title[0..S.title_len]); + try testing.expectEqualStrings("Build complete", S.body[0..S.body_len]); + + // Removing the callback takes effect immediately. + try testing.expectEqual(Result.success, set(t, .desktop_notification, null)); + vt_write(t, seq_c, seq_c.len); + try testing.expectEqual(@as(usize, 2), S.count); +} + +test "set progress_report callback" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var count: usize = 0; + var last_userdata: ?*anyopaque = null; + var last_size: usize = 0; + var last_state: ProgressState = .remove; + var last_progress: i8 = -1; + + fn progressReport( + _: Terminal, + ud: ?*anyopaque, + report: *const ProgressReport, + ) callconv(lib.calling_conv) void { + count += 1; + last_userdata = ud; + last_size = report.size; + last_state = report.state; + last_progress = report.progress; + } + }; + S.count = 0; + S.last_userdata = null; + S.last_size = 0; + S.last_state = .remove; + S.last_progress = -1; + + var sentinel: u8 = 100; + try testing.expectEqual(Result.success, set(t, .userdata, @ptrCast(&sentinel))); + try testing.expectEqual(Result.success, set( + t, + .progress_report, + @ptrCast(&S.progressReport), + )); + + const cases = [_]struct { + sequence: []const u8, + state: ProgressState, + progress: i8, + }{ + .{ .sequence = "\x1B]9;4;0;\x1B\\", .state = .remove, .progress = -1 }, + .{ .sequence = "\x1B]9;4;1;42\x07", .state = .set, .progress = 42 }, + .{ .sequence = "\x1B]9;4;2;7\x1B\\", .state = .@"error", .progress = 7 }, + .{ .sequence = "\x1B]9;4;3\x1B\\", .state = .indeterminate, .progress = -1 }, + .{ .sequence = "\x1B]9;4;4;75\x1B\\", .state = .pause, .progress = 75 }, + }; + + for (cases, 1..) |case, expected_count| { + const midpoint = case.sequence.len / 2; + vt_write(t, case.sequence.ptr, midpoint); + try testing.expectEqual(expected_count - 1, S.count); + vt_write(t, case.sequence.ptr + midpoint, case.sequence.len - midpoint); + try testing.expectEqual(expected_count, S.count); + try testing.expectEqual(@as(?*anyopaque, @ptrCast(&sentinel)), S.last_userdata); + try testing.expectEqual(@sizeOf(ProgressReport), S.last_size); + try testing.expectEqual(case.state, S.last_state); + try testing.expectEqual(case.progress, S.last_progress); + } + + try testing.expectEqual(Result.success, set(t, .progress_report, null)); + const ignored = "\x1B]9;4;1;90\x1B\\"; + vt_write(t, ignored, ignored.len); + try testing.expectEqual(@as(usize, cases.len), S.count); +} + +test "set unknown_sequence callback" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var count: usize = 0; + var last_terminal: Terminal = null; + var last_userdata: ?*anyopaque = null; + var last_tag: UnknownSequence.Tag = .apc; + var last_truncated: bool = false; + var content: [64]u8 = undefined; + var content_len: usize = 0; + + fn unknownSequence( + terminal_: Terminal, + ud: ?*anyopaque, + sequence: *const UnknownSequence.C, + ) callconv(lib.calling_conv) void { + count += 1; + last_terminal = terminal_; + last_userdata = ud; + last_tag = sequence.tag; + const apc_value = sequence.value.apc; + last_truncated = apc_value.truncated; + content_len = @min(apc_value.content.len, content.len); + @memcpy(content[0..content_len], apc_value.content.ptr[0..content_len]); + } + }; + S.count = 0; + S.last_terminal = null; + S.last_userdata = null; + S.last_tag = .apc; + S.last_truncated = false; + S.content_len = 0; + + var sentinel: u8 = 101; + try testing.expectEqual(Result.success, set(t, .userdata, @ptrCast(&sentinel))); + + const max_bytes: usize = 8; + try testing.expectEqual(Result.success, set( + t, + .unknown_max_bytes, + @ptrCast(&max_bytes), + )); + try testing.expectEqual(max_bytes, t.?.stream.handler.apc_handler.unknown_max_bytes); + + // A byte limit without a callback performs no external effect. + const before_callback = "\x1B_abc;xy\x1B\\"; + vt_write(t, before_callback, before_callback.len); + try testing.expectEqual(@as(usize, 0), S.count); + try testing.expect(t.?.stream.handler.unknown_sequence == null); + + try testing.expectEqual(Result.success, set( + t, + .unknown_sequence, + @ptrCast(&S.unknownSequence), + )); + try testing.expect(t.?.stream.handler.unknown_sequence != null); + + // Split a complete APC across writes to exercise persistent parser state. + const seq_a = "\x1B_abc;"; + const seq_b = "xy\x1B\\"; + vt_write(t, seq_a, seq_a.len); + try testing.expectEqual(@as(usize, 0), S.count); + vt_write(t, seq_b, seq_b.len); + try testing.expectEqual(@as(usize, 1), S.count); + try testing.expectEqual(t, S.last_terminal); + try testing.expectEqual(@as(?*anyopaque, @ptrCast(&sentinel)), S.last_userdata); + try testing.expectEqual(UnknownSequence.Tag.apc, S.last_tag); + try testing.expect(!S.last_truncated); + try testing.expectEqualStrings("abc;xy", S.content[0..S.content_len]); + + // Content beyond the generic limit is omitted and marked truncated. + const truncated = "\x1B_abcdefghijkl\x1B\\"; + vt_write(t, truncated, truncated.len); + try testing.expectEqual(@as(usize, 2), S.count); + try testing.expect(S.last_truncated); + try testing.expectEqualStrings("abcdefgh", S.content[0..S.content_len]); + + // CAN aborts the APC and must not invoke the callback. + const aborted = "\x1B_abcdef\x18"; + vt_write(t, aborted, aborted.len); + try testing.expectEqual(@as(usize, 2), S.count); + + // Clearing the callback restores the null fast path immediately. + try testing.expectEqual(Result.success, set(t, .unknown_sequence, null)); + try testing.expect(t.?.stream.handler.unknown_sequence == null); + vt_write(t, before_callback, before_callback.len); + try testing.expectEqual(@as(usize, 2), S.count); + + // A NULL limit disables capture even after reinstalling the callback. + try testing.expectEqual(Result.success, set( + t, + .unknown_sequence, + @ptrCast(&S.unknownSequence), + )); + try testing.expectEqual(Result.success, set(t, .unknown_max_bytes, null)); + try testing.expectEqual(@as(usize, 0), t.?.stream.handler.apc_handler.unknown_max_bytes); + vt_write(t, before_callback, before_callback.len); + try testing.expectEqual(@as(usize, 2), S.count); +} + test "set pwd_changed callback" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2800,11 +4658,8 @@ test "set clipboard_write callback" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2820,13 +4675,16 @@ test "set clipboard_write callback" { var last_mime_lens: [8]usize = @splat(0); var last_data: [8][64]u8 = undefined; var last_data_lens: [8]usize = @splat(0); - var next_result: clipboard.WriteResult = .success; + var last_name_len: usize = 0; + var last_granted: bool = true; + var last_can_remember: bool = true; + var next_result: clipboard.Write.Status = .success; fn clipboardWrite( terminal_: Terminal, ud: ?*anyopaque, request: *const ClipboardWrite, - ) callconv(lib.calling_conv) clipboard.WriteResult { + ) callconv(lib.calling_conv) void { count += 1; last_terminal = terminal_; last_userdata = ud; @@ -2834,6 +4692,9 @@ test "set clipboard_write callback" { last_location = request.location; last_contents_null = request.contents == null; last_contents_len = request.contents_len; + last_name_len = request.name.len; + last_granted = request.granted; + last_can_remember = request.can_remember; if (request.contents) |ptr| { for (ptr[0..@min(request.contents_len, last_mimes.len)], 0..) |content, i| { @@ -2851,7 +4712,11 @@ test "set clipboard_write callback" { } } - return next_result; + request.reply(request, &.{ + .size = @sizeOf(ClipboardWriteReply), + .result = next_result, + .remember = false, + }); } }; S.count = 0; @@ -2878,6 +4743,11 @@ test "set clipboard_write callback" { try testing.expectEqualStrings("text/plain", S.last_mimes[0][0..S.last_mime_lens[0]]); try testing.expectEqualSlices(u8, "hello\x00world", S.last_data[0][0..S.last_data_lens[0]]); + // OSC 52 carries no program identity or password grant state. + try testing.expectEqual(@as(usize, 0), S.last_name_len); + try testing.expect(!S.last_granted); + try testing.expect(!S.last_can_remember); + // OSC 52 destinations are normalized rather than exposed as wire bytes. const location_cases = [_]struct { selector: u8, @@ -2909,70 +4779,472 @@ test "set clipboard_write callback" { vt_write(t, malformed, malformed.len); try testing.expectEqual(@as(usize, 5), S.count); - // iTerm2 Copy reaches the same normalized callback. - const iterm = "\x1B]1337;Copy=:aVRlcm0=\x1B\\"; - vt_write(t, iterm, iterm.len); - try testing.expectEqual(@as(usize, 6), S.count); + // iTerm2 Copy reaches the same normalized callback. + const iterm = "\x1B]1337;Copy=:aVRlcm0=\x1B\\"; + vt_write(t, iterm, iterm.len); + try testing.expectEqual(@as(usize, 6), S.count); + try testing.expectEqual(clipboard.Location.standard, S.last_location); + try testing.expectEqualStrings("text/plain", S.last_mimes[0][0..S.last_mime_lens[0]]); + try testing.expectEqualStrings("iTerm", S.last_data[0][0..S.last_data_lens[0]]); + + // Every representation is converted, and callback replies propagate + // back through the C trampoline for protocols that can acknowledge + // writes. + const internal_contents = [_]clipboard.Content{ + .{ .mime = "text/plain", .data = "plain" }, + .{ .mime = "application/octet-stream", .data = "a\x00b" }, + .{ .mime = "text/html", .data = "plain" }, + .{ .mime = "text/rtf", .data = "{\\rtf1 plain}" }, + .{ .mime = "image/png", .data = "\x89PNG" }, + }; + const Reply = struct { + var last: ?clipboard.Write.Result = null; + fn reply(_: *anyopaque, result: clipboard.Write.Result) void { + last = result; + } + }; + S.next_result = .busy; + const handler = &t.?.stream.handler; + handler.effects.clipboard_write.?(handler, .{ + .location = .primary, + .contents = &internal_contents, + .name = "app", + .granted = true, + .can_remember = true, + .reply_ctx = handler, + .reply_fn = &Reply.reply, + }); + try testing.expect(Reply.last.? == .busy); + try testing.expectEqual(@as(usize, 3), S.last_name_len); + try testing.expect(S.last_granted); + try testing.expect(S.last_can_remember); + try testing.expectEqual(@as(usize, 7), S.count); + try testing.expectEqual(@as(usize, 5), S.last_contents_len); + try testing.expectEqualStrings( + "application/octet-stream", + S.last_mimes[1][0..S.last_mime_lens[1]], + ); + try testing.expectEqualSlices(u8, "a\x00b", S.last_data[1][0..S.last_data_lens[1]]); + try testing.expectEqualStrings("image/png", S.last_mimes[4][0..S.last_mime_lens[4]]); + try testing.expectEqualSlices(u8, "\x89PNG", S.last_data[4][0..S.last_data_lens[4]]); + + // Removing the callback takes effect immediately and uninstalls + // the trampoline. + try testing.expectEqual(Result.success, set(t, .clipboard_write, null)); + try testing.expect(t.?.stream.handler.effects.clipboard_write == null); + const after_remove = "\x1B]52;c;eA==\x1B\\"; + vt_write(t, after_remove, after_remove.len); + try testing.expectEqual(@as(usize, 7), S.count); +} + +test "clipboard_write without callback is unsupported and silent" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + // OSC 52 without a callback should not crash + const seq = "\x1B]52;c;aGVsbG8=\x1B\\"; + vt_write(t, seq, seq.len); + + // No trampoline is installed until a callback is set, so the + // stream skips clipboard work (and never spools a Kitty clipboard + // transaction it can't deliver). + try testing.expect(t.?.stream.handler.effects.clipboard_write == null); +} + +test "kitty clipboard write via C effects" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var responses: [512]u8 = undefined; + var responses_len: usize = 0; + var write_count: usize = 0; + var last_location: clipboard.Location = .standard; + var last_contents_len: usize = 0; + var last_mimes: [4][64]u8 = undefined; + var last_mime_lens: [4]usize = @splat(0); + var last_data: [4][64]u8 = undefined; + var last_data_lens: [4]usize = @splat(0); + var last_granted: bool = true; + var last_can_remember: bool = true; + var next_remember: bool = false; + + fn writePty( + _: Terminal, + _: ?*anyopaque, + ptr: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) void { + @memcpy(responses[responses_len..][0..len], ptr[0..len]); + responses_len += len; + } + + fn clipboardWrite( + _: Terminal, + _: ?*anyopaque, + request: *const ClipboardWrite, + ) callconv(lib.calling_conv) void { + write_count += 1; + last_location = request.location; + last_contents_len = request.contents_len; + if (request.contents) |ptr| { + for (ptr[0..@min(request.contents_len, last_mimes.len)], 0..) |content, i| { + last_mime_lens[i] = @min(content.mime.len, last_mimes[i].len); + @memcpy( + last_mimes[i][0..last_mime_lens[i]], + content.mime.ptr[0..last_mime_lens[i]], + ); + last_data_lens[i] = @min(content.data.len, last_data[i].len); + @memcpy( + last_data[i][0..last_data_lens[i]], + content.data.ptr[0..last_data_lens[i]], + ); + } + } + last_granted = request.granted; + last_can_remember = request.can_remember; + request.reply(request, &.{ + .size = @sizeOf(ClipboardWriteReply), + .result = .success, + .remember = next_remember, + }); + } + }; + S.responses_len = 0; + S.write_count = 0; + S.last_mime_lens = @splat(0); + S.last_data_lens = @splat(0); + S.last_granted = true; + S.last_can_remember = true; + S.next_remember = false; + + try testing.expectEqual(Result.success, set(t, .write_pty, @ptrCast(&S.writePty))); + try testing.expectEqual(Result.success, set(t, .clipboard_write, @ptrCast(&S.clipboardWrite))); + + // A full OSC 5522 write transaction: begin, chunked data for two + // representations, commit. Only the commit invokes the callback, + // and its result maps to the DONE response. + const seqs = [_][]const u8{ + "\x1B]5522;type=write:id=c1\x1B\\", + "\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\", // "Ghost" + "\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;dHk=\x1B\\", // "ty" + "\x1B]5522;type=wdata:mime=dGV4dC9odG1s;PGI+aGk8L2I+\x1B\\", // "hi" + "\x1B]5522;type=wdata\x1B\\", + }; + for (seqs) |seq| vt_write(t, seq.ptr, seq.len); + + try testing.expectEqual(@as(usize, 1), S.write_count); try testing.expectEqual(clipboard.Location.standard, S.last_location); + try testing.expectEqual(@as(usize, 2), S.last_contents_len); try testing.expectEqualStrings("text/plain", S.last_mimes[0][0..S.last_mime_lens[0]]); - try testing.expectEqualStrings("iTerm", S.last_data[0][0..S.last_data_lens[0]]); + try testing.expectEqualStrings("Ghostty", S.last_data[0][0..S.last_data_lens[0]]); + try testing.expectEqualStrings("text/html", S.last_mimes[1][0..S.last_mime_lens[1]]); + try testing.expectEqualStrings("hi", S.last_data[1][0..S.last_data_lens[1]]); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=DONE:id=c1\x1B\\", + S.responses[0..S.responses_len], + ); + try testing.expect(!S.last_granted); + try testing.expect(!S.last_can_remember); + + // Password grants round-trip through the C reply: the first pw'd + // commit isn't granted and asks to remember, so the next one + // arrives granted. + S.responses_len = 0; + S.next_remember = true; + const grant_seqs = [_][]const u8{ + "\x1B]5522;type=write:id=g1:pw=c2VjcmV0:name=YXBw\x1B\\", + "\x1B]5522;type=wdata\x1B\\", + "\x1B]5522;type=write:id=g2:pw=c2VjcmV0:name=YXBw\x1B\\", + "\x1B]5522;type=wdata\x1B\\", + }; + for (grant_seqs) |seq| vt_write(t, seq.ptr, seq.len); + try testing.expectEqual(@as(usize, 3), S.write_count); + try testing.expect(S.last_granted); + try testing.expect(S.last_can_remember); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=DONE:id=g1\x1B\\" ++ + "\x1B]5522;type=write:status=DONE:id=g2\x1B\\", + S.responses[0..S.responses_len], + ); + S.next_remember = false; - // Every representation is converted, and callback results propagate - // through the C trampoline for protocols that can acknowledge writes. - const internal_contents = [_]clipboard.Content{ - .{ .mime = "text/plain", .data = "plain" }, - .{ .mime = "application/octet-stream", .data = "a\x00b" }, - .{ .mime = "text/html", .data = "plain" }, - .{ .mime = "text/rtf", .data = "{\\rtf1 plain}" }, - .{ .mime = "image/png", .data = "\x89PNG" }, + // Without a read callback reads are denied. + S.responses_len = 0; + const read = "\x1B]5522;type=read:id=r1;dGV4dC9wbGFpbg==\x1B\\"; + vt_write(t, read, read.len); + try testing.expectEqual(@as(usize, 3), S.write_count); + try testing.expectEqualStrings( + "\x1B]5522;type=read:status=EPERM:id=r1\x1B\\", + S.responses[0..S.responses_len], + ); + + // With a read callback the request is served through it. + const R = struct { + var count: usize = 0; + var last_mimes_len: usize = 0; + var last_mime_is_text: bool = false; + var last_list: bool = true; + var last_name_len: usize = 0; + var last_granted: bool = true; + var last_can_remember: bool = true; + + fn clipboardRead( + _: Terminal, + _: ?*anyopaque, + request: *const ClipboardRead, + ) callconv(lib.calling_conv) void { + count += 1; + last_mimes_len = request.mimes_len; + last_mime_is_text = request.mimes_len > 0 and std.mem.eql( + u8, + request.mimes.?[0].ptr[0..request.mimes.?[0].len], + "text/plain", + ); + last_list = request.list; + last_name_len = request.name.len; + last_granted = request.granted; + last_can_remember = request.can_remember; + + const mime: []const u8 = "text/plain"; + const data: []const u8 = "hello"; + const contents = [_]ClipboardContent{.{ + .mime = .init(mime), + .data = .init(data), + }}; + request.reply(request, &.{ + .size = @sizeOf(ClipboardReadReply), + .result = .success, + .contents = &contents, + .contents_len = contents.len, + .available = null, + .available_len = 0, + .remember = false, + }); + } }; - S.next_result = .busy; - const handler = &t.?.stream.handler; - const write_result = handler.effects.clipboard_write.?(handler, .{ - .location = .primary, - .contents = &internal_contents, - }); - try testing.expectEqual(clipboard.WriteResult.busy, write_result); - try testing.expectEqual(@as(usize, 7), S.count); - try testing.expectEqual(@as(usize, 5), S.last_contents_len); + try testing.expectEqual(Result.success, set(t, .clipboard_read, @ptrCast(&R.clipboardRead))); + S.responses_len = 0; + // name="app" without a password: forwarded for prompts, not + // rememberable. + const read2 = "\x1B]5522;type=read:id=r2:name=YXBw;dGV4dC9wbGFpbg==\x1B\\"; + vt_write(t, read2, read2.len); + try testing.expectEqual(@as(usize, 1), R.count); + try testing.expectEqual(@as(usize, 1), R.last_mimes_len); + try testing.expect(R.last_mime_is_text); + try testing.expect(!R.last_list); + try testing.expectEqual(@as(usize, 3), R.last_name_len); + try testing.expect(!R.last_granted); + try testing.expect(!R.last_can_remember); try testing.expectEqualStrings( - "application/octet-stream", - S.last_mimes[1][0..S.last_mime_lens[1]], + "\x1B]5522;type=read:status=OK:id=r2\x1B\\" ++ + "\x1B]5522;type=read:status=DATA:id=r2:mime=dGV4dC9wbGFpbg==;aGVsbG8=\x1B\\" ++ + "\x1B]5522;type=read:status=DONE:id=r2\x1B\\", + S.responses[0..S.responses_len], ); - try testing.expectEqualSlices(u8, "a\x00b", S.last_data[1][0..S.last_data_lens[1]]); - try testing.expectEqualStrings("image/png", S.last_mimes[4][0..S.last_mime_lens[4]]); - try testing.expectEqualSlices(u8, "\x89PNG", S.last_data[4][0..S.last_data_lens[4]]); - // Removing the callback takes effect immediately. + // Without a clipboard callback the transaction fails up front. try testing.expectEqual(Result.success, set(t, .clipboard_write, null)); - const after_remove = "\x1B]52;c;eA==\x1B\\"; - vt_write(t, after_remove, after_remove.len); - try testing.expectEqual(@as(usize, 7), S.count); + S.responses_len = 0; + const begin = "\x1B]5522;type=write:id=c2\x1B\\"; + vt_write(t, begin, begin.len); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=ENOSYS:id=c2\x1B\\", + S.responses[0..S.responses_len], + ); } -test "clipboard_write without callback is unsupported and silent" { +test "set clipboard write max bytes" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); - // OSC 52 without a callback should not crash - const seq = "\x1B]52;c;aGVsbG8=\x1B\\"; - vt_write(t, seq, seq.len); + const S = struct { + var responses: [512]u8 = undefined; + var responses_len: usize = 0; + var write_count: usize = 0; + + fn writePty( + _: Terminal, + _: ?*anyopaque, + ptr: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) void { + @memcpy(responses[responses_len..][0..len], ptr[0..len]); + responses_len += len; + } - const handler = &t.?.stream.handler; - const result = handler.effects.clipboard_write.?(handler, .{ - .location = .standard, - .contents = &.{.{ .mime = "text/plain", .data = "hello" }}, - }); - try testing.expectEqual(clipboard.WriteResult.unsupported, result); + fn clipboardWrite( + _: Terminal, + _: ?*anyopaque, + request: *const ClipboardWrite, + ) callconv(lib.calling_conv) void { + write_count += 1; + request.reply(request, &.{ + .size = @sizeOf(ClipboardWriteReply), + .result = .success, + .remember = false, + }); + } + }; + S.responses_len = 0; + S.write_count = 0; + + try testing.expectEqual(Result.success, set(t, .write_pty, @ptrCast(&S.writePty))); + try testing.expectEqual(Result.success, set(t, .clipboard_write, @ptrCast(&S.clipboardWrite))); + + // The built-in default reads back. + var max: usize = 0; + try testing.expectEqual(Result.success, get(t, .clipboard_write_max_bytes, @ptrCast(&max))); + try testing.expectEqual(@as(usize, kitty_clipboard.max_write_size), max); + + // Set a tiny limit; an oversized text write fails with EFBIG and + // never reaches the callback. + const limit: usize = 4; + try testing.expectEqual(Result.success, set(t, .clipboard_write_max_bytes, @ptrCast(&limit))); + try testing.expectEqual(Result.success, get(t, .clipboard_write_max_bytes, @ptrCast(&max))); + try testing.expectEqual(limit, max); + + const seqs = [_][]const u8{ + "\x1B]5522;type=write:id=c1\x1B\\", + "\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;SGVsbA==\x1B\\", // "Hell" + "\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;bw==\x1B\\", // "o" + "\x1B]5522;type=wdata\x1B\\", + }; + for (seqs) |seq| vt_write(t, seq.ptr, seq.len); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EFBIG:id=c1\x1B\\", + S.responses[0..S.responses_len], + ); + + // A NULL value reverts to the built-in default. + try testing.expectEqual(Result.success, set(t, .clipboard_write_max_bytes, null)); + try testing.expectEqual(Result.success, get(t, .clipboard_write_max_bytes, @ptrCast(&max))); + try testing.expectEqual(@as(usize, kitty_clipboard.max_write_size), max); +} + +test "set clipboard_read callback" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var last_data: ?[]u8 = null; + var count: usize = 0; + var last_size: usize = 0; + var last_location: clipboard.Location = .standard; + var last_mimes_len: usize = 0; + var last_mime_is_text: bool = false; + var last_list: bool = true; + var last_name_len: usize = 1; + var last_granted: bool = true; + var last_can_remember: bool = true; + var result: clipboard.Read.Status = .success; + + fn deinit() void { + if (last_data) |d| testing.allocator.free(d); + last_data = null; + } + + fn writePty(_: Terminal, _: ?*anyopaque, ptr: [*]const u8, len: usize) callconv(lib.calling_conv) void { + if (last_data) |d| testing.allocator.free(d); + last_data = testing.allocator.dupe(u8, ptr[0..len]) catch @panic("OOM"); + } + + fn clipboardRead( + _: Terminal, + _: ?*anyopaque, + request: *const ClipboardRead, + ) callconv(lib.calling_conv) void { + count += 1; + last_size = request.size; + last_location = request.location; + last_mimes_len = request.mimes_len; + last_mime_is_text = request.mimes_len > 0 and std.mem.eql( + u8, + request.mimes.?[0].ptr[0..request.mimes.?[0].len], + "text/plain", + ); + last_list = request.list; + last_name_len = request.name.len; + last_granted = request.granted; + last_can_remember = request.can_remember; + + const mime: []const u8 = "text/plain"; + const data: []const u8 = "hello"; + const contents = [_]ClipboardContent{.{ + .mime = .init(mime), + .data = .init(data), + }}; + request.reply(request, &.{ + .size = @sizeOf(ClipboardReadReply), + .result = result, + .contents = &contents, + .contents_len = contents.len, + .available = null, + .available_len = 0, + .remember = false, + }); + } + }; + defer S.deinit(); + + try testing.expectEqual(Result.success, set(t, .write_pty, @ptrCast(&S.writePty))); + + // Without a callback the handler effect is unset and reads are silent. + try testing.expect(t.?.stream.handler.effects.clipboard_read == null); + const read_st = "\x1B]52;c;?\x1B\\"; + vt_write(t, read_st, read_st.len); + try testing.expect(S.last_data == null); + + try testing.expectEqual(Result.success, set(t, .clipboard_read, @ptrCast(&S.clipboardRead))); + try testing.expect(t.?.stream.handler.effects.clipboard_read != null); + + const read_bel = "\x1B]52;p;?\x07"; + vt_write(t, read_bel, read_bel.len); + try testing.expectEqual(1, S.count); + try testing.expectEqual(@sizeOf(ClipboardRead), S.last_size); + try testing.expectEqual(clipboard.Location.primary, S.last_location); + try testing.expectEqual(1, S.last_mimes_len); + try testing.expect(S.last_mime_is_text); + try testing.expect(!S.last_list); + try testing.expectEqual(0, S.last_name_len); + try testing.expect(!S.last_granted); + try testing.expect(!S.last_can_remember); + try testing.expectEqualStrings("\x1B]52;p;aGVsbG8=\x07", S.last_data.?); + + // Denied replies with an empty clipboard. + S.result = .denied; + vt_write(t, read_st, read_st.len); + try testing.expectEqual(2, S.count); + try testing.expectEqualStrings("\x1B]52;c;\x1B\\", S.last_data.?); + + // Clearing the callback uninstalls the handler effect. + try testing.expectEqual(Result.success, set(t, .clipboard_read, null)); + try testing.expect(t.?.stream.handler.effects.clipboard_read == null); } test "pwd_changed without callback is silent" { @@ -2980,11 +5252,8 @@ test "pwd_changed without callback is silent" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -2999,11 +5268,8 @@ test "set size callback" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3046,11 +5312,8 @@ test "size without callback is silent" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3058,16 +5321,116 @@ test "size without callback is silent" { vt_write(t, "\x1B[18t", 5); } +test "mode 2048 enable and disable use C callbacks" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var data: [128]u8 = undefined; + var len: usize = 0; + var calls: usize = 0; + + fn writePty(_: Terminal, _: ?*anyopaque, ptr: [*]const u8, length: usize) callconv(lib.calling_conv) void { + @memcpy(data[0..length], ptr[0..length]); + len = length; + calls += 1; + } + + fn sizeCb(_: Terminal, _: ?*anyopaque, out_size: *size_report.Size) callconv(lib.calling_conv) bool { + out_size.* = .{ + .rows = 24, + .columns = 80, + .cell_width = 8, + .cell_height = 16, + }; + return true; + } + }; + S.len = 0; + S.calls = 0; + + try testing.expectEqual(Result.success, set(t, .write_pty, @ptrCast(&S.writePty))); + try testing.expectEqual(Result.success, set(t, .size_cb, @ptrCast(&S.sizeCb))); + + const enable = "\x1B[?2048h"; + const disable = "\x1B[?2048l"; + vt_write(t, enable, enable.len); + vt_write(t, enable, enable.len); + + try testing.expectEqual(@as(usize, 2), S.calls); + try testing.expectEqualStrings("\x1B[48;24;80;384;640t", S.data[0..S.len]); + vt_write(t, disable, disable.len); + + try testing.expectEqual(@as(usize, 2), S.calls); + try testing.expect(!t.?.terminal.modes.get(.in_band_size_reports)); +} + +test "mode 2048 enable tolerates missing C callbacks" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var writes: usize = 0; + var sizes: usize = 0; + + fn writePty(_: Terminal, _: ?*anyopaque, _: [*]const u8, _: usize) callconv(lib.calling_conv) void { + writes += 1; + } + + fn sizeCb(_: Terminal, _: ?*anyopaque, out_size: *size_report.Size) callconv(lib.calling_conv) bool { + sizes += 1; + out_size.* = .{ + .rows = 24, + .columns = 80, + .cell_width = 8, + .cell_height = 16, + }; + return true; + } + }; + S.writes = 0; + S.sizes = 0; + + try testing.expectEqual(Result.success, set(t, .write_pty, @ptrCast(&S.writePty))); + + const sequence = "\x1B[?2048h"; + vt_write(t, sequence, sequence.len); + + try testing.expectEqual(@as(usize, 0), S.writes); + try testing.expect(t.?.terminal.modes.get(.in_band_size_reports)); + var no_write: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &no_write, + 80, + 24, + )); + defer free(no_write); + try testing.expectEqual(Result.success, set(no_write, .size_cb, @ptrCast(&S.sizeCb))); + vt_write(no_write, sequence, sequence.len); + try testing.expectEqual(@as(usize, 1), S.sizes); + try testing.expect(no_write.?.terminal.modes.get(.in_band_size_reports)); +} + test "set device_attributes callback primary" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3117,11 +5480,8 @@ test "set device_attributes callback secondary" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3171,11 +5531,8 @@ test "set device_attributes callback tertiary" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3225,11 +5582,8 @@ test "device_attributes without callback uses default" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3261,11 +5615,8 @@ test "device_attributes callback returns false uses default" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3302,11 +5653,8 @@ test "set and get title" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3341,11 +5689,8 @@ test "set and get pwd" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3373,11 +5718,8 @@ test "get title set via vt_write" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3389,24 +5731,81 @@ test "get title set via vt_write" { try testing.expectEqualStrings("VT Title", title.ptr[0..title.len]); } +test "title report requires explicit opt in" { + var t: Terminal = null; + try testing.expectEqual(Result.success, new( + &lib.alloc.test_allocator, + &t, + 80, + 24, + )); + defer free(t); + + const S = struct { + var last_data: ?[]u8 = null; + + fn deinit() void { + if (last_data) |data| testing.allocator.free(data); + last_data = null; + } + + fn writePty( + _: Terminal, + _: ?*anyopaque, + ptr: [*]const u8, + len: usize, + ) callconv(lib.calling_conv) void { + if (last_data) |data| testing.allocator.free(data); + last_data = testing.allocator.dupe(u8, ptr[0..len]) catch @panic("OOM"); + } + }; + S.last_data = null; + defer S.deinit(); + + try testing.expectEqual( + Result.success, + set(t, .write_pty, @ptrCast(&S.writePty)), + ); + + const set_title = "\x1B]2;echo vulnerable\x1B\\"; + const query_title = "\x1B[21t"; + vt_write(t, set_title, set_title.len); + + // WRITE_PTY alone must not enable the security-sensitive response. + vt_write(t, query_title, query_title.len); + try testing.expect(S.last_data == null); + + const enabled = true; + try testing.expectEqual(Result.success, set(t, .title_report, &enabled)); + vt_write(t, query_title, query_title.len); + try testing.expectEqualStrings( + "\x1b]lecho vulnerable\x1b\\", + S.last_data.?, + ); + + // NULL restores the secure default. + S.deinit(); + try testing.expectEqual(Result.success, set(t, .title_report, null)); + vt_write(t, query_title, query_title.len); + try testing.expect(S.last_data == null); +} + test "resize updates pixel dimensions" { var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); - try testing.expectEqual(Result.success, resize(t, 100, 40, 9, 18)); + // Pixel geometry must still be applied when the cell dimensions match. + try testing.expectEqual(Result.success, resize(t, 80, 24, 9, 18)); const zt = t.?.terminal; - try testing.expectEqual(@as(u32, 100 * 9), zt.width_px); - try testing.expectEqual(@as(u32, 40 * 18), zt.height_px); + try testing.expectEqual(@as(u32, 80 * 9), zt.width_px); + try testing.expectEqual(@as(u32, 24 * 18), zt.height_px); } test "resize pixel overflow saturates" { @@ -3414,11 +5813,8 @@ test "resize pixel overflow saturates" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3434,18 +5830,16 @@ test "resize disables synchronized output" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); const zt = t.?.terminal; zt.modes.set(.synchronized_output, true); - try testing.expectEqual(Result.success, resize(t, 100, 40, 9, 18)); + // The terminal-level reset must run even if grid work is unnecessary. + try testing.expectEqual(Result.success, resize(t, 80, 24, 9, 18)); try testing.expect(!zt.modes.get(.synchronized_output)); } @@ -3454,11 +5848,8 @@ test "resize sends in-band size report" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3495,11 +5886,8 @@ test "resize no size report without mode 2048" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3523,11 +5911,8 @@ test "resize in-band report without write_pty callback" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3545,11 +5930,8 @@ test "resize zero cols" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3561,11 +5943,8 @@ test "resize zero rows" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3577,11 +5956,8 @@ test "grid_ref out of bounds" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3597,11 +5973,8 @@ test "set and get color_foreground" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3625,11 +5998,8 @@ test "set and get color_background" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3650,11 +6020,8 @@ test "set and get color_cursor" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3675,11 +6042,8 @@ test "set and get color_palette" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3706,11 +6070,8 @@ test "get color default vs effective with override" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3753,11 +6114,8 @@ test "get color default returns no_value when unset" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3772,11 +6130,8 @@ test "get color_palette_default vs current" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3805,11 +6160,8 @@ test "set color sets dirty flag" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ - .cols = 80, - .rows = 24, - .max_scrollback = 0, - }, + 80, + 24, )); defer free(t); @@ -3822,11 +6174,14 @@ test "set color sets dirty flag" { } test "set glyph protocol disables APC handling and clears glossary" { + if (comptime !build_options.glyph_protocol) return error.SkipZigTest; + var t: Terminal = null; try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer free(t); @@ -3853,7 +6208,8 @@ test "get_multi success" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer free(t); @@ -3874,7 +6230,8 @@ test "get_multi error sets out_written" { try testing.expectEqual(Result.success, new( &lib.alloc.test_allocator, &t, - .{ .cols = 80, .rows = 24, .max_scrollback = 0 }, + 80, + 24, )); defer free(t); diff --git a/vendor/libghostty-vt/src/terminal/c/types-schema-verify.py b/vendor/libghostty-vt/src/terminal/c/types-schema-verify.py new file mode 100644 index 00000000..1a8e064d --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/c/types-schema-verify.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 +"""Validate the ABI manifest exported by a native or WebAssembly library.""" + +from __future__ import annotations + +import argparse +import ctypes +import json +import sys +from pathlib import Path +from typing import Any + +try: + import jsonschema +except ImportError as error: + raise SystemExit( + "missing ABI schema verifier dependencies; run this inside " + "`nix develop`" + ) from error + + +def load_native_manifest(path: Path) -> bytes: + """Call ghostty_type_json in a native shared library.""" + library = ctypes.CDLL(str(path.resolve())) + type_json = library.ghostty_type_json + type_json.argtypes = () + type_json.restype = ctypes.c_char_p + + result = type_json() + if result is None: + raise RuntimeError("ghostty_type_json returned NULL") + return result + + +def load_wasm_manifest(path: Path) -> bytes: + """Call ghostty_type_json and read its result from WebAssembly memory.""" + try: + import wasmtime + except ImportError as error: + raise SystemExit( + "missing WebAssembly verifier dependencies; run this inside " + "`nix develop`" + ) from error + + engine = wasmtime.Engine() + module = wasmtime.Module.from_file(engine, str(path)) + store = wasmtime.Store(engine) + instance = wasmtime.Instance(store, module, []) + exports = instance.exports(store) + memory = exports["memory"] + type_json = exports["ghostty_type_json"] + + pointer = type_json(store) + data = memory.read(store, pointer, memory.data_len(store)) + terminator = data.find(b"\0") + if terminator < 0: + raise RuntimeError("ghostty_type_json result is not NUL terminated") + return bytes(data[:terminator]) + + +def load_manifest(path: Path) -> dict[str, Any]: + """Execute the public export and decode its JSON result.""" + encoded = ( + load_wasm_manifest(path) + if path.suffix.lower() == ".wasm" + else load_native_manifest(path) + ) + value = json.loads(encoded) + if not isinstance(value, dict): + raise ValueError("ghostty_type_json did not return a JSON object") + return value + + +def format_path(parts: list[object]) -> str: + """Format a jsonschema error path for command-line output.""" + return "/" + "/".join(str(part) for part in parts) + + +def validate(schema_path: Path, library_path: Path) -> None: + """Validate the schema itself and the manifest returned by the library.""" + schema = json.loads(schema_path.read_bytes()) + validator_type = jsonschema.validators.validator_for(schema) + validator_type.check_schema(schema) + + manifest = load_manifest(library_path) + errors = sorted( + validator_type(schema).iter_errors(manifest), + key=lambda error: list(error.absolute_path), + ) + if errors: + for error in errors: + print( + f"{format_path(list(error.absolute_path))}: {error.message}", + file=sys.stderr, + ) + raise SystemExit(f"ABI manifest failed {schema_path}") + + abi = manifest["abi"] + print( + f"validated {abi['target']}-{abi['os']} ABI manifest " + f"({len(manifest['types'])} types)" + ) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("schema", type=Path) + parser.add_argument("library", type=Path) + args = parser.parse_args() + validate(args.schema, args.library) + + +if __name__ == "__main__": + main() diff --git a/vendor/libghostty-vt/src/terminal/c/types.schema.json b/vendor/libghostty-vt/src/terminal/c/types.schema.json new file mode 100644 index 00000000..38108ebe --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/c/types.schema.json @@ -0,0 +1,506 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "libghostty-vt ABI type manifest", + "description": "Schema for the JSON returned by ghostty_type_json.", + "type": "object", + "additionalProperties": false, + "required": ["schema", "abi", "library_version", "commit", "dirty", "types"], + "properties": { + "schema": { + "const": 1 + }, + "abi": { + "$ref": "#/$defs/abi" + }, + "library_version": { + "type": "string", + "minLength": 1 + }, + "commit": { + "type": ["string", "null"], + "minLength": 1 + }, + "dirty": { + "type": ["boolean", "null"] + }, + "types": { + "type": "object", + "minProperties": 1, + "propertyNames": { + "pattern": "^Ghostty[A-Za-z0-9_]+$" + }, + "additionalProperties": { + "$ref": "#/$defs/typeDescriptor" + } + } + }, + "$defs": { + "nonNegativeInteger": { + "type": "integer", + "minimum": 0 + }, + "positiveInteger": { + "type": "integer", + "minimum": 1 + }, + "typeReference": { + "type": "string", + "pattern": "^(Ghostty[A-Za-z0-9_]+|bool|f32|f64|function|[iu][1-9][0-9]*|opaque|void)$" + }, + "integerTypeReference": { + "type": "string", + "pattern": "^[iu][1-9][0-9]*$" + }, + "abi": { + "type": "object", + "additionalProperties": false, + "required": [ + "target", + "os", + "environment", + "pointer_size", + "usize_size", + "max_alignment", + "endian" + ], + "properties": { + "target": { + "type": "string", + "minLength": 1 + }, + "os": { + "type": "string", + "minLength": 1 + }, + "environment": { + "type": "string", + "minLength": 1 + }, + "pointer_size": { + "$ref": "#/$defs/positiveInteger" + }, + "usize_size": { + "$ref": "#/$defs/positiveInteger" + }, + "max_alignment": { + "description": "Maximum fundamental C ABI alignment in bytes.", + "$ref": "#/$defs/positiveInteger" + }, + "endian": { + "enum": ["little", "big"] + } + } + }, + "fieldBase": { + "type": "object", + "required": ["offset", "size", "type"], + "properties": { + "offset": { + "$ref": "#/$defs/nonNegativeInteger" + }, + "size": { + "$ref": "#/$defs/nonNegativeInteger" + } + } + }, + "plainField": { + "allOf": [ + { + "$ref": "#/$defs/fieldBase" + }, + { + "properties": { + "type": { + "$ref": "#/$defs/typeReference" + } + } + } + ], + "unevaluatedProperties": false + }, + "arrayField": { + "allOf": [ + { + "$ref": "#/$defs/fieldBase" + }, + { + "required": ["elem", "count"], + "properties": { + "type": { + "const": "array" + }, + "elem": { + "$ref": "#/$defs/typeReference" + }, + "count": { + "$ref": "#/$defs/nonNegativeInteger" + } + } + } + ], + "unevaluatedProperties": false + }, + "pointerField": { + "allOf": [ + { + "$ref": "#/$defs/fieldBase" + }, + { + "required": ["elem", "const"], + "properties": { + "type": { + "const": "pointer" + }, + "elem": { + "$ref": "#/$defs/typeReference" + }, + "const": { + "type": "boolean" + }, + "nullable": { + "const": true + } + } + } + ], + "unevaluatedProperties": false + }, + "taggedUnionField": { + "allOf": [ + { + "$ref": "#/$defs/fieldBase" + }, + { + "required": ["tag", "arms"], + "properties": { + "type": { + "$ref": "#/$defs/typeReference" + }, + "tag": { + "type": "string", + "minLength": 1 + }, + "arms": { + "type": "object", + "minProperties": 1, + "propertyNames": { + "pattern": "^[A-Z][A-Z0-9_]*$" + }, + "additionalProperties": { + "type": ["string", "null"] + } + } + } + } + ], + "unevaluatedProperties": false + }, + "field": { + "oneOf": [ + { + "$ref": "#/$defs/plainField" + }, + { + "$ref": "#/$defs/arrayField" + }, + { + "$ref": "#/$defs/pointerField" + }, + { + "$ref": "#/$defs/taggedUnionField" + } + ] + }, + "fields": { + "type": "object", + "additionalProperties": { + "$ref": "#/$defs/field" + } + }, + "bitBase": { + "type": "object", + "required": ["lsb", "width"], + "properties": { + "lsb": { + "description": "Least-significant bit relative to the containing numerical value.", + "$ref": "#/$defs/nonNegativeInteger" + }, + "width": { + "description": "Physical width of this field in bits.", + "$ref": "#/$defs/positiveInteger" + } + } + }, + "scalarBit": { + "allOf": [ + { + "$ref": "#/$defs/bitBase" + }, + { + "required": ["type"], + "properties": { + "type": { + "$ref": "#/$defs/typeReference" + } + } + } + ], + "unevaluatedProperties": false + }, + "packedBit": { + "allOf": [ + { + "$ref": "#/$defs/bitBase" + }, + { + "required": ["kind", "bits"], + "properties": { + "kind": { + "const": "packed" + }, + "bits": { + "$ref": "#/$defs/bits" + } + } + } + ], + "unevaluatedProperties": false + }, + "packedArm": { + "type": "object", + "additionalProperties": false, + "required": ["kind", "width", "bits"], + "properties": { + "kind": { + "const": "packed" + }, + "width": { + "description": "Physical width of this inline arm in bits.", + "$ref": "#/$defs/positiveInteger" + }, + "bits": { + "$ref": "#/$defs/bits" + } + } + }, + "taggedUnionBit": { + "allOf": [ + { + "$ref": "#/$defs/bitBase" + }, + { + "required": ["kind", "tag", "arms"], + "properties": { + "kind": { + "const": "union" + }, + "tag": { + "type": "string", + "minLength": 1 + }, + "arms": { + "type": "object", + "minProperties": 1, + "propertyNames": { + "pattern": "^[A-Z][A-Z0-9_]*$" + }, + "additionalProperties": { + "oneOf": [ + { + "$ref": "#/$defs/packedArm" + }, + { + "type": "null" + } + ] + } + } + } + } + ], + "unevaluatedProperties": false + }, + "bit": { + "oneOf": [ + { + "$ref": "#/$defs/scalarBit" + }, + { + "$ref": "#/$defs/packedBit" + }, + { + "$ref": "#/$defs/taggedUnionBit" + } + ] + }, + "bits": { + "type": "object", + "minProperties": 1, + "additionalProperties": { + "$ref": "#/$defs/bit" + } + }, + "descriptorBase": { + "type": "object", + "required": ["kind", "size", "align"], + "properties": { + "size": { + "$ref": "#/$defs/nonNegativeInteger" + }, + "align": { + "$ref": "#/$defs/positiveInteger" + } + } + }, + "structDescriptor": { + "allOf": [ + { + "$ref": "#/$defs/descriptorBase" + }, + { + "required": ["fields"], + "properties": { + "kind": { + "const": "struct" + }, + "fields": { + "$ref": "#/$defs/fields" + } + } + } + ], + "unevaluatedProperties": false + }, + "unionDescriptor": { + "allOf": [ + { + "$ref": "#/$defs/descriptorBase" + }, + { + "required": ["fields"], + "properties": { + "kind": { + "const": "union" + }, + "fields": { + "$ref": "#/$defs/fields" + } + } + } + ], + "unevaluatedProperties": false + }, + "enumDescriptor": { + "allOf": [ + { + "$ref": "#/$defs/descriptorBase" + }, + { + "required": ["underlying", "prefix", "values"], + "properties": { + "kind": { + "const": "enum" + }, + "underlying": { + "const": "i32" + }, + "prefix": { + "type": "string", + "minLength": 1 + }, + "values": { + "type": "object", + "minProperties": 1, + "propertyNames": { + "pattern": "^[A-Z][A-Z0-9_]*$" + }, + "additionalProperties": { + "type": "integer" + } + } + } + } + ], + "unevaluatedProperties": false + }, + "packedDescriptor": { + "allOf": [ + { + "$ref": "#/$defs/descriptorBase" + }, + { + "required": ["underlying", "bits"], + "properties": { + "kind": { + "const": "packed" + }, + "underlying": { + "description": "Integer backing type used to interpret the packed value.", + "$ref": "#/$defs/integerTypeReference" + }, + "bits": { + "$ref": "#/$defs/bits" + } + } + } + ], + "unevaluatedProperties": false + }, + "aliasDescriptor": { + "allOf": [ + { + "$ref": "#/$defs/descriptorBase" + }, + { + "required": ["type"], + "properties": { + "kind": { + "const": "alias" + }, + "type": { + "$ref": "#/$defs/typeReference" + } + } + } + ], + "unevaluatedProperties": false + }, + "opaqueDescriptor": { + "allOf": [ + { + "$ref": "#/$defs/descriptorBase" + }, + { + "properties": { + "kind": { + "const": "opaque" + } + } + } + ], + "unevaluatedProperties": false + }, + "typeDescriptor": { + "oneOf": [ + { + "$ref": "#/$defs/structDescriptor" + }, + { + "$ref": "#/$defs/unionDescriptor" + }, + { + "$ref": "#/$defs/enumDescriptor" + }, + { + "$ref": "#/$defs/packedDescriptor" + }, + { + "$ref": "#/$defs/aliasDescriptor" + }, + { + "$ref": "#/$defs/opaqueDescriptor" + } + ] + } + } +} diff --git a/vendor/libghostty-vt/src/terminal/c/types.zig b/vendor/libghostty-vt/src/terminal/c/types.zig index f160f385..4184de82 100644 --- a/vendor/libghostty-vt/src/terminal/c/types.zig +++ b/vendor/libghostty-vt/src/terminal/c/types.zig @@ -1,25 +1,54 @@ -//! Comptime-generated metadata describing the layout of all C API -//! extern structs for the current target. +//! Comptime-generated ABI metadata for the public libghostty-vt C types. //! -//! This is embedded in the binary as a const string and exposed via -//! `ghostty_type_json` so that WASM (and other FFI) consumers can -//! build structs without hardcoding byte offsets. +//! The manifest is embedded in the library and returned by +//! `ghostty_type_json`. It is intended for FFI consumers that cannot use the +//! C headers directly, most notably WebAssembly hosts. Its format is defined +//! by `types.schema.json`. const std = @import("std"); +const builtin = @import("builtin"); +const build_options = @import("terminal_options"); const lib = @import("../lib.zig"); +const c_abi = @import("../../lib/c_abi.zig"); + const color = @import("../color.zig"); -const color_c = @import("color.zig"); -const mouse_event = @import("mouse_event.zig"); +const clipboard = @import("../clipboard.zig"); +const device_status = @import("../device_status.zig"); +const focus_pkg = @import("../focus.zig"); +const formatter_pkg = @import("../formatter.zig"); +const modes_pkg = @import("../modes.zig"); +const mouse_pkg = @import("../mouse.zig"); +const page = @import("../page.zig"); const point = @import("../point.zig"); -const size_report = @import("size_report.zig"); +const Selection = @import("../Selection.zig"); +const sgr = @import("../sgr.zig"); -const terminal = @import("terminal.zig"); +const input_config = @import("../../input/config.zig"); +const input_key = @import("../../input/key.zig"); +const input_mouse = @import("../../input/mouse.zig"); + +const build_info = @import("build_info.zig"); +const cell = @import("cell.zig"); +const color_c = @import("color.zig"); const formatter = @import("formatter.zig"); +const grid_ref = @import("grid_ref.zig"); +const io = @import("io.zig"); +const key_encode = @import("key_encode.zig"); +const kitty_graphics = @import("kitty_graphics.zig"); +const mouse_encode = @import("mouse_encode.zig"); +const mouse_event = @import("mouse_event.zig"); +const osc = @import("osc.zig"); +const paste = @import("paste.zig"); +const render = @import("render.zig"); +const result = @import("result.zig"); +const row = @import("row.zig"); +const search = @import("search.zig"); const selection = @import("selection.zig"); const selection_gesture = @import("selection_gesture.zig"); -const render = @import("render.zig"); -const style_c = @import("style.zig"); -const mouse_encode = @import("mouse_encode.zig"); -const grid_ref = @import("grid_ref.zig"); +const size_report = @import("size_report.zig"); +const snapshot = @import("snapshot.zig"); +const style = @import("style.zig"); +const sys = @import("sys.zig"); +const terminal = @import("terminal.zig"); /// C: GhosttySurfacePosition pub const SurfacePosition = extern struct { @@ -33,233 +62,1113 @@ pub const Codepoints = extern struct { len: usize = 0, }; -/// All C API structs and their Ghostty C names. -pub const structs: std.StaticStringMap(StructInfo) = structs: { - @setEvalBranchQuota(10_000); - break :structs .initComptime(.{ - .{ "GhosttyBuffer", StructInfo.init(lib.Buffer) }, - .{ "GhosttyClipboardContent", StructInfo.init(terminal.ClipboardContent) }, - .{ "GhosttyClipboardWrite", StructInfo.init(terminal.ClipboardWrite) }, - .{ "GhosttyCodepoints", StructInfo.init(Codepoints) }, - .{ "GhosttyColorPaletteMask", StructInfo.init(color_c.PaletteMask) }, - .{ "GhosttyColorRgb", StructInfo.init(color.RGB.C) }, - .{ "GhosttyColorX11Entry", StructInfo.init(color_c.X11Entry) }, - .{ "GhosttyDeviceAttributes", StructInfo.init(terminal.DeviceAttributes) }, - .{ "GhosttyDeviceAttributesPrimary", StructInfo.init(terminal.DeviceAttributes.Primary) }, - .{ "GhosttyDeviceAttributesSecondary", StructInfo.init(terminal.DeviceAttributes.Secondary) }, - .{ "GhosttyDeviceAttributesTertiary", StructInfo.init(terminal.DeviceAttributes.Tertiary) }, - .{ "GhosttyFormatterTerminalOptions", StructInfo.init(formatter.TerminalOptions) }, - .{ "GhosttySelection", StructInfo.init(selection.CSelection) }, - .{ "GhosttyTerminalSelectWordOptions", StructInfo.init(selection.SelectWordOptions) }, - .{ "GhosttyTerminalSelectWordBetweenOptions", StructInfo.init(selection.SelectWordBetweenOptions) }, - .{ "GhosttyTerminalSelectLineOptions", StructInfo.init(selection.SelectLineOptions) }, - .{ "GhosttyFormatterTerminalExtra", StructInfo.init(formatter.TerminalOptions.Extra) }, - .{ "GhosttyFormatterScreenExtra", StructInfo.init(formatter.ScreenOptions.Extra) }, - .{ "GhosttyGridRef", StructInfo.init(grid_ref.CGridRef) }, - .{ "GhosttyMouseEncoderSize", StructInfo.init(mouse_encode.Size) }, - .{ "GhosttyMousePosition", StructInfo.init(mouse_event.Position) }, - .{ "GhosttyPoint", StructInfo.init(point.Point.C) }, - .{ "GhosttyPointCoordinate", StructInfo.init(point.Coordinate) }, - .{ "GhosttyRenderStateColors", StructInfo.init(render.Colors) }, - .{ "GhosttySelectionGestureBehaviors", StructInfo.init(selection_gesture.Behaviors) }, - .{ "GhosttySelectionGestureGeometry", StructInfo.init(selection_gesture.Geometry) }, - .{ "GhosttySizeReportSize", StructInfo.init(size_report.Size) }, - .{ "GhosttyString", StructInfo.init(lib.String) }, - .{ "GhosttySurfacePosition", StructInfo.init(SurfacePosition) }, - .{ "GhosttyStyle", StructInfo.init(style_c.Style) }, - .{ "GhosttyStyleColor", StructInfo.init(style_c.Color) }, - .{ "GhosttyTerminalOptions", StructInfo.init(terminal.Options) }, - .{ "GhosttyTerminalScrollbar", StructInfo.init(terminal.TerminalScrollbar) }, - .{ "GhosttyTerminalScrollViewport", StructInfo.init(terminal.ScrollViewport) }, - }); +const TypeDecl = struct { + name: []const u8, + T: type, + kind: Kind, + prefix: []const u8 = "", + sentinel_suffix: []const u8 = "MAX_VALUE", + alias_type: []const u8 = "", + tagged_union: ?TaggedUnion = null, + union_field_names_T: ?type = null, + + const Kind = enum { + @"struct", + @"union", + @"enum", + @"packed", + alias, + @"opaque", + }; + + const TaggedUnion = struct { + tag_field: []const u8, + value_field: []const u8, + arm_source: ArmSource, + + const ArmSource = enum { + fields, + generated, + }; + }; + + fn initStruct(comptime name: []const u8, comptime T: type) TypeDecl { + return .{ .name = name, .T = T, .kind = .@"struct" }; + } + + fn initTaggedStruct( + comptime name: []const u8, + comptime T: type, + comptime tag_field: []const u8, + comptime value_field: []const u8, + comptime arm_source: TaggedUnion.ArmSource, + ) TypeDecl { + return .{ + .name = name, + .T = T, + .kind = .@"struct", + .tagged_union = .{ + .tag_field = tag_field, + .value_field = value_field, + .arm_source = arm_source, + }, + }; + } + + fn initUnion( + comptime name: []const u8, + comptime T: type, + comptime field_names_T: ?type, + ) TypeDecl { + return .{ + .name = name, + .T = T, + .kind = .@"union", + .union_field_names_T = field_names_T, + }; + } + + fn initEnum( + comptime name: []const u8, + comptime T: type, + comptime prefix: []const u8, + ) TypeDecl { + return .{ .name = name, .T = T, .kind = .@"enum", .prefix = prefix }; + } + + fn initEnumSentinel( + comptime name: []const u8, + comptime T: type, + comptime prefix: []const u8, + comptime sentinel_suffix: []const u8, + ) TypeDecl { + return .{ + .name = name, + .T = T, + .kind = .@"enum", + .prefix = prefix, + .sentinel_suffix = sentinel_suffix, + }; + } + + fn initPacked(comptime name: []const u8, comptime T: type) TypeDecl { + return .{ .name = name, .T = T, .kind = .@"packed" }; + } + + fn initAlias( + comptime name: []const u8, + comptime T: type, + comptime alias_type: []const u8, + ) TypeDecl { + return .{ .name = name, .T = T, .kind = .alias, .alias_type = alias_type }; + } + + fn initOpaque(comptime name: []const u8) TypeDecl { + return .{ .name = name, .T = *anyopaque, .kind = .@"opaque" }; + } +}; + +/// Public C types. Names and enum prefixes are intentionally explicit because +/// Zig identifiers are not the C API contract. +const type_decls = [_]TypeDecl{ + .initStruct("GhosttyAllocator", lib.alloc.Allocator), + .initStruct("GhosttyAllocatorVtable", lib.alloc.VTable), + .initStruct("GhosttyBuffer", lib.Buffer), + .initStruct("GhosttyCellsView", cell.CellsView), + .initStruct("GhosttyClipboardContent", terminal.ClipboardContent), + .initStruct("GhosttyClipboardRead", terminal.ClipboardRead), + .initStruct("GhosttyClipboardReadReply", terminal.ClipboardReadReply), + .initStruct("GhosttyClipboardWrite", terminal.ClipboardWrite), + .initStruct("GhosttyClipboardWriteReply", terminal.ClipboardWriteReply), + .initStruct("GhosttyCodepoints", Codepoints), + .initStruct("GhosttyColorPaletteMask", color_c.PaletteMask), + .initStruct("GhosttyColorRgb", color.RGB.C), + .initStruct("GhosttyColorX11Entry", color_c.X11Entry), + .initStruct("GhosttyDeviceAttributes", terminal.DeviceAttributes), + .initStruct("GhosttyDeviceAttributesPrimary", terminal.DeviceAttributes.Primary), + .initStruct("GhosttyDeviceAttributesSecondary", terminal.DeviceAttributes.Secondary), + .initStruct("GhosttyDeviceAttributesTertiary", terminal.DeviceAttributes.Tertiary), + .initStruct("GhosttyFormatterScreenExtra", formatter.ScreenOptions.Extra), + .initStruct("GhosttyFormatterTerminalExtra", formatter.TerminalOptions.Extra), + .initStruct("GhosttyFormatterTerminalOptions", formatter.TerminalOptions), + .initStruct("GhosttyGridRef", grid_ref.CGridRef), + .initStruct("GhosttyKittyGraphicsPlacementRenderInfo", kitty_graphics.PlacementRenderInfo), + .initStruct("GhosttyMimeReader", io.MimeReader), + .initStruct("GhosttyMouseEncoderSize", mouse_encode.Size), + .initStruct("GhosttyMousePosition", mouse_event.Position), + .initStruct("GhosttyPaste", paste.Request), + .initTaggedStruct("GhosttyPoint", point.Point.C, "tag", "value", .generated), + .initStruct("GhosttyPointCoordinate", point.Coordinate), + .initUnion("GhosttyPointValue", point.Point.CValue, point.Point.C), + .initStruct("GhosttyReader", io.Reader), + .initStruct("GhosttyRenderStateColors", render.Colors), + .initStruct("GhosttyRenderStateCursor", render.Cursor), + .initStruct("GhosttyRenderStateRowSelection", render.RowSelection), + .initStruct("GhosttySelection", selection.CSelection), + .initStruct("GhosttySelectionBuffer", selection.CSelectionBuffer), + .initStruct("GhosttySelectionGestureBehaviors", selection_gesture.Behaviors), + .initStruct("GhosttySelectionGestureGeometry", selection_gesture.Geometry), + .initTaggedStruct("GhosttySgrAttribute", sgr.Attribute.C, "tag", "value", .generated), + .initStruct("GhosttySgrUnknown", sgr.Attribute.Unknown.C), + .initUnion("GhosttySgrAttributeValue", sgr.Attribute.CValue, sgr.Attribute.C), + .initStruct("GhosttySizeReportSize", size_report.Size), + .initStruct("GhosttyString", lib.String), + .initStruct("GhosttySurfacePosition", SurfacePosition), + .initStruct("GhosttyStyle", style.Style), + .initTaggedStruct("GhosttyStyleColor", style.Color, "tag", "value", .fields), + .initUnion("GhosttyStyleColorValue", style.ColorValue, null), + .initStruct("GhosttySysImage", sys.Image), + .initStruct("GhosttyTerminalDesktopNotification", terminal.DesktopNotification), + .initStruct("GhosttyTerminalModeConfig", terminal.ModeConfig), + .initStruct("GhosttyTerminalProgressReport", terminal.ProgressReport), + .initStruct("GhosttyTerminalScrollbar", terminal.TerminalScrollbar), + .initTaggedStruct("GhosttyTerminalScrollViewport", terminal.ScrollViewport, "tag", "value", .generated), + .initUnion( + "GhosttyTerminalScrollViewportValue", + terminal.ZigTerminal.ScrollViewport.CValue, + terminal.ZigTerminal.ScrollViewport.C, + ), + .initStruct("GhosttyTerminalSelectLineOptions", selection.SelectLineOptions), + .initStruct("GhosttyTerminalSelectWordBetweenOptions", selection.SelectWordBetweenOptions), + .initStruct("GhosttyTerminalSelectWordOptions", selection.SelectWordOptions), + .initStruct("GhosttyTerminalSelectionFormatOptions", selection.FormatOptions), + .initTaggedStruct("GhosttyTerminalUnknownSequence", terminal.UnknownSequence.C, "tag", "value", .generated), + .initStruct("GhosttyTerminalUnknownStringSequence", terminal.UnknownStringSequence), + .initUnion( + "GhosttyTerminalUnknownSequenceValue", + terminal.UnknownSequence.CValue, + terminal.UnknownSequence.C, + ), + .initStruct("GhosttyWriter", io.Writer), + + .initEnumSentinel("GhosttyResult", result.Result, "GHOSTTY_", "RESULT_MAX_VALUE"), + .initEnum("GhosttyBuildInfo", build_info.BuildInfo, "GHOSTTY_BUILD_INFO_"), + .initEnumSentinel("GhosttyOptimizeMode", build_info.OptimizeMode, "GHOSTTY_OPTIMIZE_", "MODE_MAX_VALUE"), + .initEnumSentinel("GhosttyCellContentTag", cell.ContentTag, "GHOSTTY_CELL_CONTENT_", "TAG_MAX_VALUE"), + .initEnum("GhosttyCellData", cell.CellData, "GHOSTTY_CELL_DATA_"), + .initEnum("GhosttyCellSemanticContent", cell.SemanticContent, "GHOSTTY_CELL_SEMANTIC_"), + .initEnum("GhosttyCellWide", cell.Wide, "GHOSTTY_CELL_WIDE_"), + .initEnum("GhosttyClipboardLocation", clipboard.Location, "GHOSTTY_CLIPBOARD_LOCATION_"), + .initEnum("GhosttyClipboardReadResult", clipboard.Read.Status, "GHOSTTY_CLIPBOARD_READ_RESULT_"), + .initEnum("GhosttyClipboardWriteResult", clipboard.Write.Status, "GHOSTTY_CLIPBOARD_WRITE_RESULT_"), + .initEnum("GhosttyColorScheme", device_status.ColorScheme, "GHOSTTY_COLOR_SCHEME_"), + .initEnum("GhosttyFocusEvent", focus_pkg.Event, "GHOSTTY_FOCUS_"), + .initEnum("GhosttyFormatterFormat", formatter_pkg.Format, "GHOSTTY_FORMATTER_FORMAT_"), + .initEnum("GhosttyKey", input_key.Key, "GHOSTTY_KEY_"), + .initEnum("GhosttyKeyAction", input_key.Action, "GHOSTTY_KEY_ACTION_"), + .initEnum("GhosttyKeyEncoderOption", key_encode.Option, "GHOSTTY_KEY_ENCODER_OPT_"), + .initEnum("GhosttyKittyGraphicsData", kitty_graphics.Data, "GHOSTTY_KITTY_GRAPHICS_DATA_"), + .initEnum("GhosttyKittyGraphicsImageData", kitty_graphics.ImageData, "GHOSTTY_KITTY_IMAGE_DATA_"), + .initEnum("GhosttyKittyGraphicsPlacementData", kitty_graphics.PlacementData, "GHOSTTY_KITTY_GRAPHICS_PLACEMENT_DATA_"), + .initEnum("GhosttyKittyGraphicsPlacementIteratorOption", kitty_graphics.PlacementIteratorOption, "GHOSTTY_KITTY_GRAPHICS_PLACEMENT_ITERATOR_OPTION_"), + .initEnum("GhosttyKittyImageCompression", kitty_graphics.ImageCompression, "GHOSTTY_KITTY_IMAGE_COMPRESSION_"), + .initEnum("GhosttyKittyImageFormat", kitty_graphics.ImageFormat, "GHOSTTY_KITTY_IMAGE_FORMAT_"), + .initEnum("GhosttyKittyPlacementLayer", kitty_graphics.PlacementLayer, "GHOSTTY_KITTY_PLACEMENT_LAYER_"), + .initEnum("GhosttyModeReportState", modes_pkg.Report.State, "GHOSTTY_MODE_REPORT_"), + .initEnum("GhosttyMouseAction", input_mouse.Action, "GHOSTTY_MOUSE_ACTION_"), + .initEnum("GhosttyMouseButton", input_mouse.Button, "GHOSTTY_MOUSE_BUTTON_"), + .initEnum("GhosttyMouseEncoderOption", mouse_encode.Option, "GHOSTTY_MOUSE_ENCODER_OPT_"), + .initEnum("GhosttyMouseFormat", mouse_pkg.Format, "GHOSTTY_MOUSE_FORMAT_"), + .initEnum("GhosttyMouseTrackingMode", mouse_pkg.Event, "GHOSTTY_MOUSE_TRACKING_"), + .initEnum("GhosttyOptionAsAlt", input_config.OptionAsAlt, "GHOSTTY_OPTION_AS_ALT_"), + .initEnum("GhosttyOscCommandData", osc.CommandData, "GHOSTTY_OSC_DATA_"), + .initEnumSentinel( + "GhosttyOscCommandType", + osc.CommandType, + "GHOSTTY_OSC_COMMAND_", + "TYPE_MAX_VALUE", + ), + .initEnum("GhosttyPasteSource", paste.Source, "GHOSTTY_PASTE_SOURCE_"), + .initEnum("GhosttyPointTag", point.Tag, "GHOSTTY_POINT_TAG_"), + .initEnum("GhosttyRenderStateCursorVisualStyle", render.CursorVisualStyle, "GHOSTTY_RENDER_STATE_CURSOR_VISUAL_STYLE_"), + .initEnum("GhosttyRenderStateData", render.Data, "GHOSTTY_RENDER_STATE_DATA_"), + .initEnum("GhosttyRenderStateDirty", render.Dirty, "GHOSTTY_RENDER_STATE_DIRTY_"), + .initEnum("GhosttyRenderStateOption", render.SetOption, "GHOSTTY_RENDER_STATE_OPTION_"), + .initEnum("GhosttyRenderStateRowCellsData", render.RowCellsData, "GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_"), + .initEnum("GhosttyRenderStateRowData", render.RowData, "GHOSTTY_RENDER_STATE_ROW_DATA_"), + .initEnum("GhosttyRenderStateRowOption", render.RowOption, "GHOSTTY_RENDER_STATE_ROW_OPTION_"), + .initEnum("GhosttyRowData", row.RowData, "GHOSTTY_ROW_DATA_"), + .initEnum("GhosttyRowSemanticPrompt", row.SemanticPrompt, "GHOSTTY_ROW_SEMANTIC_"), + .initEnum("GhosttySearchData", search.Data, "GHOSTTY_SEARCH_DATA_"), + .initEnum("GhosttySearchOption", search.Option, "GHOSTTY_SEARCH_OPT_"), + .initEnum("GhosttySearchScroll", search.Scroll, "GHOSTTY_SEARCH_SCROLL_"), + .initEnum("GhosttySearchStatus", search.Status, "GHOSTTY_SEARCH_STATUS_"), + .initEnum("GhosttySelectionAdjust", Selection.Adjustment, "GHOSTTY_SELECTION_ADJUST_"), + .initEnum("GhosttySelectionGestureAutoscroll", selection_gesture.Autoscroll, "GHOSTTY_SELECTION_GESTURE_AUTOSCROLL_"), + .initEnum("GhosttySelectionGestureBehavior", selection_gesture.Behavior, "GHOSTTY_SELECTION_GESTURE_BEHAVIOR_"), + .initEnum("GhosttySelectionGestureData", selection_gesture.Data, "GHOSTTY_SELECTION_GESTURE_DATA_"), + .initEnum("GhosttySelectionGestureEventOption", selection_gesture.EventOption, "GHOSTTY_SELECTION_GESTURE_EVENT_OPT_"), + .initEnum("GhosttySelectionGestureEventType", selection_gesture.EventType, "GHOSTTY_SELECTION_GESTURE_EVENT_TYPE_"), + .initEnum("GhosttySelectionOrder", Selection.Order, "GHOSTTY_SELECTION_ORDER_"), + .initEnum("GhosttySgrAttributeTag", sgr.Attribute.Tag, "GHOSTTY_SGR_ATTR_"), + .initEnum("GhosttySgrUnderline", sgr.Attribute.Underline, "GHOSTTY_SGR_UNDERLINE_"), + .initEnumSentinel("GhosttySizeReportStyle", size_report.Style, "GHOSTTY_SIZE_REPORT_", "STYLE_MAX_VALUE"), + .initEnum("GhosttySnapshotDecoderData", snapshot.DecoderData, "GHOSTTY_SNAPSHOT_DECODER_DATA_"), + .initEnum("GhosttySnapshotDecoderOption", snapshot.DecoderOption, "GHOSTTY_SNAPSHOT_DECODER_OPT_"), + .initEnumSentinel("GhosttyStyleColorTag", style.ColorTag, "GHOSTTY_STYLE_COLOR_", "TAG_MAX_VALUE"), + .initEnum("GhosttySysLogLevel", sys.LogLevel, "GHOSTTY_SYS_LOG_LEVEL_"), + .initEnum("GhosttySysOption", sys.Option, "GHOSTTY_SYS_OPT_"), + .initEnum("GhosttyTerminalCompressionMode", terminal.CompressionMode, "GHOSTTY_TERMINAL_COMPRESSION_MODE_"), + .initEnum("GhosttyTerminalCompressionResult", terminal.CompressionResult, "GHOSTTY_TERMINAL_COMPRESSION_RESULT_"), + .initEnum("GhosttyTerminalCursorStyle", terminal.TerminalCursorStyle, "GHOSTTY_TERMINAL_CURSOR_STYLE_"), + .initEnum("GhosttyTerminalData", terminal.TerminalData, "GHOSTTY_TERMINAL_DATA_"), + .initEnum("GhosttyTerminalOption", terminal.Option, "GHOSTTY_TERMINAL_OPT_"), + .initEnum("GhosttyTerminalProgressState", terminal.ProgressState, "GHOSTTY_TERMINAL_PROGRESS_STATE_"), + .initEnum("GhosttyTerminalScreen", terminal.TerminalScreen, "GHOSTTY_TERMINAL_SCREEN_"), + .initEnum("GhosttyTerminalScrollViewportTag", terminal.ZigTerminal.ScrollViewport.Tag, "GHOSTTY_SCROLL_VIEWPORT_"), + .initEnum("GhosttyTerminalUnknownSequenceTag", terminal.UnknownSequence.Tag, "GHOSTTY_TERMINAL_UNKNOWN_SEQUENCE_"), + + .initPacked("GhosttyCell", page.Cell.CLayout), + .initAlias("GhosttyColorPaletteIndex", u8, "u8"), + .initAlias("GhosttyKittyKeyFlags", u8, "u8"), + .initAlias("GhosttyMode", u16, "u16"), + .initAlias("GhosttyMods", u16, "u16"), + .initAlias("GhosttyRow", u64, "u64"), + .initAlias("GhosttyStyleId", u16, "u16"), + + .initOpaque("GhosttyFormatter"), + .initOpaque("GhosttyKeyEncoder"), + .initOpaque("GhosttyKeyEvent"), + .initOpaque("GhosttyKittyGraphics"), + .initOpaque("GhosttyKittyGraphicsImage"), + .initOpaque("GhosttyKittyGraphicsPlacementIterator"), + .initOpaque("GhosttyMouseEncoder"), + .initOpaque("GhosttyMouseEvent"), + .initOpaque("GhosttyOscCommand"), + .initOpaque("GhosttyOscParser"), + .initOpaque("GhosttyRenderState"), + .initOpaque("GhosttyRenderStateRowCells"), + .initOpaque("GhosttyRenderStateRowIterator"), + .initOpaque("GhosttySearch"), + .initOpaque("GhosttySelectionGesture"), + .initOpaque("GhosttySelectionGestureEvent"), + .initOpaque("GhosttySgrParser"), + .initOpaque("GhosttySnapshotDecoder"), + .initOpaque("GhosttyTerminal"), + .initOpaque("GhosttyTrackedGridRef"), }; -/// The comptime-generated JSON string of all structs. +comptime { + @setEvalBranchQuota(100_000); + for (type_decls, 0..) |decl, i| { + for (type_decls[0..i]) |previous| { + if (std.mem.eql(u8, decl.name, previous.name)) + @compileError("duplicate public ABI type: " ++ decl.name); + } + } +} + pub const json: [:0]const u8 = json: { - @setEvalBranchQuota(100000); + @setEvalBranchQuota(1_000_000); var counter: std.Io.Writer.Discarding = .init(&.{}); - jsonWriteAll(&counter.writer) catch unreachable; + Json.writeAll(&counter.writer) catch unreachable; var buf: [counter.count:0]u8 = undefined; var writer: std.Io.Writer = .fixed(&buf); - jsonWriteAll(&writer) catch unreachable; + Json.writeAll(&writer) catch unreachable; const final = buf; break :json final[0..writer.end :0]; }; -/// Returns a pointer to the comptime-generated JSON string describing -/// the layout of all C API extern structs, and writes its length to `len`. -/// Exported as `ghostty_type_json` for FFI consumers. pub fn get_json() callconv(lib.calling_conv) [*:0]const u8 { return json.ptr; } -/// Meta information about a struct that we expose to ease writing -/// bindings in some languages, particularly WASM where we can't -/// easily share struct definitions and need to hardcode byte offsets. -pub const StructInfo = struct { - name: []const u8, - size: usize, - @"align": usize, - fields: []const FieldInfo, - - pub const FieldInfo = struct { - name: []const u8, - offset: usize, - size: usize, - type: []const u8, - }; +const Json = struct { + fn writeAll(writer: *std.Io.Writer) std.Io.Writer.Error!void { + var jws: std.json.Stringify = .{ .writer = writer }; + try jws.beginObject(); + try jws.objectField("schema"); + try jws.write(1); - pub fn init(comptime T: type) StructInfo { - comptime { - const fields = @typeInfo(T).@"struct".fields; - const field_infos: [fields.len]FieldInfo = blk: { - var infos: [fields.len]FieldInfo = undefined; - for (fields, 0..) |field, i| infos[i] = .{ - .name = field.name, - .offset = @offsetOf(T, field.name), - .size = @sizeOf(field.type), - .type = typeName(field.type), - }; - break :blk infos; - }; + try jws.objectField("abi"); + try jws.beginObject(); + try jws.objectField("target"); + try jws.write(@tagName(builtin.target.cpu.arch)); + try jws.objectField("os"); + try jws.write(@tagName(builtin.target.os.tag)); + try jws.objectField("environment"); + try jws.write(@tagName(builtin.target.abi)); + try jws.objectField("pointer_size"); + try jws.write(@sizeOf(*anyopaque)); + try jws.objectField("usize_size"); + try jws.write(@sizeOf(usize)); + try jws.objectField("max_alignment"); + try jws.write(c_abi.max_alignment); + try jws.objectField("endian"); + try jws.write(@tagName(builtin.target.cpu.arch.endian())); + try jws.endObject(); - return .{ - .name = @typeName(T), - .size = @sizeOf(T), - .@"align" = @alignOf(T), - .fields = &field_infos, - }; + try jws.objectField("library_version"); + try jws.write(build_options.version_string); + try jws.objectField("commit"); + if (build_options.version_build) |commit| try jws.write(commit) else try jws.write(null); + try jws.objectField("dirty"); + try jws.write(null); + + try jws.objectField("types"); + try jws.beginObject(); + inline for (type_decls) |decl| { + try jws.objectField(decl.name); + try writeType(decl, &jws); } + try jws.endObject(); + try jws.endObject(); } - pub fn jsonStringify( - self: *const StructInfo, - jws: anytype, - ) std.Io.Writer.Error!void { + fn writeType(comptime decl: TypeDecl, jws: *std.json.Stringify) std.Io.Writer.Error!void { try jws.beginObject(); - try jws.objectField("size"); - try jws.write(self.size); - try jws.objectField("align"); - try jws.write(self.@"align"); - try jws.objectField("fields"); + try jws.objectField("kind"); + try jws.write(@tagName(decl.kind)); + + switch (decl.kind) { + .@"struct", .@"union" => { + try writeSizeAlign(decl.T, jws); + try jws.objectField("fields"); + try jws.beginObject(); + if (decl.union_field_names_T != null) { + try writeMappedUnionFields(decl, jws); + } else { + const fields = switch (decl.kind) { + .@"struct" => @typeInfo(decl.T).@"struct".fields, + .@"union" => @typeInfo(decl.T).@"union".fields, + else => unreachable, + }; + inline for (fields) |field| { + try writeField( + decl, + field.name, + field.type, + if (decl.kind == .@"union") 0 else @offsetOf(decl.T, field.name), + jws, + ); + } + } + try jws.endObject(); + }, + .@"enum" => { + try writeSizeAlign(c_int, jws); + try jws.objectField("underlying"); + try jws.write("i32"); + try jws.objectField("prefix"); + try jws.write(decl.prefix); + try jws.objectField("values"); + try jws.beginObject(); + inline for (@typeInfo(decl.T).@"enum".fields) |field| { + try writeEnumObjectField(decl.name, field.name, jws); + try jws.write(field.value); + } + try jws.objectField(decl.sentinel_suffix); + try jws.write(std.math.maxInt(c_int)); + try jws.endObject(); + }, + .@"packed" => try writePackedType(decl.T, jws), + .alias => { + try writeSizeAlign(decl.T, jws); + try jws.objectField("type"); + try jws.write(decl.alias_type); + }, + .@"opaque" => try writeSizeAlign(*anyopaque, jws), + } + try jws.endObject(); + } + + fn writePackedType( + comptime Layout: type, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + try writeSizeAlign(Layout.Zig, jws); + try jws.objectField("underlying"); + try jws.write(publicTypeName(Layout.Backing)); + try jws.objectField("bits"); try jws.beginObject(); - for (self.fields) |field| { - try jws.objectField(field.name); + try writePackedBits(Layout, jws); + try jws.endObject(); + } + + fn writePackedBits( + comptime Layout: type, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + inline for (@typeInfo(Layout.Zig).@"struct".fields) |field| { + const field_tag = @field(Layout.Field, field.name); + const name = comptime Layout.fieldName(field_tag) orelse continue; + const options = Layout.fieldOptions(field_tag); + + try jws.objectField(name); try jws.beginObject(); - try jws.objectField("offset"); - try jws.write(field.offset); - try jws.objectField("size"); - try jws.write(field.size); - try jws.objectField("type"); - try jws.write(field.type); + try jws.objectField("lsb"); + try jws.write(Layout.bitOffset(field_tag)); + try jws.objectField("width"); + try jws.write(Layout.bitWidth(field_tag)); + + switch (options.encoding) { + .scalar => { + try jws.objectField("type"); + try jws.write(options.type_name orelse publicTypeName(field.type)); + }, + .@"packed" => |Nested| { + try jws.objectField("kind"); + try jws.write("packed"); + try jws.objectField("bits"); + try jws.beginObject(); + try writePackedBits(Nested, jws); + try jws.endObject(); + }, + .tagged_union => |UnionLayout| try writePackedTaggedUnion(Layout, UnionLayout, jws), + } try jws.endObject(); } + } + + fn writePackedTaggedUnion( + comptime Layout: type, + comptime UnionLayout: type, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + try jws.objectField("kind"); + try jws.write("union"); + try jws.objectField("tag"); + try jws.write(Layout.fieldName(UnionLayout.tag_field).?); + try jws.objectField("arms"); + try jws.beginObject(); + + const tag_options = Layout.fieldOptions(UnionLayout.tag_field); + const tag_type_name = tag_options.type_name orelse publicTypeName(UnionLayout.Tag); + inline for (@typeInfo(UnionLayout.Tag).@"enum".fields) |tag| { + try writeEnumObjectField(tag_type_name, tag.name, jws); + const arm = UnionLayout.arm(@field(UnionLayout.Tag, tag.name)) orelse { + try jws.write(null); + continue; + }; + switch (arm) { + inline else => |ArmLayout| try writePackedArm(ArmLayout, jws), + } + } + + try jws.endObject(); + } + + fn writePackedArm( + comptime Layout: type, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + try jws.beginObject(); + try jws.objectField("kind"); + try jws.write("packed"); + try jws.objectField("width"); + try jws.write(@bitSizeOf(Layout.Zig)); + try jws.objectField("bits"); + try jws.beginObject(); + try writePackedBits(Layout, jws); try jws.endObject(); try jws.endObject(); } -}; -fn jsonWriteAll(writer: *std.Io.Writer) std.Io.Writer.Error!void { - var jws: std.json.Stringify = .{ .writer = writer }; - try jws.beginObject(); - for (structs.keys(), structs.values()) |name, *info| { + fn writeField( + comptime decl: TypeDecl, + comptime name: []const u8, + comptime T: type, + comptime offset: usize, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { try jws.objectField(name); - try info.jsonStringify(&jws); + try jws.beginObject(); + try jws.objectField("offset"); + try jws.write(offset); + try jws.objectField("size"); + try jws.write(@sizeOf(T)); + try writeFieldType(decl.name, name, T, jws); + if (decl.tagged_union) |tagged| { + if (std.mem.eql(u8, name, tagged.value_field)) { + try jws.objectField("tag"); + try jws.write(tagged.tag_field); + try writeTaggedArms(decl, tagged, jws); + } + } + try jws.endObject(); + } + + fn writeMappedUnionFields( + comptime decl: TypeDecl, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + const FieldNames = decl.union_field_names_T.?; + const Tag = @FieldType(FieldNames, "tag"); + const tag_fields = @typeInfo(Tag).@"enum".fields; + + inline for (tag_fields, 0..) |field, i| { + const name = comptime FieldNames.cFieldRename(@field(Tag, field.name)) orelse continue; + if (comptime mappedUnionFieldIsDuplicate(decl, i, name)) continue; + try writeField(decl, name, @FieldType(decl.T, field.name), 0, jws); + } + + if (@hasField(decl.T, "_padding")) + try writeField(decl, "_padding", @FieldType(decl.T, "_padding"), 0, jws); } - try jws.endObject(); -} -fn typeName(comptime T: type) []const u8 { - return switch (@typeInfo(T)) { - .bool => "bool", - .float => |info| switch (info.bits) { - 32 => "f32", - 64 => "f64", - else => @compileError("unsupported float size"), - }, - .int => |info| switch (info.signedness) { - .signed => switch (info.bits) { + fn mappedUnionFieldIsDuplicate( + comptime decl: TypeDecl, + comptime index: usize, + comptime name: []const u8, + ) bool { + const FieldNames = decl.union_field_names_T.?; + const Tag = @FieldType(FieldNames, "tag"); + const tag_fields = @typeInfo(Tag).@"enum".fields; + const T = @FieldType(decl.T, tag_fields[index].name); + + inline for (tag_fields[0..index]) |previous| { + const previous_name = FieldNames.cFieldRename(@field(Tag, previous.name)) orelse continue; + if (std.mem.eql(u8, previous_name, name)) { + if (@FieldType(decl.T, previous.name) != T) + @compileError("tagged union metadata maps different field types to " ++ name); + return true; + } + } + + return false; + } + + fn writeSizeAlign(comptime T: type, jws: *std.json.Stringify) std.Io.Writer.Error!void { + try jws.objectField("size"); + try jws.write(@sizeOf(T)); + try jws.objectField("align"); + try jws.write(@alignOf(T)); + } + + fn writeFieldType( + comptime owner: []const u8, + comptime field_name: []const u8, + comptime T: type, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + if (comptime std.mem.eql(u8, owner, "GhosttyCellsView") and + std.mem.eql(u8, field_name, "ptr")) + { + try writePointerTypeNamed(T, true, "GhosttyCell", jws); + return; + } + if (comptime std.mem.eql(u8, owner, "GhosttyGridRef") and + std.mem.eql(u8, field_name, "node")) + { + try writePointerTypeNamed(T, true, "opaque", jws); + return; + } + + if (comptime fieldTypeOverride(owner, field_name)) |name| { + try jws.objectField("type"); + try jws.write(name); + return; + } + + switch (@typeInfo(T)) { + .array => |info| { + try jws.objectField("type"); + try jws.write("array"); + try jws.objectField("elem"); + try jws.write(publicTypeName(info.child)); + try jws.objectField("count"); + try jws.write(info.len); + }, + .optional => |info| try writePointerType(info.child, true, jws), + .pointer => try writePointerType(T, false, jws), + else => { + try jws.objectField("type"); + try jws.write(publicTypeName(T)); + }, + } + } + + fn writePointerType( + comptime T: type, + comptime nullable: bool, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + const info = @typeInfo(T).pointer; + try jws.objectField("type"); + try jws.write("pointer"); + try jws.objectField("elem"); + try jws.write(publicTypeName(info.child)); + try jws.objectField("const"); + try jws.write(info.is_const); + if (nullable) { + try jws.objectField("nullable"); + try jws.write(true); + } + } + + fn writePointerTypeNamed( + comptime T: type, + comptime nullable: bool, + comptime elem: []const u8, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + const pointer_type = switch (@typeInfo(T)) { + .optional => |info| info.child, + .pointer => T, + else => unreachable, + }; + const info = @typeInfo(pointer_type).pointer; + try jws.objectField("type"); + try jws.write("pointer"); + try jws.objectField("elem"); + try jws.write(elem); + try jws.objectField("const"); + try jws.write(info.is_const); + if (nullable) { + try jws.objectField("nullable"); + try jws.write(true); + } + } + + fn fieldTypeOverride(comptime owner: []const u8, comptime field_name: []const u8) ?[]const u8 { + if (std.mem.eql(u8, field_name, "value")) { + if (std.mem.eql(u8, owner, "GhosttyPoint")) return "GhosttyPointValue"; + if (std.mem.eql(u8, owner, "GhosttySgrAttribute")) return "GhosttySgrAttributeValue"; + if (std.mem.eql(u8, owner, "GhosttyStyleColor")) return "GhosttyStyleColorValue"; + if (std.mem.eql(u8, owner, "GhosttyTerminalScrollViewport")) return "GhosttyTerminalScrollViewportValue"; + if (std.mem.eql(u8, owner, "GhosttyTerminalUnknownSequence")) return "GhosttyTerminalUnknownSequenceValue"; + } + if (std.mem.eql(u8, owner, "GhosttyStyleColorValue") and std.mem.eql(u8, field_name, "palette")) + return "GhosttyColorPaletteIndex"; + if (std.mem.eql(u8, owner, "GhosttySgrAttributeValue")) { + if (std.mem.eql(u8, field_name, "underline")) return "GhosttySgrUnderline"; + if (std.mem.endsWith(u8, field_name, "_8") or std.mem.endsWith(u8, field_name, "_256")) + return "GhosttyColorPaletteIndex"; + } + if (std.mem.eql(u8, owner, "GhosttyTerminalModeConfig") and std.mem.eql(u8, field_name, "mode")) + return "GhosttyMode"; + return null; + } + + fn writeTaggedArms( + comptime decl: TypeDecl, + comptime tagged: TypeDecl.TaggedUnion, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + const Tag = @FieldType(decl.T, tagged.tag_field); + try jws.objectField("arms"); + try jws.beginObject(); + inline for (@typeInfo(Tag).@"enum".fields) |field| { + try writeEnumObjectField(publicTypeName(Tag), field.name, jws); + if (comptime taggedArm(decl, field.name)) |arm| try jws.write(arm) else try jws.write(null); + } + try jws.endObject(); + } + + fn taggedArm(comptime decl: TypeDecl, comptime tag_name: []const u8) ?[]const u8 { + const tagged = decl.tagged_union.?; + const Tag = @FieldType(decl.T, tagged.tag_field); + if (tagged.arm_source == .generated) + return decl.T.cFieldRename(@field(Tag, tag_name)); + + const Value = @FieldType(decl.T, tagged.value_field); + if (!@hasField(Value, tag_name)) return null; + return if (@sizeOf(@FieldType(Value, tag_name)) == 0) null else tag_name; + } + + fn publicTypeName(comptime T: type) []const u8 { + inline for (type_decls) |decl| switch (decl.kind) { + .@"struct", .@"union", .@"enum" => if (T == decl.T) return decl.name, + .@"packed", .alias, .@"opaque" => {}, + }; + return switch (@typeInfo(T)) { + .bool => "bool", + .float => |info| switch (info.bits) { + 32 => "f32", + 64 => "f64", + else => "opaque", + }, + .int => |info| intName(info.signedness, info.bits), + .comptime_int => "comptime_int", + .void => "void", + .@"opaque" => "opaque", + .@"fn" => "function", + .@"enum" => "enum", + .@"struct" => "struct", + .@"union" => "union", + else => "opaque", + }; + } + + fn intName(comptime signedness: std.builtin.Signedness, comptime bits: u16) []const u8 { + return switch (signedness) { + .signed => switch (bits) { 8 => "i8", 16 => "i16", 32 => "i32", 64 => "i64", - else => @compileError("unsupported signed int size"), + else => std.fmt.comptimePrint("i{d}", .{bits}), }, - .unsigned => switch (info.bits) { + .unsigned => switch (bits) { 8 => "u8", 16 => "u16", 32 => "u32", 64 => "u64", - else => @compileError("unsupported unsigned int size"), + else => std.fmt.comptimePrint("u{d}", .{bits}), }, - }, - .@"enum" => "enum", - .@"struct" => "struct", - .pointer => "pointer", - .array => "array", - else => "opaque", - }; -} + }; + } -test "json parses" { - const parsed = try std.json.parseFromSlice( - std.json.Value, - std.testing.allocator, - json, - .{}, + fn writeUpperObjectField(comptime name: []const u8, jws: *std.json.Stringify) std.Io.Writer.Error!void { + var upper: [name.len]u8 = undefined; + for (name, 0..) |c, i| upper[i] = std.ascii.toUpper(c); + try jws.objectField(&upper); + } + + fn writeEnumObjectField( + comptime enum_name: []const u8, + comptime zig_name: []const u8, + jws: *std.json.Stringify, + ) std.Io.Writer.Error!void { + if (std.mem.eql(u8, enum_name, "GhosttyKey") and std.mem.startsWith(u8, zig_name, "key_")) + return writeUpperObjectField(zig_name["key_".len..], jws); + if (std.mem.eql(u8, enum_name, "GhosttySgrAttributeTag")) { + if (std.mem.eql(u8, zig_name, "256_underline_color")) return jws.objectField("UNDERLINE_COLOR_256"); + if (std.mem.eql(u8, zig_name, "8_bg")) return jws.objectField("BG_8"); + if (std.mem.eql(u8, zig_name, "8_fg")) return jws.objectField("FG_8"); + if (std.mem.eql(u8, zig_name, "8_bright_bg")) return jws.objectField("BRIGHT_BG_8"); + if (std.mem.eql(u8, zig_name, "8_bright_fg")) return jws.objectField("BRIGHT_FG_8"); + if (std.mem.eql(u8, zig_name, "256_bg")) return jws.objectField("BG_256"); + if (std.mem.eql(u8, zig_name, "256_fg")) return jws.objectField("FG_256"); + } + if (std.mem.eql(u8, enum_name, "GhosttyTerminalOption") and std.mem.eql(u8, zig_name, "size_cb")) + return jws.objectField("SIZE"); + return writeUpperObjectField(zig_name, jws); + } + + fn isBuiltinType(name: []const u8) bool { + const builtins = [_][]const u8{ + "array", "bool", "f32", "f64", "function", "i8", "i16", "i32", + "i64", "opaque", "pointer", "u8", "u16", "u32", "u64", "void", + }; + for (builtins) |builtin_name| { + if (std.mem.eql(u8, name, builtin_name)) return true; + } + if (name.len > 1 and (name[0] == 'i' or name[0] == 'u') and + name[1] >= '1' and name[1] <= '9') + { + for (name[2..]) |c| if (!std.ascii.isDigit(c)) return false; + return true; + } + return false; + } +}; + +test "manifest parses and is versioned" { + const parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, json, .{}); + defer parsed.deinit(); + const root = parsed.value.object; + try std.testing.expectEqual(@as(i64, 1), root.get("schema").?.integer); + try std.testing.expect(root.contains("abi")); + try std.testing.expectEqual( + @as(i64, c_abi.max_alignment), + root.get("abi").?.object.get("max_alignment").?.integer, ); + try std.testing.expect(root.contains("library_version")); + const manifest_types = root.get("types").?.object; + try std.testing.expectEqual(type_decls.len, manifest_types.count()); + inline for (type_decls) |decl| try std.testing.expect(manifest_types.contains(decl.name)); + + const cursor_fields = manifest_types.get("GhosttyRenderStateCursor").?.object + .get("fields").?.object; + try std.testing.expect(cursor_fields.contains("size")); + try std.testing.expect(cursor_fields.contains("viewport_has_value")); + try std.testing.expect(cursor_fields.contains("viewport_x")); + try std.testing.expect(cursor_fields.contains("viewport_y")); + try std.testing.expect(cursor_fields.contains("wide_tail")); + try std.testing.expect(cursor_fields.contains("visible")); + try std.testing.expect(cursor_fields.contains("blinking")); + try std.testing.expect(cursor_fields.contains("password_input")); + try std.testing.expect(cursor_fields.contains("visual_style")); +} + +test "manifest describes enums, arrays, and tagged unions" { + const parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, json, .{}); defer parsed.deinit(); + const manifest_types = parsed.value.object.get("types").?.object; - const root = parsed.value.object; + const data = manifest_types.get("GhosttyRenderStateData").?.object; + try std.testing.expectEqualStrings("enum", data.get("kind").?.string); + try std.testing.expectEqual(@as(i64, @intFromEnum(render.Data.dirty)), data.get("values").?.object.get("DIRTY").?.integer); - // Verify we have all expected structs - try std.testing.expect(root.contains("GhosttyClipboardContent")); - try std.testing.expect(root.contains("GhosttyClipboardWrite")); - try std.testing.expect(root.contains("GhosttyTerminalOptions")); - try std.testing.expect(root.contains("GhosttyFormatterTerminalOptions")); - - const clipboard_content = root.get("GhosttyClipboardContent").?.object; - const clipboard_content_fields = clipboard_content.get("fields").?.object; - try std.testing.expect(clipboard_content_fields.contains("mime")); - try std.testing.expect(clipboard_content_fields.contains("data")); - - const clipboard_write = root.get("GhosttyClipboardWrite").?.object; - const clipboard_write_fields = clipboard_write.get("fields").?.object; - try std.testing.expect(clipboard_write_fields.contains("size")); - try std.testing.expect(clipboard_write_fields.contains("location")); - try std.testing.expect(clipboard_write_fields.contains("contents")); - try std.testing.expect(clipboard_write_fields.contains("contents_len")); - - // Verify GhosttyTerminalOptions fields - const term_opts = root.get("GhosttyTerminalOptions").?.object; - try std.testing.expect(term_opts.contains("size")); - try std.testing.expect(term_opts.contains("align")); - try std.testing.expect(term_opts.contains("fields")); - - const fields = term_opts.get("fields").?.object; - try std.testing.expect(fields.contains("cols")); - try std.testing.expect(fields.contains("rows")); - try std.testing.expect(fields.contains("max_scrollback")); - - // Verify field offsets make sense (cols should be at 0) - const cols = fields.get("cols").?.object; - try std.testing.expectEqual(0, cols.get("offset").?.integer); + const colors = manifest_types.get("GhosttyRenderStateColors").?.object; + const palette = colors.get("fields").?.object.get("palette").?.object; + try std.testing.expectEqualStrings("array", palette.get("type").?.string); + try std.testing.expectEqualStrings("GhosttyColorRgb", palette.get("elem").?.string); + try std.testing.expectEqual(@as(i64, 256), palette.get("count").?.integer); + + const style_color = manifest_types.get("GhosttyStyleColor").?.object; + const value = style_color.get("fields").?.object.get("value").?.object; + try std.testing.expectEqualStrings("GhosttyStyleColorValue", value.get("type").?.string); + try std.testing.expectEqualStrings("tag", value.get("tag").?.string); + try std.testing.expectEqualStrings("palette", value.get("arms").?.object.get("PALETTE").?.string); + try std.testing.expect(value.get("arms").?.object.get("NONE").? == .null); } -test "struct sizes are non-zero" { - const parsed = try std.json.parseFromSlice( - std.json.Value, - std.testing.allocator, - json, - .{}, +test "manifest describes the complete packed cell layout" { + const parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, json, .{}); + defer parsed.deinit(); + const manifest_types = parsed.value.object.get("types").?.object; + const descriptor = manifest_types.get("GhosttyCell").?.object; + + try std.testing.expectEqualStrings("packed", descriptor.get("kind").?.string); + try std.testing.expectEqual(@as(i64, @sizeOf(page.Cell.CLayout.Zig)), descriptor.get("size").?.integer); + try std.testing.expectEqualStrings("u64", descriptor.get("underlying").?.string); + + const bits = descriptor.get("bits").?.object; + inline for (@typeInfo(page.Cell.CLayout.Zig).@"struct".fields) |field| { + const field_tag = @field(page.Cell.CLayout.Field, field.name); + const name = comptime page.Cell.CLayout.fieldName(field_tag); + if (name) |public_name| { + const bit = bits.get(public_name).?.object; + try std.testing.expectEqual( + @as(i64, @intCast(@bitOffsetOf(page.Cell.CLayout.Zig, field.name))), + bit.get("lsb").?.integer, + ); + try std.testing.expectEqual( + @as(i64, @intCast(@bitSizeOf(field.type))), + bit.get("width").?.integer, + ); + } else { + try std.testing.expect(!bits.contains(field.name)); + } + } + + const content = bits.get("content").?.object; + try std.testing.expectEqualStrings("union", content.get("kind").?.string); + try std.testing.expectEqualStrings("content_tag", content.get("tag").?.string); + const arms = content.get("arms").?.object; + + const Content = @FieldType(page.Cell.CLayout.Zig, "content"); + const Codepoint = @FieldType(Content, "codepoint"); + const codepoint = arms.get("CODEPOINT").?.object; + const grapheme = arms.get("CODEPOINT_GRAPHEME").?.object; + try expectPackedArmField(codepoint, "codepoint", Codepoint, "data", "u21"); + try expectPackedArmField(grapheme, "codepoint", Codepoint, "data", "u21"); + try expectPackedArmField( + arms.get("BG_COLOR_PALETTE").?.object, + "index", + @FieldType(Content, "color_palette"), + "data", + "GhosttyColorPaletteIndex", ); + + const rgb = arms.get("BG_COLOR_RGB").?.object; + const Rgb = @FieldType(Content, "color_rgb"); + inline for (@typeInfo(Rgb).@"struct".fields) |field| + try expectPackedArmField(rgb, field.name, Rgb, field.name, "u8"); +} + +test "manifest packed cell layouts decode real values" { + const parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, json, .{}); + defer parsed.deinit(); + const manifest_types = parsed.value.object.get("types").?.object; + const cell_bits = manifest_types.get("GhosttyCell").?.object.get("bits").?.object; + + var codepoint = page.Cell.CLayout.Zig.init('A'); + try expectDecodedCellContent(manifest_types, cell_bits, @bitCast(codepoint), "codepoint", 'A'); + + codepoint.content_tag = .codepoint_grapheme; + try expectDecodedCellContent(manifest_types, cell_bits, @bitCast(codepoint), "codepoint", 'A'); + + var palette: page.Cell.CLayout.Zig = @bitCast(@as(u64, 0)); + palette.content_tag = .bg_color_palette; + palette.content = .{ .color_palette = .{ .data = 173 } }; + try expectDecodedCellContent(manifest_types, cell_bits, @bitCast(palette), "index", 173); + + var rgb: page.Cell.CLayout.Zig = @bitCast(@as(u64, 0)); + rgb.content_tag = .bg_color_rgb; + rgb.content = .{ .color_rgb = .{ .r = 0x12, .g = 0x34, .b = 0x56 } }; + const rgb_arm = activeCellContentArm(manifest_types, cell_bits, @bitCast(rgb)); + const content = extractManifestBits(@bitCast(rgb), cell_bits.get("content").?.object); + try std.testing.expectEqual(@as(u64, 0x12), extractManifestBits(content, rgb_arm.get("bits").?.object.get("r").?.object)); + try std.testing.expectEqual(@as(u64, 0x34), extractManifestBits(content, rgb_arm.get("bits").?.object.get("g").?.object)); + try std.testing.expectEqual(@as(u64, 0x56), extractManifestBits(content, rgb_arm.get("bits").?.object.get("b").?.object)); +} + +test "manifest uses public enum names" { + const parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, json, .{}); defer parsed.deinit(); + const manifest_types = parsed.value.object.get("types").?.object; + + const cell_content = manifest_types.get("GhosttyCellContentTag").?.object.get("values").?.object; + try std.testing.expect(cell_content.contains("TAG_MAX_VALUE")); + try std.testing.expect(!cell_content.contains("MAX_VALUE")); + + const sgr_values = manifest_types.get("GhosttySgrAttributeTag").?.object.get("values").?.object; + try std.testing.expectEqual(@as(i64, 9), sgr_values.get("UNDERLINE_COLOR_256").?.integer); + try std.testing.expectEqual(@as(i64, 23), sgr_values.get("BG_8").?.integer); + try std.testing.expect(!sgr_values.contains("256_UNDERLINE_COLOR")); + + const sgr_arms = manifest_types.get("GhosttySgrAttribute").?.object + .get("fields").?.object.get("value").?.object.get("arms").?.object; + try std.testing.expectEqualStrings("underline_color_256", sgr_arms.get("UNDERLINE_COLOR_256").?.string); + try std.testing.expectEqualStrings("bg_8", sgr_arms.get("BG_8").?.string); + + const terminal_values = manifest_types.get("GhosttyTerminalOption").?.object.get("values").?.object; + try std.testing.expectEqual(@as(i64, 6), terminal_values.get("SIZE").?.integer); + try std.testing.expect(!terminal_values.contains("SIZE_CB")); + + const osc_values = manifest_types.get("GhosttyOscCommandType").?.object.get("values").?.object; + try std.testing.expectEqual(@as(i64, 22), osc_values.get("KITTY_TEXT_SIZING").?.integer); + try std.testing.expectEqual(@as(i64, 23), osc_values.get("KITTY_CLIPBOARD_PROTOCOL").?.integer); + try std.testing.expectEqual(@as(i64, 24), osc_values.get("KITTY_DND_PROTOCOL").?.integer); + try std.testing.expectEqual(@as(i64, 25), osc_values.get("CONTEXT_SIGNAL").?.integer); + try std.testing.expectEqual(@as(i64, 26), osc_values.get("KITTY_DESKTOP_NOTIFICATION").?.integer); + try std.testing.expect(osc_values.contains("TYPE_MAX_VALUE")); + try std.testing.expect(!osc_values.contains("MAX_VALUE")); + + const key_values = manifest_types.get("GhosttyKey").?.object.get("values").?.object; + try std.testing.expect(key_values.contains("A")); + try std.testing.expect(!key_values.contains("KEY_A")); - var it = parsed.value.object.iterator(); - while (it.next()) |entry| { - const struct_info = entry.value_ptr.object; - const size = struct_info.get("size").?.integer; - try std.testing.expect(size > 0); + const mode_values = manifest_types.get("GhosttyModeReportState").?.object.get("values").?.object; + try std.testing.expectEqual(@as(i64, 0), mode_values.get("NOT_RECOGNIZED").?.integer); + try std.testing.expectEqual(@as(i64, 4), mode_values.get("PERMANENTLY_RESET").?.integer); +} + +test "manifest named references resolve" { + const parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, json, .{}); + defer parsed.deinit(); + const manifest_types = parsed.value.object.get("types").?.object; + + var type_iterator = manifest_types.iterator(); + while (type_iterator.next()) |type_entry| { + const descriptor = type_entry.value_ptr.object; + if (descriptor.get("bits")) |bits_value| { + try expectManifestBitsValid( + manifest_types, + bits_value.object, + @intCast(descriptor.get("size").?.integer * 8), + ); + } + const fields_value = descriptor.get("fields") orelse continue; + var field_iterator = fields_value.object.iterator(); + while (field_iterator.next()) |field_entry| { + const field = field_entry.value_ptr.object; + const field_type = field.get("type").?.string; + if (!Json.isBuiltinType(field_type)) + try std.testing.expect(manifest_types.contains(field_type)); + + if (field.get("elem")) |elem_value| { + const elem = elem_value.string; + if (!Json.isBuiltinType(elem)) + try std.testing.expect(manifest_types.contains(elem)); + } + + if (field.get("arms")) |arms_value| { + const union_descriptor = manifest_types.get(field_type).?.object; + const union_fields = union_descriptor.get("fields").?.object; + const tag_field_name = field.get("tag").?.string; + const tag_type_name = fields_value.object.get(tag_field_name).?.object.get("type").?.string; + const tag_values = manifest_types.get(tag_type_name).?.object.get("values").?.object; + var arm_iterator = arms_value.object.iterator(); + while (arm_iterator.next()) |arm_entry| { + try std.testing.expect(tag_values.contains(arm_entry.key_ptr.*)); + if (arm_entry.value_ptr.* == .null) continue; + try std.testing.expect(union_fields.contains(arm_entry.value_ptr.string)); + } + } + } + } +} + +fn expectPackedArmField( + arm: std.json.ObjectMap, + manifest_name: []const u8, + comptime T: type, + comptime zig_name: []const u8, + expected_type: []const u8, +) !void { + try std.testing.expectEqualStrings("packed", arm.get("kind").?.string); + try std.testing.expectEqual(@as(i64, @bitSizeOf(T)), arm.get("width").?.integer); + const bit = arm.get("bits").?.object.get(manifest_name).?.object; + try std.testing.expectEqual(@as(i64, @bitOffsetOf(T, zig_name)), bit.get("lsb").?.integer); + try std.testing.expectEqual(@as(i64, @bitSizeOf(@FieldType(T, zig_name))), bit.get("width").?.integer); + try std.testing.expectEqualStrings(expected_type, bit.get("type").?.string); +} + +fn extractManifestBits(value: u64, bit: std.json.ObjectMap) u64 { + const lsb: u6 = @intCast(bit.get("lsb").?.integer); + const width: u7 = @intCast(bit.get("width").?.integer); + const mask = if (width == 64) + std.math.maxInt(u64) + else + (@as(u64, 1) << @intCast(width)) - 1; + return (value >> lsb) & mask; +} + +fn activeCellContentArm( + manifest_types: std.json.ObjectMap, + cell_bits: std.json.ObjectMap, + raw: u64, +) std.json.ObjectMap { + const content_tag = cell_bits.get("content_tag").?.object; + const value = extractManifestBits(raw, content_tag); + const enum_values = manifest_types.get(content_tag.get("type").?.string).?.object + .get("values").?.object; + var iterator = enum_values.iterator(); + while (iterator.next()) |entry| { + if (entry.value_ptr.integer == value) + return cell_bits.get("content").?.object.get("arms").?.object + .get(entry.key_ptr.*).?.object; + } + unreachable; +} + +fn expectDecodedCellContent( + manifest_types: std.json.ObjectMap, + cell_bits: std.json.ObjectMap, + raw: u64, + field_name: []const u8, + expected: u64, +) !void { + const arm = activeCellContentArm(manifest_types, cell_bits, raw); + const content = extractManifestBits(raw, cell_bits.get("content").?.object); + try std.testing.expectEqual( + expected, + extractManifestBits(content, arm.get("bits").?.object.get(field_name).?.object), + ); +} + +fn expectManifestBitsValid( + manifest_types: std.json.ObjectMap, + bits: std.json.ObjectMap, + container_width: usize, +) !void { + var iterator = bits.iterator(); + while (iterator.next()) |entry| { + const bit = entry.value_ptr.object; + const lsb: usize = @intCast(bit.get("lsb").?.integer); + const width: usize = @intCast(bit.get("width").?.integer); + try std.testing.expect(lsb + width <= container_width); + + if (bit.get("type")) |type_value| { + const type_name = type_value.string; + try std.testing.expect(Json.isBuiltinType(type_name) or manifest_types.contains(type_name)); + continue; + } + + const kind = bit.get("kind").?.string; + if (std.mem.eql(u8, kind, "packed")) { + try expectManifestBitsValid(manifest_types, bit.get("bits").?.object, width); + continue; + } + + try std.testing.expectEqualStrings("union", kind); + const tag_name = bit.get("tag").?.string; + const tag = bits.get(tag_name).?.object; + const enum_values = manifest_types.get(tag.get("type").?.string).?.object + .get("values").?.object; + var arm_iterator = bit.get("arms").?.object.iterator(); + while (arm_iterator.next()) |arm_entry| { + try std.testing.expect(enum_values.contains(arm_entry.key_ptr.*)); + if (arm_entry.value_ptr.* == .null) continue; + const arm = arm_entry.value_ptr.object; + const arm_width: usize = @intCast(arm.get("width").?.integer); + try std.testing.expect(arm_width <= width); + try expectManifestBitsValid(manifest_types, arm.get("bits").?.object, arm_width); + } } } diff --git a/vendor/libghostty-vt/src/terminal/clipboard.zig b/vendor/libghostty-vt/src/terminal/clipboard.zig index 554ca984..0a1c5a7a 100644 --- a/vendor/libghostty-vt/src/terminal/clipboard.zig +++ b/vendor/libghostty-vt/src/terminal/clipboard.zig @@ -1,3 +1,5 @@ +const std = @import("std"); + /// The clipboard destination for a write. pub const Location = enum(c_int) { standard = 0, @@ -6,6 +8,21 @@ pub const Location = enum(c_int) { _, }; +/// MIME types that name plain text across the platforms terminals run +/// on. We accept the union everywhere since serving text under any of these +/// names is harmless. +pub fn isTextMime(mime: []const u8) bool { + const names: []const []const u8 = &.{ + "text/plain", + "text/plain;charset=utf-8", + "UTF8_STRING", + "TEXT", + "STRING", + }; + for (names) |n| if (std.mem.eql(u8, mime, n)) return true; + return false; +} + /// A single representation of clipboard data. /// /// The MIME type and data are borrowed and only valid for the duration of a @@ -15,22 +32,225 @@ pub const Content = struct { data: []const u8, }; -/// One atomic clipboard write. +/// Requests content of a specific mime-type. For now this is used for +/// on-demand clipboard access since content can be large (particularly +/// non-text content), but it is generic so that this could handle other +/// mime-typed sources in the future like maybe drag-and-drop. /// -/// Contents are borrowed and only valid for the duration of a clipboard write -/// callback. An empty contents slice clears the destination. +/// C: GhosttyMimeReader +pub const MimeReader = struct { + /// Passed through to `read_fn`. + ctx: ?*anyopaque = null, + + /// Write all the data of the representation named by `mime` to + /// `sink`, in as many writes as is convenient. The mime and sink + /// are borrowed, only valid for the duration of the call, and + /// nothing written to the sink is retained, so the data may be + /// borrowed from anywhere. Return error.ReadFailed if the data + /// can't be produced and propagate error.WriteFailed from the + /// sink. + read_fn: *const fn ( + ctx: ?*anyopaque, + mime: []const u8, + sink: *std.Io.Writer, + ) Error!void, + + pub const Error = error{ + /// The data could not be read. + ReadFailed, + } || std.Io.Writer.Error; + + pub fn read( + self: MimeReader, + mime: []const u8, + sink: *std.Io.Writer, + ) Error!void { + return self.read_fn(self.ctx, mime, sink); + } +}; + +/// A request from the running program to write a clipboard. +/// +/// Writes are synchronous: the effect callback must answer through `reply` +/// before it returns, and the request (including its reply context) is +/// invalid afterwards. An embedder that needs user consent must block until +/// it has an answer; the VT stream waits with it. Protocols without a write +/// acknowledgement (OSC 52, OSC 1337 Copy) discard the reply. +/// +/// Contents are borrowed and only valid for the duration of the callback. +/// An empty contents slice clears the destination. pub const Write = struct { location: Location, contents: []const Content, + + /// Name of the writing program for permission prompts, if the + /// protocol carries one. Empty otherwise. + name: []const u8, + + /// True if the terminal already holds a session grant for this + /// request (kitty clipboard protocol passwords). The embedder should + /// skip any permission prompt and perform the write. + granted: bool, + + /// True if the program supplied a session password, so the embedder + /// may offer to remember the user's decision via + /// Result.Success.remember. When false, remember is ignored. + can_remember: bool, + + /// Terminal-owned reply state, only valid during the callback. + reply_ctx: *anyopaque, + reply_fn: *const fn (*anyopaque, Result) void, + + /// Answer the write. May be called at most once; later calls are + /// ignored. + pub fn reply(self: Write, result: Result) void { + self.reply_fn(self.reply_ctx, result); + } + + /// The status of a clipboard write reply. + pub const Status = enum(c_int) { + success = 0, + denied = 1, + unsupported = 2, + busy = 3, + invalid_data = 4, + io_error = 5, + _, + }; + + /// The reply to a clipboard write. + pub const Result = union(enum) { + /// The write was denied by policy or the user. + denied, + + /// The embedder cannot write this clipboard. + unsupported, + + /// The clipboard is temporarily unavailable. + busy, + + /// One or more representations contain invalid data. + invalid_data, + + /// Writing the clipboard failed. + io_error, + + /// The write succeeded. + success: Success, + + pub const Success = struct { + /// Record a session grant so future requests from the same + /// program skip the permission prompt. Only honored when the + /// request set `can_remember`. + remember: bool = false, + }; + }; }; -/// The result of a clipboard write. -pub const WriteResult = enum(c_int) { - success = 0, - denied = 1, - unsupported = 2, - busy = 3, - invalid_data = 4, - io_error = 5, - _, +/// A request from the running program to read a clipboard. +/// +/// Reads are synchronous: the effect callback must answer through `reply` +/// before it returns, and the request (including its reply context) is +/// invalid afterwards. An embedder that needs user consent must block until +/// it has an answer; the VT stream waits with it. +pub const Read = struct { + location: Location, + + /// The MIME types the program wants, in order of preference. The + /// reply should carry every requested representation the clipboard + /// has; unrequested ones are ignored. Protocols that only carry text + /// (OSC 52) request "text/plain". + mimes: []const []const u8, + + /// The program also wants the list of MIME types available on the + /// clipboard, delivered as Result.Success.available. + list: bool, + + /// Name of the requesting program for permission prompts, if the + /// protocol carries one. Empty otherwise. + name: []const u8, + + /// True if the terminal already holds a session grant for this + /// request (kitty clipboard protocol passwords). The embedder should + /// skip any permission prompt and serve the read. + /// + /// Always false when mimes is empty: such a request is served + /// without a prompt (kitty's targets-listing exemption), so the + /// terminal never consults grants for it and a one-time password + /// is preserved for the follow-up data read. + granted: bool, + + /// True if the program supplied a session password, so the embedder + /// may offer to remember the user's decision via + /// Result.Success.remember. When false, remember is ignored. + can_remember: bool, + + /// Terminal-owned reply state, only valid during the callback. + /// + /// The result is delivered through a call rather than returned so + /// the terminal consumes it while the embedder's memory is still + /// alive; a returned slice would have to outlive the callback. + reply_ctx: *anyopaque, + reply_fn: *const fn (*anyopaque, Result) void, + + /// Answer the read. May be called at most once; later calls are + /// ignored. Result memory is borrowed only for the duration of this + /// call. + pub fn reply(self: Read, result: Result) void { + self.reply_fn(self.reply_ctx, result); + } + + /// The status of a clipboard read reply. + pub const Status = enum(c_int) { + success = 0, + denied = 1, + unsupported = 2, + busy = 3, + io_error = 4, + _, + }; + + /// The reply to a clipboard read. + pub const Result = union(enum) { + /// The read was denied by policy or the user. + denied, + + /// The embedder cannot read this clipboard. + unsupported, + + /// The clipboard is temporarily unavailable. + busy, + + /// Reading the clipboard failed. + io_error, + + /// The read succeeded. All memory is borrowed for the duration + /// of the reply call. + success: Success, + + pub const Success = struct { + /// Representations of the clipboard contents, one per + /// requested MIME type the clipboard has. Protocols that + /// carry a single text value use the first entry with a + /// text MIME type (see isTextMime). + contents: []const Content = &.{}, + + /// All MIME types available on the clipboard. Only used when + /// the request set `list`. + available: []const []const u8 = &.{}, + + /// Record a session grant so future requests from the same + /// program skip the permission prompt. Only honored when the + /// request set `can_remember`. + remember: bool = false, + }; + }; }; + +test isTextMime { + const testing = std.testing; + try testing.expect(isTextMime("text/plain")); + try testing.expect(isTextMime("UTF8_STRING")); + try testing.expect(!isTextMime("image/png")); + try testing.expect(!isTextMime(".")); +} diff --git a/vendor/libghostty-vt/src/terminal/color.zig b/vendor/libghostty-vt/src/terminal/color.zig index 92de3578..99d74989 100644 --- a/vendor/libghostty-vt/src/terminal/color.zig +++ b/vendor/libghostty-vt/src/terminal/color.zig @@ -1,7 +1,10 @@ const colorpkg = @This(); const std = @import("std"); +const builtin = @import("builtin"); +const Allocator = std.mem.Allocator; const assert = @import("../quirks.zig").inlineAssert; +const fraction = @import("fraction.zig"); const x11_color = @import("x11_color.zig"); /// The default palette. @@ -128,10 +131,59 @@ pub const PaletteC = [256]RGB.C; /// Convert a Palette to a PaletteC. pub fn paletteCval(palette: *const Palette) PaletteC { var result: PaletteC = undefined; - for (&result, palette) |*dst, src| dst.* = src.cval(); + paletteCvalSlice(palette[0..], result[0..]); return result; } +/// Convert a slice of palette entries to their C representation. +/// Asserts that both slices are the same length. +pub fn paletteCvalSlice(src: []const RGB, dst: []RGB.C) void { + assert(src.len == dst.len); + + var i: usize = 0; + + // For CPUs that are LE, we can do some clever byte shuffling + // with vectorization to do a 4-to-3 conversion. + if (comptime builtin.cpu.arch.endian() == .little) { + // Process 4 entries at a time: one 16-byte load, one byte + // shuffle dropping the padding byte of each entry, and one + // 16-byte store. The store intentionally overlaps the next + // group by 4 (garbage) bytes so that it stays a single + // vector store; the loop bound guarantees the overlap stays + // in bounds and every overlapped byte is rewritten by the + // next iteration or the scalar tail. + // + // Note the input load is a memory-level reinterpretation + // (pointer cast) because a value-level @bitCast of packed + // structs operates on the 24-bit value bits, not the 4-byte + // in-memory representation. + const V = @Vector(16, u8); + const dst_bytes: [*]u8 = @ptrCast(dst.ptr); + while (i + 6 <= src.len) : (i += 4) { + // All intermediaries have to also go through a Vector type + // to ensure LLVM lowers as vectorized ops. In Zig 0.16 the + // LLVM auto-vectorizer is disabled so this is required. + const in: V = @as(*align(4) const V, @ptrCast(src[i..][0..4])).*; + const out: V = @shuffle(u8, in, undefined, [16]i32{ + 0, 1, 2, + 4, 5, 6, + 8, 9, 10, + 12, 13, 14, + + // Overflow we don't care about + 3, 7, 11, + 15, + }); + + // Scary looking but safe, mapping a byte pointer to the full + // vector type (hence align 1). + @as(*align(1) V, @ptrCast(dst_bytes[i * 3 ..][0..16])).* = out; + } + } + + for (src[i..], dst[i..]) |entry, *out| out.* = entry.cval(); +} + /// Convert a PaletteC to a Palette. pub fn paletteZval(palette: *const PaletteC) Palette { var result: Palette = undefined; @@ -271,22 +323,37 @@ pub const DynamicPalette = struct { /// The current palette including any user modifications. current: Palette, - /// The original/default palette values. - original: Palette, + /// The original/default palette values. This points at the shared + /// built-in default palette unless a different default was given + /// (see `changeDefault`), in which case it points at an + /// allocator-owned copy that `deinit` frees. Sharing the built-in + /// default saves a full palette copy for every terminal. + original: *const Palette, /// A bitset where each bit represents whether the corresponding /// palette index has been modified from its default value. mask: PaletteMask, - pub const default: DynamicPalette = .init(colorpkg.default); + /// A dynamic palette using the built-in default palette. This owns + /// no memory, but `deinit` is still safe to call on it. + pub const default: DynamicPalette = .{ + .current = colorpkg.default, + .original = &colorpkg.default, + .mask = .initEmpty(), + }; - /// Initialize a dynamic palette with a default palette. - pub fn init(def: Palette) DynamicPalette { - return .{ - .current = def, - .original = def, - .mask = .initEmpty(), - }; + /// Initialize a dynamic palette with a default palette. The result + /// must be released with `deinit`. No memory is allocated if `def` + /// is the built-in default palette. + pub fn init(alloc: Allocator, def: Palette) Allocator.Error!DynamicPalette { + var result: DynamicPalette = .default; + try result.changeDefault(alloc, def); + return result; + } + + pub fn deinit(self: *DynamicPalette, alloc: Allocator) void { + if (self.ownedOriginal()) |owned| alloc.destroy(owned); + self.* = undefined; } /// Set a custom color at the given palette index. @@ -303,16 +370,46 @@ pub const DynamicPalette = struct { /// Reset all colors to their original values. pub fn resetAll(self: *DynamicPalette) void { - self.* = .init(self.original); + self.current = self.original.*; + self.mask = .initEmpty(); } /// Change the default palette, but preserve the changed values. - pub fn changeDefault(self: *DynamicPalette, def: Palette) void { - self.original = def; + /// + /// The built-in default palette is shared rather than copied, so + /// changing back to it releases any owned copy (see `resetDefault`). + /// Any other default is copied into memory owned by this palette, + /// allocated on the first change and reused after that. On allocation + /// failure nothing changes. + pub fn changeDefault( + self: *DynamicPalette, + alloc: Allocator, + def: Palette, + ) Allocator.Error!void { + if (std.meta.eql(def, colorpkg.default)) return self.resetDefault(alloc); + + const owned = self.ownedOriginal() orelse try alloc.create(Palette); + owned.* = def; + self.original = owned; + self.applyDefault(def); + } + + /// Change the default palette back to the built-in default palette, + /// preserving the changed values. This releases any owned copy and + /// never allocates, so it is safe to use as the fallback when + /// `changeDefault` fails. + pub fn resetDefault(self: *DynamicPalette, alloc: Allocator) void { + if (self.ownedOriginal()) |owned| alloc.destroy(owned); + self.original = &colorpkg.default; + self.applyDefault(colorpkg.default); + } + /// Rebuild the current palette from a new default, preserving the + /// changed values. + fn applyDefault(self: *DynamicPalette, def: Palette) void { // Fast path, the palette is usually not changed. if (self.mask.count() == 0) { - self.current = self.original; + self.current = def; return; } @@ -323,6 +420,16 @@ pub const DynamicPalette = struct { while (it.next()) |idx| current[idx] = self.current[idx]; self.current = current; } + + /// The allocator-owned copy of the original palette, or null if the + /// original is the shared built-in default. + fn ownedOriginal(self: *const DynamicPalette) ?*Palette { + if (self.original == &colorpkg.default) return null; + + // Only the shared built-in default is ever const; anything else + // is a copy that we allocated ourselves. + return @constCast(self.original); + } }; /// RGB value that can be changed and reset. This can also be totally unset @@ -350,7 +457,7 @@ pub const DynamicRGB = struct { } pub fn reset(self: *DynamicRGB) void { - self.override = self.default; + self.override = null; } }; @@ -463,10 +570,7 @@ pub const Dynamic = enum(u5) { /// "Each successive parameter changes the next color in the list. The /// value of Ps tells the starting point in the list." pub fn next(self: Dynamic) ?Dynamic { - return std.meta.intToEnum( - Dynamic, - @intFromEnum(self) + 1, - ) catch null; + return std.enums.fromInt(Dynamic, @intFromEnum(self) + 1); } test "next" { @@ -594,14 +698,10 @@ pub const RGB = packed struct(u24) { /// /// The value should be between 0.0 and 1.0, inclusive. fn fromIntensity(value: []const u8) error{InvalidFormat}!u8 { - const i = std.fmt.parseFloat(f64, value) catch { + const i = fraction.parse(value) orelse { @branchHint(.cold); return error.InvalidFormat; }; - if (i < 0.0 or i > 1.0) { - @branchHint(.cold); - return error.InvalidFormat; - } return @intFromFloat(i * std.math.maxInt(u8)); } @@ -947,16 +1047,18 @@ test "RGB: encode" { test "DynamicPalette: init" { const testing = std.testing; - var p: DynamicPalette = .init(default); + var p: DynamicPalette = .default; + defer p.deinit(testing.allocator); try testing.expectEqual(default, p.current); - try testing.expectEqual(default, p.original); + try testing.expectEqual(default, p.original.*); + try testing.expectEqual(&default, p.original); try testing.expectEqual(@as(usize, 0), p.mask.count()); } test "DynamicPalette: set" { const testing = std.testing; - var p: DynamicPalette = .init(default); + var p: DynamicPalette = .default; const new_color = RGB{ .r = 255, .g = 0, .b = 0 }; p.set(0, new_color); @@ -970,7 +1072,7 @@ test "DynamicPalette: set" { test "DynamicPalette: reset" { const testing = std.testing; - var p: DynamicPalette = .init(default); + var p: DynamicPalette = .default; const new_color = RGB{ .r = 255, .g = 0, .b = 0 }; p.set(0, new_color); @@ -985,7 +1087,7 @@ test "DynamicPalette: reset" { test "DynamicPalette: resetAll" { const testing = std.testing; - var p: DynamicPalette = .init(default); + var p: DynamicPalette = .default; const new_color = RGB{ .r = 255, .g = 0, .b = 0 }; p.set(0, new_color); @@ -995,27 +1097,79 @@ test "DynamicPalette: resetAll" { p.resetAll(); try testing.expectEqual(default, p.current); - try testing.expectEqual(default, p.original); + try testing.expectEqual(default, p.original.*); try testing.expectEqual(@as(usize, 0), p.mask.count()); } test "DynamicPalette: changeDefault with no changes" { const testing = std.testing; - var p: DynamicPalette = .init(default); + var p: DynamicPalette = .default; + defer p.deinit(testing.allocator); var new_palette = default; new_palette[0] = RGB{ .r = 100, .g = 100, .b = 100 }; - p.changeDefault(new_palette); - try testing.expectEqual(new_palette, p.original); + try p.changeDefault(testing.allocator, new_palette); + try testing.expectEqual(new_palette, p.original.*); try testing.expectEqual(new_palette, p.current); try testing.expectEqual(@as(usize, 0), p.mask.count()); } +test "DynamicPalette: changeDefault shares the built-in default" { + const testing = std.testing; + const alloc = testing.allocator; + + var new_palette = default; + new_palette[0] = RGB{ .r = 100, .g = 100, .b = 100 }; + + // A custom default is an owned copy. + var p: DynamicPalette = try .init(alloc, new_palette); + defer p.deinit(alloc); + try testing.expect(p.original != &default); + try testing.expectEqual(new_palette, p.original.*); + + // Changing the custom default reuses the copy. + const owned = p.original; + new_palette[1] = RGB{ .r = 101, .g = 101, .b = 101 }; + try p.changeDefault(alloc, new_palette); + try testing.expectEqual(owned, p.original); + try testing.expectEqual(new_palette, p.original.*); + + // Changing back to the built-in default releases the copy (the + // testing allocator would report the leak) and shares it again. + try p.changeDefault(alloc, default); + try testing.expectEqual(&default, p.original); + try testing.expectEqual(default, p.current); + + // resetDefault does the same directly, preserving changed values, + // and never allocates. + try p.changeDefault(alloc, new_palette); + p.set(2, RGB{ .r = 1, .g = 2, .b = 3 }); + var reset_failing: std.testing.FailingAllocator = .init(alloc, .{ + .fail_index = 0, + }); + p.resetDefault(reset_failing.allocator()); + try testing.expect(!reset_failing.has_induced_failure); + try testing.expectEqual(&default, p.original); + try testing.expectEqual(default[0], p.current[0]); + try testing.expectEqual(RGB{ .r = 1, .g = 2, .b = 3 }, p.current[2]); + try testing.expect(p.mask.isSet(2)); + + // The built-in default never allocates. + var failing: std.testing.FailingAllocator = .init(alloc, .{ + .fail_index = 0, + }); + var q: DynamicPalette = try .init(failing.allocator(), default); + defer q.deinit(failing.allocator()); + try testing.expectEqual(&default, q.original); + try testing.expect(!failing.has_induced_failure); +} + test "DynamicPalette: changeDefault preserves changes" { const testing = std.testing; - var p: DynamicPalette = .init(default); + var p: DynamicPalette = .default; + defer p.deinit(testing.allocator); const custom_color = RGB{ .r = 255, .g = 0, .b = 0 }; p.set(5, custom_color); @@ -1025,9 +1179,9 @@ test "DynamicPalette: changeDefault preserves changes" { new_palette[0] = RGB{ .r = 100, .g = 100, .b = 100 }; new_palette[5] = RGB{ .r = 50, .g = 50, .b = 50 }; - p.changeDefault(new_palette); + try p.changeDefault(testing.allocator, new_palette); - try testing.expectEqual(new_palette, p.original); + try testing.expectEqual(new_palette, p.original.*); try testing.expectEqual(new_palette[0], p.current[0]); try testing.expectEqual(custom_color, p.current[5]); try testing.expect(p.mask.isSet(5)); @@ -1037,7 +1191,8 @@ test "DynamicPalette: changeDefault preserves changes" { test "DynamicPalette: changeDefault with multiple changes" { const testing = std.testing; - var p: DynamicPalette = .init(default); + var p: DynamicPalette = .default; + defer p.deinit(testing.allocator); const red = RGB{ .r = 255, .g = 0, .b = 0 }; const green = RGB{ .r = 0, .g = 255, .b = 0 }; const blue = RGB{ .r = 0, .g = 0, .b = 255 }; @@ -1050,7 +1205,7 @@ test "DynamicPalette: changeDefault with multiple changes" { new_palette[0] = RGB{ .r = 50, .g = 50, .b = 50 }; new_palette[1] = RGB{ .r = 60, .g = 60, .b = 60 }; - p.changeDefault(new_palette); + try p.changeDefault(testing.allocator, new_palette); try testing.expectEqual(new_palette[0], p.current[0]); try testing.expectEqual(red, p.current[1]); @@ -1279,3 +1434,35 @@ test "LAB.toRgb" { try testing.expectEqual(expected.b, actual.b); } } + +test paletteCvalSlice { + const testing = std.testing; + + // Every length from empty through a full palette, so that the + // vectorized groups, the overlapping store bound, and the scalar + // tail are all exercised at their edges. + var src: Palette = undefined; + for (&src, 0..) |*rgb, i| rgb.* = .{ + .r = @intCast(i % 256), + .g = @intCast((i * 7 + 1) % 256), + .b = @intCast((i * 13 + 2) % 256), + }; + + var dst: PaletteC = undefined; + for (0..src.len + 1) |len| { + // Poison the destination so unwritten bytes are detected. + @memset(std.mem.asBytes(&dst), 0xAA); + + paletteCvalSlice(src[0..len], dst[0..len]); + for (src[0..len], dst[0..len]) |rgb, c| { + try testing.expectEqual(rgb.r, c.r); + try testing.expectEqual(rgb.g, c.g); + try testing.expectEqual(rgb.b, c.b); + } + + // Nothing beyond the requested length may be written. + for (std.mem.asBytes(&dst)[len * 3 ..]) |b| { + try testing.expectEqual(@as(u8, 0xAA), b); + } + } +} diff --git a/vendor/libghostty-vt/src/terminal/compress/Page.zig b/vendor/libghostty-vt/src/terminal/compress/Page.zig index b7ad8025..8eb9caab 100644 --- a/vendor/libghostty-vt/src/terminal/compress/Page.zig +++ b/vendor/libghostty-vt/src/terminal/compress/Page.zig @@ -106,6 +106,15 @@ pub fn init( if (scratch.len < required) return error.OutputTooSmall; if (required == 0) return null; + // Page memory is an opaque snapshot containing structs whose padding has + // no semantic value. Assigning those structs can leave their padding + // undefined in Memcheck even though every byte is addressable and must be + // preserved by compression. Mark the complete byte representation as + // defined at this boundary so that undefined padding does not propagate + // through the codec and obscure real Valgrind failures. + if (std.valgrind.runningOnValgrind() > 0) + std.valgrind.memcheck.makeMemDefined(source.memory); + const encoded_len = lz4.compress( source.memory, scratch[0..required], @@ -209,6 +218,11 @@ test "compressed Page retained mapping round trip" { const expected = try testing.allocator.dupe(u8, resident.memory); defer testing.allocator.free(expected); + if (std.valgrind.runningOnValgrind() > 0) { + // This snapshot deliberately includes semantically irrelevant struct + // padding so the round trip can compare the complete representation. + std.valgrind.memcheck.makeMemDefined(expected); + } const memory_ptr = resident.memory.ptr; const memory_len = resident.memory.len; diff --git a/vendor/libghostty-vt/src/terminal/compress/lz4_differential.zig b/vendor/libghostty-vt/src/terminal/compress/lz4_differential.zig index bbf3b276..f164ec04 100644 --- a/vendor/libghostty-vt/src/terminal/compress/lz4_differential.zig +++ b/vendor/libghostty-vt/src/terminal/compress/lz4_differential.zig @@ -485,7 +485,7 @@ test "lz4 differential light" { test "lz4 differential exhaustive" { // Slow. Enable explicitly, ideally together with a test filter: // GHOSTTY_LZ4_SLOW=1 zig build test -Dtest-filter="lz4 differential" - if (!std.process.hasEnvVarConstant("GHOSTTY_LZ4_SLOW")) + if (!std.testing.environ.containsConstant("GHOSTTY_LZ4_SLOW")) return error.SkipZigTest; // Several independent seeds; the suite is deterministic per seed. diff --git a/vendor/libghostty-vt/src/terminal/cursor.zig b/vendor/libghostty-vt/src/terminal/cursor.zig index 136ee085..f5f66075 100644 --- a/vendor/libghostty-vt/src/terminal/cursor.zig +++ b/vendor/libghostty-vt/src/terminal/cursor.zig @@ -1,15 +1,25 @@ +const lib = @import("../lib/main.zig"); + /// The visual style of the cursor. Whether or not it blinks /// is determined by mode 12 (modes.zig). This mode is synchronized /// with CSI q, the same as xterm. -pub const Style = enum { - bar, // DECSCUSR 5, 6 - block, // DECSCUSR 1, 2 - underline, // DECSCUSR 3, 4 - - /// The cursor styles below aren't known by DESCUSR and are custom - /// implemented in Ghostty. They are reported as some standard style - /// if requested, though. - /// Hollow block cursor. This is a block cursor with the center empty. - /// Reported as DECSCUSR 1 or 2 (block). - block_hollow, -}; +/// +/// Bar, block, and underline correspond to DECSCUSR 5/6, 1/2, and 3/4. +/// Hollow block is Ghostty-specific and is reported as DECSCUSR 1 or 2. +pub const Style = lib.Enum(.zig, &.{ + // DECSCUSR 5, 6 + "bar", + + // DECSCUSR 1, 2 + "block", + + // DECSCUSR 3, 4 + "underline", + + // The cursor styles below aren't known by DESCUSR and are custom + // implemented in Ghostty. They are reported as some standard style + // if requested, though. + // Hollow block cursor. This is a block cursor with the center empty. + // Reported as DECSCUSR 1 or 2 (block). + "block_hollow", +}); diff --git a/vendor/libghostty-vt/src/terminal/dcs.zig b/vendor/libghostty-vt/src/terminal/dcs.zig index 425325d4..a76ab31b 100644 --- a/vendor/libghostty-vt/src/terminal/dcs.zig +++ b/vendor/libghostty-vt/src/terminal/dcs.zig @@ -254,6 +254,63 @@ pub const Command = union(enum) { decscusr, decstbm, decslrm, + + /// Fixed upper bound for an encoded DECRPSS response. The comptime + /// calculated max at the time of writing this was around 63 so this + /// leaves a ton of space for future stuff. We don't do the comptime + /// calculation cause it complicated the implementation a bit too + /// much. + pub const max_response_bytes = 256; + + /// Encode the response for this request. + pub fn encode( + self: DECRQSS, + t: *terminal.Terminal, + response: []u8, + ) ![]const u8 { + var writer: std.Io.Writer = .fixed(response); + + const prefix_fmt = "\x1bP{d}$r"; + const prefix_len = std.fmt.comptimePrint(prefix_fmt, .{0}).len; + writer.end = prefix_len; + + switch (self) { + .none => {}, + .sgr => { + const buf = try t.printAttributes(writer.buffer[writer.end..]); + writer.end += buf.len; + try writer.writeByte('m'); + }, + .decscusr => { + const blink = t.modes.get(.cursor_blinking); + const style: u8 = switch (t.screens.active.cursor.cursor_style) { + .block, .block_hollow => if (blink) 1 else 2, + .underline => if (blink) 3 else 4, + .bar => if (blink) 5 else 6, + }; + try writer.print("{d} q", .{style}); + }, + .decstbm => try writer.print("{d};{d}r", .{ + t.scrolling_region.top + 1, + t.scrolling_region.bottom + 1, + }), + .decslrm => if (t.modes.get(.enable_left_and_right_margin)) { + try writer.print("{d};{d}s", .{ + t.scrolling_region.left + 1, + t.scrolling_region.right + 1, + }); + }, + } + + const valid = writer.end > prefix_len; + try writer.writeAll("\x1b\\"); + _ = try std.fmt.bufPrint( + response[0..prefix_len], + prefix_fmt, + .{@intFromBool(valid)}, + ); + return writer.buffered(); + } }; }; @@ -405,6 +462,90 @@ test "DECRQSS invalid command" { try testing.expect(h.unhook() == null); } +test "DECRQSS response encoding" { + const testing = std.testing; + + var t: terminal.Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer t.deinit(testing.allocator); + + const S = struct { + fn expectResponse( + term: *terminal.Terminal, + request: Command.DECRQSS, + expected: []const u8, + ) !void { + var buf: [Command.DECRQSS.max_response_bytes]u8 = undefined; + const encoded = try request.encode(term, &buf); + try testing.expectEqualStrings(expected, encoded); + } + }; + + try S.expectResponse(&t, .none, "\x1BP0$r\x1B\\"); + try S.expectResponse(&t, .sgr, "\x1BP1$r0m\x1B\\"); + + try t.setAttribute(.bold); + try t.setAttribute(.{ .underline = .curly }); + try S.expectResponse(&t, .sgr, "\x1BP1$r0;1;4:3m\x1B\\"); + + t.setCursorStyle(.steady_underline); + try S.expectResponse(&t, .decscusr, "\x1BP1$r4 q\x1B\\"); + + t.scrolling_region.top = 4; + t.scrolling_region.bottom = 19; + try S.expectResponse(&t, .decstbm, "\x1BP1$r5;20r\x1B\\"); + + try S.expectResponse(&t, .decslrm, "\x1BP0$r\x1B\\"); + t.modes.set(.enable_left_and_right_margin, true); + t.scrolling_region.left = 2; + t.scrolling_region.right = 69; + try S.expectResponse(&t, .decslrm, "\x1BP1$r3;70s\x1B\\"); +} + +test "DECRQSS largest response fits fixed buffer" { + const testing = std.testing; + + var t: terminal.Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer t.deinit(testing.allocator); + + try t.setAttribute(.bold); + try t.setAttribute(.faint); + try t.setAttribute(.italic); + try t.setAttribute(.{ .underline = .dashed }); + try t.setAttribute(.blink); + try t.setAttribute(.inverse); + try t.setAttribute(.invisible); + try t.setAttribute(.strikethrough); + try t.setAttribute(.overline); + try t.setAttribute(.{ .direct_color_fg = .{ + .r = 255, + .g = 255, + .b = 255, + } }); + try t.setAttribute(.{ .direct_color_bg = .{ + .r = 255, + .g = 255, + .b = 255, + } }); + + var buf: [Command.DECRQSS.max_response_bytes]u8 = undefined; + const encoded = try Command.DECRQSS.sgr.encode(&t, &buf); + + const expected = + "\x1BP1$r0;1;2;3;4:5;53;5;7;8;9" ++ + ";38:2::255:255:255" ++ + ";48:2::255:255:255m\x1B\\"; + try testing.expectEqualStrings(expected, encoded); + try testing.expect(encoded.len <= Command.DECRQSS.max_response_bytes); +} + test "tmux enter and implicit exit" { if (comptime !build_options.tmux_control_mode) return error.SkipZigTest; diff --git a/vendor/libghostty-vt/src/terminal/device_status.zig b/vendor/libghostty-vt/src/terminal/device_status.zig index fd1a9b7e..9d158f39 100644 --- a/vendor/libghostty-vt/src/terminal/device_status.zig +++ b/vendor/libghostty-vt/src/terminal/device_status.zig @@ -7,6 +7,13 @@ pub const ColorScheme = lib.Enum(lib.target, &.{ "dark", }); +/// The visibility state reported in response to a CSI ? 998 n query or when +/// DEC mode 2033 is enabled. +pub const Visibility = lib.Enum(lib.target, &.{ + "potentially_visible", + "not_visible", +}); + /// Maximum number of bytes that `encodeColorSchemeReport` will write. pub const max_color_scheme_report_encode_size = max: { var result: usize = 0; @@ -33,26 +40,35 @@ pub fn encodeColorSchemeReport( }); } +/// Maximum number of bytes that `encodeVisibilityReport` will write. +pub const max_visibility_report_encode_size = "\x1B[?999;2n".len; + +/// Encode a visibility report response for CSI ? 998 n queries and DEC mode +/// 2033 notifications. +pub fn encodeVisibilityReport( + writer: *std.Io.Writer, + visibility: Visibility, +) std.Io.Writer.Error!void { + try writer.writeAll(switch (visibility) { + .potentially_visible => "\x1B[?999;1n", + .not_visible => "\x1B[?999;2n", + }); +} + /// An enum(u16) of the available device status requests. pub const Request = dsr_enum: { - const EnumField = std.builtin.Type.EnumField; - var fields: [entries.len]EnumField = undefined; - for (entries, 0..) |entry, i| { - fields[i] = .{ - .name = entry.name, - .value = @as(Tag.Backing, @bitCast(Tag{ - .value = entry.value, - .question = entry.question, - })), - }; + var names: [entries.len][]const u8 = undefined; + var values: [entries.len]Tag.Backing = undefined; + + for (entries, &names, &values) |entry, *name, *value| { + name.* = entry.name; + value.* = @bitCast(Tag{ + .value = entry.value, + .question = entry.question, + }); } - break :dsr_enum @Type(.{ .@"enum" = .{ - .tag_type = Tag.Backing, - .fields = &fields, - .decls = &.{}, - .is_exhaustive = true, - } }); + break :dsr_enum @Enum(Tag.Backing, .exhaustive, &names, &values); }; /// The tag type for our enum is a u16 but we use a packed struct @@ -97,6 +113,7 @@ const entries: []const Entry = &.{ .{ .name = "operating_status", .value = 5 }, .{ .name = "cursor_position", .value = 6 }, .{ .name = "color_scheme", .value = 996, .question = true }, + .{ .name = "visibility", .value = 998, .question = true }, }; test "encode color scheme report dark" { @@ -114,3 +131,14 @@ test "encode color scheme report light" { try encodeColorSchemeReport(&writer, .light); try std.testing.expectEqualStrings("\x1B[?997;2n", writer.buffered()); } + +test "encode visibility report" { + var buf: [max_visibility_report_encode_size]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try encodeVisibilityReport(&writer, .potentially_visible); + try std.testing.expectEqualStrings("\x1B[?999;1n", writer.buffered()); + + writer = .fixed(&buf); + try encodeVisibilityReport(&writer, .not_visible); + try std.testing.expectEqualStrings("\x1B[?999;2n", writer.buffered()); +} diff --git a/vendor/libghostty-vt/src/terminal/formatter.zig b/vendor/libghostty-vt/src/terminal/formatter.zig index 553ea28d..4feedda5 100644 --- a/vendor/libghostty-vt/src/terminal/formatter.zig +++ b/vendor/libghostty-vt/src/terminal/formatter.zig @@ -1,5 +1,6 @@ const std = @import("std"); const assert = @import("../quirks.zig").inlineAssert; +const fastprint = @import("../fastprint.zig"); const lib = @import("lib.zig"); const Allocator = std.mem.Allocator; const color = @import("color.zig"); @@ -119,9 +120,117 @@ pub const Options = struct { /// /// Used by formatters that operate on PageLists to track the source position /// of each byte written. The caller is responsible for freeing the map. +/// +/// The mapping is stored in two parts: a per-byte x/y coordinate (8 +/// bytes per output byte, half the size of a Pin) and a tiny table of +/// page nodes covering byte ranges (there are only ever a handful of +/// pages). This also lets page formatters write coordinates directly +/// into the map without a separate coordinate-to-pin conversion pass. pub const PinMap = struct { alloc: Allocator, - map: *std.ArrayList(Pin), + map: *Map, + + /// The type of the page node referenced by pins. + pub const Node = @FieldType(Pin, "node"); + + /// A page node covering output bytes starting at `offset` + /// (inclusive) until the next entry's offset (or the end of the + /// output). + pub const NodeRun = struct { + offset: usize, + node: Node, + }; + + pub const Map = struct { + /// The x/y coordinate within its page for every output byte. + points: std.ArrayList(Coordinate) = .empty, + + /// The page node for ranges of output bytes, ordered by offset. + nodes: std.ArrayList(NodeRun) = .empty, + + pub const empty: Map = .{}; + + pub fn deinit(self: *Map, alloc: Allocator) void { + self.points.deinit(alloc); + self.nodes.deinit(alloc); + } + + pub fn clearRetainingCapacity(self: *Map) void { + self.points.clearRetainingCapacity(); + self.nodes.clearRetainingCapacity(); + } + + /// The total number of bytes mapped. + pub fn count(self: *const Map) usize { + return self.points.items.len; + } + + /// Set the page node for all bytes appended from here on, + /// until the next call. No-op if the node is unchanged. + pub fn setNode( + self: *Map, + alloc: Allocator, + node: Node, + ) Allocator.Error!void { + if (self.nodes.getLastOrNull()) |last| { + if (last.node == node) return; + } + + try self.nodes.append(alloc, .{ + .offset = self.points.items.len, + .node = node, + }); + } + + /// Append `n` bytes that map to `pin`. + pub fn append( + self: *Map, + alloc: Allocator, + pin: Pin, + n: usize, + ) Allocator.Error!void { + if (n == 0) return; + try self.setNode(alloc, pin.node); + try self.points.appendNTimes( + alloc, + .{ .x = pin.x, .y = pin.y }, + n, + ); + } + + /// Returns the pin that the byte at the given offset maps to, + /// or null if the offset is out of range. + pub fn get(self: *const Map, offset: usize) ?Pin { + if (offset >= self.points.items.len) return null; + const coord = self.points.items[offset]; + return .{ + .node = findNode(self.nodes.items, offset) orelse return null, + .x = coord.x, + .y = @intCast(coord.y), + }; + } + + /// Returns the last pin in the map, if any. + pub fn getLastOrNull(self: *const Map) ?Pin { + const len = self.points.items.len; + if (len == 0) return null; + return self.get(len - 1); + } + }; + + /// Binary search for the node covering `offset` in a slice of node + /// runs sorted by offset. Returns null only if the slice is empty + /// or the offset precedes the first run. + pub fn findNode(runs: []const NodeRun, offset: usize) ?Node { + if (runs.len == 0 or offset < runs[0].offset) return null; + var lo: usize = 0; + var hi: usize = runs.len; + while (lo + 1 < hi) { + const mid = lo + (hi - lo) / 2; + if (runs[mid].offset <= offset) lo = mid else hi = mid; + } + return runs[lo].node; + } }; /// Terminal formatter formats the active terminal screen. @@ -288,7 +397,7 @@ pub const TerminalFormatter = struct { // Map all those bytes to the same pin. Use the top left to ensure // the node pointer is always properly initialized. - m.map.appendNTimes( + m.map.append( m.alloc, self.terminal.screens.active.pages.getTopLeft(.screen), std.math.cast(usize, discarding.count) orelse return error.WriteFailed, @@ -326,7 +435,47 @@ pub const TerminalFormatter = struct { // Map all those bytes to the same pin. Use the top left to ensure // the node pointer is always properly initialized. - m.map.appendNTimes( + m.map.append( + m.alloc, + self.terminal.screens.active.pages.getTopLeft(.screen), + std.math.cast(usize, discarding.count) orelse return error.WriteFailed, + ) catch return error.WriteFailed; + } + } + + // Emit tabstop positions before the screen contents because setting + // them moves the cursor. Screen formatting will restore the requested + // cursor position afterwards. + if (self.opts.emit == .vt and self.extra.tabstops) { + // Clear all tabs (CSI 3 g) + try writer.print("\x1b[3g", .{}); + + // Set each configured tabstop by moving cursor and using HTS + for (0..self.terminal.cols) |col| { + if (self.terminal.tabstops.get(col)) { + // Move cursor to the column (1-indexed) + try writer.print("\x1b[{d}G", .{col + 1}); + // Set tab (HTS) + try writer.print("\x1bH", .{}); + } + } + + // Screen contents are formatted relative to the top-left. + try writer.writeAll("\x1b[H"); + + // If we have a pin_map, add the bytes we wrote to map. + if (self.pin_map) |*m| { + var discarding: std.Io.Writer.Discarding = .init(&.{}); + var extra_formatter: TerminalFormatter = self; + extra_formatter.content = .none; + extra_formatter.pin_map = null; + extra_formatter.extra = .none; + extra_formatter.extra.tabstops = true; + try extra_formatter.format(&discarding.writer); + + // Map all those bytes to the same pin. Use the top left to ensure + // the node pointer is always properly initialized. + m.map.append( m.alloc, self.terminal.screens.active.pages.getTopLeft(.screen), std.math.cast(usize, discarding.count) orelse return error.WriteFailed, @@ -336,7 +485,6 @@ pub const TerminalFormatter = struct { var screen_formatter: ScreenFormatter = .init(self.terminal.screens.active, self.opts); screen_formatter.content = self.content; - screen_formatter.extra = self.extra.screen; screen_formatter.pin_map = self.pin_map; try screen_formatter.format(writer); @@ -360,22 +508,6 @@ pub const TerminalFormatter = struct { } } - // Emit tabstop positions - if (self.extra.tabstops) { - // Clear all tabs (CSI 3 g) - try writer.print("\x1b[3g", .{}); - - // Set each configured tabstop by moving cursor and using HTS - for (0..self.terminal.cols) |col| { - if (self.terminal.tabstops.get(col)) { - // Move cursor to the column (1-indexed) - try writer.print("\x1b[{d}G", .{col + 1}); - // Set tab (HTS) - try writer.print("\x1bH", .{}); - } - } - } - // Emit keyboard modes such as ModifyOtherKeys if (self.extra.keyboard) { // Only emit if modify_other_keys_2 is true @@ -398,25 +530,25 @@ pub const TerminalFormatter = struct { extra_formatter.pin_map = null; extra_formatter.extra = .none; extra_formatter.extra.scrolling_region = self.extra.scrolling_region; - extra_formatter.extra.tabstops = self.extra.tabstops; extra_formatter.extra.keyboard = self.extra.keyboard; extra_formatter.extra.pwd = self.extra.pwd; try extra_formatter.format(&discarding.writer); - m.map.appendNTimes( + m.map.append( m.alloc, - if (m.map.items.len > 0) pin: { - const last = m.map.items[m.map.items.len - 1]; - break :pin .{ - .node = last.node, - .x = last.x, - .y = last.y, - }; - } else self.terminal.screens.active.pages.getTopLeft(.screen), + m.map.getLastOrNull() orelse + self.terminal.screens.active.pages.getTopLeft(.screen), std.math.cast(usize, discarding.count) orelse return error.WriteFailed, ) catch return error.WriteFailed; } } + + // Emit extra screen state last because terminal state such + // as scrolling regions can move the cursor, so we have to set + // cursor last. + screen_formatter.content = .none; + screen_formatter.extra = self.extra.screen; + try screen_formatter.format(writer); } }; @@ -563,6 +695,43 @@ pub const ScreenFormatter = struct { .html => return, } + // Emit cursor position before the other extras because restoring a + // pending wrap requires reprinting the cell at the right edge. That + // print uses and changes active screen state, so the requested style, + // hyperlink, protection, and charset must be restored afterwards. + if (self.extra.cursor) cursor: { + const cursor = &self.screen.cursor; + + // If we don't have pending wrap, then we can just use CUP. + if (!cursor.pending_wrap or cursor.x != self.screen.pages.cols - 1) { + try writer.print("\x1b[{d};{d}H", .{ cursor.y + 1, cursor.x + 1 }); + break :cursor; + } + + // Pending wrap, we can't use CUP because it resets pending wrap. + const start_x = switch (cursor.page_cell.wide) { + .spacer_tail => cursor.x - 1, + .narrow, .wide, .spacer_head => cursor.x, + }; + + // Move cursor to the edge. + try writer.print( + "\x1b[{d};{d}H", + .{ cursor.y + 1, start_x + 1 }, + ); + + // Reformat the cell which sets the proper pending wrap state. + var cell_formatter: PageFormatter = .init( + cursor.page_pin.node.page(), + self.opts, + ); + cell_formatter.start_x = cursor.x; + cell_formatter.end_x = cursor.x; + cell_formatter.start_y = cursor.page_pin.y; + cell_formatter.end_y = cursor.page_pin.y; + try cell_formatter.format(writer); + } + // Emit current SGR style state if (self.extra.style) { const cursor = &self.screen.cursor; @@ -652,13 +821,6 @@ pub const ScreenFormatter = struct { } } - // Emit cursor position using CUP (CSI H) - if (self.extra.cursor) { - const cursor = &self.screen.cursor; - // CUP is 1-indexed - try writer.print("\x1b[{d};{d}H", .{ cursor.y + 1, cursor.x + 1 }); - } - // If we have a pin_map, we need to count how many bytes the extras // will emit so we can map them all to the same pin. We do this by // formatting to a discarding writer with content=none. @@ -671,21 +833,10 @@ pub const ScreenFormatter = struct { // Map all those bytes to the same pin. Use the first page node // to ensure the node pointer is always properly initialized. - m.map.appendNTimes( + m.map.append( m.alloc, - if (m.map.items.len > 0) pin: { - // There is a weird Zig miscompilation here on 0.15.2. - // If I return the m.map.items value directly then we - // get undefined memory (even though we're copying a - // Pin struct). If we duplicate here like this we do - // not. - const last = m.map.items[m.map.items.len - 1]; - break :pin .{ - .node = last.node, - .x = last.x, - .y = last.y, - }; - } else self.screen.pages.getTopLeft(.screen), + m.map.getLastOrNull() orelse + self.screen.pages.getTopLeft(.screen), std.math.cast(usize, discarding.count) orelse return error.WriteFailed, ) catch return error.WriteFailed; } @@ -737,10 +888,6 @@ pub const PageListFormatter = struct { const tl: PageList.Pin = self.top_left orelse self.list.getTopLeft(.screen); const br: PageList.Pin = self.bottom_right orelse self.list.getBottomRight(.screen).?; - // If we keep track of pins, we'll need this. - var point_map: std.ArrayList(Coordinate) = .empty; - defer if (self.pin_map) |*m| point_map.deinit(m.alloc); - var page_state: ?PageFormatter.TrailingState = null; var iter = tl.pageIterator(.right_down, br); while (iter.next()) |chunk| { @@ -763,27 +910,19 @@ pub const PageListFormatter = struct { if (chunk.node == br.node) formatter.end_x = br.x; } - // If we're tracking pins, then we setup a point map for the - // page formatter (cause it can't track pins). And then we convert - // this to pins later. + // If we're tracking pins, the page formatter writes its + // per-byte coordinates directly into our map's point list + // and we record which page node covers those bytes. if (self.pin_map) |*m| { - point_map.clearRetainingCapacity(); - formatter.point_map = .{ .alloc = m.alloc, .map = &point_map }; + m.map.setNode(m.alloc, chunk.node) catch return error.WriteFailed; + formatter.point_map = .{ + .alloc = m.alloc, + .map = &m.map.points, + .base = m.map.points.items.len, + }; } page_state = try formatter.formatWithState(writer); - - // If we're tracking pins then grab our points and write them - // to our pin map. - if (self.pin_map) |*m| { - for (point_map.items) |coord| { - m.map.append(m.alloc, .{ - .node = chunk.node, - .x = coord.x, - .y = @intCast(coord.y), - }) catch return error.WriteFailed; - } - } } } }; @@ -831,16 +970,25 @@ pub const PageFormatter = struct { /// The x/y coordinate will be the coordinates within the page. /// /// Warning: there is a significant performance hit to track this - point_map: ?struct { - alloc: Allocator, - map: *std.ArrayList(Coordinate), - }, + point_map: ?PointMap, /// The previous trailing state from the prior page. If you're iterating /// over multiple pages this helps ensure that unwrapping and other /// accounting works properly. trailing_state: ?TrailingState, + /// See point_map. + pub const PointMap = struct { + alloc: Allocator, + map: *std.ArrayList(Coordinate), + + /// The index in `map` at which this formatter's output begins. + /// Entries before this index belong to a caller (e.g. previous + /// pages of a PageListFormatter) and aren't inspected. This + /// exists so that callers can share one list across pages. + base: usize = 0, + }; + /// Trailing state. This is used to ensure that rows wrapped across /// multiple pages are unwrapped properly, as well as other accounting /// we may do in the future. @@ -877,6 +1025,18 @@ pub const PageFormatter = struct { pub fn formatWithState( self: PageFormatter, writer: *std.Io.Writer, + ) std.Io.Writer.Error!TrailingState { + // Specialize the hot path on the emitted format so that the + // per-cell loop contains no per-cell format dispatch. + switch (self.opts.emit) { + inline else => |emit| return self.formatWithStateEmit(writer, emit), + } + } + + fn formatWithStateEmit( + self: PageFormatter, + writer: *std.Io.Writer, + comptime emit: Format, ) std.Io.Writer.Error!TrailingState { var blank_rows: usize = 0; var blank_cells: usize = 0; @@ -934,15 +1094,14 @@ pub const PageFormatter = struct { } // Wrap HTML output in monospace font styling - switch (self.opts.emit) { + switch (emit) { .plain => {}, .html => { // Setup our div. We use a buffer here that should always // fit the stuff we need, in order to make counting bytes easier. var buf: [1024]u8 = undefined; - var stream = std.io.fixedBufferStream(&buf); - const buf_writer = stream.writer(); + var buf_writer: std.Io.Writer = .fixed(&buf); // Monospace and whitespace preserving buf_writer.writeAll("
") catch return error.WriteFailed; - const header = stream.getWritten(); + const header = buf_writer.buffered(); try writer.writeAll(header); if (self.point_map) |*map| map.map.appendNTimes( map.alloc, @@ -971,8 +1130,7 @@ pub const PageFormatter = struct { .vt => { // OSC 10 sets foreground color, OSC 11 sets background color var buf: [512]u8 = undefined; - var stream = std.io.fixedBufferStream(&buf); - const buf_writer = stream.writer(); + var buf_writer: std.Io.Writer = .fixed(&buf); if (self.opts.foreground) |fg| { buf_writer.print( "\x1b]10;rgb:{x:0>2}/{x:0>2}/{x:0>2}\x1b\\", @@ -986,7 +1144,7 @@ pub const PageFormatter = struct { ) catch return error.WriteFailed; } - const header = stream.getWritten(); + const header = buf_writer.buffered(); try writer.writeAll(header); if (self.point_map) |*map| map.map.appendNTimes( map.alloc, @@ -996,8 +1154,23 @@ pub const PageFormatter = struct { }, } - // Our style for non-plain formats + // Our style for non-plain formats. Alongside the style itself we + // track the page-local interned style id it corresponds to (styles + // are interned per-page so id equality implies style equality). + // The id is only a fast-path hint: it is set to `invalid_style_id` + // whenever the current style didn't come from an interned id + // (e.g. bg-color-only cells which synthesize styles). + const invalid_style_id: u32 = std.math.maxInt(u32); var style: Style = .{}; + var style_id: u32 = 0; + + // Whether the codepoint map has any entries. Hoisted out of the + // per-codepoint path so that the common no-map case can use the + // fast cell run path below. + const cp_map_empty: bool = if (self.opts.codepoint_map) |m| + m.len == 0 + else + true; // Track hyperlink state for HTML output. We need to close tags // when the hyperlink changes or ends. @@ -1052,11 +1225,12 @@ pub const PageFormatter = struct { // Reset style before emitting newlines to prevent background // colors from bleeding into the next line's leading cells. if (!style.default()) { - try self.formatStyleClose(writer); + try self.formatStyleClose(emit, writer); style = .{}; + style_id = 0; } - const sequence: []const u8 = switch (self.opts.emit) { + const sequence: []const u8 = switch (emit) { // Plaintext just uses standard newlines because newlines // on their own usually move the cursor back in anywhere // you type plaintext. @@ -1079,7 +1253,7 @@ pub const PageFormatter = struct { // in a prior page, so we just map to the first row of this // page. if (self.point_map) |*map| { - const start: Coordinate = if (map.map.items.len > 0) + const start: Coordinate = if (map.map.items.len > map.base) map.map.items[map.map.items.len - 1] else .{ .x = 0, .y = 0 }; @@ -1115,8 +1289,41 @@ pub const PageFormatter = struct { if (!row.wrap_continuation or !self.opts.unwrap) blank_cells = 0; // Go through each cell and print it - for (cells_subset, row_start_x..) |*cell, x_usize| { - const x: size.CellCountInt = @intCast(x_usize); + var cell_i: usize = 0; + while (cell_i < cells_subset.len) : (cell_i += 1) { + const cell: *const Cell = &cells_subset[cell_i]; + const x: size.CellCountInt = @intCast(row_start_x + cell_i); + + // Fast path: runs of simple cells (single codepoint, no + // style/hyperlink transitions) are encoded in batches, + // avoiding all of the per-cell bookkeeping below. This is + // only valid when we have no codepoint map and when our + // current style/hyperlink state is known-stable. + if (cp_map_empty) fast: { + if (comptime formatStyled(emit)) { + if (style_id == invalid_style_id) break :fast; + } + + const consumed = try self.writeCellRun( + emit, + self.point_map != null, + writer, + cells_subset[cell_i..], + x, + y, + style_id, + current_hyperlink_id, + &blank_cells, + ); + + // Zero cells consumed means the first cell isn't + // eligible for the fast path; handle it below. + if (consumed == 0) break :fast; + + // The continue expression adds the final one. + cell_i += consumed - 1; + continue; + } // Skip spacers. These happen naturally when wide characters // are printed again on the screen (for well-behaved terminals!) @@ -1132,8 +1339,9 @@ pub const PageFormatter = struct { // If we're emitting styled output (not plaintext) and // the cell has some kind of styling or is not empty // then this isn't blank. - if (formatStyled(self.opts.emit) and - (!cell.isEmpty() or cell.hasStyling())) break :blank; + if (comptime formatStyled(emit)) { + if (!cell.isEmpty() or cell.hasStyling()) break :blank; + } // Cells with no text are blank if (!cell.hasText()) { @@ -1155,32 +1363,12 @@ pub const PageFormatter = struct { if (blank_cells > 0) { try writer.splatByteAll(' ', blank_cells); - if (self.point_map) |*map| { - // Map each blank cell to its coordinate. Blank cells can span - // multiple rows if they carry over from wrap continuation. - var remaining_blanks = blank_cells; - var blank_x = x; - var blank_y = y; - while (remaining_blanks > 0) : (remaining_blanks -= 1) { - if (blank_x > 0) { - // We have space in this row - blank_x -= 1; - } else if (blank_y > 0) { - // Wrap to previous row - blank_y -= 1; - blank_x = self.page.size.cols - 1; - } else { - // Can't go back further, just use (0, 0) - blank_x = 0; - blank_y = 0; - } - - map.map.append( - map.alloc, - .{ .x = blank_x, .y = blank_y }, - ) catch return error.WriteFailed; - } - } + if (self.point_map) |*map| try self.appendBlankPoints( + map, + blank_cells, + x, + y, + ); blank_cells = 0; } @@ -1188,24 +1376,44 @@ pub const PageFormatter = struct { style: { // If we aren't emitting styled output then we don't // have to worry about styles. - if (!formatStyled(self.opts.emit)) break :style; + if (!comptime formatStyled(emit)) break :style; + + // Fast path: styles are interned per-page, so if this + // cell's style id matches the id of our current style + // then the style is unchanged. + const cell_style_id: u32 = switch (cell.content_tag) { + .codepoint, .codepoint_grapheme => cell.style_id, + .bg_color_palette, .bg_color_rgb => invalid_style_id, + }; + if (cell_style_id == style_id and + cell_style_id != invalid_style_id) break :style; // Get our cell style. const cell_style = self.cellStyle(cell); // If the style hasn't changed, don't bloat output. - if (cell_style.eql(style)) break :style; + // When both ids are interned (and thus different, since + // equal ids broke out above), interning guarantees the + // styles differ so we can skip the comparison entirely. + if (cell_style_id == invalid_style_id or + style_id == invalid_style_id) + { + if (cell_style.eql(style)) { + style_id = cell_style_id; + break :style; + } + } // If we had a previous style, we need to close it, // because we've confirmed we have some new style // (which is maybe default). - if (!style.default()) switch (self.opts.emit) { - .html => try self.formatStyleClose(writer), + if (!style.default()) switch (emit) { + .html => try self.formatStyleClose(emit, writer), // For VT, we only close if we're switching to a default // style because any non-default style will emit // a \x1b[0m as the start of a VT coloring sequence. - .vt => if (cell_style.default()) try self.formatStyleClose(writer), + .vt => if (cell_style.default()) try self.formatStyleClose(emit, writer), // Unreachable because of the styled() check at the // top of this block. @@ -1214,12 +1422,14 @@ pub const PageFormatter = struct { // At this point, we can copy our style over style = cell_style; + style_id = cell_style_id; // If we're just the default style now, we're done. if (cell_style.default()) break :style; // New style, emit it. try self.formatStyleOpen( + emit, writer, &style, ); @@ -1229,16 +1439,18 @@ pub const PageFormatter = struct { if (self.point_map) |*map| { var discarding: std.Io.Writer.Discarding = .init(&.{}); try self.formatStyleOpen( + emit, &discarding.writer, &style, ); - for (0..std.math.cast( - usize, - discarding.count, - ) orelse return error.WriteFailed) |_| map.map.append(map.alloc, .{ - .x = x, - .y = y, - }) catch return error.WriteFailed; + map.map.appendNTimes( + map.alloc, + .{ .x = x, .y = y }, + std.math.cast( + usize, + discarding.count, + ) orelse return error.WriteFailed, + ) catch return error.WriteFailed; } } @@ -1247,7 +1459,7 @@ pub const PageFormatter = struct { // We currently only emit hyperlinks for HTML. In the // future we can support emitting OSC 8 hyperlinks for // VT output as well. - if (self.opts.emit != .html) break :hyperlink; + if (comptime emit != .html) break :hyperlink; // Get the hyperlink ID. This ID is our internal ID, // not necessarily the OSC8 ID. @@ -1263,7 +1475,7 @@ pub const PageFormatter = struct { // If our prior hyperlink ID was non-null, we need to // close it because the ID has changed. if (current_hyperlink_id != null) { - try self.formatHyperlinkClose(writer); + try self.formatHyperlinkClose(emit, writer); current_hyperlink_id = null; } @@ -1280,6 +1492,7 @@ pub const PageFormatter = struct { break :uri link.uri.offset.ptr(self.page.memory)[0..link.uri.len]; }; try self.formatHyperlinkOpen( + emit, writer, uri, ); @@ -1289,16 +1502,18 @@ pub const PageFormatter = struct { if (self.point_map) |*map| { var discarding: std.Io.Writer.Discarding = .init(&.{}); try self.formatHyperlinkOpen( + emit, &discarding.writer, uri, ); - for (0..std.math.cast( - usize, - discarding.count, - ) orelse return error.WriteFailed) |_| map.map.append(map.alloc, .{ - .x = x, - .y = y, - }) catch return error.WriteFailed; + map.map.appendNTimes( + map.alloc, + .{ .x = x, .y = y }, + std.math.cast( + usize, + discarding.count, + ) orelse return error.WriteFailed, + ) catch return error.WriteFailed; } } @@ -1306,20 +1521,21 @@ pub const PageFormatter = struct { // We combine codepoint and graphemes because both have // shared style handling. We use comptime to dup it. inline .codepoint, .codepoint_grapheme => |tag| { - try self.writeCell(tag, writer, cell); + try self.writeCell(tag, emit, writer, cell); // If we have a point map, all codepoints map to this // cell. if (self.point_map) |*map| { var discarding: std.Io.Writer.Discarding = .init(&.{}); - try self.writeCell(tag, &discarding.writer, cell); - for (0..std.math.cast( - usize, - discarding.count, - ) orelse return error.WriteFailed) |_| map.map.append(map.alloc, .{ - .x = x, - .y = y, - }) catch return error.WriteFailed; + try self.writeCell(tag, emit, &discarding.writer, cell); + map.map.appendNTimes( + map.alloc, + .{ .x = x, .y = y }, + std.math.cast( + usize, + discarding.count, + ) orelse return error.WriteFailed, + ) catch return error.WriteFailed; } }, @@ -1337,13 +1553,13 @@ pub const PageFormatter = struct { } // If the style is non-default, we need to close our style tag. - if (!style.default()) try self.formatStyleClose(writer); + if (!style.default()) try self.formatStyleClose(emit, writer); // Close any open hyperlink for HTML output - if (current_hyperlink_id != null) try self.formatHyperlinkClose(writer); + if (current_hyperlink_id != null) try self.formatHyperlinkClose(emit, writer); // Close the monospace wrapper for HTML output - if (self.opts.emit == .html) { + if (comptime emit == .html) { const closing = "
"; try writer.writeAll(closing); if (self.point_map) |*map| { @@ -1356,14 +1572,315 @@ pub const PageFormatter = struct { closing.len, ); } + // Closing the div creates a newline in the output + // so make sure to create one less newline + if (blank_rows >= 1) blank_rows -= 1; } return .{ .rows = blank_rows, .cells = blank_cells }; } + /// Fast path for writing runs of simple cells: single-codepoint cells + /// that require no style or hyperlink handling. Output bytes are + /// batched into a stack buffer to avoid per-cell writer dispatch. + /// Returns the number of cells consumed, which may be zero if the + /// first cell isn't eligible for the fast path (in which case the + /// caller must handle it via the slow path). + /// + /// Requirements (asserted by the caller, not here): + /// + /// - The codepoint map is empty. + /// - For styled formats, `run_style_id` is the valid interned + /// page-local id of the currently active style. + /// - For HTML, no hyperlink is currently open. + /// + /// `run_x`/`run_y` are the page coordinates of `cells[0]`, used for + /// point map tracking. + /// + /// Blank cell accounting matches the slow path: accumulated blanks + /// are only materialized once a non-blank cell is found, and any + /// remainder is written back to `blank_cells`. + // Deliberately not inlined: this is instantiated per emit format and + // inlining every copy into formatWithStateEmit's row loop bloats the + // binary. track_points is a runtime bool for the same reason: a + // comptime bool doubles the instantiation count for one predictable + // branch per emitted cell. + noinline fn writeCellRun( + self: *const PageFormatter, + comptime emit: Format, + track_points: bool, + writer: *std.Io.Writer, + cells: []const Cell, + run_x: size.CellCountInt, + run_y: size.CellCountInt, + run_style_id: u32, + run_hyperlink_id: ?hyperlink.Id, + blank_cells: *usize, + ) std.Io.Writer.Error!usize { + assert(track_points == (self.point_map != null)); + + // The largest single-cell encoding must fit after a flush: the + // HTML entity for the maximum codepoint ("�") is 10 + // bytes, escapes are up to 6. + const max_encoding_len = 16; + var buf: [512]u8 = undefined; + var len: usize = 0; + var pending: usize = blank_cells.*; + + var i: usize = 0; + while (i < cells.len) : (i += 1) { + const cell = &cells[i]; + + // Spacers produce no output, matching the slow path which + // skips them before any blank/style handling. + switch (cell.wide) { + .narrow, .wide => {}, + .spacer_head, .spacer_tail => continue, + } + + // Only text cells: bg-color cells synthesize styles and take + // the slow path. + switch (cell.content_tag) { + .codepoint, .codepoint_grapheme => {}, + .bg_color_palette, .bg_color_rgb => break, + } + + if (comptime formatStyled(emit)) { + // Style transition, take the slow path. + if (cell.style_id != run_style_id) break; + } + + const cp: u21 = cell.content.codepoint.data; + + // Blank cell accounting, matching the slow path blank block. + if (comptime formatStyled(emit)) { + // Styled formats only treat unstyled empty cells as + // blank; anything else (including spaces) is written + // so that styling is preserved. + if (cp == 0 and cell.wide == .narrow and run_style_id == 0) { + pending += 1; + continue; + } + } else { + // Cells with no text are blank. + if (cp == 0) { + pending += 1; + continue; + } + + // Trailing spaces are blank. + if (cp == ' ' and self.opts.trim) { + pending += 1; + continue; + } + } + + // Hyperlink state must be stable within a run: any non-blank + // cell must belong to the currently open hyperlink (or none). + // Transitions take the slow path. This is checked after blank + // accounting because blank cells never touch hyperlink state. + if (comptime emit == .html) { + if (cell.hyperlink) { + const run_id = run_hyperlink_id orelse break; + const cell_id = self.page.lookupHyperlink(cell) orelse break; + if (cell_id != run_id) break; + } else if (run_hyperlink_id != null) break; + } + + // The page coordinate of this cell, for point tracking. + const x: size.CellCountInt = @intCast(run_x + i); + + // This cell produces output: materialize accumulated blanks. + if (pending > 0) { + if (track_points) try self.appendBlankPoints( + &self.point_map.?, + pending, + x, + run_y, + ); + + while (pending > 0) { + if (len == buf.len) { + try writer.writeAll(buf[0..len]); + len = 0; + } + const n = @min(pending, buf.len - len); + @memset(buf[len..][0..n], ' '); + len += n; + pending -= n; + } + } + + // Flush if the largest possible encoding may not fit. + if (len + max_encoding_len > buf.len) { + try writer.writeAll(buf[0..len]); + len = 0; + } + + var cell_bytes: usize = 0; + + // Empty (but styled or wide) cells emit a space, matching + // writeCell. + if (cp == 0) { + buf[len] = ' '; + len += 1; + cell_bytes = 1; + } else { + cell_bytes = encodeCodepoint(emit, &buf, &len, cp); + + // Multi-codepoint graphemes emit their extra codepoints, + // matching writeCell. This is out-of-line to keep the + // hot loop for the common single-codepoint case small. + if (cell.content_tag == .codepoint_grapheme) { + @branchHint(.unlikely); + cell_bytes += try self.writeGraphemeCps( + emit, + writer, + cell, + &buf, + &len, + ); + } + } + + // All of the cell's bytes map to the cell's coordinate. + if (track_points) { + const map = &self.point_map.?; + map.map.appendNTimes( + map.alloc, + .{ .x = x, .y = run_y }, + cell_bytes, + ) catch return error.WriteFailed; + } + } + + if (len > 0) try writer.writeAll(buf[0..len]); + blank_cells.* = pending; + return i; + } + + /// Encode the extra codepoints of a multi-codepoint grapheme into + /// buf, flushing to the writer as needed. Returns the number of + /// bytes written. This is deliberately not inlined so that the + /// (rare) grapheme case doesn't bloat the writeCellRun hot loop. + noinline fn writeGraphemeCps( + self: *const PageFormatter, + comptime emit: Format, + writer: *std.Io.Writer, + cell: *const Cell, + buf: *[512]u8, + len: *usize, + ) std.Io.Writer.Error!usize { + const max_encoding_len = 16; + var bytes: usize = 0; + for (self.page.lookupGrapheme(cell).?) |gcp| { + if (len.* + max_encoding_len > buf.len) { + try writer.writeAll(buf[0..len.*]); + len.* = 0; + } + bytes += encodeCodepoint(emit, buf, len, gcp); + } + return bytes; + } + + /// Encode a single codepoint into buf at len, advancing len and + /// returning the number of bytes written. The caller must guarantee + /// enough remaining buffer space for the largest possible encoding. + inline fn encodeCodepoint( + comptime emit: Format, + buf: *[512]u8, + len: *usize, + cp: u21, + ) usize { + const start = len.*; + switch (emit) { + .plain, .vt => if (cp < 0x80) { + buf[start] = @intCast(cp); + len.* += 1; + } else { + len.* += std.unicode.utf8Encode(cp, buf[start..][0..4]) catch l: { + // Matches Writer.printUnicodeCodepoint: invalid + // codepoints become the replacement character. + buf[start..][0..3].* = std.unicode.replacement_character_utf8; + break :l 3; + }; + }, + + .html => html: { + const esc: ?[]const u8 = switch (cp) { + '<' => "<", + '>' => ">", + '&' => "&", + '"' => """, + '\'' => "'", + else => null, + }; + if (esc) |s| { + @memcpy(buf[start..][0..s.len], s); + len.* += s.len; + break :html; + } + + // ASCII is emitted directly, everything else as a + // numeric entity. See writeCodepoint. + if (cp < 0x80) { + buf[start] = @intCast(cp); + len.* += 1; + break :html; + } + + buf[start..][0..2].* = "&#".*; + len.* += 2; + len.* += fastprint.printDecimal(u21, buf[len.*..], cp); + buf[len.*] = ';'; + len.* += 1; + }, + } + + return len.* - start; + } + + /// Append the point map entries for a run of `count` blank cells + /// that are materialized as spaces just before the cell at (x, y). + /// Blank cells can span multiple rows if they carry over from wrap + /// continuation, so this walks backwards from (x, y). + fn appendBlankPoints( + self: *const PageFormatter, + map: *const PointMap, + count: usize, + x: size.CellCountInt, + y: size.CellCountInt, + ) std.Io.Writer.Error!void { + map.map.ensureUnusedCapacity( + map.alloc, + count, + ) catch return error.WriteFailed; + + var remaining = count; + var blank_x = x; + var blank_y = y; + while (remaining > 0) : (remaining -= 1) { + if (blank_x > 0) { + // We have space in this row + blank_x -= 1; + } else if (blank_y > 0) { + // Wrap to previous row + blank_y -= 1; + blank_x = self.page.size.cols - 1; + } else { + // Can't go back further, just use (0, 0) + blank_x = 0; + blank_y = 0; + } + + map.map.appendAssumeCapacity(.{ .x = blank_x, .y = blank_y }); + } + } + fn writeCell( self: PageFormatter, comptime tag: Cell.ContentTag, + comptime emit: Format, writer: *std.Io.Writer, cell: *const Cell, ) !void { @@ -1375,16 +1892,17 @@ pub const PageFormatter = struct { return; } - try self.writeCodepointWithReplacement(writer, cell.content.codepoint); + try self.writeCodepointWithReplacement(emit, writer, cell.content.codepoint.data); if (comptime tag == .codepoint_grapheme) { for (self.page.lookupGrapheme(cell).?) |cp| { - try self.writeCodepointWithReplacement(writer, cp); + try self.writeCodepointWithReplacement(emit, writer, cp); } } } fn writeCodepointWithReplacement( self: PageFormatter, + comptime emit: Format, writer: *std.Io.Writer, codepoint: u21, ) !void { @@ -1406,12 +1924,14 @@ pub const PageFormatter = struct { // If no replacement, write it directly. const r = r_ orelse return try self.writeCodepoint( + emit, writer, codepoint, ); switch (r) { .codepoint => |v| try self.writeCodepoint( + emit, writer, v, ), @@ -1420,6 +1940,7 @@ pub const PageFormatter = struct { const view = std.unicode.Utf8View.init(s) catch unreachable; var it = view.iterator(); while (it.nextCodepoint()) |cp| try self.writeCodepoint( + emit, writer, cp, ); @@ -1429,11 +1950,13 @@ pub const PageFormatter = struct { fn writeCodepoint( self: PageFormatter, + comptime emit: Format, writer: *std.Io.Writer, codepoint: u21, ) !void { - switch (self.opts.emit) { - .plain, .vt => try writer.print("{u}", .{codepoint}), + _ = self; + switch (emit) { + .plain, .vt => try writer.printUnicodeCodepoint(codepoint), .html => { switch (codepoint) { '<' => try writer.writeAll("<"), @@ -1448,9 +1971,14 @@ pub const PageFormatter = struct { // meta tag because we emit partial HTML so this ensures // proper unicode handling. if (codepoint < 0x80) { - try writer.print("{u}", .{codepoint}); + try writer.writeByte(@intCast(codepoint)); } else { - try writer.print("&#{d};", .{codepoint}); + var buf: [16]u8 = undefined; + buf[0..2].* = "&#".*; + var len: usize = 2 + fastprint.printDecimal(u21, buf[2..], codepoint); + buf[len] = ';'; + len += 1; + try writer.writeAll(buf[0..len]); } }, } @@ -1475,7 +2003,7 @@ pub const PageFormatter = struct { .bg_color_palette => .{ .bg_color = .{ - .palette = cell.content.color_palette, + .palette = cell.content.color_palette.data, }, }, @@ -1495,16 +2023,17 @@ pub const PageFormatter = struct { /// and other HTML attribute values. fn formatStyleOpen( self: PageFormatter, + comptime emit: Format, writer: *std.Io.Writer, style: *const Style, ) std.Io.Writer.Error!void { - switch (self.opts.emit) { + switch (emit) { .plain => unreachable, .vt => { var formatter = style.formatterVt(); formatter.palette = self.opts.palette; - try writer.print("{f}", .{formatter}); + try formatter.format(writer); }, // We use `display: inline` so that the div doesn't impact @@ -1512,19 +2041,19 @@ pub const PageFormatter = struct { .html => { var formatter = style.formatterHtml(); formatter.palette = self.opts.palette; - try writer.print( - "
", - .{formatter}, - ); + try writer.writeAll("
"); }, } } fn formatStyleClose( self: PageFormatter, + comptime emit: Format, writer: *std.Io.Writer, ) std.Io.Writer.Error!void { - const str: []const u8 = switch (self.opts.emit) { + const str: []const u8 = switch (emit) { .plain => return, .vt => "\x1b[0m", .html => "
", @@ -1546,16 +2075,18 @@ pub const PageFormatter = struct { fn formatHyperlinkOpen( self: PageFormatter, + comptime emit: Format, writer: *std.Io.Writer, uri: []const u8, ) std.Io.Writer.Error!void { - switch (self.opts.emit) { + switch (emit) { .plain, .vt => unreachable, // layout since we're primarily using it as a CSS wrapper. .html => { try writer.writeAll(" "", .plain, .vt => return, }; @@ -1591,11 +2123,12 @@ pub const PageFormatter = struct { test "Page plain single line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -1638,11 +2171,12 @@ test "Page plain single line" { test "Page plain single line soft-wrapped unwrapped" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 3, .rows = 5, }); @@ -1708,11 +2242,12 @@ test "Page plain single line soft-wrapped unwrapped" { test "Page plain single wide char" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -1799,11 +2334,12 @@ test "Page plain single wide char" { test "Page plain single wide char soft-wrapped unwrapped" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 3, .rows = 24, }); @@ -1916,11 +2452,12 @@ test "Page plain single wide char soft-wrapped unwrapped" { test "Page plain multiline" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -1967,11 +2504,12 @@ test "Page plain multiline" { test "Page plain multiline rectangle" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2021,11 +2559,12 @@ test "Page plain multiline rectangle" { test "Page plain multi blank lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2074,11 +2613,12 @@ test "Page plain multi blank lines" { test "Page plain trailing blank lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2127,11 +2667,12 @@ test "Page plain trailing blank lines" { test "Page plain trailing whitespace" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2180,11 +2721,12 @@ test "Page plain trailing whitespace" { test "Page plain trailing whitespace no trim" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2236,11 +2778,12 @@ test "Page plain trailing whitespace no trim" { test "Page plain with prior trailing state rows" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2282,11 +2825,12 @@ test "Page plain with prior trailing state rows" { test "Page plain with prior trailing state cells no wrapped line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2327,11 +2871,12 @@ test "Page plain with prior trailing state cells no wrapped line" { test "Page plain with prior trailing state cells with wrap continuation" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2381,11 +2926,12 @@ test "Page plain with prior trailing state cells with wrap continuation" { test "Page plain soft-wrapped without unwrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 24, }); @@ -2430,11 +2976,12 @@ test "Page plain soft-wrapped without unwrap" { test "Page plain soft-wrapped with unwrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 24, }); @@ -2478,11 +3025,12 @@ test "Page plain soft-wrapped with unwrap" { test "Page plain soft-wrapped 3 lines without unwrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 24, }); @@ -2532,11 +3080,12 @@ test "Page plain soft-wrapped 3 lines without unwrap" { test "Page plain soft-wrapped 3 lines with unwrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 24, }); @@ -2584,11 +3133,12 @@ test "Page plain soft-wrapped 3 lines with unwrap" { test "Page plain start_y subset" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2631,11 +3181,12 @@ test "Page plain start_y subset" { test "Page plain end_y subset" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2678,11 +3229,12 @@ test "Page plain end_y subset" { test "Page plain start_y and end_y range" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2726,11 +3278,12 @@ test "Page plain start_y and end_y range" { test "Page plain start_y out of bounds" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2764,11 +3317,12 @@ test "Page plain start_y out of bounds" { test "Page plain end_y greater than rows" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2807,11 +3361,12 @@ test "Page plain end_y greater than rows" { test "Page plain end_y less than start_y" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2846,11 +3401,12 @@ test "Page plain end_y less than start_y" { test "Page plain start_x on first row only" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2888,11 +3444,12 @@ test "Page plain start_x on first row only" { test "Page plain end_x on last row only" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2941,11 +3498,12 @@ test "Page plain end_x on last row only" { test "Page plain start_x and end_x multiline" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -2998,11 +3556,12 @@ test "Page plain start_x and end_x multiline" { test "Page plain start_x out of bounds" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3036,11 +3595,12 @@ test "Page plain start_x out of bounds" { test "Page plain end_x greater than cols" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3078,11 +3638,12 @@ test "Page plain end_x greater than cols" { test "Page plain end_x less than start_x single row" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3118,11 +3679,12 @@ test "Page plain end_x less than start_x single row" { test "Page plain start_y non-zero ignores trailing state" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3162,11 +3724,12 @@ test "Page plain start_y non-zero ignores trailing state" { test "Page plain start_x non-zero ignores trailing state" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3206,11 +3769,12 @@ test "Page plain start_x non-zero ignores trailing state" { test "Page plain start_y and start_x zero uses trailing state" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3253,11 +3817,12 @@ test "Page plain start_y and start_x zero uses trailing state" { test "Page plain single line with styling" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3299,11 +3864,12 @@ test "Page plain single line with styling" { test "Page VT single line plain text" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3338,11 +3904,12 @@ test "Page VT single line plain text" { test "Page VT single line with bold" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3384,11 +3951,12 @@ test "Page VT single line with bold" { test "Page VT multiple styles" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3419,11 +3987,12 @@ test "Page VT multiple styles" { test "Page VT with foreground color" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3465,11 +4034,12 @@ test "Page VT with foreground color" { test "Page VT with background and foreground colors" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3502,11 +4072,12 @@ test "Page VT with background and foreground colors" { test "Page VT multi-line with styles" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3539,11 +4110,12 @@ test "Page VT multi-line with styles" { test "Page VT duplicate style not emitted twice" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3574,11 +4146,12 @@ test "Page VT duplicate style not emitted twice" { test "PageList plain single line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3589,7 +4162,7 @@ test "PageList plain single line" { s.nextSlice("hello, world"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(&t.screens.active.pages, .plain); @@ -3599,22 +4172,23 @@ test "PageList plain single line" { try testing.expectEqualStrings("hello, world", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| try testing.expectEqual( Pin{ .node = node, .x = @intCast(i), .y = 0 }, - pin_map.items[i], + pin_map.get(i).?, ); } test "PageList plain spanning two pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3641,7 +4215,7 @@ test "PageList plain spanning two pages" { s.nextSlice("page two"); // Format the entire PageList - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(pages, .plain); @@ -3652,42 +4226,43 @@ test "PageList plain spanning two pages" { try testing.expectEqualStrings("page one\npage two", output); // Verify pin map - try testing.expectEqual(full_output.len, pin_map.items.len); + try testing.expectEqual(full_output.len, pin_map.count()); const first_node = pages.pages.first.?; const last_node = pages.pages.last.?; const trimmed_count = full_output.len - output.len; // First part (trimmed blank lines) maps to first node for (0..trimmed_count) |i| { - try testing.expectEqual(first_node, pin_map.items[i].node); + try testing.expectEqual(first_node, pin_map.get(i).?.node); } // "page one" (8 chars) maps to first node for (0..8) |i| { const idx = trimmed_count + i; - try testing.expectEqual(first_node, pin_map.items[idx].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[idx].x); + try testing.expectEqual(first_node, pin_map.get(idx).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(idx).?.x); } // \n - maps to last node as it represents the transition to new page - try testing.expectEqual(last_node, pin_map.items[trimmed_count + 8].node); + try testing.expectEqual(last_node, pin_map.get(trimmed_count + 8).?.node); // "page two" (8 chars) maps to last node for (0..8) |i| { const idx = trimmed_count + 9 + i; - try testing.expectEqual(last_node, pin_map.items[idx].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[idx].x); + try testing.expectEqual(last_node, pin_map.get(idx).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(idx).?.x); } } test "PageList soft-wrapped line spanning two pages without unwrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -3707,7 +4282,7 @@ test "PageList soft-wrapped line spanning two pages without unwrap" { try testing.expect(pages.pages.first != pages.pages.last); // Format without unwrap - should show line breaks - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(pages, .plain); @@ -3718,40 +4293,41 @@ test "PageList soft-wrapped line spanning two pages without unwrap" { try testing.expectEqualStrings("hello worl\nd test", output); // Verify pin map - try testing.expectEqual(full_output.len, pin_map.items.len); + try testing.expectEqual(full_output.len, pin_map.count()); const first_node = pages.pages.first.?; const last_node = pages.pages.last.?; const trimmed_count = full_output.len - output.len; // First part (trimmed blank lines) maps to first node for (0..trimmed_count) |i| { - try testing.expectEqual(first_node, pin_map.items[i].node); + try testing.expectEqual(first_node, pin_map.get(i).?.node); } // First line maps to first node for (0..10) |i| { const idx = trimmed_count + i; - try testing.expectEqual(first_node, pin_map.items[idx].node); + try testing.expectEqual(first_node, pin_map.get(idx).?.node); } // \n - maps to last node as it represents the transition to new page - try testing.expectEqual(last_node, pin_map.items[trimmed_count + 10].node); + try testing.expectEqual(last_node, pin_map.get(trimmed_count + 10).?.node); // "d test" (6 chars) maps to last node for (0..6) |i| { const idx = trimmed_count + 11 + i; - try testing.expectEqual(last_node, pin_map.items[idx].node); + try testing.expectEqual(last_node, pin_map.get(idx).?.node); } } test "PageList soft-wrapped line spanning two pages with unwrap" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -3771,7 +4347,7 @@ test "PageList soft-wrapped line spanning two pages with unwrap" { try testing.expect(pages.pages.first != pages.pages.last); // Format with unwrap - should join the wrapped lines - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(pages, .{ .emit = .plain, .unwrap = true }); @@ -3782,37 +4358,38 @@ test "PageList soft-wrapped line spanning two pages with unwrap" { try testing.expectEqualStrings("hello world test", output); // Verify pin map - try testing.expectEqual(full_output.len, pin_map.items.len); + try testing.expectEqual(full_output.len, pin_map.count()); const first_node = pages.pages.first.?; const last_node = pages.pages.last.?; const trimmed_count = full_output.len - output.len; // First part (trimmed blank lines) maps to first node for (0..trimmed_count) |i| { - try testing.expectEqual(first_node, pin_map.items[i].node); + try testing.expectEqual(first_node, pin_map.get(i).?.node); } // First line from first page for (0..10) |i| { const idx = trimmed_count + i; - try testing.expectEqual(first_node, pin_map.items[idx].node); + try testing.expectEqual(first_node, pin_map.get(idx).?.node); } // "d test" (6 chars) from last page for (0..6) |i| { const idx = trimmed_count + 10 + i; - try testing.expectEqual(last_node, pin_map.items[idx].node); + try testing.expectEqual(last_node, pin_map.get(idx).?.node); } } test "PageList VT spanning two pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3839,7 +4416,7 @@ test "PageList VT spanning two pages" { s.nextSlice("page two"); // Format the entire PageList with VT - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(pages, .vt); @@ -3850,14 +4427,15 @@ test "PageList VT spanning two pages" { try testing.expectEqualStrings("\x1b[0m\x1b[1mpage one\x1b[0m\r\n\x1b[0m\x1b[1mpage two\x1b[0m", output); // Verify pin map - try testing.expectEqual(full_output.len, pin_map.items.len); + try testing.expectEqual(full_output.len, pin_map.count()); const first_node = pages.pages.first.?; const last_node = pages.pages.last.?; // Just verify we have entries for both pages in the pin map var first_count: usize = 0; var last_count: usize = 0; - for (pin_map.items) |pin| { + for (0..pin_map.count()) |byte_i| { + const pin = pin_map.get(byte_i).?; if (pin.node == first_node) first_count += 1; if (pin.node == last_node) last_count += 1; } @@ -3868,11 +4446,12 @@ test "PageList VT spanning two pages" { test "PageList plain with x offset on single page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3886,7 +4465,7 @@ test "PageList plain with x offset on single page" { const pages = &t.screens.active.pages; const node = pages.pages.first.?; - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(pages, .plain); @@ -3899,26 +4478,28 @@ test "PageList plain with x offset on single page" { try testing.expectEqualStrings("world\ntest case\nfoo", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); - for (pin_map.items) |pin| { + try testing.expectEqual(output.len, pin_map.count()); + for (0..pin_map.count()) |byte_i| { + const pin = pin_map.get(byte_i).?; try testing.expectEqual(node, pin.node); } // "world" starts at x=6, y=0 for (0..5) |i| { - try testing.expectEqual(@as(size.CellCountInt, @intCast(6 + i)), pin_map.items[i].x); - try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.items[i].y); + try testing.expectEqual(@as(size.CellCountInt, @intCast(6 + i)), pin_map.get(i).?.x); + try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.get(i).?.y); } } test "PageList plain with x offset spanning two pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -3946,7 +4527,7 @@ test "PageList plain with x offset spanning two pages" { const first_node = pages.pages.first.?; const last_node = pages.pages.last.?; - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(pages, .plain); @@ -3960,35 +4541,36 @@ test "PageList plain with x offset spanning two pages" { try testing.expectEqualStrings("world\nfoo", output); // Verify pin map - try testing.expectEqual(full_output.len, pin_map.items.len); + try testing.expectEqual(full_output.len, pin_map.count()); const trimmed_count = full_output.len - output.len; // "world" (5 chars) from first page for (0..5) |i| { const idx = trimmed_count + i; - try testing.expectEqual(first_node, pin_map.items[idx].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(6 + i)), pin_map.items[idx].x); + try testing.expectEqual(first_node, pin_map.get(idx).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(6 + i)), pin_map.get(idx).?.x); } // \n - maps to last node as it represents the transition to new page - try testing.expectEqual(last_node, pin_map.items[trimmed_count + 5].node); + try testing.expectEqual(last_node, pin_map.get(trimmed_count + 5).?.node); // "foo" (3 chars) from last page for (0..3) |i| { const idx = trimmed_count + 6 + i; - try testing.expectEqual(last_node, pin_map.items[idx].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[idx].x); + try testing.expectEqual(last_node, pin_map.get(idx).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(idx).?.x); } } test "PageList plain with start_x only" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4002,7 +4584,7 @@ test "PageList plain with start_x only" { const pages = &t.screens.active.pages; const node = pages.pages.first.?; - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(pages, .plain); @@ -4014,22 +4596,23 @@ test "PageList plain with start_x only" { try testing.expectEqualStrings("world", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); for (0..5) |i| { - try testing.expectEqual(node, pin_map.items[i].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(6 + i)), pin_map.items[i].x); - try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.items[i].y); + try testing.expectEqual(node, pin_map.get(i).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(6 + i)), pin_map.get(i).?.x); + try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.get(i).?.y); } } test "PageList plain with end_x only" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4043,7 +4626,7 @@ test "PageList plain with end_x only" { const pages = &t.screens.active.pages; const node = pages.pages.first.?; - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: PageListFormatter = .init(pages, .plain); @@ -4055,34 +4638,35 @@ test "PageList plain with end_x only" { try testing.expectEqualStrings("hello world\ntes", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); // "hello world" (11 chars) on y=0 for (0..11) |i| { - try testing.expectEqual(node, pin_map.items[i].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[i].x); - try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.items[i].y); + try testing.expectEqual(node, pin_map.get(i).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(i).?.x); + try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.get(i).?.y); } // \n - try testing.expectEqual(node, pin_map.items[11].node); + try testing.expectEqual(node, pin_map.get(11).?.node); // "tes" (3 chars) on y=1 for (0..3) |i| { - try testing.expectEqual(node, pin_map.items[12 + i].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[12 + i].x); - try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.items[12 + i].y); + try testing.expectEqual(node, pin_map.get(12 + i).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(12 + i).?.x); + try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.get(12 + i).?.y); } } test "PageList plain rectangle basic" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 30, .rows = 5, }); @@ -4118,11 +4702,12 @@ test "PageList plain rectangle basic" { test "PageList plain rectangle with EOL" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 30, .rows = 5, }); @@ -4160,11 +4745,12 @@ test "PageList plain rectangle with EOL" { test "PageList plain rectangle more complex with breaks" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 30, .rows = 8, }); @@ -4206,11 +4792,12 @@ test "PageList plain rectangle more complex with breaks" { test "TerminalFormatter plain no selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4230,11 +4817,12 @@ test "TerminalFormatter plain no selection" { test "TerminalFormatter vt with palette" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4255,7 +4843,7 @@ test "TerminalFormatter vt with palette" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4275,11 +4863,12 @@ test "TerminalFormatter vt with palette" { test "TerminalFormatter with selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4304,11 +4893,12 @@ test "TerminalFormatter with selection" { test "TerminalFormatter plain with pin_map" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4319,7 +4909,7 @@ test "TerminalFormatter plain with pin_map" { s.nextSlice("hello, world"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: TerminalFormatter = .init(&t, .plain); @@ -4330,22 +4920,23 @@ test "TerminalFormatter plain with pin_map" { try testing.expectEqualStrings("hello, world", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| try testing.expectEqual( Pin{ .node = node, .x = @intCast(i), .y = 0 }, - pin_map.items[i], + pin_map.get(i).?, ); } test "TerminalFormatter plain multiline with pin_map" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4356,7 +4947,7 @@ test "TerminalFormatter plain multiline with pin_map" { s.nextSlice("hello\r\nworld"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: TerminalFormatter = .init(&t, .plain); @@ -4367,33 +4958,34 @@ test "TerminalFormatter plain multiline with pin_map" { try testing.expectEqualStrings("hello\nworld", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; // "hello" (5 chars) for (0..5) |i| { - try testing.expectEqual(node, pin_map.items[i].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[i].x); - try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.items[i].y); + try testing.expectEqual(node, pin_map.get(i).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(i).?.x); + try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.get(i).?.y); } // "\n" maps to end of first line - try testing.expectEqual(node, pin_map.items[5].node); + try testing.expectEqual(node, pin_map.get(5).?.node); // "world" (5 chars) for (0..5) |i| { const idx = 6 + i; - try testing.expectEqual(node, pin_map.items[idx].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[idx].x); - try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.items[idx].y); + try testing.expectEqual(node, pin_map.get(idx).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(idx).?.x); + try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.get(idx).?.y); } } test "TerminalFormatter vt with palette and pin_map" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4406,7 +4998,7 @@ test "TerminalFormatter vt with palette and pin_map" { s.nextSlice("\x1b]4;0;rgb:12/34/56\x1b\\"); s.nextSlice("test"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: TerminalFormatter = .init(&t, .vt); @@ -4416,21 +5008,22 @@ test "TerminalFormatter vt with palette and pin_map" { const output = builder.writer.buffered(); // Verify pin map - palette bytes should be mapped to top left - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| { - try testing.expectEqual(node, pin_map.items[i].node); + try testing.expectEqual(node, pin_map.get(i).?.node); } } test "TerminalFormatter with selection and pin_map" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4441,7 +5034,7 @@ test "TerminalFormatter with selection and pin_map" { s.nextSlice("line1\r\nline2\r\nline3"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: TerminalFormatter = .init(&t, .plain); @@ -4457,24 +5050,25 @@ test "TerminalFormatter with selection and pin_map" { try testing.expectEqualStrings("line2", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; // "line2" (5 chars) from row 1 for (0..5) |i| { - try testing.expectEqual(node, pin_map.items[i].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[i].x); - try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.items[i].y); + try testing.expectEqual(node, pin_map.get(i).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(i).?.x); + try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.get(i).?.y); } } test "Screen plain single line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4485,7 +5079,7 @@ test "Screen plain single line" { s.nextSlice("hello, world"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .plain); @@ -4496,22 +5090,23 @@ test "Screen plain single line" { try testing.expectEqualStrings("hello, world", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| try testing.expectEqual( Pin{ .node = node, .x = @intCast(i), .y = 0 }, - pin_map.items[i], + pin_map.get(i).?, ); } test "Screen plain multiline" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4522,7 +5117,7 @@ test "Screen plain multiline" { s.nextSlice("hello\r\nworld"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .plain); @@ -4533,33 +5128,34 @@ test "Screen plain multiline" { try testing.expectEqualStrings("hello\nworld", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; // "hello" (5 chars) for (0..5) |i| { - try testing.expectEqual(node, pin_map.items[i].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[i].x); - try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.items[i].y); + try testing.expectEqual(node, pin_map.get(i).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(i).?.x); + try testing.expectEqual(@as(size.CellCountInt, 0), pin_map.get(i).?.y); } // "\n" maps to end of first line - try testing.expectEqual(node, pin_map.items[5].node); + try testing.expectEqual(node, pin_map.get(5).?.node); // "world" (5 chars) for (0..5) |i| { const idx = 6 + i; - try testing.expectEqual(node, pin_map.items[idx].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[idx].x); - try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.items[idx].y); + try testing.expectEqual(node, pin_map.get(idx).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(idx).?.x); + try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.get(idx).?.y); } } test "Screen plain with selection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4570,7 +5166,7 @@ test "Screen plain with selection" { s.nextSlice("line1\r\nline2\r\nline3"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .plain); @@ -4586,24 +5182,25 @@ test "Screen plain with selection" { try testing.expectEqualStrings("line2", output); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; // "line2" (5 chars) from row 1 for (0..5) |i| { - try testing.expectEqual(node, pin_map.items[i].node); - try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.items[i].x); - try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.items[i].y); + try testing.expectEqual(node, pin_map.get(i).?.node); + try testing.expectEqual(@as(size.CellCountInt, @intCast(i)), pin_map.get(i).?.x); + try testing.expectEqual(@as(size.CellCountInt, 1), pin_map.get(i).?.y); } } test "Screen vt with cursor position" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4615,7 +5212,7 @@ test "Screen vt with cursor position" { // Position cursor at a specific location s.nextSlice("hello\r\nworld"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .vt); @@ -4626,7 +5223,7 @@ test "Screen vt with cursor position" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4642,27 +5239,87 @@ test "Screen vt with cursor position" { try testing.expectEqual(t.screens.active.cursor.y, t2.screens.active.cursor.y); // Verify pin map - the extras should be mapped to the last pin - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; const content_len = "hello\r\nworld".len; // Content bytes map to their positions for (0..content_len) |i| { - try testing.expectEqual(node, pin_map.items[i].node); + try testing.expectEqual(node, pin_map.get(i).?.node); } // Extra bytes (cursor position) map to last content pin for (content_len..output.len) |i| { - try testing.expectEqual(node, pin_map.items[i].node); + try testing.expectEqual(node, pin_map.get(i).?.node); } } +test "Terminal vt cursor preserves pending wrap" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var builder: std.Io.Writer.Allocating = .init(alloc); + defer builder.deinit(); + + var source = try Terminal.init(io, alloc, .{ + .cols = 4, + .rows = 2, + }); + defer source.deinit(alloc); + + var source_stream = source.vtStream(); + defer source_stream.deinit(); + source_stream.nextSlice("abcd"); + try testing.expect(source.screens.active.cursor.pending_wrap); + + var pin_map: PinMap.Map = .empty; + defer pin_map.deinit(alloc); + + var formatter: TerminalFormatter = .init(&source, .vt); + formatter.extra = .none; + formatter.extra.screen.cursor = true; + formatter.pin_map = .{ .alloc = alloc, .map = &pin_map }; + try formatter.format(&builder.writer); + try testing.expectEqual(builder.writer.buffered().len, pin_map.count()); + + var target = try Terminal.init(io, alloc, .{ + .cols = 4, + .rows = 2, + }); + defer target.deinit(alloc); + + var target_stream = target.vtStream(); + defer target_stream.deinit(); + target_stream.nextSlice(builder.writer.buffered()); + + try testing.expectEqual( + source.screens.active.cursor.pending_wrap, + target.screens.active.cursor.pending_wrap, + ); + + source_stream.nextSlice("X"); + target_stream.nextSlice("X"); + const source_contents = try source.screens.active.dumpStringAlloc( + alloc, + .{ .screen = .{} }, + ); + defer alloc.free(source_contents); + const target_contents = try target.screens.active.dumpStringAlloc( + alloc, + .{ .screen = .{} }, + ); + defer alloc.free(target_contents); + try testing.expectEqualStrings(source_contents, target_contents); +} + test "Screen vt with style" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4674,7 +5331,7 @@ test "Screen vt with style" { // Set some style attributes s.nextSlice("\x1b[1;31mhello"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .vt); @@ -4685,7 +5342,7 @@ test "Screen vt with style" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4700,21 +5357,22 @@ test "Screen vt with style" { try testing.expect(t.screens.active.cursor.style.eql(t2.screens.active.cursor.style)); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| { - try testing.expectEqual(node, pin_map.items[i].node); + try testing.expectEqual(node, pin_map.get(i).?.node); } } test "Screen vt with hyperlink" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4726,7 +5384,7 @@ test "Screen vt with hyperlink" { // Set a hyperlink s.nextSlice("\x1b]8;;http://example.com\x1b\\hello"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .vt); @@ -4737,7 +5395,7 @@ test "Screen vt with hyperlink" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4760,21 +5418,22 @@ test "Screen vt with hyperlink" { } // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| { - try testing.expectEqual(node, pin_map.items[i].node); + try testing.expectEqual(node, pin_map.get(i).?.node); } } test "Screen vt with protection" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4786,7 +5445,7 @@ test "Screen vt with protection" { // Enable protection mode s.nextSlice("\x1b[1\"qhello"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .vt); @@ -4797,7 +5456,7 @@ test "Screen vt with protection" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4812,21 +5471,22 @@ test "Screen vt with protection" { try testing.expectEqual(t.screens.active.cursor.protected, t2.screens.active.cursor.protected); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| { - try testing.expectEqual(node, pin_map.items[i].node); + try testing.expectEqual(node, pin_map.get(i).?.node); } } test "Screen vt with kitty keyboard" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4838,7 +5498,7 @@ test "Screen vt with kitty keyboard" { // Set kitty keyboard flags (disambiguate + report_events = 3) s.nextSlice("\x1b[=3;1uhello"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .vt); @@ -4849,7 +5509,7 @@ test "Screen vt with kitty keyboard" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4866,21 +5526,22 @@ test "Screen vt with kitty keyboard" { try testing.expectEqual(flags1, flags2); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| { - try testing.expectEqual(node, pin_map.items[i].node); + try testing.expectEqual(node, pin_map.get(i).?.node); } } test "Screen vt with charsets" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4892,7 +5553,7 @@ test "Screen vt with charsets" { // Set G0 to DEC special and shift to G1 s.nextSlice("\x1b(0\x0ehello"); - var pin_map: std.ArrayList(Pin) = .empty; + var pin_map: PinMap.Map = .empty; defer pin_map.deinit(alloc); var formatter: ScreenFormatter = .init(t.screens.active, .vt); @@ -4903,7 +5564,7 @@ test "Screen vt with charsets" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4923,21 +5584,22 @@ test "Screen vt with charsets" { ); // Verify pin map - try testing.expectEqual(output.len, pin_map.items.len); + try testing.expectEqual(output.len, pin_map.count()); const node = t.screens.active.pages.pages.first.?; for (0..output.len) |i| { - try testing.expectEqual(node, pin_map.items[i].node); + try testing.expectEqual(node, pin_map.get(i).?.node); } } test "Terminal vt with scrolling region" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4956,7 +5618,7 @@ test "Terminal vt with scrolling region" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -4977,11 +5639,12 @@ test "Terminal vt with scrolling region" { test "Terminal vt with modes" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5003,7 +5666,7 @@ test "Terminal vt with modes" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5023,11 +5686,12 @@ test "Terminal vt with modes" { test "Terminal vt with tabstops" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5041,16 +5705,21 @@ test "Terminal vt with tabstops" { s.nextSlice("\x1b[5G\x1bH"); // Set tab at column 5 s.nextSlice("\x1b[15G\x1bH"); // Set tab at column 15 s.nextSlice("\x1b[30G\x1bH"); // Set tab at column 30 - s.nextSlice("hello"); + s.nextSlice("\x1b[Hhello"); + + var pin_map: PinMap.Map = .empty; + defer pin_map.deinit(alloc); var formatter: TerminalFormatter = .init(&t, .vt); formatter.extra.tabstops = true; + formatter.extra.screen.cursor = true; + formatter.pin_map = .{ .alloc = alloc, .map = &pin_map }; try formatter.format(&builder.writer); const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5069,16 +5738,33 @@ test "Terminal vt with tabstops" { try testing.expect(t2.tabstops.get(14)); // Column 15 (1-indexed) try testing.expect(t2.tabstops.get(29)); // Column 30 (1-indexed) try testing.expect(!t2.tabstops.get(8)); // Not a tab + + // Tabstop serialization must not offset the screen contents. + for ("hello", 0..) |expected, col| { + const cell = t2.screens.active.pages.getCell(.{ + .screen = .{ .x = @intCast(col), .y = 0 }, + }).?; + try testing.expectEqual(expected, cell.cell.codepoint()); + } + + // Emitting tabstops moves the cursor to each configured column. When + // cursor state is included, it must be restored afterwards. + try testing.expectEqual(t.screens.active.cursor.x, t2.screens.active.cursor.x); + try testing.expectEqual(t.screens.active.cursor.y, t2.screens.active.cursor.y); + + // Verify the reordered terminal state is still represented in the map. + try testing.expectEqual(output.len, pin_map.count()); } test "Terminal vt with keyboard modes" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5098,7 +5784,7 @@ test "Terminal vt with keyboard modes" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5117,11 +5803,12 @@ test "Terminal vt with keyboard modes" { test "Terminal vt with pwd" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5140,7 +5827,7 @@ test "Terminal vt with pwd" { const output = builder.writer.buffered(); // Create a second terminal and apply the output - var t2 = try Terminal.init(alloc, .{ + var t2 = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5158,11 +5845,12 @@ test "Terminal vt with pwd" { test "Page html with multiple styles" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5194,11 +5882,12 @@ test "Page html with multiple styles" { test "Page html plain text" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5226,11 +5915,12 @@ test "Page html plain text" { test "Page html with colors" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5260,11 +5950,12 @@ test "Page html with colors" { test "TerminalFormatter html with palette" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5297,11 +5988,12 @@ test "TerminalFormatter html with palette" { test "Page html with background and foreground colors" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5332,11 +6024,12 @@ test "Page html with background and foreground colors" { test "Page html with escaping" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5402,11 +6095,12 @@ test "Page html with escaping" { test "Page html with unicode as numeric entities" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5433,14 +6127,52 @@ test "Page html with unicode as numeric entities" { ); } +test "Page html trailing blank lines" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var builder: std.Io.Writer.Allocating = .init(alloc); + defer builder.deinit(); + + var t = try Terminal.init(io, alloc, .{ + .cols = 80, + .rows = 24, + }); + defer t.deinit(alloc); + + var s = t.vtStream(); + defer s.deinit(); + + s.nextSlice("hello\r\nworld\r\n\r\n"); + + const pages = &t.screens.active.pages; + try testing.expect(pages.pages.first != null); + try testing.expect(pages.pages.first == pages.pages.last); + + const page = pages.pages.last.?.page(); + var formatter: PageFormatter = .init(page, .{ .emit = .html }); + + const state = try formatter.formatWithState(&builder.writer); + const output = builder.writer.buffered(); + + // The closing div behaves as a newline + try testing.expectEqual(@as(usize, page.size.rows - 2), state.rows); + try testing.expectEqualStrings( + "
hello\nworld
", + output, + ); +} + test "Page html ascii characters unchanged" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5468,11 +6200,12 @@ test "Page html ascii characters unchanged" { test "Page html mixed ascii and unicode" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5500,11 +6233,12 @@ test "Page html mixed ascii and unicode" { test "Page VT with palette option emits RGB" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5544,11 +6278,12 @@ test "Page VT with palette option emits RGB" { test "Page html with palette option emits RGB" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5598,11 +6333,12 @@ test "Page html with palette option emits RGB" { test "Page VT style reset properly closes styles" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5629,11 +6365,12 @@ test "Page VT style reset properly closes styles" { test "Page codepoint_map single replacement" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5688,11 +6425,12 @@ test "Page codepoint_map single replacement" { test "Page codepoint_map conflicting replacement prefers last" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5730,11 +6468,12 @@ test "Page codepoint_map conflicting replacement prefers last" { test "Page codepoint_map replace with string" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5786,11 +6525,12 @@ test "Page codepoint_map replace with string" { test "Page codepoint_map range replacement" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5824,11 +6564,12 @@ test "Page codepoint_map range replacement" { test "Page codepoint_map multiple ranges" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5868,11 +6609,12 @@ test "Page codepoint_map multiple ranges" { test "Page codepoint_map unicode replacement" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -5933,11 +6675,12 @@ test "Page codepoint_map unicode replacement" { test "Page codepoint_map with styled formats" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 24, }); @@ -5974,11 +6717,12 @@ test "Page codepoint_map with styled formats" { test "Page codepoint_map empty map" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -6011,11 +6755,12 @@ test "Page VT background color on trailing blank cells" { // This causes TUIs like htop to lose background colors on rehydration. const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 20, .rows = 5, }); @@ -6062,11 +6807,12 @@ test "Page VT background color on trailing blank cells" { test "Page HTML with hyperlinks" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -6096,11 +6842,12 @@ test "Page HTML with hyperlinks" { test "Page HTML with multiple hyperlinks" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -6133,11 +6880,12 @@ test "Page HTML with multiple hyperlinks" { test "Page HTML with hyperlink escaping" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -6167,11 +6915,12 @@ test "Page HTML with hyperlink escaping" { test "Page HTML with styled hyperlink" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -6202,11 +6951,12 @@ test "Page HTML with styled hyperlink" { test "Page HTML hyperlink closes style before anchor" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -6237,11 +6987,12 @@ test "Page HTML hyperlink closes style before anchor" { test "Page HTML hyperlink point map maps closing to previous cell" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var builder: std.Io.Writer.Allocating = .init(alloc); defer builder.deinit(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); diff --git a/vendor/libghostty-vt/src/terminal/fraction.zig b/vendor/libghostty-vt/src/terminal/fraction.zig new file mode 100644 index 00000000..67134419 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/fraction.zig @@ -0,0 +1,142 @@ +//! Parsing of decimal fractions in the unit interval [0, 1]. +//! +//! Escape sequences that carry fractional parameters (XTerm `rgbi:` +//! color specifications, glyph protocol padding) only ever need plain +//! decimal values such as "0.5". Using `std.fmt.parseFloat` for these +//! pulls the full correctly-rounded float parser and its lookup tables +//! into the binary (~26KB). This implements only the restricted subset +//! we need. +const std = @import("std"); +const assert = std.debug.assert; + +/// Parse a decimal fraction in the range [0, 1] inclusive. +/// +/// Accepts an optional leading `+` or `-` sign followed by decimal +/// digits with an optional decimal point ("0.5", ".5", "1.", "1"); at +/// least one digit is required. Exponent and hexadecimal float syntax +/// are not supported. Returns null for invalid syntax and for any value +/// outside [0, 1] (so negative values other than -0 are rejected). +/// +/// Results are correctly rounded for inputs of up to 15 fractional +/// digits; further digits are validated but ignored, which can move the +/// result by less than 1e-15. +pub fn parse(value: []const u8) ?f64 { + var v = value; + var negative = false; + if (v.len > 0) switch (v[0]) { + '+' => v = v[1..], + '-' => { + negative = true; + v = v[1..]; + }, + else => {}, + }; + + var i: usize = 0; + const int_part: f64 = int_part: { + var int_part: f64 = 0; + while (i < v.len and v[i] != '.') : (i += 1) { + const d = v[i]; + if (d < '0' or d > '9') return null; + int_part = int_part * 10 + @as(f64, @floatFromInt(d - '0')); + } + break :int_part int_part; + }; + + var digits: usize = i; + var frac: u64 = 0; + var scale: u64 = 1; + if (i < v.len) { + assert(v[i] == '.'); + i += 1; + while (i < v.len) : (i += 1) { + const d = v[i]; + if (d < '0' or d > '9') return null; + // Stop accumulating after 15 digits so that frac and scale + // both stay exactly representable in f64 (and cannot + // overflow), making the division below round only once. + if (scale < 1_000_000_000_000_000) { + frac = frac * 10 + (d - '0'); + scale *= 10; + } + digits += 1; + } + } + if (digits == 0) return null; + + // frac and scale are both exact in f64 (below 2^53 and a power of + // ten no greater than 1e15), so this division rounds only once. + const magnitude = int_part + + @as(f64, @floatFromInt(frac)) / @as(f64, @floatFromInt(scale)); + const result = if (negative) -magnitude else magnitude; + + // Range check. Written so that -0 passes and any out-of-range or + // non-finite value fails. + if (!(result >= 0 and result <= 1)) return null; + return result; +} + +test parse { + const testing = std.testing; + + // Plain values + try testing.expectEqual(0, parse("0").?); + try testing.expectEqual(1, parse("1").?); + try testing.expectEqual(0.5, parse("0.5").?); + try testing.expectEqual(0.25, parse("0.25").?); + try testing.expectEqual(1, parse("1.0").?); + try testing.expectEqual(1, parse("1.000000").?); + try testing.expectEqual(0, parse("0.0").?); + + // Partial forms + try testing.expectEqual(0.5, parse(".5").?); + try testing.expectEqual(1, parse("1.").?); + try testing.expectEqual(0, parse("00.00").?); + + // Signs + try testing.expectEqual(0.5, parse("+0.5").?); + try testing.expectEqual(0, parse("-0").?); + try testing.expectEqual(0, parse("-0.000").?); + try testing.expectEqual(null, parse("-0.5")); + try testing.expectEqual(null, parse("-1")); + + // Matches std.fmt.parseFloat rounding for typical inputs. + try testing.expectEqual( + std.fmt.parseFloat(f64, "0.3") catch unreachable, + parse("0.3").?, + ); + try testing.expectEqual( + std.fmt.parseFloat(f64, "0.123456789012345") catch unreachable, + parse("0.123456789012345").?, + ); + + // Digits past the 15th are ignored: long fractions stay finite and + // equal their 15-digit truncation. + try testing.expectEqual( + parse("0.333333333333333").?, + parse("0." ++ "3" ** 400).?, + ); + + // Out of range + try testing.expectEqual(null, parse("1.0000001")); + try testing.expectEqual(null, parse("2")); + try testing.expectEqual(null, parse("255")); + try testing.expectEqual(null, parse("1" ** 400)); + + // Invalid syntax + try testing.expectEqual(null, parse("")); + try testing.expectEqual(null, parse(".")); + try testing.expectEqual(null, parse("+")); + try testing.expectEqual(null, parse("-")); + try testing.expectEqual(null, parse("+.")); + try testing.expectEqual(null, parse("abc")); + try testing.expectEqual(null, parse("0.5x")); + try testing.expectEqual(null, parse("0x0.8")); + try testing.expectEqual(null, parse("0..5")); + try testing.expectEqual(null, parse("0.5.5")); + try testing.expectEqual(null, parse(" 0.5")); + try testing.expectEqual(null, parse("0.5 ")); + try testing.expectEqual(null, parse("1e-1")); + try testing.expectEqual(null, parse("nan")); + try testing.expectEqual(null, parse("inf")); +} diff --git a/vendor/libghostty-vt/src/terminal/hash_map.zig b/vendor/libghostty-vt/src/terminal/hash_map.zig index d8feddcd..050bea63 100644 --- a/vendor/libghostty-vt/src/terminal/hash_map.zig +++ b/vendor/libghostty-vt/src/terminal/hash_map.zig @@ -40,6 +40,7 @@ //! by any removal. const std = @import("std"); +const builtin = @import("builtin"); const assert = @import("../quirks.zig").inlineAssert; const mem = std.mem; const Allocator = mem.Allocator; @@ -70,8 +71,47 @@ fn AutoHashMapUnmanaged( fn AutoContext(comptime K: type) type { return struct { - pub const hash = std.hash_map.getAutoHashFn(K, @This()); pub const eql = std.hash_map.getAutoEqlFn(K, @This()); + + pub fn hash(_: @This(), key: K) u64 { + if (comptime std.meta.hasUniqueRepresentation(K)) { + // On wasm32, Wyhash's 64x64 -> 128 multiplies lower to + // __multi3 libcalls (wasm has no widening multiply + // instruction), which makes the hash itself the + // dominant cost of small-key map operations. + // + // Keys here fit in a u64, so use a splitmix64-style finalizer + // instead: two native i64.mul with full avalanche in + // both the low bits (slot index) and high bits + // (metadata fingerprint). + if (comptime builtin.cpu.arch.isWasm() and @sizeOf(K) <= 8) { + var x: u64 = 0; + @memcpy( + std.mem.asBytes(&x)[0..@sizeOf(K)], + std.mem.asBytes(&key), + ); + x ^= x >> 33; + x *%= 0xff51afd7ed558ccd; + x ^= x >> 33; + x *%= 0xc4ceb9fe1a85ec53; + x ^= x >> 33; + return x; + } + + // LLVM 21 (Zig 0.16) failed to inline this which resulted + // in a measurable almost 2x slowdown on our hyperlink map + // benchmark. So, force it. + return @call( + .always_inline, + std.hash.Wyhash.hash, + .{ 0, std.mem.asBytes(&key) }, + ); + } + + var hasher = std.hash.Wyhash.init(0); + std.hash.autoHash(&hasher, key); + return hasher.final(); + } }; } @@ -99,8 +139,18 @@ pub fn OffsetHashMap( /// This is the alignment that the base pointer must have. pub const base_align = Unmanaged.base_align; + /// The slot metadata in the backing memory. The map's size counter + /// sits immediately before it (see `Unmanaged.Header`). metadata: Offset(Unmanaged.Metadata) = .{}, + /// The key and value arrays in the backing memory. + keys: Offset(K) = .{}, + values: Offset(V) = .{}, + + /// The number of slots. This never changes after init, so it is + /// kept here rather than in the backing memory. + capacity: Unmanaged.Size = 0, + /// Returns the total size of the backing memory required for a /// HashMap with the given capacity. The base ptr must also be /// aligned to base_align. @@ -111,19 +161,35 @@ pub fn OffsetHashMap( /// Initialize a new HashMap with the given capacity and backing /// memory. The backing memory must be aligned to base_align. pub fn init(buf: OffsetBuf, l: Layout) Self { + const self = initAssumeZeroed(buf, l); + var m = self.map(buf); + m.clearRetainingCapacity(); + return self; + } + + /// Like `init`, but for backing memory that the caller guarantees + /// is already zero-filled (e.g. fresh OS pages). This writes + /// nothing to the backing memory: all-zero slot metadata means + /// every slot is free and a zero size counter means empty, so the + /// OS pages behind the map stay untouched until the first insert. + pub fn initAssumeZeroed(buf: OffsetBuf, l: Layout) Self { assert(base_align.check(@intFromPtr(buf.start()))); - - const m = Unmanaged.init(buf, l); - return .{ .metadata = getOffset( - Unmanaged.Metadata, - buf, - @ptrCast(m.metadata.?), - ) }; + return .{ + .metadata = buf.member(Unmanaged.Metadata, l.metadata_start), + .keys = buf.member(K, l.keys_start), + .values = buf.member(V, l.vals_start), + .capacity = l.capacity, + }; } /// Returns the pointer-based map from a base pointer. pub fn map(self: Self, base: anytype) Unmanaged { - return .{ .metadata = self.metadata.ptr(base) }; + return .{ + .metadata = self.metadata.ptr(base), + .keys = self.keys.ptr(base), + .values = self.values.ptr(base), + .cap = self.capacity, + }; } }; } @@ -156,15 +222,21 @@ fn HashMapUnmanaged( val_align, )); - // This is actually a midway pointer to the single buffer containing - // a `Header` field, the `Metadata`s and `Entry`s. - // At `-@sizeOf(Header)` is the Header field. - // At `sizeOf(Metadata) * capacity + offset`, which is pointed to by - // self.header().entries, is the array of entries. - // This means that the hashmap only holds one live allocation, to - // reduce memory fragmentation and struct size. - /// Pointer to the metadata. - metadata: ?[*]Metadata = null, + // The backing buffer holds a `Header` (the size counter) followed + // by the `Metadata`s, then the keys and values arrays. Everything + // the map needs that does not change after init, the capacity and + // the entry pointers, lives in this struct instead of the buffer, + // so that a zero-filled buffer is a valid empty map and init never + // has to write to it. + /// Pointer to the slot metadata. The header sits right before it. + metadata: [*]Metadata, + + /// The key and value arrays. + keys: [*]K, + values: [*]V, + + /// The number of slots. Always zero or a power of two. + cap: Size, // This hashmap is specially designed for sizes that fit in a u32. pub const Size = u32; @@ -183,11 +255,10 @@ fn HashMapUnmanaged( value: V, }; + /// The part of the map's state that changes after init. It lives + /// in the backing buffer so that the map can be handed around by + /// value; its zero value is the empty map. const Header = struct { - /// The keys/values offset are relative to the metadata - values: Offset(V), - keys: Offset(K), - capacity: Size, size: Size, }; @@ -247,20 +318,20 @@ fn HashMapUnmanaged( index: Size = 0, pub fn next(it: *Iterator) ?Entry { - assert(it.index <= it.hm.capacity()); + assert(it.index <= it.hm.cap); if (it.hm.header().size == 0) return null; - const cap = it.hm.capacity(); - const end = it.hm.metadata.? + cap; - var metadata = it.hm.metadata.? + it.index; + const cap = it.hm.cap; + const end = it.hm.metadata + cap; + var metadata = it.hm.metadata + it.index; while (metadata != end) : ({ metadata += 1; it.index += 1; }) { if (metadata[0].isUsed()) { - const key = &it.hm.keys()[it.index]; - const value = &it.hm.values()[it.index]; + const key = &it.hm.keys[it.index]; + const value = &it.hm.values[it.index]; it.index += 1; return Entry{ .key_ptr = key, .value_ptr = value }; } @@ -304,24 +375,26 @@ fn HashMapUnmanaged( /// Initialize a hash map with a given capacity and a buffer. The /// buffer must fit within the size defined by `layoutForCapacity`. pub fn init(buf: OffsetBuf, layout: Layout) Self { - assert(base_align.check(@intFromPtr(buf.start()))); - - // Get all our main pointers - const metadata_buf = buf.rebase(@sizeOf(Header)); - const metadata_ptr: [*]Metadata = @ptrCast(metadata_buf.start()); - - // Build our map - var map: Self = .{ .metadata = metadata_ptr }; - const hdr = map.header(); - hdr.capacity = layout.capacity; - hdr.size = 0; - if (@sizeOf([*]K) != 0) hdr.keys = metadata_buf.member(K, layout.keys_start); - if (@sizeOf([*]V) != 0) hdr.values = metadata_buf.member(V, layout.vals_start); - map.initMetadatas(); - + var map = initAssumeZeroed(buf, layout); + map.clearRetainingCapacity(); return map; } + /// Like `init`, but for a buffer that the caller guarantees is + /// already zero-filled. Nothing is written: an all-zero metadata + /// byte is a free slot (see `Metadata.isFree`) and a zero header + /// is an empty map. Behavior is undefined if the header and + /// metadata region is not zero. + pub fn initAssumeZeroed(buf: OffsetBuf, layout: Layout) Self { + assert(base_align.check(@intFromPtr(buf.start()))); + return .{ + .metadata = @ptrCast(buf.start() + layout.metadata_start), + .keys = buf.member(K, layout.keys_start).ptr(buf), + .values = buf.member(V, layout.vals_start).ptr(buf), + .cap = layout.capacity, + }; + } + pub fn ensureTotalCapacity(self: *Self, new_size: Size) Allocator.Error!void { if (new_size > self.header().size) { try self.checkCapacity(new_size - self.header().size); @@ -333,10 +406,8 @@ fn HashMapUnmanaged( } pub fn clearRetainingCapacity(self: *Self) void { - if (self.metadata) |_| { - self.initMetadatas(); - self.header().size = 0; - } + self.initMetadatas(); + self.header().size = 0; } pub fn count(self: *const Self) Size { @@ -344,28 +415,18 @@ fn HashMapUnmanaged( } fn header(self: *const Self) *Header { - return @ptrCast(@as([*]Header, @ptrCast(@alignCast(self.metadata.?))) - 1); - } - - fn keys(self: *const Self) [*]K { - return self.header().keys.ptr(self.metadata.?); - } - - fn values(self: *const Self) [*]V { - return self.header().values.ptr(self.metadata.?); + return @ptrCast(@as([*]Header, @ptrCast(@alignCast(self.metadata))) - 1); } pub fn capacity(self: *const Self) Size { - if (self.metadata == null) return 0; - - return self.header().capacity; + return self.cap; } /// Maximum number of entries the map will hold. This is less than /// capacity when max_load_percentage is below 100, which keeps free /// slots in every probe chain and bounds probe lengths. pub fn maxLoad(self: *const Self) Size { - return maxLoadForCapacity(self.capacity()); + return maxLoadForCapacity(self.cap); } pub fn iterator(self: *const Self) Iterator { @@ -373,35 +434,19 @@ fn HashMapUnmanaged( } pub fn keyIterator(self: *const Self) KeyIterator { - if (self.metadata) |metadata| { - return .{ - .len = self.capacity(), - .metadata = metadata, - .items = self.keys(), - }; - } else { - return .{ - .len = 0, - .metadata = undefined, - .items = undefined, - }; - } + return .{ + .len = self.cap, + .metadata = self.metadata, + .items = self.keys, + }; } pub fn valueIterator(self: *const Self) ValueIterator { - if (self.metadata) |metadata| { - return .{ - .len = self.capacity(), - .metadata = metadata, - .items = self.values(), - }; - } else { - return .{ - .len = 0, - .metadata = undefined, - .items = undefined, - }; - } + return .{ + .len = self.cap, + .metadata = self.metadata, + .items = self.values, + }; } /// Insert an entry in the map. Assumes it is not already present. @@ -441,21 +486,21 @@ fn HashMapUnmanaged( assert(!self.containsContext(key, ctx)); // A free slot must exist for the probe below to terminate. - assert(self.header().size < self.capacity()); + assert(self.header().size < self.cap); const hash = ctx.hash(key); - const mask = self.capacity() - 1; + const mask = self.cap - 1; var idx = @as(usize, @truncate(hash & mask)); - var metadata = self.metadata.? + idx; + var metadata = self.metadata + idx; while (metadata[0].isUsed()) { idx = (idx + 1) & mask; - metadata = self.metadata.? + idx; + metadata = self.metadata + idx; } metadata[0].fill(Metadata.takeFingerprint(hash)); - self.keys()[idx] = key; - self.values()[idx] = value; + self.keys[idx] = key; + self.values[idx] = value; self.header().size += 1; } @@ -510,8 +555,8 @@ fn HashMapUnmanaged( pub fn fetchRemoveContext(self: *Self, key: K, ctx: Context) ?KV { const idx = self.getIndex(key, ctx) orelse return null; const result = KV{ - .key = self.keys()[idx], - .value = self.values()[idx], + .key = self.keys[idx], + .value = self.values[idx], }; self.removeByIndexContext(idx, ctx); return result; @@ -536,16 +581,16 @@ fn HashMapUnmanaged( if (@TypeOf(hash) != Hash) { @compileError("Context " ++ @typeName(@TypeOf(ctx)) ++ " has a generic hash function that returns the wrong type! " ++ @typeName(Hash) ++ " was expected, but found " ++ @typeName(@TypeOf(hash))); } - const mask = self.capacity() - 1; + const mask = self.cap - 1; const fingerprint = Metadata.takeFingerprint(hash); // Don't loop indefinitely when there are no free slots. - var limit = self.capacity(); + var limit = self.cap; var idx = @as(usize, @truncate(hash & mask)); - var metadata = self.metadata.? + idx; + var metadata = self.metadata + idx; while (!metadata[0].isFree() and limit != 0) { if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) { - const test_key = &self.keys()[idx]; + const test_key = &self.keys[idx]; // If you get a compile error on this line, it means that your generic eql // function is invalid for these parameters. const eql = ctx.eql(key, test_key.*); @@ -561,7 +606,7 @@ fn HashMapUnmanaged( limit -= 1; idx = (idx + 1) & mask; - metadata = self.metadata.? + idx; + metadata = self.metadata + idx; } return null; @@ -578,8 +623,8 @@ fn HashMapUnmanaged( pub fn getEntryAdapted(self: Self, key: anytype, ctx: anytype) ?Entry { if (self.getIndex(key, ctx)) |idx| { return Entry{ - .key_ptr = &self.keys()[idx], - .value_ptr = &self.values()[idx], + .key_ptr = &self.keys[idx], + .value_ptr = &self.values[idx], }; } return null; @@ -607,7 +652,7 @@ fn HashMapUnmanaged( } pub fn getKeyPtrAdapted(self: Self, key: anytype, ctx: anytype) ?*K { if (self.getIndex(key, ctx)) |idx| { - return &self.keys()[idx]; + return &self.keys[idx]; } return null; } @@ -623,7 +668,7 @@ fn HashMapUnmanaged( } pub fn getKeyAdapted(self: Self, key: anytype, ctx: anytype) ?K { if (self.getIndex(key, ctx)) |idx| { - return self.keys()[idx]; + return self.keys[idx]; } return null; } @@ -639,7 +684,7 @@ fn HashMapUnmanaged( } pub fn getPtrAdapted(self: Self, key: anytype, ctx: anytype) ?*V { if (self.getIndex(key, ctx)) |idx| { - return &self.values()[idx]; + return &self.values[idx]; } return null; } @@ -655,7 +700,7 @@ fn HashMapUnmanaged( } pub fn getAdapted(self: Self, key: anytype, ctx: anytype) ?V { if (self.getIndex(key, ctx)) |idx| { - return self.values()[idx]; + return self.values[idx]; } return null; } @@ -684,8 +729,8 @@ fn HashMapUnmanaged( // error, we could not add another. const index = self.getIndex(key, key_ctx) orelse return err; return GetOrPutResult{ - .key_ptr = &self.keys()[index], - .value_ptr = &self.values()[index], + .key_ptr = &self.keys[index], + .value_ptr = &self.values[index], .found_existing = true, }; }; @@ -713,15 +758,15 @@ fn HashMapUnmanaged( if (@TypeOf(hash) != Hash) { @compileError("Context " ++ @typeName(@TypeOf(ctx)) ++ " has a generic hash function that returns the wrong type! " ++ @typeName(Hash) ++ " was expected, but found " ++ @typeName(@TypeOf(hash))); } - const mask = self.capacity() - 1; + const mask = self.cap - 1; const fingerprint = Metadata.takeFingerprint(hash); - var limit = self.capacity(); + var limit = self.cap; var idx = @as(usize, @truncate(hash & mask)); - var metadata = self.metadata.? + idx; + var metadata = self.metadata + idx; while (!metadata[0].isFree() and limit != 0) { if (metadata[0].isUsed() and metadata[0].fingerprint == fingerprint) { - const test_key = &self.keys()[idx]; + const test_key = &self.keys[idx]; // If you get a compile error on this line, it means that your generic eql // function is invalid for these parameters. const eql = ctx.eql(key, test_key.*); @@ -733,7 +778,7 @@ fn HashMapUnmanaged( if (eql) { return GetOrPutResult{ .key_ptr = test_key, - .value_ptr = &self.values()[idx], + .value_ptr = &self.values[idx], .found_existing = true, }; } @@ -741,7 +786,7 @@ fn HashMapUnmanaged( limit -= 1; idx = (idx + 1) & mask; - metadata = self.metadata.? + idx; + metadata = self.metadata + idx; } // The caller guaranteed capacity for at least one new entry, so @@ -751,8 +796,8 @@ fn HashMapUnmanaged( assert(metadata[0].isFree()); metadata[0].fill(fingerprint); - const new_key = &self.keys()[idx]; - const new_value = &self.values()[idx]; + const new_key = &self.keys[idx]; + const new_value = &self.values[idx]; new_key.* = undefined; new_value.* = undefined; self.header().size += 1; @@ -799,10 +844,10 @@ fn HashMapUnmanaged( /// hole further along the cluster, until the cluster ends at a free /// slot. fn removeByIndexContext(self: *Self, idx: usize, ctx: Context) void { - const mask: usize = self.capacity() - 1; - const metadata = self.metadata.?; - const keys_ptr = self.keys(); - const values_ptr = self.values(); + const mask: usize = self.cap - 1; + const metadata = self.metadata; + const keys_ptr = self.keys; + const values_ptr = self.values; // A completely full table has no free slot to terminate the // scan, so bound it to one full cycle. That is sufficient: the @@ -810,7 +855,7 @@ fn HashMapUnmanaged( // visited, so each entry needs to be considered exactly once. var hole = idx; var j = idx; - var limit = self.capacity() - 1; + var limit = self.cap - 1; while (limit != 0) : (limit -= 1) { j = (j + 1) & mask; if (metadata[j].isFree()) break; @@ -864,7 +909,7 @@ fn HashMapUnmanaged( // map, which is assumed to exist as key_ptr must be valid. This // item must be at index 0. const idx = if (@sizeOf(K) > 0) - (@intFromPtr(key_ptr) - @intFromPtr(self.keys())) / @sizeOf(K) + (@intFromPtr(key_ptr) - @intFromPtr(self.keys)) / @sizeOf(K) else 0; @@ -872,7 +917,7 @@ fn HashMapUnmanaged( } fn initMetadatas(self: *Self) void { - @memset(@as([*]u8, @ptrCast(self.metadata.?))[0 .. @sizeOf(Metadata) * self.capacity()], 0); + @memset(@as([*]u8, @ptrCast(self.metadata))[0 .. @sizeOf(Metadata) * self.cap], 0); } /// Returns an error if the map cannot hold `new_count` more entries. @@ -892,11 +937,16 @@ fn HashMapUnmanaged( } /// The memory layout for the underlying buffer for a given capacity. + /// All offsets are from the start of the buffer. const Layout = struct { /// The total size of the buffer required. The buffer is expected /// to be aligned to `base_align`. total_size: usize, + /// The offset to the start of the slot metadata. The header + /// occupies the bytes before it. + metadata_start: usize, + /// The offset to the start of the keys data. keys_start: usize, @@ -918,9 +968,9 @@ fn HashMapUnmanaged( // See: https://github.com/ziglang/zig/pull/19048 const cap: usize = new_capacity; - // Pack our metadata, keys, and values. + // Pack our header, metadata, keys, and values. const meta_start = @sizeOf(Header); - const meta_end = @sizeOf(Header) + cap * @sizeOf(Metadata); + const meta_end = meta_start + cap * @sizeOf(Metadata); const keys_start = std.mem.alignForward(usize, meta_end, key_align); const keys_end = keys_start + cap * @sizeOf(K); const vals_start = std.mem.alignForward(usize, keys_end, val_align); @@ -934,16 +984,11 @@ fn HashMapUnmanaged( base_align.toByteUnits(), ); - // The offsets we actually store in the map are from the - // metadata pointer so that we can use self.metadata as - // the base. - const keys_offset = keys_start - meta_start; - const vals_offset = vals_start - meta_start; - return .{ .total_size = total_size, - .keys_start = keys_offset, - .vals_start = vals_offset, + .metadata_start = meta_start, + .keys_start = keys_start, + .vals_start = vals_start, .capacity = new_capacity, }; } @@ -993,13 +1038,13 @@ fn expectCanonical(map: anytype, ctx: anytype) !void { const mask = cap - 1; var used: usize = 0; for (0..cap) |idx| { - const metadata = map.metadata.?[idx]; + const metadata = map.metadata[idx]; if (!metadata.isUsed()) continue; used += 1; - var probe: usize = @truncate(ctx.hash(map.keys()[idx]) & mask); + var probe: usize = @truncate(ctx.hash(map.keys[idx]) & mask); while (probe != idx) : (probe = (probe + 1) & mask) { - try expect(map.metadata.?[probe].isUsed()); + try expect(map.metadata[probe].isUsed()); } } try expectEqual(map.count(), used); diff --git a/vendor/libghostty-vt/src/terminal/hyperlink.zig b/vendor/libghostty-vt/src/terminal/hyperlink.zig index 35a16a2a..68f914f8 100644 --- a/vendor/libghostty-vt/src/terminal/hyperlink.zig +++ b/vendor/libghostty-vt/src/terminal/hyperlink.zig @@ -194,12 +194,12 @@ pub const PageEntry = struct { const alloc = &page.string_alloc; switch (self.id) { .implicit => {}, - .explicit => |v| alloc.free( + .explicit => |v| if (v.len > 0) alloc.free( page.memory, v.slice(page.memory), ), } - alloc.free( + if (self.uri.len > 0) alloc.free( page.memory, self.uri.slice(page.memory), ); diff --git a/vendor/libghostty-vt/src/terminal/kitty.zig b/vendor/libghostty-vt/src/terminal/kitty.zig index 0868a771..bbef8d7b 100644 --- a/vendor/libghostty-vt/src/terminal/kitty.zig +++ b/vendor/libghostty-vt/src/terminal/kitty.zig @@ -3,7 +3,9 @@ const build_options = @import("terminal_options"); const key = @import("kitty/key.zig"); +pub const clipboard = @import("kitty/clipboard.zig"); pub const color = @import("kitty/color.zig"); +pub const dnd = @import("kitty/dnd.zig"); pub const graphics = if (build_options.kitty_graphics) @import("kitty/graphics.zig") else struct {}; pub const KeyFlags = key.Flags; diff --git a/vendor/libghostty-vt/src/terminal/kitty/clipboard.zig b/vendor/libghostty-vt/src/terminal/kitty/clipboard.zig new file mode 100644 index 00000000..32cf8b90 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/clipboard.zig @@ -0,0 +1,82 @@ +//! Kitty clipboard protocol (OSC 5522). +//! +//! This implements the protocol semantics on top of the raw OSC capture: +//! src/terminal/osc/parsers/kitty_clipboard_protocol.zig: +//! +//! The behavior here is modeled on the kitty reference implementation +//! (kitty/clipboard.py) rather than only the prose spec, since the two +//! disagree in places. Notable reference behaviors we reproduce: +//! +//! * Malformed metadata (any record without '=', including an empty +//! metadata section) and an unknown or missing `type` silently drop +//! the request with no response. +//! * All base64 (payloads and the `mime`, `name`, and `pw` metadata +//! values) is strict RFC 4648 per the spec's "Encoding of payloads" +//! section: characters outside the standard alphabet (including +//! whitespace) and incorrect padding are rejected, never silently +//! skipped. An invalid value on `wdata` or `walias` aborts an +//! in-flight write with EINVAL; an invalid `read` is dropped with +//! no response since reads have no error status. +//! * Decoded metadata and MIME-list payloads must be valid UTF-8. An +//! invalid value on `wdata` or `walias`, or a `walias` without a +//! target MIME type, aborts an in-flight write with EINVAL. +//! * The `wdata` payloads for one MIME type form a single base64 +//! stream split at arbitrary packet boundaries; only the +//! concatenation must be correctly padded. Like kitty (which +//! resets its streaming decoder on EOF), a packet ending exactly +//! at terminal padding restarts the stream, so independently +//! encoded chunks also work. +//! * `mime`, `name`, and `pw` metadata values are base64-encoded UTF-8; +//! everything else is verbatim. Unknown keys are ignored. +//! * `id` is sanitized by stripping characters outside [a-zA-Z0-9-_+.] +//! and truncating to 512 bytes, then echoed verbatim in every +//! response packet (omitted when empty). +//! * A `type=write` silently replaces any in-flight transaction. A +//! commit (`type=wdata` without a MIME type) with no in-flight +//! transaction is silently ignored. +//! * Responses never send a payload section for an empty payload, +//! except the targets ('.') listing DATA packet which is always sent. +//! +//! Specification: https://sw.kovidgoyal.net/kitty/clipboard/ + +const oscpkg = @import("../osc.zig"); +const protocol = @import("../osc/parsers/kitty_clipboard_protocol.zig"); +const command = @import("clipboard_command.zig"); +const write = @import("clipboard_write.zig"); +const response = @import("clipboard_response.zig"); +const grants = @import("clipboard_grants.zig"); + +pub const OSC = protocol.OSC; +pub const Operation = protocol.Operation; +pub const Status = protocol.Status; +pub const Terminator = oscpkg.Terminator; + +pub const Metadata = command.Metadata; +pub const Payload = command.Payload; +pub const readPromptExempt = command.readPromptExempt; +pub const max_id_len = command.max_id_len; +pub const max_pw_len = command.max_pw_len; +pub const max_mime_len = command.max_mime_len; +pub const max_name_len = command.max_name_len; + +pub const Content = write.Content; +pub const WriteState = write.WriteState; +pub const max_write_size = write.max_write_size; +pub const max_write_mimes = write.max_write_mimes; +pub const max_write_aliases = write.max_write_aliases; + +pub const Response = response.Response; +pub const ReadSuccess = response.ReadSuccess; +pub const PasteEvent = response.PasteEvent; +pub const read_chunk_size = response.read_chunk_size; +pub const max_read_mimes = response.max_read_mimes; +pub const max_listing_mimes = response.max_listing_mimes; +pub const targets_mime = response.targets_mime; + +pub const Grants = grants.Grants; +pub const otp_len = grants.otp_len; +pub const generateOtp = grants.generateOtp; + +test { + @import("std").testing.refAllDecls(@This()); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/clipboard_command.zig b/vendor/libghostty-vt/src/terminal/kitty/clipboard_command.zig new file mode 100644 index 00000000..67bc26f0 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/clipboard_command.zig @@ -0,0 +1,502 @@ +//! Kitty clipboard protocol (OSC 5522) request decoding. +//! +//! OSC 5522 format: `5522;metadata;payload` +//! +//! Per the spec: "metadata is a colon separated list of key-value +//! pairs and payload is base64 encoded data." +//! +//! This contains the logic for parsing this. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const simd = @import("../../simd/main.zig"); +const clipboard = @import("../clipboard.zig"); +const protocol = @import("../osc/parsers/kitty_clipboard_protocol.zig"); + +const Operation = protocol.Operation; + +/// Maximum id length, nothing specified but this is the limit used +/// in Kitty's source so we'll match it. +pub const max_id_len = 512; + +/// Maximum decoded password length. Kitty has no limit. Passwords are +/// UUID-sized in practice so anything longer simply never matches a +/// stored grant anyway. +pub const max_pw_len = 128; + +/// Maximum decoded MIME type length. Kitty has no limit but real MIME +/// types are tiny; anything longer drops the packet. +pub const max_mime_len = 256; + +/// Maximum decoded name length. Kitty has no limit but names are shown +/// in permission prompts so anything longer drops the packet. +pub const max_name_len = 256; + +/// The decoded, validated metadata of one OSC 5522 sequence. +/// +/// All slice values are allocated from the allocator given to parse and +/// are sized to their contents. Callers are expected to pass an arena +/// scoped to handling the packet. There is no deinit. +pub const Metadata = struct { + op: Operation, + + /// The clipboard this operation targets. Per the spec: "To read + /// from the primary selection instead of the clipboard, add the + /// key `loc=primary` to the metadata section." Any other value + /// means the clipboard, so only standard and primary are possible + /// here. + loc: clipboard.Location = .standard, + + /// Sanitized id: invalid characters stripped, truncated to + /// max_id_len. Empty means no id. + id: []const u8 = "", + + /// Decoded mime metadata value. Empty means absent; kitty treats an + /// empty mime the same as a missing one everywhere it matters (a + /// wdata packet with either commits the transaction). + mime: []const u8 = "", + + /// Decoded password. Empty means absent. Per the spec: + /// "Specifying a password without a human friendly name is + /// equivalent to not specifying a password and the terminal must + /// treat the request as though it had no password." + pw: []const u8 = "", + + /// Decoded human friendly name of the requesting program, shown in + /// permission prompts. Empty means absent. Its presence opts into + /// password grants. + name: []const u8 = "", + + /// Parse the metadata field. The raw value is expected to be exactly + /// the metadata (prefix and payload and separators stripped out). + /// + /// A null result means the packet should be silently dropped. An + /// InvalidValue error means a decoded textual value was not valid + /// UTF-8 or exceeded a local safety limit. Callers use the raw + /// operation to decide whether that invalid value aborts an in-flight + /// write transaction. + pub fn parse( + alloc: Allocator, + raw: []const u8, + ) error{ OutOfMemory, InvalidValue }!?Metadata { + const fields = Raw.parse(raw) orelse return null; + var result: Metadata = .{ + .op = Operation.init(fields.op orelse return null) orelse return null, + .loc = if (std.mem.eql(u8, fields.loc, "primary")) + .primary + else + .standard, + .id = try sanitizeId(alloc, fields.id), + }; + + result.mime = decodeValue( + alloc, + fields.mime, + max_mime_len, + ) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.Overflow, + error.InvalidBase64, + error.InvalidUtf8, + => return error.InvalidValue, + }; + result.pw = decodeValue( + alloc, + fields.pw, + max_pw_len, + ) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.InvalidBase64, error.InvalidUtf8 => return error.InvalidValue, + // An over-long password behaves as if none was given: it can + // never match a stored grant. + error.Overflow => "", + }; + result.name = decodeValue( + alloc, + fields.name, + max_name_len, + ) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.Overflow, + error.InvalidBase64, + error.InvalidUtf8, + => return error.InvalidValue, + }; + return result; + } + + /// Return the last recognized operation from syntactically valid raw + /// metadata. This intentionally does not decode any values, so callers + /// can still classify an InvalidValue parse error. + pub fn operation(raw: []const u8) ?Operation { + const fields = Raw.parse(raw) orelse return null; + return Operation.init(fields.op orelse return null); + } + + const Raw = struct { + op: ?[]const u8 = null, + loc: []const u8 = "", + id: []const u8 = "", + mime: []const u8 = "", + pw: []const u8 = "", + name: []const u8 = "", + + fn parse(raw: []const u8) ?Raw { + var result: Raw = .{}; + + // This visits every record even though an empty raw string + // yields one empty record. Validating the complete structure + // before decoding also ensures the last duplicate value wins. + var it = std.mem.splitScalar(u8, raw, ':'); + while (it.next()) |record| { + const eql_idx = std.mem.indexOfScalar(u8, record, '=') orelse return null; + const key = record[0..eql_idx]; + const value = record[eql_idx + 1 ..]; + + if (std.mem.eql(u8, key, "type")) { + result.op = value; + } else if (std.mem.eql(u8, key, "loc")) { + result.loc = value; + } else if (std.mem.eql(u8, key, "id")) { + result.id = value; + } else if (std.mem.eql(u8, key, "mime")) { + result.mime = value; + } else if (std.mem.eql(u8, key, "pw")) { + result.pw = value; + } else if (std.mem.eql(u8, key, "name")) { + result.name = value; + } + // Unknown keys are ignored. + } + + return result; + } + }; + + /// Sanitize the ID according to the spec: + /// + /// Valid ids must include only characters from the set: [a-zA-Z0-9-_+.]. + /// Any other characters must be stripped out from the id by the terminal + /// emulator before retransmitting it. + fn sanitizeId(alloc: Allocator, value: []const u8) Allocator.Error![]const u8 { + var list: std.ArrayListUnmanaged(u8) = .empty; + defer list.deinit(alloc); + for (value) |c| { + switch (c) { + 'a'...'z', 'A'...'Z', '0'...'9', '-', '_', '+', '.' => {}, + else => continue, + } + if (list.items.len >= max_id_len) break; + try list.append(alloc, c); + } + return try list.toOwnedSlice(alloc); + } + + /// Base64-decode a metadata value. Per the spec these values "are + /// UTF-8 strings that are base64 encoded", so the decoded result + /// must be valid UTF-8, and the encoding is strict RFC 4648 with + /// required padding per the spec's "Encoding of payloads" section. + fn decodeValue(alloc: Allocator, value: []const u8, max_len: usize) error{ + OutOfMemory, + Overflow, + InvalidBase64, + InvalidUtf8, + }![]const u8 { + const Encoder = std.base64.standard.Encoder; + + // Avoid hostile large payloads. + if (value.len > Encoder.calcSize(max_len)) return error.Overflow; + + // Decode + const buf = try alloc.alloc(u8, simd.base64.maxLen(value)); + errdefer alloc.free(buf); + const decoded = simd.base64.decodeStrict( + value, + buf, + .required, + ) catch return error.InvalidBase64; + + // Must be valid UTF-8 + if (!std.unicode.utf8ValidateSlice(decoded)) return error.InvalidUtf8; + if (decoded.len > max_len) return error.Overflow; + return decoded; + } +}; + +/// A decoded base64 payload of one OSC 5522 sequence, e.g. the MIME +/// type list of a read request or the alias list of a walias packet. +/// The data slice aliases the allocated buf. +pub const Payload = struct { + buf: []u8, + data: []const u8, + + /// Decode a base64 payload into freshly allocated memory. The + /// encoding is strict RFC 4648 with required padding per the + /// spec's "Encoding of payloads" section; how an invalid payload + /// is reported (or not) depends on the packet type. + pub fn init( + alloc: Allocator, + payload: []const u8, + ) error{ OutOfMemory, Invalid }!Payload { + const buf = try alloc.alloc(u8, simd.base64.maxLen(payload)); + errdefer alloc.free(buf); + const data = simd.base64.decodeStrict( + payload, + buf, + .required, + ) catch return error.Invalid; + return .{ .buf = buf, .data = data }; + } + + pub fn deinit(self: *const Payload, alloc: Allocator) void { + alloc.free(self.buf); + } + + pub fn isValidUtf8(self: *const Payload) bool { + return std.unicode.utf8ValidateSlice(self.data); + } + + /// Iterate the whitespace-separated MIME types of the payload. + /// Matches Python str.split() used by kitty. + pub fn mimeIterator(self: *const Payload) std.mem.TokenIterator(u8, .any) { + return std.mem.tokenizeAny( + u8, + self.data, + &std.ascii.whitespace, + ); + } +}; + +/// Whether a read request with this many data MIME types (the targets +/// type '.' excluded) is exempt from the user permission prompt. +pub fn readPromptExempt(data_mimes: usize) bool { + return data_mimes == 0; +} + +test "metadata: empty is dropped" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + try testing.expect((try Metadata.parse(arena.allocator(), "")) == null); +} + +test "metadata: record without = is dropped" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + try testing.expect((try Metadata.parse(arena.allocator(), "type=read:bare")) == null); + try testing.expect((try Metadata.parse(arena.allocator(), "bare:type=read")) == null); +} + +test "metadata: missing or unknown type is dropped" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + try testing.expect((try Metadata.parse(arena.allocator(), "loc=primary")) == null); + try testing.expect((try Metadata.parse(arena.allocator(), "type=bobr")) == null); + try testing.expect((try Metadata.parse(arena.allocator(), "type=")) == null); +} + +test "metadata: duplicate keys keep the last occurrence" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + try testing.expectEqual( + Operation.read, + (try Metadata.parse(arena.allocator(), "type=bobr:type=read")).?.op, + ); + try testing.expect((try Metadata.parse(arena.allocator(), "type=read:type=bobr")) == null); +} + +test "metadata: basic read" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const meta = (try Metadata.parse(arena.allocator(), "type=read")).?; + try testing.expectEqual(Operation.read, meta.op); + try testing.expect(meta.loc == .standard); + try testing.expectEqual(@as(usize, 0), meta.id.len); +} + +test "metadata: unknown keys ignored" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const meta = (try Metadata.parse(arena.allocator(), "type=read:bobr=kurwa")).?; + try testing.expectEqual(Operation.read, meta.op); +} + +test "metadata: loc" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + try testing.expect((try Metadata.parse(arena.allocator(), "type=read:loc=primary")).?.loc == .primary); + // Anything other than "primary" means the clipboard; it is not an + // error. + try testing.expect((try Metadata.parse(arena.allocator(), "type=read:loc=bobr")).?.loc == .standard); +} + +test "metadata: id sanitized" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + { + const meta = (try Metadata.parse(arena.allocator(), "type=read:id=abc-123_x.Y+z")).?; + try testing.expectEqualStrings("abc-123_x.Y+z", meta.id); + } + { + // Invalid characters are stripped, not rejected. + const meta = (try Metadata.parse(arena.allocator(), "type=read:id=*4 2*")).?; + try testing.expectEqualStrings("42", meta.id); + } +} + +test "metadata: id truncated to max" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const raw = "type=read:id=" ++ "a" ** (max_id_len + 100); + const meta = (try Metadata.parse(arena.allocator(), raw)).?; + try testing.expectEqual(@as(usize, max_id_len), meta.id.len); +} + +test "metadata: mime decoded" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + // "text/plain" + const meta = (try Metadata.parse(arena.allocator(), "type=wdata:mime=dGV4dC9wbGFpbg==")).?; + try testing.expectEqualStrings("text/plain", meta.mime); +} + +test "metadata: invalid mime base64 reported" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + + // Invalid base64 in a metadata value aborts an in-flight write + // per the spec, so like invalid UTF-8 it is reported rather than + // silently dropping the packet. Unpadded or whitespace-laced + // values are invalid too: metadata values use the same strict + // encoding as payloads. + const cases = [_][]const u8{ + "type=wdata:mime=!!!", + // "text/plain" without its padding. + "type=wdata:mime=dGV4dC9wbGFpbg", + // "text/plain" with its final byte replaced by '!'. + "type=wdata:mime=dGV4dC9wbGFpbg=!", + // A newline inside otherwise valid base64. + "type=wdata:mime=dGV4dC9w\nbGFpbg==", + }; + for (cases) |case| { + try testing.expectError( + error.InvalidValue, + Metadata.parse(arena.allocator(), case), + ); + try testing.expectEqual( + Operation.wdata, + Metadata.operation(case).?, + ); + } +} + +test "metadata: invalid mime utf8 reported" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + // base64 of 0xff 0xfe + try testing.expectError( + error.InvalidValue, + Metadata.parse(arena.allocator(), "type=wdata:mime=//4="), + ); + try testing.expectEqual( + Operation.wdata, + Metadata.operation("type=wdata:mime=//4=").?, + ); +} + +test "metadata: pw and name" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + // pw="secret", name="app" + const meta = (try Metadata.parse(arena.allocator(), "type=read:pw=c2VjcmV0:name=YXBw")).?; + try testing.expectEqualStrings("secret", meta.pw); + try testing.expectEqualStrings("app", meta.name); +} + +test "metadata: over-long name reported" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const Encoder = std.base64.standard.Encoder; + const long = "n" ** (max_name_len + 1); + var buf: [Encoder.calcSize(long.len)]u8 = undefined; + const raw = try std.mem.concat(arena.allocator(), u8, &.{ + "type=read:name=", + Encoder.encode(&buf, long), + }); + try testing.expectError( + error.InvalidValue, + Metadata.parse(arena.allocator(), raw), + ); +} + +test "metadata: empty name" { + const testing = std.testing; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const meta = (try Metadata.parse(arena.allocator(), "type=read:pw=c2VjcmV0:name=")).?; + try testing.expectEqual(@as(usize, 0), meta.name.len); +} + +test "read prompt exemption: only requests without data types" { + const testing = std.testing; + + // A targets-only ('.') read parses to zero data MIME types and is + // served without a prompt; any data type, even alongside the + // targets listing, still prompts. + try testing.expect(readPromptExempt(0)); + try testing.expect(!readPromptExempt(1)); +} + +test "payload: mime iterator" { + const testing = std.testing; + // base64 of "text/plain text/html\ntext/uri-list" + const payload = try Payload.init( + testing.allocator, + "dGV4dC9wbGFpbiAgdGV4dC9odG1sCnRleHQvdXJpLWxpc3Q=", + ); + defer payload.deinit(testing.allocator); + var it = payload.mimeIterator(); + try testing.expectEqualStrings("text/plain", it.next().?); + try testing.expectEqualStrings("text/html", it.next().?); + try testing.expectEqualStrings("text/uri-list", it.next().?); + try testing.expect(it.next() == null); +} + +test "payload: invalid base64" { + const testing = std.testing; + const cases = [_][]const u8{ + "!!!", + // "text/plain" without its padding: payloads use the strict + // encoding, so missing padding is rejected. + "dGV4dC9wbGFpbg", + // A newline inside otherwise valid base64. + "dGV4dC9w\nbGFpbg==", + }; + for (cases) |case| { + try testing.expectError( + error.Invalid, + Payload.init(testing.allocator, case), + ); + } +} + +test "payload: decoded text must be valid utf8" { + const testing = std.testing; + // Valid base64 encoding of a single 0xff byte. + const payload = try Payload.init(testing.allocator, "/w=="); + defer payload.deinit(testing.allocator); + try testing.expect(!payload.isValidUtf8()); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/clipboard_grants.zig b/vendor/libghostty-vt/src/terminal/kitty/clipboard_grants.zig new file mode 100644 index 00000000..59b9bef7 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/clipboard_grants.zig @@ -0,0 +1,238 @@ +//! Kitty clipboard protocol (OSC 5522) session password grants: +//! requests carrying a granted password skip the permission prompt, and +//! paste events mint one-time passwords. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const clipboard_command = @import("clipboard_command.zig"); +const sys = @import("../sys.zig"); + +const max_pw_len = clipboard_command.max_pw_len; + +/// Session password grants, used to skip permission prompts for +/// requests carrying a known pw. Callers can choose to scope these +/// however they want, e.g. Kitty does it per window and the spec +/// doesn't demand anything. +pub const Grants = struct { + entries: std.ArrayListUnmanaged(Entry) = .empty, + + const max_entries = 32; + + pub const Direction = enum { read, write }; + + const Entry = struct { + /// Owned by the allocator passed to grant. + pw: []const u8, + read: bool = false, + write: bool = false, + one_time: bool = false, + }; + + pub fn deinit(self: *Grants, alloc: Allocator) void { + for (self.entries.items) |entry| alloc.free(entry.pw); + self.entries.deinit(alloc); + } + + /// Record a grant for pw. An existing grant for the same password + /// gains the new direction. + pub fn grant( + self: *Grants, + alloc: Allocator, + pw: []const u8, + dir: Direction, + one_time: bool, + ) Allocator.Error!void { + if (pw.len == 0 or pw.len > max_pw_len) return; + + const entry: *Entry = entry: { + if (self.findIndex(pw)) |idx| { + const entry = &self.entries.items[idx]; + entry.one_time = entry.one_time and one_time; + break :entry entry; + } + + // Evict the oldest grant once full. + if (self.entries.items.len >= max_entries) { + const oldest = self.entries.orderedRemove(0); + alloc.free(oldest.pw); + } + + const owned = try alloc.dupe(u8, pw); + errdefer alloc.free(owned); + try self.entries.append(alloc, .{ + .pw = owned, + .one_time = one_time, + }); + break :entry &self.entries.items[self.entries.items.len - 1]; + }; + + switch (dir) { + .read => entry.read = true, + .write => entry.write = true, + } + } + + /// Check whether pw grants the given direction. A one-time grant is + /// consumed by this check even when the direction doesn't match, + /// matching kitty's pop-on-check behavior. + pub fn use( + self: *Grants, + alloc: Allocator, + pw: []const u8, + dir: Direction, + ) bool { + if (pw.len == 0) return false; + const idx = self.findIndex(pw) orelse return false; + const entry = &self.entries.items[idx]; + const allowed = switch (dir) { + .read => entry.read, + .write => entry.write, + }; + if (entry.one_time) { + const removed = self.entries.swapRemove(idx); + alloc.free(removed.pw); + } + return allowed; + } + + fn findIndex(self: *const Grants, pw: []const u8) ?usize { + for (self.entries.items, 0..) |*entry, idx| { + if (std.mem.eql(u8, entry.pw, pw)) return idx; + } + return null; + } +}; + +/// The length of a one-time password generated for paste events. +pub const otp_len = 22; + +/// The one-time password alphabet. This matches kitty (alphanumeric +/// without easily-confused characters), but the spec doesn't demand +/// this. +pub const otp_alphabet = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; + +/// Generate a one-time password for a paste event. +/// +/// The password is a secret: a program that learns it can read the +/// clipboard without a prompt. Entropy comes from `sys.random_secure` +/// if set, otherwise from the Io; see `sys.randomSecure`. +pub fn generateOtp(io: std.Io) std.Io.RandomSecureError![otp_len]u8 { + var result: [otp_len]u8 = undefined; + var len: usize = 0; + while (len < result.len) { + var raw: [2 * otp_len]u8 = undefined; + try sys.randomSecure(io, &raw); + const limit = (std.math.maxInt(u8) + 1) / otp_alphabet.len * otp_alphabet.len; + for (raw) |byte| { + if (byte >= limit) continue; + result[len] = otp_alphabet[byte % otp_alphabet.len]; + len += 1; + if (len == result.len) break; + } + } + + return result; +} + +test "grants: basic grant and use" { + const testing = std.testing; + const alloc = testing.allocator; + + var grants: Grants = .{}; + defer grants.deinit(alloc); + try testing.expect(!grants.use(alloc, "pw1", .read)); + + try grants.grant(alloc, "pw1", .read, false); + try testing.expect(grants.use(alloc, "pw1", .read)); + // Persistent grants survive use. + try testing.expect(grants.use(alloc, "pw1", .read)); + try testing.expect(!grants.use(alloc, "pw1", .write)); +} + +test "grants: one-time consumed on check" { + const testing = std.testing; + const alloc = testing.allocator; + + var grants: Grants = .{}; + defer grants.deinit(alloc); + try grants.grant(alloc, "otp", .read, true); + try testing.expect(grants.use(alloc, "otp", .read)); + try testing.expect(!grants.use(alloc, "otp", .read)); +} + +test "grants: one-time consumed even on direction mismatch" { + const testing = std.testing; + const alloc = testing.allocator; + + var grants: Grants = .{}; + defer grants.deinit(alloc); + try grants.grant(alloc, "otp", .read, true); + try testing.expect(!grants.use(alloc, "otp", .write)); + try testing.expect(!grants.use(alloc, "otp", .read)); +} + +test "grants: directions are independent" { + const testing = std.testing; + const alloc = testing.allocator; + + var grants: Grants = .{}; + defer grants.deinit(alloc); + try grants.grant(alloc, "pw", .read, false); + try grants.grant(alloc, "pw", .write, false); + try testing.expect(grants.use(alloc, "pw", .read)); + try testing.expect(grants.use(alloc, "pw", .write)); +} + +test "grants: capacity evicts the oldest" { + const testing = std.testing; + const alloc = testing.allocator; + + var grants: Grants = .{}; + defer grants.deinit(alloc); + + var buf: [8]u8 = undefined; + for (0..Grants.max_entries + 1) |i| { + const pw = try std.fmt.bufPrint(&buf, "pw{}", .{i}); + try grants.grant(alloc, pw, .read, false); + } + + // The oldest grant was evicted; the newest survives. + try testing.expect(!grants.use(alloc, "pw0", .read)); + const newest = try std.fmt.bufPrint(&buf, "pw{}", .{Grants.max_entries}); + try testing.expect(grants.use(alloc, newest, .read)); +} + +test "generateOtp: length and alphabet with a real Io" { + const testing = std.testing; + + const otp = try generateOtp(testing.io); + try testing.expectEqual(otp_len, otp.len); + for (otp) |c| try testing.expect(std.mem.indexOfScalar(u8, otp_alphabet, c) != null); + + // Two passwords don't collide (a repeat would mean no entropy). + const other = try generateOtp(testing.io); + try testing.expect(!std.mem.eql(u8, &otp, &other)); +} + +test "generateOtp: no entropy is an error, never a weak password" { + const testing = std.testing; + try testing.expectError(error.EntropyUnavailable, generateOtp(std.Io.failing)); +} + +test "generateOtp: sys override supplies entropy without an Io source" { + const testing = std.testing; + const S = struct { + var counter: u8 = 0; + fn fill(buffer: []u8) sys.RandomSecureError!void { + for (buffer) |*b| { + b.* = counter; + counter +%= 1; + } + } + }; + sys.random_secure = &S.fill; + defer sys.random_secure = null; + + const otp = try generateOtp(std.Io.failing); + for (otp) |c| try testing.expect(std.mem.indexOfScalar(u8, otp_alphabet, c) != null); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/clipboard_response.zig b/vendor/libghostty-vt/src/terminal/kitty/clipboard_response.zig new file mode 100644 index 00000000..50bbbc67 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/clipboard_response.zig @@ -0,0 +1,469 @@ +//! Kitty clipboard protocol (OSC 5522) response encoding. + +const std = @import("std"); +const clipboard = @import("../clipboard.zig"); +const oscpkg = @import("../osc.zig"); +const protocol = @import("../osc/parsers/kitty_clipboard_protocol.zig"); + +const b64 = std.base64.standard.Encoder; +const Content = clipboard.Content; +const Operation = protocol.Operation; +const Status = protocol.Status; +const Terminator = oscpkg.Terminator; + +/// Maximum raw (pre-base64) bytes per DATA packet in read responses. +/// This is specified by the protocol. +pub const read_chunk_size = 4096; + +/// Maximum requested MIME types served by a single read request. +/// Requests beyond this simply see no DATA packets for the extras, +/// which is how the protocol communicates an unavailable type anyway. +pub const max_read_mimes = 4; + +/// The special MIME type that requests the list of available types. +pub const targets_mime = "."; + +/// Maximum MIME types reported in a paste event's targets listing. +pub const max_listing_mimes = 16; + +/// A single response packet. +pub const Response = struct { + op: Operation, + status: Status, + primary: bool = false, + id: []const u8 = "", + mime: ?[]const u8 = null, + pw: ?[]const u8 = null, + /// The raw payload; the encoder base64-encodes it. An empty payload + /// emits no payload section at all (no ';'). + payload: []const u8 = "", + terminator: Terminator = .st, + + /// Encode the response. Errors may result in partially written data + /// so it is up to callers to buffer it if they need to. + pub fn encode( + self: *const Response, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + try self.encodeMetadata(writer); + if (self.payload.len > 0) { + try writer.writeAll(";"); + try b64.encodeWriter(writer, self.payload); + } + try writer.writeAll(self.terminator.string()); + } + + /// Encode the escape prefix and metadata section only: everything + /// up to (and not including) the payload section and terminator. + fn encodeMetadata( + self: *const Response, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + // The exact order of fields here matches what Kitty does. + try writer.print("\x1b]5522;type={t}:status={t}", .{ self.op, self.status }); + if (self.primary) try writer.writeAll(":loc=primary"); + if (self.id.len > 0) try writer.print(":id={s}", .{self.id}); + if (self.mime) |mime| { + try writer.writeAll(":mime="); + try b64.encodeWriter(writer, mime); + } + if (self.pw) |pw| { + try writer.writeAll(":pw="); + try b64.encodeWriter(writer, pw); + } + } +}; + +/// Encode a full successful read response: the OK packet, the targets +/// listing if requested, DATA chunks for each served representation, +/// and the final DONE packet. This is also the shape of an unsolicited +/// paste event (list=true, pw set to the one-time password). +pub const ReadSuccess = struct { + primary: bool = false, + id: []const u8 = "", + + /// One-time password echoed in every packet. Only used for + /// terminal-initiated paste events. + pw: ?[]const u8 = null, + + /// True when the targets ('.') listing was requested. + list: bool = false, + + /// The MIME types available on the clipboard, reported by the + /// targets listing. Kitty reports these space-separated in one + /// packet with a trailing newline when non-empty. + available: []const []const u8 = &.{}, + + /// The representations to serve, in request order. Each entry's + /// data is chunked into DATA packets under its own MIME type. An + /// entry with empty data produces no packets, which is how the + /// protocol communicates an unavailable type. + contents: []const Content = &.{}, + + terminator: Terminator = .st, + + pub fn encode( + self: *const ReadSuccess, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + // Initial read response + try (Response{ + .op = .read, + .status = .OK, + .primary = self.primary, + .id = self.id, + .pw = self.pw, + .terminator = self.terminator, + }).encode(writer); + + // Listing of mimes if requested + if (self.list) try self.encodeListing(writer); + + // Encoding of each mime-type + content + for (self.contents) |content| { + var i: usize = 0; + while (i < content.data.len) { + const n = @min(content.data.len - i, read_chunk_size); + try (Response{ + .op = .read, + .status = .DATA, + .id = self.id, + .mime = content.mime, + .pw = self.pw, + .payload = content.data[i..][0..n], + .terminator = self.terminator, + }).encode(writer); + i += n; + } + } + + // Trailing done. + try (Response{ + .op = .read, + .status = .DONE, + .id = self.id, + .pw = self.pw, + .terminator = self.terminator, + }).encode(writer); + } + + /// Encode the targets ('.') listing packet. The listing gets a + /// trailing newline when non-empty. + fn encodeListing( + self: *const ReadSuccess, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + const listing: Response = .{ + .op = .read, + .status = .DATA, + .id = self.id, + .mime = targets_mime, + .pw = self.pw, + .terminator = self.terminator, + }; + try listing.encodeMetadata(writer); + if (self.available.len > 0) { + try writer.writeAll(";"); + + // Join the types into one chunk before encoding. The + // listing is a DATA packet, so it shares the pre-encoding + // chunk size bound of any other data packet; types that + // don't fit are dropped (a listing that large doesn't + // happen in practice). + var raw: [read_chunk_size]u8 = undefined; + var fixed: std.Io.Writer = .fixed(&raw); + for (self.available, 0..) |mime, i| { + const sep: usize = if (i > 0) 1 else 0; + if (fixed.end + sep + mime.len + "\n".len > raw.len) break; + if (i > 0) fixed.writeAll(" ") catch unreachable; + fixed.writeAll(mime) catch unreachable; + } + fixed.writeAll("\n") catch unreachable; + try b64.encodeWriter(writer, fixed.buffered()); + } + try writer.writeAll(self.terminator.string()); + } +}; + +/// An unsolicited Kitty paste event (mode 5522): a read response that +/// lists the clipboard's available MIME types and carries the one-time +/// password the program uses for its follow-up read. +pub const PasteEvent = struct { + /// True if the paste came from the primary selection, reported as + /// `loc=primary` on the OK packet. + primary: bool = false, + + /// The one-time password, echoed in every packet. + pw: []const u8, + + /// The MIME types available on the clipboard. + available: []const []const u8, + + terminator: Terminator = .st, + + pub fn encode( + self: *const PasteEvent, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + try (ReadSuccess{ + .primary = self.primary, + .pw = self.pw, + .list = true, + .available = self.available, + .terminator = self.terminator, + }).encode(writer); + } +}; + +test "response: basic status packet" { + const testing = std.testing; + + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (Response{ .op = .write, .status = .DONE }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=write:status=DONE\x1b\\", + writer.buffered(), + ); +} + +test "response: id echo and terminator" { + const testing = std.testing; + + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (Response{ + .op = .write, + .status = .EPERM, + .id = "42", + .terminator = .bel, + }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=write:status=EPERM:id=42\x07", + writer.buffered(), + ); +} + +test "response: key order type,status,loc,id,mime,pw and payload" { + const testing = std.testing; + + var buf: [256]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (Response{ + .op = .read, + .status = .DATA, + .primary = true, + .id = "x", + .mime = "text/plain", + .pw = "otp", + .payload = "Ghostty", + }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=DATA:loc=primary:id=x" ++ + ":mime=dGV4dC9wbGFpbg==:pw=b3Rw;R2hvc3R0eQ==\x1b\\", + writer.buffered(), + ); +} + +test "read success: empty request is OK then DONE" { + const testing = std.testing; + + var buf: [256]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (ReadSuccess{ .id = "7" }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK:id=7\x1b\\" ++ + "\x1b]5522;type=read:status=DONE:id=7\x1b\\", + writer.buffered(), + ); +} + +test "read success: targets listing with text" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (ReadSuccess{ .list = true, .available = &.{"text/plain"} }).encode(&writer); + // "." => "Lg==", "text/plain\n" => "dGV4dC9wbGFpbgo=" + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=Lg==;dGV4dC9wbGFpbgo=\x1b\\" ++ + "\x1b]5522;type=read:status=DONE\x1b\\", + writer.buffered(), + ); +} + +test "read success: targets listing joins multiple types" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (ReadSuccess{ + .list = true, + .available = &.{ "text/plain", "image/png" }, + }).encode(&writer); + // Payload is base64 of "text/plain image/png\n". + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=Lg==;dGV4dC9wbGFpbiBpbWFnZS9wbmcK\x1b\\" ++ + "\x1b]5522;type=read:status=DONE\x1b\\", + writer.buffered(), + ); +} + +test "read success: empty targets listing packet still sent" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (ReadSuccess{ .list = true }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=Lg==\x1b\\" ++ + "\x1b]5522;type=read:status=DONE\x1b\\", + writer.buffered(), + ); +} + +test "read success: data chunks under requested mime" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (ReadSuccess{ + .contents = &.{.{ .mime = "text/plain", .data = "Ghostty" }}, + }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=dGV4dC9wbGFpbg==;R2hvc3R0eQ==\x1b\\" ++ + "\x1b]5522;type=read:status=DONE\x1b\\", + writer.buffered(), + ); +} + +test "read success: each representation carries its own data" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (ReadSuccess{ + .contents = &.{ + .{ .mime = "text/plain", .data = "hello" }, + .{ .mime = "image/png", .data = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a" }, + }, + }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=dGV4dC9wbGFpbg==;aGVsbG8=\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=aW1hZ2UvcG5n;iVBORw0KGgo=\x1b\\" ++ + "\x1b]5522;type=read:status=DONE\x1b\\", + writer.buffered(), + ); +} + +test "read success: chunking at read_chunk_size" { + const testing = std.testing; + const alloc = testing.allocator; + + const data = "z" ** (read_chunk_size + 1); + var aw: std.Io.Writer.Allocating = .init(alloc); + defer aw.deinit(); + try (ReadSuccess{ + .contents = &.{.{ .mime = "text/plain", .data = data }}, + }).encode(&aw.writer); + + // OK + 2 DATA packets + DONE = 4 packets. + const count = std.mem.count(u8, aw.written(), "\x1b]5522;"); + try testing.expectEqual(@as(usize, 4), count); + + // The first chunk is exactly read_chunk_size bytes, base64 encoded + // with padding. + const Encoder = std.base64.standard.Encoder; + var chunk_buf: [Encoder.calcSize(read_chunk_size)]u8 = undefined; + const first = Encoder.encode(&chunk_buf, data[0..read_chunk_size]); + try testing.expect(std.mem.indexOf(u8, aw.written(), first) != null); +} + +test "read success: no data packets for empty clipboard" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (ReadSuccess{ + .contents = &.{.{ .mime = "text/plain", .data = "" }}, + }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK\x1b\\" ++ + "\x1b]5522;type=read:status=DONE\x1b\\", + writer.buffered(), + ); +} + +test "read success: paste event carries pw in every packet" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (ReadSuccess{ + .list = true, + .pw = "otp", + .available = &.{"text/plain"}, + }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK:pw=b3Rw\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=Lg==:pw=b3Rw;dGV4dC9wbGFpbgo=\x1b\\" ++ + "\x1b]5522;type=read:status=DONE:pw=b3Rw\x1b\\", + writer.buffered(), + ); +} + +test "paste event: pw in every packet, listing of every type" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (PasteEvent{ + .pw = "otp", + .available = &.{ "text/plain", "image/png" }, + }).encode(&writer); + // Payload is base64 of "text/plain image/png\n". + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK:pw=b3Rw\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=Lg==:pw=b3Rw;dGV4dC9wbGFpbiBpbWFnZS9wbmcK\x1b\\" ++ + "\x1b]5522;type=read:status=DONE:pw=b3Rw\x1b\\", + writer.buffered(), + ); +} + +test "paste event: primary is reported only on the OK packet" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (PasteEvent{ + .primary = true, + .pw = "otp", + .available = &.{"text/plain"}, + .terminator = .bel, + }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK:loc=primary:pw=b3Rw\x07" ++ + "\x1b]5522;type=read:status=DATA:mime=Lg==:pw=b3Rw;dGV4dC9wbGFpbgo=\x07" ++ + "\x1b]5522;type=read:status=DONE:pw=b3Rw\x07", + writer.buffered(), + ); +} + +test "paste event: empty listing packet is still sent" { + const testing = std.testing; + + var buf: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try (PasteEvent{ .pw = "otp", .available = &.{} }).encode(&writer); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK:pw=b3Rw\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=Lg==:pw=b3Rw\x1b\\" ++ + "\x1b]5522;type=read:status=DONE:pw=b3Rw\x1b\\", + writer.buffered(), + ); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/clipboard_write.zig b/vendor/libghostty-vt/src/terminal/kitty/clipboard_write.zig new file mode 100644 index 00000000..5951dcbe --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/clipboard_write.zig @@ -0,0 +1,698 @@ +//! Kitty clipboard protocol (OSC 5522) write transactions: the +//! stateful accumulation of wdata chunks and walias aliases until the +//! commit packet arrives. See clipboard.zig for the protocol overview. + +const std = @import("std"); +const assert = @import("../../quirks.zig").inlineAssert; +const Allocator = std.mem.Allocator; +const simd = @import("../../simd/main.zig"); +const clipboard = @import("../clipboard.zig"); +const clipboard_command = @import("clipboard_command.zig"); + +const Metadata = clipboard_command.Metadata; +const Payload = clipboard_command.Payload; +const max_mime_len = clipboard_command.max_mime_len; + +const log = std.log.scoped(.kitty_clipboard); + +/// Default maximum total decoded bytes accumulated by one write +/// transaction. Embedders can override this per-transaction via +/// WriteState.Options (Ghostty exposes it as the +/// `clipboard-write-limit-bytes` configuration). +pub const max_write_size = 64 * 1024 * 1024; + +/// Maximum MIME types and aliases per write transaction. +pub const max_write_mimes = 64; +pub const max_write_aliases = 64; + +/// One MIME representation of committed clipboard data. This is the +/// same type the clipboard write effect consumes so committed contents +/// can be passed through directly. +pub const Content = clipboard.Content; + +/// The state of one in-flight write transaction: a single `type=write` +/// plus all the `wdata` chunks and `walias` aliases until completion +/// or error. +pub const WriteState = struct { + arena: std.heap.ArenaAllocator, + loc: clipboard.Location, + id: []const u8, + pw: []const u8, + name: []const u8, + spool: std.ArrayListUnmanaged(u8) = .empty, + entries: std.ArrayListUnmanaged(Entry) = .empty, + aliases: std.ArrayListUnmanaged(Alias) = .empty, + + /// Maximum total decoded bytes this transaction will accumulate. + /// Captured at init so a change to the configured limit doesn't + /// apply to a transaction already in flight. + max_size: usize, + + /// Index into entries currently receiving data. + current: ?usize = null, + + /// Decodes the concatenated payload stream of the entry currently + /// receiving data. Per the spec's "Encoding of payloads" section, + /// individual wdata packet payloads split one base64 stream at + /// arbitrary boundaries; only the concatenation per MIME type must + /// be valid, correctly padded base64. + decoder: simd.base64.Streaming = .{}, + + pub const Options = struct { + /// Maximum total decoded bytes accumulated by the transaction. + max_size: usize = max_write_size, + }; + + const Entry = struct { + /// Owned by the transaction arena. + mime: []const u8, + start: usize = 0, + len: usize = 0, + }; + + const Alias = struct { + /// Owned by the transaction arena. + alias: []const u8, + target: []const u8, + }; + + /// Begin a transaction from a type=write packet. + pub fn init( + alloc: Allocator, + meta: *const Metadata, + opts: Options, + ) Allocator.Error!WriteState { + assert(meta.op == .write); + var arena: std.heap.ArenaAllocator = .init(alloc); + errdefer arena.deinit(); + const id = try arena.allocator().dupe(u8, meta.id); + const pw = try arena.allocator().dupe(u8, meta.pw); + const name = try arena.allocator().dupe(u8, meta.name); + return .{ + .arena = arena, + .loc = meta.loc, + .id = id, + .pw = pw, + .name = name, + .max_size = opts.max_size, + }; + } + + pub fn deinit(self: *WriteState, alloc: Allocator) void { + self.spool.deinit(alloc); + self.entries.deinit(alloc); + self.aliases.deinit(alloc); + self.arena.deinit(); + } + + /// Accumulate one wdata chunk carrying data for meta.mime (which + /// must be non-empty; an empty mime is a commit, not data). + /// + /// Returns error.TooLarge when the transaction exceeds max_size + /// and error.Invalid when the payload stream is not valid base64. + /// The caller must fail the whole transaction with EFBIG or EINVAL + /// respectively and abort it, as required by the protocol. + pub fn data( + self: *WriteState, + alloc: Allocator, + meta: *const Metadata, + payload: []const u8, + ) error{ OutOfMemory, TooLarge, Invalid }!void { + assert(meta.op == .wdata); + assert(meta.mime.len > 0); + // Switch the receiving entry if this chunk is for a different + // MIME type than the last one. + entry: { + if (self.current) |idx| { + const entry = &self.entries.items[idx]; + if (std.mem.eql(u8, entry.mime, meta.mime)) { + break :entry; + } + + // Finalize the previous region. Its concatenated + // stream must have ended on a complete base64 group, + // otherwise the data was not correctly padded. + try self.finishCurrent(); + entry.len = self.spool.items.len - entry.start; + } + + // Re-using an earlier MIME type starts a fresh region, + // overwriting the previous mapping. + for (self.entries.items, 0..) |*entry, idx| { + if (std.mem.eql(u8, entry.mime, meta.mime)) { + entry.start = self.spool.items.len; + entry.len = 0; + self.current = idx; + break :entry; + } + } + + if (self.entries.items.len >= max_write_mimes) { + log.warn( + "clipboard write has too many MIME types, ignoring mime={s}", + .{meta.mime}, + ); + self.current = null; + return; + } + + try self.entries.append(alloc, .{ + .mime = try self.arena.allocator().dupe(u8, meta.mime), + .start = self.spool.items.len, + }); + self.current = self.entries.items.len - 1; + } + + // The payloads for one MIME region concatenate into a single + // strict base64 stream, decoded directly into the spool's + // unused capacity. Invalid data aborts the transaction; per + // the spec it must not be silently discarded "since that + // turns corrupted data into apparently valid data". + try self.spool.ensureUnusedCapacity(alloc, self.decoder.maxLen(payload)); + const decoded = self.decoder.feed( + payload, + self.spool.unusedCapacitySlice(), + ) catch return error.Invalid; + + // The limit covers all decoded data in the transaction. Going + // over it aborts the entire write; partial clipboard contents + // must never reach the embedder. + const remaining = self.max_size -| self.spool.items.len; + if (decoded.len > remaining) return error.TooLarge; + self.spool.items.len += decoded.len; + } + + /// Finish the decode stream of the entry currently receiving + /// data. Returns error.Invalid when the stream ends mid-group, + /// i.e. the concatenated payload was not correctly padded. + fn finishCurrent(self: *WriteState) error{Invalid}!void { + self.decoder.finish() catch return error.Invalid; + } + + /// Register aliases from a walias packet: meta.mime is the target + /// (the type that carries data) and the payload is a base64-encoded, + /// whitespace-separated list of aliases. Returns error.Invalid for + /// an undecodable payload, which aborts the transaction with EINVAL. + pub fn alias( + self: *WriteState, + alloc: Allocator, + meta: *const Metadata, + payload: []const u8, + ) error{ OutOfMemory, Invalid }!void { + assert(meta.op == .walias); + assert(meta.mime.len > 0); + const decoded = try Payload.init(alloc, payload); + defer decoded.deinit(alloc); + if (!decoded.isValidUtf8()) return error.Invalid; + var it = decoded.mimeIterator(); + + // Copy the target only if at least one valid alias exists. + const target: []const u8 = target: { + while (it.next()) |name| { + if (name.len > max_mime_len) continue; + break :target try self.arena.allocator().dupe(u8, meta.mime); + } + + // If we didn't find a target then ignore it. + return; + }; + + // Rewind so the alias that satisfied the check above is + // associated too. + it.reset(); + + // Associate the aliases + while (it.next()) |name| { + if (name.len > max_mime_len) continue; + + // A repeated alias overwrites its previous target. + for (self.aliases.items) |*a| { + if (std.mem.eql(u8, a.alias, name)) { + a.target = target; + break; + } + } else { + if (self.aliases.items.len >= max_write_aliases) { + log.warn("clipboard write has too many aliases, ignoring", .{}); + return; + } + + try self.aliases.append(alloc, .{ + .alias = try self.arena.allocator().dupe(u8, name), + .target = target, + }); + } + } + } + + /// The result of a committed transaction. All slices borrow the + /// WriteState's memory and are valid until it is deinited. + pub const Committed = struct { + loc: clipboard.Location, + id: []const u8, + pw: []const u8, + name: []const u8, + contents: []const Content, + + pub fn deinit(self: *const Committed, alloc: Allocator) void { + alloc.free(self.contents); + } + }; + + /// Commit the transaction (a wdata packet without a MIME type). + /// The caller must use the result, call Committed.deinit, and then + /// deinit this state. Returns error.Invalid when the last region's + /// concatenated payload was not correctly padded; the caller must + /// fail the transaction with EINVAL and abort it. + pub fn commit( + self: *WriteState, + alloc: Allocator, + ) error{ OutOfMemory, Invalid }!Committed { + // Finalize the region receiving data. + if (self.current) |idx| { + try self.finishCurrent(); + const entry = &self.entries.items[idx]; + entry.len = self.spool.items.len - entry.start; + self.current = null; + } + + // Resolve the final MIME map: entries in arrival order, then + // aliases applied sequentially against the evolving map so + // chained aliases work like kitty's dict iteration. An alias + // whose target has no mapping is dropped; an alias colliding + // with an existing name overwrites it. + var contents: std.ArrayListUnmanaged(Content) = .empty; + defer contents.deinit(alloc); + try contents.ensureTotalCapacity( + alloc, + self.entries.items.len + self.aliases.items.len, + ); + + for (self.entries.items) |*entry| { + contents.appendAssumeCapacity(.{ + .mime = entry.mime, + .data = self.spool.items[entry.start..][0..entry.len], + }); + } + + for (self.aliases.items) |*a| { + const target: Content = target: { + for (contents.items) |c| { + if (std.mem.eql(u8, c.mime, a.target)) { + break :target c; + } + } + continue; + }; + + for (contents.items) |*c| { + if (std.mem.eql(u8, c.mime, a.alias)) { + c.data = target.data; + break; + } + } else { + contents.appendAssumeCapacity(.{ + .mime = a.alias, + .data = target.data, + }); + } + } + + return .{ + .loc = self.loc, + .id = self.id, + .pw = self.pw, + .name = self.name, + .contents = try contents.toOwnedSlice(alloc), + }; + } +}; + +test "write: basic transaction" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write, .id = "42" }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "R2hvc3R0eQ=="); // "Ghostty" + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqualStrings("42", committed.id); + try testing.expect(committed.loc == .standard); + try testing.expectEqual(@as(usize, 1), committed.contents.len); + try testing.expectEqualStrings("text/plain", committed.contents[0].mime); + try testing.expectEqualStrings("Ghostty", committed.contents[0].data); +} + +test "write: chunked data accumulates" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "SGVsbG8="); // "Hello" + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "V29ybGQ="); // "World" + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqualStrings("HelloWorld", committed.contents[0].data); +} + +test "write: multiple mimes in order" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "YQ=="); // "a" + try state.data(alloc, &.{ .op = .wdata, .mime = "text/html" }, "Yg=="); // "b" + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqual(@as(usize, 2), committed.contents.len); + try testing.expectEqualStrings("text/plain", committed.contents[0].mime); + try testing.expectEqualStrings("a", committed.contents[0].data); + try testing.expectEqualStrings("text/html", committed.contents[1].mime); + try testing.expectEqualStrings("b", committed.contents[1].data); +} + +test "write: reused mime overwrites" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "YQ=="); // "a" + try state.data(alloc, &.{ .op = .wdata, .mime = "text/html" }, "Yg=="); // "b" + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "Yw=="); // "c" + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqual(@as(usize, 2), committed.contents.len); + // Position preserved, data replaced. + try testing.expectEqualStrings("text/plain", committed.contents[0].mime); + try testing.expectEqualStrings("c", committed.contents[0].data); +} + +test "write: invalid base64 chunk aborts the transaction" { + const testing = std.testing; + const alloc = testing.allocator; + + // Invalid characters anywhere in the stream are error.Invalid, + // which the handlers turn into an EINVAL abort. These mirror + // kitty's own tests for the spec change. + const invalid_payloads = [_][]const u8{ + "!!!", + "SGVs!!!bG8=", + "\nZGF0YSB3aXRoIGEgbmV3bGluZQ==", + }; + for (invalid_payloads) |payload| { + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + try testing.expectError(error.Invalid, state.data( + alloc, + &.{ .op = .wdata, .mime = "text/plain" }, + payload, + )); + } + + // Also after an earlier valid chunk for the same MIME type. + { + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "Z29vZA=="); // "good" + try testing.expectError(error.Invalid, state.data( + alloc, + &.{ .op = .wdata, .mime = "text/plain" }, + "SGVs!!!bG8=", + )); + } +} + +test "write: chunks split one base64 stream at arbitrary boundaries" { + const testing = std.testing; + const alloc = testing.allocator; + + // The concatenation of all payloads per MIME type is the base64 + // stream; individual packets need not be a multiple of four bytes. + const encoded = "c29tZSBkYXRh"; // "some data" + const splits = [_][]const usize{ + &.{ 3, 7 }, + &.{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, + }; + for (splits) |split| { + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + var prev: usize = 0; + for (split) |end| { + try state.data( + alloc, + &.{ .op = .wdata, .mime = "text/plain" }, + encoded[prev..end], + ); + prev = end; + } + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, encoded[prev..]); + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqualStrings("some data", committed.contents[0].data); + } +} + +test "write: incorrectly padded stream aborts at commit" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + // "Hello" without its final padding byte: every chunk decodes, + // but the stream ends mid-group so the commit reports it. + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "SGVsbG8"); + try testing.expectError(error.Invalid, state.commit(alloc)); +} + +test "write: incorrectly padded stream aborts at MIME switch" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "SGVsbG8"); + try testing.expectError(error.Invalid, state.data( + alloc, + &.{ .op = .wdata, .mime = "text/html" }, + "PGI+aGk8L2I+", + )); +} + +test "write: independently padded chunks accumulate" { + const testing = std.testing; + const alloc = testing.allocator; + + // A packet payload ending exactly at terminal padding resets the + // stream, so clients that base64-encode every chunk independently + // keep working (matching kitty, which resets its streaming + // decoder on EOF). Padding followed by more data within a single + // packet stays invalid. + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "Z29vZA=="); // "good" + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "bW9yZQ=="); // "more" + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqualStrings("goodmore", committed.contents[0].data); +} + +test "write: data after padding within one chunk aborts" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + try testing.expectError(error.Invalid, state.data( + alloc, + &.{ .op = .wdata, .mime = "text/plain" }, + "Z29vZA==bW9yZQ==", + )); +} + +test "write: aliases resolve at commit" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "R2hvc3R0eQ=="); // "Ghostty" + + // Alias "TEXT UTF8_STRING" -> text/plain. + const alias_meta: Metadata = .{ .op = .walias, .mime = "text/plain" }; + try state.alias(alloc, &alias_meta, "VEVYVCBVVEY4X1NUUklORw=="); + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqual(@as(usize, 3), committed.contents.len); + try testing.expectEqualStrings("TEXT", committed.contents[1].mime); + try testing.expectEqualStrings("Ghostty", committed.contents[1].data); + try testing.expectEqualStrings("UTF8_STRING", committed.contents[2].mime); + try testing.expectEqualStrings("Ghostty", committed.contents[2].data); +} + +test "write: alias payload must be valid utf8" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + const alias_meta: Metadata = .{ .op = .walias, .mime = "text/plain" }; + // Valid base64 encoding of a single 0xff byte. + try testing.expectError( + error.Invalid, + state.alias(alloc, &alias_meta, "/w=="), + ); +} + +test "write: default limit when unset" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + try testing.expectEqual(@as(usize, max_write_size), state.max_size); +} + +test "write: text over limit rejects transaction" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{ .max_size = 8 }); + defer state.deinit(alloc); + + try testing.expectError(error.TooLarge, state.data( + alloc, + &.{ .op = .wdata, .mime = "text/plain" }, + "SGVsbG9Xb3JsZA==", // "HelloWorld" + )); +} + +test "write: text crossing limit across chunks rejects transaction" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{ .max_size = 8 }); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "SGVsbG8="); // "Hello" + try testing.expectError(error.TooLarge, state.data( + alloc, + &.{ .op = .wdata, .mime = "text/plain" }, + "V29ybGQ=", // "World" + )); +} + +test "write: data exactly at limit is accepted" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{ .max_size = 5 }); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "SGVsbG8="); // "Hello" + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqualStrings("Hello", committed.contents[0].data); +} + +test "write: non-text under limit is unaffected" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{ .max_size = 8 }); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "image/png" }, "iVBORw=="); // "\x89PNG" + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqualStrings("\x89PNG", committed.contents[0].data); +} + +test "write: non-text over limit rejects transaction" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{ .max_size = 8 }); + defer state.deinit(alloc); + + try testing.expectError(error.TooLarge, state.data( + alloc, + &.{ .op = .wdata, .mime = "image/png" }, + "SGVsbG9Xb3JsZA==", // "HelloWorld" + )); +} + +test "write: total data across MIME types is limited" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{ .max_size = 8 }); + defer state.deinit(alloc); + + try state.data(alloc, &.{ .op = .wdata, .mime = "text/plain" }, "SGVsbG8="); // "Hello" + + try testing.expectError(error.TooLarge, state.data( + alloc, + &.{ .op = .wdata, .mime = "image/png" }, + "V29ybGQ=", // "World" + )); +} + +test "write: alias without data target is dropped" { + const testing = std.testing; + const alloc = testing.allocator; + + const begin_meta: Metadata = .{ .op = .write }; + var state: WriteState = try .init(alloc, &begin_meta, .{}); + defer state.deinit(alloc); + + const alias_meta: Metadata = .{ .op = .walias, .mime = "text/plain" }; + try state.alias(alloc, &alias_meta, "VEVYVA=="); // "TEXT" + + const committed = try state.commit(alloc); + defer committed.deinit(alloc); + try testing.expectEqual(@as(usize, 0), committed.contents.len); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/dnd.zig b/vendor/libghostty-vt/src/terminal/kitty/dnd.zig new file mode 100644 index 00000000..3142a415 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/dnd.zig @@ -0,0 +1,69 @@ +//! Kitty drag and drop protocol (OSC 72). +//! +//! Specification: https://sw.kovidgoyal.net/kitty/dnd-protocol/ +//! +//! The protocol lets a program running in the terminal participate in +//! native OS drag and drop. A client registers to accept drops (t=a); +//! the terminal then forwards native drag movement (t=m) and drops +//! (t=M) to it and serves the dropped data on request (t=r), instead +//! of the traditional behavior of pasting dropped paths or text. +//! +//! ## Divergences +//! +//! These will be fixed in the future: +//! +//! * Dropped data is captured eagerly at drop time from a curated +//! set of representations the embedder can serve (typically +//! text/uri-list and text/plain), rather than fetched from the OS +//! on demand. Consequently the native drag session concludes at +//! drop time and the client's concluding operation (t=r with +//! x=y=Y=0) only frees the held data, and kitty's 128-entry +//! request queue and EMFILE overflow handling are unnecessary +//! because requests are served synchronously in order. +//! * Every client is treated as local: machine IDs (t=a:x=1) are +//! accepted and ignored, responses never carry the X=1 remote +//! marker, and remote file transfer requests (t=r with y or Y +//! keys) are answered with EINVAL. A remote client (e.g. over +//! ssh) can still receive text drops; only file-content transfer +//! is unavailable. +//! * The terminal never initiates drags (drag out): enabling and +//! disabling offers (t=o:x=1, t=o:x=2) are accepted and ignored, +//! and since the terminal never sends a drag start request a +//! conforming client never offers a drag. Direct offers (t=o:x=0) +//! and drag data/start commands (t=p, t=P) are refused with EPERM. +//! +//! These are on purpose forever: +//! +//! * Responses echo the requesting command's terminator (ST or BEL) +//! per ghostty convention; kitty always uses ST. Terminal- +//! initiated events always use ST. + +const dnd_command = @import("dnd_command.zig"); +const dnd_response = @import("dnd_response.zig"); +const dnd_drop = @import("dnd_drop.zig"); + +pub const EventType = dnd_command.EventType; +pub const Metadata = dnd_command.Metadata; +pub const Operation = dnd_command.Operation; +pub const Operations = dnd_command.Operations; +pub const Request = dnd_command.Request; +pub const Chunking = dnd_command.Chunking; + +pub const Errno = dnd_response.Errno; +pub const RequestKeys = dnd_response.RequestKeys; +pub const encode = dnd_response.encode; +pub const encodeError = dnd_response.encodeError; + +pub const State = dnd_drop.State; +pub const Item = dnd_drop.State.Item; +pub const MoveEvent = dnd_drop.State.MoveEvent; +pub const max_mime_list_bytes = dnd_drop.max_mime_list_bytes; +pub const handleCommand = dnd_drop.handleCommand; +pub const Event = dnd_drop.Event; + +test { + _ = dnd_command; + _ = dnd_response; + _ = dnd_drop; + _ = @import("dnd_test.zig"); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/dnd_command.zig b/vendor/libghostty-vt/src/terminal/kitty/dnd_command.zig new file mode 100644 index 00000000..b5f63d02 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/dnd_command.zig @@ -0,0 +1,436 @@ +const std = @import("std"); + +/// Decoded OSC 72 metadata. +pub const Metadata = struct { + /// Event type (`t`). Null when the metadata had no `t` key; such + /// commands parse successfully but are ignored, matching kitty. + type: ?EventType = null, + + /// Chunking flag (`m`): true when more chunks follow. + more: bool = false, + + /// Multiplexer client ID (`i`), echoed in every response so a + /// terminal multiplexer can route responses to the correct client. + client_id: u32 = 0, + + /// Operation (`o`): meaning depends on the event type, commonly + /// 0=none/reject, 1=copy, 2=move, 3=copy or move. + operation: u32 = 0, + + /// `x`, `y`, `X`, `Y` keys. + cell_x: i32 = 0, + cell_y: i32 = 0, + pixel_x: i32 = 0, + pixel_y: i32 = 0, + + /// Parse raw OSC 72 metadata (the part before the first `;`). + /// Returns null when malformed; callers should ignore the command, + /// matching kitty which rejects the entire command on any error. + pub fn parse(raw: []const u8) ?Metadata { + var result: Metadata = .{}; + var pos: usize = 0; + // The continue expression consumes the ':' separating a field + // from the next; the body advances past the field itself. + while (pos < raw.len) : (pos += 1) { + // Single-character key. + const key = raw[pos]; + pos += 1; + switch (key) { + 't', 'm', 'i', 'o', 'x', 'y', 'X', 'Y' => {}, + else => return null, + } + + // '=' separator. + if (pos >= raw.len) return null; + if (raw[pos] != '=') return null; + pos += 1; + + // Value. + if (pos >= raw.len) return null; + switch (key) { + 't' => { + result.type = std.enums.fromInt( + EventType, + raw[pos], + ) orelse return null; + pos += 1; + }, + + 'm', 'i', 'o' => { + const v = parseUnsigned(raw, &pos) orelse return null; + switch (key) { + 'm' => result.more = v != 0, + 'i' => result.client_id = v, + 'o' => result.operation = v, + else => unreachable, + } + }, + + 'x', 'y', 'X', 'Y' => { + const negative = raw[pos] == '-'; + if (negative) pos += 1; + const unsigned = parseUnsigned(raw, &pos) orelse return null; + // Matches kitty's cast of the u32 magnitude to i32, + // which wraps rather than erroring on overflow. + const magnitude: i32 = @bitCast(unsigned); + const v = if (negative) 0 -% magnitude else magnitude; + switch (key) { + 'x' => result.cell_x = v, + 'y' => result.cell_y = v, + 'X' => result.pixel_x = v, + 'Y' => result.pixel_y = v, + else => unreachable, + } + }, + + else => unreachable, + } + + // Values are separated by ':'. + if (pos >= raw.len) break; + if (raw[pos] != ':') return null; + } + + return result; + } + + /// Parse an unsigned decimal value at `pos`, advancing it. At most + /// 10 digits and at most maxInt(u32), matching kitty. Returns null + /// when there are no digits or the value is too large. + fn parseUnsigned(raw: []const u8, pos: *usize) ?u32 { + const start = pos.*; + var acc: u64 = 0; + var i = start; + while (i < raw.len and i < start + 10) : (i += 1) { + const d = raw[i] -% '0'; + if (d > 9) break; + acc = acc * 10 + d; + } + if (i == start) return null; + pos.* = i; + return std.math.cast(u32, acc) orelse null; + } +}; + +/// The event type, i.e. values for the `t` metadata key. A single OSC 72 +/// code is used for both directions of the protocol, so most types have +/// one meaning when received by the terminal from a client and another +/// when sent by the terminal to a client. +/// +/// The `drop` and `request_response` types are only ever sent by the +/// terminal; kitty parses but ignores them when received and we do the +/// same. +pub const EventType = enum(u8) { + /// 'a': (recv) client registers to accept drops. With x=1 the payload + /// is the client's machine ID for remote drop support instead. + register = 'a', + + /// 'A': (recv) client unregisters from accepting drops. + unregister = 'A', + + /// 'm': (recv) client reports acceptance status for the drag currently + /// over the terminal: `o` is the chosen operation and the payload is + /// the accepted MIME list. (send) pointer moved over the terminal + /// during a drag, or with x=-1,y=-1 the drag left the window. + status = 'm', + + /// 'M': (send only) items were dropped onto the terminal. + drop = 'M', + + /// 'r': (recv) client requests drop data, or with x=y=Y=0 concludes + /// the drop with `o` as the performed operation. (send) drop data + /// response chunks. + request = 'r', + + /// 'R': (send only) error response to a data request. + request_error = 'R', + + /// 'o': (recv) drag source control: x=1 enables offering drags (payload + /// optionally the client machine ID), x=2 disables, x=0 offers a MIME + /// list for a new drag. (send) request that the client start a drag at + /// the given position. + offer = 'o', + + /// 'p': (recv) pre-sent data for an offered drag: x>=0 is a 0-based + /// MIME index, x<0 attaches drag image -x. + present = 'p', + + /// 'P': (recv) x=-1 starts the offered drag, x>=0 changes the drag + /// image mid-drag. + start_drag = 'P', + + /// 'e': (recv) drag data for MIME index `y` of an in-progress drag. + /// (send) drag status events (accepted, dropped, finished, ...). + drag_event = 'e', + + /// 'E': (recv) client aborts the whole drag (y=-1) or reports an error + /// for MIME index `y`. (send) drag start response (OK or error). + drag_error = 'E', + + /// 'k': (recv) remote file data for a drag. (send) request for remote + /// file data. + remote_data = 'k', + + /// 'q': (recv) query protocol support. (send) the query response. + query = 'q', +}; + +/// A drop operation. Values match the protocol's `o` key. +pub const Operation = enum(u2) { + none = 0, + copy = 1, + move = 2, + + /// Convert a protocol `o` value the way kitty does: anything other + /// than copy or move means none. + pub fn fromProtocol(v: u32) Operation { + return switch (v) { + 1 => .copy, + 2 => .move, + else => .none, + }; + } +}; + +/// The set of operations allowed by a drag source, sent as a bitmask in +/// the `o` key of move and drop events. +pub const Operations = packed struct(u2) { + copy: bool = false, + move: bool = false, + + pub fn protocolValue(self: Operations) u2 { + return @bitCast(self); + } +}; + +/// A decoded `t=r` data request. The request form is disambiguated by +/// which keys are non-zero, mirroring kitty's drop_process_queue. +pub const Request = union(enum) { + /// x=y=Y=0: the drop is concluded with the given operation. + conclude: Operation, + + /// Y=0, y=0, x!=0: request data for the 1-based MIME index x. + mime: i32, + + /// Y=0, y!=0: request the contents of the y'th (1-based) file in the + /// text/uri-list MIME at 1-based index x. Remote drops only. + uri: struct { + mime_idx: i32, + uri_idx: i32, + }, + + /// Y!=0: request entry x (1-based) of directory handle Y, or close + /// the handle when x=0. Remote drops only. + dir: struct { + handle: i32, + entry: i32, + }, + + pub fn init(meta: Metadata) Request { + if (meta.pixel_y != 0) return .{ .dir = .{ + .handle = meta.pixel_y, + .entry = meta.cell_x, + } }; + if (meta.cell_y != 0) return .{ .uri = .{ + .mime_idx = meta.cell_x, + .uri_idx = meta.cell_y, + } }; + if (meta.cell_x != 0) return .{ .mime = meta.cell_x }; + return .{ .conclude = .fromProtocol(meta.operation) }; + } +}; + +/// Chunk reassembly state. +/// +/// While a chunked command is in progress, the metadata of the first +/// chunk is reused for all subsequent chunks; only the `more` flag is +/// taken from each continuation. +pub const Chunking = struct { + active: bool = false, + metadata: Metadata = .{}, + + /// Returns the effective metadata for a received chunk and updates + /// the reassembly state. + pub fn apply(self: *Chunking, meta: Metadata) Metadata { + if (self.active) { + var copy = self.metadata; + copy.more = meta.more; + self.active = meta.more; + return copy; + } + + if (meta.more) { + self.active = true; + self.metadata = meta; + } + + return meta; + } +}; + +test "Metadata: empty" { + const testing = std.testing; + const meta = Metadata.parse("").?; + try testing.expect(meta.type == null); + try testing.expect(!meta.more); + try testing.expectEqual(@as(u32, 0), meta.client_id); +} + +test "Metadata: all keys" { + const testing = std.testing; + const meta = Metadata.parse("t=m:m=1:i=3:o=2:x=10:y=5:X=320:Y=200").?; + try testing.expectEqual(EventType.status, meta.type.?); + try testing.expect(meta.more); + try testing.expectEqual(@as(u32, 3), meta.client_id); + try testing.expectEqual(@as(u32, 2), meta.operation); + try testing.expectEqual(@as(i32, 10), meta.cell_x); + try testing.expectEqual(@as(i32, 5), meta.cell_y); + try testing.expectEqual(@as(i32, 320), meta.pixel_x); + try testing.expectEqual(@as(i32, 200), meta.pixel_y); +} + +test "Metadata: all event types" { + const testing = std.testing; + const cases = .{ + .{ "t=a", EventType.register }, + .{ "t=A", EventType.unregister }, + .{ "t=m", EventType.status }, + .{ "t=M", EventType.drop }, + .{ "t=r", EventType.request }, + .{ "t=R", EventType.request_error }, + .{ "t=o", EventType.offer }, + .{ "t=p", EventType.present }, + .{ "t=P", EventType.start_drag }, + .{ "t=e", EventType.drag_event }, + .{ "t=E", EventType.drag_error }, + .{ "t=k", EventType.remote_data }, + .{ "t=q", EventType.query }, + }; + inline for (cases) |case| { + try testing.expectEqual(case[1], Metadata.parse(case[0]).?.type.?); + } +} + +test "Metadata: case-sensitive coordinate keys" { + const testing = std.testing; + const meta = Metadata.parse("x=10:Y=200").?; + try testing.expectEqual(@as(i32, 10), meta.cell_x); + try testing.expectEqual(@as(i32, 0), meta.cell_y); + try testing.expectEqual(@as(i32, 0), meta.pixel_x); + try testing.expectEqual(@as(i32, 200), meta.pixel_y); +} + +test "Metadata: negative coordinates" { + const testing = std.testing; + const meta = Metadata.parse("t=m:x=-1:y=-1").?; + try testing.expectEqual(@as(i32, -1), meta.cell_x); + try testing.expectEqual(@as(i32, -1), meta.cell_y); +} + +test "Metadata: malformed inputs rejected" { + const testing = std.testing; + // Unknown event type. + try testing.expect(Metadata.parse("t=z") == null); + // Unknown key. + try testing.expect(Metadata.parse("z=1") == null); + // Missing '=' mid-stream. + try testing.expect(Metadata.parse("x10") == null); + // No digits. + try testing.expect(Metadata.parse("x=notanumber") == null); + try testing.expect(Metadata.parse("x=-") == null); + // Too large. + try testing.expect(Metadata.parse("i=4294967296") == null); + try testing.expect(Metadata.parse("i=99999999999") == null); + // Garbage after value. + try testing.expect(Metadata.parse("x=1z") == null); + // No whitespace tolerance, matching kitty. + try testing.expect(Metadata.parse("t=a: x=1") == null); + try testing.expect(Metadata.parse("t = a") == null); + // Truncated mid-construct, matching kitty's final-state check. + // Nothing state-changing may be parsed out of these: e.g. "t=r:x=" + // must not be treated as a drop conclusion. + try testing.expect(Metadata.parse("t") == null); + try testing.expect(Metadata.parse("t=") == null); + try testing.expect(Metadata.parse("x=") == null); + try testing.expect(Metadata.parse("t=r:x=") == null); + try testing.expect(Metadata.parse("x=1:t=") == null); +} + +test "Metadata: trailing separator accepted" { + const testing = std.testing; + + // Kitty's state machine accepts the metadata ending right after a + // value separator. + const meta = Metadata.parse("t=a:").?; + try testing.expectEqual(EventType.register, meta.type.?); +} + +test "Metadata: u32 boundary accepted" { + const testing = std.testing; + const meta = Metadata.parse("i=4294967295").?; + try testing.expectEqual(@as(u32, std.math.maxInt(u32)), meta.client_id); +} + +test "Request: classification" { + const testing = std.testing; + + // Conclude. + { + const r: Request = .init(Metadata.parse("t=r:o=2").?); + try testing.expectEqual(Operation.move, r.conclude); + } + // Conclude with unknown operation is none (canceled). + { + const r: Request = .init(Metadata.parse("t=r:o=9").?); + try testing.expectEqual(Operation.none, r.conclude); + } + // MIME data request. + { + const r: Request = .init(Metadata.parse("t=r:x=2").?); + try testing.expectEqual(@as(i32, 2), r.mime); + } + // URI file request. + { + const r: Request = .init(Metadata.parse("t=r:x=1:y=3").?); + try testing.expectEqual(@as(i32, 1), r.uri.mime_idx); + try testing.expectEqual(@as(i32, 3), r.uri.uri_idx); + } + // Directory handle request. + { + const r: Request = .init(Metadata.parse("t=r:Y=2:x=1").?); + try testing.expectEqual(@as(i32, 2), r.dir.handle); + try testing.expectEqual(@as(i32, 1), r.dir.entry); + } +} + +test "Chunking: reassembly reuses first chunk metadata" { + const testing = std.testing; + var chunking: Chunking = .{}; + + // First chunk starts reassembly. + const first = chunking.apply(Metadata.parse("t=m:o=1:m=1").?); + try testing.expect(chunking.active); + try testing.expectEqual(EventType.status, first.type.?); + try testing.expect(first.more); + + // Continuation metadata is ignored except for `more`. + const second = chunking.apply(Metadata.parse("t=q:o=2:m=1").?); + try testing.expect(chunking.active); + try testing.expectEqual(EventType.status, second.type.?); + try testing.expectEqual(@as(u32, 1), second.operation); + try testing.expect(second.more); + + // Final chunk ends reassembly. + const last = chunking.apply(Metadata.parse("t=q:m=0").?); + try testing.expect(!chunking.active); + try testing.expectEqual(EventType.status, last.type.?); + try testing.expect(!last.more); +} + +test "Chunking: unchunked commands pass through" { + const testing = std.testing; + var chunking: Chunking = .{}; + const meta = chunking.apply(Metadata.parse("t=q").?); + try testing.expect(!chunking.active); + try testing.expectEqual(EventType.query, meta.type.?); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/dnd_drop.zig b/vendor/libghostty-vt/src/terminal/kitty/dnd_drop.zig new file mode 100644 index 00000000..e3c7ee5c --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/dnd_drop.zig @@ -0,0 +1,715 @@ +//! Kitty drag and drop protocol (OSC 72) state machine. + +const std = @import("std"); +const Allocator = std.mem.Allocator; + +const assert = @import("../../quirks.zig").inlineAssert; +const osc = @import("../osc.zig"); +const command = @import("dnd_command.zig"); +const response = @import("dnd_response.zig"); + +const Metadata = command.Metadata; +const Operation = command.Operation; +const Operations = command.Operations; + +const log = std.log.scoped(.kitty_dnd); + +/// Maximum accumulated size of a client-sent MIME list (the accepted +/// list of a `t=m` status update). Matches kitty's MIME_LIST_SIZE_CAP. +pub const max_mime_list_bytes = 1024 * 1024; + +/// Process one OSC 72 command received from the client, writing any +/// responses to the writer. Returns the state change the embedder may +/// need to act on, if any. +pub fn handleCommand( + slot: *?*State, + alloc: Allocator, + writer: *std.Io.Writer, + v: osc.Command.KittyDndProtocol, +) (Allocator.Error || std.Io.Writer.Error)!?Event { + const raw = Metadata.parse(v.metadata) orelse { + log.debug("dropping malformed OSC 72 metadata", .{}); + return null; + }; + + // Chunk reassembly lives in the state, so before registration + // each command stands alone. The only legitimately chunked + // command before registration is t=a itself, which seeds the + // reassembly on its first chunk below. + const continuation = if (slot.*) |state| state.chunking.active else false; + const meta = if (slot.*) |state| state.chunking.apply(raw) else raw; + const payload = v.payload orelse ""; + const t = meta.type orelse return null; + + switch (t) { + .register => { + // x=1 declares the client's machine ID for remote drop + // support. We don't support remote drop yet, so accept and ignore. + if (meta.cell_x == 1) return null; + + // Setup our state if we haven't already + const state = slot.* orelse state: { + const state = try State.create(alloc); + slot.* = state; + _ = state.chunking.apply(raw); + break :state state; + }; + + // Update the client ID on every registration + state.drop.client_id = meta.client_id; + + return try state.register( + alloc, + payload, + continuation, + meta.more, + ); + }, + + .unregister => { + const state = slot.* orelse return null; + state.destroy(alloc); + slot.* = null; + return .registration; + }, + + .status => { + const state = slot.* orelse return null; + return try state.acceptStatus(alloc, meta, payload); + }, + + .request => return try dataRequest( + slot.*, + alloc, + writer, + meta, + v.terminator, + ), + + // Drag source control. Enabling (x=1, with an optional + // machine ID payload) and disabling (x=2) offers are + // accepted and ignored since the terminal never requests a + // drag start. Offering a MIME list (x=0) for a new drag is + // refused since drag-out is not implemented. + .offer => if (meta.cell_x == 0) try refuseDragOut( + writer, + meta, + v.terminator, + ), + + // Drag-out data and start commands. A conforming client + // never sends these because the terminal never requests a + // drag start, but refuse them properly if one does. + .present, .start_drag => try refuseDragOut( + writer, + meta, + v.terminator, + ), + + // Responses to drag-out requests the terminal never makes. + .drag_event, .drag_error, .remote_data => {}, + + .query => try response.encode( + writer, + "t=q", + meta.client_id, + "", + .plain, + v.terminator, + ), + + // Only ever sent by the terminal. Ignore. + .drop, .request_error => {}, + } + + return null; +} + +/// Handle a t=r data request or drop conclusion from the client. +/// Requests from an unregistered client (no state) get the same +/// errors kitty sends from its zeroed drop state. +fn dataRequest( + state: ?*State, + alloc: Allocator, + writer: *std.Io.Writer, + meta: Metadata, + terminator: osc.Terminator, +) (Allocator.Error || std.Io.Writer.Error)!?Event { + // Responses echo the registration's client ID, matching kitty. + const client_id = if (state) |s| s.drop.client_id else 0; + + switch (command.Request.init(meta)) { + .conclude => |op| { + // The client is done with the drop: free the held data and + // report the operation it performed. Kitty hands that to + // the still-open OS drag session; ours ended at drop time + // (see dnd.zig), so the embedder decides what to do with + // it. A conclusion with no drop in progress is a no-op. + const s = state orelse return null; + const dropped = s.drop.dropped; + s.resetDrop(alloc); + return if (dropped) Event.concluded(op) else null; + }, + + .mime => |idx| { + const keys: response.RequestKeys = .{ .x = idx }; + const items = (if (state) |s| s.drop.items else null) orelse { + try response.encodeError( + writer, + .drop, + keys, + client_id, + .ENOENT, + "no drop data available", + terminator, + ); + return null; + }; + if (idx < 1 or @as(usize, @intCast(idx)) > items.len) { + try response.encodeError( + writer, + .drop, + keys, + client_id, + .ENOENT, + "drop data request index out of bounds", + terminator, + ); + return null; + } + + var header_buf: [32]u8 = undefined; + const header = std.fmt.bufPrint( + &header_buf, + "t=r{f}", + .{keys}, + ) catch unreachable; + + // The data chunks followed by the empty end-of-data + // message, which is how the client detects completion. + // An empty item is just the end-of-data message alone; + // clients treat a duplicate as a second completion. + const item = items[@intCast(idx - 1)]; + if (item.data.len > 0) try response.encode( + writer, + header, + client_id, + item.data, + .base64, + terminator, + ); + try response.encode(writer, header, client_id, "", .base64, terminator); + return null; + }, + + // Remote drop transfers (URI file contents and directory + // handles). We never advertise remote support (no X=1 + // marker), so a conforming client never sends these. + .uri => |uri| try response.encodeError( + writer, + .drop, + .{ .x = uri.mime_idx, .y = uri.uri_idx }, + client_id, + .EINVAL, + "remote drop data is not supported", + terminator, + ), + .dir => |dir| try response.encodeError( + writer, + .drop, + .{ .x = dir.entry, .Y = dir.handle }, + client_id, + .EINVAL, + "remote drop data is not supported", + terminator, + ), + } + + return null; +} + +/// Refuse a drag-out command with an error, since ghostty does not +/// implement the terminal side of client-initiated drags yet. +fn refuseDragOut( + writer: *std.Io.Writer, + meta: Metadata, + terminator: osc.Terminator, +) std.Io.Writer.Error!void { + try response.encodeError( + writer, + .drag, + .{}, + meta.client_id, + .EPERM, + "drag out is not supported by this terminal", + terminator, + ); +} + +/// A protocol state change an embedder may need to act on, returned by +/// `handleCommand` and delivered through the stream handler's +/// `drag_and_drop` effect. This is a flat enum so it can cross a C API +/// unchanged; any details are read back from `Terminal.kitty_dnd`. +pub const Event = enum { + /// The client registered (t=a), re-registered, or unregistered + /// (t=A) to accept drops. An embedder may want to use this + /// to setup the proper mime types to accept (e.g. on macOS) + /// or not (unregistered). + registration, + + /// The client answered the drag currently over the terminal. + /// `State.clientAccepted` has the answer. Embedders can refresh the + /// OS drag feedback immediately rather than on the next move. + acceptance, + + /// The client concluded a drop, performing no operation (it + /// canceled), a copy, or a move. The held drop data has been freed. + concluded_none, + concluded_copy, + concluded_move, + + /// The conclusion event for a performed operation. + pub fn concluded(op: Operation) Event { + return switch (op) { + .none => .concluded_none, + .copy => .concluded_copy, + .move => .concluded_move, + }; + } +}; + +/// The per-terminal drop target state. +/// +/// The primary entrypoint is `handleCommand` which takes a `*?*State` +/// slot that it can heap allocate into when DnD activates and free when +/// it deactivates. +/// +/// The normal lifecycle: +/// +/// 1. The stream handler feeds every OSC 72 command received from the +/// client to `handleCommand`. The client registers (t=a), which +/// allocates the state into the slot and yields a `registration` +/// event so the embedder can register any declared MIME types +/// with the OS. Until then `handleCommand` only answers stateless +/// commands (queries, error responses). +/// 2. A native drag enters or moves over the terminal. When the slot +/// is non-null, the embedder calls `dragMove` with the pointer +/// position, the operations the drag source allows, and the MIME +/// types it can serve if dropped. This sends the client a t=m +/// move event; when the slot is null the embedder should handle +/// the drag as it would without the protocol. +/// 3. The client answers with its acceptance (t=m:o=N), recorded by +/// `handleCommand` which yields an `acceptance` event. The +/// embedder reads `clientAccepted` then and on subsequent moves +/// to give the OS drag session its feedback. +/// 4. The drag either leaves, and the embedder calls `dragLeave` to +/// send the t=m leave event, or drops: the embedder captures the +/// representations it advertised and calls `dragDrop`, which +/// copies and holds them and sends the client a t=M drop event. A +/// new drag entering before the client concludes discards the +/// held drop. +/// 5. The client requests data (t=r:x=N), which `handleCommand` +/// serves from the held copies, and then concludes the drop +/// (t=r:o=N), which frees them and yields a `concluded_*` event +/// naming the operation the client performed. +/// 6. The client unregisters (t=A) and `handleCommand` frees the +/// state, yielding a final `registration` event, or the terminal +/// is deinitialized and calls `destroy`. +/// +/// All calls must use the allocator the state was created with (the +/// terminal's) and require the same synchronization as any other +/// terminal mutation. +pub const State = struct { + /// Chunk reassembly for client commands. This is the only part of + /// the state cleared by a terminal reset (RIS), matching kitty. + chunking: command.Chunking = .{}, + + /// Drop target state for the registered client. + drop: DropTarget = .{}, + + pub const DropTarget = struct { + /// Multiplexer client ID from registration, echoed in every + /// drop-side message the terminal sends. + client_id: u32 = 0, + + /// The MIME list the client registered with (the t=a payload), + /// space-separated as received and accumulated across chunks. + /// Only needed by embedders that must register types with the + /// OS ahead of a drag; kitty frees it after doing so, we keep + /// it so the `registration` event can be acted on from here. + registered_mimes: std.ArrayListUnmanaged(u8) = .empty, + + /// True while the pointer of a native drag is over the terminal. + hovered: bool = false, + + /// True after the native drop until the client concludes it. + dropped: bool = false, + + /// The client's response to the current drag, null until the + /// client has responded. `none` means the client rejected it. + accepted: ?Operation = null, + + /// True while a chunked t=m acceptance is being accumulated. + accept_in_progress: bool = false, + + /// The client's accepted MIME list: space-separated while + /// accumulating, converted to NUL-separated (with a trailing + /// NUL) once complete, matching kitty's in-place conversion. + accepted_mimes: std.ArrayListUnmanaged(u8) = .empty, + + /// The MIME types of the current native drag, in the order + /// that data request indices refer to. + offered: ?Offered = null, + + /// The data captured at drop time, parallel to `offered`. + items: ?[]const Item = null, + }; + + /// One dropped representation: a MIME type and its data. + pub const Item = struct { + mime: []const u8, + data: []const u8, + }; + + /// The MIME list of the current drag plus the pre-joined move-event + /// payload ("mime1 mime2 " with a trailing space after every entry, + /// matching kitty) so per-move encoding is allocation-free. + const Offered = struct { + mimes: []const []const u8, + payload: []const u8, + + fn init(alloc: Allocator, mimes: []const []const u8) Allocator.Error!Offered { + const copies = try alloc.alloc([]const u8, mimes.len); + errdefer alloc.free(copies); + + var payload_len: usize = 0; + for (mimes) |m| payload_len += m.len + 1; + + const payload = try alloc.alloc(u8, payload_len); + errdefer alloc.free(payload); + + var offset: usize = 0; + for (mimes, copies) |m, *copy| { + @memcpy(payload[offset..][0..m.len], m); + payload[offset + m.len] = ' '; + copy.* = payload[offset..][0..m.len]; + offset += m.len + 1; + } + + return .{ .mimes = copies, .payload = payload }; + } + + fn deinit(self: *const Offered, alloc: Allocator) void { + alloc.free(self.mimes); + alloc.free(self.payload); + } + + fn eql(self: *const Offered, mimes: []const []const u8) bool { + if (self.mimes.len != mimes.len) return false; + for (self.mimes, mimes) |a, b| { + if (!std.mem.eql(u8, a, b)) return false; + } + return true; + } + }; + + /// The maximum number of dropped items. Embedders provide a small + /// curated set of representations (see dnd.zig), so this is a + /// generous bound that keeps the MIME list assembly on the stack. + pub const max_items = 16; + + /// Allocate a fresh state. Done by `handleCommand` on registration. + fn create(alloc: Allocator) Allocator.Error!*State { + const state = try alloc.create(State); + state.* = .{}; + return state; + } + + /// Free the state and everything it holds. + pub fn destroy(self: *State, alloc: Allocator) void { + self.deinit(alloc); + alloc.destroy(self); + } + + fn deinit(self: *State, alloc: Allocator) void { + self.freeDragData(alloc); + self.drop.accepted_mimes.deinit(alloc); + self.drop.registered_mimes.deinit(alloc); + } + + /// Iterate the MIME types the client registered with, in order. + /// Empty when the client declared none, which is the common case. + /// The list is only needed to register exotic types with the OS, + /// such as macOS pasteboard stuff. + pub fn registeredMimes(self: *const State) std.mem.TokenIterator(u8, .scalar) { + return std.mem.tokenizeScalar( + u8, + self.drop.registered_mimes.items, + ' ', + ); + } + + /// Record one chunk of a registration's MIME list. + /// + /// `continuation` is true for every chunk but the first of a chunked + /// registration. Returns the registration event once the list is complete. + fn register( + self: *State, + alloc: Allocator, + payload: []const u8, + continuation: bool, + more: bool, + ) Allocator.Error!?Event { + const list = &self.drop.registered_mimes; + if (!continuation) list.clearRetainingCapacity(); + + // Matching kitty, an over-cap chunk is dropped and does not + // complete the registration. + if (list.items.len + payload.len > max_mime_list_bytes) return null; + try list.appendSlice(alloc, payload); + + return if (more) null else .registration; + } + + /// The client's acceptance response for the drag currently over the + /// terminal, for OS drag feedback. Null when the client hasn't + /// responded yet (embedders should fall back to their default, + /// typically copy) or `none` when the client rejected the drag. + pub fn clientAccepted(self: *const State) ?Operation { + if (self.drop.accept_in_progress) return null; + return self.drop.accepted; + } + + /// Free the per-drag data (offered MIME list and held drop items). + fn freeDragData(self: *State, alloc: Allocator) void { + if (self.drop.offered) |*offered| { + offered.deinit(alloc); + self.drop.offered = null; + } + if (self.drop.items) |items| { + for (items) |item| { + alloc.free(item.mime); + alloc.free(item.data); + } + alloc.free(items); + self.drop.items = null; + } + } + + /// Clear the per-drag state while preserving the registration, + /// mirroring kitty's reset_drop. Called when a new drag enters and + /// when a drop concludes. + fn resetDrop(self: *State, alloc: Allocator) void { + self.freeDragData(alloc); + self.drop.accepted_mimes.clearAndFree(alloc); + self.drop.hovered = false; + self.drop.dropped = false; + self.drop.accepted = null; + self.drop.accept_in_progress = false; + } + + /// Handle a t=m acceptance status update from the client, mirroring + /// kitty's drop_set_status. + fn acceptStatus( + self: *State, + alloc: Allocator, + meta: Metadata, + payload: []const u8, + ) Allocator.Error!?Event { + const d = &self.drop; + if (!d.accept_in_progress) { + d.accepted_mimes.clearRetainingCapacity(); + d.accept_in_progress = true; + d.accepted = .fromProtocol(meta.operation); + } + + if (payload.len > 0) { + // Matching kitty, an over-cap list stops accumulating and + // never finalizes, leaving the acceptance unanswered. + if (d.accepted_mimes.items.len + payload.len > max_mime_list_bytes) return null; + try d.accepted_mimes.appendSlice(alloc, payload); + } + + if (meta.more) return null; + d.accept_in_progress = false; + if (d.accepted_mimes.items.len > 0) { + for (d.accepted_mimes.items) |*c| { + if (c.* == ' ') c.* = 0; + } + try d.accepted_mimes.append(alloc, 0); + } + return .acceptance; + } + + /// A native drag position report from the embedder. + pub const MoveEvent = struct { + /// Grid cell under the pointer, zero-based from the top-left. + cell_x: u32, + cell_y: u32, + + /// Pointer position in pixels relative to the top-left of the + /// terminal's content area. + pixel_x: i32, + pixel_y: i32, + + /// The operations the drag source allows. + operations: Operations, + }; + + /// Report a native drag moving over the terminal, sending a t=m + /// move event to the client. `mimes` is the list of MIME types the + /// terminal can provide for this drag, in the order data request + /// indices will refer to. + pub fn dragMove( + self: *State, + alloc: Allocator, + writer: *std.Io.Writer, + ev: MoveEvent, + mimes: []const []const u8, + ) (Allocator.Error || std.Io.Writer.Error)!void { + try self.moveEvent( + alloc, + writer, + ev, + mimes, + false, + ); + } + + /// Report a native drop onto the terminal. The items' data is + /// copied and held so the client's data requests can be served; it + /// is freed when the client concludes the drop, a new drag enters, + /// or the client unregisters. + /// + /// Sends a t=M drop event listing the items' MIME types. + pub fn dragDrop( + self: *State, + alloc: Allocator, + writer: *std.Io.Writer, + ev: MoveEvent, + items: []const Item, + ) (Allocator.Error || std.Io.Writer.Error)!void { + // Copy the items so they can be served after this call returns. + // Items beyond the cap are dropped so the held list always + // matches the advertised MIME list. + const accepted_items = items[0..@min(items.len, max_items)]; + const copies = try alloc.alloc(Item, accepted_items.len); + errdefer alloc.free(copies); + var copied: usize = 0; + errdefer for (copies[0..copied]) |item| { + alloc.free(item.mime); + alloc.free(item.data); + }; + for (accepted_items, copies) |item, *copy| { + const mime = try alloc.dupe(u8, item.mime); + errdefer alloc.free(mime); + const data = try alloc.dupe(u8, item.data); + copy.* = .{ .mime = mime, .data = data }; + copied += 1; + } + + // The move handling below resets per-drag state when this drop + // arrives without a preceding move, so the items are attached + // after it runs. Collect the MIME list first. + var mimes_buf: [max_items][]const u8 = undefined; + const mimes = mimes_buf[0..copies.len]; + for (mimes, copies) |*m, item| m.* = item.mime; + + try self.moveEvent( + alloc, + writer, + ev, + mimes, + true, + ); + + assert(self.drop.items == null); + self.drop.items = copies; + } + + /// Report the native drag leaving the terminal, sending the t=m + /// leave event (x=-1, y=-1). + /// + /// Ignored after a drop: some toolkits emit a leave notification + /// for the drop itself, and the held data must survive until the + /// client concludes. + pub fn dragLeave( + self: *State, + alloc: Allocator, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + if (self.drop.dropped) return; + const hovered = self.drop.hovered; + self.drop.hovered = false; + if (self.drop.offered) |*offered| { + offered.deinit(alloc); + self.drop.offered = null; + } + + // Only a client that saw the drag enter gets the leave event, + // matching kitty which notifies hovered windows only. + if (!hovered) return; + + try response.encode( + writer, + "t=m:x=-1:y=-1", + self.drop.client_id, + "", + .plain, + .st, + ); + } + + /// Shared implementation of move and drop events, mirroring kitty's + /// drop_move_on_child. + fn moveEvent( + self: *State, + alloc: Allocator, + writer: *std.Io.Writer, + ev: MoveEvent, + mimes: []const []const u8, + is_drop: bool, + ) (Allocator.Error || std.Io.Writer.Error)!void { + if (!self.drop.hovered) { + self.resetDrop(alloc); + self.drop.hovered = true; + } + if (is_drop) { + self.drop.dropped = true; + self.drop.hovered = false; + } + + // (Re)build the offered MIME list when it changed. + if (self.drop.offered == null or !self.drop.offered.?.eql(mimes)) { + if (self.drop.offered) |*offered| offered.deinit(alloc); + self.drop.offered = null; + self.drop.offered = try Offered.init(alloc, mimes); + } + + var header_buf: [96]u8 = undefined; + const header = std.fmt.bufPrint( + &header_buf, + "t={c}:x={d}:y={d}:X={d}:Y={d}:o={d}", + .{ + @as(u8, if (is_drop) 'M' else 'm'), + ev.cell_x, + ev.cell_y, + ev.pixel_x, + ev.pixel_y, + ev.operations.protocolValue(), + }, + ) catch unreachable; + + // The MIME list is sent with every move event, matching kitty + // (the spec suggests only the first, but kitty always sends it + // and clients depend on that). + try response.encode( + writer, + header, + self.drop.client_id, + self.drop.offered.?.payload, + .plain, + .st, + ); + } +}; diff --git a/vendor/libghostty-vt/src/terminal/kitty/dnd_response.zig b/vendor/libghostty-vt/src/terminal/kitty/dnd_response.zig new file mode 100644 index 00000000..1f7584f1 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/dnd_response.zig @@ -0,0 +1,248 @@ +const std = @import("std"); +const Terminator = @import("../osc.zig").Terminator; + +/// The maximum raw bytes per base64-encoded chunk, chosen by kitty so a +/// chunk is exactly 4096 base64 characters (the protocol's chunk limit). +pub const max_chunk_raw = 3072; + +/// The maximum bytes per plain-text (non-base64) chunk. +pub const max_chunk_plain = 4096; + +/// Error names used in protocol error payloads. The wire encoding is +/// the tag name itself. This matches kitty's get_errno_name vocabulary, +/// which extends the spec's list with EISDIR, ENOSPC, and OK. +pub const Errno = enum { + OK, + EPERM, + ENOENT, + EIO, + EINVAL, + EMFILE, + ENOMEM, + EFBIG, + EISDIR, + ENOSPC, + EUNKNOWN, +}; + +/// The payload encoding for a message. +pub const Encoding = enum { + /// Payload bytes are sent as-is (MIME lists, error strings). + plain, + + /// Payload bytes are base64-encoded (all binary data). + base64, +}; + +/// The `x`/`y`/`Y` keys of the data request currently being answered, +/// echoed in responses and errors so the client can match them up. Only +/// non-zero keys are written, matching kitty's drop_append_request_keys. +pub const RequestKeys = struct { + x: i32 = 0, + y: i32 = 0, + Y: i32 = 0, + + pub fn format(self: RequestKeys, writer: *std.Io.Writer) !void { + if (self.x != 0) try writer.print(":x={d}", .{self.x}); + if (self.y != 0) try writer.print(":y={d}", .{self.y}); + if (self.Y != 0) try writer.print(":Y={d}", .{self.Y}); + } +}; + +/// Encode a complete protocol message: one bare OSC when `data` is +/// empty, otherwise one complete OSC per chunk of `data`, each +/// repeating the header. The final chunk carries `m=0`, earlier +/// chunks `m=1`. +/// +/// `header` is the metadata without the OSC introducer, e.g. "t=q" or +/// "t=m:x=5:y=3". The client ID is appended as `:i=N` when non-zero. +pub fn encode( + writer: *std.Io.Writer, + header: []const u8, + client_id: u32, + data: []const u8, + encoding: Encoding, + terminator: Terminator, +) std.Io.Writer.Error!void { + // The client ID is part of the repeated header. + var id_buf: [16]u8 = undefined; + const id: []const u8 = if (client_id != 0) std.fmt.bufPrint( + &id_buf, + ":i={d}", + .{client_id}, + ) catch unreachable else ""; + + if (data.len == 0) { + try writer.print("\x1b]72;{s}{s}{s}", .{ + header, + id, + terminator.string(), + }); + return; + } + + const limit: usize = switch (encoding) { + .base64 => max_chunk_raw, + .plain => max_chunk_plain, + }; + + var offset: usize = 0; + while (offset < data.len) { + const chunk_len = @min(data.len - offset, limit); + const chunk = data[offset .. offset + chunk_len]; + offset += chunk_len; + const last: u8 = if (offset >= data.len) '0' else '1'; + + try writer.print("\x1b]72;{s}{s}:m={c};", .{ header, id, last }); + switch (encoding) { + .plain => try writer.writeAll(chunk), + .base64 => { + var b64_buf: [std.base64.standard.Encoder.calcSize(max_chunk_raw)]u8 = undefined; + try writer.writeAll(std.base64.standard.Encoder.encode( + &b64_buf, + chunk, + )); + }, + } + try writer.writeAll(terminator.string()); + } +} + +/// Encode an error response. `kind` selects the header: t=R for drop +/// data request errors, t=E for drag offer errors. The payload is +/// "NAME" or "NAME:description", sent plain (not base64). +pub fn encodeError( + writer: *std.Io.Writer, + kind: enum { drop, drag }, + keys: RequestKeys, + client_id: u32, + errno: Errno, + desc: []const u8, + terminator: Terminator, +) std.Io.Writer.Error!void { + var header_buf: [64]u8 = undefined; + const header = std.fmt.bufPrint(&header_buf, "t={c}{f}", .{ + @as(u8, switch (kind) { + .drop => 'R', + .drag => 'E', + }), + keys, + }) catch unreachable; + + // Description strings are short static messages; size the buffer + // for the longest error name plus a generous description. + var payload_buf: [256]u8 = undefined; + const payload = if (desc.len > 0) std.fmt.bufPrint( + &payload_buf, + "{t}:{s}", + .{ errno, desc }, + ) catch unreachable else @tagName(errno); + + try encode(writer, header, client_id, payload, .plain, terminator); +} + +test "encode: bare message" { + const testing = std.testing; + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try encode(&writer, "t=q", 0, "", .plain, .st); + try testing.expectEqualStrings("\x1b]72;t=q\x1b\\", writer.buffered()); +} + +test "encode: bare message with client id" { + const testing = std.testing; + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try encode(&writer, "t=q", 7, "", .plain, .st); + try testing.expectEqualStrings("\x1b]72;t=q:i=7\x1b\\", writer.buffered()); +} + +test "encode: plain payload single chunk" { + const testing = std.testing; + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try encode(&writer, "t=m:x=1:y=2", 0, "text/plain ", .plain, .st); + try testing.expectEqualStrings( + "\x1b]72;t=m:x=1:y=2:m=0;text/plain \x1b\\", + writer.buffered(), + ); +} + +test "encode: base64 payload chunking" { + const testing = std.testing; + const alloc = testing.allocator; + + var aw: std.Io.Writer.Allocating = .init(alloc); + defer aw.deinit(); + + // Exactly one byte more than a chunk to force two chunks. + const data = [_]u8{'A'} ** (max_chunk_raw + 1); + try encode(&aw.writer, "t=r:x=1", 3, &data, .base64, .st); + + const out = aw.written(); + + // First chunk: full header, m=1, 4096 base64 chars. + const prefix = "\x1b]72;t=r:x=1:i=3:m=1;"; + try testing.expect(std.mem.startsWith(u8, out, prefix)); + const first_end = std.mem.indexOf(u8, out, "\x1b\\").?; + try testing.expectEqual(@as(usize, prefix.len + 4096), first_end); + + // Second chunk: m=0 with the single remaining byte. + const rest = out[first_end + 2 ..]; + try testing.expect(std.mem.startsWith(u8, rest, "\x1b]72;t=r:x=1:i=3:m=0;")); + + // Decodes back to the original data. + var decoded: std.ArrayList(u8) = .empty; + defer decoded.deinit(alloc); + var it = std.mem.splitSequence(u8, out, "\x1b\\"); + while (it.next()) |osc| { + if (osc.len == 0) continue; + const payload_start = std.mem.indexOfScalar(u8, osc, ';').?; + const payload = osc[std.mem.indexOfScalarPos(u8, osc, payload_start + 1, ';').? + 1 ..]; + const n = try std.base64.standard.Decoder.calcSizeForSlice(payload); + const start = decoded.items.len; + try decoded.resize(alloc, start + n); + try std.base64.standard.Decoder.decode(decoded.items[start..], payload); + } + try testing.expectEqualSlices(u8, &data, decoded.items); +} + +test "encodeError: with and without description" { + const testing = std.testing; + var buf: [256]u8 = undefined; + + { + var writer: std.Io.Writer = .fixed(&buf); + try encodeError(&writer, .drop, .{ .x = 2 }, 0, .ENOENT, "drop data request index out of bounds", .st); + try testing.expectEqualStrings( + "\x1b]72;t=R:x=2:m=0;ENOENT:drop data request index out of bounds\x1b\\", + writer.buffered(), + ); + } + { + var writer: std.Io.Writer = .fixed(&buf); + try encodeError(&writer, .drag, .{}, 5, .EPERM, "", .st); + try testing.expectEqualStrings( + "\x1b]72;t=E:i=5:m=0;EPERM\x1b\\", + writer.buffered(), + ); + } +} + +test "RequestKeys: only non-zero keys written" { + const testing = std.testing; + var buf: [64]u8 = undefined; + + { + const s = try std.fmt.bufPrint(&buf, "{f}", .{RequestKeys{}}); + try testing.expectEqualStrings("", s); + } + { + const s = try std.fmt.bufPrint(&buf, "{f}", .{RequestKeys{ .x = 1, .y = 2, .Y = 3 }}); + try testing.expectEqualStrings(":x=1:y=2:Y=3", s); + } + { + const s = try std.fmt.bufPrint(&buf, "{f}", .{RequestKeys{ .Y = 4 }}); + try testing.expectEqualStrings(":Y=4", s); + } +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/dnd_test.zig b/vendor/libghostty-vt/src/terminal/kitty/dnd_test.zig new file mode 100644 index 00000000..c78ce00f --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/dnd_test.zig @@ -0,0 +1,626 @@ +//! End-to-end tests for the OSC 72 protocol state machine, validating +//! wire behavior against kitty's implementation (using kitty_tests/dnd.py as +//! an oracle for the expected bytes). +//! +//! It isn't normal for us to have dedicated test files but in this case +//! the dnd protocol is complicated enough that I wanted full e2e covering +//! the full machine. + +const std = @import("std"); +const testing = std.testing; + +const osc = @import("../osc.zig"); +const dnd = @import("dnd.zig"); + +/// A test harness holding the lazily allocated protocol state and an +/// output collector. +const Harness = struct { + state: ?*dnd.State = null, + output: std.Io.Writer.Allocating, + + fn init() Harness { + return .{ .output = .init(testing.allocator) }; + } + + fn deinit(self: *Harness) void { + if (self.state) |state| state.destroy(testing.allocator); + self.output.deinit(); + } + + /// The registered state; asserts a client has registered. + fn registered(self: *Harness) *dnd.State { + return self.state.?; + } + + /// Feed one client command, as it would arrive from the OSC parser, + /// returning the event the stream handler would pass to its effect. + fn command(self: *Harness, metadata: []const u8, payload: ?[]const u8) !?dnd.Event { + return try dnd.handleCommand(&self.state, testing.allocator, &self.output.writer, .{ + .metadata = metadata, + .payload = payload, + .terminator = .st, + }); + } + + /// Consume and return the collected output. + fn consume(self: *Harness) []const u8 { + const written = self.output.written(); + return written; + } + + fn clear(self: *Harness) void { + self.output.clearRetainingCapacity(); + } + + fn expectOutput(self: *Harness, expected: []const u8) !void { + try testing.expectEqualStrings(expected, self.output.written()); + self.clear(); + } +}; + +test "dnd: query response" { + var h: Harness = .init(); + defer h.deinit(); + + // Works without any registration, matching kitty, and allocates + // nothing. + _ = try h.command("t=q", null); + try h.expectOutput("\x1b]72;t=q\x1b\\"); + try testing.expect(h.state == null); +} + +test "dnd: query response echoes client id" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=q:i=31", null); + try h.expectOutput("\x1b]72;t=q:i=31\x1b\\"); +} + +test "dnd: register and unregister" { + var h: Harness = .init(); + defer h.deinit(); + + try testing.expect(h.state == null); + + // Registration allocates the state and reports it, with the + // declared MIME list readable from the state. + try testing.expect((try h.command("t=a", "text/plain text/uri-list")).? == .registration); + try h.expectOutput(""); + { + var it = h.registered().registeredMimes(); + try testing.expectEqualStrings("text/plain", it.next().?); + try testing.expectEqualStrings("text/uri-list", it.next().?); + try testing.expect(it.next() == null); + } + + // Machine ID declaration is accepted and ignored. + try testing.expect((try h.command("t=a:x=1", "1:deadbeef")) == null); + try h.expectOutput(""); + try testing.expect(h.state != null); + + // Re-registration replaces the list. + try testing.expect((try h.command("t=a", "image/png")).? == .registration); + { + var it = h.registered().registeredMimes(); + try testing.expectEqualStrings("image/png", it.next().?); + try testing.expect(it.next() == null); + } + + // Registering without a list is the common case. + try testing.expect((try h.command("t=a", null)).? == .registration); + { + var it = h.registered().registeredMimes(); + try testing.expect(it.next() == null); + } + + // Unregistration frees it and reports the change. + try testing.expect((try h.command("t=A", null)).? == .registration); + try h.expectOutput(""); + try testing.expect(h.state == null); + + // Unregistering again changes nothing. + try testing.expect((try h.command("t=A", null)) == null); + try h.expectOutput(""); + try testing.expect(h.state == null); +} + +test "dnd: no state before registration" { + var h: Harness = .init(); + defer h.deinit(); + + // State-dependent commands from an unregistered client allocate + // nothing; a data request gets the error kitty sends from its + // zeroed state. + _ = try h.command("t=m:o=1", "text/plain"); + _ = try h.command("t=r", null); + try h.expectOutput(""); + _ = try h.command("t=r:x=1", null); + try h.expectOutput( + "\x1b]72;t=R:x=1:m=0;ENOENT:no drop data available\x1b\\", + ); + try testing.expect(h.state == null); +} + +test "dnd: move event carries position, operations, and mime list" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", "text/plain"); + + try h.registered().dragMove(testing.allocator, &h.output.writer, .{ + .cell_x = 5, + .cell_y = 3, + .pixel_x = 100, + .pixel_y = 60, + .operations = .{ .copy = true }, + }, &.{ "text/plain", "text/uri-list" }); + + // Note the trailing space after every MIME entry, matching kitty. + try h.expectOutput( + "\x1b]72;t=m:x=5:y=3:X=100:Y=60:o=1:m=0;text/plain text/uri-list \x1b\\", + ); +} + +test "dnd: move event echoes registration client id" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a:i=7", ""); + try h.registered().dragMove(testing.allocator, &h.output.writer, .{ + .cell_x = 1, + .cell_y = 2, + .pixel_x = 8, + .pixel_y = 16, + .operations = .{ .copy = true, .move = true }, + }, &.{"text/plain"}); + try h.expectOutput("\x1b]72;t=m:x=1:y=2:X=8:Y=16:o=3:i=7:m=0;text/plain \x1b\\"); +} + +test "dnd: re-registration updates client id in place" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a:i=7", ""); + const state = h.registered(); + _ = try h.command("t=a:i=9", ""); + // Same allocation, new client ID. + try testing.expect(h.state.? == state); + try testing.expectEqual(@as(u32, 9), state.drop.client_id); +} + +test "dnd: mime list sent on every move" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + const ev: dnd.MoveEvent = .{ + .cell_x = 0, + .cell_y = 0, + .pixel_x = 0, + .pixel_y = 0, + .operations = .{ .copy = true }, + }; + try h.registered().dragMove(testing.allocator, &h.output.writer, ev, &.{"text/plain"}); + h.clear(); + + // Kitty resends the list even when unchanged; clients depend on it. + try h.registered().dragMove(testing.allocator, &h.output.writer, ev, &.{"text/plain"}); + try h.expectOutput("\x1b]72;t=m:x=0:y=0:X=0:Y=0:o=1:m=0;text/plain \x1b\\"); +} + +test "dnd: leave event" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + try h.registered().dragMove(testing.allocator, &h.output.writer, .{ + .cell_x = 0, + .cell_y = 0, + .pixel_x = 0, + .pixel_y = 0, + .operations = .{ .copy = true }, + }, &.{"text/plain"}); + h.clear(); + + try h.registered().dragLeave(testing.allocator, &h.output.writer); + try h.expectOutput("\x1b]72;t=m:x=-1:y=-1\x1b\\"); +} + +test "dnd: client acceptance recorded" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + try testing.expect(h.registered().clientAccepted() == null); + + try testing.expect((try h.command("t=m:o=1", "text/plain")).? == .acceptance); + try h.expectOutput(""); + try testing.expectEqual(dnd.Operation.copy, h.registered().clientAccepted().?); + + // Rejection. + try testing.expect((try h.command("t=m:o=0", "")).? == .acceptance); + try testing.expectEqual(dnd.Operation.none, h.registered().clientAccepted().?); +} + +test "dnd: chunked client acceptance" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + + // Chunked accept: continuation metadata is ignored, the acceptance + // is pending until the final chunk. + try testing.expect((try h.command("t=m:o=2:m=1", "text/pl")) == null); + try testing.expect(h.registered().clientAccepted() == null); + try testing.expect((try h.command("t=m:m=1", "ain text")) == null); + try testing.expect((try h.command("t=m:m=0", "/html")).? == .acceptance); + try testing.expectEqual(dnd.Operation.move, h.registered().clientAccepted().?); + + // The accumulated list was converted to NUL-separated entries. + try testing.expectEqualSlices( + u8, + "text/plain\x00text/html\x00", + h.registered().drop.accepted_mimes.items, + ); +} + +test "dnd: drop and data serving round trip" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", "text/plain text/uri-list"); + + const ev: dnd.MoveEvent = .{ + .cell_x = 4, + .cell_y = 2, + .pixel_x = 40, + .pixel_y = 20, + .operations = .{ .copy = true }, + }; + try h.registered().dragDrop(testing.allocator, &h.output.writer, ev, &.{ + .{ .mime = "text/uri-list", .data = "file:///tmp/a.txt\r\n" }, + .{ .mime = "text/plain", .data = "hello" }, + }); + try h.expectOutput( + "\x1b]72;t=M:x=4:y=2:X=40:Y=20:o=1:m=0;text/uri-list text/plain \x1b\\", + ); + + // Request the second MIME's data: base64 chunk plus the empty + // end-of-data message. + _ = try h.command("t=r:x=2", null); + try h.expectOutput( + "\x1b]72;t=r:x=2:m=0;aGVsbG8=\x1b\\" ++ "\x1b]72;t=r:x=2\x1b\\", + ); + + // Out-of-bounds request. + _ = try h.command("t=r:x=3", null); + try h.expectOutput( + "\x1b]72;t=R:x=3:m=0;ENOENT:drop data request index out of bounds\x1b\\", + ); + + // Conclude: the performed operation is reported, held data is + // freed, and further requests fail. + try testing.expectEqual(dnd.Event.concluded_copy, (try h.command("t=r:o=1", null)).?); + try h.expectOutput(""); + try testing.expect((try h.command("t=r:o=1", null)) == null); + _ = try h.command("t=r:x=1", null); + try h.expectOutput( + "\x1b]72;t=R:x=1:m=0;ENOENT:no drop data available\x1b\\", + ); +} + +test "dnd: empty item served as a single end-of-data message" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + try h.registered().dragDrop(testing.allocator, &h.output.writer, .{ + .cell_x = 0, + .cell_y = 0, + .pixel_x = 0, + .pixel_y = 0, + .operations = .{ .copy = true }, + }, &.{.{ .mime = "text/plain", .data = "" }}); + h.clear(); + + // Kitty's oracle (test_empty_data) asserts exactly one message: + // the empty response is itself the end-of-data signal, and a + // duplicate would be a second completion to the client. + _ = try h.command("t=r:x=1", null); + try h.expectOutput("\x1b]72;t=r:x=1\x1b\\"); +} + +test "dnd: leave without hover sends nothing" { + var h: Harness = .init(); + defer h.deinit(); + + // Client registered but no move was ever forwarded (e.g. it + // registered mid-drag): kitty only notifies hovered windows. + _ = try h.command("t=a", ""); + try h.registered().dragLeave(testing.allocator, &h.output.writer); + try h.expectOutput(""); +} + +test "dnd: data request with no drop" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + _ = try h.command("t=r:x=1", null); + try h.expectOutput( + "\x1b]72;t=R:x=1:m=0;ENOENT:no drop data available\x1b\\", + ); +} + +test "dnd: leave after drop is ignored" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + try h.registered().dragDrop(testing.allocator, &h.output.writer, .{ + .cell_x = 0, + .cell_y = 0, + .pixel_x = 0, + .pixel_y = 0, + .operations = .{ .copy = true }, + }, &.{.{ .mime = "text/plain", .data = "x" }}); + h.clear(); + + // Some toolkits emit a leave for the drop itself; the held data + // must survive so the client can still fetch it. + try h.registered().dragLeave(testing.allocator, &h.output.writer); + try h.expectOutput(""); + + _ = try h.command("t=r:x=1", null); + try h.expectOutput( + "\x1b]72;t=r:x=1:m=0;eA==\x1b\\" ++ "\x1b]72;t=r:x=1\x1b\\", + ); +} + +test "dnd: new drag resets held drop data" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + const ev: dnd.MoveEvent = .{ + .cell_x = 0, + .cell_y = 0, + .pixel_x = 0, + .pixel_y = 0, + .operations = .{ .copy = true }, + }; + try h.registered().dragDrop(testing.allocator, &h.output.writer, ev, &.{ + .{ .mime = "text/plain", .data = "old" }, + }); + h.clear(); + + // A new drag entering resets the per-drag state including the held + // items from the unconcluded previous drop. + try h.registered().dragMove(testing.allocator, &h.output.writer, ev, &.{"text/plain"}); + h.clear(); + _ = try h.command("t=r:x=1", null); + try h.expectOutput( + "\x1b]72;t=R:x=1:m=0;ENOENT:no drop data available\x1b\\", + ); +} + +test "dnd: remote transfer requests refused" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + + // URI file content request. + _ = try h.command("t=r:x=1:y=2", null); + try h.expectOutput( + "\x1b]72;t=R:x=1:y=2:m=0;EINVAL:remote drop data is not supported\x1b\\", + ); + + // Directory handle request. + _ = try h.command("t=r:Y=2:x=1", null); + try h.expectOutput( + "\x1b]72;t=R:x=1:Y=2:m=0;EINVAL:remote drop data is not supported\x1b\\", + ); +} + +test "dnd: drag out refused" { + var h: Harness = .init(); + defer h.deinit(); + + // Enabling and disabling offers is accepted silently and allocates + // nothing. + _ = try h.command("t=o:x=1", null); + _ = try h.command("t=o:x=2", null); + try h.expectOutput(""); + try testing.expect(h.state == null); + + // Offering a drag is refused. + _ = try h.command("t=o:x=1", null); + _ = try h.command("t=o:o=3", "text/plain"); + try h.expectOutput( + "\x1b]72;t=E:m=0;EPERM:drag out is not supported by this terminal\x1b\\", + ); + + // Starting a drag is refused, echoing the command's client id. + _ = try h.command("t=P:x=-1:i=9", null); + try h.expectOutput( + "\x1b]72;t=E:i=9:m=0;EPERM:drag out is not supported by this terminal\x1b\\", + ); + try testing.expect(h.state == null); +} + +test "dnd: unregister frees held drop data" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + try h.registered().dragDrop(testing.allocator, &h.output.writer, .{ + .cell_x = 0, + .cell_y = 0, + .pixel_x = 0, + .pixel_y = 0, + .operations = .{ .copy = true }, + }, &.{.{ .mime = "text/plain", .data = "x" }}); + h.clear(); + + // The testing allocator would report the held data as leaked if + // unregistration didn't free the whole state. + _ = try h.command("t=A", null); + try testing.expect(h.state == null); +} + +test "dnd: chunked registration reuses first chunk metadata" { + var h: Harness = .init(); + defer h.deinit(); + + // Registration split over two chunks: the first chunk allocates + // the state and seeds chunk reassembly, so the continuation (which + // carries a different type) is still treated as the registration. + try testing.expect((try h.command("t=a:i=4:m=1", "text/pla")) == null); + try testing.expect(h.state != null); + try testing.expect((try h.command("t=q:m=0", "in")).? == .registration); + try h.expectOutput(""); + try testing.expectEqual(@as(u32, 4), h.registered().drop.client_id); + { + var it = h.registered().registeredMimes(); + try testing.expectEqualStrings("text/plain", it.next().?); + } + + // A query after the chunked command completes works again. + _ = try h.command("t=q", null); + try h.expectOutput("\x1b]72;t=q\x1b\\"); +} + +test "dnd: malformed metadata ignored" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a:zz=1", ""); + try h.expectOutput(""); + try testing.expect(h.state == null); + + // Command with no type is ignored, matching kitty (the spec's + // default of t=a is not honored by the reference implementation). + _ = try h.command("x=1", ""); + try h.expectOutput(""); + try testing.expect(h.state == null); +} + +test "dnd: bel terminator echoed in responses" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try dnd.handleCommand(&h.state, testing.allocator, &h.output.writer, .{ + .metadata = "t=q", + .payload = null, + .terminator = .bel, + }); + try h.expectOutput("\x1b]72;t=q\x07"); +} + +test "dnd: kitten 0.47 conversation replay" { + // This replays a conversation recorded from the reference client + // (`kitten dnd --drop-anywhere=copy --drop text/plain:out.txt`, + // kitten 0.47.0) driven over a pty by a harness that sent exactly + // the bytes this engine produces. The kitten accepted the events, + // wrote the dropped payload to disk intact, and concluded; its + // client bytes are frozen here as an interop regression test. + var h: Harness = .init(); + defer h.deinit(); + + // Startup: register with MIME list and machine ID, then the test + // harness reset (unregister both directions, re-register). + _ = try h.command("t=a:m=0", "text/uri-list text/plain"); + _ = try h.command( + "t=a:x=1:m=0", + "1:5cff8247c477900a8727e2281fe890252f8848f87c224dd8dd7fb6303e94ddbd", + ); + _ = try h.command("t=A", null); + try testing.expect(h.state == null); + _ = try h.command("t=o:x=2", null); + _ = try h.command("t=a:m=0", "text/uri-list text/plain"); + _ = try h.command( + "t=a:x=1:m=0", + "1:5cff8247c477900a8727e2281fe890252f8848f87c224dd8dd7fb6303e94ddbd", + ); + try h.expectOutput(""); + try testing.expect(h.state != null); + + // Native drag moves over the terminal and drops. + const ev: dnd.MoveEvent = .{ + .cell_x = 2, + .cell_y = 1, + .pixel_x = 20, + .pixel_y = 18, + .operations = .{ .copy = true }, + }; + try h.registered().dragMove(testing.allocator, &h.output.writer, ev, &.{"text/plain"}); + try h.expectOutput("\x1b]72;t=m:x=2:y=1:X=20:Y=18:o=1:m=0;text/plain \x1b\\"); + + // The kitten accepts as a copy of text/plain. + _ = try h.command("t=m:o=1:m=0", "text/plain"); + try h.expectOutput(""); + try testing.expectEqual(dnd.Operation.copy, h.registered().clientAccepted().?); + + try h.registered().dragDrop(testing.allocator, &h.output.writer, ev, &.{ + .{ .mime = "text/plain", .data = "hello from ghostty\n" }, + }); + try h.expectOutput("\x1b]72;t=M:x=2:y=1:X=20:Y=18:o=1:m=0;text/plain \x1b\\"); + + // The kitten requests the data and concludes with a copy. + _ = try h.command("t=r:x=1", null); + try h.expectOutput( + "\x1b]72;t=r:x=1:m=0;aGVsbG8gZnJvbSBnaG9zdHR5Cg==\x1b\\" ++ + "\x1b]72;t=r:x=1\x1b\\", + ); + _ = try h.command("t=r:o=1", null); + try h.expectOutput(""); + try testing.expect(h.registered().drop.items == null); +} + +test "dnd: large data served in chunks" { + var h: Harness = .init(); + defer h.deinit(); + + _ = try h.command("t=a", ""); + + // 3073 bytes: one full chunk plus one byte. + const data = [_]u8{'Z'} ** 3073; + try h.registered().dragDrop(testing.allocator, &h.output.writer, .{ + .cell_x = 0, + .cell_y = 0, + .pixel_x = 0, + .pixel_y = 0, + .operations = .{ .copy = true }, + }, &.{.{ .mime = "application/octet-stream", .data = &data }}); + h.clear(); + + _ = try h.command("t=r:x=1", null); + const out = h.consume(); + + // First chunk is m=1 with 4096 base64 chars, second is m=0, and + // the final message is the bare end-of-data marker. + try testing.expect(std.mem.startsWith(u8, out, "\x1b]72;t=r:x=1:m=1;")); + try testing.expect(std.mem.indexOf(u8, out, "\x1b]72;t=r:x=1:m=0;") != null); + try testing.expect(std.mem.endsWith(u8, out, "\x1b]72;t=r:x=1\x1b\\")); + h.clear(); +} + +test "dnd: over-cap registration list never completes" { + var h: Harness = .init(); + defer h.deinit(); + + // Matching kitty, a chunk that would exceed the cap is dropped and + // the registration is not reported, though the client stays + // registered (the state exists). + const big = try testing.allocator.alloc(u8, dnd.max_mime_list_bytes + 1); + defer testing.allocator.free(big); + @memset(big, 'a'); + try testing.expect((try h.command("t=a", big)) == null); + try testing.expect(h.state != null); + { + var it = h.registered().registeredMimes(); + try testing.expect(it.next() == null); + } +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/graphics.zig b/vendor/libghostty-vt/src/terminal/kitty/graphics.zig index 1f639fab..ef45ee8d 100644 --- a/vendor/libghostty-vt/src/terminal/kitty/graphics.zig +++ b/vendor/libghostty-vt/src/terminal/kitty/graphics.zig @@ -3,11 +3,6 @@ //! Documentation: //! https://sw.kovidgoyal.net/kitty/graphics-protocol //! -//! Unimplemented features that are still todo: -//! - shared memory transmit -//! - virtual placement w/ unicode -//! - animation -//! //! Performance: //! The performance of this particular subsystem of Ghostty is not great. //! We can avoid a lot more allocations, we can replace some C code (which @@ -21,7 +16,10 @@ const command = @import("graphics_command.zig"); const exec = @import("graphics_exec.zig"); const image = @import("graphics_image.zig"); const storage = @import("graphics_storage.zig"); +pub const animation = @import("graphics_animation.zig"); +pub const pixel = @import("graphics_pixel.zig"); pub const unicode = @import("graphics_unicode.zig"); +pub const Animation = animation.Animation; pub const Command = command.Command; pub const CommandParser = command.Parser; pub const Image = image.Image; diff --git a/vendor/libghostty-vt/src/terminal/kitty/graphics_animation.zig b/vendor/libghostty-vt/src/terminal/kitty/graphics_animation.zig new file mode 100644 index 00000000..0822a451 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/graphics_animation.zig @@ -0,0 +1,149 @@ +//! Kitty graphics protocol animation support. +//! +//! https://sw.kovidgoyal.net/kitty/graphics-protocol/#animation +//! +//! An animation is a set of frames attached to an existing image. +//! Frame 1 (the "root frame") is the image's own base data. Frames +//! 2..N are stored here. Unlike Kitty, which stores frames as delta +//! rectangles in a disk cache and composes them lazily, we compose +//! every frame eagerly into a full image-sized RGBA buffer at load +//! time. This trades some memory for a much simpler model: a frame is +//! always ready to display and there are no reference chains to +//! maintain, coalesce, or garbage collect. Frame storage is bounded +//! by the same byte limit as image storage. +//! +//! To keep composition to a single pixel format, an image is +//! converted to RGBA the first time an animation command composes +//! into it, and all transmitted frame data is converted to RGBA +//! before composition. Blending an opaque (alpha=255) source pixel +//! degenerates to a copy, so this loses no information relative to +//! Kitty's separate RGB/RGBA paths. The pixel-level primitives +//! (conversion, fill, rectangle composition) live in +//! graphics_pixel.zig. + +const std = @import("std"); +const Allocator = std.mem.Allocator; + +/// The gap assigned to a newly created frame when the command doesn't +/// specify one (z omitted or z=0). Taken from Kitty (DEFAULT_GAP). +pub const default_gap_ms: u32 = 40; + +/// Animation state for a single image. Heap-allocated and owned by +/// the Image. This is created lazily by the first animation command that +/// needs it. +pub const Animation = struct { + /// The frames following the root frame: `frames.items[i]` is + /// protocol frame number `i + 2`. The root frame (frame 1) is the + /// image's base data and is not stored here. + frames: std.ArrayListUnmanaged(Frame) = .empty, + + /// The gap of the root frame in milliseconds. Zero means gapless: + /// the frame is skipped during playback. Kitty creates the root + /// frame with a zero gap; the client gives it one with a=a,r=1,z=N. + root_gap_ms: u32 = 0, + + /// The zero-based index of the frame currently displayed: 0 is + /// the root frame, i >= 1 is frames.items[i - 1]. + current_index: u32 = 0, + + /// Playback state. Every image starts stopped (client-driven). + state: State = .stopped, + + /// Maximum number of loops to play (0 = infinite). Set from the + /// a=a v key as v-1, per the protocol's off-by-one encoding. + max_loops: u32 = 0, + + /// Number of completed loops since playback started. + current_loop: u32 = 0, + + /// Timestamp in milliseconds (on the ticker's clock, see + /// ImageStorage.animationTick) when the current frame was shown. + /// Null means "not yet shown"; the next tick stamps it. + frame_shown_at_ms: ?u64 = null, + + pub const State = enum { + /// Not advancing; frames change only via a=a c=N (client-driven). + stopped, + /// Advancing, but playback parks on the last frame waiting + /// for more frames instead of looping (a=a s=2). + loading, + /// Advancing and looping (a=a s=3). + running, + }; + + pub const Frame = struct { + /// Fully composed pixel data, always image width * height * 4 + /// bytes of RGBA. + data: []u8, + + /// Milliseconds this frame is displayed before advancing. + /// Zero means gapless: skipped during playback. + gap_ms: u32, + }; + + pub fn deinit(self: *Animation, alloc: Allocator) void { + for (self.frames.items) |frame| alloc.free(frame.data); + self.frames.deinit(alloc); + } + + /// The total number of frames including the root frame. + pub fn frameCount(self: *const Animation) u32 { + return @intCast(self.frames.items.len + 1); + } + + /// The gap of the frame at the given zero-based index. + pub fn gapAt(self: *const Animation, index: u32) u32 { + if (index == 0) return self.root_gap_ms; + return self.frames.items[index - 1].gap_ms; + } + + /// Set the gap of the frame at the given zero-based index. + pub fn setGapAt(self: *Animation, index: u32, gap_ms: u32) void { + if (index == 0) { + self.root_gap_ms = gap_ms; + } else { + self.frames.items[index - 1].gap_ms = gap_ms; + } + } + + /// The sum of all frame gaps. An animation with a zero duration + /// (every frame gapless) never advances; this doubles as the + /// guard that keeps the gapless-skip loop in animationTick from + /// spinning forever, exactly like Kitty's animation_duration. + pub fn durationMs(self: *const Animation) u64 { + var total: u64 = self.root_gap_ms; + for (self.frames.items) |frame| total += frame.gap_ms; + return total; + } + + /// Bytes of frame data held by this animation, counted against + /// the image storage limit. + pub fn frameBytes(self: *const Animation) usize { + var total: usize = 0; + for (self.frames.items) |frame| total += frame.data.len; + return total; + } +}; + +test "animation gap helpers" { + const testing = std.testing; + const alloc = testing.allocator; + + var anim: Animation = .{}; + defer anim.deinit(alloc); + try anim.frames.append(alloc, .{ + .data = try alloc.alloc(u8, 4), + .gap_ms = 100, + }); + + try testing.expectEqual(@as(u32, 2), anim.frameCount()); + try testing.expectEqual(@as(u32, 0), anim.gapAt(0)); + try testing.expectEqual(@as(u32, 100), anim.gapAt(1)); + try testing.expectEqual(@as(u64, 100), anim.durationMs()); + try testing.expectEqual(@as(usize, 4), anim.frameBytes()); + + anim.setGapAt(0, 40); + anim.setGapAt(1, 60); + try testing.expectEqual(@as(u64, 100), anim.durationMs()); + try testing.expectEqual(@as(u32, 40), anim.root_gap_ms); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/graphics_command.zig b/vendor/libghostty-vt/src/terminal/kitty/graphics_command.zig index bc0ffb5b..e57ad174 100644 --- a/vendor/libghostty-vt/src/terminal/kitty/graphics_command.zig +++ b/vendor/libghostty-vt/src/terminal/kitty/graphics_command.zig @@ -1,25 +1,61 @@ const std = @import("std"); const assert = @import("../../quirks.zig").inlineAssert; const Allocator = std.mem.Allocator; -const ArenaAllocator = std.heap.ArenaAllocator; const simd = @import("../../simd/main.zig"); const lib = @import("../lib.zig"); const log = std.log.scoped(.kitty_gfx); /// The key-value pairs for the control information for a command. The -/// keys are always single characters and the values are either single -/// characters or 32-bit unsigned integers. +/// recognized keys are always single ASCII letters and the values are either +/// single characters or 32-bit unsigned integers. Unknown non-letter keys are +/// ignored, matching how unknown letter keys are ignored when building a +/// command. /// -/// For the value of this: if the value is a single printable ASCII character -/// it is the ASCII code. Otherwise, it is parsed as a 32-bit unsigned integer. -const KV = std.AutoHashMapUnmanaged(u8, u32); +/// This is deliberately a dense value table plus a presence bitmap. There are +/// only 52 possible keys(technically KIP has fever than 52 possible .. at the moment of writing), so a lookup is one machine-word bit test followed by +/// an indexed load. The values are undefined until their presence bit is set. +/// +/// Other alternatives were considered and should probably be revisited in the +/// future. Some of them were: +/// A `[52]?u32` table is 416 bytes versus 216 bytes for this layout, and +/// its larger copies erased the smaller lookup code. +/// `std.EnumMap` added optional-return lowering, +/// `std.StaticBitSet(52)` kept the same size but generated larger code +/// for the packed mask. Passing the table by pointer removed the copies +/// but did not improve end-to-end throughput. Keep this representation +/// unless a new benchmark shows an improvement in the zig compilers codegen. +/// +/// For the value itself: if it is a single printable ASCII character it is the +/// ASCII code. Otherwise, it is parsed as a 32-bit unsigned integer. +const KV = struct { + values: [52]u32 = undefined, + present: u64 = 0, + + fn index(key: u8) ?u6 { + return switch (key) { + 'a'...'z' => @intCast(key - 'a'), + 'A'...'Z' => @intCast(26 + key - 'A'), + else => null, + }; + } + + fn get(self: *const KV, key: u8) ?u32 { + const idx = index(key) orelse return null; + if (self.present & (@as(u64, 1) << idx) == 0) return null; + return self.values[idx]; + } + + fn put(self: *KV, key: u8, value: u32) void { + const idx = index(key) orelse return; + self.values[idx] = value; + self.present |= @as(u64, 1) << idx; + } +}; /// Command parser parses the Kitty graphics protocol escape sequence. pub const Parser = struct { - /// The memory used by the parser is stored in an arena because it is - /// all freed at the end of the command. - arena: ArenaAllocator, + alloc: Allocator, /// This is the list of KV pairs that we're building up. kv: KV, @@ -60,10 +96,8 @@ pub const Parser = struct { /// Initialize the parser. The allocator given will be used for both /// temporary data and long-lived values such as the final image blob. pub fn init(alloc: Allocator, max_bytes: usize) Parser { - var arena = ArenaAllocator.init(alloc); - errdefer arena.deinit(); var result: Parser = .{ - .arena = arena, + .alloc = alloc, .data = .empty, .kv = .{}, .kv_temp_len = 0, @@ -82,9 +116,7 @@ pub const Parser = struct { } pub fn deinit(self: *Parser) void { - // We don't free the hash map because its in the arena - self.data.deinit(self.arena.child_allocator); - self.arena.deinit(); + self.data.deinit(self.alloc); } /// Parse a complete command string. @@ -144,7 +176,7 @@ pub const Parser = struct { .data => { if (self.data.items.len >= self.max_bytes) return error.OutOfMemory; - try self.data.append(self.arena.child_allocator, c); + try self.data.append(self.alloc, c); }, } } @@ -160,7 +192,7 @@ pub const Parser = struct { if (self.data.items.len + rem.len > self.max_bytes) { return error.OutOfMemory; } - try self.data.appendSlice(self.arena.child_allocator, rem); + try self.data.appendSlice(self.alloc, rem); return; } @@ -205,18 +237,18 @@ pub const Parser = struct { 'p' => .{ .display = try .parse(self.kv) }, 'd' => .{ .delete = try .parse(self.kv) }, 'f' => .{ .transmit_animation_frame = try .parse(self.kv) }, - 'a' => .{ .control_animation = try .parse(self.kv) }, - 'c' => .{ .compose_animation = try .parse(self.kv) }, + 'a' => .{ .control_animation = .parse(self.kv) }, + 'c' => .{ .compose_animation = .parse(self.kv) }, else => return error.InvalidFormat, }; - // Determine our quiet value + // Determine our quiet value. The spec specifies 0 and 1 but Kitty + // allows anything greater than 1 to suppress. const quiet: Command.Quiet = if (self.kv.get('q')) |v| quiet: { break :quiet switch (v) { 0 => .no, 1 => .ok, - 2 => .failures, - else => return error.InvalidFormat, + else => .failures, }; } else .no; @@ -269,8 +301,6 @@ pub const Parser = struct { } fn finishValue(self: *Parser, next_state: State) !void { - const alloc = self.arena.allocator(); - // We can move states right away, we don't use it. self.state = next_state; @@ -278,7 +308,7 @@ pub const Parser = struct { if (self.kv_temp_len == 1) { const c = self.kv_temp[0]; if (c < '0' or c > '9') { - try self.kv.put(alloc, self.kv_current, @intCast(c)); + self.kv.put(self.kv_current, @intCast(c)); self.kv_temp_len = 0; return; } @@ -291,7 +321,7 @@ pub const Parser = struct { 'z', 'H', 'V' => @bitCast(try std.fmt.parseInt(i32, self.kv_temp[0..self.kv_temp_len], 10)), else => try std.fmt.parseInt(u32, self.kv_temp[0..self.kv_temp_len], 10), }; - try self.kv.put(alloc, self.kv_current, v); + self.kv.put(self.kv_current, v); // Clear our temp buffer self.kv_temp_len = 0; @@ -303,6 +333,10 @@ pub const Response = struct { id: u32 = 0, image_number: u32 = 0, placement_id: u32 = 0, + /// The 1-based animation frame number, echoed as "r=" for the + /// animation frame actions. This is how clients learn the frame + /// number assigned to a newly created frame. + frame: u32 = 0, message: []const u8 = "OK", pub fn encode(self: Response, writer: *std.Io.Writer) !void { @@ -326,6 +360,10 @@ pub const Response = struct { if (prior) try writer.writeByte(',') else prior = true; try writer.print("p={}", .{self.placement_id}); } + if (self.frame > 0) { + if (prior) try writer.writeByte(',') else prior = true; + try writer.print("r={}", .{self.frame}); + } try writer.writeByte(';'); try writer.writeAll(self.message); try writer.writeAll("\x1b\\"); @@ -376,6 +414,53 @@ pub const Command = struct { transmit_animation_frame: AnimationFrameLoading, control_animation: AnimationControl, compose_animation: AnimationFrameComposition, + + pub const Identifiers = struct { + image_id: u32 = 0, + image_number: u32 = 0, + placement_id: u32 = 0, + }; + + /// Returns the image and placement identifiers for any action. + pub fn identifiers(self: Control) Identifiers { + return switch (self) { + .query, .transmit => |t| .{ + .image_id = t.image_id, + .image_number = t.image_number, + .placement_id = t.placement_id, + }, + .transmit_and_display => |t| .{ + .image_id = t.transmission.image_id, + .image_number = t.transmission.image_number, + .placement_id = t.transmission.placement_id, + }, + .display => |d| .{ + .image_id = d.image_id, + .image_number = d.image_number, + .placement_id = d.placement_id, + }, + .delete => |d| .{ + .image_id = d.image_id, + .image_number = d.image_number, + .placement_id = d.placement_id, + }, + .transmit_animation_frame => |f| .{ + .image_id = f.transmission.image_id, + .image_number = f.transmission.image_number, + .placement_id = f.transmission.placement_id, + }, + .control_animation => |a| .{ + .image_id = a.image_id, + .image_number = a.image_number, + .placement_id = a.placement_id, + }, + .compose_animation => |c| .{ + .image_id = c.image_id, + .image_number = c.image_number, + .placement_id = c.placement_id, + }, + }; + } }; /// Take ownership over the data in this command. If the returned value @@ -392,6 +477,7 @@ pub const Command = struct { .query => |t| t, .transmit => |t| t, .transmit_and_display => |t| t.transmission, + .transmit_animation_frame => |f| f.transmission, else => null, }; } @@ -412,6 +498,11 @@ pub const Command = struct { pub const Transmission = struct { format: Format = .rgba, // f + /// Set when "f" carried a value we don't recognize. Kitty rejects these + /// while handling the command so the EINVAL response can carry the image + /// id, so we defer to `LoadingImage.init`. `format` is meaningless when + /// this is set. + format_unknown: bool = false, medium: Medium = .direct, // t width: u32 = 0, // s height: u32 = 0, // v @@ -422,6 +513,7 @@ pub const Transmission = struct { placement_id: u32 = 0, // p compression: Compression = .none, // o more_chunks: bool = false, // m + usage: Usage = .default, // N pub const Format = lib.Enum(lib.target, &.{ "rgb", // 24 @@ -446,6 +538,19 @@ pub const Transmission = struct { "zlib_deflate", // z }); + /// Usage hints allow for optimising resource consumption strategies. + /// + /// https://sw.kovidgoyal.net/kitty/graphics-protocol/#usage-hints + pub const Usage = packed struct(u32) { + /// Image with this usage hint is assumed to be used for only a short + /// time, so may be evicted before other images if memory pressure is + /// encountered. + transient: bool = false, + _padding: u31 = 0, + + pub const default: Usage = .{}; + }; + pub fn formatBpp(format: Format) u8 { return switch (format) { .gray => 1, @@ -461,9 +566,13 @@ pub const Transmission = struct { if (kv.get('f')) |v| { result.format = switch (v) { 24 => .rgb, - 32 => .rgba, + 0, 32 => .rgba, 100 => .png, - else => return error.InvalidFormat, + else => unknown: { + // Defer returning an error until the image is processed. + result.format_unknown = true; + break :unknown .rgba; + }, }; } @@ -529,6 +638,10 @@ pub const Transmission = struct { } } + if (kv.get('N')) |v| { + result.usage = @bitCast(v); + } + return result; } }; @@ -606,19 +719,13 @@ pub const Display = struct { } if (kv.get('C')) |v| { - result.cursor_movement = switch (v) { - 0 => .after, - 1 => .none, - else => return error.InvalidFormat, - }; + // Kitty only tests this against 1 so any other value moves the cursor. + result.cursor_movement = if (v == 1) .none else .after; } if (kv.get('U')) |v| { - result.virtual_placement = switch (v) { - 0 => false, - 1 => true, - else => return error.InvalidFormat, - }; + // Kitty stores this in a bool, so any non-zero value is virtual. + result.virtual_placement = v != 0; } if (kv.get('z')) |v| { @@ -649,23 +756,51 @@ pub const Display = struct { }; pub const AnimationFrameLoading = struct { + /// Frame data is transmitted exactly like image data, so an a=f + /// command carries a full set of transmission keys: medium (t), + /// format (f), the frame rectangle size (s/v), chunking (m), etc. + /// The image id/number here identify the (existing) image the + /// frame belongs to. + transmission: Transmission = .{}, + + /// The left/top edge (pixels) of the rectangle within the frame + /// that the transmitted data updates. x: u32 = 0, // x y: u32 = 0, // y + + /// The 1-based frame number whose pixels serve as the base canvas + /// for a newly created frame. Zero means no base frame: the + /// canvas is filled with `background` instead. create_frame: u32 = 0, // c + + /// The 1-based frame number of an existing frame to edit. Zero + /// (or any value past the next frame number) creates a new frame. edit_frame: u32 = 0, // r - gap_ms: u32 = 0, // z + + /// The gap in milliseconds before the next frame is shown. For + /// new frames, zero selects the default gap and a negative value + /// creates a gapless (never displayed) frame. For edits, zero + /// leaves the gap unchanged. + gap_ms: i32 = 0, // z + composition_mode: CompositionMode = .alpha_blend, // X background: Background = .{}, // Y + /// The canvas background color as a 32-bit RGBA value where R is + /// the most significant byte (0xRRGGBBAA), e.g. Y=4278190335 + /// (0xff0000ff) is opaque red. Bit-cast from the little-endian + /// u32 value, so the field order is reversed from the byte order. pub const Background = packed struct(u32) { - r: u8 = 0, - g: u8 = 0, - b: u8 = 0, a: u8 = 0, + b: u8 = 0, + g: u8 = 0, + r: u8 = 0, }; - fn parse(kv: KV) !AnimationFrameLoading { - var result: AnimationFrameLoading = .{}; + fn parse(kv: KV) error{InvalidFormat}!AnimationFrameLoading { + var result: AnimationFrameLoading = .{ + .transmission = try .parse(kv), + }; if (kv.get('x')) |v| { result.x = v; @@ -684,15 +819,15 @@ pub const AnimationFrameLoading = struct { } if (kv.get('z')) |v| { - result.gap_ms = v; + // 'z' is one of the keys Parser.finishValue parses as an + // i32, so the u32 in the KV table is the bit pattern of + // that i32 and this reconstructs it exactly. + result.gap_ms = @bitCast(v); } + // Tested only against 1 like Kitty. if (kv.get('X')) |v| { - result.composition_mode = switch (v) { - 0 => .alpha_blend, - 1 => .overwrite, - else => return error.InvalidFormat, - }; + result.composition_mode = if (v == 1) .overwrite else .alpha_blend; } if (kv.get('Y')) |v| { @@ -704,25 +839,56 @@ pub const AnimationFrameLoading = struct { }; pub const AnimationFrameComposition = struct { - frame: u32 = 0, // c - edit_frame: u32 = 0, // r + image_id: u32 = 0, // i + image_number: u32 = 0, // I + placement_id: u32 = 0, // p + + /// The 1-based frame number being edited (the destination). + dest_frame: u32 = 0, // c + + /// The 1-based frame number whose pixels are composed onto the + /// destination (the source). + source_frame: u32 = 0, // r + + /// The left/top edge (pixels) of the destination rectangle. x: u32 = 0, // x y: u32 = 0, // y + + /// The size of the rectangle to compose. Zero selects the full + /// image width/height. width: u32 = 0, // w height: u32 = 0, // h + + /// The left/top edge (pixels) of the source rectangle. left_edge: u32 = 0, // X top_edge: u32 = 0, // Y + composition_mode: CompositionMode = .alpha_blend, // C - fn parse(kv: KV) !AnimationFrameComposition { + // Cannot fail: every key is stored as parsed, with no range + // validation (Kitty range-checks these while handling the + // command; see the comment above the execution-deferral tests). + fn parse(kv: KV) AnimationFrameComposition { var result: AnimationFrameComposition = .{}; + if (kv.get('i')) |v| { + result.image_id = v; + } + + if (kv.get('I')) |v| { + result.image_number = v; + } + + if (kv.get('p')) |v| { + result.placement_id = v; + } + if (kv.get('c')) |v| { - result.frame = v; + result.dest_frame = v; } if (kv.get('r')) |v| { - result.edit_frame = v; + result.source_frame = v; } if (kv.get('x')) |v| { @@ -750,11 +916,8 @@ pub const AnimationFrameComposition = struct { } if (kv.get('C')) |v| { - result.composition_mode = switch (v) { - 0 => .alpha_blend, - 1 => .overwrite, - else => return error.InvalidFormat, - }; + // Kitty tests this against zero for compose so any non-zero overwrites + result.composition_mode = if (v == 0) .alpha_blend else .overwrite; } return result; @@ -762,10 +925,24 @@ pub const AnimationFrameComposition = struct { }; pub const AnimationControl = struct { + image_id: u32 = 0, // i + image_number: u32 = 0, // I + placement_id: u32 = 0, // p action: AnimationAction = .invalid, // s + + /// The 1-based frame number whose gap is set from gap_ms. frame: u32 = 0, // r - gap_ms: u32 = 0, // z + + /// The new gap for `frame` in milliseconds. Zero is ignored and a + /// negative value makes the frame gapless. + gap_ms: i32 = 0, // z + + /// The 1-based frame number to make current (client-driven + /// animation). Zero is ignored. current_frame: u32 = 0, // c + + /// The loop count: zero is ignored, 1 loops forever, and any + /// larger value plays that many minus one loops. loops: u32 = 0, // v pub const AnimationAction = enum { @@ -775,16 +952,32 @@ pub const AnimationControl = struct { run, // 3 }; - fn parse(kv: KV) !AnimationControl { + // Cannot fail: every key is stored as parsed. Out-of-range values + // (frame numbers, states) are silently ignored during execution, + // matching Kitty. + fn parse(kv: KV) AnimationControl { var result: AnimationControl = .{}; + if (kv.get('i')) |v| { + result.image_id = v; + } + + if (kv.get('I')) |v| { + result.image_number = v; + } + + if (kv.get('p')) |v| { + result.placement_id = v; + } + if (kv.get('s')) |v| { + // Kitty ignores values it doesn't know, leaving the animation + // state untouched, which is what `.invalid` means here. result.action = switch (v) { - 0 => .invalid, 1 => .stop, 2 => .run_wait, 3 => .run, - else => return error.InvalidFormat, + else => .invalid, }; } @@ -793,7 +986,10 @@ pub const AnimationControl = struct { } if (kv.get('z')) |v| { - result.gap_ms = v; + // 'z' is one of the keys Parser.finishValue parses as an + // i32, so the u32 in the KV table is the bit pattern of + // that i32 and this reconstructs it exactly. + result.gap_ms = @bitCast(v); } if (kv.get('c')) |v| { @@ -808,180 +1004,218 @@ pub const AnimationControl = struct { } }; -pub const Delete = union(enum) { - // a/A - all: bool, - - // i/I - id: struct { - delete: bool = false, // uppercase - image_id: u32 = 0, // i - placement_id: u32 = 0, // p - }, - - // n/N - newest: struct { - delete: bool = false, // uppercase - image_number: u32 = 0, // I - placement_id: u32 = 0, // p - }, - - // c/C, - intersect_cursor: bool, - - // f/F - animation_frames: bool, - - // p/P - intersect_cell: struct { - delete: bool = false, // uppercase - x: u32 = 0, // x - y: u32 = 0, // y - }, - - // q/Q - intersect_cell_z: struct { - delete: bool = false, // uppercase - x: u32 = 0, // x - y: u32 = 0, // y - z: i32 = 0, // z - }, - - // r/R - range: struct { - delete: bool = false, // uppercase - first: u32 = 0, // x - last: u32 = 0, // y - }, - - // x/X - column: struct { - delete: bool = false, // uppercase - x: u32 = 0, // x - }, - - // y/Y - row: struct { - delete: bool = false, // uppercase - y: u32 = 0, // y - }, - - // z/Z - z: struct { - delete: bool = false, // uppercase - z: i32 = 0, // z - }, +pub const Delete = struct { + image_id: u32 = 0, // i + image_number: u32 = 0, // I + placement_id: u32 = 0, // p + action: Action, fn parse(kv: KV) !Delete { - const what: u8 = what: { - const value = kv.get('d') orelse break :what 'a'; - const c = std.math.cast(u8, value) orelse return error.InvalidFormat; - break :what c; + return .{ + .image_id = kv.get('i') orelse 0, + .image_number = kv.get('I') orelse 0, + .placement_id = kv.get('p') orelse 0, + .action = try .parse(kv), }; + } - return switch (what) { - 'a', 'A' => .{ .all = what == 'A' }, + pub const Action = union(enum) { + // a/A + all: bool, - 'i', 'I' => blk: { - var result: Delete = .{ .id = .{ .delete = what == 'I' } }; - if (kv.get('i')) |v| { - result.id.image_id = v; - } - if (kv.get('p')) |v| { - result.id.placement_id = v; - } + // i/I + id: struct { + delete: bool = false, // uppercase + image_id: u32 = 0, // i + placement_id: u32 = 0, // p + }, - break :blk result; - }, + // n/N + newest: struct { + delete: bool = false, // uppercase + image_number: u32 = 0, // I + placement_id: u32 = 0, // p + }, - 'n', 'N' => blk: { - var result: Delete = .{ .newest = .{ .delete = what == 'N' } }; - if (kv.get('I')) |v| { - result.newest.image_number = v; - } - if (kv.get('p')) |v| { - result.newest.placement_id = v; - } + // c/C, + intersect_cursor: bool, - break :blk result; - }, + // f/F + animation_frames: AnimationFrames, - 'c', 'C' => .{ .intersect_cursor = what == 'C' }, + // p/P + intersect_cell: struct { + delete: bool = false, // uppercase + x: u32 = 0, // x + y: u32 = 0, // y + }, - 'f', 'F' => .{ .animation_frames = what == 'F' }, + // q/Q + intersect_cell_z: struct { + delete: bool = false, // uppercase + x: u32 = 0, // x + y: u32 = 0, // y + z: i32 = 0, // z + }, - 'p', 'P' => blk: { - var result: Delete = .{ .intersect_cell = .{ .delete = what == 'P' } }; - if (kv.get('x')) |v| { - result.intersect_cell.x = v; - } - if (kv.get('y')) |v| { - result.intersect_cell.y = v; - } + // r/R + range: struct { + delete: bool = false, // uppercase + first: u32 = 0, // x + last: u32 = 0, // y + }, - break :blk result; - }, + // x/X + column: struct { + delete: bool = false, // uppercase + x: u32 = 0, // x + }, - 'q', 'Q' => blk: { - var result: Delete = .{ .intersect_cell_z = .{ .delete = what == 'Q' } }; - if (kv.get('x')) |v| { - result.intersect_cell_z.x = v; - } - if (kv.get('y')) |v| { - result.intersect_cell_z.y = v; - } - if (kv.get('z')) |v| { - // We can bitcast here because of how we parse it earlier. - result.intersect_cell_z.z = @bitCast(v); - } + // y/Y + row: struct { + delete: bool = false, // uppercase + y: u32 = 0, // y + }, - break :blk result; - }, + // z/Z + z: struct { + delete: bool = false, // uppercase + z: i32 = 0, // z + }, - 'r', 'R' => blk: { - const x = kv.get('x') orelse return error.InvalidFormat; - const y = kv.get('y') orelse return error.InvalidFormat; - if (x > y) return error.InvalidFormat; - break :blk .{ - .range = .{ - .delete = what == 'R', - .first = x, - .last = y, - }, - }; - }, + pub const AnimationFrames = struct { + delete: bool = false, // uppercase + image_id: u32 = 0, // i + image_number: u32 = 0, // I + frame: u32 = 0, // r + }; - 'x', 'X' => blk: { - var result: Delete = .{ .column = .{ .delete = what == 'X' } }; - if (kv.get('x')) |v| { - result.column.x = v; - } + fn parse(kv: KV) error{InvalidFormat}!Action { + const what: u8 = what: { + const value = kv.get('d') orelse break :what 'a'; + const c = std.math.cast(u8, value) orelse return error.InvalidFormat; + break :what c; + }; - break :blk result; - }, + return switch (what) { + 'a', 'A' => .{ .all = what == 'A' }, - 'y', 'Y' => blk: { - var result: Delete = .{ .row = .{ .delete = what == 'Y' } }; - if (kv.get('y')) |v| { - result.row.y = v; - } + 'i', 'I' => blk: { + var result: Action = .{ .id = .{ .delete = what == 'I' } }; + if (kv.get('i')) |v| { + result.id.image_id = v; + } + if (kv.get('p')) |v| { + result.id.placement_id = v; + } - break :blk result; - }, + break :blk result; + }, - 'z', 'Z' => blk: { - var result: Delete = .{ .z = .{ .delete = what == 'Z' } }; - if (kv.get('z')) |v| { - // We can bitcast here because of how we parse it earlier. - result.z.z = @bitCast(v); - } + 'n', 'N' => blk: { + var result: Action = .{ .newest = .{ .delete = what == 'N' } }; + if (kv.get('I')) |v| { + result.newest.image_number = v; + } + if (kv.get('p')) |v| { + result.newest.placement_id = v; + } - break :blk result; - }, + break :blk result; + }, - else => return error.InvalidFormat, - }; - } + 'c', 'C' => .{ .intersect_cursor = what == 'C' }, + + 'f', 'F' => blk: { + var result: Action = .{ .animation_frames = .{ .delete = what == 'F' } }; + if (kv.get('i')) |v| { + result.animation_frames.image_id = v; + } + if (kv.get('I')) |v| { + result.animation_frames.image_number = v; + } + if (kv.get('r')) |v| { + result.animation_frames.frame = v; + } + + break :blk result; + }, + + 'p', 'P' => blk: { + var result: Action = .{ .intersect_cell = .{ .delete = what == 'P' } }; + if (kv.get('x')) |v| { + result.intersect_cell.x = v; + } + if (kv.get('y')) |v| { + result.intersect_cell.y = v; + } + + break :blk result; + }, + + 'q', 'Q' => blk: { + var result: Action = .{ .intersect_cell_z = .{ .delete = what == 'Q' } }; + if (kv.get('x')) |v| { + result.intersect_cell_z.x = v; + } + if (kv.get('y')) |v| { + result.intersect_cell_z.y = v; + } + if (kv.get('z')) |v| { + // We can bitcast here because of how we parse it earlier. + result.intersect_cell_z.z = @bitCast(v); + } + + break :blk result; + }, + + 'r', 'R' => blk: { + // Both bounds default to zero when omitted and no + // validation is performed; an inverted or zero range + // simply matches no images. + var result: Action = .{ .range = .{ .delete = what == 'R' } }; + if (kv.get('x')) |v| { + result.range.first = v; + } + if (kv.get('y')) |v| { + result.range.last = v; + } + + break :blk result; + }, + + 'x', 'X' => blk: { + var result: Action = .{ .column = .{ .delete = what == 'X' } }; + if (kv.get('x')) |v| { + result.column.x = v; + } + + break :blk result; + }, + + 'y', 'Y' => blk: { + var result: Action = .{ .row = .{ .delete = what == 'Y' } }; + if (kv.get('y')) |v| { + result.row.y = v; + } + + break :blk result; + }, + + 'z', 'Z' => blk: { + var result: Action = .{ .z = .{ .delete = what == 'Z' } }; + if (kv.get('z')) |v| { + // We can bitcast here because of how we parse it earlier. + result.z.z = @bitCast(v); + } + + break :blk result; + }, + + else => return error.InvalidFormat, + }; + } + }; }; pub const CompositionMode = enum { @@ -1005,6 +1239,26 @@ test "transmission command" { try testing.expectEqual(Transmission.Format.rgb, v.format); try testing.expectEqual(@as(u32, 10), v.width); try testing.expectEqual(@as(u32, 20), v.height); + try testing.expectEqual(false, v.usage.transient); +} + +test "transmission command with transient hint" { + const testing = std.testing; + const alloc = testing.allocator; + var p = Parser.init(alloc, 1024 * 1024); + defer p.deinit(); + + const input = "f=24,s=10,v=20,N=1"; + for (input) |c| try p.feed(c); + const command = try p.complete(alloc); + defer command.deinit(alloc); + + try testing.expect(command.control == .transmit); + const v = command.control.transmit; + try testing.expectEqual(Transmission.Format.rgb, v.format); + try testing.expectEqual(@as(u32, 10), v.width); + try testing.expectEqual(@as(u32, 20), v.height); + try testing.expectEqual(true, v.usage.transient); } test "feedSlice matches per-byte feed" { @@ -1142,7 +1396,7 @@ test "delete command" { defer command.deinit(alloc); try testing.expect(command.control == .delete); - const v = command.control.delete; + const v = command.control.delete.action; try testing.expect(v == .intersect_cell); const dv = v.intersect_cell; try testing.expect(!dv.delete); @@ -1183,6 +1437,24 @@ test "ignore unknown keys (long)" { try testing.expectEqual(@as(u32, 20), v.height); } +test "ignore unknown keys (non-letter)" { + const testing = std.testing; + const alloc = testing.allocator; + var p = Parser.init(alloc, 1024 * 1024); + defer p.deinit(); + + const input = "f=24,s=10,v=20,!=1"; + for (input) |c| try p.feed(c); + const command = try p.complete(alloc); + defer command.deinit(alloc); + + try testing.expect(command.control == .transmit); + const v = command.control.transmit; + try testing.expectEqual(Transmission.Format.rgb, v.format); + try testing.expectEqual(@as(u32, 10), v.width); + try testing.expectEqual(@as(u32, 20), v.height); +} + test "ignore very long values" { const testing = std.testing; const alloc = testing.allocator; @@ -1342,7 +1614,7 @@ test "delete range command 1" { defer command.deinit(alloc); try testing.expect(command.control == .delete); - const v = command.control.delete; + const v = command.control.delete.action; try testing.expect(v == .range); const range = v.range; try testing.expect(!range.delete); @@ -1362,7 +1634,7 @@ test "delete range command 2" { defer command.deinit(alloc); try testing.expect(command.control == .delete); - const v = command.control.delete; + const v = command.control.delete.action; try testing.expect(v == .range); const range = v.range; try testing.expect(range.delete); @@ -1371,6 +1643,9 @@ test "delete range command 2" { } test "delete range command 3" { + // An inverted range is accepted; it simply matches no images. This + // matches kitty, which does no validation and filters with + // `x <= image_id <= y`. const testing = std.testing; const alloc = testing.allocator; var p = Parser.init(alloc, 1024 * 1024); @@ -1378,10 +1653,21 @@ test "delete range command 3" { const input = "a=d,d=R,x=5,y=4"; for (input) |c| try p.feed(c); - try testing.expectError(error.InvalidFormat, p.complete(alloc)); + const command = try p.complete(alloc); + defer command.deinit(alloc); + + try testing.expect(command.control == .delete); + const v = command.control.delete.action; + try testing.expect(v == .range); + const range = v.range; + try testing.expect(range.delete); + try testing.expectEqual(@as(u32, 5), range.first); + try testing.expectEqual(@as(u32, 4), range.last); } test "delete range command 4" { + // An omitted upper bound defaults to zero, which matches no images + // since image IDs are always non-zero. const testing = std.testing; const alloc = testing.allocator; var p = Parser.init(alloc, 1024 * 1024); @@ -1389,7 +1675,16 @@ test "delete range command 4" { const input = "a=d,d=R,x=5"; for (input) |c| try p.feed(c); - try testing.expectError(error.InvalidFormat, p.complete(alloc)); + const command = try p.complete(alloc); + defer command.deinit(alloc); + + try testing.expect(command.control == .delete); + const v = command.control.delete.action; + try testing.expect(v == .range); + const range = v.range; + try testing.expect(range.delete); + try testing.expectEqual(@as(u32, 5), range.first); + try testing.expectEqual(@as(u32, 0), range.last); } test "delete range command 5" { @@ -1400,5 +1695,178 @@ test "delete range command 5" { const input = "a=d,d=R,y=5"; for (input) |c| try p.feed(c); - try testing.expectError(error.InvalidFormat, p.complete(alloc)); + const command = try p.complete(alloc); + defer command.deinit(alloc); + + try testing.expect(command.control == .delete); + const v = command.control.delete.action; + try testing.expect(v == .range); + const range = v.range; + try testing.expect(range.delete); + try testing.expectEqual(@as(u32, 0), range.first); + try testing.expectEqual(@as(u32, 5), range.last); +} + +test "delete range command 6" { + // Both bounds omitted defaults to the empty range [0, 0]. + const testing = std.testing; + const alloc = testing.allocator; + var p = Parser.init(alloc, 1024 * 1024); + defer p.deinit(); + + const input = "a=d,d=r"; + for (input) |c| try p.feed(c); + const command = try p.complete(alloc); + defer command.deinit(alloc); + + try testing.expect(command.control == .delete); + const v = command.control.delete.action; + try testing.expect(v == .range); + const range = v.range; + try testing.expect(!range.delete); + try testing.expectEqual(@as(u32, 0), range.first); + try testing.expectEqual(@as(u32, 0), range.last); +} + +// Kitty range-checks non-flag keys while handling the command, not while +// parsing, so a bad value never drops the command (which would make a +// response impossible even when the client gave an id). The flag keys +// "a", "d", "t" and "o" are the exception. + +test "unknown format value is deferred to execution" { + const testing = std.testing; + const alloc = testing.allocator; + + const command = try Parser.parseString(alloc, "a=t,f=42,i=31,s=1,v=1"); + defer command.deinit(alloc); + + try testing.expect(command.control == .transmit); + try testing.expect(command.control.transmit.format_unknown); + try testing.expectEqual(@as(u32, 31), command.control.transmit.image_id); +} + +test "zero format value is rgba" { + const testing = std.testing; + const alloc = testing.allocator; + + const command = try Parser.parseString(alloc, "a=t,f=0,i=31,s=1,v=1"); + defer command.deinit(alloc); + + try testing.expect(command.control == .transmit); + const v = command.control.transmit; + try testing.expect(!v.format_unknown); + try testing.expectEqual(Transmission.Format.rgba, v.format); +} + +test "known format values are not unknown" { + const testing = std.testing; + const alloc = testing.allocator; + + for ([_]struct { []const u8, Transmission.Format }{ + .{ "f=24", .rgb }, + .{ "f=32", .rgba }, + .{ "f=100", .png }, + }) |entry| { + const command = try Parser.parseString(alloc, entry[0]); + defer command.deinit(alloc); + + const v = command.control.transmit; + try testing.expect(!v.format_unknown); + try testing.expectEqual(entry[1], v.format); + } +} + +test "quiet value above two suppresses all responses" { + const testing = std.testing; + const alloc = testing.allocator; + + const command = try Parser.parseString(alloc, "a=t,q=3,i=31,s=1,v=1"); + defer command.deinit(alloc); + + try testing.expectEqual(Command.Quiet.failures, command.quiet); +} + +test "cursor movement value above one moves the cursor" { + const testing = std.testing; + const alloc = testing.allocator; + + const command = try Parser.parseString(alloc, "a=p,i=31,C=2"); + defer command.deinit(alloc); + + try testing.expect(command.control == .display); + try testing.expectEqual( + Display.CursorMovement.after, + command.control.display.cursor_movement, + ); +} + +test "virtual placement value above one is virtual" { + const testing = std.testing; + const alloc = testing.allocator; + + const command = try Parser.parseString(alloc, "a=p,i=31,U=2"); + defer command.deinit(alloc); + + try testing.expect(command.control == .display); + try testing.expect(command.control.display.virtual_placement); +} + +test "animation frame composition value above one alpha blends" { + const testing = std.testing; + const alloc = testing.allocator; + + const command = try Parser.parseString(alloc, "a=f,i=31,X=2"); + defer command.deinit(alloc); + + try testing.expect(command.control == .transmit_animation_frame); + try testing.expectEqual( + CompositionMode.alpha_blend, + command.control.transmit_animation_frame.composition_mode, + ); +} + +test "animation compose value above one overwrites" { + const testing = std.testing; + const alloc = testing.allocator; + + const command = try Parser.parseString(alloc, "a=c,i=31,C=2"); + defer command.deinit(alloc); + + try testing.expect(command.control == .compose_animation); + try testing.expectEqual( + CompositionMode.overwrite, + command.control.compose_animation.composition_mode, + ); +} + +test "animation control action above three is ignored" { + const testing = std.testing; + const alloc = testing.allocator; + + const command = try Parser.parseString(alloc, "a=a,i=31,s=4"); + defer command.deinit(alloc); + + try testing.expect(command.control == .control_animation); + try testing.expectEqual( + AnimationControl.AnimationAction.invalid, + command.control.control_animation.action, + ); +} + +test "unknown flag key values still fail parsing" { + const testing = std.testing; + const alloc = testing.allocator; + + // Kitty rejects these while parsing too. See gen/apc_parsers.py. + for ([_][]const u8{ + "a=z,i=31", // action + "a=d,d=w,i=31", // delete action + "a=t,t=q,i=31", // transmission medium + "a=t,o=q,i=31", // compression + }) |input| { + try testing.expectError( + error.InvalidFormat, + Parser.parseString(alloc, input), + ); + } } diff --git a/vendor/libghostty-vt/src/terminal/kitty/graphics_exec.zig b/vendor/libghostty-vt/src/terminal/kitty/graphics_exec.zig index 6c72ec4e..b1b968b2 100644 --- a/vendor/libghostty-vt/src/terminal/kitty/graphics_exec.zig +++ b/vendor/libghostty-vt/src/terminal/kitty/graphics_exec.zig @@ -5,6 +5,8 @@ const Allocator = std.mem.Allocator; const Terminal = @import("../Terminal.zig"); const command = @import("graphics_command.zig"); const image = @import("graphics_image.zig"); +const animation = @import("graphics_animation.zig"); +const pixel = @import("graphics_pixel.zig"); const Command = command.Command; const Response = command.Response; const LoadingImage = image.LoadingImage; @@ -21,6 +23,7 @@ const log = std.log.scoped(.kitty_gfx); /// The allocator must be the same allocator that was used to build /// the command. pub fn execute( + io: std.Io, alloc: Allocator, terminal: *Terminal, cmd: *const Command, @@ -43,12 +46,32 @@ pub fn execute( // this can change. var quiet = cmd.quiet; + // The protocol makes i and I mutually exclusive for every action, so this + // must happen before dispatch and before an action can mutate storage. + // https://sw.kovidgoyal.net/kitty/graphics-protocol/#requesting-image-ids-from-the-terminal + const identifiers = cmd.control.identifiers(); + if (identifiers.image_id > 0 and identifiers.image_number > 0) { + const resp: Response = .{ + .id = identifiers.image_id, + .image_number = identifiers.image_number, + .placement_id = identifiers.placement_id, + .message = "EINVAL: image ID and number are mutually exclusive", + }; + log.warn("erroneous kitty graphics response: {s}", .{resp.message}); + + return switch (quiet) { + .no => resp, + .ok => resp, + .failures => null, + }; + } + const resp_: ?Response = switch (cmd.control) { - .query => query(alloc, terminal, cmd), - .display => display(alloc, terminal, cmd), - .delete => delete(alloc, terminal, cmd), + .query => query(io, alloc, terminal, cmd), + .display => display(io, alloc, terminal, cmd), + .delete => delete(io, alloc, terminal, cmd), - .transmit, .transmit_and_display => resp: { + .transmit, .transmit_and_display, .transmit_animation_frame => resp: { // If we're transmitting, then our `q` setting value is complicated. // The `q` setting inherits the value from the starting command // unless `q` is set >= 1 on this command. If it is, then we save @@ -65,13 +88,35 @@ pub fn execute( }, }; - break :resp transmit(alloc, terminal, cmd); + break :resp switch (cmd.control) { + .transmit_animation_frame => transmitAnimationFrame( + io, + alloc, + terminal, + cmd, + ), + else => transmit( + io, + alloc, + terminal, + cmd, + ), + }; }, - .transmit_animation_frame, - .control_animation, - .compose_animation, - => .{ .message = "ERROR: unimplemented action" }, + .control_animation => controlAnimation( + io, + alloc, + terminal, + cmd, + ), + + .compose_animation => composeAnimation( + io, + alloc, + terminal, + cmd, + ), }; // Handle the quiet settings @@ -89,12 +134,14 @@ pub fn execute( return null; } + /// Execute a "query" command. /// /// This command is used to attempt to load an image and respond with /// success/error but does not persist any of the command to the terminal /// state. fn query( + io: std.Io, alloc: Allocator, terminal: *const Terminal, cmd: *const Command, @@ -115,13 +162,21 @@ fn query( .placement_id = t.placement_id, }; - // Attempt to load the image. If we cannot, then set an appropriate error. + // A query must attempt a complete load, then discard the result without + // changing image storage. + // https://sw.kovidgoyal.net/kitty/graphics-protocol/#querying-support-and-available-transmission-mediums const storage = &terminal.screens.active.kitty_images; - var loading = LoadingImage.init(alloc, cmd, storage.image_limits) catch |err| { + var loading = LoadingImage.init(io, alloc, cmd, storage.image_limits) catch |err| { encodeError(&result, err); return result; }; - loading.deinit(alloc); + defer loading.deinit(alloc); + + var img = loading.complete(alloc) catch |err| { + encodeError(&result, err); + return result; + }; + img.deinit(alloc); return result; } @@ -131,21 +186,35 @@ fn query( /// This loads the image, validates it, and puts it into the terminal /// screen storage. It does not display the image. fn transmit( + io: std.Io, alloc: Allocator, terminal: *Terminal, cmd: *const Command, ) Response { const t = cmd.transmission().?; - var result: Response = .{ + const storage = &terminal.screens.active.kitty_images; + + var result: Response = if (storage.loading) |loading| loading: { + // Any transmit-like command received while a load is in progress + // is a continuation chunk of that load, and the load completes + // according to how it started. If an animation frame load (a=f) + // is in progress, this chunk belongs to it even when the client + // didn't repeat a=f on the chunk. + if (loading.frame != null) return transmitAnimationFrame( + io, + alloc, + terminal, + cmd, + ); + + break :loading loading.response; + } else .{ .id = t.image_id, .image_number = t.image_number, .placement_id = t.placement_id, }; - if (t.image_id > 0 and t.image_number > 0) { - return .{ .message = "EINVAL: image ID and number are mutually exclusive" }; - } - const load = loadAndAddImage(alloc, terminal, cmd) catch |err| { + const load = loadAndAddImage(io, alloc, terminal, cmd) catch |err| { encodeError(&result, err); return result; }; @@ -158,7 +227,7 @@ fn transmit( assert(!load.more); var d_copy = d; d_copy.image_id = load.image.id; - result = display(alloc, terminal, &.{ + result = display(io, alloc, terminal, &.{ .control = .{ .display = d_copy }, .quiet = cmd.quiet, }); @@ -169,7 +238,7 @@ fn transmit( // If the loaded image was assigned its ID automatically, not based // on a number or explicitly specified ID, then we don't respond. - if (load.image.implicit_id) return .{}; + if (load.image.metadata.implicit_id) return .{}; // After the image is added, set the ID in case it changed. // The resulting image number and placement ID never change. @@ -180,6 +249,7 @@ fn transmit( /// Display a previously transmitted image. fn display( + io: std.Io, alloc: Allocator, terminal: *Terminal, cmd: *const Command, @@ -198,6 +268,13 @@ fn display( .placement_id = d.placement_id, }; + // A virtual placement (U=1) cannot also be a relative placement. + // Kitty checks this before even looking up the image. + if (d.virtual_placement and d.parent_id > 0) { + result.message = "EINVAL: virtual placement cannot refer to a parent"; + return result; + } + // Verify the requested image exists if we have an ID const storage = &terminal.screens.active.kitty_images; const img_: ?Image = if (d.image_id != 0) @@ -215,42 +292,84 @@ fn display( // Location where the placement will go. const location: ImageStorage.Placement.Location = location: { // Virtual placements are not tracked - if (d.virtual_placement) { - if (d.parent_id > 0) { - result.message = "EINVAL: virtual placement cannot refer to a parent"; + if (d.virtual_placement) break :location .virtual; + + // No parent reference (P=): the placement is pinned to the + // cursor. The cursor is always tracked but we don't want + // this pin to move with the cursor. + if (d.parent_id == 0) { + const pin = terminal.screens.active.pages.trackPin( + terminal.screens.active.cursor.page_pin.*, + ) catch |err| { + log.warn("failed to create pin for Kitty graphics err={}", .{err}); + result.message = "EINVAL: failed to prepare terminal state"; return result; - } - - break :location .{ .virtual = {} }; + }; + break :location .{ .pin = pin }; } - // Track a new pin for our cursor. The cursor is always tracked but we - // don't want this one to move with the cursor. - const pin = terminal.screens.active.pages.trackPin( - terminal.screens.active.cursor.page_pin.*, + // A parent reference makes this a relative placement: it is + // positioned relative to the parent placement instead of the + // cursor. + + // The key of the placement being created, when it is + // addressable (an explicit placement ID). Needed for + // self-parent and cycle detection. + const child: ?ImageStorage.PlacementKey = if (d.placement_id > 0) .{ + .image_id = img.id, + .placement_id = .{ .tag = .external, .id = d.placement_id }, + } else null; + + const parent = storage.resolveParent( + io, + terminal.screens.active, + child, + d.parent_id, + d.parent_placement_id, ) catch |err| { - log.warn("failed to create pin for Kitty graphics err={}", .{err}); - result.message = "EINVAL: failed to prepare terminal state"; + result.message = switch (err) { + error.ParentImageNotFound => "ENOPARENT: parent image not found", + error.ParentPlacementNotFound => "ENOPARENT: parent placement not found", + error.SelfParent => "EINVAL: placement cannot be its own parent", + error.Cycle => "ECYCLE: parent chain creates a cycle", + error.TooDeep => "ETOODEEP: parent chain too deep", + error.AncestorNotFound => "ENOENT: parent chain ancestor not found", + }; return result; }; - break :location .{ .pin = pin }; + + break :location .{ .relative = .{ + .parent = parent, + .horizontal_offset = d.horizontal_offset, + .vertical_offset = d.vertical_offset, + } }; }; // Add the placement - const p: ImageStorage.Placement = .{ - .location = location, - .x_offset = d.x_offset, - .y_offset = d.y_offset, - .source_x = d.x, - .source_y = d.y, - .source_width = d.width, - .source_height = d.height, - .columns = d.columns, - .rows = d.rows, - .z = d.z, + const p: ImageStorage.Placement = placement: { + var p: ImageStorage.Placement = .{ + .location = location, + .x_offset = d.x_offset, + .y_offset = d.y_offset, + .source_x = d.x, + .source_y = d.y, + .source_width = d.width, + .source_height = d.height, + .columns = d.columns, + .rows = d.rows, + .z = d.z, + }; + + const cell_offset = p.cellOffset(terminal); + if (terminal.width_px / terminal.cols > 0) p.x_offset = cell_offset.x; + if (terminal.height_px / terminal.rows > 0) p.y_offset = cell_offset.y; + + break :placement p; }; storage.addPlacement( + io, alloc, + terminal.screens.active, img.id, result.placement_id, p, @@ -260,22 +379,54 @@ fn display( return result; }; - // Apply cursor movement setting. This only applies to pin placements. + // Apply cursor movement setting. This only applies to pin placements: + // relative placements never move the cursor regardless of C=, just + // like kitty. switch (p.location) { - .virtual => {}, + .virtual, .relative => {}, .pin => |pin| switch (d.cursor_movement) { .none => {}, .after => { // We use terminal.index to properly handle scroll regions. + const screen = terminal.screens.active; const size = p.gridSize(img, terminal); - for (0..size.rows) |_| terminal.index() catch |err| { + const target_x = @as(usize, pin.x) +| @as(usize, size.cols); + const wraps = target_x >= @as(usize, terminal.cols); + const requested_rows = + (@as(usize, size.rows) -| 1) +| @intFromBool(wraps); + + // The requested row count comes from the application and can + // be as large as u32. Calling terminal.index once for every + // row could therefore leave the terminal unresponsive for a + // very long time. + // + // First allow enough calls to reach the bottom of the scroll + // region. Each call after that scrolls the region by one row, + // so limit those extra calls to one screen. This follows + // Kitty's behavior while keeping the amount of work bounded. + const region = terminal.scrolling_region; + const rows_before_scroll: usize = if (screen.cursor.y >= region.top and + screen.cursor.y <= region.bottom and + screen.cursor.x >= region.left and + screen.cursor.x <= region.right) + @as(usize, region.bottom - screen.cursor.y) + else + 0; + const rows_to_move: usize = @min( + requested_rows, + rows_before_scroll +| @as(usize, terminal.rows), + ); + for (0..rows_to_move) |_| terminal.index() catch |err| { log.warn("failed to move cursor: {}", .{err}); break; }; - terminal.setCursorPos( - terminal.screens.active.cursor.y, - pin.x + size.cols + 1, + // Kitty wraps once when the movement reaches the right edge; + // otherwise it leaves the cursor immediately to the right of + // the placement. + screen.cursor.pending_wrap = false; + screen.cursorHorizontalAbsolute( + if (wraps) 0 else @intCast(target_x), ); }, }, @@ -284,20 +435,556 @@ fn display( return result; } +/// Transmit animation frame data (a=f). +/// +/// Frame data is loaded exactly like image data, including chunking +/// and every transmission medium, and once complete it is composed +/// into a frame of an existing image's animation. +fn transmitAnimationFrame( + io: std.Io, + alloc: Allocator, + terminal: *Terminal, + cmd: *const Command, +) Response { + const storage = &terminal.screens.active.kitty_images; + + // A chunk arriving while a load is in progress continues that load. + if (storage.loading) |loading| { + // If this is the first frame, then we treat it like a normal image + // because the first frame is just the image. + if (loading.frame == null) return transmit( + io, + alloc, + terminal, + cmd, + ); + + // Not the first frame, so continue loading similar to transmit + // but this is for a subsequent frame. + var result = loading.response; + loading.addData(alloc, cmd.data) catch |err| { + encodeError(&result, err); + return result; + }; + + // If more chunks are expected we don't respond yet. + const t = cmd.transmission().?; + if (t.more_chunks) return .{}; + + // Final chunk: copy the loading state out and free the + // pointer, mirroring loadAndAddImage. + var loading_copy = loading.*; + alloc.destroy(loading); + storage.loading = null; + defer loading_copy.deinit(alloc); + return completeAnimationFrame( + io, + alloc, + terminal, + &loading_copy, + ); + } + + // We are starting our load, either the first frame or a subsequent frame. + const f = cmd.control.transmit_animation_frame; + const t = f.transmission; + var result: Response = .{ + .id = t.image_id, + .image_number = t.image_number, + .placement_id = t.placement_id, + // Errors echo the client's frame number; on success this is + // replaced with the resolved (possibly newly assigned) one. + .frame = f.edit_frame, + }; + + // A frame can only be added to an existing image. + if (t.image_id == 0 and t.image_number == 0) { + result.message = "EINVAL: image ID or number required"; + return result; + } + const img = storage.imagePtrByIdOrNumber( + t.image_id, + t.image_number, + ) orelse { + result.message = "ENOENT: image not found"; + return result; + }; + result.id = img.id; + + var loading = LoadingImage.init( + io, + alloc, + cmd, + storage.image_limits, + ) catch |err| { + encodeError(&result, err); + return result; + }; + loading.frame = .{ + .cmd = f, + .image_generation = img.generation, + }; + loading.response.id = img.id; + + // Chunked: store the loading state and wait for the rest. The + // frame parameters above are saved with it; continuation chunks + // contribute only payload bytes. + if (t.more_chunks) { + const loading_ptr = alloc.create(LoadingImage) catch |err| { + loading.deinit(alloc); + encodeError(&result, err); + return result; + }; + loading_ptr.* = loading; + storage.loading = loading_ptr; + return .{}; + } + + defer loading.deinit(alloc); + return completeAnimationFrame( + io, + alloc, + terminal, + &loading, + ); +} + +/// Complete a fully loaded animation frame: validate it against its +/// image and compose it into the image's animation. +fn completeAnimationFrame( + io: std.Io, + alloc: Allocator, + terminal: *Terminal, + loading: *LoadingImage, +) Response { + const storage = &terminal.screens.active.kitty_images; + var result = loading.response; + const f = loading.frame.?.cmd; + + // Re-resolve the image: it may have been deleted, evicted, or + // replaced while the frame data was being transmitted. The saved + // generation pins the exact image the load started against. + var img = storage.imagePtrByIdOrNumber( + loading.image.id, + loading.image.number, + ) orelse { + result.message = "ENOENT: image not found"; + return result; + }; + if (img.generation != loading.frame.?.image_generation) { + result.message = "ENOENT: image not found"; + return result; + } + if (img.data.bytes() == null) { + result.message = "EINVAL: image data incomplete"; + return result; + } + + // Finish decoding the frame data: decompression, PNG decoding, + // and length validation all match image loading. + var frame_img = loading.complete(alloc) catch |err| { + encodeError(&result, err); + return result; + }; + defer frame_img.deinit(alloc); + + // The frame rectangle may not exceed the image's size. The x/y + // offsets are deliberately not validated: composition clips, + // matching Kitty. + if (frame_img.width > img.width or frame_img.height > img.height) { + result.message = "EINVAL: frame dimensions exceed image"; + return result; + } + + // All composition happens in RGBA; convert both sides as needed. + if (frame_img.format != .rgba) { + const rgba = pixel.rgbaFromFormat( + alloc, + frame_img.format, + frame_img.data.bytes().?, + ) catch |err| { + encodeError(&result, err); + return result; + }; + frame_img.data.deinit(alloc); + frame_img.data = .{ .complete = rgba }; + frame_img.format = .rgba; + } + storage.convertImageToRgba(io, alloc, img) catch |err| { + encodeError(&result, err); + return result; + }; + + const anim = ensureAnimation(alloc, img) catch |err| { + encodeError(&result, err); + return result; + }; + + // Resolve the frame number: r in 1..count edits that existing + // frame, anything else (including omitted) creates a new frame + // appended at count+1. The resolved number is echoed in the + // response so clients can learn assigned frame numbers. + const count: u32 = anim.frameCount(); + const number: u32 = number: { + const r = f.edit_frame; + if (r == 0 or r > count + 1) break :number count + 1; + break :number r; + }; + result.frame = number; + + const src = frame_img.data.bytes().?; + if (number == count + 1) { + // Creating a new frame. The gap defaults to 40ms when omitted + // and a negative gap creates a gapless (never shown) frame. + const gap: u32 = if (f.gap_ms > 0) + @intCast(f.gap_ms) + else if (f.gap_ms < 0) + 0 + else + animation.default_gap_ms; + + // The base canvas frame must exist before we reserve space. + if (f.create_frame > 0 and img.frameData(f.create_frame) == null) { + result.message = "EINVAL: base frame not found"; + return result; + } + + // Reserve room for the new frame, evicting other images if + // needed. Eviction can remove images (never this one), so we + // re-resolve our pointer afterwards to be safe. + const frame_len: usize = @as(usize, img.width) * img.height * 4; + const image_id = img.id; + storage.reserveAnimationBytes( + io, + alloc, + terminal.screens.active, + image_id, + frame_len, + ) catch { + result.message = "ENOSPC: animation frame storage full"; + return result; + }; + img = storage.imagePtrByIdOrNumber(image_id, 0).?; + + const canvas = alloc.alloc(u8, frame_len) catch { + storage.releaseAnimationBytes(frame_len); + result.message = "ENOMEM: out of memory"; + return result; + }; + if (f.create_frame > 0) { + @memcpy(canvas, img.frameData(f.create_frame).?); + } else { + pixel.fillBackground(canvas, f.background); + } + pixel.composeRect( + canvas, + img.width, + img.height, + src, + frame_img.width, + frame_img.height, + f.x, + f.y, + f.composition_mode, + ); + + anim.frames.append(alloc, .{ + .data = canvas, + .gap_ms = gap, + }) catch { + alloc.free(canvas); + storage.releaseAnimationBytes(frame_len); + result.message = "ENOMEM: out of memory"; + return result; + }; + + // A new frame never changes the displayed frame (playback + // reaches it later), but the storage content changed. + storage.markMutated(io); + } else { + // Editing an existing frame. A nonzero gap also updates the + // frame's gap; the 40ms default doesn't apply to edits. + // + // Unlike frame creation there is no byte reservation here: + // frames are always stored at full image size, so the edit + // composes into the existing buffer in place and storage + // usage cannot change. Kitty likewise exempts frame edits + // from its quota check. + if (f.gap_ms != 0) anim.setGapAt( + number - 1, + if (f.gap_ms > 0) @intCast(f.gap_ms) else 0, + ); + + // The frame data is owned by this storage, so the const cast + // is safe (same reasoning as the renderer's image uploads). + const dst = @constCast(img.frameData(number).?); + pixel.composeRect( + dst, + img.width, + img.height, + src, + frame_img.width, + frame_img.height, + f.x, + f.y, + f.composition_mode, + ); + + if (number - 1 == anim.current_index) { + // The displayed pixels changed; restart the frame's gap + // timer like Kitty's re-upload does. + anim.frame_shown_at_ms = null; + storage.markImageContentChanged(io, img); + } else { + storage.markMutated(io); + } + } + + return result; +} + +/// Control animation playback (a=a). +/// +/// Successful commands never respond. The only possible responses are +/// for a missing image or identifier. Invalid key values are silently +/// ignored, matching Kitty. +fn controlAnimation( + io: std.Io, + alloc: Allocator, + terminal: *Terminal, + cmd: *const Command, +) Response { + const a = cmd.control.control_animation; + const storage = &terminal.screens.active.kitty_images; + + var result: Response = .{ + .id = a.image_id, + .image_number = a.image_number, + .placement_id = a.placement_id, + }; + if (a.image_id == 0 and a.image_number == 0) { + result.message = "EINVAL: image ID or number required"; + return result; + } + const img = storage.imagePtrByIdOrNumber( + a.image_id, + a.image_number, + ) orelse { + result.message = "ENOENT: image not found"; + return result; + }; + + const anim = ensureAnimation(alloc, img) catch |err| { + encodeError(&result, err); + return result; + }; + + // The keys below are applied independently and in the same order as Kitty. + + // Set a frame's gap (r= together with z=). This is the only way + // to give the root frame a gap, since it is created gapless. + if (a.frame != 0 and a.frame <= anim.frameCount() and a.gap_ms != 0) { + anim.setGapAt( + a.frame - 1, + if (a.gap_ms > 0) @intCast(a.gap_ms) else 0, + ); + storage.markMutated(io); + } + + // Set the current frame (c=), the client-driven animation + // primitive. + if (a.current_frame != 0 and a.current_frame <= anim.frameCount() and + a.current_frame - 1 != anim.current_index) + { + anim.current_index = a.current_frame - 1; + anim.frame_shown_at_ms = null; + storage.markImageContentChanged(io, img); + } + + // Set the playback state (s=). Any state change resets the loop + // counter; leaving the stopped state restarts the gap timer. + if (a.action != .invalid) { + const old = anim.state; + anim.state = switch (a.action) { + .invalid => unreachable, + .stop => .stopped, + .run_wait => .loading, + .run => .running, + }; + if (old == .stopped and anim.state != .stopped) { + anim.frame_shown_at_ms = null; + } + anim.current_loop = 0; + storage.markMutated(io); + } + + // Set the loop count (v=), stored off by one per the protocol: + // v=1 loops forever, v=n plays n-1 loops. + if (a.loops != 0) { + anim.max_loops = a.loops - 1; + storage.markMutated(io); + } + + // Successful animation control commands never respond. + return .{}; +} + +/// Compose a rectangle of pixels from one animation frame onto +/// another (a=c). Frame 1 (the root frame) always exists, so this also works +/// on images without any animation state. +fn composeAnimation( + io: std.Io, + alloc: Allocator, + terminal: *Terminal, + cmd: *const Command, +) Response { + const c = cmd.control.compose_animation; + const storage = &terminal.screens.active.kitty_images; + + var result: Response = .{ + .id = c.image_id, + .image_number = c.image_number, + .placement_id = c.placement_id, + }; + if (c.image_id == 0 and c.image_number == 0) { + result.message = "EINVAL: image ID or number required"; + return result; + } + const img = storage.imagePtrByIdOrNumber( + c.image_id, + c.image_number, + ) orelse { + result.message = "ENOENT: image not found"; + return result; + }; + result.id = img.id; + if (img.data.bytes() == null) { + result.message = "EINVAL: image data incomplete"; + return result; + } + + // Both frames must exist. Note that r is the source frame and c + // the destination; the spec's reference table describes these + // backwards (see AnimationFrameComposition). + if (img.frameData(c.source_frame) == null) { + result.message = "ENOENT: source frame not found"; + return result; + } + if (img.frameData(c.dest_frame) == null) { + result.message = "ENOENT: destination frame not found"; + return result; + } + + // Rectangle validation is done in u64 so untrusted 32-bit values + // can't overflow. Out-of-bounds rectangles are errors here, + // unlike a=f which clips. + const width: u64 = if (c.width > 0) c.width else img.width; + const height: u64 = if (c.height > 0) c.height else img.height; + if (@as(u64, c.x) + width > img.width or + @as(u64, c.y) + height > img.height) + { + result.message = "EINVAL: destination rectangle out of bounds"; + return result; + } + if (@as(u64, c.left_edge) + width > img.width or + @as(u64, c.top_edge) + height > img.height) + { + result.message = "EINVAL: source rectangle out of bounds"; + return result; + } + + // Composing a frame onto itself requires non-overlapping + // rectangles. + if (c.source_frame == c.dest_frame) { + const x_overlaps = @max(c.left_edge, c.x) < @min(c.left_edge, c.x) + width; + const y_overlaps = @max(c.top_edge, c.y) < @min(c.top_edge, c.y) + height; + if (x_overlaps and y_overlaps) { + result.message = "EINVAL: source and destination rectangles overlap"; + return result; + } + } + + // All composition happens in RGBA. + storage.convertImageToRgba( + io, + alloc, + img, + ) catch |err| { + encodeError(&result, err); + return result; + }; + + // The frame data is owned by this storage, so the const cast is + // safe. The rectangles were validated disjoint above, so in-place + // composition within one frame is well-defined. + const src = img.frameData(c.source_frame).?; + const dst = @constCast(img.frameData(c.dest_frame).?); + pixel.composeCanvasRect( + dst, + src, + img.width, + @intCast(width), + @intCast(height), + c.left_edge, + c.top_edge, + c.x, + c.y, + c.composition_mode, + ); + + // If the destination is the displayed frame then the on-screen + // content changed. + const current: u32 = if (img.animation) |anim| anim.current_index else 0; + if (c.dest_frame - 1 == current) { + storage.markImageContentChanged(io, img); + } else { + storage.markMutated(io); + } + + return result; +} + +/// Get or lazily create the animation state for an image. +fn ensureAnimation( + alloc: Allocator, + img: *Image, +) Allocator.Error!*animation.Animation { + if (img.animation) |anim| return anim; + const anim = try alloc.create(animation.Animation); + anim.* = .{}; + img.animation = anim; + return anim; +} + /// Display a previously transmitted image. fn delete( + io: std.Io, alloc: Allocator, terminal: *Terminal, cmd: *const Command, ) Response { const storage = &terminal.screens.active.kitty_images; - storage.delete(alloc, terminal, cmd.control.delete); + + // Every delete command aborts an incomplete chunked upload. + if (storage.loading) |loading| { + loading.destroy(alloc); + storage.loading = null; + } + + // Then perform the actual deletion request, too. + storage.delete( + io, + alloc, + terminal, + cmd.control.delete.action, + ); // Delete never responds on success return .{}; } fn loadAndAddImage( + io: std.Io, alloc: Allocator, terminal: *Terminal, cmd: *const Command, @@ -327,7 +1014,18 @@ fn loadAndAddImage( } break :loading loading.*; - } else try .init(alloc, cmd, storage.image_limits); + } else loading: { + // Reusing a specific image ID deletes the old image and all of its + // placements when the new transmission begins, not when it completes. + if (t.image_id > 0) { + storage.delete(io, alloc, terminal, .{ .id = .{ + .image_id = t.image_id, + .delete = true, + } }); + } + + break :loading try .init(io, alloc, cmd, storage.image_limits); + }; // We only want to deinit on error. If we're chunking, then we don't // want to deinit at all. If we're not chunking, then we'll deinit @@ -336,12 +1034,12 @@ fn loadAndAddImage( // If the image has no ID, we assign one if (loading.image.id == 0) { - loading.image.id = storage.next_image_id; - storage.next_image_id +%= 1; - - // If the image also has no number then its auto-ID is "implicit". - // See the doc comment on the Image.implicit_id field for more detail. - if (loading.image.number == 0) loading.image.implicit_id = true; + if (loading.image.number > 0) { + loading.image.id = storage.nextImageId(.explicit); + } else { + loading.image.id = storage.nextImageId(.implicit); + loading.image.metadata.implicit_id = true; + } } // If this is chunked, this is the beginning of a new chunked transmission. @@ -362,7 +1060,7 @@ fn loadAndAddImage( // Validate and store our image var img = try loading.complete(alloc); errdefer img.deinit(alloc); - try storage.addImage(alloc, img); + try storage.addImage(io, alloc, terminal.screens.active, img); // Get our display settings const display_ = loading.display; @@ -380,6 +1078,7 @@ const EncodeableError = Image.Error || Allocator.Error; fn encodeError(r: *Response, err: EncodeableError) void { switch (err) { error.OutOfMemory => r.message = "ENOMEM: out of memory", + error.InsufficientData => r.message = "ENODATA: insufficient data", error.InvalidData => r.message = "EINVAL: invalid data", error.DecompressionFailed => r.message = "EINVAL: decompression failed", error.FilePathTooLong => r.message = "EINVAL: file path too long", @@ -393,32 +1092,283 @@ fn encodeError(r: *Response, err: EncodeableError) void { } } -test "kittygfx more chunks with q=1" { +test "kittygfx query validates image data" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; + + var terminal = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer terminal.deinit(alloc); + + var cmd: Command = .{ + .control = .{ .query = .{ + .format = .rgb, + .width = 1, + .height = 1, + .image_id = 31, + } }, + // A 1x1 RGB image requires three bytes. + .data = try alloc.dupe(u8, &.{ 0, 0 }), + }; + defer cmd.deinit(alloc); - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); - defer t.deinit(alloc); + const resp = execute(io, alloc, &terminal, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqual(@as(u32, 31), resp.id); + try testing.expectEqualStrings("EINVAL: invalid data", resp.message); + try testing.expectEqual( + @as(usize, 0), + terminal.screens.active.kitty_images.images.count(), + ); +} - // Initial chunk has q=1 +test "kittygfx valid query does not replace or store image" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var terminal = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer terminal.deinit(alloc); + const storage = &terminal.screens.active.kitty_images; + + // Store a red pixel under the same ID used by the query. { const cmd = try command.Parser.parseString( alloc, - "a=T,f=24,t=d,i=1,s=1,v=2,c=10,r=1,m=1,q=1;////", + "a=t,f=24,s=1,v=1,i=31;/wAA", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd); - try testing.expect(resp == null); + try testing.expect(execute(io, alloc, &terminal, &cmd).?.ok()); } - // Subsequent chunk has no q but should respect initial - { - const cmd = try command.Parser.parseString( - alloc, - "m=0;////", + // Successfully validate a black pixel without replacing the red one. + var cmd: Command = .{ + .control = .{ .query = .{ + .format = .rgb, + .width = 1, + .height = 1, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, &.{ 0, 0, 0 }), + }; + defer cmd.deinit(alloc); + + const resp = execute(io, alloc, &terminal, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(usize, 1), storage.images.count()); + try testing.expectEqualSlices( + u8, + &.{ 255, 0, 0 }, + storage.imageById(31).?.data.bytes().?, + ); +} + +test "kittygfx image id and number are mutually exclusive for every action" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + const inputs = [_][]const u8{ + "a=q,f=24,s=1,v=1,i=1,I=2,p=3;AAAA", + "a=t,f=24,s=1,v=1,i=1,I=2,p=3;AAAA", + "a=T,f=24,s=1,v=1,i=1,I=2,p=3;AAAA", + "a=p,i=1,I=2,p=3", + "a=d,d=a,i=1,I=2,p=3", + "a=f,i=1,I=2,p=3;AAAA", + "a=a,i=1,I=2,p=3", + "a=c,i=1,I=2,p=3", + }; + + for (inputs) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.id); + try testing.expectEqual(@as(u32, 2), resp.image_number); + try testing.expectEqual(@as(u32, 3), resp.placement_id); + try testing.expectEqualStrings( + "EINVAL: image ID and number are mutually exclusive", + resp.message, + ); + + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try resp.encode(&writer); + try testing.expectEqualStrings( + "\x1b_Gi=1,I=2,p=3;EINVAL: image ID and number are mutually exclusive\x1b\\", + writer.buffered(), + ); + } + + try testing.expectEqual(@as(usize, 0), t.screens.active.kitty_images.images.count()); + try testing.expectEqual(@as(usize, 0), t.screens.active.kitty_images.placements.count()); +} + +test "kittygfx conflicting identifiers are rejected before mutation" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // Store an image so a buggy put would succeed and mutate placement state. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Directly constructed commands must receive the same validation as + // commands produced by the parser. + { + const cmd: Command = .{ .control = .{ .display = .{ + .image_id = 1, + .image_number = 2, + .placement_id = 7, + .cursor_movement = .none, + } } }; + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.id); + try testing.expectEqual(@as(u32, 2), resp.image_number); + try testing.expectEqual(@as(u32, 7), resp.placement_id); + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + } + + // Add a real placement so a buggy delete would remove it. + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=1,p=7,C=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + } + + { + const cmd = try command.Parser.parseString( + alloc, + "a=d,d=i,i=1,I=2,p=7", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + } + + // q=2 suppresses the required error response but not the validation. + { + const cmd = try command.Parser.parseString( + alloc, + "a=d,d=i,i=1,I=2,p=7,q=2", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + } +} + +test "kittygfx chunked success response uses initial identifiers" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=2,I=93,p=7,m=1;AAAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + + { + const cmd = try command.Parser.parseString(alloc, "m=0;AAAA"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.id); + try testing.expectEqual(@as(u32, 93), resp.image_number); + try testing.expectEqual(@as(u32, 7), resp.placement_id); + + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try resp.encode(&writer); + try testing.expectEqualStrings( + "\x1b_Gi=1,I=93,p=7;OK\x1b\\", + writer.buffered(), + ); + } +} + +test "kittygfx chunked error response uses initial identifiers" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=41,p=7,m=1;AA==", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + + { + const cmd = try command.Parser.parseString(alloc, "m=0;AA=="); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + + try testing.expect(!resp.ok()); + try testing.expectEqual(@as(u32, 41), resp.id); + try testing.expectEqual(@as(u32, 7), resp.placement_id); + try testing.expectEqualStrings("EINVAL: invalid data", resp.message); + } +} + +test "kittygfx more chunks with q=1" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + // Initial chunk has q=1 + { + const cmd = try command.Parser.parseString( + alloc, + "a=T,f=24,t=d,i=1,s=1,v=2,c=10,r=1,m=1,q=1;////", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd); + try testing.expect(resp == null); + } + + // Subsequent chunk has no q but should respect initial + { + const cmd = try command.Parser.parseString( + alloc, + "m=0;////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd); + const resp = execute(io, alloc, &t, &cmd); try testing.expect(resp == null); } } @@ -426,8 +1376,9 @@ test "kittygfx more chunks with q=1" { test "kittygfx more chunks with q=0" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Initial chunk has q=0 @@ -437,7 +1388,7 @@ test "kittygfx more chunks with q=0" { "a=t,f=24,t=d,s=1,v=2,c=10,r=1,m=1,i=1,q=0;////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd); + const resp = execute(io, alloc, &t, &cmd); try testing.expect(resp == null); } @@ -448,7 +1399,7 @@ test "kittygfx more chunks with q=0" { "m=0;////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd).?; + const resp = execute(io, alloc, &t, &cmd).?; try testing.expect(resp.ok()); } } @@ -456,8 +1407,9 @@ test "kittygfx more chunks with q=0" { test "kittygfx more chunks with chunk increasing q" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); // Initial chunk has q=0 @@ -467,7 +1419,7 @@ test "kittygfx more chunks with chunk increasing q" { "a=t,f=24,t=d,s=1,v=2,c=10,r=1,m=1,i=1,q=0;////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd); + const resp = execute(io, alloc, &t, &cmd); try testing.expect(resp == null); } @@ -478,16 +1430,93 @@ test "kittygfx more chunks with chunk increasing q" { "m=0,q=1;////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd); + const resp = execute(io, alloc, &t, &cmd); try testing.expect(resp == null); } } +test "kittygfx delete aborts chunked image load" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // Begin a two-chunk RGB image, then interrupt it with a delete. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=2,i=1,m=1;AAAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expect(storage.loading != null); + + { + const cmd = try command.Parser.parseString(alloc, "a=d"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expect(storage.loading == null); + + // A fresh chunked upload must start from empty state after the delete. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=2,i=1,m=1;AAAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + { + const cmd = try command.Parser.parseString(alloc, "m=0;AAAA"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(@as(usize, 6), storage.imageById(1).?.data.len()); +} + +test "kittygfx uppercase id delete preserves image when placement does not match" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // Store an unplaced 1x1 RGB image. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;AAAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Uppercase deletion may free data only if the named placement matched. + { + const cmd = try command.Parser.parseString( + alloc, + "a=d,d=I,i=1,p=7", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + + try testing.expect(storage.imageById(1) != null); +} + test "kittygfx default format is rgba" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); const cmd = try command.Parser.parseString( @@ -495,7 +1524,7 @@ test "kittygfx default format is rgba" { "a=t,t=d,i=1,s=1,v=2,c=10,r=1;///////////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd).?; + const resp = execute(io, alloc, &t, &cmd).?; try testing.expect(resp.ok()); const storage = &t.screens.active.kitty_images; @@ -506,8 +1535,9 @@ test "kittygfx default format is rgba" { test "kittygfx test valid u32 (expect invalid image ID)" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); const cmd = try command.Parser.parseString( @@ -515,7 +1545,7 @@ test "kittygfx test valid u32 (expect invalid image ID)" { "a=p,i=4294967295", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd).?; + const resp = execute(io, alloc, &t, &cmd).?; try testing.expect(!resp.ok()); try testing.expectEqual(resp.message, "ENOENT: image not found"); } @@ -523,8 +1553,9 @@ test "kittygfx test valid u32 (expect invalid image ID)" { test "kittygfx test valid i32 (expect invalid image ID)" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); const cmd = try command.Parser.parseString( @@ -532,7 +1563,7 @@ test "kittygfx test valid i32 (expect invalid image ID)" { "a=p,i=1,z=-2147483648", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd).?; + const resp = execute(io, alloc, &t, &cmd).?; try testing.expect(!resp.ok()); try testing.expectEqual(resp.message, "ENOENT: image not found"); } @@ -540,8 +1571,9 @@ test "kittygfx test valid i32 (expect invalid image ID)" { test "kittygfx no response with no image ID or number" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); { @@ -550,7 +1582,7 @@ test "kittygfx no response with no image ID or number" { "a=t,f=24,t=d,s=1,v=2,c=10,r=1,i=0,I=0;////////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd); + const resp = execute(io, alloc, &t, &cmd); try testing.expect(resp == null); } } @@ -558,8 +1590,9 @@ test "kittygfx no response with no image ID or number" { test "kittygfx no response with no image ID or number load and display" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); { @@ -568,16 +1601,17 @@ test "kittygfx no response with no image ID or number load and display" { "a=T,f=24,t=d,s=1,v=2,c=10,r=1,i=0,I=0;////////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd); + const resp = execute(io, alloc, &t, &cmd); try testing.expect(resp == null); } } test "kittygfx retransmit same id gets fresh image generation" { const testing = std.testing; + const io = testing.io; const alloc = testing.allocator; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); const storage = &t.screens.active.kitty_images; @@ -588,7 +1622,7 @@ test "kittygfx retransmit same id gets fresh image generation" { "a=t,t=d,f=24,i=1,s=1,v=2;////////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd).?; + const resp = execute(io, alloc, &t, &cmd).?; try testing.expect(resp.ok()); } const gen1 = storage.imageById(1).?.generation; @@ -604,7 +1638,7 @@ test "kittygfx retransmit same id gets fresh image generation" { "a=t,t=d,f=24,i=1,s=1,v=2;AAAAAAAA", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd).?; + const resp = execute(io, alloc, &t, &cmd).?; try testing.expect(resp.ok()); } const gen2 = storage.imageById(1).?.generation; @@ -612,11 +1646,134 @@ test "kittygfx retransmit same id gets fresh image generation" { try testing.expectEqual(gen2, storage.generation); } +test "kittygfx retransmit same id removes existing placements" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + const tracked = t.screens.active.pages.countTrackedPins(); + + // Transmit and display an image, then add anonymous and named placements. + // Multiple anonymous a=p placements for one image are explicitly valid. + { + const cmd = try command.Parser.parseString( + alloc, + "a=T,t=d,f=24,i=1,s=1,v=2,C=1;////////", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=1,C=1"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=1,p=7,C=1"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + try testing.expectEqual(@as(usize, 3), storage.placements.count()); + try testing.expectEqual( + tracked + 3, + t.screens.active.pages.countTrackedPins(), + ); + + // Retransmitting replaces the image and must delete every old placement. + // Plain a=t creates no replacement placement of its own. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,t=d,f=24,i=1,s=1,v=2;AAAAAAAA", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins()); + + // a=T creates one new placement after deleting the previous image and + // placements, so repeated redraws remain bounded at one placement. + { + const cmd = try command.Parser.parseString( + alloc, + "a=T,t=d,f=24,i=1,s=1,v=2,C=1;////////", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + { + const cmd = try command.Parser.parseString( + alloc, + "a=T,t=d,f=24,i=1,s=1,v=2,C=1;AAAAAAAA", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + try testing.expectEqual( + tracked + 1, + t.screens.active.pages.countTrackedPins(), + ); +} + +test "kittygfx retransmit same id removes image on first chunk" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + const tracked = t.screens.active.pages.countTrackedPins(); + + // Store and display the old image. + { + const cmd = try command.Parser.parseString( + alloc, + "a=T,t=d,f=24,i=1,s=1,v=2,C=1;////////", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expect(storage.imageById(1) != null); + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + try testing.expectEqual( + tracked + 1, + t.screens.active.pages.countTrackedPins(), + ); + + // Starting a replacement for the same explicit ID removes the old image + // and placements before the replacement's final chunk arrives. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,t=d,f=24,i=1,s=1,v=2,m=1;AAAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expect(storage.loading != null); + try testing.expect(storage.imageById(1) == null); + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins()); +} + test "kittygfx delete then retransmit same id gets fresh generation" { const testing = std.testing; + const io = testing.io; const alloc = testing.allocator; - var t = try Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); const storage = &t.screens.active.kitty_images; @@ -628,7 +1785,7 @@ test "kittygfx delete then retransmit same id gets fresh generation" { "a=T,t=d,f=24,i=1,s=1,v=2,c=1,r=1;////////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd).?; + const resp = execute(io, alloc, &t, &cmd).?; try testing.expect(resp.ok()); } const gen1 = storage.imageById(1).?.generation; @@ -636,7 +1793,7 @@ test "kittygfx delete then retransmit same id gets fresh generation" { { const cmd = try command.Parser.parseString(alloc, "a=d,d=A"); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd); + const resp = execute(io, alloc, &t, &cmd); try testing.expect(resp == null); } try testing.expect(storage.imageById(1) == null); @@ -649,10 +1806,1798 @@ test "kittygfx delete then retransmit same id gets fresh generation" { "a=t,t=d,f=24,i=1,s=1,v=2;////////", ); defer cmd.deinit(alloc); - const resp = execute(alloc, &t, &cmd).?; + const resp = execute(io, alloc, &t, &cmd).?; try testing.expect(resp.ok()); } const gen2 = storage.imageById(1).?.generation; try testing.expect(gen2 > gen1); try testing.expect(gen2 > gen_delete); } + +test "kittygfx display clamps cell offsets" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + t.width_px = 50; // 10 px per col + t.height_px = 100; // 20 px per row + + const cmd = try command.Parser.parseString( + alloc, + "a=T,t=d,f=24,i=1,s=1,v=1,c=2,r=1,X=99,Y=99,C=1;AAAA", + ); + defer cmd.deinit(alloc); + + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + + const storage = &t.screens.active.kitty_images; + var it = storage.placements.iterator(); + const placement = it.next().?.value_ptr; + try testing.expectEqual(@as(u32, 9), placement.x_offset); + try testing.expectEqual(@as(u32, 19), placement.y_offset); + + const actual = placement.pixelSize(storage.imageById(1).?, &t); + try testing.expectEqual(@as(u32, 11), actual.width); + try testing.expectEqual(@as(u32, 1), actual.height); +} + +test "kittygfx placement bounds cursor movement for untrusted dimensions" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + const cmd = try command.Parser.parseString( + alloc, + "a=T,t=d,f=24,i=1,s=1,v=1,c=4294967295,r=4294967295;////", + ); + defer cmd.deinit(alloc); + + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(usize, 1), t.screens.active.kitty_images.placements.count()); + // Reaching the bottom takes four rows, then scrolling is capped to one + // screen (five rows), matching Kitty's bounded screen-scroll behavior. + try testing.expectEqual(@as(usize, 10), t.screens.active.pages.scrollbar().total); +} + +test "kittygfx placement moves cursor past a tall image" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + // Load a one-pixel RGB image, then place it over the full width and eight + // rows. Eight rows are taller than the screen but still within Kitty's + // scroll budget. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,t=d,f=24,i=1,s=1,v=1;////", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + { + const cmd = try command.Parser.parseString( + alloc, + "a=p,i=1,p=1,c=5,r=8", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + + // A subsequent placement begins immediately after the first one instead + // of overlapping it at the old one-screen movement cap. + { + const cmd = try command.Parser.parseString( + alloc, + "a=p,i=1,p=2,C=1", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + + const storage = &t.screens.active.kitty_images; + const first = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const second = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 2 }, + }).?; + const first_pin = switch (first.location) { + .pin => |pin| pin, + .virtual, .relative => unreachable, + }; + const second_pin = switch (second.location) { + .pin => |pin| pin, + .virtual, .relative => unreachable, + }; + const first_y = t.screens.active.pages.pointFromPin( + .screen, + first_pin.*, + ).?.screen.y; + const second_y = t.screens.active.pages.pointFromPin( + .screen, + second_pin.*, + ).?.screen.y; + try testing.expectEqual(first_y + 8, second_y); +} + +test "kittygfx unknown format responds with EINVAL" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=42,t=d,i=31,s=1,v=1;AAAA", + ); + defer cmd.deinit(alloc); + + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqual(@as(u32, 31), resp.id); + try testing.expectEqualStrings("EINVAL: unsupported format", resp.message); + try testing.expectEqual( + @as(usize, 0), + t.screens.active.kitty_images.images.count(), + ); +} + +test "kittygfx unknown format on query responds with EINVAL" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + const cmd = try command.Parser.parseString( + alloc, + "a=q,f=42,t=d,i=31,s=1,v=1;AAAA", + ); + defer cmd.deinit(alloc); + + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqual(@as(u32, 31), resp.id); + try testing.expectEqualStrings("EINVAL: unsupported format", resp.message); +} + +test "kittygfx zero format is rgba" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + // Kitty treats f=0 as an absent f, i.e. RGBA. + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=0,t=d,i=1,s=1,v=2,c=10,r=1;///////////", + ); + defer cmd.deinit(alloc); + + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + + const img = t.screens.active.kitty_images.imageById(1).?; + try testing.expectEqual(command.Transmission.Format.rgba, img.format); +} + +test "kittygfx unknown format with q=3 has no response" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + // q above 2 is out of range in the spec but Kitty suppresses + // everything for anything above 1, so we must still parse it. + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=42,t=d,i=31,q=3,s=1,v=1;AAAA", + ); + defer cmd.deinit(alloc); + + try testing.expect(execute(io, alloc, &t, &cmd) == null); +} + +test "kittygfx out of range display keys are tolerated" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + { + const cmd = try command.Parser.parseString( + alloc, + "a=T,f=24,t=d,i=31,s=1,v=1,C=2,U=2;AAAA", + ); + defer cmd.deinit(alloc); + + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 31), resp.id); + } + + // U=2 must behave like U=1, so the placement is virtual. + const storage = &t.screens.active.kitty_images; + var it = storage.placements.iterator(); + const entry = it.next().?; + try testing.expect(entry.value_ptr.location == .virtual); +} + +test "kittygfx number-based transmission assigns smallest free id" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // Empty storage: the first free ID is 1, as in Kitty. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,I=42;/wAA", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.id); + try testing.expectEqual(@as(u32, 42), resp.image_number); + } + + // Occupy ID 2 explicitly; the next number gets 3. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=2;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,I=43;/wAA", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 3), resp.id); + } + + // Deleting ID 1 opens a gap that the next number fills. + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=I,i=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,I=44;/wAA", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.id); + } + + try testing.expectEqual(@as(usize, 3), storage.images.count()); + try testing.expectEqual(@as(u32, 44), storage.imageById(1).?.number); + try testing.expectEqual(@as(u32, 0), storage.imageById(2).?.number); + try testing.expectEqual(@as(u32, 43), storage.imageById(3).?.number); +} + +test "kittygfx number-based id assignment does not replace client image" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // A client stores and places a red pixel on the ID the old wrapping + // counter would assign first. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=2147483647;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=2147483647,C=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // A number-based transmission must not collide with it. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,I=42;AAD/", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.id); + } + + // The client's image and placement are untouched. + try testing.expectEqual(@as(usize, 2), storage.images.count()); + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + try testing.expectEqualSlices( + u8, + &.{ 255, 0, 0 }, + storage.imageById(2147483647).?.data.bytes().?, + ); +} + +test "kittygfx implicit id assignment does not replace client image" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // A client stores and places a red pixel on the ID the implicit + // counter assigns first. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=2147483647;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=2147483647,C=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Transmit without an ID or number: no response, and the counter + // skips over the in-use ID instead of replacing that image. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1;AAD/", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + + try testing.expectEqual(@as(usize, 2), storage.images.count()); + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + try testing.expectEqualSlices( + u8, + &.{ 255, 0, 0 }, + storage.imageById(2147483647).?.data.bytes().?, + ); + const implicit = storage.imageById(2147483648).?; + try testing.expect(implicit.metadata.implicit_id); + try testing.expectEqualSlices(u8, &.{ 0, 0, 255 }, implicit.data.bytes().?); +} + +test "kittygfx relative placement with missing parent image" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;AAAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // The parent image does not exist: the placement must be rejected + // with ENOPARENT, nothing may be created, and the cursor must not + // move (a relative placement never moves the cursor, and a failed + // one certainly must not). + { + const cmd = try command.Parser.parseString( + alloc, + "a=p,i=1,p=1,P=42,Q=1,H=2,V=2", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings( + "ENOPARENT: parent image not found", + resp.message, + ); + } + + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + try testing.expectEqual(0, t.screens.active.cursor.x); + try testing.expectEqual(0, t.screens.active.cursor.y); +} + +test "kittygfx relative placement with missing parent placement" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=t,f=24,s=1,v=1,i=2;AAAA", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // The parent image exists but has no placements at all. + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=1,P=2"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings( + "ENOPARENT: parent placement not found", + resp.message, + ); + } + + // The parent image has a placement, but not the requested one. + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=2,p=1,C=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=1,P=2,Q=9"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings( + "ENOPARENT: parent placement not found", + resp.message, + ); + } + + try testing.expectEqual(@as(usize, 1), storage.placements.count()); +} + +test "kittygfx relative placement cannot parent itself" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=p,i=1,p=1,C=1", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Explicitly via Q, and implicitly when the Q=0 fallback selects + // the placement being replaced. + for ([_][]const u8{ + "a=p,i=1,p=1,P=1,Q=1", + "a=p,i=1,p=1,P=1", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings( + "EINVAL: placement cannot be its own parent", + resp.message, + ); + } + + // The original placement must be untouched. + const storage = &t.screens.active.kitty_images; + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + try testing.expect(p.location == .pin); +} + +test "kittygfx relative placement cycle" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=p,i=1,p=1,C=1", + "a=p,i=1,p=2,P=1,Q=1", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Replacing placement 1 with a parent of placement 2 would create + // the cycle 1 -> 2 -> 1. + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=1,p=1,P=1,Q=2"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings( + "ECYCLE: parent chain creates a cycle", + resp.message, + ); + } + + // The original placement must be untouched, keeping the stored + // chains acyclic. + const storage = &t.screens.active.kitty_images; + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + try testing.expect(p.location == .pin); +} + +test "kittygfx relative placement chain depth limit" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=p,i=1,p=1,C=1", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Chains of up to parent_chain_limit (8) links must work: these + // placements form the chain 9 -> 8 -> ... -> 2 -> 1. + for (2..10) |id| { + var buf: [64]u8 = undefined; + const cmd = try command.Parser.parseString( + alloc, + try std.fmt.bufPrint(&buf, "a=p,i=1,p={},P=1,Q={}", .{ id, id - 1 }), + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // One more link exceeds the limit. + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=1,p=10,P=1,Q=9"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings( + "ETOODEEP: parent chain too deep", + resp.message, + ); + } + + const storage = &t.screens.active.kitty_images; + try testing.expectEqual(@as(usize, 9), storage.placements.count()); +} + +test "kittygfx relative placement does not move cursor" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // C is left at its default (move after) on the relative placement + // but it must never move the cursor. + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=p,i=1,p=1,C=1", + "a=p,i=1,p=2,P=1,Q=1,H=3,V=2,c=2,r=2", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(0, t.screens.active.cursor.x); + try testing.expectEqual(0, t.screens.active.cursor.y); + + // The stored placement carries the parent link and offsets. + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 2 }, + }).?; + const rel = p.location.relative; + try testing.expect(rel.parent.eql(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + })); + try testing.expectEqual(@as(i32, 3), rel.horizontal_offset); + try testing.expectEqual(@as(i32, 2), rel.vertical_offset); +} + +test "kittygfx virtual placement with parent rejected before image lookup" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + // Kitty rejects U=1 + P= before checking that the image exists, + // so this must not be ENOENT. + const cmd = try command.Parser.parseString(alloc, "a=p,i=42,U=1,P=1"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings( + "EINVAL: virtual placement cannot refer to a parent", + resp.message, + ); +} + +test "kittygfx deleting a parent deletes relative placements transitively" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // Root (pin) <- child <- grandchild + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=p,i=1,p=1,C=1", + "a=p,i=1,p=2,P=1,Q=1", + "a=p,i=1,p=3,P=1,Q=2", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(@as(usize, 3), storage.placements.count()); + + // Deleting the middle placement takes the grandchild with it but + // leaves the root. + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=i,i=1,p=2"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + try testing.expect(storage.placements.contains(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + })); + + // Deleting the root removes the remaining placement, and the + // image itself is retained (lowercase delete). + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=i,i=1,p=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + try testing.expect(storage.imageById(1) != null); +} + +test "kittygfx retransmitting parent image deletes relative placements" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // Image 2's placement is relative to image 1's placement. + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=t,f=24,s=1,v=1,i=2;AAAA", + "a=p,i=1,p=1,C=1", + "a=p,i=2,p=1,P=1,Q=1", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(@as(usize, 2), storage.placements.count()); + + // Retransmitting image 1 removes its placements, which orphans + // and removes image 2's placement too. Image 2 is left without + // any placement so it is freed as well: retransmission deletes + // with uppercase semantics, and kitty also removes an image whose + // last placement died from a broken parent chain. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;AAAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + try testing.expect(storage.imageById(2) == null); +} + +test "kittygfx relative placement parent fallback picks lowest external id" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=t,f=24,s=1,v=1,i=2;AAAA", + "a=p,i=1,p=7,C=1", + "a=p,i=1,p=3,C=1", + "a=p,i=2,P=1", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + var it = storage.placements.iterator(); + const rel = while (it.next()) |entry| { + if (entry.key_ptr.image_id == 2) break entry.value_ptr.location.relative; + } else return error.PlacementNotFound; + try testing.expect(rel.parent.eql(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 3 }, + })); +} + +test "kittygfx placements created after a full reset are retained" { + // Regression test: Screen.reset reuses the cursor's tracked pin + // but PageList.reset marked it garbage. Placements copy the cursor + // pin, so every placement created after a reset was born garbage + // and silently swept by the next placement command. + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 24, .cols = 80 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + t.fullReset(); + + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=p,i=1,p=1,C=1", + "a=p,i=1,p=2,C=1", + "a=p,i=1,p=3,P=1,Q=2", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(@as(usize, 3), storage.placements.count()); +} + +test "kittygfx relative placement with pruned parent is ENOPARENT" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=p,i=1,p=1,C=1", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // The parent's tracked content is pruned from history but the + // placement hasn't been swept yet. The put must reap it and answer + // ENOPARENT rather than storing an orphan against it. + const parent = storage.placements.getPtr(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + parent.location.pin.garbage = true; + + { + const cmd = try command.Parser.parseString(alloc, "a=p,i=1,p=2,P=1,Q=1"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings( + "ENOPARENT: parent placement not found", + resp.message, + ); + } + try testing.expectEqual(@as(usize, 0), storage.placements.count()); +} + +test "kittygfx uppercase delete frees image of cascaded placements" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + for ([_][]const u8{ + "a=t,f=24,s=1,v=1,i=1;AAAA", + "a=p,i=1,p=1,C=1", + "a=p,i=1,p=2,P=1,Q=1", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // The uppercase delete only matches placement 1. The cascade + // removes placement 2, leaving the image without placements, so + // the image must be freed too. + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=I,i=1,p=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + try testing.expect(storage.imageById(1) == null); +} + +// Animation tests frequently start from a 1x1 RGB red image with +// id=1 ("/wAA" is FF0000). Composition converts images to RGBA, so +// after the first frame command the base data is FF0000FF. + +test "kittygfx animation: new frame with default gap responds with frame number" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Transmit a 1x1 RGB blue frame ("AAD/" is 0000FF). + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1;AAD/", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.id); + try testing.expectEqual(@as(u32, 2), resp.frame); + + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try resp.encode(&writer); + try testing.expectEqualStrings("\x1b_Gi=1,r=2;OK\x1b\\", writer.buffered()); + + const img = storage.imagePtrByIdOrNumber(1, 0).?; + try testing.expectEqual(command.Transmission.Format.rgba, img.format); + try testing.expectEqualSlices(u8, &.{ 255, 0, 0, 255 }, img.data.bytes().?); + + const anim = img.animation.?; + try testing.expectEqual(@as(u32, 2), anim.frameCount()); + try testing.expectEqual(@as(u32, 0), anim.root_gap_ms); + try testing.expectEqual(@as(u32, 40), anim.frames.items[0].gap_ms); + try testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255 }, anim.frames.items[0].data); + + // The displayed frame is still the root: renderData is the base. + try testing.expectEqualSlices(u8, &.{ 255, 0, 0, 255 }, img.renderData().bytes().?); + + // Frame bytes count against the storage total. + try testing.expectEqual(@as(usize, 8), storage.total_bytes); +} + +test "kittygfx animation: frame gap normalization on create" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + for ([_][]const u8{ + "a=f,i=1,f=24,s=1,v=1,z=100;AAD/", + "a=f,i=1,f=24,s=1,v=1,z=-5;AAD/", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqual(@as(u32, 100), anim.frames.items[0].gap_ms); + try testing.expectEqual(@as(u32, 0), anim.frames.items[1].gap_ms); +} + +test "kittygfx animation: background fill and offset composition" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // 2x1 RGB white image ("////////" is FFFFFF FFFFFF). + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=2,v=1,i=1;////////", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // New frame: 1x1 RGB white transmitted at x=1 over an opaque red + // background (Y=4278190335 is 0xff0000ff, R in the MSB). + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1,x=1,Y=4278190335;////", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqualSlices( + u8, + &.{ 255, 0, 0, 255, 255, 255, 255, 255 }, + anim.frames.items[0].data, + ); +} + +test "kittygfx animation: create from base frame with overwrite" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Frame 2 based on the root (c=1) with a semi-transparent blue + // pixel ("AAD/gA==" is 0000FF80) in overwrite mode: the source + // replaces the canvas including alpha. + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=32,s=1,v=1,c=1,X=1;AAD/gA==", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + } + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqualSlices(u8, &.{ 0, 0, 255, 128 }, anim.frames.items[0].data); +} + +test "kittygfx animation: alpha blend composes over base frame" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Blend a fully opaque blue pixel over the root: alpha blending + // an opaque source is equivalent to a copy. + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=32,s=1,v=1,c=1;AAD//w==", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Blend a fully transparent pixel over the root: the canvas is + // unchanged (still the red base). + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=32,s=1,v=1,c=1;AAD/AA==", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255 }, anim.frames.items[0].data); + try testing.expectEqualSlices(u8, &.{ 255, 0, 0, 255 }, anim.frames.items[1].data); +} + +test "kittygfx animation: edit root frame bumps generation" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + const gen1 = storage.imagePtrByIdOrNumber(1, 0).?.generation; + + // r=1 edits the root frame in place. The root is the displayed + // frame so the image generation must change. + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1,r=1;AAD/", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.frame); + + const img = storage.imagePtrByIdOrNumber(1, 0).?; + try testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255 }, img.data.bytes().?); + try testing.expect(img.generation > gen1); +} + +test "kittygfx animation: edit frame gap without data change" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Create frame 2, then edit it with z=-1: gap becomes gapless. + // Editing with z=0 leaves the gap unchanged (no 40ms default). + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1,z=77;AAD/", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1,r=2;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqual(@as(u32, 77), anim.frames.items[0].gap_ms); + + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1,r=2,z=-1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(@as(u32, 0), anim.frames.items[0].gap_ms); +} + +test "kittygfx animation: frame errors" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + const cases = [_]struct { + input: []const u8, + message: []const u8, + }{ + // Unknown image. + .{ + .input = "a=f,i=9,f=24,s=1,v=1;AAD/", + .message = "ENOENT: image not found", + }, + // Frame bigger than the image. + .{ + .input = "a=f,i=1,f=24,s=2,v=1;////////", + .message = "EINVAL: frame dimensions exceed image", + }, + // Nonexistent base frame. + .{ + .input = "a=f,i=1,f=24,s=1,v=1,c=5;AAD/", + .message = "EINVAL: base frame not found", + }, + // Insufficient data for the declared rectangle. + .{ + .input = "a=f,i=1,f=24,s=1,v=1;AA==", + .message = "ENODATA: insufficient data", + }, + }; + for (cases) |case| { + const cmd = try command.Parser.parseString(alloc, case.input); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings(case.message, resp.message); + } + + // None of the failures may have created a frame. + const storage = &t.screens.active.kitty_images; + const img = storage.imagePtrByIdOrNumber(1, 0).?; + if (img.animation) |anim| { + try testing.expectEqual(@as(usize, 0), anim.frames.items.len); + } +} + +test "kittygfx animation: excess frame data is truncated" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Six bytes for a 1x1 RGB frame: Kitty tolerates the excess. + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1;AAD/////", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255 }, anim.frames.items[0].data); +} + +test "kittygfx animation: chunked frame transmission" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // 2x1 RGB white image. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=2,v=1,i=1;////////", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Transmit a 2x1 frame in two chunks of one pixel each. The + // chunks repeat a=f per the protocol. No response until the + // final chunk. + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=2,v=1,z=60,m=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expect(storage.loading != null); + { + const cmd = try command.Parser.parseString(alloc, "a=f,m=0;AAD/"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 1), resp.id); + try testing.expectEqual(@as(u32, 2), resp.frame); + } + try testing.expect(storage.loading == null); + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqual(@as(u32, 60), anim.frames.items[0].gap_ms); + try testing.expectEqualSlices( + u8, + &.{ 255, 0, 0, 255, 0, 0, 255, 255 }, + anim.frames.items[0].data, + ); +} + +test "kittygfx animation: chunked frame continuation without a=f" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=2,v=1,i=1;////////", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // The spec requires chunks to repeat a=f, but a bare continuation + // (which parses as a transmit action) must still continue the + // frame load rather than turn into an image transmission. + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=2,v=1,m=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + { + const cmd = try command.Parser.parseString(alloc, "m=0;AAD/"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 2), resp.frame); + } + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqual(@as(usize, 1), anim.frames.items.len); +} + +test "kittygfx animation: control command sets gap current state and loops" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1;AAD/", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + const img = storage.imagePtrByIdOrNumber(1, 0).?; + const gen1 = img.generation; + + // One command can set several things: the root frame's gap + // (r=1,z=50), the current frame (c=2), the state (s=3, running), + // and the loop count (v=3, meaning two loops). Successful a=a + // commands never respond. + const cmd = try command.Parser.parseString( + alloc, + "a=a,i=1,r=1,z=50,c=2,s=3,v=3", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + + const anim = img.animation.?; + try testing.expectEqual(@as(u32, 50), anim.root_gap_ms); + try testing.expectEqual(@as(u32, 1), anim.current_index); + try testing.expectEqual(animation.Animation.State.running, anim.state); + try testing.expectEqual(@as(u32, 2), anim.max_loops); + + // Changing the current frame changes the displayed content. + try testing.expect(img.generation > gen1); + try testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255 }, img.renderData().bytes().?); +} + +test "kittygfx animation: control command ignores invalid values" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Out-of-range frame numbers and s values are silently ignored. + const cmd = try command.Parser.parseString( + alloc, + "a=a,i=1,r=9,z=50,c=9,s=7", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqual(@as(u32, 0), anim.root_gap_ms); + try testing.expectEqual(@as(u32, 0), anim.current_index); + try testing.expectEqual(animation.Animation.State.stopped, anim.state); +} + +test "kittygfx animation: control command missing image responds ENOENT" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + const cmd = try command.Parser.parseString(alloc, "a=a,i=9,s=3"); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings("ENOENT: image not found", resp.message); +} + +test "kittygfx animation: compose frames" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // 2x1 RGB image: red then blue ("/wAAAAD/" is FF0000 0000FF). + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=2,v=1,i=1;/wAAAAD/", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + // Frame 2 starts as a transparent canvas with a white pixel at + // x=0 ("////" is FFFFFF). + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1;////", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Compose the root's pixel at (1,0) onto frame 2 at (0,0): + // r=1 is the source frame, c=2 the destination, X/Y the source + // offsets, x/y the destination offsets. + { + const cmd = try command.Parser.parseString( + alloc, + "a=c,i=1,r=1,c=2,w=1,h=1,X=1,x=0", + ); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(resp.ok()); + try testing.expectEqual(@as(u32, 0), resp.frame); + } + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqualSlices( + u8, + &.{ 0, 0, 255, 255, 0, 0, 0, 0 }, + anim.frames.items[0].data, + ); +} + +test "kittygfx animation: compose current frame bumps generation" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // 2x1 RGB white image; the root frame is the displayed frame. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=2,v=1,i=1;////////", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + const img = storage.imagePtrByIdOrNumber(1, 0).?; + const gen1 = img.generation; + + // a=c works on images without animation state: both frames are + // the root. Copy pixel 0 onto pixel 1. + const cmd = try command.Parser.parseString( + alloc, + "a=c,i=1,r=1,c=1,w=1,h=1,x=1", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + try testing.expect(img.generation > gen1); +} + +test "kittygfx animation: compose errors" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + const cases = [_]struct { + input: []const u8, + message: []const u8, + }{ + .{ + .input = "a=c,i=9,r=1,c=1", + .message = "ENOENT: image not found", + }, + .{ + .input = "a=c,i=1,r=2,c=1", + .message = "ENOENT: source frame not found", + }, + .{ + .input = "a=c,i=1,r=1,c=2", + .message = "ENOENT: destination frame not found", + }, + // 1x1 image: any offset pushes the rect out of bounds. + .{ + .input = "a=c,i=1,r=1,c=1,x=1", + .message = "EINVAL: destination rectangle out of bounds", + }, + .{ + .input = "a=c,i=1,r=1,c=1,X=1", + .message = "EINVAL: source rectangle out of bounds", + }, + // Same frame, same (whole-image) rect: overlap. + .{ + .input = "a=c,i=1,r=1,c=1", + .message = "EINVAL: source and destination rectangles overlap", + }, + }; + for (cases) |case| { + const cmd = try command.Parser.parseString(alloc, case.input); + defer cmd.deinit(alloc); + const resp = execute(io, alloc, &t, &cmd).?; + try testing.expect(!resp.ok()); + try testing.expectEqualStrings(case.message, resp.message); + } +} + +test "kittygfx animation: delete frame promotes root and fixes current" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + // Frames 2 and 3: blue and white. + for ([_][]const u8{ + "a=f,i=1,f=24,s=1,v=1,z=10;AAD/", + "a=f,i=1,f=24,s=1,v=1,z=20;////", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + const total_before = storage.total_bytes; + + // Delete the root frame (r omitted selects it): frame 2 becomes + // the new root, including its gap. Deletes never respond. + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=f,i=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + const img = storage.imagePtrByIdOrNumber(1, 0).?; + const anim = img.animation.?; + try testing.expectEqual(@as(u32, 2), anim.frameCount()); + try testing.expectEqual(@as(u32, 10), anim.root_gap_ms); + try testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255 }, img.data.bytes().?); + try testing.expectEqual(total_before - 4, storage.total_bytes); + + // Delete a frame number past the end: clamps to the last frame. + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=f,i=1,r=9"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expectEqual(@as(u32, 1), anim.frameCount()); + try testing.expectEqual(total_before - 8, storage.total_bytes); + + // Down to one frame: d=f is now a no-op and the image survives. + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=f,i=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expect(storage.imageById(1) != null); +} + +test "kittygfx animation: uppercase frame delete removes non-animated image" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + + // Transmit and place: unlike d=I, an uppercase frame delete on a + // non-animated image removes it even though it is placed. + { + const cmd = try command.Parser.parseString( + alloc, + "a=T,f=24,s=1,v=1,i=1,C=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(@as(usize, 1), storage.placements.count()); + + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=F,i=1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expect(storage.imageById(1) == null); + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + try testing.expectEqual(@as(usize, 0), storage.total_bytes); +} + +test "kittygfx animation: deleting current frame refreshes display" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + for ([_][]const u8{ + "a=f,i=1,f=24,s=1,v=1;AAD/", + "a=f,i=1,f=24,s=1,v=1;////", + "a=a,i=1,c=3", + }) |input| { + const cmd = try command.Parser.parseString(alloc, input); + defer cmd.deinit(alloc); + _ = execute(io, alloc, &t, &cmd); + } + + const img = storage.imagePtrByIdOrNumber(1, 0).?; + const anim = img.animation.?; + try testing.expectEqual(@as(u32, 2), anim.current_index); + const gen1 = img.generation; + + // Deleting the last frame (the current one) clamps the index and + // refreshes the display. + { + const cmd = try command.Parser.parseString(alloc, "a=d,d=f,i=1,r=3"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + } + try testing.expectEqual(@as(u32, 1), anim.current_index); + try testing.expect(img.generation > gen1); + try testing.expectEqualSlices(u8, &.{ 0, 0, 255, 255 }, img.renderData().bytes().?); +} + +test "kittygfx animation: retransmitting base image resets animation" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1;AAD/", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + try testing.expectEqual(@as(usize, 8), storage.total_bytes); + + // Retransmit the base image: the animation is dropped and the + // frame bytes are credited back. + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + const img = storage.imagePtrByIdOrNumber(1, 0).?; + try testing.expect(img.animation == null); + try testing.expectEqual(@as(usize, 3), storage.total_bytes); +} + +test "kittygfx animation: control negative gap makes frame gapless" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + const storage = &t.screens.active.kitty_images; + { + const cmd = try command.Parser.parseString( + alloc, + "a=t,f=24,s=1,v=1,i=1;/wAA", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + { + const cmd = try command.Parser.parseString( + alloc, + "a=f,i=1,f=24,s=1,v=1;AAD/", + ); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd).?.ok()); + } + + const cmd = try command.Parser.parseString(alloc, "a=a,i=1,r=2,z=-1"); + defer cmd.deinit(alloc); + try testing.expect(execute(io, alloc, &t, &cmd) == null); + + const anim = storage.imagePtrByIdOrNumber(1, 0).?.animation.?; + try testing.expectEqual(@as(u32, 0), anim.frames.items[0].gap_ms); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/graphics_image.zig b/vendor/libghostty-vt/src/terminal/kitty/graphics_image.zig index b6f44fac..e411bafe 100644 --- a/vendor/libghostty-vt/src/terminal/kitty/graphics_image.zig +++ b/vendor/libghostty-vt/src/terminal/kitty/graphics_image.zig @@ -6,15 +6,12 @@ const ArenaAllocator = std.heap.ArenaAllocator; const posix = std.posix; const fastmem = @import("../../fastmem.zig"); +const animation = @import("graphics_animation.zig"); const command = @import("graphics_command.zig"); +const kitty_windows = @import("windows.zig"); const PageList = @import("../PageList.zig"); const sys = @import("../sys.zig"); - -const temp_dir = struct { - const TempDir = @import("../../os/TempDir.zig"); - const allocTmpDir = @import("../../os/file.zig").allocTmpDir; - const freeTmpDir = @import("../../os/file.zig").freeTmpDir; -}; +const LimitedAllocator = @import("../../datastruct/main.zig").LimitedAllocator; const log = std.log.scoped(.kitty_gfx); @@ -34,36 +31,75 @@ pub const LoadingImage = struct { image: Image, /// The data that is being built up. - data: std.ArrayListUnmanaged(u8) = .{}, + data: std.ArrayListUnmanaged(u8) = .empty, /// This is non-null when a transmit and display command is given /// so that we display the image after it is fully loaded. display: ?command.Display = null, + /// This is non-null when this load is an animation frame + /// transmission (a=f) rather than a new image. On completion the + /// data is composed into the target image's animation instead of + /// being stored as an image. + frame: ?FrameContext = null, + /// Quiet is the quiet settings for the initial load command. This is /// used if q isn't set on subsequent chunks. quiet: command.Command.Quiet, + /// Response identifiers from the initial load command. Subsequent chunks + /// omit these, so completion responses must use the saved values. + response: command.Response = .{}, + + /// The temporary directory for file transmission (null means that + /// temporary directory transmission is disabled). + temporary_directory: ?[]const u8, + + pub const FrameContext = struct { + /// The frame parameters from the initial a=f command. Chunked + /// continuations only contribute payload bytes; all parameters + /// come from the command that started the load, matching the + /// protocol's requirement that chunks repeat a=f. + cmd: command.AnimationFrameLoading, + + /// The generation of the target image when the load began. + /// A different generation at completion means the image was + /// replaced or evicted mid-transmission and the frame must be + /// discarded rather than composed onto the wrong image. + image_generation: u64, + }; + /// The limits of the Kitty Graphics protocol we should allow. /// /// This can be used to restrict the type of images and other /// parameters for resource or security reasons. Note that depending /// on how libghostty is compiled, some of these may be fully unsupported /// and ignored (e.g. "file" on wasm32-freestanding). - pub const Limits = packed struct { + pub const Limits = struct { file: bool, - temporary_file: bool, + temporary_file: union(enum) { + enabled: struct { + /// The directory to expect temporary files in. + directory: []const u8, + }, + disabled: void, + }, shared_memory: bool, - pub const all: Limits = .{ - .file = true, - .temporary_file = true, - .shared_memory = true, - }; + /// Enables all filesystem-related image transmission mediums. `path` + /// is the temporary directory to expect files in when files are + /// transmitted using said medium. + pub fn allWithTempDir(path: []const u8) Limits { + return .{ + .file = true, + .temporary_file = .{ .enabled = .{ .directory = path } }, + .shared_memory = true, + }; + } pub const direct: Limits = .{ .file = false, - .temporary_file = false, + .temporary_file = .disabled, .shared_memory = false, }; }; @@ -72,6 +108,7 @@ pub const LoadingImage = struct { /// If this is a multi-chunk image, this should only be the FIRST /// chunk. pub fn init( + io: std.Io, alloc: Allocator, cmd: *const command.Command, limits: Limits, @@ -80,6 +117,10 @@ pub const LoadingImage = struct { // These can be overwritten by the data loading process. For example, // PNG loading sets the width/height from the data. const t = cmd.transmission().?; + + // Validated here rather than while parsing so the response can + // carry the image id, matching Kitty's initialize_load_data. + if (t.format_unknown) return error.UnsupportedFormat; var result: LoadingImage = .{ .image = .{ .id = t.image_id, @@ -88,10 +129,20 @@ pub const LoadingImage = struct { .height = t.height, .compression = t.compression, .format = t.format, + .metadata = .{ .transient = t.usage.transient }, }, .display = cmd.display(), .quiet = cmd.quiet, + .response = .{ + .id = t.image_id, + .image_number = t.image_number, + .placement_id = t.placement_id, + }, + .temporary_directory = switch (limits.temporary_file) { + .enabled => |d| d.directory, + .disabled => null, + }, }; // Special case for the direct medium, we just add the chunk directly. @@ -115,7 +166,7 @@ pub const LoadingImage = struct { switch (t.medium) { .direct => unreachable, .file => if (!limits.file) return error.UnsupportedMedium, - .temporary_file => if (!limits.temporary_file) return error.UnsupportedMedium, + .temporary_file => if (limits.temporary_file == .disabled) return error.UnsupportedMedium, .shared_memory => if (!limits.shared_memory) return error.UnsupportedMedium, } } @@ -124,29 +175,18 @@ pub const LoadingImage = struct { if (comptime builtin.os.tag != .windows) { if (std.mem.indexOfScalar(u8, cmd.data, 0) != null) { - // posix.realpath *asserts* that the path does not have - // internal nulls instead of erroring. - log.warn("failed to get absolute path: BadPathName", .{}); + // POSIX paths cannot contain internal nulls. + log.warn("invalid image path: BadPathName", .{}); return error.InvalidData; } } - var abs_buf: [std.fs.max_path_bytes]u8 = undefined; - const path = switch (t.medium) { - .direct => unreachable, // handled above - .file, .temporary_file => posix.realpath(cmd.data, &abs_buf) catch |err| { - log.warn("failed to get absolute path: {}", .{err}); - return error.InvalidData; - }, - .shared_memory => cmd.data, - }; - // Depending on the medium, load the data from the path. switch (t.medium) { .direct => unreachable, // handled above - .file => try result.readFile(.file, alloc, t, path), - .temporary_file => try result.readFile(.temporary_file, alloc, t, path), - .shared_memory => try result.readSharedMemory(alloc, t, path), + .file => try result.readFile(.file, io, alloc, t, cmd.data), + .temporary_file => try result.readFile(.temporary_file, io, alloc, t, cmd.data), + .shared_memory => try result.readSharedMemory(io, alloc, t, cmd.data), } return result; @@ -155,6 +195,7 @@ pub const LoadingImage = struct { /// Reads the data from a shared memory segment. fn readSharedMemory( self: *LoadingImage, + io: std.Io, alloc: Allocator, t: command.Transmission, path: []const u8, @@ -170,12 +211,21 @@ pub const LoadingImage = struct { return error.UnsupportedMedium; } + // POSIX shared memory names must begin with a slash, contain at + // least one character after it, contain no other slashes, and fit + // within NAME_MAX. Some shm_open implementations accept names + // without the leading slash, but the Kitty protocol does not. + if (!validSharedMemoryName(path, posix.NAME_MAX)) { + log.warn("invalid shared memory name", .{}); + return error.InvalidData; + } + // Since we're only supporting posix then max_path_bytes should // be enough to stack allocate the path. var buf: [std.fs.max_path_bytes]u8 = undefined; const pathz = std.fmt.bufPrintZ(&buf, "{s}", .{path}) catch return error.InvalidData; - const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY })), 0); + const fd = std.c.shm_open(pathz, @as(c_int, @bitCast(std.c.O{ .ACCMODE = .RDONLY })), @as(u16, 0)); switch (std.posix.errno(fd)) { .SUCCESS => {}, else => |err| { @@ -183,46 +233,34 @@ pub const LoadingImage = struct { return error.InvalidData; }, } - defer _ = std.c.close(fd); + const file: std.Io.File = .{ + .handle = fd, + .flags = .{ .nonblocking = false }, + }; + + defer file.close(io); defer _ = std.c.shm_unlink(pathz); // The size from stat on may be larger than our expected size because // shared memory has to be a multiple of the page size. const stat_size: usize = stat: { - const stat = std.posix.fstat(fd) catch |err| { + const stat = file.stat(io) catch |err| { log.warn("unable to fstat shared memory {s}: {}", .{ path, err }); return error.InvalidData; }; if (stat.size <= 0) return error.InvalidData; - break :stat @intCast(stat.size); - }; - - const expected_size: usize = switch (self.image.format) { - // Png we decode the full data size because later decoding will - // get the proper dimensions and assert validity. - .png => stat_size, - - // For these formats we have a size we must have. - .gray, .gray_alpha, .rgb, .rgba => size: { - const bpp = command.Transmission.formatBpp(self.image.format); - break :size self.image.width * self.image.height * bpp; - }, + break :stat std.math.cast(usize, stat.size) orelse + return error.InvalidData; }; - // Our stat size must be at least the expected size otherwise - // the shared memory data is invalid. - if (stat_size < expected_size) { - log.warn( - "shared memory size too small expected={} actual={}", - .{ expected_size, stat_size }, - ); - return error.InvalidData; - } + // Get the memory range we'll read. Validate it to make sure + // it doesn't overflow. + const range = try self.sharedMemoryRange(t, stat_size); const map = std.posix.mmap( null, stat_size, // mmap always uses the stat size - std.c.PROT.READ, + .{ .READ = true }, std.c.MAP{ .TYPE = .SHARED }, fd, 0, @@ -232,16 +270,63 @@ pub const LoadingImage = struct { }; defer std.posix.munmap(map); - // Our end size always uses the expected size so we cut off the - // padding for mmap alignment. - const start: usize = @intCast(t.offset); - const end: usize = if (t.size > 0) @min( - @as(usize, @intCast(t.offset)) + @as(usize, @intCast(t.size)), - expected_size, - ) else expected_size; - assert(self.data.items.len == 0); - try self.data.appendSlice(alloc, map[start..end]); + try self.data.appendSlice(alloc, map[range.start..range.end]); + } + + const SharedMemoryRange = struct { + start: usize, + end: usize, + }; + + /// Returns the byte range to copy from a shared memory object. + fn sharedMemoryRange( + self: *const LoadingImage, + t: command.Transmission, + stat_size: usize, + ) error{ + InvalidData, + DimensionsTooLarge, + }!SharedMemoryRange { + const expected_size: ?usize = switch (self.image.format) { + // PNG dimensions come from the decoded data. + .png => null, + + // Validate before multiplying because protocol dimensions are + // u32 values and may otherwise overflow in safe builds. + .gray, .gray_alpha, .rgb, .rgba => size: { + if (self.image.width > max_dimension or + self.image.height > max_dimension) + { + return error.DimensionsTooLarge; + } + + const bpp: usize = command.Transmission.formatBpp(self.image.format); + break :size @as(usize, self.image.width) * + @as(usize, self.image.height) * bpp; + }, + }; + + // Get our start offset and validate its within the range of + // the statted data. + const start = std.math.cast(usize, t.offset) orelse + return error.InvalidData; + if (start > stat_size) return error.InvalidData; + + // Validate that our length is within the stat range too. + const available = stat_size - start; + const data_size: usize = if (t.size > 0) + std.math.cast(usize, t.size) orelse return error.InvalidData + else if (self.image.compression == .none and expected_size != null) + expected_size.? + else + available; + if (data_size > max_size or data_size > available) { + return error.InvalidData; + } + + // data_size <= available guarantees this addition cannot overflow. + return .{ .start = start, .end = start + data_size }; } /// Reads the data from a temporary file and returns it. This allocates @@ -251,6 +336,7 @@ pub const LoadingImage = struct { fn readFile( self: *LoadingImage, comptime medium: command.Transmission.Medium, + io: std.Io, alloc: Allocator, t: command.Transmission, path: []const u8, @@ -260,37 +346,78 @@ pub const LoadingImage = struct { else => @compileError("readFile only supports file and temporary_file"), } - // Verify file seems "safe". This is logic copied directly from Kitty, - // mostly. This is really rough but it will catch obvious bad actors. - if (std.mem.startsWith(u8, path, "/proc/") or - std.mem.startsWith(u8, path, "/sys/") or - (std.mem.startsWith(u8, path, "/dev/") and - !std.mem.startsWith(u8, path, "/dev/shm/"))) - { - return error.InvalidData; + // Some Windows paths are dangerous to even open, so the raw path + // is checked before the open. The canonical path of the opened + // file is checked again in validatedFilePath. See kitty_windows + // for what is refused and why. + if (comptime builtin.os.tag == .windows) { + kitty_windows.checkPath(path) catch |err| { + log.warn("invalid image path: {}", .{err}); + return error.InvalidData; + }; } - // Temporary file logic - if (medium == .temporary_file) { - if (!isPathInTempDir(path)) return error.TemporaryFileNotInTempDir; - if (std.mem.indexOf(u8, path, "tty-graphics-protocol") == null) { - return error.TemporaryFileNotNamedCorrectly; + // The canonical path buffer is sized for the longest path the + // platform allows, which is about 96 KiB on Windows, so it is + // heap allocated rather than placed on the stack of whichever + // host thread feeds the stream. It is allocated before the open + // so the deferred temporary file deletion below can still use it. + const abs_buf = try alloc.alloc(u8, std.fs.max_path_bytes); + defer alloc.free(abs_buf); + + // Open our file right away before we do validation. This avoids + // TOCTOU issues. + var file = std.Io.Dir.cwd().openFile( + io, + path, + .{}, + ) catch |err| { + log.warn("failed to open image file: {}", .{err}); + return error.InvalidData; + }; + + // We'll populate a delete path if this is a temporary file. + var delete_path: ?[]const u8 = null; + defer { + file.close(io); + if (delete_path) |p| { + std.Io.Dir.cwd().deleteFile(io, p) catch |err| { + log.warn("failed to delete temporary file: {}", .{err}); + }; } } - defer if (medium == .temporary_file) { - posix.unlink(path) catch |err| { - log.warn("failed to delete temporary file: {}", .{err}); - }; - }; - var file = std.fs.cwd().openFile(path, .{}) catch |err| { - log.warn("failed to open temporary file: {}", .{err}); + // Derive the path from the open handle so the file we validate is the + // exact file we read. Resolving a path before opening it would allow a + // cooperating process to swap a symlink or directory entry in between. + const abs_path = validatedFilePath( + io, + file, + abs_buf, + ) catch |err| { + log.warn("failed to validate image file path: {}", .{err}); return error.InvalidData; }; - defer file.close(); + + // Temporary file logic + if (medium == .temporary_file) { + assert(self.temporary_directory != null); + if (!try isPathInTempDir( + io, + alloc, + self.temporary_directory.?, + abs_path, + )) return error.TemporaryFileNotInTempDir; + if (std.mem.indexOf( + u8, + abs_path, + "tty-graphics-protocol", + ) == null) return error.TemporaryFileNotNamedCorrectly; + delete_path = abs_path; + } // File must be a regular file - if (file.stat()) |stat| { + if (file.stat(io)) |stat| { if (stat.kind != .file) { log.warn("file is not a regular file kind={}", .{stat.kind}); return error.InvalidData; @@ -300,46 +427,96 @@ pub const LoadingImage = struct { return error.InvalidData; } + var buf: [4096]u8 = undefined; + var buf_reader = file.reader(io, &buf); if (t.offset > 0) { - file.seekTo(@intCast(t.offset)) catch |err| { + buf_reader.seekTo(@intCast(t.offset)) catch |err| { log.warn("failed to seek to offset {}: {}", .{ t.offset, err }); return error.InvalidData; }; } - - var buf: [4096]u8 = undefined; - var buf_reader = file.reader(&buf); const reader = &buf_reader.interface; // Read the file var managed: std.ArrayList(u8) = .empty; errdefer managed.deinit(alloc); - const size: usize = if (t.size > 0) @min(t.size, max_size) else max_size; - reader.appendRemaining(alloc, &managed, .limited(size)) catch { - log.warn("failed to read temporary file: {?}", .{buf_reader.err}); - return error.InvalidData; - }; + if (t.size > 0) { + // S is the exact number of bytes to read, not a maximum: + // https://sw.kovidgoyal.net/kitty/graphics-protocol/#local-client + const size = std.math.cast(usize, t.size) orelse + return error.InvalidData; + if (size > max_size) return error.InvalidData; + + // Read exact size + const data = reader.readAlloc(alloc, size) catch |err| { + log.warn("failed to read image file: {}", .{err}); + return switch (err) { + error.OutOfMemory => error.OutOfMemory, + else => error.InvalidData, + }; + }; + managed = .{ .items = data, .capacity = data.len }; + } else { + reader.appendRemaining(alloc, &managed, .limited(max_size)) catch { + log.warn("failed to read image file: {?}", .{buf_reader.err}); + return error.InvalidData; + }; + } // Set our data assert(self.data.items.len == 0); self.data = .{ .items = managed.items, .capacity = managed.capacity }; } + /// Returns the canonical path of an open file after applying the file + /// transmission blocklist. + fn validatedFilePath(io: std.Io, file: std.Io.File, buf: []u8) ![]const u8 { + const path = buf[0..try file.realPath(io, buf)]; + + if (comptime builtin.os.tag == .windows) { + try kitty_windows.checkCanonicalPath(path); + return path; + } + + // This is logic copied directly from Kitty, mostly. This is really + // rough but it will catch obvious bad actors. + if (std.mem.startsWith(u8, path, "/proc/") or + std.mem.startsWith(u8, path, "/sys/") or + (std.mem.startsWith(u8, path, "/dev/") and + !std.mem.startsWith(u8, path, "/dev/shm/"))) + { + return error.InvalidData; + } + + return path; + } + /// Returns true if path appears to be in a temporary directory. /// Copies logic from Kitty. - fn isPathInTempDir(path: []const u8) bool { - if (std.mem.startsWith(u8, path, "/tmp")) return true; - if (std.mem.startsWith(u8, path, "/dev/shm")) return true; - const dir = temp_dir.allocTmpDir(std.heap.page_allocator) catch return false; - defer temp_dir.freeTmpDir(std.heap.page_allocator, dir); - if (std.mem.startsWith(u8, path, dir)) return true; + fn isPathInTempDir( + io: std.Io, + alloc: Allocator, + dir: []const u8, + path: []const u8, + ) Allocator.Error!bool { + if (isPathInDir("/tmp", path)) return true; + if (isPathInDir("/dev/shm", path)) return true; + if (isPathInDir(dir, path)) return true; // The temporary dir is sometimes a symlink. On macOS for - // example /tmp is /private/var/... - var buf: [std.fs.max_path_bytes]u8 = undefined; - if (posix.realpath(dir, &buf)) |real_dir| { - if (std.mem.startsWith(u8, path, real_dir)) return true; - } else |_| {} + // example /tmp is /private/var/... On Windows the directory a + // host passes (typically GetTempPath output) regularly differs + // from the canonical path in case or uses 8.3 short names, and + // resolving it covers both. The buffer is heap allocated for + // the reason given in readFile. + const buf = try alloc.alloc(u8, std.fs.max_path_bytes); + defer alloc.free(buf); + const real_dir = buf[0 .. std.Io.Dir.cwd().realPathFile( + io, + dir, + buf, + ) catch return false]; + if (isPathInDir(real_dir, path)) return true; return false; } @@ -389,11 +566,23 @@ pub const LoadingImage = struct { if (img.width == 0 or img.height == 0) return error.DimensionsRequired; if (img.width > max_dimension or img.height > max_dimension) return error.DimensionsTooLarge; - // Data length must be what we expect + // Data length must be what we expect. const bpp = command.Transmission.formatBpp(img.format); const expected_len = img.width * img.height * bpp; const actual_len = self.data.items.len; - if (actual_len != expected_len) { + if (self.frame != null) { + // Kitty allows animation frames to exceed their expected length + // and just truncates it. Not sure if thats expected but lets + // allow it too. + if (actual_len < expected_len) { + std.log.warn( + "insufficient frame data image id={} expected_len={} actual_len={}", + .{ img.id, expected_len, actual_len }, + ); + return error.InsufficientData; + } + self.data.items.len = expected_len; + } else if (actual_len != expected_len) { std.log.warn( "unexpected length image id={} width={} height={} bpp={} expected_len={} actual_len={}", .{ img.id, img.width, img.height, bpp, expected_len, actual_len }, @@ -403,7 +592,7 @@ pub const LoadingImage = struct { // Everything looks good, copy the image data over. var result = self.image; - result.data = try self.data.toOwnedSlice(alloc); + result.data = .{ .complete = try self.data.toOwnedSlice(alloc) }; errdefer result.deinit(alloc); self.image = .{}; return result; @@ -411,7 +600,7 @@ pub const LoadingImage = struct { /// Debug function to write the data to a file. This is useful for /// capturing some test data for unit tests. - pub fn debugDump(self: LoadingImage) !void { + pub fn debugDump(io: std.Io, self: LoadingImage) !void { if (comptime builtin.mode != .Debug) @compileError("debugDump in non-debug"); var buf: [1024]u8 = undefined; @@ -426,9 +615,9 @@ pub const LoadingImage = struct { self.image.id, }, ); - const cwd = std.fs.cwd(); - const f = try cwd.createFile(filename, .{}); - defer f.close(); + const cwd = std.Io.Dir.cwd(); + const f = try cwd.createFile(io, filename, .{}); + defer f.close(io); const writer = f.writer(); try writer.writeAll(self.data.items); @@ -470,14 +659,20 @@ pub const LoadingImage = struct { const decode_png_fn = sys.decode_png orelse return error.UnsupportedFormat; + + var limited: LimitedAllocator = .init(alloc, max_size); + const decode_alloc = limited.allocator(); const result = decode_png_fn( - alloc, + decode_alloc, self.data.items, ) catch |err| switch (err) { error.InvalidData => return error.InvalidData, - error.OutOfMemory => return error.OutOfMemory, + error.OutOfMemory => if (limited.limit_exceeded) + return error.InvalidData + else + return error.OutOfMemory, }; - defer alloc.free(result.data); + defer decode_alloc.free(result.data); if (result.data.len > max_size) { log.warn("png image too large size={} max_size={}", .{ result.data.len, max_size }); @@ -486,7 +681,7 @@ pub const LoadingImage = struct { // Replace our data self.data.deinit(alloc); - self.data = .{}; + self.data = .empty; try self.data.ensureUnusedCapacity(alloc, result.data.len); try self.data.appendSlice(alloc, result.data[0..result.data.len]); @@ -497,13 +692,13 @@ pub const LoadingImage = struct { } }; -/// Image represents a single fully loaded image. +/// Image represents a single image whose metadata is fully known. /// -/// The image data is always fully decoded raw pixels: loading inflates -/// any zlib-compressed payload and decodes PNG into RGBA before an image -/// is completed, so `compression` is always `.none` and `format` is -/// never `.png` for a stored image, and `data.len` always equals -/// `width * height * bytes-per-pixel`. +/// Complete image data is always fully decoded raw pixels: loading inflates +/// any zlib-compressed payload and decodes PNG into RGBA before an image is +/// completed, so `compression` is always `.none` and `format` is never `.png` +/// for a stored image. Pending image data reserves the exact decoded byte +/// length that will be attached later. pub const Image = struct { id: u32 = 0, number: u32 = 0, @@ -511,7 +706,23 @@ pub const Image = struct { height: u32 = 0, format: command.Transmission.Format = .rgb, compression: command.Transmission.Compression = .none, - data: []const u8 = "", + data: Data = .{ .complete = "" }, + metadata: packed struct(u32) { + /// The image's transient usage hint, used to prioritize eviction. + transient: bool = false, + + /// Set this if the image was loaded without an ID or number. Such + /// images must not receive responses. Kitty gives these client ID + /// 0 (unaddressable); our storage keys everything by one public + /// u32 ID, so they get an ID from the upper half of the range + /// that is guaranteed unused at assignment time, but a client + /// that explicitly transmits that ID later can still replace + /// them. + implicit_id: bool = false, + + /// Number of placements referencing this image. + placement_count: u30 = 0, + } = .{}, /// Unique, monotonically increasing stamp assigned each time an /// image is added to (or replaced in) an ImageStorage. A changed @@ -519,15 +730,25 @@ pub const Image = struct { /// have changed, even if the dimensions and byte length are the /// same (e.g. a retransmission of the same ID). Stamps order by /// transmission time. Zero means "never stored". + /// + /// For animated images this also changes whenever the frame that + /// should be displayed changes (advance, edit, or delete of the + /// current frame), since consumers key texture caches off it. generation: u64 = 0, - /// Set this to true if this image was loaded by a command that - /// doesn't specify an ID or number, since such commands should - /// not be responded to, even though we do currently give them - /// IDs in the public range (which is bad!). - implicit_id: bool = false, + /// Animation state, non-null once any animation command (a=f, + /// a=a) has attached animation state to this image. Owned by the + /// image; replaced/retransmitted images drop it, which implements + /// the protocol's "retransmission resets the animation" rule. + /// + /// This is only ever attached to images stored in an ImageStorage + /// and must only be mutated through the storage's own pointer + /// (Image values are copied around freely; copies share this + /// pointer and never own it). + animation: ?*animation.Animation = null, pub const Error = error{ + InsufficientData, InvalidData, DecompressionFailed, DimensionsRequired, @@ -540,14 +761,97 @@ pub const Image = struct { UnsupportedDepth, }; + pub const Data = union(enum) { + /// Owned, decoded image bytes. The empty default is not allocated. + complete: []const u8, + + /// Expected decoded byte length for a payload that has not arrived. + pending: usize, + + /// Bytes reserved against the storage limit. + pub fn len(self: Data) usize { + return switch (self) { + .complete => |data| data.len, + .pending => |expected_len| expected_len, + }; + } + + /// Returns decoded bytes when the payload is complete. + pub fn bytes(self: Data) ?[]const u8 { + return switch (self) { + .complete => |data| data, + .pending => null, + }; + } + + pub fn isPending(self: Data) bool { + return self == .pending; + } + + pub fn deinit(self: *Data, alloc: Allocator) void { + switch (self.*) { + .complete => |data| if (data.len > 0) alloc.free(data), + .pending => {}, + } + } + }; + pub fn deinit(self: *Image, alloc: Allocator) void { - if (self.data.len > 0) alloc.free(self.data); + self.data.deinit(alloc); + if (self.animation) |anim| { + anim.deinit(alloc); + alloc.destroy(anim); + self.animation = null; + } + } + + /// The pixel data that should be displayed for this image. For an + /// animated image this is the current animation frame; otherwise + /// (and for the root frame) it is the image's own data. + pub fn renderData(self: *const Image) Data { + if (self.animation) |anim| { + if (anim.current_index > 0) { + return .{ .complete = anim.frames.items[anim.current_index - 1].data }; + } + } + + return self.data; + } + + /// The pixel data of the given 1-based animation frame number, or + /// null if the frame doesn't exist. Frame 1 (the root frame) + /// always exists as long as the image data is complete, even for + /// images without animation state. + /// + /// The returned slice is owned by the image (or its animation) + /// and remains valid until the image or frame is mutated. + pub fn frameData(self: *const Image, number: u32) ?[]const u8 { + switch (number) { + 0 => return null, + 1 => return self.data.bytes(), + else => { + const anim = self.animation orelse return null; + // Minus 2 because frame is 1-based and frame 1 is the + // image base data, so the animation frames start at frame 2. + const idx = number - 2; + if (idx >= anim.frames.items.len) return null; + return anim.frames.items[idx].data; + }, + } + } + + /// Total bytes of pixel data reserved against the storage limit + /// for this image: the base data plus any animation frames. + pub fn storageSize(self: *const Image) usize { + var total: usize = self.data.len(); + if (self.animation) |anim| total += anim.frameBytes(); + return total; } /// Mostly for logging pub fn withoutData(self: *const Image) Image { var copy = self.*; - copy.data = ""; + if (copy.data == .complete) copy.data = .{ .complete = "" }; return copy; } }; @@ -558,13 +862,168 @@ pub const Image = struct { pub const Rect = struct { top_left: PageList.Pin, bottom_right: PageList.Pin, + + /// Returns true if the grid cell is inside this rectangle. Pin.isBetween + /// compares page order, so its interior rows intentionally do not constrain + /// x. Check the column independently and use isBetween only for the row. + pub fn contains(self: Rect, cell: PageList.Pin) bool { + if (cell.x < self.top_left.x or cell.x > self.bottom_right.x) return false; + + var row = cell; + row.x = self.top_left.x; + return row.isBetween(self.top_left, self.bottom_right); + } }; +/// Returns whether a name follows the POSIX shared memory name format. +fn validSharedMemoryName(name: []const u8, name_max: usize) bool { + if (name.len < 2 or name.len > name_max or name[0] != '/') return false; + for (name[1..]) |c| { + if (c == '/' or c == 0) return false; + } + + return true; +} + +/// Returns true if `path` is `dir` or is contained within it, requiring a +/// path-separator boundary so similarly prefixed directories do not match. +fn isPathInDir(dir: []const u8, path: []const u8) bool { + if (dir.len == 0 or !std.mem.startsWith(u8, path, dir)) return false; + if (path.len == dir.len or std.fs.path.isSep(dir[dir.len - 1])) return true; + return std.fs.path.isSep(path[dir.len]); +} + +test { + _ = kitty_windows; +} + +test "temporary file path must be inside directory" { + const testing = std.testing; + + try testing.expect(isPathInDir("/tmp", "/tmp/tty-graphics-protocol-image.data")); + try testing.expect(isPathInDir("/tmp/", "/tmp/tty-graphics-protocol-image.data")); + try testing.expect(isPathInDir("/tmp", "/tmp")); + + try testing.expect(!isPathInDir("", "/tmp/tty-graphics-protocol-image.data")); + try testing.expect(!isPathInDir("/tmp", "/tmpX/tty-graphics-protocol-image.data")); + try testing.expect(!isPathInDir("/dev/shm", "/dev/shm-evil/tty-graphics-protocol-image.data")); + try testing.expect(!isPathInDir("/custom/tmp", "/custom/tmp-suffix/tty-graphics-protocol-image.data")); +} + +test "shared memory names follow POSIX rules" { + const testing = std.testing; + + try testing.expect(validSharedMemoryName("/kitty", 8)); + try testing.expect(validSharedMemoryName("/1234567", 8)); + + try testing.expect(!validSharedMemoryName("", 8)); + try testing.expect(!validSharedMemoryName("/", 8)); + try testing.expect(!validSharedMemoryName("kitty", 8)); + try testing.expect(!validSharedMemoryName("/kitty/image", 16)); + try testing.expect(!validSharedMemoryName("/kitty\x00image", 16)); + try testing.expect(!validSharedMemoryName("/12345678", 8)); +} + +test "image load rejects invalid POSIX shared memory names" { + if (comptime builtin.abi.isAndroid() or + builtin.target.os.tag == .windows or + !builtin.link_libc) + { + return error.SkipZigTest; + } + + const testing = std.testing; + const alloc = testing.allocator; + + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .shared_memory, + .width = 1, + .height = 1, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, "kitty-without-leading-slash"), + }; + defer cmd.deinit(alloc); + + try testing.expectError( + error.InvalidData, + LoadingImage.init(testing.io, alloc, &cmd, .{ + .file = false, + .temporary_file = .disabled, + .shared_memory = true, + }), + ); +} + +test "shared memory range with offset and size" { + const testing = std.testing; + + const loading: LoadingImage = .{ + .image = .{ + .width = 1, + .height = 1, + .format = .rgb, + }, + .quiet = .no, + .temporary_directory = null, + }; + + const explicit = try loading.sharedMemoryRange(.{ + .offset = 2, + .size = 3, + }, 5); + try testing.expectEqual(@as(usize, 2), explicit.start); + try testing.expectEqual(@as(usize, 5), explicit.end); + + const implicit = try loading.sharedMemoryRange(.{ + .offset = 2, + }, 5); + try testing.expectEqual(@as(usize, 2), implicit.start); + try testing.expectEqual(@as(usize, 5), implicit.end); +} + +test "shared memory range rejects out of bounds offset" { + const loading: LoadingImage = .{ + .image = .{ + .width = 1, + .height = 1, + .format = .rgb, + }, + .quiet = .no, + .temporary_directory = null, + }; + + try std.testing.expectError( + error.InvalidData, + loading.sharedMemoryRange(.{ .offset = 4 }, 3), + ); +} + +test "shared memory range validates dimensions before multiplication" { + const loading: LoadingImage = .{ + .image = .{ + .width = std.math.maxInt(u32), + .height = std.math.maxInt(u32), + .format = .rgba, + }, + .quiet = .no, + .temporary_directory = null, + }; + + try std.testing.expectError( + error.DimensionsTooLarge, + loading.sharedMemoryRange(.{}, 1), + ); +} + // This specifically tests we ALLOW invalid RGB data because Kitty // documents that this should work. test "image load with invalid RGB data" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; // _Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\ var cmd: command.Command = .{ @@ -577,13 +1036,14 @@ test "image load with invalid RGB data" { .data = try alloc.dupe(u8, "AAAA"), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .direct); + var loading = try LoadingImage.init(io, alloc, &cmd, .direct); defer loading.deinit(alloc); } test "image load with image too wide" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -595,7 +1055,7 @@ test "image load with image too wide" { .data = try alloc.dupe(u8, "AAAA"), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .direct); + var loading = try LoadingImage.init(io, alloc, &cmd, .direct); defer loading.deinit(alloc); try testing.expectError(error.DimensionsTooLarge, loading.complete(alloc)); } @@ -603,6 +1063,7 @@ test "image load with image too wide" { test "image load with image too tall" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -614,7 +1075,7 @@ test "image load with image too tall" { .data = try alloc.dupe(u8, "AAAA"), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .direct); + var loading = try LoadingImage.init(io, alloc, &cmd, .direct); defer loading.deinit(alloc); try testing.expectError(error.DimensionsTooLarge, loading.complete(alloc)); } @@ -622,6 +1083,7 @@ test "image load with image too tall" { test "image load: rgb, zlib compressed, direct" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -638,7 +1100,7 @@ test "image load: rgb, zlib compressed, direct" { ), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .direct); + var loading = try LoadingImage.init(io, alloc, &cmd, .direct); defer loading.deinit(alloc); var img = try loading.complete(alloc); defer img.deinit(alloc); @@ -650,6 +1112,7 @@ test "image load: rgb, zlib compressed, direct" { test "image load: rgb, not compressed, direct" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -666,7 +1129,7 @@ test "image load: rgb, not compressed, direct" { ), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .direct); + var loading = try LoadingImage.init(io, alloc, &cmd, .direct); defer loading.deinit(alloc); var img = try loading.complete(alloc); defer img.deinit(alloc); @@ -678,6 +1141,7 @@ test "image load: rgb, not compressed, direct" { test "image load: rgb, zlib compressed, direct, chunked" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; const data = @embedFile("testdata/image-rgb-zlib_deflate-128x96-2147483647-raw.data"); @@ -695,13 +1159,13 @@ test "image load: rgb, zlib compressed, direct, chunked" { .data = try alloc.dupe(u8, data[0..1024]), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .direct); + var loading = try LoadingImage.init(io, alloc, &cmd, .direct); defer loading.deinit(alloc); // Read our remaining chunks - var fbs = std.io.fixedBufferStream(data[1024..]); + var fbs: std.Io.Reader = .fixed(data[1024..]); var buf: [1024]u8 = undefined; - while (fbs.reader().readAll(&buf)) |size| { + while (fbs.readSliceShort(&buf)) |size| { try loading.addData(alloc, buf[0..size]); if (size < buf.len) break; } else |err| return err; @@ -715,6 +1179,7 @@ test "image load: rgb, zlib compressed, direct, chunked" { test "image load: rgb, zlib compressed, direct, chunked with zero initial chunk" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; const data = @embedFile("testdata/image-rgb-zlib_deflate-128x96-2147483647-raw.data"); @@ -731,13 +1196,13 @@ test "image load: rgb, zlib compressed, direct, chunked with zero initial chunk" } }, }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .direct); + var loading = try LoadingImage.init(io, alloc, &cmd, .direct); defer loading.deinit(alloc); // Read our remaining chunks - var fbs = std.io.fixedBufferStream(data); + var fbs: std.Io.Reader = .fixed(data); var buf: [1024]u8 = undefined; - while (fbs.reader().readAll(&buf)) |size| { + while (fbs.readSliceShort(&buf)) |size| { try loading.addData(alloc, buf[0..size]); if (size < buf.len) break; } else |err| return err; @@ -751,17 +1216,18 @@ test "image load: rgb, zlib compressed, direct, chunked with zero initial chunk" test "image load: temporary file without correct path" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var tmp_dir = try temp_dir.TempDir.init(); - defer tmp_dir.deinit(); + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); - try tmp_dir.dir.writeFile(.{ + try tmp_dir.dir.writeFile(testing.io, .{ .sub_path = "image.data", .data = data, }); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try tmp_dir.dir.realpath("image.data", &buf); + const path = buf[0..try tmp_dir.dir.realPathFile(testing.io, "image.data", &buf)]; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -775,26 +1241,81 @@ test "image load: temporary file without correct path" { .data = try alloc.dupe(u8, path), }; defer cmd.deinit(alloc); - try testing.expectError(error.TemporaryFileNotNamedCorrectly, LoadingImage.init(alloc, &cmd, .all)); + var dir_path_buf: [std.fs.max_path_bytes]u8 = undefined; + try testing.expectError(error.TemporaryFileNotNamedCorrectly, LoadingImage.init( + io, + alloc, + &cmd, + .allWithTempDir(dir_path_buf[0..try tmp_dir.dir.realPath(testing.io, &dir_path_buf)]), + )); // Temporary file should still be there - try tmp_dir.dir.access(path, .{}); + try tmp_dir.dir.access(testing.io, path, .{}); +} + +test "image load: temporary file outside directory prefix is rejected" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + try tmp_dir.dir.createDir(io, "temp", .default_dir); + try tmp_dir.dir.createDir(io, "temp-suffix", .default_dir); + + var trusted_dir = try tmp_dir.dir.openDir(io, "temp", .{}); + defer trusted_dir.close(io); + var outside_dir = try tmp_dir.dir.openDir(io, "temp-suffix", .{}); + defer outside_dir.close(io); + + const filename = "tty-graphics-protocol-image.data"; + const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); + try outside_dir.writeFile(io, .{ + .sub_path = filename, + .data = data, + }); + + var trusted_path_buf: [std.fs.max_path_bytes]u8 = undefined; + const trusted_path = trusted_path_buf[0..try trusted_dir.realPath(io, &trusted_path_buf)]; + var outside_path_buf: [std.fs.max_path_bytes]u8 = undefined; + const outside_path = outside_path_buf[0..try outside_dir.realPathFile(io, filename, &outside_path_buf)]; + + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .temporary_file, + .compression = .none, + .width = 20, + .height = 15, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, outside_path), + }; + defer cmd.deinit(alloc); + try testing.expectError( + error.TemporaryFileNotInTempDir, + LoadingImage.init(io, alloc, &cmd, .allWithTempDir(trusted_path)), + ); + + // Rejection must happen before temporary-file cleanup is armed. + try outside_dir.access(io, filename, .{}); } test "image load: rgb, not compressed, temporary file" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var tmp_dir = try temp_dir.TempDir.init(); - defer tmp_dir.deinit(); + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); - try tmp_dir.dir.writeFile(.{ + try tmp_dir.dir.writeFile(testing.io, .{ .sub_path = "tty-graphics-protocol-image.data", .data = data, }); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try tmp_dir.dir.realpath("tty-graphics-protocol-image.data", &buf); + const path = buf[0..try tmp_dir.dir.realPathFile(testing.io, "tty-graphics-protocol-image.data", &buf)]; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -808,30 +1329,37 @@ test "image load: rgb, not compressed, temporary file" { .data = try alloc.dupe(u8, path), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .all); + var dir_path_buf: [std.fs.max_path_bytes]u8 = undefined; + var loading = try LoadingImage.init( + io, + alloc, + &cmd, + .allWithTempDir(dir_path_buf[0..try tmp_dir.dir.realPath(testing.io, &dir_path_buf)]), + ); defer loading.deinit(alloc); var img = try loading.complete(alloc); defer img.deinit(alloc); try testing.expect(img.compression == .none); // Temporary file should be gone - try testing.expectError(error.FileNotFound, tmp_dir.dir.access(path, .{})); + try testing.expectError(error.FileNotFound, tmp_dir.dir.access(testing.io, path, .{})); } test "image load: rgb, not compressed, regular file" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var tmp_dir = try temp_dir.TempDir.init(); - defer tmp_dir.deinit(); + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); - try tmp_dir.dir.writeFile(.{ + try tmp_dir.dir.writeFile(testing.io, .{ .sub_path = "image.data", .data = data, }); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try tmp_dir.dir.realpath("image.data", &buf); + const path = buf[0..try tmp_dir.dir.realPathFile(testing.io, "image.data", &buf)]; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -845,12 +1373,508 @@ test "image load: rgb, not compressed, regular file" { .data = try alloc.dupe(u8, path), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .all); + var dir_path_buf: [std.fs.max_path_bytes]u8 = undefined; + var loading = try LoadingImage.init( + io, + alloc, + &cmd, + .allWithTempDir(dir_path_buf[0..try tmp_dir.dir.realPath(testing.io, &dir_path_buf)]), + ); defer loading.deinit(alloc); var img = try loading.complete(alloc); defer img.deinit(alloc); try testing.expect(img.compression == .none); - try tmp_dir.dir.access(path, .{}); + try tmp_dir.dir.access(testing.io, path, .{}); +} + +test "image load: regular file size reads exactly requested bytes" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + try tmp_dir.dir.writeFile(io, .{ + .sub_path = "image.data", + .data = &.{ 1, 2, 3, 4, 5, 6 }, + }); + + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + const path = path_buf[0..try tmp_dir.dir.realPathFile( + io, + "image.data", + &path_buf, + )]; + + const cases = [_]struct { + offset: u32, + expected: [3]u8, + }{ + .{ .offset = 0, .expected = .{ 1, 2, 3 } }, + .{ .offset = 3, .expected = .{ 4, 5, 6 } }, + }; + for (cases) |case| { + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .file, + .width = 1, + .height = 1, + .size = 3, + .offset = case.offset, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, path), + }; + defer cmd.deinit(alloc); + + var loading = try LoadingImage.init(io, alloc, &cmd, .{ + .file = true, + .temporary_file = .disabled, + .shared_memory = false, + }); + defer loading.deinit(alloc); + var img = try loading.complete(alloc); + defer img.deinit(alloc); + + try testing.expectEqualSlices(u8, &case.expected, img.data.complete); + } +} + +test "image load: regular file size rejects short data" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + try tmp_dir.dir.writeFile(io, .{ + .sub_path = "image.data", + .data = &.{ 1, 2 }, + }); + + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + const path = path_buf[0..try tmp_dir.dir.realPathFile( + io, + "image.data", + &path_buf, + )]; + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .file, + .width = 1, + .height = 1, + .size = 3, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, path), + }; + defer cmd.deinit(alloc); + + try testing.expectError( + error.InvalidData, + LoadingImage.init(io, alloc, &cmd, .{ + .file = true, + .temporary_file = .disabled, + .shared_memory = false, + }), + ); +} + +test "image load: rgb, not compressed, relative regular file" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); + try tmp_dir.dir.writeFile(testing.io, .{ + .sub_path = "image.data", + .data = data, + }); + + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .file, + .compression = .none, + .width = 20, + .height = 15, + .image_id = 31, + } }, + .data = try std.fmt.allocPrint( + alloc, + ".zig-cache/tmp/{s}/image.data", + .{tmp_dir.sub_path}, + ), + }; + defer cmd.deinit(alloc); + var loading = try LoadingImage.init(io, alloc, &cmd, .{ + .file = true, + .temporary_file = .disabled, + .shared_memory = false, + }); + defer loading.deinit(alloc); + var img = try loading.complete(alloc); + defer img.deinit(alloc); + try testing.expect(img.compression == .none); +} + +test "image load: blocklist applies to opened file after symlink swap" { + if (builtin.os.tag == .windows) return error.SkipZigTest; + + const testing = std.testing; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + try tmp_dir.dir.writeFile(io, .{ + .sub_path = "safe.data", + .data = "safe", + }); + try tmp_dir.dir.symLink(io, "/dev/null", "image.data", .{}); + + // Pin the blocked file, then simulate the cooperating process replacing + // the path with a safe target before validation. + const blocked_file = try tmp_dir.dir.openFile(io, "image.data", .{}); + defer blocked_file.close(io); + try tmp_dir.dir.symLinkAtomic(io, "safe.data", "image.data", .{}); + + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + try testing.expectError( + error.InvalidData, + LoadingImage.validatedFilePath(io, blocked_file, &path_buf), + ); + + // The pathname now resolves to the safe replacement, demonstrating that + // the rejection above came from the already-open file handle. + const safe_file = try tmp_dir.dir.openFile(io, "image.data", .{}); + defer safe_file.close(io); + _ = try LoadingImage.validatedFilePath(io, safe_file, &path_buf); +} + +test "image load: windows UNC path is rejected before open" { + if (builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + // Opening a UNC path would resolve the host name and authenticate to + // it over SMB, so the rejection has to happen on the raw path. The + // host name is in the reserved .invalid TLD so it can never resolve. + const paths = [_][]const u8{ + "\\\\nonexistent-host.invalid\\share\\tty-graphics-protocol-image.data", + "//nonexistent-host.invalid/share/tty-graphics-protocol-image.data", + "\\\\?\\UNC\\nonexistent-host.invalid\\share\\image.data", + }; + for (paths) |path| { + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .file, + .compression = .none, + .width = 20, + .height = 15, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, path), + }; + defer cmd.deinit(alloc); + try testing.expectError( + error.InvalidData, + LoadingImage.init(io, alloc, &cmd, .{ + .file = true, + .temporary_file = .disabled, + .shared_memory = false, + }), + ); + } +} + +test "image load: windows device namespace paths are rejected before open" { + if (builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + const filename = "tty-graphics-protocol-image.data"; + const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); + try tmp_dir.dir.writeFile(io, .{ .sub_path = filename, .data = data }); + + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir_path = dir_buf[0..try tmp_dir.dir.realPath(io, &dir_buf)]; + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + const real_path = path_buf[0..try tmp_dir.dir.realPathFile(io, filename, &path_buf)]; + + // The verbatim, local device and NT namespace spellings all resolve + // to the same real file, so rejecting them proves the check runs on + // the raw path rather than on the canonical one. + const prefixes = [_][]const u8{ "\\\\?\\", "\\\\.\\", "\\??\\" }; + for (prefixes) |prefix| { + const path = try std.mem.concat(alloc, u8, &.{ prefix, real_path }); + defer alloc.free(path); + + const mediums = [_]command.Transmission.Medium{ .file, .temporary_file }; + for (mediums) |medium| { + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = medium, + .compression = .none, + .width = 20, + .height = 15, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, path), + }; + defer cmd.deinit(alloc); + try testing.expectError( + error.InvalidData, + LoadingImage.init(io, alloc, &cmd, .allWithTempDir(dir_path)), + ); + } + } + + // A named pipe reached through the local device namespace. + { + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .file, + .compression = .none, + .width = 20, + .height = 15, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, "\\\\.\\pipe\\ghostty-kitty-graphics-test"), + }; + defer cmd.deinit(alloc); + try testing.expectError( + error.InvalidData, + LoadingImage.init(io, alloc, &cmd, .allWithTempDir(dir_path)), + ); + } + + // Nothing above reached the temporary file deletion. + try tmp_dir.dir.access(io, filename, .{}); +} + +test "image load: windows reserved device names are rejected before open" { + if (builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir_path = dir_buf[0..try tmp_dir.dir.realPath(io, &dir_buf)]; + + const names = [_][]const u8{ "CON", "NUL.data", "com1", "LPT9.png", "AUX", "PRN." }; + for (names) |name| { + const path = try std.fs.path.join(alloc, &.{ dir_path, name }); + defer alloc.free(path); + + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .file, + .compression = .none, + .width = 20, + .height = 15, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, path), + }; + defer cmd.deinit(alloc); + try testing.expectError( + error.InvalidData, + LoadingImage.init(io, alloc, &cmd, .{ + .file = true, + .temporary_file = .disabled, + .shared_memory = false, + }), + ); + } +} + +test "image load: windows local file accepted in forward slash and upper case spellings" { + if (builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + const filename = "image.data"; + const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); + try tmp_dir.dir.writeFile(io, .{ .sub_path = filename, .data = data }); + + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + const real_path = path_buf[0..try tmp_dir.dir.realPathFile(io, filename, &path_buf)]; + + // Both spellings open the same file; the canonical path the checks + // see is the backslash, on-disk case form either way. + const forward = try alloc.dupe(u8, real_path); + defer alloc.free(forward); + std.mem.replaceScalar(u8, forward, '\\', '/'); + const upper = try alloc.dupe(u8, real_path); + defer alloc.free(upper); + _ = std.ascii.upperString(upper, real_path); + + const spellings = [_][]const u8{ forward, upper }; + for (spellings) |path| { + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .file, + .compression = .none, + .width = 20, + .height = 15, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, path), + }; + defer cmd.deinit(alloc); + var loading = try LoadingImage.init(io, alloc, &cmd, .{ + .file = true, + .temporary_file = .disabled, + .shared_memory = false, + }); + defer loading.deinit(alloc); + var img = try loading.complete(alloc); + defer img.deinit(alloc); + try testing.expect(img.compression == .none); + } + + try tmp_dir.dir.access(io, filename, .{}); +} + +test "image load: windows temporary file with differently spelled directory" { + if (builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + const filename = "tty-graphics-protocol-image.data"; + const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); + try tmp_dir.dir.writeFile(io, .{ .sub_path = filename, .data = data }); + + var dir_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir_path = dir_buf[0..try tmp_dir.dir.realPath(io, &dir_buf)]; + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + const real_path = path_buf[0..try tmp_dir.dir.realPathFile(io, filename, &path_buf)]; + + // Hosts hand over GetTempPath output, which regularly differs from + // the canonical path in case (`C:\WINDOWS\TEMP\`), so the directory + // prefix check only passes through the real-path fallback. + const dir_upper = try alloc.dupe(u8, dir_path); + defer alloc.free(dir_upper); + _ = std.ascii.upperString(dir_upper, dir_path); + const path_upper = try alloc.dupe(u8, real_path); + defer alloc.free(path_upper); + _ = std.ascii.upperString(path_upper, real_path); + try testing.expect(!isPathInDir(dir_upper, real_path) or + std.mem.eql(u8, dir_upper, dir_path)); + + var cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .rgb, + .medium = .temporary_file, + .compression = .none, + .width = 20, + .height = 15, + .image_id = 31, + } }, + .data = try alloc.dupe(u8, path_upper), + }; + defer cmd.deinit(alloc); + var loading = try LoadingImage.init(io, alloc, &cmd, .allWithTempDir(dir_upper)); + defer loading.deinit(alloc); + var img = try loading.complete(alloc); + defer img.deinit(alloc); + try testing.expect(img.compression == .none); + + // Temporary file should be gone + try testing.expectError(error.FileNotFound, tmp_dir.dir.access(io, filename, .{})); +} + +test "image load: windows canonical path check accepts a local file opened through a device spelling" { + if (builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + const filename = "image.data"; + try tmp_dir.dir.writeFile(io, .{ .sub_path = filename, .data = "safe" }); + + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + const real_path = path_buf[0..try tmp_dir.dir.realPathFile(io, filename, &path_buf)]; + + // readFile refuses this spelling before the open, but the post-open + // check works from the canonical path and so does not care how the + // file was reached: `\\.\C:\...` canonicalizes back to `C:\...`. + const device_path = try std.mem.concat(alloc, u8, &.{ "\\\\.\\", real_path }); + defer alloc.free(device_path); + const file = try std.Io.Dir.cwd().openFile(io, device_path, .{}); + defer file.close(io); + + const canon_buf = try alloc.alloc(u8, std.fs.max_path_bytes); + defer alloc.free(canon_buf); + const canon = try LoadingImage.validatedFilePath(io, file, canon_buf); + try testing.expectEqualStrings(real_path, canon); +} + +test "image load: windows canonical path check rejects a file reached through a UNC share" { + if (builtin.os.tag != .windows) return error.SkipZigTest; + + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); + const filename = "image.data"; + try tmp_dir.dir.writeFile(io, .{ .sub_path = filename, .data = "safe" }); + + var path_buf: [std.fs.max_path_bytes]u8 = undefined; + const real_path = path_buf[0..try tmp_dir.dir.realPathFile(io, filename, &path_buf)]; + try testing.expect(kitty_windows.isDriveAbsolute(real_path)); + + // Reach the same local file through the loopback administrative + // share, which is how a junction or symlink into a share would look + // to the post-open check. The share needs the server service and an + // administrative token, so the test is skipped when the open fails. + const unc_path = try std.fmt.allocPrint( + alloc, + "\\\\localhost\\{c}$\\{s}", + .{ real_path[0], real_path[3..] }, + ); + defer alloc.free(unc_path); + const file = std.Io.Dir.cwd().openFile(io, unc_path, .{}) catch + return error.SkipZigTest; + defer file.close(io); + + const canon_buf = try alloc.alloc(u8, std.fs.max_path_bytes); + defer alloc.free(canon_buf); + try testing.expectError( + error.NotDriveAbsolute, + LoadingImage.validatedFilePath(io, file, canon_buf), + ); } test "image load: png, not compressed, regular file" { @@ -858,17 +1882,18 @@ test "image load: png, not compressed, regular file" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var tmp_dir = try temp_dir.TempDir.init(); - defer tmp_dir.deinit(); + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); const data = @embedFile("testdata/image-png-none-50x76-2147483647-raw.data"); - try tmp_dir.dir.writeFile(.{ + try tmp_dir.dir.writeFile(testing.io, .{ .sub_path = "tty-graphics-protocol-image.data", .data = data, }); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try tmp_dir.dir.realpath("tty-graphics-protocol-image.data", &buf); + const path = buf[0..try tmp_dir.dir.realPathFile(testing.io, "tty-graphics-protocol-image.data", &buf)]; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -882,18 +1907,91 @@ test "image load: png, not compressed, regular file" { .data = try alloc.dupe(u8, path), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .all); + var dir_path_buf: [std.fs.max_path_bytes]u8 = undefined; + var loading = try LoadingImage.init( + io, + alloc, + &cmd, + .allWithTempDir(dir_path_buf[0..try tmp_dir.dir.realPath(testing.io, &dir_path_buf)]), + ); defer loading.deinit(alloc); var img = try loading.complete(alloc); defer img.deinit(alloc); try testing.expect(img.compression == .none); try testing.expect(img.format == .rgba); - try tmp_dir.dir.access(path, .{}); + try tmp_dir.dir.access(testing.io, path, .{}); +} + +test "image load: png rejects oversized decoder allocation" { + const testing = std.testing; + + const oversized_decoder = struct { + fn decode( + alloc: Allocator, + _: []const u8, + ) sys.DecodeError!sys.Image { + const data = try alloc.alloc(u8, max_size + 1); + return .{ + .width = 1, + .height = 1, + .data = data, + }; + } + }.decode; + + const original_decode_png = sys.decode_png; + defer sys.decode_png = original_decode_png; + sys.decode_png = &oversized_decoder; + + // Fail any allocation which reaches the underlying allocator. The size + // limiter should reject the decoder's request before it gets that far. + var failing = testing.FailingAllocator.init(testing.allocator, .{ + .fail_index = 0, + }); + const alloc = failing.allocator(); + + var loading: LoadingImage = .{ + .image = .{ .format = .png }, + .quiet = .no, + .temporary_directory = null, + }; + defer loading.deinit(alloc); + + try testing.expectError(error.InvalidData, loading.complete(alloc)); + try testing.expect(!failing.has_induced_failure); +} + +test "image load: png rejects oversized Wuffs image before allocation" { + if (sys.decode_png == null) return error.SkipZigTest; + + const testing = std.testing; + const alloc = testing.allocator; + + // Turn the small test PNG into a 32768x32767 image. Its decoded RGBA + // size is just under Wuffs' 4 GiB package limit but over Kitty's 400 MiB + // limit, which previously allowed the large allocation to happen first. + var data = @embedFile("testdata/image-png-none-50x76-2147483647-raw.data").*; + std.mem.writeInt(u32, data[16..20], 32768, .big); + std.mem.writeInt(u32, data[20..24], 32767, .big); + std.mem.writeInt(u32, data[29..33], std.hash.Crc32.hash(data[12..29]), .big); + + const cmd: command.Command = .{ + .control = .{ .transmit = .{ + .format = .png, + .medium = .direct, + } }, + .data = &data, + }; + var loading = try LoadingImage.init(testing.io, alloc, &cmd, .direct); + defer loading.deinit(alloc); + + try testing.expectError(error.InvalidData, loading.complete(alloc)); } test "limits: direct medium always allowed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -908,24 +2006,25 @@ test "limits: direct medium always allowed" { defer cmd.deinit(alloc); // Direct medium should work even with the most restrictive limits - var loading = try LoadingImage.init(alloc, &cmd, .direct); + var loading = try LoadingImage.init(io, alloc, &cmd, .direct); defer loading.deinit(alloc); } test "limits: file medium blocked by limits" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var tmp_dir = try temp_dir.TempDir.init(); - defer tmp_dir.deinit(); + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); - try tmp_dir.dir.writeFile(.{ + try tmp_dir.dir.writeFile(testing.io, .{ .sub_path = "image.data", .data = data, }); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try tmp_dir.dir.realpath("image.data", &buf); + const path = buf[0..try tmp_dir.dir.realPathFile(testing.io, "image.data", &buf)]; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -939,23 +2038,24 @@ test "limits: file medium blocked by limits" { .data = try alloc.dupe(u8, path), }; defer cmd.deinit(alloc); - try testing.expectError(error.UnsupportedMedium, LoadingImage.init(alloc, &cmd, .direct)); + try testing.expectError(error.UnsupportedMedium, LoadingImage.init(io, alloc, &cmd, .direct)); } test "limits: file medium allowed by limits" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var tmp_dir = try temp_dir.TempDir.init(); - defer tmp_dir.deinit(); + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); - try tmp_dir.dir.writeFile(.{ + try tmp_dir.dir.writeFile(testing.io, .{ .sub_path = "image.data", .data = data, }); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try tmp_dir.dir.realpath("image.data", &buf); + const path = buf[0..try tmp_dir.dir.realPathFile(testing.io, "image.data", &buf)]; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -969,9 +2069,9 @@ test "limits: file medium allowed by limits" { .data = try alloc.dupe(u8, path), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .{ + var loading = try LoadingImage.init(io, alloc, &cmd, .{ .file = true, - .temporary_file = false, + .temporary_file = .disabled, .shared_memory = false, }); defer loading.deinit(alloc); @@ -980,17 +2080,18 @@ test "limits: file medium allowed by limits" { test "limits: temporary file medium blocked by limits" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var tmp_dir = try temp_dir.TempDir.init(); - defer tmp_dir.deinit(); + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); - try tmp_dir.dir.writeFile(.{ + try tmp_dir.dir.writeFile(testing.io, .{ .sub_path = "tty-graphics-protocol-image.data", .data = data, }); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try tmp_dir.dir.realpath("tty-graphics-protocol-image.data", &buf); + const path = buf[0..try tmp_dir.dir.realPathFile(testing.io, "tty-graphics-protocol-image.data", &buf)]; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -1004,30 +2105,31 @@ test "limits: temporary file medium blocked by limits" { .data = try alloc.dupe(u8, path), }; defer cmd.deinit(alloc); - try testing.expectError(error.UnsupportedMedium, LoadingImage.init(alloc, &cmd, .{ + try testing.expectError(error.UnsupportedMedium, LoadingImage.init(io, alloc, &cmd, .{ .file = true, - .temporary_file = false, + .temporary_file = .disabled, .shared_memory = true, })); // File should still exist since we blocked before reading - try tmp_dir.dir.access("tty-graphics-protocol-image.data", .{}); + try tmp_dir.dir.access(testing.io, "tty-graphics-protocol-image.data", .{}); } test "limits: temporary file medium allowed by limits" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var tmp_dir = try temp_dir.TempDir.init(); - defer tmp_dir.deinit(); + var tmp_dir = testing.tmpDir(.{}); + defer tmp_dir.cleanup(); const data = @embedFile("testdata/image-rgb-none-20x15-2147483647-raw.data"); - try tmp_dir.dir.writeFile(.{ + try tmp_dir.dir.writeFile(testing.io, .{ .sub_path = "tty-graphics-protocol-image.data", .data = data, }); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try tmp_dir.dir.realpath("tty-graphics-protocol-image.data", &buf); + const path = buf[0..try tmp_dir.dir.realPathFile(testing.io, "tty-graphics-protocol-image.data", &buf)]; var cmd: command.Command = .{ .control = .{ .transmit = .{ @@ -1041,10 +2143,19 @@ test "limits: temporary file medium allowed by limits" { .data = try alloc.dupe(u8, path), }; defer cmd.deinit(alloc); - var loading = try LoadingImage.init(alloc, &cmd, .{ - .file = false, - .temporary_file = true, - .shared_memory = false, - }); + var dir_path_buf: [std.fs.max_path_bytes]u8 = undefined; + var loading = try LoadingImage.init( + io, + alloc, + &cmd, + + .{ + .file = false, + .temporary_file = .{ + .enabled = .{ .directory = dir_path_buf[0..try tmp_dir.dir.realPath(testing.io, &dir_path_buf)] }, + }, + .shared_memory = false, + }, + ); defer loading.deinit(alloc); } diff --git a/vendor/libghostty-vt/src/terminal/kitty/graphics_pixel.zig b/vendor/libghostty-vt/src/terminal/kitty/graphics_pixel.zig new file mode 100644 index 00000000..1f446873 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/graphics_pixel.zig @@ -0,0 +1,235 @@ +//! Pixel-buffer operations for the Kitty graphics protocol: format +//! conversion to RGBA, background fill, and rectangle composition. +//! These are the primitives behind animation frame loading (a=f) and +//! frame composition (a=c). See graphics_animation.zig for the +//! animation model built on top of them. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const wuffs = @import("wuffs"); +const command = @import("graphics_command.zig"); + +/// Convert pixel data in the given format to a freshly allocated RGBA +/// buffer. The caller owns the result; the input is not freed. +/// +/// The input length must be a multiple of the format's bytes per +/// pixel; image loading validates data length against the image +/// dimensions before storing it. +pub fn rgbaFromFormat( + alloc: Allocator, + format: command.Transmission.Format, + data: []const u8, +) Allocator.Error![]u8 { + const result = switch (format) { + .rgba => return try alloc.dupe(u8, data), + .rgb => wuffs.swizzle.rgbToRgba(alloc, data), + .gray => wuffs.swizzle.gToRgba(alloc, data), + .gray_alpha => wuffs.swizzle.gaToRgba(alloc, data), + + // PNG is decoded to RGBA during image loading. + .png => unreachable, + }; + + return result catch |err| switch (err) { + error.OutOfMemory => error.OutOfMemory, + // These are fixed, supported swizzles; wuffs cannot fail to + // prepare them and nothing else in the conversion errors. + error.WuffsError, error.Overflow => unreachable, + }; +} + +/// Fill an RGBA buffer with the given background color. +pub fn fillBackground( + data: []u8, + bg: command.AnimationFrameLoading.Background, +) void { + const raw: u32 = @bitCast(bg); + if (raw == 0) { + @memset(data, 0); + return; + } + var i: usize = 0; + while (i + 4 <= data.len) : (i += 4) { + data[i + 0] = bg.r; + data[i + 1] = bg.g; + data[i + 2] = bg.b; + data[i + 3] = bg.a; + } +} + +/// Compose the `src` rectangle (src_width x src_height RGBA pixels) +/// onto the `dst` canvas (dst_width x dst_height RGBA pixels) with +/// its top-left corner at (x, y). Portions of the rectangle outside +/// the canvas are silently clipped, matching Kitty's a=f behavior. +pub fn composeRect( + dst: []u8, + dst_width: u32, + dst_height: u32, + src: []const u8, + src_width: u32, + src_height: u32, + x: u32, + y: u32, + mode: command.CompositionMode, +) void { + if (x >= dst_width or y >= dst_height) return; + const width: usize = @min(src_width, dst_width - x); + const height: usize = @min(src_height, dst_height - y); + for (0..height) |row| { + const dst_off = ((y + row) * dst_width + x) * 4; + const src_off = row * @as(usize, src_width) * 4; + composeRow( + dst[dst_off..][0 .. width * 4], + src[src_off..][0 .. width * 4], + mode, + ); + } +} + +/// Compose a width x height rectangle between two full-size RGBA +/// canvases sharing the same `canvas_width` stride, reading from +/// (src_x, src_y) in `src` and writing at (dst_x, dst_y) in `dst`. +/// The caller must have validated that both rectangles are within +/// bounds; a=c reports out-of-bounds rectangles as errors rather +/// than clipping. +pub fn composeCanvasRect( + dst: []u8, + src: []const u8, + canvas_width: u32, + width: u32, + height: u32, + src_x: u32, + src_y: u32, + dst_x: u32, + dst_y: u32, + mode: command.CompositionMode, +) void { + for (0..height) |row| { + const dst_off = ((dst_y + row) * @as(usize, canvas_width) + dst_x) * 4; + const src_off = ((src_y + row) * @as(usize, canvas_width) + src_x) * 4; + composeRow( + dst[dst_off..][0 .. @as(usize, width) * 4], + src[src_off..][0 .. @as(usize, width) * 4], + mode, + ); + } +} + +fn composeRow(dst: []u8, src: []const u8, mode: command.CompositionMode) void { + switch (mode) { + .overwrite => @memcpy(dst, src), + .alpha_blend => wuffs.swizzle.rgbaSrcOver(dst, src), + } +} + +test "rgba conversion" { + const testing = std.testing; + const alloc = testing.allocator; + + { + const rgb = [_]u8{ 1, 2, 3, 4, 5, 6 }; + const result = try rgbaFromFormat(alloc, .rgb, &rgb); + defer alloc.free(result); + try testing.expectEqualSlices(u8, &.{ 1, 2, 3, 255, 4, 5, 6, 255 }, result); + } + + { + const gray = [_]u8{ 7, 8 }; + const result = try rgbaFromFormat(alloc, .gray, &gray); + defer alloc.free(result); + try testing.expectEqualSlices(u8, &.{ 7, 7, 7, 255, 8, 8, 8, 255 }, result); + } + + { + const ga = [_]u8{ 7, 100, 8, 200 }; + const result = try rgbaFromFormat(alloc, .gray_alpha, &ga); + defer alloc.free(result); + try testing.expectEqualSlices(u8, &.{ 7, 7, 7, 100, 8, 8, 8, 200 }, result); + } + + { + const rgba = [_]u8{ 1, 2, 3, 4 }; + const result = try rgbaFromFormat(alloc, .rgba, &rgba); + defer alloc.free(result); + try testing.expectEqualSlices(u8, &rgba, result); + } +} + +test "fill background" { + var buf: [8]u8 = undefined; + fillBackground(&buf, .{ .r = 1, .g = 2, .b = 3, .a = 4 }); + try std.testing.expectEqualSlices(u8, &.{ 1, 2, 3, 4, 1, 2, 3, 4 }, &buf); + + fillBackground(&buf, .{}); + try std.testing.expectEqualSlices(u8, &(.{0} ** 8), &buf); +} + +test "compose rect overwrite with clipping" { + // 2x2 canvas, compose a 2x1 rect at (1, 1): only the first pixel + // of the rect fits, the rest clips off the right edge. + var dst = [_]u8{0} ** 16; + const src = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 }; + composeRect(&dst, 2, 2, &src, 2, 1, 1, 1, .overwrite); + + var expect = [_]u8{0} ** 16; + expect[12] = 1; + expect[13] = 2; + expect[14] = 3; + expect[15] = 4; + try std.testing.expectEqualSlices(u8, &expect, &dst); +} + +test "compose rect entirely out of bounds" { + var dst = [_]u8{9} ** 16; + const src = [_]u8{1} ** 4; + composeRect(&dst, 2, 2, &src, 1, 1, 2, 0, .overwrite); + composeRect(&dst, 2, 2, &src, 1, 1, 0, 2, .overwrite); + try std.testing.expectEqualSlices(u8, &(.{9} ** 16), &dst); +} + +test "alpha blend source-over semantics" { + const testing = std.testing; + + // Opaque source overwrites exactly. + { + var dst = [4]u8{ 10, 20, 30, 40 }; + composeRect(&dst, 1, 1, &.{ 100, 110, 120, 255 }, 1, 1, 0, 0, .alpha_blend); + try testing.expectEqualSlices(u8, &.{ 100, 110, 120, 255 }, &dst); + } + + // Fully transparent source leaves the destination untouched + // (like Kitty; see the module doc on rounding). + { + var dst = [4]u8{ 10, 20, 30, 40 }; + composeRect(&dst, 1, 1, &.{ 100, 110, 120, 0 }, 1, 1, 0, 0, .alpha_blend); + try testing.expectEqualSlices(u8, &.{ 10, 20, 30, 40 }, &dst); + } + + // 50% source over opaque destination. + { + var dst = [4]u8{ 0, 0, 0, 255 }; + composeRect(&dst, 1, 1, &.{ 255, 255, 255, 128 }, 1, 1, 0, 0, .alpha_blend); + try testing.expectEqual(@as(u8, 255), dst[3]); + try testing.expectEqual(@as(u8, 128), dst[0]); + } + + // Blending over a transparent destination yields the source + // exactly, matching Kitty: wuffs passes the source through + // untouched when the destination is transparent. + { + var dst = [4]u8{ 0, 0, 0, 0 }; + composeRect(&dst, 1, 1, &.{ 200, 100, 50, 128 }, 1, 1, 0, 0, .alpha_blend); + try testing.expectEqualSlices(u8, &.{ 200, 100, 50, 128 }, &dst); + } +} + +test "compose canvas rect" { + // 3x1 canvas: copy pixel at x=0 onto x=2. + var canvas = [_]u8{ 1, 2, 3, 4, 0, 0, 0, 0, 9, 9, 9, 9 }; + composeCanvasRect(&canvas, &canvas, 3, 1, 1, 0, 0, 2, 0, .overwrite); + try std.testing.expectEqualSlices( + u8, + &.{ 1, 2, 3, 4, 0, 0, 0, 0, 1, 2, 3, 4 }, + &canvas, + ); +} diff --git a/vendor/libghostty-vt/src/terminal/kitty/graphics_storage.zig b/vendor/libghostty-vt/src/terminal/kitty/graphics_storage.zig index ba82ea40..897c8944 100644 --- a/vendor/libghostty-vt/src/terminal/kitty/graphics_storage.zig +++ b/vendor/libghostty-vt/src/terminal/kitty/graphics_storage.zig @@ -6,6 +6,8 @@ const ArenaAllocator = std.heap.ArenaAllocator; const terminal = @import("../main.zig"); const point = @import("../point.zig"); const size = @import("../size.zig"); +const animation = @import("graphics_animation.zig"); +const pixel = @import("graphics_pixel.zig"); const command = @import("graphics_command.zig"); const PageList = @import("../PageList.zig"); const Screen = @import("../Screen.zig"); @@ -32,8 +34,8 @@ var generation_counter: GenerationCounter = .{}; /// Returns the next generation stamp. Stamps are unique and strictly /// monotonically increasing process-wide, starting at 1 (0 is reserved /// to mean "never stamped"). -pub fn nextGeneration() u64 { - return generation_counter.next(); +pub fn nextGeneration(io: std.Io) u64 { + return generation_counter.next(io); } /// Backing implementation for the generation counter. We use a @@ -48,16 +50,17 @@ pub fn nextGeneration() u64 { const GenerationCounter = if (@bitSizeOf(usize) >= 64) struct { value: std.atomic.Value(u64) = .init(0), - fn next(self: *@This()) u64 { + fn next(self: *@This(), io: std.Io) u64 { + _ = io; return self.value.fetchAdd(1, .monotonic) + 1; } } else struct { - mutex: std.Thread.Mutex = .{}, + mutex: std.Io.Mutex = .init, value: u64 = 0, - fn next(self: *@This()) u64 { - self.mutex.lock(); - defer self.mutex.unlock(); + fn next(self: *@This(), io: std.Io) u64 { + self.mutex.lockUncancelable(io); + defer self.mutex.unlock(io); self.value += 1; return self.value; } @@ -100,11 +103,10 @@ pub const ImageStorage = struct { /// This field must only be written via markMutated. generation: u64 = 0, - /// This is the next automatically assigned image ID. We start mid-way - /// through the u32 range to avoid collisions with buggy programs. - /// TODO: This isn't good enough, it's perfectly legal for programs - /// to use IDs in the latter half of the range and collisions - /// are not gracefully handled. + /// This is the next automatically assigned image ID for images + /// transmitted without an ID or number. We start mid-way through + /// the u32 range to stay clear of the low IDs client programs + /// typically pick. See nextImageId. next_image_id: u32 = 2147483647, /// This is the next automatically assigned placement ID. This is never @@ -131,6 +133,38 @@ pub const ImageStorage = struct { total_bytes: usize = 0, total_limit: usize = 320 * 1000 * 1000, // 320MB + /// Identifies one exact pending image transmission. The generation is + /// assigned by this storage when the pending image is inserted, so a + /// later replacement of the same ID cannot consume stale payload bytes. + pub const PendingImage = struct { + id: u32, + generation: u64, + + /// Attach owned decoded bytes if this exact pending transmission is + /// still resident. On true, storage owns `data`; on false, the caller + /// retains ownership. Completion preserves the image generation (and + /// therefore eviction age) while marking storage content as mutated. + pub fn complete( + self: PendingImage, + storage: *ImageStorage, + io: std.Io, + data: []const u8, + ) bool { + const img = storage.images.getPtr(self.id) orelse return false; + if (img.generation != self.generation) return false; + + const expected_len = switch (img.data) { + .complete => return false, + .pending => |len| len, + }; + if (data.len != expected_len) return false; + + img.data = .{ .complete = data }; + storage.markMutated(io); + return true; + } + }; + pub fn deinit( self: *ImageStorage, alloc: Allocator, @@ -138,12 +172,12 @@ pub const ImageStorage = struct { ) void { if (self.loading) |loading| loading.destroy(alloc); + self.clearPlacements(s); + self.placements.deinit(alloc); + var it = self.images.iterator(); while (it.next()) |kv| kv.value_ptr.deinit(alloc); self.images.deinit(alloc); - - self.clearPlacements(s); - self.placements.deinit(alloc); } /// Kitty image protocol is enabled if we have a non-zero limit. @@ -153,15 +187,16 @@ pub const ImageStorage = struct { /// Record a content mutation: marks the storage dirty and assigns a /// fresh generation stamp. Must be called by anything that changes - /// the set of images or placements (or image contents). + /// the set of images or placements (or image contents), including + /// the animation command handlers in graphics_exec.zig. /// /// Do NOT call this for geometry-only events (scrolling, resizing, /// screen switches); those must set only the dirty flag directly. /// Bumping the generation for geometry changes would break the /// contract that an unchanged generation means unchanged contents. - fn markMutated(self: *ImageStorage) void { + pub fn markMutated(self: *ImageStorage, io: std.Io) void { self.dirty = true; - self.generation = nextGeneration(); + self.generation = nextGeneration(io); } /// Sets the limit in bytes for the total amount of image data that @@ -170,23 +205,24 @@ pub const ImageStorage = struct { /// be disabled. pub fn setLimit( self: *ImageStorage, + io: std.Io, alloc: Allocator, s: *terminal.Screen, limit: usize, - ) !void { + ) void { // Special case disabling by quickly deleting all if (limit == 0) { const image_limits = self.image_limits; self.deinit(alloc, s); self.* = .{ .image_limits = image_limits }; - self.markMutated(); + self.markMutated(io); } // If we re lowering our limit, check if we need to evict. if (limit < self.total_bytes) { const req_bytes = self.total_bytes - limit; log.info("evicting images to lower limit, evicting={}", .{req_bytes}); - if (!try self.evictImage(alloc, req_bytes)) { + if (!self.evictImage(io, alloc, s, req_bytes)) { log.warn("failed to evict enough images for required bytes", .{}); } } @@ -194,54 +230,137 @@ pub const ImageStorage = struct { self.total_limit = limit; } - /// Add an already-loaded image to the storage. This will automatically - /// free any existing image with the same ID. - pub fn addImage(self: *ImageStorage, alloc: Allocator, img: Image) Allocator.Error!void { + /// Returns the next ID to automatically assign to an image that + /// was transmitted without an explicit ID (i=). The result is + /// never zero (zero means "no ID") and never an ID currently in + /// use, so an automatic ID never replaces an existing image. + pub fn nextImageId( + self: *ImageStorage, + mode: enum { explicit, implicit }, + ) u32 { + // Starting ID depends on mode. + var id: u32 = switch (mode) { + // Yeah, starting from 1 is wasteful. This matches what Kitty + // does. In the future we can probably cache a low-water-mark + // based on delete behavior. + .explicit => 1, + .implicit => self.next_image_id, + }; + + // Go through all possible images. We have +2 because we can + // exceed the total by 1 (new ID). + const count: usize = self.images.count(); + for (0..count + 2) |_| { + if (id != 0 and !self.images.contains(id)) break; + id +%= 1; + } + + // Zero is never allowed. + if (id == 0) id = 1; + + // Keep track of our next image id for implicit to speed that up. + if (mode == .implicit) self.next_image_id = id +% 1; + + return id; + } + + /// Add an image to the storage. This will automatically free any existing + /// image with the same ID. Prefer addPendingImage for pending data so the + /// caller receives a completion token. + pub fn addImage( + self: *ImageStorage, + io: std.Io, + alloc: Allocator, + s: *terminal.Screen, + img: Image, + ) Allocator.Error!void { + const new_len = img.data.len(); + // If the image itself is over the limit, then error immediately - if (img.data.len > self.total_limit) return error.OutOfMemory; + if (new_len > self.total_limit) return error.OutOfMemory; + + // Credit an existing image's reservation before calculating the + // replacement size. In particular, a completed live transmission + // replacing pending snapshot metadata must be able to reuse the + // reservation without evicting its own ID. + const old_len = if (self.images.get(img.id)) |old| + old.storageSize() + else + 0; + assert(old_len <= self.total_bytes); + + // Reserve map capacity before evicting anything so allocation failure + // cannot leave storage partially evicted. Existing keys need no room. + if (!self.images.contains(img.id)) { + try self.images.ensureUnusedCapacity(alloc, 1); + } // If this would put us over the limit, then evict. - const total_bytes = self.total_bytes + img.data.len; + const total_bytes = self.total_bytes - old_len + new_len; if (total_bytes > self.total_limit) { const req_bytes = total_bytes - self.total_limit; log.info("evicting images to make space for {} bytes", .{req_bytes}); - if (!try self.evictImage(alloc, req_bytes)) { + if (!self.evictImageExcept(io, alloc, s, req_bytes, img.id)) { log.warn("failed to evict enough images for required bytes", .{}); return error.OutOfMemory; } } - // Do the gop op first so if it fails we don't get a partial state - const gop = try self.images.getOrPut(alloc, img.id); + const gop = self.images.getOrPutAssumeCapacity(img.id); - log.debug("addImage image={}", .{img: { - var copy = img; - copy.data = ""; - break :img copy; - }}); + log.debug("addImage image={}", .{img.withoutData()}); - // Write our new image if (gop.found_existing) { - self.total_bytes -= gop.value_ptr.data.len; + // Retransmitting a specific image ID replaces the old image and all + // of its placements, as required by the Kitty graphics protocol. + self.removePlacementsByImageId(s, img.id); + + // Relative placements parented to the removed placements go too. + _ = self.removeOrphans(s, null); + + // Replacing an image drops its animation frames with it, + // implementing the protocol rule that retransmitting the + // base image resets the animation. + self.total_bytes -= gop.value_ptr.storageSize(); gop.value_ptr.deinit(alloc); } gop.value_ptr.* = img; - self.total_bytes += img.data.len; + gop.value_ptr.metadata.placement_count = 0; + self.total_bytes += new_len; // Stamp the stored image with a fresh generation. This gives // every add/replace a unique stamp even when the same image ID // is retransmitted with identical dimensions, so consumers // (e.g. renderer texture caches) can detect content changes. - self.markMutated(); + self.markMutated(io); gop.value_ptr.generation = self.generation; } + /// Add an image whose decoded payload bytes have not arrived yet. + /// Returns the token required to complete this exact transmission. + pub fn addPendingImage( + self: *ImageStorage, + io: std.Io, + alloc: Allocator, + s: *terminal.Screen, + img: Image, + ) Allocator.Error!PendingImage { + assert(img.data == .pending); + try self.addImage(io, alloc, s, img); + const stored = self.images.get(img.id).?; + return .{ .id = stored.id, .generation = stored.generation }; + } + /// Add a placement for a given image. The caller must verify in advance - /// the image exists to prevent memory corruption. + /// the image exists to prevent memory corruption. On success, storage + /// owns `p`; on error, the caller retains ownership. The screen must own + /// any tracked pin in `p` and in the placement it replaces. pub fn addPlacement( self: *ImageStorage, + io: std.Io, alloc: Allocator, + s: *terminal.Screen, image_id: u32, placement_id: u32, p: Placement, @@ -253,6 +372,12 @@ pub const ImageStorage = struct { p, }); + // When we add a placement, take this opportunity to reap garbage. + // Garbage are placements that disappeared off scrollback (were + // pruned). We don't clear Kitty state in the hot path so we do + // this opportunistically here. + self.reapGarbagePlacements(io, s); + // The important piece here is that the placement ID needs to // be marked internal if it is zero. This allows multiple placements // to be added for the same image. If it is non-zero, then it is @@ -273,9 +398,227 @@ pub const ImageStorage = struct { }; const gop = try self.placements.getOrPut(alloc, key); + if (gop.found_existing) { + gop.value_ptr.deinit(s); + } else { + const img = self.images.getPtr(image_id).?; + img.metadata.placement_count = std.math.add( + @TypeOf(img.metadata.placement_count), + img.metadata.placement_count, + 1, + ) catch { + self.placements.removeByPtr(gop.key_ptr); + return error.OutOfMemory; + }; + } gop.value_ptr.* = p; - self.markMutated(); + // This always mutates + self.markMutated(io); + } + + /// How the row operation behind a scrollMarginsBegin/end pair moves + /// tracked pins, which determines how much repositioning work the + /// placements need. + pub const ScrollOp = enum { + /// The operation shifts the entire active window down (it + /// creates scrollback), so every placement anchored in the + /// active area changes active position and must be restored. + window_shift, + + /// The operation moves rows in place within the scrolling + /// region: pins anchored outside the region rows keep their + /// active position without help, so only placements anchored + /// inside the region rows need handling. This is the common + /// case (e.g. a status bar image beside a scrolling pane + /// costs almost nothing per scroll). + in_place, + }; + + /// Adjust placements for a scroll of the terminal's scrolling region + /// by `delta` rows (negative moves content up). This implements the + /// Kitty graphics protocol behavior for scrolls bounded by margins: + /// placements entirely inside the scrolling region move with the content + /// and are clipped when the move would extend them past a margin (deleted + /// when fully clipped), while every other placement (straddling a margin, + /// outside the region, or in the scrollback) must not move at all. + /// + /// This is split into two phases around the row operation because + /// the various scroll implementations move tracked pins in + /// inconsistent ways (some shift the active area so every pin moves, + /// some rotate rows in place and adjust only pins inside the + /// region). We record the desired final position of every placement + /// up front in the returned state, whose end() repositions the pins + /// once the rows have settled, making the result independent of the + /// path taken. Every call must be paired with exactly one end(). + /// + /// Callers must skip this entirely when the scrolling region is the + /// full screen: a full-screen scroll moves placements with their + /// anchored rows (possibly into the scrollback) via pin tracking, + /// which already matches kitty's marginless scroll behavior. + pub fn scrollMarginsBegin( + self: *ImageStorage, + io: std.Io, + t: *terminal.Terminal, + delta: isize, + op: ScrollOp, + ) ScrollMargins { + const s: *terminal.Screen = t.screens.active; + assert(self == &s.kitty_images); + var result: ScrollMargins = .{ .screen = s }; + + const top: i64 = t.scrolling_region.top; + const bottom: i64 = t.scrolling_region.bottom; + + var mutated = false; + var it = self.placements.iterator(); + while (it.next()) |entry| { + const p: *Placement = entry.value_ptr; + + // Virtual placements follow their placeholder cells and + // relative placements follow their parents, so neither is + // ever adjusted by scrolls, matching kitty. + const pin: *PageList.Pin = switch (p.location) { + .pin => |pin| pin, + .virtual, .relative => continue, + }; + + // Pruned placements are reaped by reapGarbagePlacements. + if (pin.garbage) continue; + + // Placements anchored outside the active area (scrollback) + // are never touched by a scroll within the margins. + const coord = (s.pages.pointFromPin( + .active, + pin.*, + ) orelse continue).coord(); + const y: i64 = coord.y; + + // An in-place operation doesn't touch pins anchored outside + // the region rows, and a placement anchored there can never + // be entirely inside the region, so it needs no work at all. + if (op == .in_place and (y < top or y > bottom)) continue; + + // The final position defaults to unmoved; restoring the pin + // to this exact spot afterwards is what stops the row + // operations from dragging the placement around. + var final_y: i64 = y; + + inside: { + const image = self.images.get(entry.key_ptr.image_id) orelse break :inside; + const grid = p.gridSize(image, t); + + // Unknown geometry (e.g. pixel sizes not yet reported) + // means we can't know the extent, so we can't consider + // the placement contained. + if (grid.rows == 0 or grid.cols == 0) break :inside; + + // Only placements entirely within the scrolling region + // move. Kitty only has top/bottom margins so it only + // checks rows; the column check is our generalization + // for left/right margins and is trivially true for + // full-width regions. + if (y < top or y + grid.rows - 1 > bottom) break :inside; + const x: i64 = coord.x; + if (x < t.scrolling_region.left or + @min(x + grid.cols - 1, @as(i64, t.cols) - 1) > t.scrolling_region.right) break :inside; + + const new_y: i64 = y + delta; + const rows: i64 = grid.rows; + const top_clip: i64 = @max(0, top - new_y); + const bottom_clip: i64 = @max(0, new_y + rows - 1 - bottom); + const visible: bool = clip: { + if (top_clip >= rows or bottom_clip >= rows) break :clip false; + + if (top_clip > 0) { + if (!p.clipTop( + image, + t, + @intCast(top_clip), + grid.rows, + )) break :clip false; + mutated = true; + final_y = top; + break :clip true; + } + + if (bottom_clip > 0) { + if (!p.clipBottom( + image, + t, + @intCast(bottom_clip), + grid.rows, + )) break :clip false; + mutated = true; + } + + final_y = new_y; + break :clip true; + }; + + // Scrolled or clipped entirely out of the region: the + // placement is deleted, like kitty. The image itself is + // retained for future placements. + if (!visible) { + self.removePlacement(s, entry); + mutated = true; + continue; + } + } + + result.restores.append(s.alloc, .{ + .pin = pin, + .x = pin.x, + .y = @intCast(final_y), + }) catch { + // OOM: this placement is left to the whims of the + // generic pin tracking. Degraded, but safe. + log.warn("OOM adjusting image placement for scroll", .{}); + continue; + }; + } + + if (mutated) { + // Placements deleted by clipping may orphan relative placements. + // Orphans have no pins so this never touches the restores. + _ = self.removeOrphans(s, null); + self.markMutated(io); + } + + return result; + } + + /// Reap pin-backed placements whose tracked content has been pruned + /// from history, along with any relative placements orphaned by + /// that, marking the content mutation. + /// + /// Virtual and relative placements have no tracked screen location and + /// are never pruned themselves. Kitty removes placements once they + /// scroll out of retained history. + fn reapGarbagePlacements( + self: *ImageStorage, + io: std.Io, + s: *terminal.Screen, + ) void { + var removed = false; + var it = self.placements.iterator(); + while (it.next()) |entry| { + const pin = switch (entry.value_ptr.location) { + .pin => |pin| pin, + .virtual, .relative => continue, + }; + if (!pin.garbage) continue; + + self.removePlacement(s, entry); + removed = true; + } + if (!removed) return; + + // If we removed a placement, then also remove any orphan + // children if this was a parent. + _ = self.removeOrphans(s, null); + + self.markMutated(io); } fn clearPlacements(self: *ImageStorage, s: *terminal.Screen) void { @@ -284,6 +627,290 @@ pub const ImageStorage = struct { self.placements.clearRetainingCapacity(); } + fn removePlacementsByImageId( + self: *ImageStorage, + s: *terminal.Screen, + image_id: u32, + ) void { + var it = self.placements.iterator(); + while (it.next()) |entry| { + if (entry.key_ptr.image_id != image_id) continue; + self.removePlacement(s, entry); + } + } + + /// Remove a placement by its map entry, releasing any tracked pin + /// and keeping the image's placement count in sync. The entry must + /// point into the placements map (via iteration or getEntry). + fn removePlacement( + self: *ImageStorage, + s: *terminal.Screen, + entry: PlacementMap.Entry, + ) void { + entry.value_ptr.deinit(s); + const img = self.images.getPtr(entry.key_ptr.image_id).?; + assert(img.metadata.placement_count > 0); + img.metadata.placement_count -= 1; + self.placements.removeByPtr(entry.key_ptr); + } + + /// Remove relative placements whose parent placement no longer + /// exists, keeping a relative placement's lifetime tied to its + /// parent chain. Chains can be several links deep, so this loops + /// until a pass removes nothing to take out entire orphaned + /// subtrees. Returns true if anything was removed. + /// + /// If `delete_unused` is non-null, an image left without placements + /// by the reap is freed using it. This matches uppercase delete + /// semantics; every other removal path retains image data and + /// passes null. + /// + /// This must be called, outside of any placements iteration, after + /// every operation that removes placements. Kitty gets the same + /// effect lazily by dropping unresolvable placements while drawing; + /// we do it eagerly because our renderer never mutates terminal + /// state. + fn removeOrphans( + self: *ImageStorage, + s: *terminal.Screen, + delete_unused: ?Allocator, + ) bool { + var removed_any = false; + var removed = true; + while (removed) { + removed = false; + var it = self.placements.iterator(); + while (it.next()) |entry| { + const rel = switch (entry.value_ptr.location) { + .relative => |rel| rel, + .pin, .virtual => continue, + }; + if (self.placements.contains(rel.parent)) continue; + + // Parent is gone, remove this placement. + const image_id = entry.key_ptr.image_id; + self.removePlacement(s, entry); + removed = true; + removed_any = true; + + if (delete_unused) |alloc| self.deleteIfUnused( + alloc, + image_id, + ); + } + } + + return removed_any; + } + + /// Maximum number of parent links in a relative placement chain, + /// matching the minimum specified in the spec and the actual + /// limit defined by Kitty at the time of authoring. + pub const parent_chain_limit = 8; + + pub const ParentError = error{ + /// The parent image (P=) does not exist. + ParentImageNotFound, + /// The parent image exists but the requested placement (Q=, or + /// any placement when Q is omitted) does not. + ParentPlacementNotFound, + /// The placement refers to itself as its own parent. + SelfParent, + /// The parent chain loops back to the placement being created. + Cycle, + /// The parent chain exceeds parent_chain_limit links. + TooDeep, + /// An ancestor in the chain no longer exists. This should not + /// happen since removeOrphans keeps chains intact, but we + /// check anyway rather than trusting the invariant. + AncestorNotFound, + }; + + /// Resolve and validate the parent reference (P=/Q=) of a relative + /// placement, returning the concrete key of the parent placement. + /// `child` is the key of the placement being created when it is + /// addressable (an explicit placement ID); null for placements that + /// will receive a fresh internal ID, which nothing can refer to yet. + /// + /// Checks: the parent must exist, the placement must not + /// parent itself, and the resulting ancestor chain must be acyclic + /// and within parent_chain_limit. + pub fn resolveParent( + self: *ImageStorage, + io: std.Io, + s: *terminal.Screen, + child: ?PlacementKey, + parent_image_id: u32, + parent_placement_id: u32, + ) ParentError!PlacementKey { + // Reap placements whose content has been pruned from history + // before resolving: a pruned parent must be reported as missing, + // not accepted and then immediately reaped by addPlacement's own + // sweep, which would store an orphan. + self.reapGarbagePlacements(io, s); + + // If the parent doesn't exist we're already failed. + if (!self.images.contains(parent_image_id)) { + return error.ParentImageNotFound; + } + + // Find the parent + const parent: PlacementKey = parent: { + // An explicit parent placement ID (Q=) selects exactly that + // placement. + if (parent_placement_id > 0) { + const key: PlacementKey = .{ + .image_id = parent_image_id, + .placement_id = .{ + .tag = .external, + .id = parent_placement_id, + }, + }; + if (!self.placements.contains(key)) { + return error.ParentPlacementNotFound; + } + break :parent key; + } + + // No Q: pick a placement of the parent image. Kitty picks + // the oldest surviving placement; our placement map doesn't + // track creation order so we pick by PlacementId.preferredOver + // instead. This is unspecified so any behavior here is fine. + // In practice the parent image has a single placement, and + // clients use Q when it doesn't. + const best: PlacementId = best: { + var best: ?PlacementId = null; + var it = self.placements.keyIterator(); + while (it.next()) |key| { + if (key.image_id != parent_image_id) continue; + const id = key.placement_id; + const b = best orelse { + best = id; + continue; + }; + if (id.preferredOver(b)) best = id; + } + break :best best orelse return error.ParentPlacementNotFound; + }; + + break :parent .{ + .image_id = parent_image_id, + .placement_id = best, + }; + }; + + // A placement cannot be its own parent. This must be checked + // before the chain walk so it reports EINVAL, not ECYCLE. + if (child) |c| if (parent.eql(c)) return error.SelfParent; + + // Walk the would-be ancestor chain. `depth` counts parent links, + // starting at one for the link we are about to create. + var depth: usize = 1; + var key = parent; + while (true) { + if (child) |c| if (key.eql(c)) return error.Cycle; + const p = self.placements.get(key) orelse return error.AncestorNotFound; + const rel = switch (p.location) { + .relative => |rel| rel, + // A pin or virtual placement is the chain root. + .pin, .virtual => break, + }; + if (depth >= parent_chain_limit) return error.TooDeep; + depth += 1; + key = rel.parent; + } + + return parent; + } + + /// The result of resolving a relative placement's parent chain: + /// the chain's root placement (always pin or virtual, never + /// relative) and the total cell offset from the root's origin. + pub const ResolvedChain = struct { + root_key: PlacementKey, + root: Placement, + horizontal_offset: i32, + vertical_offset: i32, + }; + + /// Resolve a relative placement's parent chain to its root, + /// accumulating the cell offsets (H=/V=) of every link on the way. + /// The placement's position is the root's origin plus the accumulated + /// offsets. + /// + /// Returns null when the chain is broken or too deep. Neither can + /// normally happen for stored placements (resolveParent validates + /// chains and removeOrphans removes broken ones), with one kitty + /// quirk: replacing an ancestor placement can deepen the chains + /// below it beyond parent_chain_limit after the fact. + pub fn resolveChain( + self: *const ImageStorage, + rel: Placement.Relative, + ) ?ResolvedChain { + var horizontal = rel.horizontal_offset; + var vertical = rel.vertical_offset; + var key = rel.parent; + var depth: usize = 1; + while (true) { + const p = self.placements.get(key) orelse return null; + switch (p.location) { + .relative => |parent_rel| { + if (depth >= parent_chain_limit) return null; + depth += 1; + horizontal +|= parent_rel.horizontal_offset; + vertical +|= parent_rel.vertical_offset; + key = parent_rel.parent; + }, + + .pin, .virtual => return .{ + .root_key = key, + .root = p, + .horizontal_offset = horizontal, + .vertical_offset = vertical, + }, + } + } + } + + /// Returns the placement that a unicode placeholder cell referencing + /// this image/placement ID pair targets, or null if there is none. A + /// zero placement ID targets one of the image's virtual placements, + /// chosen by PlacementId.preferredOver so the choice is stable (an + /// image rarely has more than one). This is the shared lookup used + /// both for sizing placeholder runs and for positioning relative + /// placements whose chain roots at a virtual placement. + pub fn placeholderTarget( + self: *const ImageStorage, + image_id: u32, + placement_id: u32, + ) ?struct { key: PlacementKey, placement: Placement } { + if (placement_id > 0) { + const key: PlacementKey = .{ + .image_id = image_id, + .placement_id = .{ .tag = .external, .id = placement_id }, + }; + const p = self.placements.get(key) orelse return null; + return .{ .key = key, .placement = p }; + } + + var best: ?PlacementKey = null; + var it = self.placements.iterator(); + while (it.next()) |entry| { + if (entry.key_ptr.image_id != image_id) continue; + if (entry.value_ptr.location != .virtual) continue; + const b = best orelse { + best = entry.key_ptr.*; + continue; + }; + if (entry.key_ptr.placement_id.preferredOver(b.placement_id)) { + best = entry.key_ptr.*; + } + } + + const key = best orelse return null; + return .{ .key = key, .placement = self.placements.get(key).? }; + } + /// Get an image by its ID. If the image doesn't exist, null is returned. pub fn imageById(self: *const ImageStorage, image_id: u32) ?Image { return self.images.get(image_id); @@ -307,46 +934,285 @@ pub const ImageStorage = struct { return newest; } + /// Get a mutable pointer to a stored image, by ID or (newest by) + /// number, following the protocol's id/number addressing. Used by + /// the animation commands, which mutate images in place. The + /// pointer is invalidated by any operation that adds or removes + /// images. + pub fn imagePtrByIdOrNumber( + self: *const ImageStorage, + image_id: u32, + image_number: u32, + ) ?*Image { + if (image_id != 0) return self.images.getPtr(image_id); + + var newest: ?*Image = null; + var it = self.images.iterator(); + while (it.next()) |kv| { + if (kv.value_ptr.number != image_number) continue; + if (newest == null or + kv.value_ptr.generation > newest.?.generation) + { + newest = kv.value_ptr; + } + } + + return newest; + } + + /// Record that the displayed content of an image changed without + /// the image being re-added: marks the storage mutated and stamps + /// the image with the fresh generation so consumers (e.g. the + /// renderer's texture cache) replace what they hold. Used when an + /// animation changes which frame is current or edits the current + /// frame's pixels. + pub fn markImageContentChanged( + self: *ImageStorage, + io: std.Io, + img: *Image, + ) void { + self.markMutated(io); + img.generation = self.generation; + } + + /// Convert a stored image's base data to RGBA in place, adjusting + /// byte accounting. All animation composition happens in RGBA; + /// this is called before the first composition into an image. + /// + /// The pixels are unchanged visually but the stored representation + /// changed, so the image is stamped with a fresh generation. + pub fn convertImageToRgba( + self: *ImageStorage, + io: std.Io, + alloc: Allocator, + img: *Image, + ) Allocator.Error!void { + if (img.format == .rgba) return; + const old = img.data.bytes() orelse return; + + const rgba = try pixel.rgbaFromFormat(alloc, img.format, old); + self.total_bytes -= old.len; + self.total_bytes += rgba.len; + img.data.deinit(alloc); + img.data = .{ .complete = rgba }; + img.format = .rgba; + self.markImageContentChanged(io, img); + } + + /// Reserve `bytes` of storage for animation frame data belonging + /// to `image_id`, evicting other images if needed, mirroring how + /// image transmission reserves space. + /// + /// Errors if the space cannot be made available. On success the caller + /// owns the reservation and must either attach the frame data to the + /// image or call releaseAnimationBytes. + pub fn reserveAnimationBytes( + self: *ImageStorage, + io: std.Io, + alloc: Allocator, + s: *terminal.Screen, + image_id: u32, + bytes: usize, + ) Allocator.Error!void { + if (bytes > self.total_limit) return error.OutOfMemory; + + const total_bytes = self.total_bytes + bytes; + if (total_bytes > self.total_limit) { + const req_bytes = total_bytes - self.total_limit; + // Excess this large cannot be recovered by evicting other + // images (evictImageExcept also requires it). + if (req_bytes > self.total_limit) return error.OutOfMemory; + log.info("evicting images for animation frame, evicting={}", .{req_bytes}); + if (!self.evictImageExcept( + io, + alloc, + s, + req_bytes, + image_id, + )) { + log.warn("failed to evict enough images for animation frame", .{}); + return error.OutOfMemory; + } + } + + self.total_bytes += bytes; + } + + /// Release a reservation made by reserveAnimationBytes, or credit + /// bytes freed by deleting animation frame data. + pub fn releaseAnimationBytes(self: *ImageStorage, bytes: usize) void { + assert(bytes <= self.total_bytes); + self.total_bytes -= bytes; + } + + /// Advance every running animation to the frame that should be + /// displayed at `now_ms` and report when the next frame change is + /// due, as a delay in milliseconds relative to `now_ms`. Null + /// means no running animation needs a future tick. + /// + /// `now_ms` is a monotonic timestamp on a clock of the caller's + /// choosing. The same clock must be used for every call. The + /// caller is expected to be the renderer, ticking once per frame + /// build and scheduling a wakeup for the returned delay. + pub fn animationTick(self: *ImageStorage, io: std.Io, now_ms: u64) ?u64 { + var min_delay: ?u64 = null; + + var it = self.images.iterator(); + while (it.next()) |entry| { + const img: *Image = entry.value_ptr; + + // The gates below mirror Kitty's image_is_animatable. + + // No animation state was ever attached (plain image). + const anim = img.animation orelse continue; + + // Stopped is the initial state of every animation: frames + // then only change client-driven (a=a c=N), never by time. + if (anim.state == .stopped) continue; + + // Only the root frame exists; there is nothing to advance + // to yet even in the running state. + if (anim.frames.items.len == 0) continue; + + // Unplaced images don't animate. This is our simpler + // approximation of Kitty's "is actually drawn" visibility + // gate; it is what stops an image that was transmitted but + // never placed from waking the renderer forever. + if (img.metadata.placement_count == 0) continue; + + // The base pixel data hasn't arrived yet (e.g. an image + // restored from a snapshot); nothing can be displayed. + if (img.data.isPending()) continue; + + // A zero total duration means every frame is gapless and + // no frame can ever be displayed, so the animation can + // never advance. This also guards the gapless-skip loop + // below from never terminating. + if (anim.durationMs() == 0) continue; + + // A finite loop budget (a=a v=N) that ran out on an + // earlier tick froze playback on the last frame for good. + if (anim.max_loops > 0 and anim.current_loop >= anim.max_loops) continue; + + const shown_at: u64 = shown_at: { + // First tick since playback started (or since the + // current frame changed through another path, e.g. + // a=a c=N): the frame is considered shown as of now, + // and its gap starts counting from here. + const at = anim.frame_shown_at_ms orelse break :shown_at now_ms; + + // A timestamp from the future means the caller's clock + // restarted; re-anchor rather than stalling until the + // old timestamp comes around again. + if (at > now_ms) break :shown_at now_ms; + + break :shown_at at; + }; + anim.frame_shown_at_ms = shown_at; + + // The current frame is replaced once its gap has elapsed. + // We advance at most one displayed frame per tick with no + // catch-up, exactly like Kitty: if ticks lag behind the + // gaps, the animation slows down rather than skipping. + var next_at: u64 = shown_at +| anim.gapAt(anim.current_index); + if (now_ms >= next_at) advance: { + // Walk forward to the next displayable frame. This is + // a loop only because gapless (gap=0) frames are never + // displayed and are stepped over; the durationMs gate + // above guarantees a displayable frame exists. + const count: u32 = anim.frameCount(); + var idx = anim.current_index; + while (true) { + const next = (idx + 1) % count; + if (next == 0) { + // Wrapping past the last frame back to the + // root. A loading-state (a=a s=2) animation + // refuses the wrap: it parks on the last frame + // awaiting more frames from the client. + if (anim.state == .loading) break :advance; + + // Each wrap completes a loop; a finite budget + // that just ran out parks on the last frame. + anim.current_loop += 1; + if (anim.max_loops > 0 and + anim.current_loop >= anim.max_loops) break :advance; + } + idx = next; + if (anim.gapAt(idx) != 0) break; + } + + // Show the chosen frame: restart its gap timer and + // stamp a fresh generation so consumers (the renderer + // texture cache, the C API) pick up the new pixels. + anim.current_index = idx; + anim.frame_shown_at_ms = now_ms; + self.markImageContentChanged(io, img); + next_at = now_ms +| anim.gapAt(idx); + } + + // Schedule the next tick. A parked animation left next_at + // in the past and so never schedules one; it is woken by + // its trigger instead (a new frame arriving, or an a=a + // command changing the state). + if (next_at > now_ms) { + const delay = next_at - now_ms; + min_delay = if (min_delay) |m| @min(m, delay) else delay; + } + } + + return min_delay; + } + + /// Clear placements intersecting the active screen, then reclaim every + /// image with no remaining placement. Unlike protocol d=A, a terminal + /// clear also reclaims images that were already unplaced. + pub fn clearScreen( + self: *ImageStorage, + io: std.Io, + alloc: Allocator, + t: *terminal.Terminal, + ) void { + const placements_before = self.placements.count(); + const images_before = self.images.count(); + + // Delete unused placements and images. Orphaned relative + // placements must be reaped before the image sweep so that + // their images are reclaimable too. + self.deleteVisiblePlacements(alloc, t, true); + _ = self.removeOrphans(t.screens.active, null); + var image_it = self.images.iterator(); + while (image_it.next()) |entry| { + self.deleteIfUnused(alloc, entry.key_ptr.*); + } + + // Mark mutated only if things changed. + if (self.placements.count() != placements_before or + self.images.count() != images_before) self.markMutated(io); + } + /// Delete placements, images. pub fn delete( self: *ImageStorage, + io: std.Io, alloc: Allocator, t: *terminal.Terminal, - cmd: command.Delete, + cmd: command.Delete.Action, ) void { // Deletes only ever remove placements/images, so comparing counts // before and after tells us whether anything actually changed. - // Only then do we mark a mutation. This matters because a - // delete-all runs on every screen clear (e.g. `ESC [ 2 J`), and - // we don't want empty clears to dirty the image state or bump - // the generation. + // Only then do we mark a mutation, so a delete that matches nothing + // doesn't dirty the image state or bump the generation. const placements_before = self.placements.count(); const images_before = self.images.count(); defer if (self.placements.count() != placements_before or - self.images.count() != images_before) self.markMutated(); + self.images.count() != images_before) self.markMutated(io); switch (cmd) { - .all => |delete_images| { - var it = self.placements.iterator(); - while (it.next()) |entry| { - // Skip virtual placements - switch (entry.value_ptr.location) { - .pin => {}, - .virtual => continue, - } - - // Deinit the placement and remove it - const image_id = entry.key_ptr.image_id; - entry.value_ptr.deinit(t.screens.active); - self.placements.removeByPtr(entry.key_ptr); - if (delete_images) self.deleteIfUnused(alloc, image_id); - } - - if (delete_images) { - var image_it = self.images.iterator(); - while (image_it.next()) |kv| self.deleteIfUnused(alloc, kv.key_ptr.*); - } - }, + .all => |delete_images| self.deleteVisiblePlacements( + alloc, + t, + delete_images, + ), .id => |v| self.deleteById( alloc, @@ -435,8 +1301,7 @@ pub const ImageStorage = struct { const img = self.imageById(entry.key_ptr.image_id) orelse continue; const rect = entry.value_ptr.rect(img, t) orelse continue; if (rect.top_left.x <= x and rect.bottom_right.x >= x) { - entry.value_ptr.deinit(t.screens.active); - self.placements.removeByPtr(entry.key_ptr); + self.removePlacement(t.screens.active, entry); if (v.delete) self.deleteIfUnused(alloc, img.id); } } @@ -464,8 +1329,7 @@ pub const ImageStorage = struct { var target_pin_copy = target_pin; target_pin_copy.x = rect.top_left.x; if (target_pin_copy.isBetween(rect.top_left, rect.bottom_right)) { - entry.value_ptr.deinit(t.screens.active); - self.placements.removeByPtr(entry.key_ptr); + self.removePlacement(t.screens.active, entry); if (v.delete) self.deleteIfUnused(alloc, img.id); } } @@ -475,7 +1339,9 @@ pub const ImageStorage = struct { var it = self.placements.iterator(); while (it.next()) |entry| { switch (entry.value_ptr.location) { - .pin => {}, + // Relative placements carry their own z value and + // are matched by z deletes just like kitty does. + .pin, .relative => {}, // Virtual placeholders cannot delete by z according // to the spec. @@ -484,87 +1350,226 @@ pub const ImageStorage = struct { if (entry.value_ptr.z == v.z) { const image_id = entry.key_ptr.image_id; - entry.value_ptr.deinit(t.screens.active); - self.placements.removeByPtr(entry.key_ptr); + self.removePlacement(t.screens.active, entry); if (v.delete) self.deleteIfUnused(alloc, image_id); } } }, .range => |v| range: { - if (v.first <= 0 or v.last <= 0) { - log.warn("delete range values must be greater than zero", .{}); - break :range; - } - if (v.first > v.last) { - log.warn("delete range 'x' ({}) must be less than or equal to 'y' ({})", .{ v.first, v.last }); - break :range; + // Both bounds default to zero when omitted. An inverted range + // or a zero upper bound is not an error, it just selects + // nothing: image IDs are always non-zero. + if (v.last == 0 or v.first > v.last) break :range; + + // Remove matching placements in one pass. + var placement_it = self.placements.iterator(); + while (placement_it.next()) |entry| { + if (entry.key_ptr.image_id < v.first or + entry.key_ptr.image_id > v.last) continue; + + self.removePlacement(t.screens.active, entry); } - var it = self.placements.iterator(); - while (it.next()) |entry| { - if (entry.key_ptr.image_id >= v.first or entry.key_ptr.image_id <= v.last) { - const image_id = entry.key_ptr.image_id; - entry.value_ptr.deinit(t.screens.active); - self.placements.removeByPtr(entry.key_ptr); - if (v.delete) self.deleteIfUnused(alloc, image_id); + // Uppercase deletion also frees matching images that are now + // unused, including images that had no placements initially. + if (!v.delete) break :range; + var image_it = self.images.iterator(); + while (image_it.next()) |entry| { + const image_id = entry.key_ptr.*; + if (image_id >= v.first and image_id <= v.last) { + self.deleteIfUnused(alloc, image_id); } } }, - // We don't support animation frames yet so they are successfully - // deleted! - .animation_frames => {}, + .animation_frames => |v| self.deleteAnimationFrame( + io, + alloc, + t.screens.active, + v, + ), } + + // Deleting placements orphans any relative placements parented + // to them (transitively). Their lifetime is tied to the parent + // so they are removed as well, and an uppercase delete also + // frees any image the cascade leaves without placements (the + // per-branch deleteIfUnused calls above ran while the orphans + // still counted as placements). + const delete_unused: bool = switch (cmd) { + .all, .intersect_cursor => |v| v, + inline else => |v| v.delete, + }; + _ = self.removeOrphans( + t.screens.active, + if (delete_unused) alloc else null, + ); } - fn deleteById( + /// Delete only non-virtual placements that intersect the active screen. + /// The protocol defines d=a/A in terms of visible placements, and an + /// uppercase delete only frees data for images selected by those + /// placements. + fn deleteVisiblePlacements( self: *ImageStorage, alloc: Allocator, - s: *terminal.Screen, - image_id: u32, - placement_id: u32, + t: *terminal.Terminal, delete_unused: bool, ) void { - // If no placement, we delete all placements with the ID - if (placement_id == 0) { - var it = self.placements.iterator(); - while (it.next()) |entry| { - if (entry.key_ptr.image_id == image_id) { - entry.value_ptr.deinit(s); - self.placements.removeByPtr(entry.key_ptr); - } - } - } else { - if (self.placements.getEntry(.{ - .image_id = image_id, - .placement_id = .{ .tag = .external, .id = placement_id }, - })) |entry| { - entry.value_ptr.deinit(s); - self.placements.removeByPtr(entry.key_ptr); + var it = self.placements.iterator(); + while (it.next()) |entry| { + const pin = switch (entry.value_ptr.location) { + .pin => |pin| pin, + // Virtual placements are never selected by visible + // deletes per the protocol. Relative placements are + // removed with their parents instead (removeOrphans). + .virtual, .relative => continue, + }; + if (pin.garbage) continue; + + // Placements anchored in the active area are necessarily visible. + // Only compute their extent when the anchor is already in history + // and the bottom edge may still intersect the active area. + if (t.screens.active.pages.pointFromPin( + .active, + pin.*, + ) == null) { + const image = self.imageById(entry.key_ptr.image_id) orelse continue; + const rect = entry.value_ptr.rect(image, t) orelse continue; + if (t.screens.active.pages.pointFromPin(.active, rect.bottom_right) == null) continue; } + + const image_id = entry.key_ptr.image_id; + self.removePlacement(t.screens.active, entry); + if (delete_unused) self.deleteIfUnused(alloc, image_id); + } + } + + fn deleteById( + self: *ImageStorage, + alloc: Allocator, + s: *terminal.Screen, + image_id: u32, + placement_id: u32, + delete_unused: bool, + ) void { + var matched = placement_id == 0; + + // If no placement, we delete all placements with the ID + if (placement_id == 0) self.removePlacementsByImageId( + s, + image_id, + ) else if (self.placements.getEntry(.{ + .image_id = image_id, + .placement_id = .{ + .tag = .external, + .id = placement_id, + }, + })) |entry| { + self.removePlacement(s, entry); + matched = true; } - // If this is specified, then we also delete the image - // if it is no longer in use. - if (delete_unused) self.deleteIfUnused(alloc, image_id); + // A placement ID narrows the selection to that exact placement, so an + // unmatched selector must not free otherwise-unreferenced image data. + // https://sw.kovidgoyal.net/kitty/graphics-protocol/#deleting-images + if (delete_unused and matched) self.deleteIfUnused(alloc, image_id); } - /// Delete an image if it is unused. - fn deleteIfUnused(self: *ImageStorage, alloc: Allocator, image_id: u32) void { - var it = self.placements.iterator(); - while (it.next()) |kv| { - if (kv.key_ptr.image_id == image_id) { - return; - } + /// Delete an animation frame (d=f/F). Deletes never produce + /// responses, so all failures are only logged. Kitty behaviors + /// implemented here: on an image without extra frames a lowercase + /// delete is a no-op while an uppercase delete removes the entire + /// image, placements included; the frame number is clamped to the + /// last frame and zero selects the root frame; deleting the root + /// frame promotes frame 2 to be the new root. + fn deleteAnimationFrame( + self: *ImageStorage, + io: std.Io, + alloc: Allocator, + s: *terminal.Screen, + v: command.Delete.Action.AnimationFrames, + ) void { + if (v.image_id == 0 and v.image_number == 0) { + log.warn("delete animation frames requires image id or number", .{}); + return; } + const img = self.imagePtrByIdOrNumber( + v.image_id, + v.image_number, + ) orelse { + log.warn( + "delete animation frames for unknown image id={} number={}", + .{ v.image_id, v.image_number }, + ); + return; + }; + + const anim: *animation.Animation = anim: { + if (img.animation) |anim| { + if (anim.frames.items.len > 0) break :anim anim; + } - // If we get here, we can delete the image. - if (self.images.getEntry(image_id)) |entry| { - self.total_bytes -= entry.value_ptr.data.len; + // The image is not (or no longer) an animation. The + // uppercase delete removes the entire image, even when it + // still has placements. + if (!v.delete) return; + self.removePlacementsByImageId(s, img.id); + const entry = self.images.getEntry(img.id).?; + self.total_bytes -= entry.value_ptr.storageSize(); entry.value_ptr.deinit(alloc); self.images.removeByPtr(entry.key_ptr); + return; + }; + + // Clamp the frame number: zero selects the root frame and + // values past the end select the last frame. + const count: u32 = anim.frameCount(); + var number: u32 = @min(v.frame, count); + if (number == 0) number = 1; + + if (number == 1) { + // Deleting the root frame promotes frame 2 to root. The + // promoted frame's bytes stay reserved; only the old root + // data is freed. + self.releaseAnimationBytes(img.data.len()); + img.data.deinit(alloc); + const promoted = anim.frames.orderedRemove(0); + img.data = .{ .complete = promoted.data }; + anim.root_gap_ms = promoted.gap_ms; + } else { + const removed = anim.frames.orderedRemove(number - 2); + self.releaseAnimationBytes(removed.data.len); + alloc.free(removed.data); } + + // Fix up the current frame. + const removed_idx: u32 = if (number == 1) 0 else number - 2; + const remaining: u32 = @intCast(anim.frames.items.len); + if (anim.current_index > remaining) { + anim.current_index = remaining; + anim.frame_shown_at_ms = null; + self.markImageContentChanged(io, img); + return; + } + if (removed_idx == anim.current_index) { + anim.frame_shown_at_ms = null; + self.markImageContentChanged(io, img); + } else { + if (removed_idx < anim.current_index) anim.current_index -= 1; + self.markMutated(io); + } + } + + /// Delete an image if it is unused. + fn deleteIfUnused(self: *ImageStorage, alloc: Allocator, image_id: u32) void { + const entry = self.images.getEntry(image_id) orelse return; + if (entry.value_ptr.metadata.placement_count > 0) return; + + self.total_bytes -= entry.value_ptr.storageSize(); + entry.value_ptr.deinit(alloc); + self.images.removeByPtr(entry.key_ptr); } /// Deletes all placements intersecting a screen point. @@ -584,133 +1589,173 @@ pub const ImageStorage = struct { while (it.next()) |entry| { const img = self.imageById(entry.key_ptr.image_id) orelse continue; const rect = entry.value_ptr.rect(img, t) orelse continue; - if (target_pin.isBetween(rect.top_left, rect.bottom_right)) { + if (rect.contains(target_pin)) { if (filter) |f| if (!f(filter_ctx, entry.value_ptr.*)) continue; - entry.value_ptr.deinit(t.screens.active); - self.placements.removeByPtr(entry.key_ptr); + self.removePlacement(t.screens.active, entry); if (delete_unused) self.deleteIfUnused(alloc, img.id); } } } /// Evict image to make space. This will evict the oldest image, - /// prioritizing unused images first, as recommended by the published - /// Kitty spec. + /// prioritizing transient images and unused images as recommended by the + /// published Kitty spec. /// /// This will evict as many images as necessary to make space for /// req bytes. - fn evictImage(self: *ImageStorage, alloc: Allocator, req: usize) !bool { + fn evictImage( + self: *ImageStorage, + io: std.Io, + alloc: Allocator, + s: *terminal.Screen, + req: usize, + ) bool { + return self.evictImageExcept(io, alloc, s, req, null); + } + + /// Evict images while preserving `exclude_id`, used when replacing an + /// existing image whose old reservation has already been credited. + fn evictImageExcept( + self: *ImageStorage, + io: std.Io, + alloc: Allocator, + s: *terminal.Screen, + req: usize, + exclude_id: ?u32, + ) bool { assert(req <= self.total_limit); - // Ironically we allocate to evict. We should probably redesign the - // data structures to avoid this but for now allocating a little - // bit is fine compared to the megabytes we're looking to save. const Candidate = struct { id: u32, generation: u64, - used: bool, - }; - - var candidates: std.ArrayList(Candidate) = .empty; - defer candidates.deinit(alloc); - - var it = self.images.iterator(); - while (it.next()) |kv| { - const img = kv.value_ptr; - - // This is a huge waste. See comment above about redesigning - // our data structures to avoid this. Eviction should be very - // rare though and we never have that many images/placements - // so hopefully this will last a long time. - const used = used: { - var p_it = self.placements.iterator(); - while (p_it.next()) |p_kv| { - if (p_kv.key_ptr.image_id == img.id) { - break :used true; - } - } - break :used false; - }; + // Lower values are evicted first: + // 0: transient, unused + // 1: not transient, unused + // 2: transient, used + // 3: not transient, used + priority: u2, - try candidates.append(alloc, .{ - .id = img.id, - .generation = img.generation, - .used = used, - }); - } + fn init(img: *const Image) @This() { + return .{ + .id = img.id, + .generation = img.generation, + .priority = (if (img.metadata.transient) @as(u2, 0) else @as(u2, 1)) + + (if (img.metadata.placement_count > 0) @as(u2, 2) else @as(u2, 0)), + }; + } - // Sort - std.mem.sortUnstable( - Candidate, - candidates.items, - {}, - struct { - fn lessThan( - ctx: void, - lhs: Candidate, - rhs: Candidate, - ) bool { - _ = ctx; - - // If their usage matches, then it's based on the - // generation stamp, which orders by transmit time. - // (Stamps are unique but tie-break by ID anyway to - // stay deterministic for hand-built test images.) - if (lhs.used == rhs.used) return if (lhs.generation == rhs.generation) - lhs.id < rhs.id - else - lhs.generation < rhs.generation; - - // If not used, then its a better candidate - return !lhs.used; - } - }.lessThan, - ); + fn lessThan(lhs: @This(), rhs: @This()) bool { + if (lhs.priority != rhs.priority) + return lhs.priority < rhs.priority; + if (lhs.generation != rhs.generation) + return lhs.generation < rhs.generation; + return lhs.id < rhs.id; + } + }; // Evicting anything is a content mutation. This matters for the // setLimit path in particular, which doesn't otherwise mark it. - var any_evicted = false; - defer if (any_evicted) self.markMutated(); + // Evicted placements can also orphan relative placements of + // other images, which must be reaped along with them. + const images_before = self.images.count(); + defer if (self.images.count() != images_before) { + _ = self.removeOrphans(s, null); + self.markMutated(io); + }; - // They're in order of best to evict. var evicted: usize = 0; - for (candidates.items) |c| { - // Delete all the placements for this image and the image. + while (evicted < req) { + const c = candidate: { + var best: ?Candidate = null; + var it = self.images.iterator(); + while (it.next()) |kv| { + const img = kv.value_ptr; + if (exclude_id != null and img.id == exclude_id.?) continue; + + const current = Candidate.init(img); + if (best == null or current.lessThan(best.?)) best = current; + } + break :candidate best orelse return false; + }; + var p_it = self.placements.iterator(); while (p_it.next()) |entry| { if (entry.key_ptr.image_id == c.id) { - self.placements.removeByPtr(entry.key_ptr); - any_evicted = true; + self.removePlacement(s, entry); } } - if (self.images.getEntry(c.id)) |entry| { - log.info("evicting image id={} bytes={}", .{ c.id, entry.value_ptr.data.len }); + const entry = self.images.getEntry(c.id).?; + const image_len = entry.value_ptr.storageSize(); + log.info("evicting image id={} bytes={}", .{ c.id, image_len }); - evicted += entry.value_ptr.data.len; - self.total_bytes -= entry.value_ptr.data.len; + evicted += image_len; + self.total_bytes -= image_len; - entry.value_ptr.deinit(alloc); - self.images.removeByPtr(entry.key_ptr); - any_evicted = true; - - if (evicted > req) return true; - } + entry.value_ptr.deinit(alloc); + self.images.removeByPtr(entry.key_ptr); } - return false; + return true; } + /// State returned by scrollMarginsBegin: the final active-area + /// position every surviving placement pin must be restored to once + /// the scrolled rows have settled. Every begin must be paired with + /// exactly one end() call after the scroll's row operations + /// complete. + pub const ScrollMargins = struct { + screen: *terminal.Screen, + restores: std.ArrayListUnmanaged(Restore) = .empty, + + const Restore = struct { + pin: *PageList.Pin, + x: size.CellCountInt, + y: u32, + }; + + /// Reposition every recorded placement pin to its post-scroll + /// position and release the state. Must be called exactly + /// once, after the scroll's row operations complete. + pub fn end(self: *ScrollMargins) void { + for (self.restores.items) |restore| { + restore.pin.* = self.screen.pages.pin(.{ .active = .{ + .x = restore.x, + .y = restore.y, + } }) orelse continue; + } + self.restores.deinit(self.screen.alloc); + } + }; + /// Every placement is uniquely identified by the image ID and the /// placement ID. If an image ID isn't specified it is assumed to be 0. /// Likewise, if a placement ID isn't specified it is assumed to be 0. pub const PlacementKey = struct { image_id: u32, - placement_id: packed struct { - tag: enum(u1) { internal, external }, - id: u32, - }, + placement_id: PlacementId, + + pub fn eql(self: PlacementKey, other: PlacementKey) bool { + return std.meta.eql(self, other); + } + }; + + /// Internal placement IDs are assigned by us for placements created + /// without an explicit ID (p=0); external IDs are client-specified. + /// The two are separate namespaces, hence the tag. + pub const PlacementId = packed struct { + tag: enum(u1) { internal, external }, + id: u32, + + /// Deterministic preference order used when an operation must + /// pick a single placement out of several (kitty uses creation + /// order, which our map doesn't track): external IDs win over + /// internal ones, and lower IDs win within a tag. + pub fn preferredOver(self: PlacementId, other: PlacementId) bool { + if (self.tag != other.tag) return self.tag == .external; + return self.id < other.id; + } }; pub const Placement = struct { @@ -739,7 +1784,27 @@ pub const ImageStorage = struct { pin: *PageList.Pin, /// Virtual placement (U=1) for unicode placeholders. - virtual: void, + virtual, + + /// Placed relative to a parent placement (P=/Q=). The + /// placement has no screen position of its own; it is + /// positioned at render time by resolving the parent chain + /// to its root (see resolveChain) and therefore follows the + /// parent through scrolls automatically. Its lifetime is + /// tied to the parent: removing any ancestor removes it + /// too (see removeOrphans). + relative: Relative, + }; + + pub const Relative = struct { + /// The parent placement. Resolved to a concrete key when + /// the placement is created, so the parent is guaranteed + /// to exist at that point. + parent: PlacementKey, + + /// Cell offsets from the parent's origin (H=/V=). + horizontal_offset: i32 = 0, + vertical_offset: i32 = 0, }; pub fn deinit( @@ -748,10 +1813,80 @@ pub const ImageStorage = struct { ) void { switch (self.location) { .pin => |p| s.pages.untrackPin(p), - .virtual => {}, + .virtual, .relative => {}, } } + /// Multiply two protocol-controlled values without allowing them to + /// wrap. Placement geometry is exposed as u32, so values larger than + /// that are represented by the largest possible value. + fn saturatingMul(lhs: u32, rhs: u32) u32 { + return std.math.mul(u32, lhs, rhs) catch std.math.maxInt(u32); + } + + /// Scale a dimension by an aspect ratio and round to the nearest + /// integer. The u64 intermediate can hold the product of two u32s as + /// well as the rounding adjustment. + fn scaleDimension(value: u32, numerator: u32, denominator: u32) u32 { + if (denominator == 0) return 0; + + const rounded = (@as(u64, value) * @as(u64, numerator) + + @as(u64, denominator) / 2) / @as(u64, denominator); + return std.math.cast(u32, rounded) orelse std.math.maxInt(u32); + } + + pub const SourceRect = struct { + x: u32, + y: u32, + width: u32, + height: u32, + }; + + /// Returns the requested source rectangle intersected with the image. + /// A zero width or height requests the full corresponding image + /// dimension before intersection, as defined by the Kitty protocol. + pub fn sourceRect(self: Placement, image: Image) SourceRect { + const x = @min(self.source_x, image.width); + const y = @min(self.source_y, image.height); + return .{ + .x = x, + .y = y, + .width = @min( + if (self.source_width > 0) self.source_width else image.width, + image.width - x, + ), + .height = @min( + if (self.source_height > 0) self.source_height else image.height, + image.height - y, + ), + }; + } + + /// Returns the placement offset clamped within the first cell. Pixel + /// geometry may temporarily be unavailable, in which case there is no + /// valid offset within the cell. + pub fn cellOffset( + self: Placement, + t: *const terminal.Terminal, + ) struct { + x: u32, + y: u32, + } { + const cell_width: u32 = t.width_px / t.cols; + const cell_height: u32 = t.height_px / t.rows; + + return .{ + .x = if (cell_width > 0) + @min(self.x_offset, cell_width - 1) + else + 0, + .y = if (cell_height > 0) + @min(self.y_offset, cell_height - 1) + else + 0, + }; + } + /// Returns the size of this placement's image in pixels, /// taking into account the source rectangle, specified /// rows/columns, and aspect ratio. @@ -763,9 +1898,9 @@ pub const ImageStorage = struct { width: u32, height: u32, } { - // Height / width of the image in px. - const width = if (self.source_width > 0) self.source_width else image.width; - const height = if (self.source_height > 0) self.source_height else image.height; + const source = self.sourceRect(image); + const width = source.width; + const height = source.height; // If we don't have any specified cols or rows then the placement // should be the native size of the image, and doesn't need to be @@ -782,20 +1917,18 @@ pub const ImageStorage = struct { // count and the height by the row count, because it should be. const cell_width: u32 = t.width_px / t.cols; const cell_height: u32 = t.height_px / t.rows; - - const width_f64: f64 = @floatFromInt(width); - const height_f64: f64 = @floatFromInt(height); + const cell_offset = self.cellOffset(t); // If we have a specified cols AND rows then we calculate // the width and height from them directly, we don't need // to adjust for aspect ratio. if (self.columns > 0 and self.rows > 0) { - const calc_width = cell_width * self.columns; - const calc_height = cell_height * self.rows; + const calc_width = saturatingMul(cell_width, self.columns); + const calc_height = saturatingMul(cell_height, self.rows); return .{ - .width = calc_width, - .height = calc_height, + .width = calc_width -| cell_offset.x, + .height = calc_height -| cell_offset.y, }; } @@ -805,11 +1938,11 @@ pub const ImageStorage = struct { // If only the columns were specified, we determine // the height of the image based on the aspect ratio. if (self.columns > 0) { - const aspect = height_f64 / width_f64; - const calc_width: u32 = cell_width * self.columns; - const calc_height: u32 = @intFromFloat(@round( - @as(f64, @floatFromInt(calc_width)) * aspect, - )); + const calc_width = saturatingMul( + cell_width, + self.columns, + ) -| cell_offset.x; + const calc_height = scaleDimension(calc_width, height, width); return .{ .width = calc_width, @@ -820,11 +1953,11 @@ pub const ImageStorage = struct { // Otherwise, only the rows were specified, so we // determine the width based on the aspect ratio. { - const aspect = width_f64 / height_f64; - const calc_height: u32 = cell_height * self.rows; - const calc_width: u32 = @intFromFloat(@round( - @as(f64, @floatFromInt(calc_height)) * aspect, - )); + const calc_height = saturatingMul( + cell_height, + self.rows, + ) -| cell_offset.y; + const calc_width = scaleDimension(calc_height, width, height); return .{ .width = calc_width, @@ -851,15 +1984,16 @@ pub const ImageStorage = struct { // Otherwise we calculate the pixel size, divide by // cell size, and round up to the nearest integer. const calc_size = self.pixelSize(image, t); + const cell_offset = self.cellOffset(t); return .{ .cols = std.math.divCeil( u32, - calc_size.width + self.x_offset, + calc_size.width +| cell_offset.x, t.width_px / t.cols, ) catch 0, .rows = std.math.divCeil( u32, - calc_size.height + self.y_offset, + calc_size.height +| cell_offset.y, t.height_px / t.rows, ) catch 0, }; @@ -867,9 +2001,82 @@ pub const ImageStorage = struct { // in such a case it seems safe to return 0 for this. } + /// Permanently clip `count` rows off the top of this placement, + /// which currently spans `span` grid rows, by shrinking the + /// source rectangle (the requested source rect is materialized + /// into explicit values in the process). Used when a scroll + /// within margins pushes the placement past the top margin. + /// Requires 0 < count < span. Returns false if no visible + /// source pixels would remain, in which case the placement is + /// unmodified and should be deleted. + fn clipTop( + self: *Placement, + image: Image, + t: *const terminal.Terminal, + count: u32, + span: u32, + ) bool { + assert(count > 0 and count < span); + const src = self.sourceRect(image); + const crop: u32 = if (self.rows > 0) + // Scaled placement: each grid row shows an equal + // fraction of the source. + @intCast(@as(u64, src.height) * count / span) + else + // Native size: each grid row shows one cell height of + // source pixels (kitty does the same). + saturatingMul(t.height_px / t.rows, count); + if (crop >= src.height) return false; + self.source_x = src.x; + self.source_y = src.y + crop; + self.source_width = src.width; + self.source_height = src.height - crop; + if (self.rows > 0) self.rows -= count; + return true; + } + + /// clipTop, but clipping `count` rows off the bottom. + fn clipBottom( + self: *Placement, + image: Image, + t: *const terminal.Terminal, + count: u32, + span: u32, + ) bool { + assert(count > 0 and count < span); + const src = self.sourceRect(image); + // An empty source can never be clipped smaller. This also + // guarantees we never store an explicit zero source height, + // which would be reinterpreted as "full image". + if (src.height == 0) return false; + if (self.rows > 0) { + const crop: u32 = @intCast(@as(u64, src.height) * count / span); + if (crop >= src.height) return false; + self.source_height = src.height - crop; + self.rows -= count; + } else { + // The remaining rows can show this many source pixels, + // with the first cell losing cellOffset pixels to the + // placement's y offset. + const visible = saturatingMul(t.height_px / t.rows, span - count); + const offset = self.cellOffset(t).y; + if (visible <= offset) return false; + self.source_height = @min(src.height, visible - offset); + } + self.source_x = src.x; + self.source_y = src.y; + self.source_width = src.width; + return true; + } + /// Returns a selection of the entire rectangle this placement - /// occupies within the screen. This can return null if the placement - /// doesn't have an associated rect (i.e. a virtual placement). + /// occupies within the screen. This can return null for a virtual + /// placement or when unavailable pixel geometry makes it empty. + /// + /// Relative placements also return null: they have no screen + /// position of their own, so geometric queries (delete by + /// intersection, row, column, etc.) never match them directly. + /// They are instead removed when their parent chain is removed. pub fn rect( self: Placement, image: Image, @@ -878,20 +2085,25 @@ pub const ImageStorage = struct { const grid_size = self.gridSize(image, t); const pin = switch (self.location) { .pin => |p| p, - .virtual => return null, + .virtual, .relative => return null, }; + if (pin.garbage) return null; + + // A zero pixel-sized placement can produce a zero grid size when + // pixel geometry is unavailable. It occupies no rectangle. + if (grid_size.cols == 0 or grid_size.rows == 0) return null; var br = switch (pin.downOverflow(grid_size.rows - 1)) { .offset => |v| v, .overflow => |v| v.end, }; - br.x = @min( + br.x = @intCast(@min( // We need to sub one here because the x value is // one width already. So if the image is width "1" // then we add zero to X because X itself is width 1. - pin.x + (grid_size.cols - 1), - t.cols - 1, - ); + @as(u32, pin.x) +| (grid_size.cols - 1), + @as(u32, t.cols) - 1, + )); return .{ .top_left = pin.*, @@ -914,17 +2126,18 @@ fn trackPin( test "storage: add placement with zero placement id" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .cols = 100, .rows = 100 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 100, .rows = 100 }); defer t.deinit(alloc); t.width_px = 100; t.height_px = 100; var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1, .width = 50, .height = 50 }); - try s.addImage(alloc, .{ .id = 2, .width = 25, .height = 25 }); - try s.addPlacement(alloc, 1, 0, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); - try s.addPlacement(alloc, 1, 0, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); try testing.expectEqual(@as(usize, 2), s.placements.count()); try testing.expectEqual(@as(usize, 2), s.images.count()); @@ -940,25 +2153,124 @@ test "storage: add placement with zero placement id" { }) != null); } -test "storage: delete all placements and images" { +test "storage: replacing placement releases tracked pin" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 3, .rows = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + + const tracked = t.screens.active.pages.countTrackedPins(); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) }, + }); + try testing.expectEqual( + tracked + 1, + t.screens.active.pages.countTrackedPins(), + ); + + const replacement_pin = try trackPin(&t, .{ .x = 2, .y = 2 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = replacement_pin }, + }); + try testing.expectEqual(@as(usize, 1), s.placements.count()); + try testing.expectEqual( + tracked + 1, + t.screens.active.pages.countTrackedPins(), + ); + try testing.expectEqual( + replacement_pin, + s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?.location.pin, + ); +} + +test "storage: adding placement reclaims garbage placements" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 3, .rows = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + + const tracked = t.screens.active.pages.countTrackedPins(); + const old_pin = try trackPin(&t, .{ .x = 0, .y = 0 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ + .location = .{ .pin = old_pin }, + }); + old_pin.garbage = true; + + const new_pin = try trackPin(&t, .{ .x = 1, .y = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ + .location = .{ .pin = new_pin }, + }); + + try testing.expectEqual(@as(usize, 1), s.placements.count()); + try testing.expectEqual( + tracked + 1, + t.screens.active.pages.countTrackedPins(), + ); + try testing.expectEqual( + new_pin, + s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .internal, .id = 1 }, + }).?.location.pin, + ); +} + +test "storage: placement count limit permits replacement" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .virtual = {} } }); + + const img = s.images.getPtr(1).?; + img.metadata.placement_count = std.math.maxInt(@TypeOf(img.metadata.placement_count)); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .virtual = {} } }); + try testing.expectError( + error.OutOfMemory, + s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .virtual = {} } }), + ); + try testing.expectEqual(@as(usize, 1), s.placements.count()); +} + +test "storage: delete all visible placements and matching images" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); s.dirty = false; - s.delete(alloc, &t, .{ .all = true }); + s.delete(io, alloc, &t, .{ .all = true }); try testing.expect(s.dirty); - try testing.expectEqual(@as(usize, 0), s.images.count()); + try testing.expectEqual(@as(usize, 1), s.images.count()); + try testing.expect(s.images.contains(3)); try testing.expectEqual(@as(usize, 0), s.placements.count()); try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins()); } @@ -966,45 +2278,133 @@ test "storage: delete all placements and images" { test "storage: delete all placements and images preserves limit" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); s.total_limit = 5000; - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); s.dirty = false; - s.delete(alloc, &t, .{ .all = true }); + s.delete(io, alloc, &t, .{ .all = true }); try testing.expect(s.dirty); - try testing.expectEqual(@as(usize, 0), s.images.count()); + try testing.expectEqual(@as(usize, 1), s.images.count()); + try testing.expect(s.images.contains(3)); try testing.expectEqual(@as(usize, 0), s.placements.count()); try testing.expectEqual(@as(usize, 5000), s.total_limit); try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins()); } +test "storage: delete all visible placements preserves scrollback" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ + .rows = 2, + .cols = 2, + .max_scrollback_bytes = 4096, + }); + defer t.deinit(alloc); + t.width_px = 2; + t.height_px = 2; + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .y = 0 }) }, + }); + try t.scrollUp(1); + + const history_key: ImageStorage.PlacementKey = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }; + try testing.expect(t.screens.active.pages.pointFromPin( + .active, + s.placements.get(history_key).?.location.pin.*, + ) == null); + + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ + .location = .{ .pin = try trackPin(&t, .{ .y = 0 }) }, + }); + + s.delete(io, alloc, &t, .{ .all = true }); + try testing.expectEqual(@as(usize, 1), s.placements.count()); + try testing.expect(s.placements.contains(history_key)); + try testing.expectEqual(@as(usize, 1), s.images.count()); + try testing.expect(s.images.contains(1)); +} + +test "storage: delete all includes placements spanning into active area" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ + .rows = 2, + .cols = 2, + .max_scrollback_bytes = 4096, + }); + defer t.deinit(alloc); + t.width_px = 2; + t.height_px = 2; + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 1, + .height = 2, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .y = 0 }) }, + }); + try t.scrollUp(1); + + const placement = s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + try testing.expect(t.screens.active.pages.pointFromPin( + .active, + placement.location.pin.*, + ) == null); + const rect = placement.rect(s.imageById(1).?, &t).?; + try testing.expect(t.screens.active.pages.pointFromPin( + .active, + rect.bottom_right, + ) != null); + + s.delete(io, alloc, &t, .{ .all = true }); + try testing.expectEqual(@as(usize, 0), s.placements.count()); + try testing.expectEqual(@as(usize, 0), s.images.count()); +} + test "storage: delete all placements" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); s.dirty = false; - s.delete(alloc, &t, .{ .all = false }); + s.delete(io, alloc, &t, .{ .all = false }); try testing.expect(s.dirty); try testing.expectEqual(@as(usize, 0), s.placements.count()); try testing.expectEqual(@as(usize, 3), s.images.count()); @@ -1014,20 +2414,21 @@ test "storage: delete all placements" { test "storage: delete all placements by image id" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); s.dirty = false; - s.delete(alloc, &t, .{ .id = .{ .image_id = 2 } }); + s.delete(io, alloc, &t, .{ .id = .{ .image_id = 2 } }); try testing.expect(s.dirty); try testing.expectEqual(@as(usize, 1), s.placements.count()); try testing.expectEqual(@as(usize, 3), s.images.count()); @@ -1037,20 +2438,21 @@ test "storage: delete all placements by image id" { test "storage: delete all placements by image id and unused images" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); s.dirty = false; - s.delete(alloc, &t, .{ .id = .{ .delete = true, .image_id = 2 } }); + s.delete(io, alloc, &t, .{ .id = .{ .delete = true, .image_id = 2 } }); try testing.expect(s.dirty); try testing.expectEqual(@as(usize, 1), s.placements.count()); try testing.expectEqual(@as(usize, 2), s.images.count()); @@ -1060,21 +2462,22 @@ test "storage: delete all placements by image id and unused images" { test "storage: delete placement by specific id" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); s.dirty = false; - s.delete(alloc, &t, .{ .id = .{ + s.delete(io, alloc, &t, .{ .id = .{ .delete = true, .image_id = 1, .placement_id = 2, @@ -1085,42 +2488,84 @@ test "storage: delete placement by specific id" { try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins()); } -test "storage: delete intersecting cursor" { +test "storage: uppercase id delete preserves image when placement does not match" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 100, .cols = 100 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); - t.width_px = 100; - t.height_px = 100; - const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1, .width = 50, .height = 50 }); - try s.addImage(alloc, .{ .id = 2, .width = 25, .height = 25 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); - try s.addPlacement(alloc, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); - t.screens.active.cursorAbsolute(12, 12); + s.dirty = false; + const generation = s.generation; + s.delete(io, alloc, &t, .{ .id = .{ + .delete = true, + .image_id = 1, + .placement_id = 7, + } }); + + try testing.expect(s.imageById(1) != null); + try testing.expect(!s.dirty); + try testing.expectEqual(generation, s.generation); +} + +test "storage: uppercase id delete frees image after placement matches" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + const tracked = t.screens.active.pages.countTrackedPins(); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 9, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) }, + }); s.dirty = false; - s.delete(alloc, &t, .{ .intersect_cursor = false }); + s.delete(io, alloc, &t, .{ .id = .{ + .delete = true, + .image_id = 1, + .placement_id = 9, + } }); + + try testing.expectEqual(@as(usize, 0), s.placements.count()); + try testing.expectEqual(@as(usize, 0), s.images.count()); try testing.expect(s.dirty); - try testing.expectEqual(@as(usize, 1), s.placements.count()); - try testing.expectEqual(@as(usize, 2), s.images.count()); - try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins()); + try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins()); +} - // verify the placement is what we expect - try testing.expect(s.placements.get(.{ +test "storage: uppercase id delete without placement frees unplaced image" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + + s.dirty = false; + s.delete(io, alloc, &t, .{ .id = .{ + .delete = true, .image_id = 1, - .placement_id = .{ .tag = .external, .id = 2 }, - }) != null); + } }); + + try testing.expectEqual(@as(usize, 0), s.images.count()); + try testing.expect(s.dirty); } -test "storage: delete intersecting cursor plus unused" { +test "storage: delete intersecting cursor" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 100, .cols = 100 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); defer t.deinit(alloc); t.width_px = 100; t.height_px = 100; @@ -1128,15 +2573,15 @@ test "storage: delete intersecting cursor plus unused" { var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1, .width = 50, .height = 50 }); - try s.addImage(alloc, .{ .id = 2, .width = 25, .height = 25 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); - try s.addPlacement(alloc, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); t.screens.active.cursorAbsolute(12, 12); s.dirty = false; - s.delete(alloc, &t, .{ .intersect_cursor = true }); + s.delete(io, alloc, &t, .{ .intersect_cursor = false }); try testing.expect(s.dirty); try testing.expectEqual(@as(usize, 1), s.placements.count()); try testing.expectEqual(@as(usize, 2), s.images.count()); @@ -1149,26 +2594,115 @@ test "storage: delete intersecting cursor plus unused" { }) != null); } -test "storage: delete intersecting cursor hits multiple" { +test "storage: delete intersecting cursor checks interior row column" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 100, .cols = 100 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); defer t.deinit(alloc); t.width_px = 100; t.height_px = 100; - const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1, .width = 50, .height = 50 }); - try s.addImage(alloc, .{ .id = 2, .width = 25, .height = 25 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); - try s.addPlacement(alloc, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 10, .height = 10 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 20, .y = 0 }) } }); - t.screens.active.cursorAbsolute(26, 26); + // This is inside the right placement and on an interior row shared by + // both placements, but it is outside the left placement's columns. + t.screens.active.cursorAbsolute(21, 5); + s.delete(io, alloc, &t, .{ .intersect_cursor = false }); + + try testing.expectEqual(@as(usize, 1), s.placements.count()); + try testing.expect(s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }) != null); +} + +test "storage: delete intersecting cell checks interior row column" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 10, .height = 10 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 20, .y = 0 }) } }); + + // Protocol coordinates are one-based, so this targets grid cell (21, 5). + s.delete(io, alloc, &t, .{ .intersect_cell = .{ + .x = 22, + .y = 6, + } }); + + try testing.expectEqual(@as(usize, 1), s.placements.count()); + try testing.expect(s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }) != null); +} + +test "storage: delete intersecting cursor plus unused" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + const tracked = t.screens.active.pages.countTrackedPins(); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + + t.screens.active.cursorAbsolute(12, 12); + + s.dirty = false; + s.delete(io, alloc, &t, .{ .intersect_cursor = true }); + try testing.expect(s.dirty); + try testing.expectEqual(@as(usize, 1), s.placements.count()); + try testing.expectEqual(@as(usize, 2), s.images.count()); + try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins()); + + // verify the placement is what we expect + try testing.expect(s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 2 }, + }) != null); +} + +test "storage: delete intersecting cursor hits multiple" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + const tracked = t.screens.active.pages.countTrackedPins(); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + + t.screens.active.cursorAbsolute(26, 26); s.dirty = false; - s.delete(alloc, &t, .{ .intersect_cursor = true }); + s.delete(io, alloc, &t, .{ .intersect_cursor = true }); try testing.expect(s.dirty); try testing.expectEqual(@as(usize, 0), s.placements.count()); try testing.expectEqual(@as(usize, 1), s.images.count()); @@ -1178,7 +2712,8 @@ test "storage: delete intersecting cursor hits multiple" { test "storage: delete by column" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 100, .cols = 100 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); defer t.deinit(alloc); t.width_px = 100; t.height_px = 100; @@ -1186,13 +2721,13 @@ test "storage: delete by column" { var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1, .width = 50, .height = 50 }); - try s.addImage(alloc, .{ .id = 2, .width = 25, .height = 25 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); - try s.addPlacement(alloc, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); s.dirty = false; - s.delete(alloc, &t, .{ .column = .{ + s.delete(io, alloc, &t, .{ .column = .{ .delete = false, .x = 60, } }); @@ -1211,19 +2746,20 @@ test "storage: delete by column" { test "storage: delete by column 1x1" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 100, .cols = 100 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); defer t.deinit(alloc); t.width_px = 100; t.height_px = 100; var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1, .width = 1, .height = 1 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); - try s.addPlacement(alloc, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 0 }) } }); - try s.addPlacement(alloc, 1, 3, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 2, .y = 0 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 3, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 2, .y = 0 }) } }); - s.delete(alloc, &t, .{ .column = .{ + s.delete(io, alloc, &t, .{ .column = .{ .delete = false, .x = 2, } }); @@ -1244,7 +2780,8 @@ test "storage: delete by column 1x1" { test "storage: delete by row" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 100, .cols = 100 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); defer t.deinit(alloc); t.width_px = 100; t.height_px = 100; @@ -1252,13 +2789,13 @@ test "storage: delete by row" { var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1, .width = 50, .height = 50 }); - try s.addImage(alloc, .{ .id = 2, .width = 25, .height = 25 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); - try s.addPlacement(alloc, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 50, .height = 50 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 25, .height = 25 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 25, .y = 25 }) } }); s.dirty = false; - s.delete(alloc, &t, .{ .row = .{ + s.delete(io, alloc, &t, .{ .row = .{ .delete = false, .y = 60, } }); @@ -1277,19 +2814,20 @@ test "storage: delete by row" { test "storage: delete by row 1x1" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 100, .cols = 100 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 100, .cols = 100 }); defer t.deinit(alloc); t.width_px = 100; t.height_px = 100; var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1, .width = 1, .height = 1 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 0 }) } }); - try s.addPlacement(alloc, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 1 }) } }); - try s.addPlacement(alloc, 1, 3, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 2 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 0 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 3, .{ .location = .{ .pin = try trackPin(&t, .{ .y = 2 }) } }); - s.delete(alloc, &t, .{ .row = .{ + s.delete(io, alloc, &t, .{ .row = .{ .delete = false, .y = 2, } }); @@ -1310,22 +2848,23 @@ test "storage: delete by row 1x1" { test "storage: delete images by range 1" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); try testing.expectEqual(@as(usize, 3), s.images.count()); try testing.expectEqual(@as(usize, 2), s.placements.count()); s.dirty = false; - s.delete(alloc, &t, .{ .range = .{ .delete = false, .first = 1, .last = 2 } }); + s.delete(io, alloc, &t, .{ .range = .{ .delete = false, .first = 1, .last = 2 } }); try testing.expect(s.dirty); try testing.expectEqual(@as(usize, 3), s.images.count()); try testing.expectEqual(@as(usize, 0), s.placements.count()); @@ -1335,22 +2874,23 @@ test "storage: delete images by range 1" { test "storage: delete images by range 2" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); try testing.expectEqual(@as(usize, 3), s.images.count()); try testing.expectEqual(@as(usize, 2), s.placements.count()); s.dirty = false; - s.delete(alloc, &t, .{ .range = .{ .delete = true, .first = 1, .last = 2 } }); + s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 1, .last = 2 } }); try testing.expect(s.dirty); try testing.expectEqual(@as(usize, 1), s.images.count()); try testing.expectEqual(@as(usize, 0), s.placements.count()); @@ -1360,58 +2900,237 @@ test "storage: delete images by range 2" { test "storage: delete images by range 3" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 3, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); try testing.expectEqual(@as(usize, 3), s.images.count()); - try testing.expectEqual(@as(usize, 2), s.placements.count()); + try testing.expectEqual(@as(usize, 3), s.placements.count()); s.dirty = false; - s.delete(alloc, &t, .{ .range = .{ .delete = false, .first = 1, .last = 1 } }); + s.delete(io, alloc, &t, .{ .range = .{ .delete = false, .first = 2, .last = 2 } }); try testing.expect(s.dirty); try testing.expectEqual(@as(usize, 3), s.images.count()); - try testing.expectEqual(@as(usize, 0), s.placements.count()); - try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins()); + try testing.expectEqual(@as(usize, 2), s.placements.count()); + try testing.expect(s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }) != null); + try testing.expect(s.placements.get(.{ + .image_id = 3, + .placement_id = .{ .tag = .external, .id = 1 }, + }) != null); + try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins()); } test "storage: delete images by range 4" { const testing = std.testing; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); const tracked = t.screens.active.pages.countTrackedPins(); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); - try s.addImage(alloc, .{ .id = 2 }); - try s.addImage(alloc, .{ .id = 3 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); - try s.addPlacement(alloc, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 3, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); try testing.expectEqual(@as(usize, 3), s.images.count()); - try testing.expectEqual(@as(usize, 2), s.placements.count()); + try testing.expectEqual(@as(usize, 3), s.placements.count()); s.dirty = false; - s.delete(alloc, &t, .{ .range = .{ .delete = true, .first = 1, .last = 1 } }); + s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 2, .last = 2 } }); try testing.expect(s.dirty); + try testing.expectEqual(@as(usize, 2), s.images.count()); + try testing.expectEqual(@as(usize, 2), s.placements.count()); + try testing.expect(s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }) != null); + try testing.expect(s.placements.get(.{ + .image_id = 3, + .placement_id = .{ .tag = .external, .id = 1 }, + }) != null); + try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins()); +} + +test "storage: uppercase range deletes unplaced image data" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + + s.delete(io, alloc, &t, .{ + .range = .{ + .delete = true, + // Zero is the default lower bound when x is omitted. + .first = 0, + .last = 2, + }, + }); try testing.expectEqual(@as(usize, 1), s.images.count()); - try testing.expectEqual(@as(usize, 0), s.placements.count()); - try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins()); + try testing.expect(s.images.contains(3)); +} + +test "storage: delete images by empty range" { + // Ranges that select nothing are a silent no-op, matching kitty, which + // performs no validation and filters with `x <= image_id <= y`. + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + const tracked = t.screens.active.pages.countTrackedPins(); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + + // Inverted range. + s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 5, .last = 4 } }); + try testing.expectEqual(@as(usize, 2), s.images.count()); + try testing.expectEqual(@as(usize, 2), s.placements.count()); + + // Upper bound omitted, so it defaults to zero. + s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 5, .last = 0 } }); + try testing.expectEqual(@as(usize, 2), s.images.count()); + try testing.expectEqual(@as(usize, 2), s.placements.count()); + + // Both bounds omitted. Image IDs are never zero so this matches nothing. + s.delete(io, alloc, &t, .{ .range = .{ .delete = true, .first = 0, .last = 0 } }); + try testing.expectEqual(@as(usize, 2), s.images.count()); + try testing.expectEqual(@as(usize, 2), s.placements.count()); + + // Both placements survive, so both of their pins are still tracked. + try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins()); +} + +test "storage: erase display preserves scrollback and reclaims unplaced images" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ + .rows = 2, + .cols = 2, + .max_scrollback_bytes = 4096, + }); + defer t.deinit(alloc); + t.width_px = 2; + t.height_px = 2; + + const s = &t.screens.active.kitty_images; + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .y = 0 }) }, + }); + try t.scrollUp(1); + + const history_key: ImageStorage.PlacementKey = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }; + try testing.expect(t.screens.active.pages.pointFromPin( + .active, + s.placements.get(history_key).?.location.pin.*, + ) == null); + + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 1, .height = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .y = 0 }) }, + }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3, .width = 1, .height = 1 }); + + t.eraseDisplay(.complete, false); + try testing.expectEqual(@as(usize, 1), s.placements.count()); + try testing.expect(s.placements.contains(history_key)); + try testing.expectEqual(@as(usize, 1), s.images.count()); + try testing.expect(s.images.contains(1)); + try testing.expect(!s.images.contains(2)); + try testing.expect(!s.images.contains(3)); +} + +test "storage: cell offsets stay within explicit destination rectangle" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 5, .rows = 5 }); + defer t.deinit(alloc); + t.width_px = 50; // 10 px per col + t.height_px = 100; // 20 px per row + + // Explicit columns and rows describe the far cell boundaries. Offsets + // move the near edge inward without moving those far edges. + const placement: ImageStorage.Placement = .{ + .location = .{ .virtual = {} }, + .x_offset = 3, + .y_offset = 4, + .columns = 2, + .rows = 1, + }; + const actual = placement.pixelSize(.{ .width = 4, .height = 3 }, &t); + try testing.expectEqual(@as(u32, 17), actual.width); + try testing.expectEqual(@as(u32, 16), actual.height); + try testing.expectEqual(@as(u32, 2), placement.gridSize(.{ .width = 4, .height = 3 }, &t).cols); + try testing.expectEqual(@as(u32, 1), placement.gridSize(.{ .width = 4, .height = 3 }, &t).rows); +} + +test "storage: cell offsets clamp to cell bounds" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 5, .rows = 5 }); + defer t.deinit(alloc); + t.width_px = 50; // 10 px per col + t.height_px = 100; // 20 px per row + + const placement: ImageStorage.Placement = .{ + .location = .{ .virtual = {} }, + .x_offset = std.math.maxInt(u32), + .y_offset = std.math.maxInt(u32), + .columns = 1, + .rows = 1, + }; + const offset = placement.cellOffset(&t); + try testing.expectEqual(@as(u32, 9), offset.x); + try testing.expectEqual(@as(u32, 19), offset.y); + + // Even hostile offsets leave one pixel inside the requested cell. + const actual = placement.pixelSize(.{ .width = 1, .height = 1 }, &t); + try testing.expectEqual(@as(u32, 1), actual.width); + try testing.expectEqual(@as(u32, 1), actual.height); } test "storage: aspect ratio calculation when only columns or rows specified" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try terminal.Terminal.init(alloc, .{ .cols = 100, .rows = 100 }); + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 100, .rows = 100 }); defer t.deinit(alloc); t.width_px = 1000; // 10 px per col t.height_px = 2000; // 20 px per row @@ -1453,10 +3172,162 @@ test "storage: aspect ratio calculation when only columns or rows specified" { } } +test "storage: default source rectangle is intersected before sizing" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 5, .rows = 5 }); + defer t.deinit(alloc); + + const placement: ImageStorage.Placement = .{ + .location = .{ .virtual = {} }, + .source_x = 3, + .source_y = 1, + }; + const actual = placement.pixelSize(.{ .width = 4, .height = 3 }, &t); + try testing.expectEqual(@as(u32, 1), actual.width); + try testing.expectEqual(@as(u32, 2), actual.height); +} + +test "storage: explicit source rectangle is intersected before sizing" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + + // The requested 8x8 rectangle intersects this 10x10 image as 2x3. + // With a two-column destination, the clipped 2:3 aspect ratio produces + // a 20x30 pixel destination. + const placement: ImageStorage.Placement = .{ + .location = .{ .virtual = {} }, + .source_x = 8, + .source_y = 7, + .source_width = 8, + .source_height = 8, + .columns = 2, + }; + const actual = placement.pixelSize(.{ .width = 10, .height = 10 }, &t); + try testing.expectEqual(@as(u32, 20), actual.width); + try testing.expectEqual(@as(u32, 30), actual.height); +} + +test "storage: placement geometry handles untrusted dimensions" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + const max = std.math.maxInt(u32); + + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 2, .rows = 2 }); + defer t.deinit(alloc); + t.width_px = max; + t.height_px = max; + + // Cell dimensions multiplied by protocol-controlled row and column + // counts saturate instead of panicking or wrapping. + { + const placement: ImageStorage.Placement = .{ + .location = .{ .virtual = {} }, + .columns = 3, + .rows = 3, + }; + const actual = placement.pixelSize(.{ .width = 1, .height = 1 }, &t); + try testing.expectEqual(max, actual.width); + try testing.expectEqual(max, actual.height); + } + + // Aspect-ratio scaling also saturates when the derived dimension does + // not fit in the public u32 geometry type. + { + const placement: ImageStorage.Placement = .{ + .location = .{ .virtual = {} }, + .columns = 3, + .source_height = max, + }; + const actual = placement.pixelSize(.{ .width = 1, .height = 1 }, &t); + try testing.expectEqual(max, actual.width); + try testing.expectEqual(max, actual.height); + } + { + const placement: ImageStorage.Placement = .{ + .location = .{ .virtual = {} }, + .rows = 3, + .source_width = max, + }; + const actual = placement.pixelSize(.{ .width = 1, .height = 1 }, &t); + try testing.expectEqual(max, actual.width); + try testing.expectEqual(max, actual.height); + } + + // Pixel offsets are protocol-controlled too. Clamp them to the cell + // before including them in grid geometry. + t.width_px = 2; + t.height_px = 2; + { + const placement: ImageStorage.Placement = .{ + .location = .{ .virtual = {} }, + .x_offset = max, + .y_offset = max, + }; + const actual = placement.gridSize(.{ .width = 1, .height = 1 }, &t); + try testing.expectEqual(@as(u32, 1), actual.cols); + try testing.expectEqual(@as(u32, 1), actual.rows); + } + + const pin = try trackPin(&t, .{ .x = 0, .y = 0 }); + defer t.screens.active.pages.untrackPin(pin); + + // Explicit maximum dimensions must clamp the rectangle to the terminal + // without overflowing its horizontal extent. + { + const placement: ImageStorage.Placement = .{ + .location = .{ .pin = pin }, + .columns = max, + .rows = 1, + }; + const rect = placement.rect(.{ .width = 1, .height = 1 }, &t).?; + try testing.expectEqual(@as(size.CellCountInt, 1), rect.bottom_right.x); + } + + // A garbage pin represents content that has been pruned from retained + // history. Its fallback location must not make the placement visible. + pin.garbage = true; + { + const placement: ImageStorage.Placement = .{ + .location = .{ .pin = pin }, + .columns = 1, + .rows = 1, + }; + try testing.expect(placement.rect(.{ .width = 1, .height = 1 }, &t) == null); + } + pin.garbage = false; + + // Terminals can temporarily have no pixel geometry. A placement whose + // computed grid is empty has no rectangle, so rect must not subtract one + // from a zero row count. + t.width_px = 0; + t.height_px = 0; + { + const placement: ImageStorage.Placement = .{ + .location = .{ .pin = pin }, + .columns = 1, + }; + const actual = placement.gridSize(.{ .width = 1, .height = 1 }, &t); + try testing.expectEqual(@as(u32, 0), actual.cols); + try testing.expectEqual(@as(u32, 0), actual.rows); + try testing.expect(placement.rect(.{ .width = 1, .height = 1 }, &t) == null); + } +} + test "storage: generation stamps on image add and replace" { const testing = std.testing; + const io = testing.io; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); var s: ImageStorage = .{}; @@ -1465,7 +3336,7 @@ test "storage: generation stamps on image add and replace" { // Fresh storage has generation zero (never mutated). try testing.expectEqual(@as(u64, 0), s.generation); - try s.addImage(alloc, .{ .id = 1, .width = 1, .height = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 }); const gen1 = s.generation; try testing.expect(gen1 > 0); @@ -1473,7 +3344,7 @@ test "storage: generation stamps on image add and replace" { try testing.expectEqual(gen1, img1.generation); // A second image gets a strictly greater stamp. - try s.addImage(alloc, .{ .id = 2, .width = 1, .height = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .width = 1, .height = 1 }); const gen2 = s.generation; try testing.expect(gen2 > gen1); try testing.expectEqual(gen2, s.imageById(2).?.generation); @@ -1481,7 +3352,7 @@ test "storage: generation stamps on image add and replace" { // Retransmitting the same image ID (identical dimensions) gets a // fresh stamp: this is what makes same-sized retransmissions // detectable by renderers. - try s.addImage(alloc, .{ .id = 1, .width = 1, .height = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .width = 1, .height = 1 }); const gen3 = s.generation; try testing.expect(gen3 > gen2); try testing.expectEqual(gen3, s.imageById(1).?.generation); @@ -1492,16 +3363,17 @@ test "storage: generation stamps on image add and replace" { test "storage: generation bumps on placement and delete" { const testing = std.testing; + const io = testing.io; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); - try s.addImage(alloc, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); const gen_add = s.generation; - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); const gen_place = s.generation; try testing.expect(gen_place > gen_add); @@ -1510,26 +3382,32 @@ test "storage: generation bumps on placement and delete" { _ = s.imageByNumber(1); try testing.expectEqual(gen_place, s.generation); - s.delete(alloc, &t, .{ .all = true }); + s.delete(io, alloc, &t, .{ .all = true }); try testing.expect(s.generation > gen_place); } test "storage: generation bumps when setLimit evicts or disables" { const testing = std.testing; + const io = testing.io; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); const data = try alloc.dupe(u8, "1234"); - try s.addImage(alloc, .{ .id = 1, .width = 1, .height = 1, .data = data }); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 1, + .height = 1, + .data = .{ .complete = data }, + }); const gen_add = s.generation; // Lowering the limit evicts the image and must mark a mutation. s.dirty = false; - try s.setLimit(alloc, t.screens.active, 1); + s.setLimit(io, alloc, t.screens.active, 1); try testing.expect(s.dirty); try testing.expect(s.generation > gen_add); try testing.expectEqual(@as(usize, 0), s.images.count()); @@ -1537,15 +3415,16 @@ test "storage: generation bumps when setLimit evicts or disables" { // Disabling (limit=0) resets the storage and must mark a mutation. s.dirty = false; - try s.setLimit(alloc, t.screens.active, 0); + s.setLimit(io, alloc, t.screens.active, 0); try testing.expect(s.dirty); try testing.expect(s.generation > gen_evict); } test "storage: imageByNumber returns most recently transmitted" { const testing = std.testing; + const io = testing.io; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); var s: ImageStorage = .{}; @@ -1553,27 +3432,28 @@ test "storage: imageByNumber returns most recently transmitted" { // Two images sharing a number: the newest transmission wins, // regardless of insertion order or clock resolution. - try s.addImage(alloc, .{ .id = 1, .number = 7 }); - try s.addImage(alloc, .{ .id = 2, .number = 7 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .number = 7 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2, .number = 7 }); try testing.expectEqual(@as(u32, 2), s.imageByNumber(7).?.id); // Retransmit the first: it becomes the newest. - try s.addImage(alloc, .{ .id = 1, .number = 7 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1, .number = 7 }); try testing.expectEqual(@as(u32, 1), s.imageByNumber(7).?.id); } test "storage: nextGeneration is unique and monotonic" { const testing = std.testing; - const a = nextGeneration(); - const b = nextGeneration(); + const a = nextGeneration(testing.io); + const b = nextGeneration(testing.io); try testing.expect(b > a); try testing.expect(a > 0); } test "storage: no-op delete does not mark a mutation" { const testing = std.testing; + const io = testing.io; const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 3, .cols = 3 }); + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); defer t.deinit(alloc); var s: ImageStorage = .{}; @@ -1581,21 +3461,1204 @@ test "storage: no-op delete does not mark a mutation" { // A delete-all on an empty storage (this runs on every screen // clear) must not dirty the state or bump the generation. - s.delete(alloc, &t, .{ .all = true }); + s.delete(io, alloc, &t, .{ .all = true }); try testing.expect(!s.dirty); try testing.expectEqual(@as(u64, 0), s.generation); // Same for a delete that matches nothing. - try s.addImage(alloc, .{ .id = 1 }); - try s.addPlacement(alloc, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }); const gen = s.generation; s.dirty = false; - s.delete(alloc, &t, .{ .id = .{ .image_id = 42 } }); + s.delete(io, alloc, &t, .{ .id = .{ .image_id = 42 } }); try testing.expect(!s.dirty); try testing.expectEqual(gen, s.generation); // But a delete that removes something does mark a mutation. - s.delete(alloc, &t, .{ .id = .{ .image_id = 1 } }); + s.delete(io, alloc, &t, .{ .id = .{ .image_id = 1 } }); + try testing.expect(s.dirty); + try testing.expect(s.generation > gen); +} + +test "storage: evicts images in priority order" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{ .total_limit = 192 }; + defer s.deinit(alloc, t.screens.active); + + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .data = .{ .complete = try alloc.dupe(u8, "*" ** 64) }, + .metadata = .{ .transient = false }, + }); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 2, + .data = .{ .complete = try alloc.dupe(u8, "*" ** 64) }, + .metadata = .{ .transient = true }, + }); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 3, + .data = .{ .complete = try alloc.dupe(u8, "*" ** 64) }, + .metadata = .{ .transient = true }, + }); + try s.addPlacement( + io, + alloc, + t.screens.active, + 2, + 1, + .{ .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) } }, + ); + + const gen = s.generation; + const result = s.evictImage(io, alloc, t.screens.active, 96); try testing.expect(s.dirty); try testing.expect(s.generation > gen); + try testing.expectEqual(true, result); + try testing.expectEqual(1, s.images.count()); + try testing.expect(!s.images.contains(1)); + try testing.expect(s.images.contains(2)); + try testing.expect(!s.images.contains(3)); +} + +test "storage: eviction releases placement pins" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{ .total_limit = 128 }; + defer s.deinit(alloc, t.screens.active); + + const tracked = t.screens.active.pages.countTrackedPins(); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .data = .{ .complete = try alloc.dupe(u8, "*" ** 64) }, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) }, + }); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 2, + .data = .{ .complete = try alloc.dupe(u8, "*" ** 64) }, + }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) }, + }); + try testing.expectEqual(tracked + 2, t.screens.active.pages.countTrackedPins()); + + // Adding a third image evicts the oldest equally-ranked image and its + // placement. The newer image's placement and tracked pin remain intact. + try s.addImage(io, alloc, t.screens.active, .{ + .id = 3, + .data = .{ .complete = try alloc.dupe(u8, "*" ** 64) }, + }); + try testing.expect(!s.images.contains(1)); + try testing.expect(s.images.contains(2)); + try testing.expect(s.images.contains(3)); + try testing.expectEqual(@as(usize, 1), s.placements.count()); + try testing.expect(s.placements.contains(.{ + .image_id = 2, + .placement_id = .{ .tag = .external, .id = 1 }, + })); + try testing.expectEqual(tracked + 1, t.screens.active.pages.countTrackedPins()); +} + +test "storage: pending image completes once and preserves age" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{ .total_limit = 16 }; + defer s.deinit(alloc, t.screens.active); + + const pending = try s.addPendingImage(io, alloc, t.screens.active, .{ + .id = 1, + .number = 7, + .width = 2, + .height = 2, + .format = .rgba, + .data = .{ .pending = 16 }, + }); + try testing.expectEqual(@as(usize, 16), s.total_bytes); + try testing.expectEqual(@as(u32, 1), s.imageByNumber(7).?.id); + try testing.expect(s.imageById(1).?.data.isPending()); + + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) }, + }); + try testing.expectEqual(@as(usize, 1), s.placements.count()); + + const wrong = try alloc.dupe(u8, "short"); + const wrong_completed = pending.complete(&s, io, wrong); + try testing.expect(!wrong_completed); + defer alloc.free(wrong); + + const storage_generation = s.generation; + s.dirty = false; + const pixels = try alloc.dupe(u8, "*" ** 16); + try testing.expect(pending.complete(&s, io, pixels)); + try testing.expect(s.dirty); + try testing.expect(s.generation > storage_generation); + try testing.expectEqual(pending.generation, s.imageById(1).?.generation); + try testing.expectEqual(@as(usize, 16), s.total_bytes); + try testing.expectEqualSlices(u8, pixels, s.imageById(1).?.data.bytes().?); + + const duplicate = try alloc.dupe(u8, "!" ** 16); + const duplicate_completed = pending.complete(&s, io, duplicate); + try testing.expect(!duplicate_completed); + defer alloc.free(duplicate); +} + +test "storage: stale pending completion loses to delete replacement and eviction" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{ .total_limit = 8 }; + defer s.deinit(alloc, t.screens.active); + + const deleted = try s.addPendingImage(io, alloc, t.screens.active, .{ + .id = 1, + .data = .{ .pending = 4 }, + }); + s.delete(io, alloc, &t, .{ .id = .{ .delete = true, .image_id = 1 } }); + const deleted_data = try alloc.dupe(u8, "gone"); + const deleted_completed = deleted.complete(&s, io, deleted_data); + try testing.expect(!deleted_completed); + defer alloc.free(deleted_data); + + const replaced = try s.addPendingImage(io, alloc, t.screens.active, .{ + .id = 2, + .data = .{ .pending = 4 }, + }); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 2, + .data = .{ .complete = try alloc.dupe(u8, "live") }, + }); + const replaced_data = try alloc.dupe(u8, "late"); + const replaced_completed = replaced.complete(&s, io, replaced_data); + try testing.expect(!replaced_completed); + defer alloc.free(replaced_data); + try testing.expectEqualStrings("live", s.imageById(2).?.data.bytes().?); + + const evicted = try s.addPendingImage(io, alloc, t.screens.active, .{ + .id = 3, + .data = .{ .pending = 4 }, + }); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 4, + .data = .{ .complete = try alloc.dupe(u8, "12345678") }, + }); + try testing.expect(s.imageById(3) == null); + const evicted_data = try alloc.dupe(u8, "late"); + const evicted_completed = evicted.complete(&s, io, evicted_data); + try testing.expect(!evicted_completed); + defer alloc.free(evicted_data); +} + +test "storage: replacement reuses pending reservation and removes placements" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{ .total_limit = 12 }; + defer s.deinit(alloc, t.screens.active); + const tracked = t.screens.active.pages.countTrackedPins(); + + const pending = try s.addPendingImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 2, + .height = 1, + .format = .rgba, + .data = .{ .pending = 8 }, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) }, + }); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 2, + .data = .{ .complete = try alloc.dupe(u8, "keep") }, + }); + + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 2, + .height = 1, + .format = .rgba, + .data = .{ .complete = try alloc.dupe(u8, "12345678") }, + }); + try testing.expect(s.images.contains(1)); + try testing.expect(s.images.contains(2)); + try testing.expectEqual(@as(usize, 0), s.placements.count()); + try testing.expectEqual(tracked, t.screens.active.pages.countTrackedPins()); + try testing.expectEqual( + @as(u30, 0), + s.imageById(1).?.metadata.placement_count, + ); + try testing.expectEqual(@as(usize, 12), s.total_bytes); + + const stale = try alloc.dupe(u8, "snapshot"); + const stale_completed = pending.complete(&s, io, stale); + try testing.expect(!stale_completed); + defer alloc.free(stale); + + // Growing the replacement requires eviction, but the replacement ID is + // excluded. The other image supplies the needed bytes. + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .data = .{ .complete = try alloc.dupe(u8, "1234567890") }, + }); + try testing.expect(s.images.contains(1)); + try testing.expect(!s.images.contains(2)); + try testing.expectEqual(@as(usize, 0), s.placements.count()); + try testing.expectEqual(@as(usize, 10), s.total_bytes); + + s.delete(io, alloc, &t, .{ .id = .{ .delete = true, .image_id = 1 } }); + try testing.expectEqual(@as(usize, 0), s.images.count()); + try testing.expectEqual(@as(usize, 0), s.placements.count()); +} + +test "storage: pending images share exact eviction ordering" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{ .total_limit = 192 }; + defer s.deinit(alloc, t.screens.active); + + _ = try s.addPendingImage(io, alloc, t.screens.active, .{ + .id = 1, + .data = .{ .pending = 64 }, + .metadata = .{ .transient = false }, + }); + _ = try s.addPendingImage(io, alloc, t.screens.active, .{ + .id = 2, + .data = .{ .pending = 64 }, + .metadata = .{ .transient = true }, + }); + try s.addImage(io, alloc, t.screens.active, .{ + .id = 3, + .data = .{ .complete = try alloc.dupe(u8, "*" ** 64) }, + .metadata = .{ .transient = true }, + }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) }, + }); + + // ID 3 is transient and unused, so one exact-size eviction is enough. + try testing.expect(s.evictImage(io, alloc, t.screens.active, 64)); + try testing.expect(s.images.contains(1)); + try testing.expect(s.images.contains(2)); + try testing.expect(!s.images.contains(3)); + try testing.expectEqual(@as(usize, 128), s.total_bytes); +} + +test "storage: nextImageId number matches Kitty get_free_client_id" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + + // Empty storage returns 1, as does Kitty. + try testing.expectEqual(@as(u32, 1), s.nextImageId(.explicit)); + + // Contiguous IDs from 1: the next ID after them. + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 3 }); + try testing.expectEqual(@as(u32, 4), s.nextImageId(.explicit)); + + // The first gap is filled, even when higher IDs exist. + try s.addImage(io, alloc, t.screens.active, .{ .id = 100 }); + s.delete(io, alloc, &t, .{ .id = .{ .image_id = 2, .delete = true } }); + try testing.expectEqual(@as(u32, 2), s.nextImageId(.explicit)); + + // 1 is reused as soon as it is free. + s.delete(io, alloc, &t, .{ .id = .{ .image_id = 1, .delete = true } }); + try testing.expectEqual(@as(u32, 1), s.nextImageId(.explicit)); +} + +test "storage: nextImageId implicit skips in-use ids and zero" { + const testing = std.testing; + const io = testing.io; + const alloc = testing.allocator; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 3, .cols = 3 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + + // A client image already sits on the counter's first two values. + try s.addImage(io, alloc, t.screens.active, .{ .id = 2147483647 }); + try s.addImage(io, alloc, t.screens.active, .{ .id = 2147483648 }); + try testing.expectEqual(@as(u32, 2147483649), s.nextImageId(.implicit)); + try testing.expectEqual(@as(u32, 2147483650), s.nextImageId(.implicit)); + + // Wrapping skips zero, which means "no ID" protocol-wide. + s.next_image_id = std.math.maxInt(u32); + try testing.expectEqual(std.math.maxInt(u32), s.nextImageId(.implicit)); + try testing.expectEqual(@as(u32, 1), s.nextImageId(.implicit)); +} + +test "storage: scroll margins placement inside region scrolls and clips at top" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // 2-row image (10x10px cells) inside the region (rows 1-4). + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 20, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) }, + }); + t.setTopAndBottomMargin(2, 5); + t.setCursorPos(5, 1); + + // Entirely inside: moves up with the content. + try t.index(); + { + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 1 }, pt.coord()); + } + + // Would extend above the top margin: anchored at the top margin + // with the top cell row clipped off the source. + try t.index(); + { + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 1 }, pt.coord()); + try testing.expectEqual(@as(u32, 10), p.source_y); + try testing.expectEqual(@as(u32, 10), p.source_height); + } + + // Fully clipped: deleted. + try t.index(); + try testing.expectEqual(@as(usize, 0), storage.placements.count()); + // The image itself is retained. + try testing.expectEqual(@as(usize, 1), storage.images.count()); +} + +test "storage: scroll margins placement straddling region does not move" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // 3-row image anchored at the top of a 2-row region: not entirely + // within the region, so region scrolls must not move it. This + // exercises the scrollback-creating path (region top at row 0). + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 30, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) }, + }); + t.setTopAndBottomMargin(1, 2); + t.setCursorPos(2, 1); + + try t.index(); + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 0 }, pt.coord()); + try testing.expectEqual(@as(u32, 0), p.source_y); + try testing.expectEqual(@as(u32, 0), p.source_height); +} + +test "storage: scroll margins placement below region does not move" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // A "status bar" image below the bottom margin must stay put while + // the region above it scrolls (scrollback-creating path). + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 10, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 5 }) }, + }); + t.setTopAndBottomMargin(1, 4); + t.setCursorPos(4, 1); + + try t.index(); + try t.index(); + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 5 }, pt.coord()); +} + +test "storage: scroll margins reverse index clips at bottom" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // 2-row image at the bottom of the region (rows 1-4). + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 20, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 3 }) }, + }); + t.setTopAndBottomMargin(2, 5); + t.setCursorPos(2, 1); + + // Moves down, with the bottom cell row clipped off the source. + t.reverseIndex(); + { + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 4 }, pt.coord()); + try testing.expectEqual(@as(u32, 0), p.source_y); + try testing.expectEqual(@as(u32, 10), p.source_height); + } + + // Fully clipped: deleted. + t.reverseIndex(); + try testing.expectEqual(@as(usize, 0), storage.placements.count()); +} + +test "storage: scroll margins scaled placement clips proportionally" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // A 40px-tall image scaled into 2 rows, inside a 2-row region. + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 40, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 1 }) }, + .columns = 1, + .rows = 2, + }); + t.setTopAndBottomMargin(2, 3); + t.setCursorPos(3, 1); + + try t.index(); + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 1 }, pt.coord()); + try testing.expectEqual(@as(u32, 20), p.source_y); + try testing.expectEqual(@as(u32, 20), p.source_height); + try testing.expectEqual(@as(u32, 1), p.rows); + try testing.expectEqual(@as(u32, 1), p.columns); +} + +test "storage: scroll margins left/right margins respect columns" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // One placement outside the left/right margin columns, one inside. + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 10, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) }, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 2, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 2, .y = 2 }) }, + }); + + t.modes.set(.enable_left_and_right_margin, true); + t.setLeftAndRightMargin(2, 8); + + try t.scrollUp(1); + + // Outside the margin columns: stays. + { + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 2 }, pt.coord()); + } + + // Inside: moves up. + { + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 2 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 2, .y = 1 }, pt.coord()); + } +} + +test "storage: scroll margins insert/delete lines do not move placements" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // Kitty does not scroll images for IL/DL, only for IND/RI/SU/SD. + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 10, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) }, + }); + t.setTopAndBottomMargin(2, 5); + t.setCursorPos(2, 1); + + t.deleteLines(1); + { + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 2 }, pt.coord()); + } + + t.insertLines(1); + { + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 2 }, pt.coord()); + } +} + +test "storage: scroll without margins moves placement into scrollback" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // No margins: the placement follows its anchored row into the + // scrollback via pin tracking (kitty's marginless behavior). + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 20, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) }, + }); + t.setCursorPos(6, 1); + + try t.index(); + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + try testing.expect(t.screens.active.pages.pointFromPin( + .active, + p.location.pin.*, + ) == null); + const pt = t.screens.active.pages.pointFromPin(.screen, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 0 }, pt.coord()); +} + +test "storage: scroll margins large scroll deletes inside placement" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 10, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) }, + }); + t.setTopAndBottomMargin(2, 5); + + try t.scrollUp(10); + try testing.expectEqual(@as(usize, 0), storage.placements.count()); +} + +test "storage: scroll margins straddling placement pin inside region restored" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // 2-row image anchored on the bottom margin row, extending below + // the region: not entirely inside, so it must not move even though + // the region scroll moves every tracked pin within the region rows. + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 20, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 4 }) }, + }); + t.setTopAndBottomMargin(2, 5); + t.setCursorPos(5, 1); + + try t.index(); + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 4 }, pt.coord()); + try testing.expectEqual(@as(u32, 0), p.source_y); + try testing.expectEqual(@as(u32, 0), p.source_height); +} + +test "storage: scroll margins multi-line scroll up with scrollback" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 6 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 60; + + // 1-row image inside a region whose top is the screen top, so the + // scroll pushes text into the scrollback (window shift) while the + // image moves within the region. + const storage: *ImageStorage = &t.screens.active.kitty_images; + try storage.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 10, + .height = 10, + }); + try storage.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 2 }) }, + }); + t.setTopAndBottomMargin(1, 4); + + try t.scrollUp(2); + { + const p = storage.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }).?; + const pt = t.screens.active.pages.pointFromPin(.active, p.location.pin.*).?; + try testing.expectEqual(point.Coordinate{ .x = 0, .y = 0 }, pt.coord()); + } + + // Another two rows scrolls it out of the region: deleted. + try t.scrollUp(2); + try testing.expectEqual(@as(usize, 0), storage.placements.count()); +} + +test "storage: resolveChain accumulates offsets to the pin root" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) }, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ + .location = .{ .relative = .{ + .parent = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }, + .horizontal_offset = 2, + .vertical_offset = 1, + } }, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 3, .{ + .location = .{ .relative = .{ + .parent = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 2 }, + }, + .horizontal_offset = -1, + .vertical_offset = 4, + } }, + }); + + const grandchild = s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 3 }, + }).?; + const chain = s.resolveChain(grandchild.location.relative).?; + try testing.expect(chain.root_key.eql(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + })); + try testing.expect(chain.root.location == .pin); + try testing.expectEqual(@as(i32, 1), chain.horizontal_offset); + try testing.expectEqual(@as(i32, 5), chain.vertical_offset); +} + +test "storage: resolveChain finds virtual roots" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .virtual = {} }, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 2, .{ + .location = .{ .relative = .{ + .parent = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + }, + .horizontal_offset = 1, + } }, + }); + + const child = s.placements.get(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 2 }, + }).?; + const chain = s.resolveChain(child.location.relative).?; + try testing.expect(chain.root.location == .virtual); + try testing.expect(chain.root_key.eql(.{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + })); + try testing.expectEqual(@as(i32, 1), chain.horizontal_offset); + try testing.expectEqual(@as(i32, 0), chain.vertical_offset); +} + +test "storage: eviction removes orphaned relative placements" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + s.total_limit = 8; + + // Image 1 holds most of the byte budget and has a pin placement. + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 2, + .height = 1, + .data = .{ .complete = try alloc.dupe(u8, &.{ 0, 0, 0, 0, 0, 0 }) }, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 1, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 1, .y = 1 }) }, + }); + + // Image 2's placement is relative to image 1's placement. + try s.addImage(io, alloc, t.screens.active, .{ .id = 2 }); + try s.addPlacement(io, alloc, t.screens.active, 2, 1, .{ + .location = .{ .relative = .{ .parent = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 1 }, + } } }, + }); + + // Adding image 3 exceeds the limit and evicts image 1 (oldest), + // removing its placement, which orphans image 2's placement. + try s.addImage(io, alloc, t.screens.active, .{ + .id = 3, + .width = 2, + .height = 1, + .data = .{ .complete = try alloc.dupe(u8, &.{ 0, 0, 0, 0, 0, 0 }) }, + }); + + try testing.expect(s.imageById(1) == null); + try testing.expectEqual(@as(usize, 0), s.placements.count()); + try testing.expect(s.imageById(2) != null); +} + +test "storage: placeholderTarget lookup" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); + defer t.deinit(alloc); + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + try s.addImage(io, alloc, t.screens.active, .{ .id = 1 }); + try s.addPlacement(io, alloc, t.screens.active, 1, 5, .{ + .location = .{ .virtual = {} }, + }); + + // Exact external ID match. + { + const target = s.placeholderTarget(1, 5).?; + const expected: ImageStorage.PlacementKey = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 5 }, + }; + try testing.expectEqual(expected, target.key); + try testing.expect(target.placement.location == .virtual); + } + + // Zero placement ID falls back to the image's virtual placement. + { + const target = s.placeholderTarget(1, 0).?; + const expected: ImageStorage.PlacementKey = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 5 }, + }; + try testing.expectEqual(expected, target.key); + } + + try testing.expect(s.placeholderTarget(1, 9) == null); + try testing.expect(s.placeholderTarget(2, 0) == null); + + // With multiple virtual placements, the zero-ID fallback picks + // deterministically by PlacementId.preferredOver: lowest external. + try s.addPlacement(io, alloc, t.screens.active, 1, 3, .{ + .location = .{ .virtual = {} }, + }); + { + const expected: ImageStorage.PlacementKey = .{ + .image_id = 1, + .placement_id = .{ .tag = .external, .id = 3 }, + }; + try testing.expectEqual(expected, s.placeholderTarget(1, 0).?.key); + } +} + +test "storage: animation tick advances and schedules" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + + // A running 1x1 RGBA image whose animation has one extra frame. + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 1, + .height = 1, + .format = .rgba, + .data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) }, + }); + const img = s.images.getPtr(1).?; + const anim = try alloc.create(animation.Animation); + anim.* = .{ .state = .running }; + img.animation = anim; + try anim.frames.append(alloc, .{ + .data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }), + .gap_ms = 40, + }); + + // Without a placement the animation doesn't advance (our + // approximation of Kitty's visibility gate). + try testing.expect(s.animationTick(io, 0) == null); + try testing.expectEqual(@as(u32, 0), anim.current_index); + + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) }, + }); + const gen1 = img.generation; + s.dirty = false; + + // First tick: the gapless root frame is due immediately and is + // skipped over to frame 2, which is due again in its 40ms gap. + try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 0)); + try testing.expectEqual(@as(u32, 1), anim.current_index); + try testing.expect(img.generation > gen1); + try testing.expect(s.dirty); + + // Nothing due yet: no advance, and the delay counts down. + const gen2 = img.generation; + try testing.expectEqual(@as(?u64, 30), s.animationTick(io, 10)); + try testing.expectEqual(gen2, img.generation); + + // Wrapping is fine with an infinite loop budget: the gapless + // root is skipped and frame 2 is shown again. + try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 40)); + try testing.expectEqual(@as(u32, 1), anim.current_index); + try testing.expectEqual(@as(u32, 1), anim.current_loop); +} + +test "storage: animation tick loading state parks on last frame" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + + // A placed 1x1 RGBA image in the loading state (a=a s=2) whose + // animation has one extra frame. + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 1, + .height = 1, + .format = .rgba, + .data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) }, + }); + const anim = try alloc.create(animation.Animation); + anim.* = .{ .state = .loading }; + s.images.getPtr(1).?.animation = anim; + try anim.frames.append(alloc, .{ + .data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }), + .gap_ms = 40, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) }, + }); + + // Reach the last frame, then park: no wakeup is scheduled while + // waiting for more frames and the loop counter stays untouched. + try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 0)); + try testing.expectEqual(@as(u32, 1), anim.current_index); + try testing.expect(s.animationTick(io, 100) == null); + try testing.expectEqual(@as(u32, 1), anim.current_index); + try testing.expectEqual(@as(u32, 0), anim.current_loop); + + // A new frame arriving un-parks playback. + try anim.frames.append(alloc, .{ + .data = try alloc.dupe(u8, &.{ 0, 255, 0, 255 }), + .gap_ms = 25, + }); + try testing.expectEqual(@as(?u64, 25), s.animationTick(io, 150)); + try testing.expectEqual(@as(u32, 2), anim.current_index); +} + +test "storage: animation tick exhausts loop budget" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + + // A placed, running 1x1 RGBA image with a gapped root frame, one + // extra frame, and a one-loop budget (a=a v=2). + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 1, + .height = 1, + .format = .rgba, + .data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) }, + }); + const anim = try alloc.create(animation.Animation); + anim.* = .{ + .state = .running, + .root_gap_ms = 10, + .max_loops = 1, + }; + s.images.getPtr(1).?.animation = anim; + try anim.frames.append(alloc, .{ + .data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }), + .gap_ms = 40, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) }, + }); + + // Root shows for 10ms, frame 2 for 40ms, then the wrap exhausts + // the budget and playback freezes on the last frame for good. + try testing.expectEqual(@as(?u64, 10), s.animationTick(io, 0)); + try testing.expectEqual(@as(u32, 0), anim.current_index); + try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 10)); + try testing.expectEqual(@as(u32, 1), anim.current_index); + try testing.expect(s.animationTick(io, 50) == null); + try testing.expectEqual(@as(u32, 1), anim.current_index); + try testing.expect(s.animationTick(io, 500) == null); +} + +test "storage: animation tick ignores ineligible animations" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + + // A placed 1x1 RGBA image with one extra frame, in the default + // stopped state. + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 1, + .height = 1, + .format = .rgba, + .data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) }, + }); + const anim = try alloc.create(animation.Animation); + anim.* = .{}; + s.images.getPtr(1).?.animation = anim; + try anim.frames.append(alloc, .{ + .data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }), + .gap_ms = 40, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) }, + }); + + // Stopped (the default) never advances. + try testing.expect(s.animationTick(io, 0) == null); + + // An all-gapless animation can never advance either. + anim.state = .running; + anim.frames.items[0].gap_ms = 0; + try testing.expect(s.animationTick(io, 0) == null); + try testing.expectEqual(@as(u32, 0), anim.current_index); +} + +test "storage: animation tick re-anchors a restarted clock" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 10, .rows = 10 }); + defer t.deinit(alloc); + t.width_px = 100; + t.height_px = 100; + + var s: ImageStorage = .{}; + defer s.deinit(alloc, t.screens.active); + + // A placed, running 1x1 RGBA image displaying its extra frame, + // with a shown-at timestamp far ahead of the tick clock. + try s.addImage(io, alloc, t.screens.active, .{ + .id = 1, + .width = 1, + .height = 1, + .format = .rgba, + .data = .{ .complete = try alloc.dupe(u8, &.{ 255, 0, 0, 255 }) }, + }); + const anim = try alloc.create(animation.Animation); + anim.* = .{ + .state = .running, + .current_index = 1, + .frame_shown_at_ms = 1000, + }; + s.images.getPtr(1).?.animation = anim; + try anim.frames.append(alloc, .{ + .data = try alloc.dupe(u8, &.{ 0, 0, 255, 255 }), + .gap_ms = 40, + }); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ + .location = .{ .pin = try trackPin(&t, .{ .x = 0, .y = 0 }) }, + }); + + // A timestamp in the future relative to now means the caller's + // clock restarted; the animation must not stall until the old + // timestamp comes around again. + try testing.expectEqual(@as(?u64, 40), s.animationTick(io, 5)); + try testing.expectEqual(@as(?u64, 5), anim.frame_shown_at_ms); } diff --git a/vendor/libghostty-vt/src/terminal/kitty/graphics_unicode.zig b/vendor/libghostty-vt/src/terminal/kitty/graphics_unicode.zig index a223797b..b62f8177 100644 --- a/vendor/libghostty-vt/src/terminal/kitty/graphics_unicode.zig +++ b/vendor/libghostty-vt/src/terminal/kitty/graphics_unicode.zig @@ -363,29 +363,17 @@ pub const Placement = struct { image: *const Image, cell_width: u32, cell_height: u32, - ) !struct { + ) Error!struct { rows: u32, columns: u32, } { // Get the placement. If an ID is specified we look for the exact one. // If no ID, then we find the first virtual placement for this image. - const placement = if (self.placement_id > 0) storage.placements.get(.{ - .image_id = self.image_id, - .placement_id = .{ .tag = .external, .id = self.placement_id }, - }) orelse { - return Error.PlacementMissingPlacement; - } else placement: { - var it = storage.placements.iterator(); - while (it.next()) |entry| { - if (entry.key_ptr.image_id == self.image_id and - entry.value_ptr.location == .virtual) - { - break :placement entry.value_ptr.*; - } - } - - return Error.PlacementMissingPlacement; - }; + const target = storage.placeholderTarget( + self.image_id, + self.placement_id, + ) orelse return Error.PlacementMissingPlacement; + const placement = target.placement; // Use requested rows/columns if specified // For unspecified rows/columns, calculate based on the image size. @@ -885,7 +873,8 @@ test "unicode diacritic" { test "unicode placement: none" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -900,7 +889,8 @@ test "unicode placement: none" { test "unicode placement: single row/col" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -924,7 +914,8 @@ test "unicode placement: single row/col" { test "unicode placement: continuation break" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 10 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 10 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -958,7 +949,8 @@ test "unicode placement: continuation break" { test "unicode placement: continuation with diacritics set" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 10 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 10 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -985,7 +977,8 @@ test "unicode placement: continuation with diacritics set" { test "unicode placement: continuation with no col" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 10 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 10 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -1012,7 +1005,8 @@ test "unicode placement: continuation with no col" { test "unicode placement: continuation with no diacritics" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 10 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 10 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -1039,7 +1033,8 @@ test "unicode placement: continuation with no diacritics" { test "unicode placement: run ending" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 10 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 10 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -1066,7 +1061,8 @@ test "unicode placement: run ending" { test "unicode placement: run starting in the middle" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 10 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 10 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -1093,7 +1089,8 @@ test "unicode placement: run starting in the middle" { test "unicode placement: specifying image id as palette" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -1118,7 +1115,8 @@ test "unicode placement: specifying image id as palette" { test "unicode placement: specifying image id with high bits" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -1143,7 +1141,8 @@ test "unicode placement: specifying image id with high bits" { test "unicode placement: specifying placement id as palette" { const alloc = testing.allocator; - var t = try terminal.Terminal.init(alloc, .{ .rows = 5, .cols = 5 }); + const io = testing.io; + var t = try terminal.Terminal.init(io, alloc, .{ .rows = 5, .cols = 5 }); defer t.deinit(alloc); t.modes.set(.grapheme_cluster, true); @@ -1174,17 +1173,18 @@ test "unicode placement: specifying placement id as palette" { // printf "\033_Ga=p,i=1,U=1,q=2,c=4,r=2\033\\" test "unicode render placement: dog 4x2" { const alloc = testing.allocator; + const io = testing.io; const cell_width = 36; const cell_height = 80; - var t = try terminal.Terminal.init(alloc, .{ .cols = 100, .rows = 100 }); + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 100, .rows = 100 }); defer t.deinit(alloc); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); const image: Image = .{ .id = 1, .width = 500, .height = 306 }; - try s.addImage(alloc, image); - try s.addPlacement(alloc, 1, 0, .{ + try s.addImage(io, alloc, t.screens.active, image); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ .location = .{ .virtual = {} }, .columns = 4, .rows = 2, @@ -1241,17 +1241,18 @@ test "unicode render placement: dog 4x2" { // printf "\033_Ga=p,i=1,U=1,q=2,c=2,r=2\033\\" test "unicode render placement: dog 2x2 with blank cells" { const alloc = testing.allocator; + const io = testing.io; const cell_width = 36; const cell_height = 80; - var t = try terminal.Terminal.init(alloc, .{ .cols = 100, .rows = 100 }); + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 100, .rows = 100 }); defer t.deinit(alloc); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); const image: Image = .{ .id = 1, .width = 500, .height = 306 }; - try s.addImage(alloc, image); - try s.addPlacement(alloc, 1, 0, .{ + try s.addImage(io, alloc, t.screens.active, image); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ .location = .{ .virtual = {} }, .columns = 2, .rows = 2, @@ -1307,17 +1308,18 @@ test "unicode render placement: dog 2x2 with blank cells" { // printf "\033_Ga=p,i=1,U=1,q=2,c=1,r=1\033\\" test "unicode render placement: dog 1x1" { const alloc = testing.allocator; + const io = testing.io; const cell_width = 36; const cell_height = 80; - var t = try terminal.Terminal.init(alloc, .{ .cols = 100, .rows = 100 }); + var t = try terminal.Terminal.init(io, alloc, .{ .cols = 100, .rows = 100 }); defer t.deinit(alloc); var s: ImageStorage = .{}; defer s.deinit(alloc, t.screens.active); const image: Image = .{ .id = 1, .width = 500, .height = 306 }; - try s.addImage(alloc, image); - try s.addPlacement(alloc, 1, 0, .{ + try s.addImage(io, alloc, t.screens.active, image); + try s.addPlacement(io, alloc, t.screens.active, 1, 0, .{ .location = .{ .virtual = {} }, .columns = 1, .rows = 1, diff --git a/vendor/libghostty-vt/src/terminal/kitty/windows.zig b/vendor/libghostty-vt/src/terminal/kitty/windows.zig new file mode 100644 index 00000000..41d2b62d --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/kitty/windows.zig @@ -0,0 +1,277 @@ +//! Windows path checks for the Kitty graphics protocol file and +//! temporary file mediums. +//! +//! On POSIX the file transmission blocklist is a few prefixes +//! (`/proc`, `/sys`, `/dev`) applied to the canonical path of the +//! opened file. Windows needs more care because some paths are +//! dangerous to even open: a UNC path makes this process resolve and +//! authenticate to the named SMB server, and the Win32 device and NT +//! object namespaces reach raw volumes and named pipes, where the +//! open or the read can block. +//! +//! Everything here is pure string logic, so it is tested on every host. + +const std = @import("std"); + +pub const Error = error{ + /// A UNC path such as `\\server\share`, in any mix of `\` and `/`. + /// Opening it resolves the host name and authenticates to the + /// server over SMB. + UncPath, + + /// A Win32 device path (`\\.\`, `\\?\`) or an NT object path + /// (`\??\`). These reach raw volumes, named pipes, and through + /// `GLOBALROOT` any kernel object. + DevicePath, + + /// A component names a reserved DOS device such as CON or COM1. + /// Windows resolves these to the device from inside any directory. + ReservedDeviceName, + + /// The canonical path of an opened file is not `X:\...`, so the + /// file is not on a local volume with a drive letter. The canonical + /// path of a file on an SMB share is a UNC path, for example. + NotDriveAbsolute, +}; + +/// Check a path supplied by a client before it is opened. This never +/// touches the filesystem. A plain local path of any shape passes: +/// drive-absolute, drive-relative, rooted, or relative. Whether it +/// resolves to a local regular file is decided after the open with +/// `checkCanonicalPath`. +pub fn checkPath(path: []const u8) Error!void { + if (path.len >= 2 and isSep(path[0]) and isSep(path[1])) { + // `\\?\` and `\\.\` are the Win32 device namespace. Anything + // else after two separators is a server name. + if (path.len >= 4 and + (path[2] == '?' or path[2] == '.') and + isSep(path[3])) return error.DevicePath; + return error.UncPath; + } + + if (path.len >= 4 and + isSep(path[0]) and + path[1] == '?' and + path[2] == '?' and + isSep(path[3])) return error.DevicePath; + + var it = std.mem.splitAny(u8, path, "\\/"); + while (it.next()) |component| { + if (isReservedDeviceName(component)) return error.ReservedDeviceName; + } +} + +/// Check the canonical path of an opened file, as produced by +/// `GetFinalPathNameByHandle`. The file must be on a local volume with +/// a drive letter, and the checks of `checkPath` apply as well. +pub fn checkCanonicalPath(path: []const u8) Error!void { + if (!isDriveAbsolute(path)) return error.NotDriveAbsolute; + try checkPath(path); +} + +/// Returns true if `path` is a drive-absolute path (`C:\...`), the +/// only shape the canonical path of a file on a local volume with a +/// drive letter takes. +pub fn isDriveAbsolute(path: []const u8) bool { + return path.len >= 3 and + std.ascii.isAlphabetic(path[0]) and + path[1] == ':' and + path[2] == '\\'; +} + +/// Returns true if `c` is a Windows path separator. +fn isSep(c: u8) bool { + return std.fs.path.PathType.windows.isSep(u8, c); +} + +/// Returns true if a path component names a reserved DOS device: CON, +/// PRN, AUX, NUL, COM0 to COM9, LPT0 to LPT9, and the superscript digit +/// variants. +fn isReservedDeviceName(component: []const u8) bool { + // The name ends at the extension or stream separator. + const end = std.mem.findAny(u8, component, ".:") orelse component.len; + const stem = std.mem.trimEnd(u8, component[0..end], " "); + if (stem.len < 3) return false; + + const base = stem[0..3]; + const rest = stem[3..]; + if (rest.len == 0) { + return std.ascii.eqlIgnoreCase(base, "CON") or + std.ascii.eqlIgnoreCase(base, "PRN") or + std.ascii.eqlIgnoreCase(base, "AUX") or + std.ascii.eqlIgnoreCase(base, "NUL"); + } + + if (!std.ascii.eqlIgnoreCase(base, "COM") and + !std.ascii.eqlIgnoreCase(base, "LPT")) return false; + return switch (rest.len) { + 1 => std.ascii.isDigit(rest[0]), + // U+00B9, U+00B2, U+00B3 (¹ ² ³) encoded as WTF-8. + 2 => rest[0] == 0xC2 and + (rest[1] == 0xB9 or rest[1] == 0xB2 or rest[1] == 0xB3), + else => false, + }; +} + +test "checkPath rejects UNC paths" { + const testing = std.testing; + + const unc = [_][]const u8{ + "\\\\server\\share\\tty-graphics-protocol-image.data", + "//server/share/tty-graphics-protocol-image.data", + "\\/server/share/image.data", + "/\\server\\share\\image.data", + "\\\\.x\\share\\image.data", + "\\\\", + }; + for (unc) |path| { + try testing.expectError(error.UncPath, checkPath(path)); + } +} + +test "checkPath rejects device and NT namespaces" { + const testing = std.testing; + + const device = [_][]const u8{ + "\\\\?\\UNC\\server\\share\\image.data", + "\\\\?\\C:\\Windows\\Temp\\image.data", + "//?/C:/Windows/Temp/image.data", + "\\\\.\\C:\\Windows\\Temp\\image.data", + "\\\\.\\pipe\\image", + "\\\\.\\PhysicalDrive0", + "\\\\?\\GLOBALROOT\\Device\\HarddiskVolume1\\image.data", + "//?/GLOBALROOT/Device/HarddiskVolume1/image.data", + "\\??\\C:\\Windows\\Temp\\image.data", + "\\??\\Device\\HarddiskVolume1\\image.data", + "\\??\\UNC\\server\\share\\image.data", + "/??/C:/image.data", + }; + for (device) |path| { + try testing.expectError(error.DevicePath, checkPath(path)); + } +} + +test "checkPath accepts plain local paths" { + const accepted = [_][]const u8{ + "C:\\Windows\\Temp\\tty-graphics-protocol-image.data", + "C:/Windows/Temp/tty-graphics-protocol-image.data", + "c:\\image.data", + "C:image.data", + "\\Windows\\Temp\\image.data", + "/Windows/Temp/image.data", + "image.data", + ".zig-cache\\tmp\\image.data", + "C:\\Device\\HarddiskVolume1\\image.data", + "C:\\GLOBALROOT\\image.data", + "C:\\dir\\console.png", + "\\?\\image.data", + "", + }; + for (accepted) |path| { + try checkPath(path); + } +} + +test "checkPath rejects reserved device names in any component" { + const testing = std.testing; + + const reserved = [_][]const u8{ + "CON", + "con", + "CON.png", + "CON.tar.gz", + "CON.", + "CON ", + "CON .png", + "NUL:stream", + "PRN", + "AUX", + "nul", + "COM1", + "COM0", + "COM9.rgba", + "com5", + "LPT1", + "LPT9", + "lpt0.png", + "COM\xC2\xB9", + "LPT\xC2\xB3.png", + "C:\\Windows\\Temp\\NUL.rgba", + "C:\\CON\\image.data", + "dir/aux/image.data", + "C:/Windows/Temp/com1", + "..\\PRN", + }; + for (reserved) |path| { + try testing.expectError(error.ReservedDeviceName, checkPath(path)); + } + + const plain = [_][]const u8{ + "CONSOLE", + "CON1", + "xCON", + " CON", + "COM", + "COM10", + "COMA", + "COM\xC2\xB9\xC2\xB9", + "COM\xC2\xB0", + "LPT", + "LPT.png", + "conx.png", + "C:", + "C:\\", + "image.data", + "C:\\Windows\\Temp\\tty-graphics-protocol-image.data", + "C:\\Windows\\Temp\\console.png", + "", + }; + for (plain) |path| { + try checkPath(path); + } +} + +test "checkCanonicalPath requires a drive-absolute local path" { + const testing = std.testing; + + try checkCanonicalPath("C:\\Windows\\Temp\\image.data"); + try checkCanonicalPath("c:\\image.data"); + + // Shapes GetFinalPathNameByHandle produces for files that are not + // on a local drive-letter volume. + try testing.expectError( + error.NotDriveAbsolute, + checkCanonicalPath("\\\\server\\share\\image.data"), + ); + try testing.expectError( + error.NotDriveAbsolute, + checkCanonicalPath("\\\\?\\UNC\\server\\share\\image.data"), + ); + try testing.expectError( + error.NotDriveAbsolute, + checkCanonicalPath("\\\\?\\Volume{383da0b0-717f-41b6-8c36-00500992b58d}\\image.data"), + ); + + // The raw path checks still apply to the canonical form. + try testing.expectError( + error.ReservedDeviceName, + checkCanonicalPath("C:\\Windows\\Temp\\CON"), + ); +} + +test isDriveAbsolute { + const testing = std.testing; + + try testing.expect(isDriveAbsolute("C:\\Windows\\Temp\\image.data")); + try testing.expect(isDriveAbsolute("c:\\image.data")); + try testing.expect(isDriveAbsolute("Z:\\")); + + try testing.expect(!isDriveAbsolute("\\\\server\\share\\image.data")); + try testing.expect(!isDriveAbsolute("UNC\\server\\share\\image.data")); + try testing.expect(!isDriveAbsolute("C:/Windows/image.data")); + try testing.expect(!isDriveAbsolute("C:image.data")); + try testing.expect(!isDriveAbsolute("\\Windows\\image.data")); + try testing.expect(!isDriveAbsolute("1:\\image.data")); + try testing.expect(!isDriveAbsolute("C:")); + try testing.expect(!isDriveAbsolute("")); +} diff --git a/vendor/libghostty-vt/src/terminal/lib.zig b/vendor/libghostty-vt/src/terminal/lib.zig index 921d831b..040d1c4c 100644 --- a/vendor/libghostty-vt/src/terminal/lib.zig +++ b/vendor/libghostty-vt/src/terminal/lib.zig @@ -14,6 +14,7 @@ pub const calling_conv: std.builtin.CallingConvention = .c; /// Forwarded decls from lib that are used. pub const alloc = lib.allocator; +pub const TinyIo = lib.TinyIo; pub const Buffer = lib.Buffer; pub const Enum = lib.Enum; pub const TaggedUnion = lib.TaggedUnion; diff --git a/vendor/libghostty-vt/src/terminal/main.zig b/vendor/libghostty-vt/src/terminal/main.zig index af277f97..5d7c7387 100644 --- a/vendor/libghostty-vt/src/terminal/main.zig +++ b/vendor/libghostty-vt/src/terminal/main.zig @@ -20,7 +20,9 @@ pub const kitty = @import("kitty.zig"); pub const modes = @import("modes.zig"); pub const page = @import("page.zig"); pub const parse_table = @import("parse_table.zig"); +pub const paste = @import("paste.zig"); pub const search = @import("search.zig"); +pub const snapshot = @import("snapshot/main.zig"); pub const sgr = @import("sgr.zig"); pub const size = @import("size.zig"); pub const size_report = @import("size_report.zig"); @@ -58,6 +60,12 @@ pub const Terminal = @import("Terminal.zig"); pub const TerminalStream = stream_terminal.Stream; pub const Stream = stream.Stream; pub const StreamAction = stream.Action; +pub const UnknownSequence = stream_terminal.Handler.UnknownSequence; +pub const Paste = paste.Request; +pub const PasteContents = paste.Contents; +pub const PasteSource = paste.Source; +pub const MimeReader = clipboard.MimeReader; +pub const PasteError = stream_terminal.Handler.PasteError; pub const Cursor = Screen.Cursor; pub const CursorStyle = Screen.CursorStyle; pub const CursorStyleReq = ansi.CursorStyle; @@ -88,6 +96,7 @@ test { // Internals _ = @import("bitmap_allocator.zig"); _ = @import("compress.zig"); + _ = @import("fraction.zig"); _ = @import("hash_map.zig"); _ = @import("mem.zig"); _ = @import("ref_counted_set.zig"); diff --git a/vendor/libghostty-vt/src/terminal/mem.zig b/vendor/libghostty-vt/src/terminal/mem.zig index 7fa31480..f1c018f8 100644 --- a/vendor/libghostty-vt/src/terminal/mem.zig +++ b/vendor/libghostty-vt/src/terminal/mem.zig @@ -4,9 +4,15 @@ //! discard the physical pages behind one of those mappings without releasing //! its virtual address range, then prepare the same range for reuse. It does //! not allocate memory or decide which terminal pages should be discarded. +//! +//! Decommit releases physical pages only. The address range and its memory +//! accounting (the Linux VMA, the Windows commit charge) stay with the +//! process, so a read after decommit returns zeros or the old contents rather +//! than faulting, and recommit has nothing to acquire that could fail. const std = @import("std"); const builtin = @import("builtin"); const assert = @import("../quirks.zig").inlineAssert; +const windows = @import("../os/windows.zig"); const log = std.log.scoped(.terminal_mem); @@ -26,9 +32,9 @@ pub const DecommitMode = enum { /// /// Test builds support both modes because `decommit` simulates reclamation by /// clearing the supplied range. Runtime reclamation is intentionally limited -/// to 64-bit Linux and Darwin. Other targets must leave strict callers' memory -/// resident; zero mode still provides its documented memset fallback through -/// `decommit` even when this function returns false. +/// to 64-bit Linux, Darwin, and Windows. Other targets must leave strict +/// callers' memory resident; zero mode still provides its documented memset +/// fallback through `decommit` even when this function returns false. pub inline fn canReclaim(comptime mode: DecommitMode) bool { // Both modes use the same retained-mapping primitives. Keeping the switch // exhaustive makes additions to DecommitMode choose target support @@ -58,6 +64,13 @@ pub inline fn canReclaim(comptime mode: DecommitMode) bool { // dependency to libghostty-vt. if (builtin.target.os.tag.isDarwin()) break :supported true; + // Windows provides DiscardVirtualMemory, which releases the + // physical pages behind a committed range while keeping it + // committed, so nothing has to be committed again before reuse. + // Page memory is already a VirtualAlloc region (see page.zig) + // and kernel32 is linked by every Windows build. + if (builtin.target.os.tag == .windows) break :supported true; + // Other targets have no retained-mapping reclamation contract in // this module. Zero mode can still clear through its memset // fallback, but strict callers must leave their mapping resident. @@ -144,16 +157,41 @@ pub fn decommit( } } + // DiscardVirtualMemory releases the physical pages behind the range but + // leaves it committed, so the commit charge stays with the process and a + // later access finds a zero page or the old contents instead of faulting. + // Zero mode clears its dirty prefix first, as on Darwin: the bytes read + // as zero afterward whether or not the discard took. Strict mode skips + // that write because its caller replaces the entire mapping after + // recommit. The call reports failure through its return value rather + // than the thread's last error. + if (comptime builtin.os.tag == .windows) { + if (comptime mode == .zero) @memset(memory[0..dirty_len], 0); + + const rc = windows.exp.kernel32.DiscardVirtualMemory( + memory.ptr, + memory.len, + ); + if (rc == windows.ERROR_SUCCESS) return true; + + // Zero mode has already cleared its bytes and strict callers must + // leave the still-resident mapping alone, so there is nothing more + // to do for either mode. + log.warn("DiscardVirtualMemory failed err={d}", .{rc}); + return false; + } + if (comptime mode == .zero) @memset(memory[0..dirty_len], 0); return false; } /// Prepare a mapping previously passed to decommit for reuse. /// -/// Linux and test builds need no explicit operation. Darwin pairs -/// FREE_REUSABLE with FREE_REUSE so pages touched by the caller are accounted -/// to the process again. Failure does not invalidate the retained mapping, so -/// reuse can continue after logging the accounting failure. +/// Linux, Windows, and test builds need no explicit operation because their +/// mappings stay committed through decommit. Darwin pairs FREE_REUSABLE with +/// FREE_REUSE so pages touched by the caller are accounted to the process +/// again. Failure does not invalidate the retained mapping, so reuse can +/// continue after logging the accounting failure. pub fn recommit(memory: []align(std.heap.page_size_min) u8) void { assert(memory.len > 0); assert(@intFromPtr(memory.ptr) % std.heap.page_size_min == 0); diff --git a/vendor/libghostty-vt/src/terminal/modes.zig b/vendor/libghostty-vt/src/terminal/modes.zig index c92f97b0..ed5f1b72 100644 --- a/vendor/libghostty-vt/src/terminal/modes.zig +++ b/vendor/libghostty-vt/src/terminal/modes.zig @@ -8,6 +8,8 @@ //! to ensure all our various types and logic remain in sync. const std = @import("std"); +const builtin = @import("builtin"); +const build_options = @import("terminal_options"); const testing = std.testing; /// A struct that maintains the state of all the settable modes. @@ -34,22 +36,18 @@ pub const ModeState = struct { /// Set a mode to a value. pub fn set(self: *ModeState, mode: Mode, value: bool) void { - switch (mode) { - inline else => |mode_comptime| { - const entry = comptime entryForMode(mode_comptime); - @field(self.values, entry.name) = value; - }, - } + setPacked(&self.values, mode, value); + } + + /// Set the reset default and current value for a mode. + pub fn setDefault(self: *ModeState, mode: Mode, value: bool) void { + setPacked(&self.values, mode, value); + setPacked(&self.default, mode, value); } /// Get the value of a mode. pub fn get(self: *const ModeState, mode: Mode) bool { - switch (mode) { - inline else => |mode_comptime| { - const entry = comptime entryForMode(mode_comptime); - return @field(self.values, entry.name); - }, - } + return getPacked(&self.values, mode); } /// Save the state of the given mode. This can then be restored @@ -79,6 +77,19 @@ pub const ModeState = struct { /// Return a DECRPM report for the given mode tag. If the tag does /// not correspond to a known mode, the report state is .not_recognized. pub fn getReport(self: *const ModeState, tag: ModeTag) Report { + // DECECM (Erase Color Mode, DEC private mode 117) controls whether erasing + // and scrolling use the default background or the active background color. + // Ghostty's behavior is fixed equivalent to DECECM reset, and DECRQM has a + // "permanently reset" response for recognized modes that cannot be changed. + // Report that instead of "not recognized" so applications can query and adapt + // to Ghostty's erase-color behavior. + // + // See VT520/VT525 Programmer Information, "Erase Color" and DECRQM/DECRPM: + // https://web.mit.edu/dosathena/doc/www/ek-vt520-rm.pdf + + if (!tag.ansi and tag.value == 117) { + return .{ .tag = tag, .state = .permanently_reset }; + } const mode = modeFromInt(tag.value, tag.ansi) orelse return .{ .tag = tag, .state = .not_recognized, @@ -97,49 +108,53 @@ pub const ModeState = struct { } }; +fn setPacked(values: *ModePacked, mode: Mode, value: bool) void { + switch (mode) { + inline else => |mode_comptime| { + const entry = comptime entryForMode(mode_comptime); + @field(values, entry.name) = value; + }, + } +} + +fn getPacked(values: *const ModePacked, mode: Mode) bool { + switch (mode) { + inline else => |mode_comptime| { + const entry = comptime entryForMode(mode_comptime); + return @field(values, entry.name); + }, + } +} + /// A packed struct of all the settable modes. This shouldn't /// be used directly but rather through the ModeState struct. pub const ModePacked = packed_struct: { const StructField = std.builtin.Type.StructField; - var fields: [entries.len]StructField = undefined; - for (entries, 0..) |entry, i| { - fields[i] = .{ - .name = entry.name, - .type = bool, - .default_value_ptr = &entry.default, - .is_comptime = false, - .alignment = 0, - }; + var names: [entries.len][]const u8 = undefined; + var types = [_]type{bool} ** entries.len; + var attrs: [entries.len]StructField.Attributes = undefined; + + for (entries, &names, &attrs) |entry, *name, *attr| { + name.* = entry.name; + attr.* = .{ .default_value_ptr = &entry.default }; } - break :packed_struct @Type(.{ .@"struct" = .{ - .layout = .@"packed", - .fields = &fields, - .decls = &.{}, - .is_tuple = false, - } }); + break :packed_struct @Struct(.@"packed", null, &names, &types, &attrs); }; /// An enum(u16) of the available modes. See entries for available values. pub const Mode = mode_enum: { - const EnumField = std.builtin.Type.EnumField; - var fields: [entries.len]EnumField = undefined; - for (entries, 0..) |entry, i| { - fields[i] = .{ - .name = entry.name, - .value = @as(ModeTag.Backing, @bitCast(ModeTag{ - .value = entry.value, - .ansi = entry.ansi, - })), - }; + var names: [entries.len][]const u8 = undefined; + var values: [entries.len]ModeTag.Backing = undefined; + for (entries, &names, &values) |entry, *name, *value| { + name.* = entry.name; + value.* = @bitCast(ModeTag{ + .value = entry.value, + .ansi = entry.ansi, + }); } - break :mode_enum @Type(.{ .@"enum" = .{ - .tag_type = ModeTag.Backing, - .fields = &fields, - .decls = &.{}, - .is_exhaustive = true, - } }); + break :mode_enum @Enum(ModeTag.Backing, .exhaustive, &names, &values); }; /// The tag type for our enum is a u16 but we use a packed struct @@ -240,8 +255,24 @@ const ModeEntry = struct { /// set or queried. The mode enum still has it, allowing Ghostty developers /// to develop a mode without exposing it to real users. disabled: bool = false, + + /// Whether an embedder may safely configure this bit as reset policy. + /// Modes that perform a transition or mirror additional terminal state + /// must use their semantic configuration API instead. + default_configurable: bool = true, }; +/// Return whether a mode can safely be configured as a reset default by an +/// embedder. This excludes modes whose set/reset operations have side effects +/// beyond changing the mode bit. +pub fn defaultConfigurable(mode: Mode) bool { + switch (mode) { + inline else => |mode_comptime| { + return comptime entryForMode(mode_comptime).default_configurable; + }, + } +} + /// The full list of available entries. For documentation see how /// they're used within Ghostty or google their values. It is not /// valuable to redocument them all here. @@ -257,46 +288,58 @@ const entries: []const ModeEntry = &.{ // DEC .{ .name = "cursor_keys", .value = 1 }, // DECCKM - .{ .name = "132_column", .value = 3 }, + .{ .name = "132_column", .value = 3, .default_configurable = false }, .{ .name = "slow_scroll", .value = 4 }, .{ .name = "reverse_colors", .value = 5 }, - .{ .name = "origin", .value = 6 }, + .{ .name = "origin", .value = 6, .default_configurable = false }, .{ .name = "wraparound", .value = 7, .default = true }, .{ .name = "autorepeat", .value = 8 }, - .{ .name = "mouse_event_x10", .value = 9 }, - .{ .name = "cursor_blinking", .value = 12 }, + .{ .name = "mouse_event_x10", .value = 9, .default_configurable = false }, + .{ .name = "cursor_blinking", .value = 12, .default_configurable = false }, .{ .name = "cursor_visible", .value = 25, .default = true }, .{ .name = "enable_mode_3", .value = 40 }, .{ .name = "reverse_wrap", .value = 45 }, - .{ .name = "alt_screen_legacy", .value = 47 }, + .{ .name = "alt_screen_legacy", .value = 47, .default_configurable = false }, .{ .name = "keypad_keys", .value = 66 }, // DEC Backarrow Key Mode (DECBKM) // See https://vt100.net/dec/ek-vt3xx-tp-002.pdf page 170 // If `false` (the default), `backspace` emits 0x7f // If `true`, `backspace` emits 0x08 .{ .name = "backarrow_key_mode", .value = 67 }, - .{ .name = "enable_left_and_right_margin", .value = 69 }, - .{ .name = "mouse_event_normal", .value = 1000 }, - .{ .name = "mouse_event_button", .value = 1002 }, - .{ .name = "mouse_event_any", .value = 1003 }, + .{ .name = "enable_left_and_right_margin", .value = 69, .default_configurable = false }, + .{ .name = "mouse_event_normal", .value = 1000, .default_configurable = false }, + .{ .name = "mouse_event_button", .value = 1002, .default_configurable = false }, + .{ .name = "mouse_event_any", .value = 1003, .default_configurable = false }, .{ .name = "focus_event", .value = 1004 }, - .{ .name = "mouse_format_utf8", .value = 1005 }, - .{ .name = "mouse_format_sgr", .value = 1006 }, + .{ .name = "mouse_format_utf8", .value = 1005, .default_configurable = false }, + .{ .name = "mouse_format_sgr", .value = 1006, .default_configurable = false }, .{ .name = "mouse_alternate_scroll", .value = 1007, .default = true }, - .{ .name = "mouse_format_urxvt", .value = 1015 }, - .{ .name = "mouse_format_sgr_pixels", .value = 1016 }, + .{ .name = "mouse_format_urxvt", .value = 1015, .default_configurable = false }, + .{ .name = "mouse_format_sgr_pixels", .value = 1016, .default_configurable = false }, .{ .name = "ignore_keypad_with_numlock", .value = 1035, .default = true }, .{ .name = "alt_esc_prefix", .value = 1036, .default = true }, .{ .name = "alt_sends_escape", .value = 1039 }, .{ .name = "reverse_wrap_extended", .value = 1045 }, - .{ .name = "alt_screen", .value = 1047 }, - .{ .name = "save_cursor", .value = 1048 }, - .{ .name = "alt_screen_save_cursor_clear_enter", .value = 1049 }, + .{ .name = "alt_screen", .value = 1047, .default_configurable = false }, + .{ .name = "save_cursor", .value = 1048, .default_configurable = false }, + .{ .name = "alt_screen_save_cursor_clear_enter", .value = 1049, .default_configurable = false }, .{ .name = "bracketed_paste", .value = 2004 }, - .{ .name = "synchronized_output", .value = 2026 }, + .{ .name = "synchronized_output", .value = 2026, .default_configurable = false }, .{ .name = "grapheme_cluster", .value = 2027 }, .{ .name = "report_color_scheme", .value = 2031 }, + .{ .name = "report_visibility", .value = 2033, .default_configurable = false }, .{ .name = "in_band_size_reports", .value = 2048 }, + // Kitty clipboard protocol paste events. When set, a user-initiated + // paste sends an unsolicited OSC 5522 targets listing with a + // one-time password instead of pasting the text. + // See https://sw.kovidgoyal.net/kitty/clipboard/ + .{ + .name = "kitty_paste_events", + .value = 5522, + // The macOS app and libghostty-vt can both serve the follow-up + // Kitty clipboard read that a paste event grants. + .disabled = build_options.artifact != .lib and builtin.os.tag != .macos, + }, }; test { @@ -327,6 +370,32 @@ test ModeState { try testing.expect(state.get(.cursor_keys)); } +test "ModeState set default updates current and reset value" { + var state: ModeState = .{}; + + state.setDefault(.grapheme_cluster, true); + try testing.expect(state.get(.grapheme_cluster)); + try testing.expect(state.default.grapheme_cluster); + + state.set(.grapheme_cluster, false); + try testing.expect(!state.get(.grapheme_cluster)); + try testing.expect(state.default.grapheme_cluster); + + state.setDefault(.grapheme_cluster, true); + try testing.expect(state.get(.grapheme_cluster)); + + state.set(.grapheme_cluster, false); + state.reset(); + try testing.expect(state.get(.grapheme_cluster)); +} + +test "default configurable modes" { + try testing.expect(defaultConfigurable(.grapheme_cluster)); + try testing.expect(defaultConfigurable(.wraparound)); + try testing.expect(!defaultConfigurable(.alt_screen)); + try testing.expect(!defaultConfigurable(.cursor_blinking)); +} + test "getReport known DEC mode" { var state: ModeState = .{}; const report = state.getReport(.{ .value = 1 }); @@ -347,6 +416,13 @@ test "getReport known ANSI mode" { try testing.expectEqual(true, report.tag.ansi); } +test "getReport DECECM permanently reset" { + const state: ModeState = .{}; + const report = state.getReport(.{ .value = 117, .ansi = false }); + try testing.expectEqual(Report.State.permanently_reset, report.state); + try testing.expectEqual(false, report.tag.ansi); +} + test "getReport unknown mode" { const state: ModeState = .{}; const report = state.getReport(.{ .value = 9999 }); diff --git a/vendor/libghostty-vt/src/terminal/mouse.zig b/vendor/libghostty-vt/src/terminal/mouse.zig index 2861ad1e..2b0740a3 100644 --- a/vendor/libghostty-vt/src/terminal/mouse.zig +++ b/vendor/libghostty-vt/src/terminal/mouse.zig @@ -1,6 +1,7 @@ const std = @import("std"); const build_options = @import("terminal_options"); const lib = @import("lib.zig"); +const compat_testing = @import("../lib/compat/testing.zig"); /// The event types that can be reported for mouse-related activities. /// These are all mutually exclusive (hence in a single enum). @@ -164,5 +165,5 @@ test "cursor shape from string" { } test { - _ = std.testing.refAllDeclsRecursive(@This()); + _ = compat_testing.refAllDeclsRecursive(@This()); } diff --git a/vendor/libghostty-vt/src/terminal/osc.zig b/vendor/libghostty-vt/src/terminal/osc.zig index 26ce1267..dbc35492 100644 --- a/vendor/libghostty-vt/src/terminal/osc.zig +++ b/vendor/libghostty-vt/src/terminal/osc.zig @@ -46,12 +46,13 @@ pub const Command = union(Key) { /// Semantic prompt command: https://gitlab.freedesktop.org/Per_Bothner/specifications/blob/master/proposals/semantic-prompts.md semantic_prompt: SemanticPrompt, - /// Set or get clipboard contents. If data is null, then the current - /// clipboard contents are sent to the pty. If data is set, this - /// contents is set on the clipboard. + /// Set or get clipboard contents. If data is "?", then the current + /// clipboard contents are sent to the pty. Otherwise, the contents + /// are set on the clipboard. clipboard_contents: struct { kind: u8, data: [:0]const u8, + terminator: Terminator = .st, }, /// OSC 7. Reports the current working directory of the shell. This is @@ -163,12 +164,17 @@ pub const Command = union(Key) { /// https://uapi-group.org/specifications/specs/osc_context/ context_signal: parsers.context_signal.Command, + /// Kitty desktop notifications (OSC 99) + kitty_desktop_notification: KittyDesktopNotification, + pub const SemanticPrompt = parsers.semantic_prompt.Command; pub const KittyClipboardProtocol = parsers.kitty_clipboard_protocol.OSC; pub const KittyDndProtocol = parsers.kitty_dnd_protocol.OSC; + pub const KittyDesktopNotification = parsers.kitty_desktop_notification.OSC; + pub const Key = LibEnum( lib.target, // NOTE: Order matters, see LibEnum documentation. @@ -199,23 +205,21 @@ pub const Command = union(Key) { "kitty_clipboard_protocol", "kitty_dnd_protocol", "context_signal", + "kitty_desktop_notification", }, ); pub const ProgressReport = struct { - pub const State = enum(c_int) { - remove, - set, - @"error", - indeterminate, - pause, - - test "ghostty.h Command.ProgressReport.State" { - if (comptime build_options.artifact == .lib) return error.SkipZigTest; - try lib.checkGhosttyHEnum(State, "GHOSTTY_PROGRESS_STATE_"); - } + const state_keys = &.{ + "remove", + "set", + "error", + "indeterminate", + "pause", }; + pub const State = LibEnum(lib.target, state_keys); + state: State, progress: ?u8 = null, @@ -235,6 +239,12 @@ pub const Command = union(Key) { )) else -1, }; } + + test "ghostty.h Command.ProgressReport.State" { + if (comptime build_options.artifact == .lib) return error.SkipZigTest; + const CState = LibEnum(.c, state_keys); + try lib.checkGhosttyHEnum(CState, "GHOSTTY_PROGRESS_STATE_"); + } }; comptime { @@ -297,11 +307,20 @@ pub const Parser = struct { /// Maximum size of a "normal" OSC. pub const MAX_BUF = 2048; + /// Maximum size of an OSC that requires dynamically allocated storage. + /// OSC input is untrusted, so these captures must have a finite bound. + pub const MAX_ALLOCATING_BUF = 8 * 1024 * 1024; + /// Optional allocator used to accept data longer than MAX_BUF. /// This only applies to some commands (e.g. OSC 52) that can /// reasonably exceed MAX_BUF. alloc: ?Allocator, + /// Maximum number of bytes retained by an allocating capture. + /// This is configurable primarily so callers and tests can choose a + /// smaller policy than the default. + max_allocating_bytes: usize, + /// Current state of the parser. state: State, @@ -352,6 +371,7 @@ pub const Parser = struct { @"66", @"72", @"77", + @"99", @"104", @"110", @"111", @@ -373,6 +393,7 @@ pub const Parser = struct { pub fn init(alloc: ?Allocator) Parser { var result: Parser = .{ .alloc = alloc, + .max_allocating_bytes = MAX_ALLOCATING_BUF, .state = .start, .capture = null, .command = .invalid, @@ -431,6 +452,7 @@ pub const Parser = struct { .kitty_text_sizing, .kitty_clipboard_protocol, .kitty_dnd_protocol, + .kitty_desktop_notification, .context_signal, => {}, } @@ -458,6 +480,7 @@ pub const Parser = struct { const Capture = struct { writer: *std.Io.Writer, backing: Backing, + max_bytes: usize, const Backing = union(enum) { fixed: std.Io.Writer, @@ -473,22 +496,83 @@ pub const Parser = struct { new.* = .{ .backing = .{ .fixed = .fixed(buf) }, .writer = &new.*.?.backing.fixed, + .max_bytes = buf.len, }; } pub inline fn allocating( new: *?Capture, alloc: Allocator, + max_bytes: usize, ) error{OutOfMemory}!void { new.* = .{ .backing = .{ .allocating = try std.Io.Writer.Allocating.initCapacity( alloc, - 2048, + @min(MAX_BUF, max_bytes), ) }, .writer = &new.*.?.backing.allocating.writer, + .max_bytes = max_bytes, }; } + /// Append one byte without permitting the backing allocation to grow + /// beyond max_bytes. Allocating.Writer normally grows super-linearly, + /// so grow it explicitly to keep the allocation itself bounded too. + pub inline fn writeByte(self: *Capture, byte: u8) error{WriteFailed}!void { + if (self.writer.buffered().len >= self.max_bytes) return error.WriteFailed; + + switch (self.backing) { + .fixed => {}, + .allocating => |*w| { + if (w.writer.end >= w.writer.buffer.len) { + const new_capacity = @min( + self.max_bytes, + @max(w.writer.buffer.len *| 2, 1), + ); + w.writer.buffer = w.allocator.realloc( + w.writer.buffer, + new_capacity, + ) catch return error.WriteFailed; + } + }, + } + + try self.writer.writeByte(byte); + } + + /// Append a slice without permitting the backing allocation to + /// grow beyond max_bytes. This matches the byte-at-a-time + /// semantics of writeByte: bytes are retained up to exactly + /// max_bytes and the first byte that doesn't fit fails the + /// write. + pub fn writeSlice( + self: *Capture, + bytes: []const u8, + ) error{WriteFailed}!void { + const avail = self.max_bytes - self.writer.buffered().len; + const n = @min(bytes.len, avail); + + switch (self.backing) { + .fixed => {}, + .allocating => |*w| { + const needed = w.writer.end + n; + if (needed > w.writer.buffer.len) { + const new_capacity = @min( + self.max_bytes, + @max(w.writer.buffer.len *| 2, needed), + ); + w.writer.buffer = w.allocator.realloc( + w.writer.buffer, + new_capacity, + ) catch return error.WriteFailed; + } + }, + } + + try self.writer.writeAll(bytes[0..n]); + if (n < bytes.len) return error.WriteFailed; + } + pub fn deinit(self: *Capture) void { switch (self.backing) { .fixed => {}, @@ -532,6 +616,7 @@ pub const Parser = struct { Capture.allocating( &self.capture, alloc, + self.max_allocating_bytes, ) catch { // The allocator failed for some reason, fall back to a fixed buffer // and hope that it's big enough. @@ -542,6 +627,33 @@ pub const Parser = struct { } } + /// Consume a slice of bytes, advancing the parser state. This is + /// equivalent to calling `next` for each byte in order, but is much + /// faster once a data capture is active because the remaining bytes + /// are appended to the capture in bulk. + pub fn nextSlice(self: *Parser, input: []const u8) void { + if (self.state == .invalid) return; + + // Run the state machine byte-at-a-time until a capture begins. + // The command prefix before a capture starts is only a handful + // of bytes so this loop is short in practice. + var offset: usize = 0; + while (self.capture == null) { + if (offset >= input.len) return; + self.next(input[offset]); + offset += 1; + if (self.state == .invalid) return; + } + + const rem = input[offset..]; + if (rem.len == 0) return; + self.capture.?.writeSlice(rem) catch |err| switch (err) { + // We have overflowed our buffer or had some other error, set + // the state to invalid so that we discard any further input. + error.WriteFailed => self.state = .invalid, + }; + } + /// Consume the next character c and advance the parser state. pub fn next(self: *Parser, c: u8) void { // If the state becomes invalid for any reason, just discard @@ -551,7 +663,7 @@ pub const Parser = struct { // If a writer has been initialized, we just accumulate the rest of the // OSC sequence in the writer's buffer and skip the state machine. if (self.capture) |*cap| { - cap.writer.writeByte(c) catch |err| switch (err) { + cap.writeByte(c) catch |err| switch (err) { // We have overflowed our buffer or had some other error, set the // state to invalid so that we discard any further input. error.WriteFailed => self.state = .invalid, @@ -740,11 +852,24 @@ pub const Parser = struct { else => self.state = .invalid, }, + .@"9", + => switch (c) { + ';' => self.captureTrailing(.fixed), + '9' => self.state = .@"99", + else => self.state = .invalid, + }, + + .@"99", + => switch (c) { + // OSC 99 encoded payloads can exceed the fixed buffer. + ';' => self.captureTrailing(.allocating), + else => self.state = .invalid, + }, + .@"0", .@"22", .@"777", .@"8", - .@"9", => switch (c) { ';' => self.captureTrailing(.fixed), else => self.state = .invalid, @@ -825,6 +950,8 @@ pub const Parser = struct { .@"77" => null, + .@"99" => parsers.kitty_desktop_notification.parse(self, terminator_ch), + .@"133" => parsers.semantic_prompt.parse(self, terminator_ch), .@"552" => null, @@ -842,3 +969,106 @@ test { _ = parsers; _ = encoding; } + +test "Parser allocating captures have a hard limit" { + const testing = std.testing; + const prefixes = [_][]const u8{ "52;", "66;", "72;", "99;", "5522;" }; + const limit = Parser.MAX_BUF + 1; + + for (prefixes) |prefix| { + var p: Parser = .init(testing.allocator); + defer p.deinit(); + p.max_allocating_bytes = limit; + + for (prefix) |ch| p.next(ch); + for (0..limit) |_| p.next('a'); + + const cap = &p.capture.?; + try testing.expectEqual(@as(usize, limit), cap.trailing().len); + try testing.expectEqual(@as(usize, limit), cap.writer.buffer.len); + + p.next('a'); + try testing.expectEqual(Parser.State.invalid, p.state); + try testing.expectEqual(@as(usize, limit), cap.trailing().len); + try testing.expectEqual(@as(usize, limit), cap.writer.buffer.len); + } +} + +test "Parser nextSlice allocating captures have a hard limit" { + const testing = std.testing; + const limit = Parser.MAX_BUF + 1; + + var p: Parser = .init(testing.allocator); + defer p.deinit(); + p.max_allocating_bytes = limit; + + const data = try testing.allocator.alloc(u8, limit); + defer testing.allocator.free(data); + @memset(data, 'a'); + + // Exactly at the limit stays valid and bounded. + p.nextSlice("52;"); + p.nextSlice(data); + const cap = &p.capture.?; + try testing.expect(p.state != .invalid); + try testing.expectEqual(@as(usize, limit), cap.trailing().len); + try testing.expectEqual(@as(usize, limit), cap.writer.buffer.len); + + // One more byte overflows: the state becomes invalid and the + // retained bytes and allocation stay bounded. + p.nextSlice("a"); + try testing.expectEqual(Parser.State.invalid, p.state); + try testing.expectEqual(@as(usize, limit), cap.trailing().len); + try testing.expectEqual(@as(usize, limit), cap.writer.buffer.len); + try testing.expect(p.end(null) == null); +} + +test "Parser nextSlice overflowing slice is truncated at the limit" { + const testing = std.testing; + + var p: Parser = .init(testing.allocator); + defer p.deinit(); + p.max_allocating_bytes = 4; + + p.nextSlice("52;abcdef"); + try testing.expectEqual(Parser.State.invalid, p.state); + try testing.expect(p.end(null) == null); + + const cap = &p.capture.?; + try testing.expectEqualStrings("abcd", cap.trailing()); + try testing.expectEqual(@as(usize, 4), cap.writer.buffer.len); +} + +test "Parser nextSlice matches per-byte parsing" { + const testing = std.testing; + const input = "52;c;aGVsbG8="; + + // Every two-way split of the input must parse identically to + // the byte-at-a-time path. + for (0..input.len + 1) |split| { + var p: Parser = .init(testing.allocator); + defer p.deinit(); + p.nextSlice(input[0..split]); + p.nextSlice(input[split..]); + + const cmd = p.end(null).?.*; + try testing.expect(cmd == .clipboard_contents); + try testing.expectEqual(@as(u8, 'c'), cmd.clipboard_contents.kind); + try testing.expectEqualStrings("aGVsbG8=", cmd.clipboard_contents.data); + } +} + +test "Parser allocating capture limit includes parser-added bytes" { + const testing = std.testing; + var p: Parser = .init(testing.allocator); + defer p.deinit(); + p.max_allocating_bytes = 4; + + for ("52;abcd") |ch| p.next(ch); + try testing.expect(p.end(null) == null); + try testing.expectEqual(Parser.State.invalid, p.state); + + const cap = &p.capture.?; + try testing.expectEqual(@as(usize, 4), cap.trailing().len); + try testing.expectEqual(@as(usize, 4), cap.writer.buffer.len); +} diff --git a/vendor/libghostty-vt/src/terminal/osc/kitty_metadata.zig b/vendor/libghostty-vt/src/terminal/osc/kitty_metadata.zig new file mode 100644 index 00000000..469c97a0 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/osc/kitty_metadata.zig @@ -0,0 +1,94 @@ +//! Helpers for parsing metadata shared by Kitty OSC protocols. +//! +//! Kitty OSC 99 and OSC 5522 encode metadata as colon-separated `key=value` +//! fields. The iterator in this module lazily searches that metadata for one +//! key, preserving the order of repeated values without allocating. +//! +//! Parsing is intentionally tolerant. Whitespace around keys and values is +//! trimmed, while malformed fields, non-matching keys, and invalid values are +//! skipped. Returned values are slices of the original metadata and remain +//! valid only as long as that input remains valid. + +const std = @import("std"); + +/// Return an iterator over values whose key exactly matches `key`. +/// +/// If `valid_value_characters` is non-null, every byte in a returned value must +/// appear in that character set. Passing null disables value validation. Both +/// arguments are comptime-known so each protocol can specialize the iterator +/// for its metadata grammar without storing a key or validator at runtime. +pub fn ValueIterator( + comptime key: []const u8, + comptime valid_value_characters: ?[]const u8, +) type { + return struct { + const Self = @This(); + + metadata: []const u8, + pos: usize, + + /// Initialize an iterator borrowing `metadata`. + pub fn init(metadata: []const u8) Self { + return .{ + .metadata = metadata, + .pos = 0, + }; + } + + /// Return the next valid matching value, or null when none remain. + /// The returned slice borrows the metadata passed to `init`. + pub fn next(self: *Self) ?[]const u8 { + while (self.pos < self.metadata.len) { + const end = std.mem.indexOfScalarPos( + u8, + self.metadata, + self.pos, + ':', + ) orelse self.metadata.len; + const field = self.metadata[self.pos..end]; + self.pos = if (end < self.metadata.len) end + 1 else end; + + const equals = std.mem.indexOfScalar(u8, field, '=') orelse + continue; + const field_key = std.mem.trim( + u8, + field[0..equals], + &std.ascii.whitespace, + ); + if (!std.mem.eql(u8, field_key, key)) continue; + + const value = std.mem.trim( + u8, + field[equals + 1 ..], + &std.ascii.whitespace, + ); + if (valid_value_characters) |valid| { + if (std.mem.indexOfNone(u8, value, valid) != null) continue; + } + + return value; + } + + return null; + } + }; +} + +test "ValueIterator skips malformed and prefix-matching keys" { + const testing = std.testing; + var it: ValueIterator("id", null) = .init( + "id-extra=wrong:id: id = first :id=second", + ); + + try testing.expectEqualStrings("first", it.next().?); + try testing.expectEqualStrings("second", it.next().?); + try testing.expect(it.next() == null); +} + +test "ValueIterator skips values containing disallowed characters" { + const testing = std.testing; + var it: ValueIterator("i", "abc") = .init("i=a?:i=abc"); + + try testing.expectEqualStrings("abc", it.next().?); + try testing.expect(it.next() == null); +} diff --git a/vendor/libghostty-vt/src/terminal/osc/parsers.zig b/vendor/libghostty-vt/src/terminal/osc/parsers.zig index 8cb91fb9..148bfa33 100644 --- a/vendor/libghostty-vt/src/terminal/osc/parsers.zig +++ b/vendor/libghostty-vt/src/terminal/osc/parsers.zig @@ -10,6 +10,7 @@ pub const iterm2 = @import("parsers/iterm2.zig"); pub const kitty_clipboard_protocol = @import("parsers/kitty_clipboard_protocol.zig"); pub const kitty_color = @import("parsers/kitty_color.zig"); pub const kitty_dnd_protocol = @import("parsers/kitty_dnd_protocol.zig"); +pub const kitty_desktop_notification = @import("parsers/kitty_desktop_notification.zig"); pub const kitty_text_sizing = @import("parsers/kitty_text_sizing.zig"); pub const mouse_shape = @import("parsers/mouse_shape.zig"); pub const osc9 = @import("parsers/osc9.zig"); diff --git a/vendor/libghostty-vt/src/terminal/osc/parsers/clipboard_operation.zig b/vendor/libghostty-vt/src/terminal/osc/parsers/clipboard_operation.zig index 8d149f97..01588771 100644 --- a/vendor/libghostty-vt/src/terminal/osc/parsers/clipboard_operation.zig +++ b/vendor/libghostty-vt/src/terminal/osc/parsers/clipboard_operation.zig @@ -6,13 +6,13 @@ const Parser = @import("../../osc.zig").Parser; const Command = @import("../../osc.zig").Command; /// Parse OSC 52 -pub fn parse(parser: *Parser, _: ?u8) ?*Command { +pub fn parse(parser: *Parser, terminator_ch: ?u8) ?*Command { assert(parser.state == .@"52"); const cap = if (parser.capture) |*c| c else { parser.state = .invalid; return null; }; - cap.writer.writeByte(0) catch { + cap.writeByte(0) catch { parser.state = .invalid; return null; }; @@ -26,6 +26,7 @@ pub fn parse(parser: *Parser, _: ?u8) ?*Command { .clipboard_contents = .{ .kind = 'c', .data = data[1 .. data.len - 1 :0], + .terminator = .init(terminator_ch), }, }; } else { @@ -41,6 +42,7 @@ pub fn parse(parser: *Parser, _: ?u8) ?*Command { .clipboard_contents = .{ .kind = data[0], .data = data[2 .. data.len - 1 :0], + .terminator = .init(terminator_ch), }, }; } @@ -59,6 +61,20 @@ test "OSC 52: get/set clipboard" { try testing.expect(cmd == .clipboard_contents); try testing.expect(cmd.clipboard_contents.kind == 's'); try testing.expectEqualStrings("?", cmd.clipboard_contents.data); + try testing.expectEqual(.st, cmd.clipboard_contents.terminator); +} + +test "OSC 52: get clipboard with BEL terminator" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "52;c;?"; + for (input) |ch| p.next(ch); + + const cmd = p.end(0x07).?.*; + try testing.expect(cmd == .clipboard_contents); + try testing.expectEqual(.bel, cmd.clipboard_contents.terminator); } test "OSC 52: get/set clipboard (optional parameter)" { diff --git a/vendor/libghostty-vt/src/terminal/osc/parsers/color.zig b/vendor/libghostty-vt/src/terminal/osc/parsers/color.zig index 547c98ea..d8b89f41 100644 --- a/vendor/libghostty-vt/src/terminal/osc/parsers/color.zig +++ b/vendor/libghostty-vt/src/terminal/osc/parsers/color.zig @@ -6,6 +6,7 @@ const SpecialColor = @import("../../color.zig").Special; const RGB = @import("../../color.zig").RGB; const Parser = @import("../../osc.zig").Parser; const Command = @import("../../osc.zig").Command; +const SegmentedList = @import("../../../datastruct/segmented_list.zig").SegmentedList; const log = std.log.scoped(.osc_color); @@ -182,19 +183,19 @@ fn parseGetSetAnsiColor( // Parse the color. const target: Target = switch (op) { // OSC5 maps directly to the Special enum. - .osc_5 => .{ .special = std.meta.intToEnum( + .osc_5 => .{ .special = std.enums.fromInt( SpecialColor, std.math.cast(u3, color) orelse return result, - ) catch return result }, + ) orelse return result }, // OSC4 maps 0-255 to palette, 256-259 to special offset // by the palette count. .osc_4 => if (std.math.cast(u8, color)) |idx| .{ .palette = idx, - } else .{ .special = std.meta.intToEnum( + } else .{ .special = std.enums.fromInt( SpecialColor, std.math.cast(u3, color - 256) orelse return result, - ) catch return result }, + ) orelse return result }, else => comptime unreachable, }; @@ -255,19 +256,19 @@ fn parseResetAnsiColor( // Parse the color. const target: Target = switch (op) { // OSC105 maps directly to the Special enum. - .osc_105 => .{ .special = std.meta.intToEnum( + .osc_105 => .{ .special = std.enums.fromInt( SpecialColor, std.math.cast(u3, color) orelse continue, - ) catch continue }, + ) orelse continue }, // OSC104 maps 0-255 to palette, 256-259 to special offset // by the palette count. .osc_104 => if (std.math.cast(u8, color)) |idx| .{ .palette = idx, - } else .{ .special = std.meta.intToEnum( + } else .{ .special = std.enums.fromInt( SpecialColor, std.math.cast(u3, color - 256) orelse continue, - ) catch continue }, + ) orelse continue }, else => comptime unreachable, }; @@ -329,7 +330,7 @@ fn parseResetDynamicColor( /// The exact prealloc value is chosen arbitrarily assuming most /// color ops have very few. If we can get empirical data on more /// typical values we can switch to that. -pub const List = std.SegmentedList( +pub const List = SegmentedList( Request, 2, ); @@ -450,7 +451,7 @@ test "OSC 4:" { // Test every special color for (0..@typeInfo(SpecialColor).@"enum".fields.len) |i| { - const special = try std.meta.intToEnum(SpecialColor, i); + const special = std.enums.fromInt(SpecialColor, i) orelse return error.InvalidEnumValue; // Simple color set // printf '\e]4;256;red\\' @@ -482,7 +483,7 @@ test "OSC 5:" { // Test every special color for (0..@typeInfo(SpecialColor).@"enum".fields.len) |i| { - const special = try std.meta.intToEnum(SpecialColor, i); + const special = std.enums.fromInt(SpecialColor, i) orelse return error.InvalidEnumValue; // Simple color set // printf '\e]4;256;red\\' @@ -592,7 +593,7 @@ test "OSC 104:" { // Test every special color for (0..@typeInfo(SpecialColor).@"enum".fields.len) |i| { - const special = try std.meta.intToEnum(SpecialColor, i); + const special = std.enums.fromInt(SpecialColor, i) orelse return error.InvalidEnumValue; // Simple color set // printf '\e]104;256\\' diff --git a/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_clipboard_protocol.zig b/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_clipboard_protocol.zig index 5bd0e254..1bcc7d38 100644 --- a/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_clipboard_protocol.zig +++ b/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_clipboard_protocol.zig @@ -10,6 +10,7 @@ const assert = @import("../../../quirks.zig").inlineAssert; const Parser = @import("../../osc.zig").Parser; const Command = @import("../../osc.zig").Command; const Terminator = @import("../../osc.zig").Terminator; +const kitty_metadata = @import("../kitty_metadata.zig"); const encoding = @import("../encoding.zig"); const log = std.log.scoped(.kitty_clipboard_protocol); @@ -22,6 +23,12 @@ pub const OSC = struct { /// The terminator that was used in case we need to send a response. terminator: Terminator, + pub const C = void; + + pub fn cval(_: OSC) C { + return {}; + } + /// Decode an option from the metadata. pub fn readOption(self: OSC, comptime key: Option) ?key.Type() { return key.read(self.metadata); @@ -51,6 +58,7 @@ pub const Status = enum { DATA, DONE, EBUSY, + EFBIG, EINVAL, EIO, ENOSYS, @@ -90,37 +98,8 @@ pub const Option = enum { comptime key: Option, metadata: []const u8, ) ?key.Type() { - const value: []const u8 = value: { - var pos: usize = 0; - while (pos < metadata.len) { - // skip any whitespace - while (pos < metadata.len and std.ascii.isWhitespace(metadata[pos])) pos += 1; - // bail if we are out of metadata - if (pos >= metadata.len) return null; - if (!std.mem.startsWith(u8, metadata[pos..], @tagName(key))) { - // this isn't the key we are looking for, skip to the next option, or bail if - // there is no next option - pos = std.mem.indexOfScalarPos(u8, metadata, pos, ':') orelse return null; - pos += 1; - continue; - } - // skip past the key - pos += @tagName(key).len; - // skip any whitespace - while (pos < metadata.len and std.ascii.isWhitespace(metadata[pos])) pos += 1; - // bail if we are out of metadata - if (pos >= metadata.len) return null; - // a valid option has an '=' - if (metadata[pos] != '=') return null; - // the end of the value is bounded by a ':' or the end of the metadata - const end = std.mem.indexOfScalarPos(u8, metadata, pos, ':') orelse metadata.len; - const start = pos + 1; - // strip any leading or trailing whitespace - break :value std.mem.trim(u8, metadata[start..end], &std.ascii.whitespace); - } - // the key was not found - return null; - }; + var it: kitty_metadata.ValueIterator(@tagName(key), null) = .init(metadata); + const value = it.next() orelse return null; // return the parsed value return switch (key) { diff --git a/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_desktop_notification.zig b/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_desktop_notification.zig new file mode 100644 index 00000000..27fc8670 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_desktop_notification.zig @@ -0,0 +1,1201 @@ +//! Kitty's desktop notification protocol (OSC 99) +//! Specification: https://sw.kovidgoyal.net/kitty/desktop-notifications/ + +const std = @import("std"); + +const assert = @import("../../../quirks.zig").inlineAssert; + +const Parser = @import("../../osc.zig").Parser; +const Command = @import("../../osc.zig").Command; +const Terminator = @import("../../osc.zig").Terminator; +const kitty_metadata = @import("../kitty_metadata.zig"); +const encoding = @import("../encoding.zig"); + +const log = std.log.scoped(.kitty_desktop_notification); + +pub const MAX_PLAIN_PAYLOAD_BYTES = 2048; +pub const MAX_ENCODED_PAYLOAD_BYTES = 4096; + +pub const OSC = struct { + /// The raw metadata that was received. It can be parsed by using the `readOption` method. + metadata: []const u8, + /// The raw payload. It may be Base64 encoded, check the `e` option. + payload: []const u8, + /// The terminator that was used in case we need to send a response. + terminator: Terminator, + + /// Decode an option from the metadata. + pub fn readOption(self: OSC, comptime key: Option) key.Type() { + return key.read(self.metadata); + } +}; + +pub const Action = packed struct { + focus: bool, + report: bool, + + pub const default: Action = .{ + .focus = true, + .report = false, + }; + + pub fn init(str: []const u8) Action { + return parsePackedStruct(Action, str); + } +}; + +pub const Occasion = enum { + always, + invisible, + unfocused, + + pub const default: Occasion = .always; + + pub fn init(str: []const u8) Occasion { + return std.meta.stringToEnum(Occasion, str) orelse .default; + } +}; + +pub const Payload = enum { + alive, + body, + buttons, + close, + icon, + query, + title, + /// This is a special value to indicate that an unknown payload value was + /// specified and it should be ignored. + unknown, + + pub const default: Payload = .title; + + pub fn init(str: []const u8) Payload { + if (str.len == 1 and str[0] == '?') return .query; + // The string `query` is not allowed, it should be a single question + // mark if you want a query. + if (std.mem.eql(u8, "query", str)) return .unknown; + return std.meta.stringToEnum(Payload, str) orelse .unknown; + } +}; + +pub const Urgency = enum { + low, + normal, + high, + + pub const default: Urgency = .normal; + + pub fn init(str: []const u8) Urgency { + if (str.len != 1) return .default; + return switch (str[0]) { + '0' => .low, + '1' => .normal, + '2' => .high, + else => .default, + }; + } +}; + +pub const Option = enum { + /// What action(s) should be taken when a notification is clicked. + a, + /// Should a notification be sent to the application when the notification + /// is closed? + c, + /// Are we done with the notification, and it is ready to be sent? + d, + /// Is the payload encoded with Base64? + e, + /// The name of the application that is sending the notification. + f, + /// Identifier for icon data. Only used when the payload is icon data. + g, + /// Identifier for the notification. + i, + /// Icon name. + n, + /// When to honor the notification request. + o, + /// Type of the payload. + p, + /// The sound name to play with the notification. + s, + /// The type of the notification. + t, + /// The urgency of the notification. + u, + /// When to auto-close the notification. + w, + + pub fn Type(comptime key: Option) type { + return switch (key) { + .a => Action, + .c => bool, + .d => bool, + .e => bool, + .f => ?[]const u8, + .g => ?[]const u8, + .i => ?[]const u8, + .n => kitty_metadata.ValueIterator( + @tagName(key), + valid_metadata_value_characters, + ), + .o => Occasion, + .p => Payload, + .s => []const u8, + .t => kitty_metadata.ValueIterator( + @tagName(key), + valid_metadata_value_characters, + ), + .u => Urgency, + .w => i32, + }; + } + + pub fn default(comptime key: Option) key.Type() { + return switch (key) { + .a => .default, + .c => false, + .d => true, + .e => false, + .f => null, + .g => null, + .i => null, + .n => unreachable, + .o => .default, + .p => .default, + .s => "system", + .t => unreachable, + .u => .default, + .w => -1, + }; + } + + /// Read the option value from the raw metadata string. + /// + /// Unknown and malformed values are ignored. Optional values return null; + /// all other values return the protocol default. + pub fn read( + comptime key: Option, + metadata: []const u8, + ) key.Type() { + var it: kitty_metadata.ValueIterator( + @tagName(key), + valid_metadata_value_characters, + ) = switch (key) { + .t, .n => return .init(metadata), + else => .init(metadata), + }; + + const value = it.next() orelse return key.default(); + + // return the parsed value, the iterator guarantees that it's a valid + // metadata value + return switch (key) { + .a => .init(value), + .c => parseBool(value) orelse key.default(), + .d => parseBool(value) orelse key.default(), + .e => parseBool(value) orelse key.default(), + .f => value, + .g => parseIdentifier(value), + .i => parseIdentifier(value), + .n => unreachable, + .o => .init(value), + .p => .init(value), + .s => value, + .t => unreachable, + .u => .init(value), + .w => value: { + // Zig's integer parser allows '_', we don't + if (std.mem.indexOfScalar(u8, value, '_')) |_| break :value key.default(); + const tmp = std.fmt.parseInt(i32, value, 10) catch break :value key.default(); + // negative values less than -1 are not allowed + if (tmp < -1) break :value key.default(); + break :value tmp; + }, + }; + } +}; + +/// Parse the protocol's booleans +fn parseBool(str: []const u8) ?bool { + if (str.len != 1) return null; + return switch (str[0]) { + '0' => false, + '1' => true, + else => null, + }; +} + +/// This is similar to the packed struct parser used in the configs. The +/// differences are that a literal `true` or `false` value does not turn on/off +/// all the values, and the negation prefix is `-` not `no-`. +fn parsePackedStruct(comptime T: type, str: []const u8) T { + const info = @typeInfo(T).@"struct"; + comptime assert(info.layout == .@"packed"); + + var result: T = .default; + + // We split each value by "," + var iter = std.mem.splitSequence(u8, str, ","); + loop: while (iter.next()) |raw| { + // Determine the field we're looking for and the value. If the + // field is prefixed with "-" then we set the value to false. + const part, const value = part: { + const negation_prefix = "-"; + const trimmed = std.mem.trim(u8, raw, &std.ascii.whitespace); + if (std.mem.startsWith(u8, trimmed, negation_prefix)) { + break :part .{ trimmed[negation_prefix.len..], false }; + } else { + break :part .{ trimmed, true }; + } + }; + + inline for (info.fields) |field| { + comptime assert(field.type == bool); + if (std.mem.eql(u8, field.name, part)) { + @field(result, field.name) = value; + continue :loop; + } + } + + // No field matched + return .default; + } + + return result; +} + +/// Characters that are valid in a metadata value. Including `=` is technically +/// against the spec but is needed since Base64 encoded values (with padding) +/// are valid for some options. Including `?` is technically against the spec +/// but is needed since it is a valid value for the `p` option. +const valid_metadata_value_characters: []const u8 = valid_identifier_characters ++ "/,(){}[]*&^%$#@!`~=?"; + +/// Characters that are valid in identifiers. +const valid_identifier_characters: []const u8 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+."; + +fn isValidIdentifier(str: []const u8) bool { + return std.mem.indexOfNone(u8, str, valid_identifier_characters) == null; +} + +fn parseIdentifier(str: []const u8) ?[]const u8 { + if (isValidIdentifier(str)) return str; + return null; +} + +pub fn parse(parser: *Parser, terminator_ch: ?u8) ?*Command { + assert(parser.state == .@"99"); + + const cap = if (parser.capture) |*c| c else { + parser.state = .invalid; + return null; + }; + + const data = cap.trailing(); + + const payload_start = std.mem.indexOfScalar(u8, data, ';') orelse { + log.warn("missing semicolon before payload", .{}); + parser.state = .invalid; + return null; + }; + + const metadata = data[0..payload_start]; + const payload = data[payload_start + 1 .. data.len]; + + const max_payload_bytes: usize = if (Option.e.read(metadata)) + MAX_ENCODED_PAYLOAD_BYTES + else + MAX_PLAIN_PAYLOAD_BYTES; + if (payload.len > max_payload_bytes) { + log.warn( + "payload is too large: size={d} max={d}", + .{ payload.len, max_payload_bytes }, + ); + parser.state = .invalid; + return null; + } + + // Payload has to be an escape-code-safe UTF-8 string. + if (!encoding.isSafeUtf8(payload)) { + log.warn("payload is not escape code safe UTF-8", .{}); + parser.state = .invalid; + return null; + } + + parser.command = .{ + .kitty_desktop_notification = .{ + .metadata = metadata, + .payload = payload, + .terminator = .init(terminator_ch), + }, + }; + + return &parser.command; +} + +test "OSC 99: empty metadata and payload" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;;"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("", cmd.kitty_desktop_notification.metadata); + try testing.expectEqualStrings("", cmd.kitty_desktop_notification.payload); + try testing.expectEqualDeep(Option.a.default(), cmd.kitty_desktop_notification.readOption(.a)); + try testing.expect(cmd.kitty_desktop_notification.readOption(.c) == false); + try testing.expect(cmd.kitty_desktop_notification.readOption(.d) == true); + try testing.expect(cmd.kitty_desktop_notification.readOption(.e) == false); + try testing.expect(cmd.kitty_desktop_notification.readOption(.f) == null); + try testing.expect(cmd.kitty_desktop_notification.readOption(.g) == null); + try testing.expect(cmd.kitty_desktop_notification.readOption(.i) == null); + { + var it = cmd.kitty_desktop_notification.readOption(.n); + try testing.expect(it.next() == null); + } + try testing.expectEqual(.always, cmd.kitty_desktop_notification.readOption(.o)); + try testing.expectEqual(.title, cmd.kitty_desktop_notification.readOption(.p)); + try testing.expectEqualStrings("system", cmd.kitty_desktop_notification.readOption(.s)); + { + var it = cmd.kitty_desktop_notification.readOption(.t); + try testing.expect(it.next() == null); + } + try testing.expectEqual(.normal, cmd.kitty_desktop_notification.readOption(.u)); + try testing.expectEqual(-1, cmd.kitty_desktop_notification.readOption(.w)); + try testing.expectEqual(.st, cmd.kitty_desktop_notification.terminator); +} + +test "OSC 99: empty metadata with payload" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;;bobr"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("", cmd.kitty_desktop_notification.metadata); + try testing.expectEqualStrings("bobr", cmd.kitty_desktop_notification.payload); + try testing.expectEqualDeep(Option.a.default(), cmd.kitty_desktop_notification.readOption(.a)); + try testing.expect(cmd.kitty_desktop_notification.readOption(.c) == false); + try testing.expect(cmd.kitty_desktop_notification.readOption(.d) == true); + try testing.expect(cmd.kitty_desktop_notification.readOption(.e) == false); + try testing.expect(cmd.kitty_desktop_notification.readOption(.f) == null); + try testing.expect(cmd.kitty_desktop_notification.readOption(.g) == null); + try testing.expect(cmd.kitty_desktop_notification.readOption(.i) == null); + { + var it = cmd.kitty_desktop_notification.readOption(.n); + try testing.expect(it.next() == null); + } + try testing.expectEqual(.always, cmd.kitty_desktop_notification.readOption(.o)); + try testing.expectEqual(.title, cmd.kitty_desktop_notification.readOption(.p)); + try testing.expectEqualStrings("system", cmd.kitty_desktop_notification.readOption(.s)); + { + var it = cmd.kitty_desktop_notification.readOption(.t); + try testing.expect(it.next() == null); + } + try testing.expectEqual(.normal, cmd.kitty_desktop_notification.readOption(.u)); + try testing.expectEqual(-1, cmd.kitty_desktop_notification.readOption(.w)); +} + +test "OSC 99: payload size limits" { + const testing = std.testing; + const cases = [_]struct { + metadata: []const u8, + payload_size: usize, + valid: bool, + }{ + .{ .metadata = "", .payload_size = MAX_PLAIN_PAYLOAD_BYTES, .valid = true }, + .{ .metadata = "", .payload_size = MAX_PLAIN_PAYLOAD_BYTES + 1, .valid = false }, + .{ .metadata = "e=1", .payload_size = MAX_ENCODED_PAYLOAD_BYTES, .valid = true }, + .{ .metadata = "e=1", .payload_size = MAX_ENCODED_PAYLOAD_BYTES + 1, .valid = false }, + }; + + for (cases) |case| { + var p: Parser = .init(testing.allocator); + defer p.deinit(); + + for ("99;") |ch| p.next(ch); + for (case.metadata) |ch| p.next(ch); + p.next(';'); + for (0..case.payload_size) |_| p.next('a'); + + try testing.expectEqual(case.valid, p.end('\x1b') != null); + } +} + +test "OSC 99: unknown prefix does not hide dotted identifier" { + const testing = std.testing; + + var p: Parser = .init(null); + const input = "99;invalid=wrong:i=org.ghostty;payload"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expectEqualStrings( + "org.ghostty", + cmd.kitty_desktop_notification.readOption(.i).?, + ); +} + +test "OSC 99: single parameter i" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;i=bobr;payload"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("bobr", cmd.kitty_desktop_notification.readOption(.i).?); + try testing.expectEqualStrings("payload", cmd.kitty_desktop_notification.payload); +} + +test "OSC 99: repeated parameter i" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;i=bobr:i=foobar;payload"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("bobr", cmd.kitty_desktop_notification.readOption(.i).?); + try testing.expectEqualStrings("payload", cmd.kitty_desktop_notification.payload); +} + +test "OSC 99: multiple types" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;t=mail: t = chat : t = alert ;notification"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("notification", cmd.kitty_desktop_notification.payload); + var it = cmd.kitty_desktop_notification.readOption(.t); + try testing.expectEqualStrings("mail", it.next().?); + try testing.expectEqualStrings("chat", it.next().?); + try testing.expectEqualStrings("alert", it.next().?); + try testing.expect(it.next() == null); +} + +test "OSC 99: a 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;a=report,focus;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualDeep(Action{ .report = true, .focus = true }, cmd.kitty_desktop_notification.readOption(.a)); +} + +test "OSC 99: a 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;a=report,-focus;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualDeep(Action{ .report = true, .focus = false }, cmd.kitty_desktop_notification.readOption(.a)); +} + +test "OSC 99: a 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;a=-report,focus;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualDeep(Action{ .report = false, .focus = true }, cmd.kitty_desktop_notification.readOption(.a)); +} + +test "OSC 99: a 4" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;a=-report,-focus;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualDeep(Action{ .report = false, .focus = false }, cmd.kitty_desktop_notification.readOption(.a)); +} + +test "OSC 99: c 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;c=0;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.c) == false); +} + +test "OSC 99: c 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;c=1;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.c) == true); +} + +test "OSC 99: c 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;c=bobr;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.c) == false); +} + +test "OSC 99: d 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;d=0;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.d) == false); +} + +test "OSC 99: d 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;d=1;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.d) == true); +} + +test "OSC 99: d 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;d=bobr;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.d) == true); +} + +test "OSC 99: e 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;e=0;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.e) == false); +} + +test "OSC 99: e 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;e=1;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.e) == true); +} + +test "OSC 99: e 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;e=bobr;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.e) == false); +} + +test "OSC 99: f 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;f=R2hvc3R0eQ==;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("R2hvc3R0eQ==", cmd.kitty_desktop_notification.readOption(.f).?); +} + +test "OSC 99: f 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;c=0:f= R2hvc3R0eQ== ;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("R2hvc3R0eQ==", cmd.kitty_desktop_notification.readOption(.f).?); +} + +test "OSC 99: g 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;c=0:g=7f8a9129-a35d-4e9f-8043-ce2700e15e2c;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("7f8a9129-a35d-4e9f-8043-ce2700e15e2c", cmd.kitty_desktop_notification.readOption(.g).?); +} + +test "OSC 99: g 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;c=0:g=aaa*bbb;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.g) == null); +} + +test "OSC 99: i 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expect(cmd.kitty_desktop_notification.readOption(.i) == null); +} + +test "OSC 99: i 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;i=bobr;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("bobr", cmd.kitty_desktop_notification.readOption(.i).?); +} + +test "OSC 99: i 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;i=;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("", cmd.kitty_desktop_notification.readOption(.i).?); +} + +test "OSC 99: i 4" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;i= :;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("", cmd.kitty_desktop_notification.readOption(.i).?); +} + +test "OSC 99: i 5" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;i= bobr ;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("bobr", cmd.kitty_desktop_notification.readOption(.i).?); +} + +test "OSC 99: i 6" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;i= bobr : i=kurwa ;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("bobr", cmd.kitty_desktop_notification.readOption(.i).?); +} + +test "OSC 99: n 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;n=R2hvc3R0eQ==;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + var it = cmd.kitty_desktop_notification.readOption(.n); + try testing.expectEqualStrings("R2hvc3R0eQ==", it.next().?); + try testing.expect(it.next() == null); +} + +test "OSC 99: n 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;n=R2hvc3R0eQ==:n=R2hvc3R0eQ==;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + var it = cmd.kitty_desktop_notification.readOption(.n); + try testing.expectEqualStrings("R2hvc3R0eQ==", it.next().?); + try testing.expectEqualStrings("R2hvc3R0eQ==", it.next().?); + try testing.expect(it.next() == null); +} + +test "OSC 99: o 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;o= ;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.always, cmd.kitty_desktop_notification.readOption(.o)); +} + +test "OSC 99: o 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;o=always;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.always, cmd.kitty_desktop_notification.readOption(.o)); +} + +test "OSC 99: o 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;o=unfocused;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.unfocused, cmd.kitty_desktop_notification.readOption(.o)); +} + +test "OSC 99: o 4" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;o=invisible;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.invisible, cmd.kitty_desktop_notification.readOption(.o)); +} + +test "OSC 99: o 5" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;o=bobr;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.always, cmd.kitty_desktop_notification.readOption(.o)); +} + +test "OSC 99: p 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=alive;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.alive, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: p 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=body;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.body, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: p 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=buttons;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.buttons, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: p 4" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=close;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.close, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: p 5" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=icon;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.icon, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: p 6" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=?;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.query, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: p 7" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=title;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.title, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: p 8" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=query;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.unknown, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: p 9" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;p=bobr;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.unknown, cmd.kitty_desktop_notification.readOption(.p)); +} + +test "OSC 99: s 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;s=R2hvc3R0eQ==;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqualStrings("R2hvc3R0eQ==", cmd.kitty_desktop_notification.readOption(.s)); +} + +test "OSC 99: t 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;t=R2hvc3R0eQ==;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + var it = cmd.kitty_desktop_notification.readOption(.t); + try testing.expectEqualStrings("R2hvc3R0eQ==", it.next().?); + try testing.expect(it.next() == null); +} + +test "OSC 99: t 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;t=R2hvc3R0eQ==:t=R2hvc3R0eQ==;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + var it = cmd.kitty_desktop_notification.readOption(.t); + try testing.expectEqualStrings("R2hvc3R0eQ==", it.next().?); + try testing.expectEqualStrings("R2hvc3R0eQ==", it.next().?); + try testing.expect(it.next() == null); +} + +test "OSC 99: u 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;u=0;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.low, cmd.kitty_desktop_notification.readOption(.u)); +} + +test "OSC 99: u 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;u=1;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.normal, cmd.kitty_desktop_notification.readOption(.u)); +} + +test "OSC 99: u 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;u=2;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.high, cmd.kitty_desktop_notification.readOption(.u)); +} + +test "OSC 99: u 4" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;u=bobr;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(.normal, cmd.kitty_desktop_notification.readOption(.u)); +} + +test "OSC 99: w 1" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;w=0;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(0, cmd.kitty_desktop_notification.readOption(.w)); +} + +test "OSC 99: w 2" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;w=-1;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(-1, cmd.kitty_desktop_notification.readOption(.w)); +} + +test "OSC 99: w 3" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;w=-42;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(-1, cmd.kitty_desktop_notification.readOption(.w)); +} + +test "OSC 99: w 4" { + const testing = std.testing; + + var p: Parser = .init(null); + + const input = "99;w=4294967296;foobar"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b').?.*; + try testing.expect(cmd == .kitty_desktop_notification); + try testing.expectEqualStrings("foobar", cmd.kitty_desktop_notification.payload); + try testing.expectEqual(-1, cmd.kitty_desktop_notification.readOption(.w)); +} diff --git a/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_dnd_protocol.zig b/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_dnd_protocol.zig index 35d23906..839e3961 100644 --- a/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_dnd_protocol.zig +++ b/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_dnd_protocol.zig @@ -1,5 +1,12 @@ //! Kitty's drag and drop protocol (OSC 72) -//! Specification: https://sw.kovidgoyal.net/kitty/drag-and-drop-protocol/ +//! +//! This only captures the raw metadata and payload for the OSC. The +//! actual protocol grammar (metadata keys, event types, chunking) is +//! implemented in `terminal/kitty/dnd.zig` and its submodules, since the +//! protocol requires stateful handling that doesn't belong in the +//! stateless OSC parser. +//! +//! Specification: https://sw.kovidgoyal.net/kitty/dnd-protocol/ const std = @import("std"); @@ -9,143 +16,52 @@ const Parser = @import("../../osc.zig").Parser; const Command = @import("../../osc.zig").Command; const Terminator = @import("../../osc.zig").Terminator; -const log = std.log.scoped(.kitty_dnd_protocol); - pub const OSC = struct { - /// The raw metadata that was received. Parse individual values with `readOption`. + /// The raw metadata that was received. Parse with + /// `kitty.dnd.Metadata.parse`. metadata: []const u8, - /// The raw payload. Its meaning and encoding depend on the event type (`t` key). + + /// The raw payload. Its meaning and encoding depend on the event + /// type (`t` metadata key). Null when the OSC had no `;` after the + /// metadata; an empty payload is distinct from no payload. payload: ?[]const u8, + /// The terminator used for this OSC, so any response can match it. terminator: Terminator, - pub fn readOption(self: OSC, comptime key: Option) ?key.Type() { - return key.read(self.metadata); - } -}; + /// We don't currently support encoding this to C in any way. + pub const C = void; -/// Values for the `t` (event type) metadata key. -pub const EventType = enum { - /// ('a') Terminal registers itself as willing to accept drops. - accept_drops, - /// ('A') Terminal unregisters itself; drops should no longer be forwarded. - stop_accepting_drops, - /// ('m') Pointer is moving over the terminal while a drag is in progress. - /// Carries `x`/`y` cursor position; -1 signals the drag left the window. - drop_move, - /// ('M') Items were dropped onto the terminal. - /// Carries `x`/`y` drop position and `i` (multiplexer session ID). - drop_dropped, - /// ('r') Terminal requests data for a specific MIME type from the drag source. - /// Carries `i` (multiplexer session ID) and `y` (1-based MIME type index). - request_data, - /// ('R') Error response to a `request_data` event. - request_error, - /// ('o') Terminal offers data for an outgoing drag (drag-out from terminal). - offer_drag, - /// ('p') Drag source presents the actual payload for a previously requested MIME type. - /// Carries `i` (multiplexer session ID), `o` (operation), and `m` (chunking flag). - present_data, - /// ('P') Replace the current drag image with a new one. - /// Payload is the image data; `X`/`Y` carry image dimensions in pixels. - change_drag_image, - /// ('e') Notification of an event on an outgoing drag offer (e.g., accepted or rejected). - drag_offer_event, - /// ('E') Error on an outgoing drag offer. - drag_offer_error, - /// ('k') URI list data delivered as part of a drag or clipboard transfer. - uri_list_data, - /// ('q') Query terminal capabilities related to the drag-and-drop protocol. - query, - - pub fn init(str: []const u8) ?EventType { - if (str.len != 1) return null; - return switch (str[0]) { - 'a' => .accept_drops, - 'A' => .stop_accepting_drops, - 'm' => .drop_move, - 'M' => .drop_dropped, - 'r' => .request_data, - 'R' => .request_error, - 'o' => .offer_drag, - 'p' => .present_data, - 'P' => .change_drag_image, - 'e' => .drag_offer_event, - 'E' => .drag_offer_error, - 'k' => .uri_list_data, - 'q' => .query, - else => null, - }; + pub fn cval(_: OSC) C { + return {}; } }; -/// Metadata keys defined by the protocol. Keys are case-sensitive: `x` and `X` are distinct. -pub const Option = enum { - /// Event type. Maps to `EventType`; present in every OSC 72 sequence. - t, - /// Chunking flag. `0` = this is the final (or only) chunk; `1` = more chunks follow. - m, - /// Multiplexer session ID. Echoed back in responses so a terminal multiplexer - /// (e.g. tmux) can route data to the correct pane. - i, - /// Drop operation. `0` = reject, `1` = copy, `2` = move, `3` = copy or move. - o, - /// Cursor column in cell units (zero-based). -1 signals the drag has left the window. - x, - /// Cursor row in cell units (zero-based). Also used as a 1-based MIME type index - /// in some events (e.g. `request_data`). -1 signals the drag has left the window. - y, - /// Pixel offset from the left edge of the cell; also used as image width - /// (with `change_drag_image`) or as a symlink/directory marker. - X, - /// Pixel offset from the top edge of the cell; also used as image height - /// (with `change_drag_image`) or as a parent directory handle. - Y, - - pub fn Type(comptime key: Option) type { - return switch (key) { - .t => EventType, - // The spec uses 32-bit signed or unsigned; we standardize on - // i32 because the location keys legitimately take -1 (drag - // leaves the window) and other keys never exceed i32 range. - .m, .i, .o, .x, .y, .X, .Y => i32, - }; - } +pub fn parse(parser: *Parser, terminator_ch: ?u8) ?*Command { + assert(parser.state == .@"72"); - pub fn read(comptime key: Option, metadata: []const u8) ?key.Type() { - const name = @tagName(key); - - const value: []const u8 = value: { - var pos: usize = 0; - while (pos < metadata.len) { - while (pos < metadata.len and std.ascii.isWhitespace(metadata[pos])) pos += 1; - if (pos >= metadata.len) return null; - - // Case-sensitive match: x and X must not be confused. - if (!std.mem.startsWith(u8, metadata[pos..], name)) { - pos = std.mem.indexOfScalarPos(u8, metadata, pos, ':') orelse return null; - pos += 1; - continue; - } - pos += name.len; - - while (pos < metadata.len and std.ascii.isWhitespace(metadata[pos])) pos += 1; - if (pos >= metadata.len) return null; - if (metadata[pos] != '=') return null; - - const end = std.mem.indexOfScalarPos(u8, metadata, pos, ':') orelse metadata.len; - const start = pos + 1; - break :value std.mem.trim(u8, metadata[start..end], &std.ascii.whitespace); - } - return null; - }; - - return switch (key) { - .t => .init(value), - .m, .i, .o, .x, .y, .X, .Y => std.fmt.parseInt(i32, value, 10) catch null, - }; - } -}; + const cap = if (parser.capture) |*c| c else { + parser.state = .invalid; + return null; + }; + + const data = cap.trailing(); + + const metadata: []const u8, const payload: ?[]const u8 = result: { + const sep = std.mem.indexOfScalar(u8, data, ';') orelse break :result .{ data, null }; + break :result .{ data[0..sep], data[sep + 1 .. data.len] }; + }; + + parser.command = .{ + .kitty_dnd_protocol = .{ + .metadata = metadata, + .payload = payload, + .terminator = .init(terminator_ch), + }, + }; + + return &parser.command; +} test "OSC 72: metadata only, no payload" { const testing = std.testing; @@ -192,134 +108,19 @@ test "OSC 72: metadata and non-empty payload" { try testing.expectEqualStrings("text/plain text/uri-list", cmd.kitty_dnd_protocol.payload.?); } -test "OSC 72: readOption .t valid event types" { - const testing = std.testing; - - var p: Parser = .init(testing.allocator); - defer p.deinit(); - - const cases = .{ - .{ "72;t=a", EventType.accept_drops }, - .{ "72;t=A", EventType.stop_accepting_drops }, - .{ "72;t=m", EventType.drop_move }, - .{ "72;t=M", EventType.drop_dropped }, - .{ "72;t=r", EventType.request_data }, - .{ "72;t=R", EventType.request_error }, - .{ "72;t=o", EventType.offer_drag }, - .{ "72;t=p", EventType.present_data }, - .{ "72;t=P", EventType.change_drag_image }, - .{ "72;t=e", EventType.drag_offer_event }, - .{ "72;t=E", EventType.drag_offer_error }, - .{ "72;t=k", EventType.uri_list_data }, - .{ "72;t=q", EventType.query }, - }; - - inline for (cases) |case| { - p.deinit(); - p = .init(testing.allocator); - for (case[0]) |ch| p.next(ch); - const cmd = p.end('\x1b').?.*; - try testing.expect(cmd == .kitty_dnd_protocol); - try testing.expectEqual(case[1], cmd.kitty_dnd_protocol.readOption(.t).?); - } -} - -test "OSC 72: readOption .t unknown value returns null" { - const testing = std.testing; - - var p: Parser = .init(testing.allocator); - defer p.deinit(); - - const input = "72;t=z"; - for (input) |ch| p.next(ch); - - const cmd = p.end('\x1b').?.*; - try testing.expect(cmd == .kitty_dnd_protocol); - try testing.expect(cmd.kitty_dnd_protocol.readOption(.t) == null); -} - -test "OSC 72: readOption integer keys" { - const testing = std.testing; - - var p: Parser = .init(testing.allocator); - defer p.deinit(); - - const input = "72;t=m:i=3:x=10:y=5:X=320:Y=200:o=1:m=0"; - for (input) |ch| p.next(ch); - - const cmd = p.end('\x1b').?.*; - try testing.expect(cmd == .kitty_dnd_protocol); - try testing.expectEqual(@as(i32, 3), cmd.kitty_dnd_protocol.readOption(.i).?); - try testing.expectEqual(@as(i32, 10), cmd.kitty_dnd_protocol.readOption(.x).?); - try testing.expectEqual(@as(i32, 5), cmd.kitty_dnd_protocol.readOption(.y).?); - try testing.expectEqual(@as(i32, 320), cmd.kitty_dnd_protocol.readOption(.X).?); - try testing.expectEqual(@as(i32, 200), cmd.kitty_dnd_protocol.readOption(.Y).?); - try testing.expectEqual(@as(i32, 1), cmd.kitty_dnd_protocol.readOption(.o).?); - try testing.expectEqual(@as(i32, 0), cmd.kitty_dnd_protocol.readOption(.m).?); -} - -test "OSC 72: readOption negative sentinel (-1 for drag leave)" { - const testing = std.testing; - - var p: Parser = .init(testing.allocator); - defer p.deinit(); - - const input = "72;t=m:x=-1:y=-1"; - for (input) |ch| p.next(ch); - - const cmd = p.end('\x1b').?.*; - try testing.expect(cmd == .kitty_dnd_protocol); - try testing.expectEqual(@as(i32, -1), cmd.kitty_dnd_protocol.readOption(.x).?); - try testing.expectEqual(@as(i32, -1), cmd.kitty_dnd_protocol.readOption(.y).?); -} - -test "OSC 72: readOption case-sensitive key matching" { - const testing = std.testing; - - var p: Parser = .init(testing.allocator); - defer p.deinit(); - - // x=10 must not be returned when asking for .X - const input = "72;x=10:Y=200"; - for (input) |ch| p.next(ch); - - const cmd = p.end('\x1b').?.*; - try testing.expect(cmd == .kitty_dnd_protocol); - try testing.expectEqual(@as(i32, 10), cmd.kitty_dnd_protocol.readOption(.x).?); - try testing.expect(cmd.kitty_dnd_protocol.readOption(.X) == null); - try testing.expectEqual(@as(i32, 200), cmd.kitty_dnd_protocol.readOption(.Y).?); - try testing.expect(cmd.kitty_dnd_protocol.readOption(.y) == null); -} - -test "OSC 72: readOption absent key returns null" { - const testing = std.testing; - - var p: Parser = .init(testing.allocator); - defer p.deinit(); - - const input = "72;t=a"; - for (input) |ch| p.next(ch); - - const cmd = p.end('\x1b').?.*; - try testing.expect(cmd == .kitty_dnd_protocol); - try testing.expect(cmd.kitty_dnd_protocol.readOption(.i) == null); - try testing.expect(cmd.kitty_dnd_protocol.readOption(.x) == null); - try testing.expect(cmd.kitty_dnd_protocol.readOption(.X) == null); - try testing.expect(cmd.kitty_dnd_protocol.readOption(.m) == null); -} - -test "OSC 72: readOption malformed integer returns null" { +test "OSC 72: empty metadata with payload" { const testing = std.testing; var p: Parser = .init(testing.allocator); defer p.deinit(); - const input = "72;x=notanumber"; + const input = "72;;payload"; for (input) |ch| p.next(ch); const cmd = p.end('\x1b').?.*; try testing.expect(cmd == .kitty_dnd_protocol); - try testing.expect(cmd.kitty_dnd_protocol.readOption(.x) == null); + try testing.expectEqualStrings("", cmd.kitty_dnd_protocol.metadata); + try testing.expectEqualStrings("payload", cmd.kitty_dnd_protocol.payload.?); } test "OSC 72: BEL terminator recorded" { @@ -335,29 +136,3 @@ test "OSC 72: BEL terminator recorded" { try testing.expect(cmd == .kitty_dnd_protocol); try testing.expect(cmd.kitty_dnd_protocol.terminator == .bel); } - -pub fn parse(parser: *Parser, terminator_ch: ?u8) ?*Command { - assert(parser.state == .@"72"); - - const cap = if (parser.capture) |*c| c else { - parser.state = .invalid; - return null; - }; - - const data = cap.trailing(); - - const metadata: []const u8, const payload: ?[]const u8 = result: { - const sep = std.mem.indexOfScalar(u8, data, ';') orelse break :result .{ data, null }; - break :result .{ data[0..sep], data[sep + 1 .. data.len] }; - }; - - parser.command = .{ - .kitty_dnd_protocol = .{ - .metadata = metadata, - .payload = payload, - .terminator = .init(terminator_ch), - }, - }; - - return &parser.command; -} diff --git a/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_text_sizing.zig b/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_text_sizing.zig index 94f27100..a60ed5d7 100644 --- a/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_text_sizing.zig +++ b/vendor/libghostty-vt/src/terminal/osc/parsers/kitty_text_sizing.zig @@ -77,7 +77,7 @@ pub fn parse(parser: *Parser, _: ?u8) ?*Command { }; // Write a NUL byte to ensure that `text` is NUL-terminated - cap.writer.writeByte(0) catch { + cap.writeByte(0) catch { parser.state = .invalid; return null; }; diff --git a/vendor/libghostty-vt/src/terminal/osc/parsers/semantic_prompt.zig b/vendor/libghostty-vt/src/terminal/osc/parsers/semantic_prompt.zig index 97212fbb..e060f8bf 100644 --- a/vendor/libghostty-vt/src/terminal/osc/parsers/semantic_prompt.zig +++ b/vendor/libghostty-vt/src/terminal/osc/parsers/semantic_prompt.zig @@ -1,6 +1,7 @@ //! https://gitlab.freedesktop.org/Per_Bothner/specifications/blob/master/proposals/semantic-prompts.md const std = @import("std"); +const lib = @import("../../lib.zig"); const Parser = @import("../../osc.zig").Parser; const OSCCommand = @import("../../osc.zig").Command; const string_encoding = @import("../../../os/string_encoding.zig"); @@ -68,7 +69,10 @@ pub const Command = struct { // See https://github.com/ghostty-org/ghostty/issues/10865 and // https://github.com/kovidgoyal/kitty/issues/9500 // for further details. -pub const ClickEvents = enum { absolute, relative }; +pub const ClickEvents = lib.Enum( + lib.target, + &.{ "absolute", "relative" }, +); pub const Option = enum { aid, @@ -199,7 +203,7 @@ pub const Option = enum { return switch (self) { .aid => value, - .cl => .init(value), + .cl => parseClick(value), .prompt_kind => if (value.len == 1) PromptKind.init(value[0]) else null, .err => value, .redraw => if (std.mem.eql(u8, value, "0")) @@ -234,42 +238,50 @@ pub const Option = enum { /// The `cl` option specifies what kind of cursor key sequences are handled /// by the application for click-to-move-cursor functionality. -pub const Click = enum { - /// Value: "line". Allows motion within a single input line using standard - /// left/right arrow escape sequences. Only a single left/right sequence - /// should be emitted for double-width characters. - line, - - /// Value: "m". Allows movement between different lines in the same group, - /// but only using left/right arrow escape sequences. - multiple, - - /// Value: "v". Like `multiple` but cursor up/down should be used. The - /// terminal should be conservative when moving between lines: move the - /// cursor left to the start of line, emit the needed up/down sequences, - /// then move the cursor right to the clicked destination. - conservative_vertical, - - /// Value: "w". Like `conservative_vertical` but specifies that there are - /// no spurious spaces at the end of the line, and the application editor - /// handles "smart vertical movement" (moving 2 lines up from position 20, - /// where the intermediate line is 15 chars wide and the destination is - /// 18 chars wide, ends at position 18). - smart_vertical, - - pub fn init(value: []const u8) ?Click { - return if (value.len == 1) switch (value[0]) { - 'm' => .multiple, - 'v' => .conservative_vertical, - 'w' => .smart_vertical, - else => null, - } else if (std.mem.eql( - u8, - value, - "line", - )) .line else null; - } -}; +/// +/// `line` allows movement within one input line. `multiple` allows movement +/// across lines with left/right sequences. The two vertical modes additionally +/// allow up/down sequences, with `smart_vertical` permitting editor-aware +/// column clamping. +pub const Click = lib.Enum( + lib.target, + &.{ + // Value: "line". Allows motion within a single input line using + // standard left/right arrow escape sequences. Only a single left/right + // sequence should be emitted for double-width characters. + "line", + + // Value: "m". Allows movement between different lines in the same + // group, but only using left/right arrow escape sequences. + "multiple", + + // Value: "v". Like `multiple` but cursor up/down should be used. The + // terminal should be conservative when moving between lines: move the + // cursor left to the start of line, emit the needed up/down sequences, + // then move the cursor right to the clicked destination. + "conservative_vertical", + + // Value: "w". Like `conservative_vertical` but specifies that there + // are no spurious spaces at the end of the line, and the application + // editor handles "smart vertical movement" (moving 2 lines up from + // position 20, where the intermediate line is 15 chars wide and the + // destination is 18 chars wide, ends at position 18). + "smart_vertical", + }, +); + +fn parseClick(value: []const u8) ?Click { + return if (value.len == 1) switch (value[0]) { + 'm' => .multiple, + 'v' => .conservative_vertical, + 'w' => .smart_vertical, + else => null, + } else if (std.mem.eql( + u8, + value, + "line", + )) .line else null; +} pub const PromptKind = enum { initial, diff --git a/vendor/libghostty-vt/src/terminal/page.zig b/vendor/libghostty-vt/src/terminal/page.zig index ca6d9854..ad2b1a7e 100644 --- a/vendor/libghostty-vt/src/terminal/page.zig +++ b/vendor/libghostty-vt/src/terminal/page.zig @@ -1,12 +1,13 @@ const std = @import("std"); const builtin = @import("builtin"); const build_options = @import("terminal_options"); +const lib = @import("../lib/main.zig"); const Allocator = std.mem.Allocator; const ArenaAllocator = std.heap.ArenaAllocator; const assert = @import("../quirks.zig").inlineAssert; const testing = std.testing; const posix = std.posix; -const windows = std.os.windows; +const windows = @import("../os/windows.zig"); const fastmem = @import("../fastmem.zig"); const color = @import("color.zig"); const hyperlink = @import("hyperlink.zig"); @@ -23,7 +24,6 @@ const BitmapAllocator = @import("bitmap_allocator.zig").BitmapAllocator; const hash_map = @import("hash_map.zig"); const AutoOffsetHashMap = hash_map.AutoOffsetHashMap; const alignForward = std.mem.alignForward; -const alignBackward = std.mem.alignBackward; const log = std.log.scoped(.page); @@ -43,7 +43,7 @@ const AllocPosix = struct { return try posix.mmap( null, n, - posix.PROT.READ | posix.PROT.WRITE, + .{ .READ = true, .WRITE = true }, .{ .TYPE = .PRIVATE, .ANONYMOUS = true }, -1, 0, @@ -59,12 +59,12 @@ const AllocPosix = struct { /// MEM_COMMIT | MEM_RESERVE which guarantees zeroed pages. const AllocWindows = struct { pub fn alloc(n: usize) error{OutOfMemory}![]align(std.heap.page_size_min) u8 { - const addr = windows.VirtualAlloc( + const addr = windows.exp.kernel32.VirtualAlloc( null, n, windows.MEM_COMMIT | windows.MEM_RESERVE, windows.PAGE_READWRITE, - ) catch return error.OutOfMemory; + ) orelse return error.OutOfMemory; return @as( [*]align(std.heap.page_size_min) u8, @@ -73,7 +73,7 @@ const AllocWindows = struct { } pub fn free(mem: []align(std.heap.page_size_min) u8) void { - windows.VirtualFree( + _ = windows.exp.kernel32.VirtualFree( @ptrCast(@alignCast(mem.ptr)), 0, windows.MEM_RELEASE, @@ -89,6 +89,7 @@ const AllocWindows = struct { /// for alignment. const grapheme_chunk_len = 4; const grapheme_chunk = grapheme_chunk_len * @sizeOf(u21); +pub const grapheme_max_len = 64; const GraphemeAlloc = BitmapAllocator(grapheme_chunk); const grapheme_count_default = GraphemeAlloc.bitmap_bit_size; pub const grapheme_bytes_default = grapheme_count_default * grapheme_chunk; @@ -125,6 +126,19 @@ const hyperlink_count_default = 4; const hyperlink_bytes_default = hyperlink_count_default * @sizeOf(hyperlink.Set.Item); const hyperlink_cell_multiplier = 16; +/// The alignment of the start of a page's cell array. Align it to a cache +/// line so that row cells always start on a cache line. This avoids +/// a scenario where cells in every row always start mid-cache line and +/// straddle an extra. +const cells_align: usize = @max( + @alignOf(Cell), + @min( + std.atomic.cache_line, + // Cap it at page size for freestanding targets. + std.heap.page_size_min, + ), +); + /// A page represents a specific section of terminal screen. The primary /// idea of a page is that it is a fully self-contained unit that can be /// serialized, copied, etc. as a convenient way to represent a section @@ -146,18 +160,9 @@ pub const Page = struct { // alignment is always divisible by this. assert(std.heap.page_size_min % @max( @alignOf(Row), - @alignOf(Cell), - StyleSet.base_align.toByteUnits(), + cells_align, + MetaLayout.alignment, ) == 0); - - // The PageList memory pool requires that initBuf overwrites at - // least the first pointer-size bytes of the backing buffer: - // std.heap.MemoryPool stores its free list node there when a - // page buffer is returned to it, and pool reuse skips zeroing - // in release builds. This holds because the rows array is at - // offset 0 (see layout), a page always has at least one row, - // and initBuf fully rewrites every row. - assert(@sizeOf(Row) >= @sizeOf(usize)); } /// The backing memory for the page. A page is always made up of a @@ -249,13 +254,16 @@ pub const Page = struct { /// Initialize a new page using the given backing memory. /// It is up to the caller to not call deinit on these pages. + /// + /// The backing memory must be zero-filled. A page treats zero as the + /// empty state everywhere: cells are blank when zero, and every + /// metadata member initializes from zeroed memory without writing + /// anything. Only the row headers are written, so the OS pages behind + /// everything else stay untouched until first use. pub inline fn initBuf(buf: OffsetBuf, l: Layout) Page { const cap = l.capacity; - // A page must always have at least one row. Aside from being - // useless otherwise, the row initialization below must always - // overwrite the start of the buffer for pool reuse. See the - // comptime assert at the top of Page. + // A page must always have at least one row. assert(cap.rows > 0); const rows = buf.member(Row, l.rows_start); @@ -277,28 +285,28 @@ pub const Page = struct { .memory = @alignCast(buf.start()[0..l.total_size]), .rows = rows, .cells = cells, - .styles = StyleSet.init( + .styles = StyleSet.initAssumeZeroed( buf.add(l.styles_start), l.styles_layout, .{}, ), - .string_alloc = .init( + .string_alloc = .initAssumeZeroed( buf.add(l.string_alloc_start), l.string_alloc_layout, ), - .grapheme_alloc = .init( + .grapheme_alloc = .initAssumeZeroed( buf.add(l.grapheme_alloc_start), l.grapheme_alloc_layout, ), - .grapheme_map = .init( + .grapheme_map = .initAssumeZeroed( buf.add(l.grapheme_map_start), l.grapheme_map_layout, ), - .hyperlink_map = .init( + .hyperlink_map = .initAssumeZeroed( buf.add(l.hyperlink_map_start), l.hyperlink_map_layout, ), - .hyperlink_set = .init( + .hyperlink_set = .initAssumeZeroed( buf.add(l.hyperlink_set_start), l.hyperlink_set_layout, .{}, @@ -358,11 +366,11 @@ pub const Page = struct { } } - /// A helper that can be used to assert the integrity of the page - /// when runtime safety is enabled. This is a no-op when runtime - /// safety is disabled. This uses the libc allocator. + /// A helper that can be used to assert the integrity of the page when + /// runtime safety is enabled. This is a no-op when runtime safety is + /// disabled or the target is freestanding. This uses the libc allocator. pub inline fn assertIntegrity(self: *const Page) void { - if (comptime build_options.slow_runtime_safety) { + if (comptime build_options.slow_runtime_safety and builtin.os.tag != .freestanding) { var debug_allocator: std.heap.DebugAllocator(.{}) = .init; defer _ = debug_allocator.deinit(); const alloc = debug_allocator.allocator(); @@ -1289,6 +1297,18 @@ pub const Page = struct { @memset(@as([]u64, @ptrCast(cells)), 0); } + /// Reset the given row to the default state: all cells zeroed and + /// all row metadata reset, as if the row was never used. This + /// reclaims memory used by graphemes, styles, etc. like clearCells. + /// + /// This must be used instead of clearCells whenever a row's storage + /// is recycled. Clearing the cells alone is not enough because row + /// metadata such as the wrap state and semantic prompt remain. + pub inline fn resetRow(self: *Page, row: *Row) void { + self.clearCells(row, 0, self.size.cols); + row.reset(); + } + /// Returns the hyperlink ID for the given cell. pub inline fn lookupHyperlink(self: *const Page, cell: *const Cell) ?hyperlink.Id { const cell_offset = getOffset(Cell, self.memory, cell); @@ -1502,7 +1522,8 @@ pub const Page = struct { } /// Set the graphemes for the given cell. This asserts that the cell - /// has no graphemes set, and only contains a single codepoint. + /// has no graphemes set, and only contains a single codepoint. Input + /// beyond grapheme_max_len is ignored. pub inline fn setGraphemes( self: *Page, row: *Row, @@ -1516,14 +1537,15 @@ pub const Page = struct { const cell_offset = getOffset(Cell, self.memory, cell); var map = self.grapheme_map.map(self.memory); + const stored_cps = cps[0..@min(cps.len, grapheme_max_len)]; - const slice = self.grapheme_alloc.alloc(u21, self.memory, cps.len) catch |e| { + const slice = self.grapheme_alloc.alloc(u21, self.memory, stored_cps.len) catch |e| { comptime assert(@TypeOf(e) == error{OutOfMemory}); // The grapheme alloc capacity needs to be increased. return error.GraphemeAllocOutOfMemory; }; errdefer self.grapheme_alloc.free(self.memory, slice); - @memcpy(slice, cps); + @memcpy(slice, stored_cps); map.putNoClobber(cell_offset, .{ .offset = getOffset(u21, self.memory, @ptrCast(slice.ptr)), @@ -1541,7 +1563,8 @@ pub const Page = struct { return; } - /// Append a codepoint to the given cell as a grapheme. + /// Append a codepoint to the given cell as a grapheme. Once the cell has + /// grapheme_max_len suffix codepoints, additional codepoints are ignored. pub fn appendGrapheme(self: *Page, row: *Row, cell: *Cell, cp: u21) Allocator.Error!void { defer self.assertIntegrity(); @@ -1575,6 +1598,10 @@ pub const Page = struct { const slice = map.getPtr(cell_offset).?; + // Terminal input is untrusted. In addition to bounding memory, this + // prevents repeated chunk growth and copying from becoming quadratic. + if (slice.len >= grapheme_max_len) return; + // If our slice len doesn't divide evenly by the grapheme chunk // length then we can utilize the additional chunk space. if (slice.len % grapheme_chunk_len != 0) { @@ -1721,61 +1748,32 @@ pub const Page = struct { /// The memory layout for a page given a desired minimum cols /// and rows size. + /// + /// The backing memory is laid out as the row headers, the cell + /// array, and then the metadata block (see `MetaLayout`): + /// + /// [rows][cells][styles, graphemes, strings, hyperlinks] + /// + /// Row headers always start at offset zero. The cell array is aligned + /// to a cache line (see `cells_align`). Initializing a page writes only + /// the row headers: the cells and every metadata member treat zero as + /// their empty state, so the OS pages behind everything past the row + /// headers stay untouched until something is stored in them. pub inline fn layout(cap: Capacity) Layout { - const rows_count: usize = @intCast(cap.rows); + const meta: MetaLayout = .init(cap); - // The rows array must stay at offset 0: the PageList memory - // pool relies on initBuf overwriting the first bytes of a - // reused page buffer, which hold the pool's free list node. - // See the comptime assert at the top of Page. + const rows_count: usize = @intCast(cap.rows); const rows_start = 0; const rows_end: usize = rows_start + (rows_count * @sizeOf(Row)); const cells_count: usize = @as(usize, cap.cols) * @as(usize, cap.rows); - const cells_start = alignForward(usize, rows_end, @alignOf(Cell)); + const cells_start = alignForward(usize, rows_end, cells_align); const cells_end = cells_start + (cells_count * @sizeOf(Cell)); - const styles_layout: StyleSet.Layout = .init(cap.styles); - const styles_start = alignForward(usize, cells_end, StyleSet.base_align.toByteUnits()); - const styles_end = styles_start + styles_layout.total_size; - - const grapheme_alloc_layout = GraphemeAlloc.layout(cap.grapheme_bytes); - const grapheme_alloc_start = alignForward(usize, styles_end, GraphemeAlloc.base_align.toByteUnits()); - const grapheme_alloc_end = grapheme_alloc_start + grapheme_alloc_layout.total_size; + const meta_start = alignForward(usize, cells_end, MetaLayout.alignment); + const meta_end = meta_start + meta.total_size; - const grapheme_count: usize = count: { - if (cap.grapheme_bytes == 0) break :count 0; - // Use divCeil to match GraphemeAlloc.layout() which uses alignForward, - // ensuring grapheme_map has capacity when grapheme_alloc has chunks. - const base = std.math.divCeil(usize, cap.grapheme_bytes, grapheme_chunk) catch unreachable; - break :count std.math.ceilPowerOfTwo(usize, base) catch unreachable; - }; - const grapheme_map_layout = GraphemeMap.layout(@intCast(grapheme_count)); - const grapheme_map_start = alignForward(usize, grapheme_alloc_end, GraphemeMap.base_align.toByteUnits()); - const grapheme_map_end = grapheme_map_start + grapheme_map_layout.total_size; - - const string_layout = StringAlloc.layout(cap.string_bytes); - const string_start = alignForward(usize, grapheme_map_end, StringAlloc.base_align.toByteUnits()); - const string_end = string_start + string_layout.total_size; - - const hyperlink_count = @divFloor(cap.hyperlink_bytes, @sizeOf(hyperlink.Set.Item)); - const hyperlink_set_layout: hyperlink.Set.Layout = .init(@intCast(hyperlink_count)); - const hyperlink_set_start = alignForward(usize, string_end, hyperlink.Set.base_align.toByteUnits()); - const hyperlink_set_end = hyperlink_set_start + hyperlink_set_layout.total_size; - - const hyperlink_map_count: u32 = count: { - if (hyperlink_count == 0) break :count 0; - const mult = std.math.cast( - u32, - hyperlink_count * hyperlink_cell_multiplier, - ) orelse break :count std.math.maxInt(u32); - break :count mult; - }; - const hyperlink_map_layout = hyperlink.Map.layout(hyperlink_map_count); - const hyperlink_map_start = alignForward(usize, hyperlink_set_end, hyperlink.Map.base_align.toByteUnits()); - const hyperlink_map_end = hyperlink_map_start + hyperlink_map_layout.total_size; - - const total_size = alignForward(usize, hyperlink_map_end, std.heap.page_size_min); + const total_size = alignForward(usize, meta_end, std.heap.page_size_min); return .{ .total_size = total_size, @@ -1783,21 +1781,111 @@ pub const Page = struct { .rows_size = rows_end - rows_start, .cells_start = cells_start, .cells_size = cells_end - cells_start, - .styles_start = styles_start, - .styles_layout = styles_layout, - .grapheme_alloc_start = grapheme_alloc_start, - .grapheme_alloc_layout = grapheme_alloc_layout, - .grapheme_map_start = grapheme_map_start, - .grapheme_map_layout = grapheme_map_layout, - .string_alloc_start = string_start, - .string_alloc_layout = string_layout, - .hyperlink_map_start = hyperlink_map_start, - .hyperlink_map_layout = hyperlink_map_layout, - .hyperlink_set_start = hyperlink_set_start, - .hyperlink_set_layout = hyperlink_set_layout, + .styles_start = meta_start + meta.styles_start, + .styles_layout = meta.styles_layout, + .grapheme_alloc_start = meta_start + meta.grapheme_alloc_start, + .grapheme_alloc_layout = meta.grapheme_alloc_layout, + .grapheme_map_start = meta_start + meta.grapheme_map_start, + .grapheme_map_layout = meta.grapheme_map_layout, + .string_alloc_start = meta_start + meta.string_alloc_start, + .string_alloc_layout = meta.string_alloc_layout, + .hyperlink_map_start = meta_start + meta.hyperlink_map_start, + .hyperlink_map_layout = meta.hyperlink_map_layout, + .hyperlink_set_start = meta_start + meta.hyperlink_set_start, + .hyperlink_set_layout = meta.hyperlink_set_layout, .capacity = cap, }; } + + /// Meta is everything that isn't the grid, such as styles, graphemes, + /// etc. `layout` places it directly after the grid. + pub const MetaLayout = struct { + /// The size of the block including internal alignment padding. + total_size: usize, + styles_start: usize, + styles_layout: StyleSet.Layout, + grapheme_alloc_start: usize, + grapheme_alloc_layout: GraphemeAlloc.Layout, + grapheme_map_start: usize, + grapheme_map_layout: GraphemeMap.Layout, + string_alloc_start: usize, + string_alloc_layout: StringAlloc.Layout, + hyperlink_set_start: usize, + hyperlink_set_layout: hyperlink.Set.Layout, + hyperlink_map_start: usize, + hyperlink_map_layout: hyperlink.Map.Layout, + + /// The alignment of the block's start: the largest alignment any + /// member requires, so that the member offsets above don't depend + /// on where the block is placed. + pub const alignment = @max( + StyleSet.base_align.toByteUnits(), + GraphemeAlloc.base_align.toByteUnits(), + GraphemeMap.base_align.toByteUnits(), + StringAlloc.base_align.toByteUnits(), + hyperlink.Set.base_align.toByteUnits(), + hyperlink.Map.base_align.toByteUnits(), + ); + + /// Compute the layout of the block for the given capacity. + pub fn init(cap: Capacity) MetaLayout { + const styles_layout: StyleSet.Layout = .init(cap.styles); + const styles_start = 0; + const styles_end = styles_start + styles_layout.total_size; + + const grapheme_alloc_layout = GraphemeAlloc.layout(cap.grapheme_bytes); + const grapheme_alloc_start = alignForward(usize, styles_end, GraphemeAlloc.base_align.toByteUnits()); + const grapheme_alloc_end = grapheme_alloc_start + grapheme_alloc_layout.total_size; + + const grapheme_count: usize = count: { + if (cap.grapheme_bytes == 0) break :count 0; + // Use divCeil to match GraphemeAlloc.layout() which uses alignForward, + // ensuring grapheme_map has capacity when grapheme_alloc has chunks. + const base = std.math.divCeil(usize, cap.grapheme_bytes, grapheme_chunk) catch unreachable; + break :count std.math.ceilPowerOfTwo(usize, base) catch unreachable; + }; + const grapheme_map_layout = GraphemeMap.layout(@intCast(grapheme_count)); + const grapheme_map_start = alignForward(usize, grapheme_alloc_end, GraphemeMap.base_align.toByteUnits()); + const grapheme_map_end = grapheme_map_start + grapheme_map_layout.total_size; + + const string_layout = StringAlloc.layout(cap.string_bytes); + const string_start = alignForward(usize, grapheme_map_end, StringAlloc.base_align.toByteUnits()); + const string_end = string_start + string_layout.total_size; + + const hyperlink_count = @divFloor(cap.hyperlink_bytes, @sizeOf(hyperlink.Set.Item)); + const hyperlink_set_layout: hyperlink.Set.Layout = .init(@intCast(hyperlink_count)); + const hyperlink_set_start = alignForward(usize, string_end, hyperlink.Set.base_align.toByteUnits()); + const hyperlink_set_end = hyperlink_set_start + hyperlink_set_layout.total_size; + + const hyperlink_map_count: u32 = count: { + if (hyperlink_count == 0) break :count 0; + const mult = std.math.cast( + u32, + hyperlink_count * hyperlink_cell_multiplier, + ) orelse break :count std.math.maxInt(u32); + break :count mult; + }; + const hyperlink_map_layout = hyperlink.Map.layout(hyperlink_map_count); + const hyperlink_map_start = alignForward(usize, hyperlink_set_end, hyperlink.Map.base_align.toByteUnits()); + const hyperlink_map_end = hyperlink_map_start + hyperlink_map_layout.total_size; + + return .{ + .total_size = hyperlink_map_end, + .styles_start = styles_start, + .styles_layout = styles_layout, + .grapheme_alloc_start = grapheme_alloc_start, + .grapheme_alloc_layout = grapheme_alloc_layout, + .grapheme_map_start = grapheme_map_start, + .grapheme_map_layout = grapheme_map_layout, + .string_alloc_start = string_start, + .string_alloc_layout = string_layout, + .hyperlink_set_start = hyperlink_set_start, + .hyperlink_set_layout = hyperlink_set_layout, + .hyperlink_map_start = hyperlink_map_start, + .hyperlink_map_layout = hyperlink_map_layout, + }; + } + }; }; /// The standard capacity for a page that doesn't have special @@ -1859,15 +1947,16 @@ pub const Capacity = struct { /// the amount of memory the original capacity will take. If you modify /// the original capacity to add rows, then you can fit more columns. pub fn maxCols(self: Capacity) ?size.CellCountInt { - const available_bits = self.availableBitsForGrid(); + const available = self.availableBytesForGrid(); - // If we can't even fit the row metadata, return null - if (available_bits <= @bitSizeOf(Row)) return null; + // A single row's header occupies a whole cell-aligned region + // ahead of the cells. If we can't even fit that, return null. + const row_region = alignForward(usize, @sizeOf(Row), cells_align); + if (available <= row_region) return null; // We do the math of how many columns we can fit in the remaining - // bits ignoring the metadata of a row. - const remaining_bits = available_bits - @bitSizeOf(Row); - const max_cols = remaining_bits / @bitSizeOf(Cell); + // bytes ignoring the metadata of a row. + const max_cols = (available - row_region) / @sizeOf(Cell); // Clamp to CellCountInt max return @min(std.math.maxInt(size.CellCountInt), max_cols); @@ -1881,51 +1970,44 @@ pub const Capacity = struct { pub fn adjust(self: Capacity, req: Adjustment) Allocator.Error!Capacity { var adjusted = self; if (req.cols) |cols| { - const available_bits = self.availableBitsForGrid(); + const total_size = Page.layout(self).total_size; + const available = self.availableBytesForGrid(); // The size per row is: // - The row metadata itself // - The cells per row (n=cols) - const bits_per_row: usize = @bitSizeOf(Row) + @bitSizeOf(Cell) * @as(usize, @intCast(cols)); - const new_rows: usize = @divFloor(available_bits, bits_per_row); + const bytes_per_row: usize = @sizeOf(Row) + @sizeOf(Cell) * @as(usize, @intCast(cols)); + var new_rows: usize = @divFloor(available, bytes_per_row); + + // The cell array is aligned to a cache line, so the padding + // between the row headers and the cells depends on the row + // count. Trim rows until the layout fits the original size. + // The padding is less than a cache line so this takes a + // handful of iterations at most. + adjusted.cols = cols; + while (new_rows > 0) : (new_rows -= 1) { + adjusted.rows = @intCast(new_rows); + if (Page.layout(adjusted).total_size <= total_size) break; + } // If our rows go to zero then we can't fit any row metadata // for the desired number of columns. if (new_rows == 0) return error.OutOfMemory; - - adjusted.cols = cols; - adjusted.rows = @intCast(new_rows); } return adjusted; } - /// Computes the number of bits available for rows and cells in the page. - /// - /// This is done by laying out the "meta" members (styles, graphemes, - /// hyperlinks, strings) from the end of the page and finding where they - /// start, which gives us the space available for rows and cells. - fn availableBitsForGrid(self: Capacity) usize { - // The math below only works if there is no alignment gap between - // the end of the rows array and the start of the cells array. - // - // To guarantee this, we assert that Row's size is a multiple of - // Cell's alignment, so that any length array of Rows will end on - // a valid alignment for the start of the Cell array. - assert(@sizeOf(Row) % @alignOf(Cell) == 0); + /// Computes the number of bytes available for the row headers and + /// cells in the page: the page size minus the metadata block. + fn availableBytesForGrid(self: Capacity) usize { + comptime { + assert(cells_align % Page.MetaLayout.alignment == 0); + assert(@sizeOf(Cell) % Page.MetaLayout.alignment == 0); + } const l = Page.layout(self); - - // Layout meta members from the end to find styles_start - const hyperlink_map_start = alignBackward(usize, l.total_size - l.hyperlink_map_layout.total_size, hyperlink.Map.base_align.toByteUnits()); - const hyperlink_set_start = alignBackward(usize, hyperlink_map_start - l.hyperlink_set_layout.total_size, hyperlink.Set.base_align.toByteUnits()); - const string_alloc_start = alignBackward(usize, hyperlink_set_start - l.string_alloc_layout.total_size, StringAlloc.base_align.toByteUnits()); - const grapheme_map_start = alignBackward(usize, string_alloc_start - l.grapheme_map_layout.total_size, GraphemeMap.base_align.toByteUnits()); - const grapheme_alloc_start = alignBackward(usize, grapheme_map_start - l.grapheme_alloc_layout.total_size, GraphemeAlloc.base_align.toByteUnits()); - const styles_start = alignBackward(usize, grapheme_alloc_start - l.styles_layout.total_size, StyleSet.base_align.toByteUnits()); - - // Multiply by 8 to convert bytes to bits - return styles_start * 8; + return l.total_size - Page.MetaLayout.init(self).total_size; } }; @@ -2032,6 +2114,26 @@ pub const Row = packed struct(u64) { // Ordered on purpose for likelihood. return self.styled or self.hyperlink or self.grapheme; } + + /// Reset all row metadata to the default state, preserving only + /// the cells offset, and mark the row dirty. This is a single + /// 8-byte store. + /// + /// This must be applied to any row whose storage is recycled as a + /// blank row or retired into unused page capacity, in addition to + /// clearing its cells (in either order; this doesn't touch cell + /// memory). See Page.resetRow, which does both, for details. This + /// exists separately for callers that clear the cells in a + /// specialized way (e.g. filling with a background-colored blank + /// cell rather than zeroing). + /// + /// Asserts that the row has no managed memory: releasing that is + /// the cell-clearing side's job and must happen while the flags + /// are still accurate. + pub inline fn reset(self: *Row) void { + assert(!self.managedMemory()); + self.* = .{ .cells = self.cells, .dirty = true }; + } }; /// A cell represents a single terminal grid cell. @@ -2048,12 +2150,18 @@ pub const Cell = packed struct(u64) { /// The codepoint that this cell contains. If `grapheme` is false, /// then this is the only codepoint in the cell. If `grapheme` is /// true, then this is the first codepoint in the grapheme cluster. - codepoint: u21, + codepoint: packed struct(u24) { + data: u21, + _pad: u3 = 0, + }, /// The content is an empty cell with a background color. - color_palette: u8, + color_palette: packed struct(u24) { + data: u8, + _pad: u16 = 0, + }, color_rgb: RGB, - } = .{ .codepoint = 0 }, + } = .{ .codepoint = .{ .data = 0 } }, /// The style ID to use for this cell within the style map. Zero /// is always the default style so no lookup is required. @@ -2130,6 +2238,51 @@ pub const Cell = packed struct(u64) { prompt = 2, }; + /// Metadata for the C representation. All physical bit offsets and + /// widths are reflected from Cell. + pub const CLayout = lib.Packed(Cell, .{ .fields = .{ + .content_tag = .{ .type_name = "GhosttyCellContentTag" }, + .content = .{ .encoding = .{ .tagged_union = lib.PackedTaggedUnion( + Cell, + .content, + .content_tag, + .{ .arms = .{ + .codepoint = .{ .codepoint = lib.Packed( + @FieldType(@FieldType(Cell, "content"), "codepoint"), + .{ .fields = .{ + .data = .{ .name = "codepoint" }, + ._pad = .{ .omit = true }, + } }, + ) }, + .codepoint_grapheme = .{ .codepoint = lib.Packed( + @FieldType(@FieldType(Cell, "content"), "codepoint"), + .{ .fields = .{ + .data = .{ .name = "codepoint" }, + ._pad = .{ .omit = true }, + } }, + ) }, + .bg_color_palette = .{ .color_palette = lib.Packed( + @FieldType(@FieldType(Cell, "content"), "color_palette"), + .{ .fields = .{ + .data = .{ + .name = "index", + .type_name = "GhosttyColorPaletteIndex", + }, + ._pad = .{ .omit = true }, + } }, + ) }, + .bg_color_rgb = .{ .color_rgb = lib.Packed( + @FieldType(@FieldType(Cell, "content"), "color_rgb"), + .{}, + ) }, + } }, + ) } }, + .style_id = .{ .type_name = "GhosttyStyleId" }, + .wide = .{ .type_name = "GhosttyCellWide" }, + .semantic_content = .{ .type_name = "GhosttyCellSemanticContent" }, + ._padding = .{ .omit = true }, + } }); + /// The backing integer of this packed struct. Prefer this over /// hardcoding the integer type so that code is resilient to the /// size changing. @@ -2150,7 +2303,7 @@ pub const Cell = packed struct(u64) { // memory in the packed union. Valgrind verifies this. var cell: Cell = @bitCast(@as(u64, 0)); cell.content_tag = .codepoint; - cell.content = .{ .codepoint = cp }; + cell.content = .{ .codepoint = .{ .data = cp } }; return cell; } @@ -2168,7 +2321,7 @@ pub const Cell = packed struct(u64) { return switch (self.content_tag) { .codepoint, .codepoint_grapheme, - => self.content.codepoint != 0, + => self.content.codepoint.data != 0, .bg_color_palette, .bg_color_rgb, @@ -2180,7 +2333,7 @@ pub const Cell = packed struct(u64) { return switch (self.content_tag) { .codepoint, .codepoint_grapheme, - => self.content.codepoint, + => self.content.codepoint.data, .bg_color_palette, .bg_color_rgb, @@ -2233,6 +2386,12 @@ pub const Cell = packed struct(u64) { /// struct T, used for masked compares of raw backing-integer values /// (e.g. `Row.Backing`, `Cell.Backing`). This is an implementation /// detail of `Mask`, which is the public API built on top of this. +/// +/// A field may be a dot-separated path (e.g. "content.codepoint.data") +/// to cover only a nested field of a packed struct or packed union +/// member. This allows a mask to be more precise than a whole +/// top-level field, e.g. covering the codepoint bits of a cell without +/// its padding. fn fieldMask( comptime T: type, comptime fields: []const []const u8, @@ -2240,16 +2399,33 @@ fn fieldMask( // Backing int of the packed struct const Int = @typeInfo(T).@"struct".backing_integer.?; - var mask: Int = 0; - inline for (fields) |field| { + comptime var mask: Int = 0; + inline for (fields) |path| { + // Walk the path to find the total bit offset and the type of + // the (possibly nested) field. + comptime var offset = 0; + comptime var Field = T; + comptime var it = std.mem.splitScalar(u8, path, '.'); + inline while (comptime it.next()) |name| { + offset += switch (@typeInfo(Field)) { + .@"struct" => @bitOffsetOf(Field, name), + + // Packed union members all share bit offset zero. + .@"union" => |u| offset: { + comptime assert(u.layout == .@"packed"); + break :offset 0; + }, + + else => @compileError("invalid field path: " ++ path), + }; + Field = @FieldType(Field, name); + } + // The type that fits all the bits we need to set. - const Ones = std.meta.Int( - .unsigned, - @bitSizeOf(@FieldType(T, field)), - ); + const Ones = std.meta.Int(.unsigned, @bitSizeOf(Field)); // Mask out the ones - mask |= @as(Int, std.math.maxInt(Ones)) << @bitOffsetOf(T, field); + mask |= @as(Int, std.math.maxInt(Ones)) << offset; } return mask; @@ -2372,6 +2548,21 @@ pub fn Mask( return pattern(v) == expected; } + /// Returns true if any value in the group of group_len values + /// starting at index i has masked fields equal to the expected + /// pattern (see `pattern`). This is the "any" counterpart to + /// `eql`: use it to detect the presence of a specific value + /// within a group, e.g. a run scan that must stop when it + /// encounters a sentinel codepoint anywhere in the group. + pub inline fn eqlAny( + values: []const T, + i: usize, + expected: Backing, + ) bool { + const masked = load(values, i) & @as(Group, @splat(mask)); + return @reduce(.Or, masked == @as(Group, @splat(expected))); + } + /// Like `eql` but returns the number of leading values whose /// masked fields equal the expected pattern, i.e. group_len if /// the entire group matches. This is useful for early-exit run @@ -2491,6 +2682,59 @@ test "Mask" { try testing.expectEqual(M.strip(styled), M.strip(styled_other)); try testing.expect(M.strip(styled) != M.strip(styled2)); } + + // eqlAny: presence of a matching value anywhere in the group + { + const expected = M.pattern(styled); + var cells: [4]Cell = .{ plain, plain, plain, plain }; + try testing.expect(!M.eqlAny(&cells, 0, expected)); + + cells[2] = styled; + try testing.expect(M.eqlAny(&cells, 0, expected)); + + // Masked compare: same masked fields with a different + // codepoint still matches. + cells[2] = styled2; + try testing.expect(M.eqlAny(&cells, 0, expected)); + } +} + +test "Mask nested field path" { + // Mask only the codepoint data bits of the content field, not + // the padding next to it or any other field. + const M = Mask(Cell, &.{"content.codepoint.data"}, 4); + + const a: Cell = .init('A'); + var b: Cell = .init('A'); + b.style_id = 5; + b.wide = .wide; + const c: Cell = .init('C'); + + // Same codepoint matches regardless of other fields. + const expected = M.pattern(a); + try testing.expect(M.eqlScalar(b, expected)); + try testing.expect(!M.eqlScalar(c, expected)); + + // The mask must cover exactly the codepoint data bits. + const cp_offset = @bitOffsetOf(Cell, "content"); + try testing.expectEqual( + @as(u64, std.math.maxInt(u21)) << cp_offset, + comptime fieldMask(Cell, &.{"content.codepoint.data"}), + ); + + // Group variants + { + var cells: [4]Cell = .{ a, b, a, b }; + try testing.expect(M.eql(&cells, 0, expected)); + try testing.expect(M.eqlAny(&cells, 0, expected)); + + cells[1] = c; + try testing.expect(!M.eql(&cells, 0, expected)); + try testing.expect(M.eqlAny(&cells, 0, expected)); + + const none: [4]Cell = .{ c, c, c, c }; + try testing.expect(!M.eqlAny(&none, 0, expected)); + } } // Uncomment this when you want to do some math. @@ -2668,14 +2912,16 @@ test "Page read and write cells" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ + .codepoint = .{ .data = @intCast(y) }, + }, }; } // Read it again for (0..page.capacity.rows) |y| { const rac = page.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } } @@ -2732,6 +2978,49 @@ test "Page appendGrapheme larger than chunk" { } } +test "Page appendGrapheme caps codepoints per cell" { + var page = try Page.init(.{ + .cols = 10, + .rows = 10, + .styles = 8, + }); + defer page.deinit(); + + const rac = page.getRowAndCell(0, 0); + rac.cell.* = .init('A'); + + for (0..grapheme_max_len + 16) |i| { + try page.appendGrapheme(rac.row, rac.cell, @intCast(0x0300 + i)); + } + + const cps = page.lookupGrapheme(rac.cell).?; + try testing.expectEqual(@as(usize, grapheme_max_len), cps.len); + for (0..grapheme_max_len) |i| { + try testing.expectEqual(@as(u21, @intCast(0x0300 + i)), cps[i]); + } +} + +test "Page setGraphemes caps codepoints per cell" { + var page = try Page.init(.{ + .cols = 10, + .rows = 10, + .styles = 8, + }); + defer page.deinit(); + + var input: [grapheme_max_len + 16]u21 = undefined; + for (&input, 0..) |*cp, i| cp.* = @intCast(0x0300 + i); + + const rac = page.getRowAndCell(0, 0); + rac.cell.* = .init('A'); + try page.setGraphemes(rac.row, rac.cell, &input); + + try testing.expectEqual( + @as(usize, grapheme_max_len), + page.lookupGrapheme(rac.cell).?.len, + ); +} + test "Page clearGrapheme not all cells" { var page = try Page.init(.{ .cols = 10, @@ -2769,7 +3058,7 @@ test "Page clone" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -2781,7 +3070,7 @@ test "Page clone" { // Read it again for (0..page2.capacity.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } // Write again @@ -2789,20 +3078,20 @@ test "Page clone" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, }; } // Read it again, should be unchanged for (0..page2.capacity.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } // Read the original for (0..page.capacity.rows) |y| { const rac = page.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); } } @@ -2852,7 +3141,7 @@ test "Page clone styles" { rac.row.styled = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, .style_id = id, }; page.styles.use(page.memory, id); @@ -2897,7 +3186,7 @@ test "Page cloneFrom" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -2913,7 +3202,7 @@ test "Page cloneFrom" { // Read it again for (0..page2.capacity.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } // Write again @@ -2921,20 +3210,20 @@ test "Page cloneFrom" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, }; } // Read it again, should be unchanged for (0..page2.capacity.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } // Read the original for (0..page.capacity.rows) |y| { const rac = page.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); } } @@ -2951,7 +3240,7 @@ test "Page cloneFrom shrink columns" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -2968,7 +3257,7 @@ test "Page cloneFrom shrink columns" { // Read it again for (0..page2.capacity.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } } @@ -2985,7 +3274,7 @@ test "Page cloneFrom partial" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y) }, + .content = .{ .codepoint = .{ .data = @intCast(y) } }, }; } @@ -3001,11 +3290,11 @@ test "Page cloneFrom partial" { // Read it again for (0..5) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y)), rac.cell.content.codepoint.data); } for (5..page2.size.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); } } @@ -3032,7 +3321,7 @@ test "Page cloneFrom hyperlinks exact capacity" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 42 }, + .content = .{ .codepoint = .{ .data = 42 } }, }; try page.setHyperlink(rac.row, rac.cell, hyperlink_id); page.hyperlink_set.use(page.memory, hyperlink_id); @@ -3066,7 +3355,7 @@ test "Page cloneFrom graphemes" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(y + 1) } }, }; try page.appendGrapheme(rac.row, rac.cell, 0x0A); } @@ -3083,7 +3372,7 @@ test "Page cloneFrom graphemes" { // Read it again for (0..page2.capacity.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y + 1)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y + 1)), rac.cell.content.codepoint.data); try testing.expect(rac.row.grapheme); try testing.expect(rac.cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{0x0A}, page2.lookupGrapheme(rac.cell).?); @@ -3096,14 +3385,14 @@ test "Page cloneFrom graphemes" { page.updateRowGraphemeFlag(rac.row); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0 }, + .content = .{ .codepoint = .{ .data = 0 } }, }; } // Read it again, should be unchanged for (0..page2.capacity.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y + 1)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y + 1)), rac.cell.content.codepoint.data); try testing.expect(rac.row.grapheme); try testing.expect(rac.cell.hasGrapheme()); try testing.expectEqualSlices(u21, &.{0x0A}, page2.lookupGrapheme(rac.cell).?); @@ -3112,7 +3401,7 @@ test "Page cloneFrom graphemes" { // Read the original for (0..page.capacity.rows) |y| { const rac = page.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, 0), rac.cell.content.codepoint.data); } } @@ -3127,7 +3416,7 @@ test "Page cloneFrom frees dst graphemes" { const rac = page.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(y + 1) } }, }; } @@ -3142,7 +3431,7 @@ test "Page cloneFrom frees dst graphemes" { const rac = page2.getRowAndCell(1, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(y + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(y + 1) } }, }; try page2.appendGrapheme(rac.row, rac.cell, 0x0A); } @@ -3153,7 +3442,7 @@ test "Page cloneFrom frees dst graphemes" { // Read it again for (0..page2.capacity.rows) |y| { const rac = page2.getRowAndCell(1, y); - try testing.expectEqual(@as(u21, @intCast(y + 1)), rac.cell.content.codepoint); + try testing.expectEqual(@as(u21, @intCast(y + 1)), rac.cell.content.codepoint.data); try testing.expect(!rac.row.grapheme); try testing.expect(!rac.cell.hasGrapheme()); } @@ -3175,7 +3464,7 @@ test "Page cloneRowFrom partial" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; } } @@ -3201,7 +3490,7 @@ test "Page cloneRowFrom partial" { for (0..page2.size.cols) |x| { const expected: u21 = if (x >= 2 and x < 8) @intCast(x + 1) else 0; const rac = page2.getRowAndCell(x, y); - try testing.expectEqual(expected, rac.cell.content.codepoint); + try testing.expectEqual(expected, rac.cell.content.codepoint.data); } } } @@ -3221,7 +3510,7 @@ test "Page cloneRowFrom partial grapheme in non-copied source region" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; } { @@ -3256,7 +3545,7 @@ test "Page cloneRowFrom partial grapheme in non-copied source region" { for (0..page2.size.cols) |x| { const expected: u21 = if (x >= 2 and x < 8) @intCast(x + 1) else 0; const rac = page2.getRowAndCell(x, y); - try testing.expectEqual(expected, rac.cell.content.codepoint); + try testing.expectEqual(expected, rac.cell.content.codepoint.data); try testing.expect(!rac.cell.hasGrapheme()); } { @@ -3282,7 +3571,7 @@ test "Page cloneRowFrom partial grapheme in non-copied dest region" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; } } @@ -3301,7 +3590,7 @@ test "Page cloneRowFrom partial grapheme in non-copied dest region" { const rac = page2.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = 0xBB }, + .content = .{ .codepoint = .{ .data = 0xBB } }, }; } { @@ -3327,7 +3616,7 @@ test "Page cloneRowFrom partial grapheme in non-copied dest region" { for (0..page2.size.cols) |x| { const expected: u21 = if (x >= 2 and x < 8) @intCast(x + 1) else 0xBB; const rac = page2.getRowAndCell(x, y); - try testing.expectEqual(expected, rac.cell.content.codepoint); + try testing.expectEqual(expected, rac.cell.content.codepoint.data); } { const rac = page2.getRowAndCell(9, y); @@ -3355,7 +3644,7 @@ test "Page cloneRowFrom partial hyperlink in same page copy" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; } @@ -3382,7 +3671,7 @@ test "Page cloneRowFrom partial hyperlink in same page copy" { for (0..page.size.cols) |x| { const expected: u21 = if (x >= 2 and x < 8) @intCast(x + 1) else 0; const rac = page.getRowAndCell(x, y); - try testing.expectEqual(expected, rac.cell.content.codepoint); + try testing.expectEqual(expected, rac.cell.content.codepoint.data); } { const rac = page.getRowAndCell(7, y); @@ -3411,7 +3700,7 @@ test "Page cloneRowFrom partial hyperlink in same page omit" { const rac = page.getRowAndCell(x, y); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; } @@ -3438,7 +3727,7 @@ test "Page cloneRowFrom partial hyperlink in same page omit" { for (0..page.size.cols) |x| { const expected: u21 = if (x >= 2 and x < 6) @intCast(x + 1) else 0; const rac = page.getRowAndCell(x, y); - try testing.expectEqual(expected, rac.cell.content.codepoint); + try testing.expectEqual(expected, rac.cell.content.codepoint.data); } { const rac = page.getRowAndCell(7, y); @@ -3462,7 +3751,7 @@ test "Page moveCells text-only" { const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; } @@ -3475,7 +3764,7 @@ test "Page moveCells text-only" { const rac = page.getRowAndCell(x, 1); try testing.expectEqual( @as(u21, @intCast(x + 1)), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, ); } @@ -3484,7 +3773,7 @@ test "Page moveCells text-only" { const rac = page.getRowAndCell(x, 0); try testing.expectEqual( @as(u21, 0), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, ); } } @@ -3502,7 +3791,7 @@ test "Page moveCells graphemes" { const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; try page.appendGrapheme(rac.row, rac.cell, 0x0A); } @@ -3518,7 +3807,7 @@ test "Page moveCells graphemes" { const rac = page.getRowAndCell(x, 1); try testing.expectEqual( @as(u21, @intCast(x + 1)), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, ); try testing.expectEqualSlices( u21, @@ -3532,7 +3821,7 @@ test "Page moveCells graphemes" { const rac = page.getRowAndCell(x, 0); try testing.expectEqual( @as(u21, 0), - rac.cell.content.codepoint, + rac.cell.content.codepoint.data, ); } } @@ -3554,7 +3843,7 @@ test "Page verifyIntegrity graphemes good" { const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; try page.appendGrapheme(rac.row, rac.cell, 0x0A); } @@ -3579,7 +3868,7 @@ test "Page verifyIntegrity grapheme row not marked" { const rac = page.getRowAndCell(x, 0); rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, }; try page.appendGrapheme(rac.row, rac.cell, 0x0A); } @@ -3616,7 +3905,7 @@ test "Page verifyIntegrity styles good" { rac.row.styled = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, .style_id = id, }; page.styles.use(page.memory, id); @@ -3652,7 +3941,7 @@ test "Page verifyIntegrity styles ref count mismatch" { rac.row.styled = true; rac.cell.* = .{ .content_tag = .codepoint, - .content = .{ .codepoint = @intCast(x + 1) }, + .content = .{ .codepoint = .{ .data = @intCast(x + 1) } }, .style_id = id, }; page.styles.use(page.memory, id); diff --git a/vendor/libghostty-vt/src/terminal/parse_table.zig b/vendor/libghostty-vt/src/terminal/parse_table.zig index 01bd569c..fa074fa0 100644 --- a/vendor/libghostty-vt/src/terminal/parse_table.zig +++ b/vendor/libghostty-vt/src/terminal/parse_table.zig @@ -206,6 +206,13 @@ fn genTable() Table { single(&result, 0x19, source, source, .ignore); range(&result, 0, 0x17, source, source, .ignore); range(&result, 0x1C, 0x1F, source, source, .ignore); + + // High bytes are ignored payload data, overriding the + // "anywhere" C1 transitions. See dcs_passthrough below for more. + // In dcs_ignore the additional concern is that a UTF-8 payload in + // an ignored DCS could otherwise begin a live sequence mid-string + // (e.g. 0x9B => csi_entry). + range(&result, 0x80, 0xFF, source, source, .ignore); } // dcs_param @@ -241,6 +248,21 @@ fn genTable() Table { range(&result, 0x1C, 0x1F, source, source, .put); range(&result, 0x20, 0x7E, source, source, .put); single(&result, 0x7F, source, source, .ignore); + + // High bytes are payload data, overriding the "anywhere" C1 + // transitions, matching how osc_string handles them below. + // Ghostty is UTF-8 only, and DCS payloads carry UTF-8 text + // (e.g. tmux control mode pane content): without this, a + // UTF-8 continuation byte in the C1 range terminates or + // corrupts the string and 0xA0-0xFF are silently dropped. + // + // This includes 0x9C (8-bit ST) on purpose: a raw 0x9C is + // indistinguishable from a UTF-8 continuation byte ("Ü" is + // 0xC3 0x9C), and Ghostty doesn't honor 8-bit C1 controls in + // the ground state either (they go through UTF-8 decoding). + // DCS strings terminate via 7-bit ST (ESC \) and abort via + // CAN/SUB, which are unaffected here. + range(&result, 0x80, 0xFF, source, source, .put); } // csi_param @@ -386,3 +408,41 @@ test { // that it succeeds in creation. _ = table; } + +test "dcs_passthrough: high bytes are payload data" { + // Bytes 0x80-0xFF within a DCS string are payload data, not C1 + // controls. This includes 0x9C (8-bit ST): a raw 0x9C is + // indistinguishable from a UTF-8 continuation byte (e.g. "Ü" is + // 0xC3 0x9C) and Ghostty doesn't support 8-bit C1 controls + // anywhere else. + for (0x80..0x100) |c| { + const entry = table[c][@intFromEnum(State.dcs_passthrough)]; + try std.testing.expectEqual(State.dcs_passthrough, entry.state); + try std.testing.expectEqual(Action.put, entry.action); + } +} + +test "dcs_ignore: high bytes are ignored payload data" { + // Same as dcs_passthrough: a UTF-8 payload inside an ignored DCS + // must not trigger "anywhere" C1 transitions (e.g. 0x9B beginning + // a CSI mid-string). + for (0x80..0x100) |c| { + const entry = table[c][@intFromEnum(State.dcs_ignore)]; + try std.testing.expectEqual(State.dcs_ignore, entry.state); + try std.testing.expectEqual(Action.ignore, entry.action); + } +} + +test "dcs_passthrough: ESC, CAN, and SUB still exit" { + // 7-bit ST (ESC \) is the DCS terminator and CAN/SUB abort, so + // these must continue to leave dcs_passthrough. dcs_unhook is + // emitted by the parser on any transition out of dcs_passthrough. + const esc = table[0x1B][@intFromEnum(State.dcs_passthrough)]; + try std.testing.expectEqual(State.escape, esc.state); + + const can = table[0x18][@intFromEnum(State.dcs_passthrough)]; + try std.testing.expectEqual(State.ground, can.state); + + const sub = table[0x1A][@intFromEnum(State.dcs_passthrough)]; + try std.testing.expectEqual(State.ground, sub.state); +} diff --git a/vendor/libghostty-vt/src/terminal/paste.zig b/vendor/libghostty-vt/src/terminal/paste.zig new file mode 100644 index 00000000..05e088ee --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/paste.zig @@ -0,0 +1,247 @@ +//! Pasting into a terminal. +//! +//! This is the single place that turns "the user pasted" into bytes for +//! the pty, applying the terminal's current state: +//! +//! * Mode 5522 (Kitty clipboard protocol paste events) set, a +//! user-initiated clipboard paste, and the embedder able to serve +//! the program's follow-up clipboard read: send a paste event +//! listing the clipboard's MIME types with a fresh one-time password +//! and record a one-time read grant for it. No data is read. +//! * Otherwise: write the first text representation, with unsafe bytes +//! replaced (xterm behavior), framed per mode 2004 (bracketed paste) +//! or with newlines converted to carriage returns if not. +//! +//! The precedence (5522 event, else 2004 framing, else plain) and the +//! safety rule live only here so every embedder of the terminal shares +//! one implementation. + +const std = @import("std"); +const Allocator = std.mem.Allocator; + +const clipboard = @import("clipboard.zig"); +const kitty_clipboard = @import("kitty/clipboard.zig"); +const input_paste = @import("../input/paste.zig"); +const Terminal = @import("Terminal.zig"); + +/// Why a paste happened. Only clipboard pastes may become paste +/// events, which is why only they carry a location: it's meaningless +/// for text insertion. +/// +/// C: GhosttyPasteSource, flattened next to the location since C has +/// no tagged unions. +pub const Source = union(enum) { + /// The user pasted from a clipboard: keybind, menu, middle click. + /// The payload is the clipboard the contents came from. + clipboard: clipboard.Location, + + /// Text inserted some other way: IME commit, drag and drop, + /// scripted input. + text, +}; + +/// A paste of clipboard contents into the terminal. What actually gets +/// written depends on terminal state; see `paste`. +pub const Request = struct { + /// Why this paste happened, and for a clipboard paste, from which + /// clipboard. Only a user-initiated clipboard paste may become a + /// paste event; text insertion always writes text. + source: Source = .{ .clipboard = .standard }, + + /// The representations available. Borrowed only during the + /// duration of the paste function call. + contents: Contents, + + /// Write data that could inject commands (see `paste`). The usual + /// flow is to call with false, confirm with the user on + /// error.UnsafePaste, and call again with true. + allow_unsafe: bool = false, +}; + +/// The representations available for a paste, in the embedder's +/// preferred order. +pub const Contents = union(enum) { + /// Every representation already in memory. For text the embedder + /// holds anyway (an IME commit, dropped text) or small clipboards. + memory: []const clipboard.Content, + + /// Representations read on demand, so nothing is loaded that isn't + /// pasted. This is the form for a real clipboard, whose non-text + /// items may be huge. + reader: Reader, + + /// The on-demand form: the MIME types available plus the reader + /// that produces the data of any one of them. + pub const Reader = struct { + /// The MIME types available, in preferred order. + mimes: []const []const u8, + + /// Produces the data of any entry of `mimes`, which is passed + /// through to it as is. A paste reads at most once: the text + /// representation being pasted, never anything else and never + /// anything for a paste event. There is no requirement across + /// paste calls, so a source that changes between an unsafe + /// refusal and the embedder's confirmed retry simply pastes + /// its current contents. + read: clipboard.MimeReader, + }; + + /// The number of representations. + pub fn len(self: Contents) usize { + return switch (self) { + .memory => |v| v.len, + .reader => |v| v.mimes.len, + }; + } + + /// The MIME type of representation `index`. + pub fn mime(self: Contents, index: usize) []const u8 { + return switch (self) { + .memory => |v| v[index].mime, + .reader => |v| v.mimes[index], + }; + } +}; + +/// What a caller supplies to `paste`: the terminal state the decision +/// depends on, the session state an event records into, and the sink. +pub const Context = struct { + /// The terminal whose modes decide the encoding. + terminal: *const Terminal, + + /// Allocator for transient state: the buffered read and a paste + /// event's grant. Must be the allocator `kitty_clipboard.grants` + /// is freed with. + alloc: Allocator, + + /// Receives the encoded text in chunks, or the event. Must be + /// buffered (the encoder works in its buffer) and is not flushed + /// by `paste`; the caller flushes once it returns. + writer: *std.Io.Writer, + + /// Kitty clipboard protocol session state, or null if the embedder + /// does not serve Kitty clipboard reads (`clipboard.Read`). A + /// paste event is that protocol: it is only useful if the + /// program's follow-up read can be answered, since otherwise the + /// read would be refused and the user's paste would vanish. With + /// null, `paste` always writes text. + kitty_clipboard: ?KittyClipboard, + + pub const KittyClipboard = struct { + /// Session grants. A paste event records its one-time password + /// here so the program's follow-up read is served without a + /// prompt. + grants: *kitty_clipboard.Grants, + + /// Secure entropy for one-time passwords. See generateOtp for + /// why there is no fallback when this has none. + io: std.Io, + }; +}; + +pub const Error = Allocator.Error || + std.Io.RandomSecureError || + clipboard.MimeReader.Error || + error{ + /// The data could inject commands and allow_unsafe was false. + UnsafePaste, + }; + +/// Paste into the terminal, applying the terminal's current state as +/// described in the module docs. Returns true if anything was written +/// to `ctx.writer`: the encoded text or a paste event. False means +/// there was nothing to paste (no non-empty text representation). +/// +/// The safety rule for a text paste (`input.paste.isSafeWith`): a +/// bracketed paste is unsafe only if it contains the bracket terminator +/// (CSI 201~); an unbracketed paste is unsafe if it contains a newline +/// or the terminator. Embedders wanting a stricter rule check +/// `input.paste.isSafe` themselves before calling. A paste event never +/// puts the data on the input stream, so the rule doesn't apply to it. +/// +/// The contents are read at most once per call and buffered whole, so +/// the source needs no stability across reads. Nothing reaches the +/// writer until the read completed and the text passed the rule: +/// every error writes nothing, and a failed event records no grant. +pub fn paste(ctx: Context, req: Request) Error!bool { + // If the source is a clipboard and mode 5522 is enabled and + // the caller can handle kitty events, then do a kitty event. + if (req.source == .clipboard and + ctx.terminal.modes.get(.kitty_paste_events)) + { + if (ctx.kitty_clipboard) |kitty| { + try pasteKittyEvent(ctx, kitty, req); + return true; + } + } + + // For non-Kitty paste events we can only accept text content. + const index: usize = for (0..req.contents.len()) |i| { + if (clipboard.isTextMime(req.contents.mime(i))) break i; + } else return false; + + const opts: input_paste.Options = .fromTerminal(ctx.terminal); + + // In-memory contents are used directly to avoid a double copy but + // reader-based contents are read into memory so we can do the unsafe + // scan. + var aw: std.Io.Writer.Allocating = .init(ctx.alloc); + defer aw.deinit(); + const text: []const u8 = switch (req.contents) { + .memory => |v| v[index].data, + .reader => |v| text: { + v.read.read(v.mimes[index], &aw.writer) catch |err| switch (err) { + // An allocating writer only fails to allocate. + error.WriteFailed => return error.OutOfMemory, + error.ReadFailed => |e| return e, + }; + break :text aw.writer.buffered(); + }, + }; + if (text.len == 0) return false; + if (!req.allow_unsafe and !input_paste.isSafeWith( + text, + opts, + )) return error.UnsafePaste; + + // The text is copied exactly once per chunk, into the writer, + // where the encoder strips and converts it in place. + try input_paste.encodeWriter(ctx.writer, text, opts); + return true; +} + +fn pasteKittyEvent( + ctx: Context, + kitty: Context.KittyClipboard, + req: Request, +) Error!void { + const otp = try kitty_clipboard.generateOtp(kitty.io); + + // Every representation is listed, never read. The listing is + // bounded; a clipboard with more types than that is not a thing. + var mimes_buf: [kitty_clipboard.max_listing_mimes][]const u8 = undefined; + const mimes_len = @min(req.contents.len(), mimes_buf.len); + for (mimes_buf[0..mimes_len], 0..) |*mime, i| mime.* = req.contents.mime(i); + + // The grant is recorded before the event can reach the program, + // and revoked if the event fails to be written so a failure never + // leaves a grant for an event that was never sent. Using a + // one-time grant consumes it, which is the revocation. + try kitty.grants.grant(ctx.alloc, &otp, .read, true); + errdefer _ = kitty.grants.use(ctx.alloc, &otp, .read); + + try (kitty_clipboard.PasteEvent{ + // The protocol only distinguishes the clipboard from the + // primary selection, so both non-standard locations report as + // primary. Only a clipboard paste gets here; see `paste`. + .primary = req.source.clipboard != .standard, + .pw = &otp, + .available = mimes_buf[0..mimes_len], + }).encode(ctx.writer); +} + +test { + // The behavior is tested end to end through the stream handler + // (stream_terminal.zig), which is the primary caller. + std.testing.refAllDecls(@This()); +} diff --git a/vendor/libghostty-vt/src/terminal/point.zig b/vendor/libghostty-vt/src/terminal/point.zig index f6bf25c3..5937576c 100644 --- a/vendor/libghostty-vt/src/terminal/point.zig +++ b/vendor/libghostty-vt/src/terminal/point.zig @@ -71,7 +71,15 @@ pub const Point = union(Tag) { @This(), // Padding: largest variant is Coordinate (u16 + u32 = 6 bytes). // Use [2]u64 (16 bytes) for future expansion. - [2]u64, + .{ + .padding = [2]u64, + .field_renames = .{ + .active = "coordinate", + .viewport = "coordinate", + .screen = "coordinate", + .history = "coordinate", + }, + }, ); pub const C = c_union.C; pub const CValue = c_union.CValue; diff --git a/vendor/libghostty-vt/src/terminal/ref_counted_set.zig b/vendor/libghostty-vt/src/terminal/ref_counted_set.zig index dd76079f..669c2c57 100644 --- a/vendor/libghostty-vt/src/terminal/ref_counted_set.zig +++ b/vendor/libghostty-vt/src/terminal/ref_counted_set.zig @@ -1,5 +1,6 @@ const std = @import("std"); const assert = @import("../quirks.zig").inlineAssert; +const testing = std.testing; const size = @import("size.zig"); const Offset = size.Offset; @@ -211,6 +212,30 @@ pub fn RefCountedSet( @memset(table.ptr(base)[0..l.table_cap], 0); @memset(items.ptr(base)[0..l.cap], .{}); + return initFromParts(table, items, l, context); + } + + /// Like `init`, but for backing memory that the caller guarantees + /// is already zero-filled (e.g. fresh OS pages). This writes + /// nothing to the backing buffer, so the OS pages behind the table + /// and items stay untouched until the first `add`. + /// + /// Behavior is undefined if the backing memory is not zero. + pub fn initAssumeZeroed(base: OffsetBuf, l: Layout, context: Context) Self { + return initFromParts( + base.member(Id, l.table_start), + base.member(Item, l.items_start), + l, + context, + ); + } + + fn initFromParts( + table: Offset(Id), + items: Offset(Item), + l: Layout, + context: Context, + ) Self { return .{ .table = table, .items = items, @@ -316,6 +341,19 @@ pub fn RefCountedSet( if (id < self.next_id) { if (items[id].meta.ref == 0) { + // Requested ID is dead, but if the value exists not under + // another ID then ref count that and increase the ID. + if (self.lookupContext(base, value, ctx)) |existing_id| { + // Notify the context that the value is "deleted" + // because we're reusing the existing value in the + // set. This allows callers to clean up any + // resources associated with the value. + if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value); + + items[existing_id].meta.ref += 1; + return existing_id; + } + // See comment in `addContext` for details. if (self.psl_stats[self.psl_stats.len - 1] > 0) { @branchHint(.cold); @@ -323,8 +361,7 @@ pub fn RefCountedSet( } self.deleteItem(base, id, ctx); - - const added_id = self.upsert(base, value, id, ctx); + const added_id = self.insert(base, value, id, ctx); items[added_id].meta.ref += 1; @@ -447,6 +484,47 @@ pub fn RefCountedSet( return self.living; } + /// A live entry returned by `Iterator`. + pub const Entry = struct { + id: Id, + value_ptr: *T, + }; + + /// Iterates live entries in ascending ID order. + /// + /// Released entries whose reference count reached zero are skipped. + /// Any mutation of the set invalidates the iterator. + pub const Iterator = struct { + items: [*]Item, + id: Id = 1, + end: Id, + + pub fn next(self: *Iterator) ?Entry { + while (self.id < self.end) { + const id = self.id; + self.id += 1; + + const item = &self.items[id]; + if (item.meta.ref == 0) continue; + + return .{ + .id = id, + .value_ptr = &item.value, + }; + } + + return null; + } + }; + + /// Return an iterator over the live entries in this set. + pub fn iterator(self: *const Self, base: anytype) Iterator { + return .{ + .items = self.items.ptr(base), + .end = self.next_id, + }; + } + /// Delete an item, removing any references from /// the table, and freeing its ID to be reused. fn deleteItem(self: *Self, base: anytype, id: Id, ctx: Context) void { @@ -547,24 +625,6 @@ pub fn RefCountedSet( return null; } - /// Find the provided value in the hash table, or add a new item - /// for it if not present. If a new item is added, `new_id` will - /// be used as the ID. If an existing item is found, the `new_id` - /// is ignored and the existing item's ID is returned. - fn upsert(self: *Self, base: anytype, value: T, new_id: Id, ctx: Context) Id { - // If the item already exists, return it. - if (self.lookupContext(base, value, ctx)) |id| { - // Notify the context that the value is "deleted" because - // we're reusing the existing value in the set. This allows - // callers to clean up any resources associated with the value. - if (comptime @hasDecl(Context, "deleted")) ctx.deleted(value); - - return id; - } - - return self.insert(base, value, new_id, ctx); - } - /// Insert the given value into the hash table with the given ID. /// /// If runtime safety is enabled, asserts that @@ -690,7 +750,9 @@ pub fn RefCountedSet( var psl_stats: [32]Id = @splat(0); - for (items[0..self.layout.cap], 0..) |item, id| { + // Start at item 1 because item 0 is reserved and never + // assigned to. Its metadata doesn't matter. + for (items[1..self.next_id], 1..) |item, id| { if (item.meta.bucket < std.math.maxInt(Id)) { assert(table[item.meta.bucket] == id); psl_stats[item.meta.psl] += 1; @@ -704,6 +766,7 @@ pub fn RefCountedSet( psl_stats = @splat(0); for (table[0..self.layout.table_cap], 0..) |id, bucket| { + if (id == 0) continue; const item = items[id]; if (item.meta.bucket < std.math.maxInt(Id)) { assert(item.meta.bucket == bucket); @@ -721,3 +784,107 @@ pub fn RefCountedSet( } }; } + +test "addWithId dead id resolving to an existing value" { + const alloc = testing.allocator; + const TestSet = RefCountedSet( + u32, + u16, + u16, + struct { + pub fn hash(_: *const @This(), value: u32) u64 { + return std.hash.int(value); + } + + pub fn eql(_: *const @This(), a: u32, b: u32) bool { + return a == b; + } + }, + ); + + const layout: TestSet.Layout = .init(8); + const buf = try alloc.alignedAlloc( + u8, + TestSet.base_align, + layout.total_size, + ); + defer alloc.free(buf); + + var set: TestSet = .init(.init(buf), layout, .{}); + + // Create a dead item between two live ones so that it can't + // be reaped by the trim loop in `add`, then release it. This + // mirrors a page style set after a styled run is erased. + const live = try set.add(buf, 11); + const released = try set.add(buf, 22); + const last = try set.add(buf, 33); + set.release(buf, released); + try testing.expectEqual(@as(usize, 2), set.count()); + + // Request the dead ID for a value that is already live under + // a different ID: we must resolve to the existing item and + // take a reference, without changing the living count. + const resolved = (try set.addWithId(buf, 11, released)).?; + try testing.expectEqual(live, resolved); + try testing.expectEqual(@as(u16, 2), set.refCount(buf, live)); + try testing.expectEqual(@as(usize, 2), set.count()); + + // The living count must agree with the iterator. + var it = set.iterator(buf); + var iterated: usize = 0; + while (it.next()) |_| iterated += 1; + try testing.expectEqual(set.count(), iterated); + + // The dead slot is still reusable for a value that is not + // in the set: the requested ID must be used (null return). + try testing.expectEqual(@as(?u16, null), try set.addWithId(buf, 44, released)); + try testing.expectEqual(@as(u16, 1), set.refCount(buf, released)); + try testing.expectEqual(@as(usize, 3), set.count()); + + _ = last; +} + +test "iterator visits live entries in ID order" { + const alloc = testing.allocator; + const TestSet = RefCountedSet( + u32, + u16, + u16, + struct { + pub fn hash(_: *const @This(), value: u32) u64 { + return std.hash.int(value); + } + + pub fn eql(_: *const @This(), a: u32, b: u32) bool { + return a == b; + } + }, + ); + + const layout: TestSet.Layout = .init(8); + const buf = try alloc.alignedAlloc( + u8, + TestSet.base_align, + layout.total_size, + ); + defer alloc.free(buf); + + var set: TestSet = .init(.init(buf), layout, .{}); + const first = try set.add(buf, 11); + const released = try set.add(buf, 22); + const last = try set.add(buf, 33); + _ = try set.add(buf, 11); + set.release(buf, released); + + var it = set.iterator(buf); + + const first_entry = it.next().?; + try testing.expectEqual(first, first_entry.id); + try testing.expectEqual(@as(u32, 11), first_entry.value_ptr.*); + + const last_entry = it.next().?; + try testing.expectEqual(last, last_entry.id); + try testing.expectEqual(@as(u32, 33), last_entry.value_ptr.*); + + try testing.expect(it.next() == null); +} diff --git a/vendor/libghostty-vt/src/terminal/render.zig b/vendor/libghostty-vt/src/terminal/render.zig index 04f0c967..6bcddb2c 100644 --- a/vendor/libghostty-vt/src/terminal/render.zig +++ b/vendor/libghostty-vt/src/terminal/render.zig @@ -235,6 +235,21 @@ pub const RenderState = struct { /// The highlights within this row. highlights: std.ArrayList(Highlight), + + /// The style runs applied to this row's per-cell style data + /// by the last `endUpdate` that touched it. This is what lets + /// `endUpdate` skip the (comparatively large) per-cell style + /// fill when a rebuilt row produced identical runs, which is + /// the common case: text changes far more often than styling. + /// + /// The invariant is that this always describes the current + /// contents of the cell style data for the covered ranges. It + /// is cleared whenever the cell storage is reallocated. + /// + /// This uses the general allocator (NOT the row arena) + /// because it must survive row rebuilds. `endUpdate` cannot + /// allocate, so `beginUpdate` reserves the capacity. + applied_styles: std.ArrayList(StyleRun), }; pub const Highlight = struct { @@ -304,10 +319,12 @@ pub const RenderState = struct { for ( self.row_data.items(.arena), self.row_data.items(.cells), - ) |state, *cells| { + self.row_data.items(.applied_styles), + ) |state, *cells, *applied| { var arena: ArenaAllocator = state.promote(alloc); arena.deinit(); cells.deinit(alloc); + applied.deinit(alloc); } self.row_data.deinit(alloc); self.pending_styles.deinit(alloc); @@ -464,6 +481,7 @@ pub const RenderState = struct { .dirty = true, .selection = null, .highlights = .empty, + .applied_styles = .empty, }); } } else { @@ -471,10 +489,12 @@ pub const RenderState = struct { for ( row_data.items(.arena)[self.rows..], row_data.items(.cells)[self.rows..], - ) |state, *cell| { + row_data.items(.applied_styles)[self.rows..], + ) |state, *cell, *applied| { var arena: ArenaAllocator = state.promote(alloc); arena.deinit(); cell.deinit(alloc); + applied.deinit(alloc); } self.row_data.shrinkRetainingCapacity(self.rows); } @@ -490,6 +510,7 @@ pub const RenderState = struct { const row_sels = row_data.items(.selection); const row_highlights = row_data.items(.highlights); const row_dirties = row_data.items(.dirty); + const row_applied = row_data.items(.applied_styles); // If we're redrawing then every row will be rebuilt, superseding // any pending style runs from prior updates. Clearing also @@ -512,6 +533,7 @@ pub const RenderState = struct { .highlights = row_highlights, .dirties = row_dirties, .pending_styles = &self.pending_styles, + .applied_styles = row_applied, }; var y: usize = 0; var any_dirty: bool = false; @@ -735,22 +757,113 @@ pub const RenderState = struct { const row_data = self.row_data.slice(); const row_cells = row_data.items(.cells); - for (self.pending_styles.items) |run| { + const row_applied = row_data.items(.applied_styles); + + // Process the pending runs one row segment at a time. All the + // runs for a row are appended contiguously by a single + // beginUpdate, so a segment boundary is simply a change in y. + const runs = self.pending_styles.items; + var i: usize = 0; + while (i < runs.len) { + const y = runs[i].y; + var j = i + 1; + while (j < runs.len and runs[j].y == y) j += 1; + const segment = runs[i..j]; + i = j; + // Defensive: the row data may have changed shape if the // caller violated ordering (e.g. an error path skipped an // endUpdate between updates). Any update that changes // dimensions clears the pending list (redraw), so this // should never actually trigger, but the cost is trivial. - if (run.y >= row_cells.len) continue; - const styles = row_cells[run.y].slice().items(.style); - const end = @min(run.end, styles.len); - const start = @min(run.start, end); + if (y >= row_cells.len) continue; + + // If the segment matches the runs already denormalized + // into this row's cell data then the per-cell styles are + // already correct and the (comparatively large) fill can + // be skipped entirely. This is the common case: rebuilt + // rows usually keep their styling (only the text + // changed), and full redraws of an unchanged screen keep + // both. + const applied = &row_applied[y]; + if (runsEql(applied.items, segment)) continue; + + for (segment) |run| { + const styles = row_cells[run.y].slice().items(.style); + const end = @min(run.end, styles.len); + const start = @min(run.start, end); + + fillStyles(styles[start..end], run.style); + } - @memset(styles[start..end], run.style); + // Record what we applied so the next rebuild of this row + // can skip the fill. beginUpdate reserved the capacity + // (we cannot allocate here); if it doesn't fit (e.g. + // segments merged across multiple begins without an end) + // leave the cache empty, which never matches and simply + // means the next rebuild applies its runs. + applied.clearRetainingCapacity(); + if (applied.capacity >= segment.len) { + applied.appendSliceAssumeCapacity(segment); + } } self.pending_styles.clearRetainingCapacity(); } + /// Mark all render-state data as consumed by the renderer. + /// + /// This clears both the global dirty state and every per-row dirty flag. + /// Callers that only consume part of a frame should clear the two layers + /// individually instead. + pub fn clean(self: *RenderState) void { + self.dirty = .false; + @memset(self.row_data.items(.dirty), false); + } + + /// Fill a slice of styles with one value. + /// + /// This is equivalent to `@memset(dst, value)` but manually vectorized: + /// `@memset` with a struct value lowers to a per-element field copy + /// that reloads the source at every iteration because LLVM cannot + /// prove the destination doesn't alias it. And, Zig 0.16 disables + /// auto-vectorization due to an LLVM bug. + /// + /// This complexity is justified by accounting for ~10% of the endUpdate + /// times under heavily styled cases. + fn fillStyles(dst: []Style, value: Style) void { + // Each element is written as two overlapping 16-byte vector + // stores held in registers, which requires 16 <= size <= 32. + const elem_size = @sizeOf(Style); + comptime assert(elem_size >= 16 and elem_size <= 32); + + const V = @Vector(16, u8); + const src: *align(@alignOf(Style)) const [elem_size]u8 = @ptrCast(&value); + const lo = @as(*align(4) const V, @ptrCast(src[0..16])).*; + const hi = @as(*align(1) const V, @ptrCast(src[elem_size - 16 ..][0..16])).*; + + const dst_bytes = std.mem.sliceAsBytes(dst); + var off: usize = 0; + for (0..dst.len) |_| { + @as(*align(1) V, @ptrCast(dst_bytes[off..][0..16])).* = lo; + @as(*align(1) V, @ptrCast(dst_bytes[off + elem_size - 16 ..][0..16])).* = hi; + off += elem_size; + } + } + + /// Returns true if the two style run lists denormalize to + /// identical per-cell style data. This is a semantic comparison + /// (styles are compared field-wise, never by bytes, since padding + /// is undefined). + fn runsEql(a: []const StyleRun, b: []const StyleRun) bool { + if (a.len != b.len) return false; + for (a, b) |ar, br| { + if (ar.start != br.start or + ar.end != br.end or + !ar.style.eql(br.style)) return false; + } + return true; + } + /// Update the highlights in the render state from the given flattened /// highlights. Because this uses flattened highlights, it does not require /// reading from the terminal state so it should be done outside of @@ -1022,6 +1135,7 @@ const RowBuilder = struct { highlights: []std.ArrayList(RenderState.Highlight), dirties: []bool, pending_styles: *std.ArrayList(RenderState.StyleRun), + applied_styles: []std.ArrayList(RenderState.StyleRun), fn row( b: *const RowBuilder, @@ -1058,7 +1172,14 @@ const RowBuilder = struct { // Our per-row arena is only used for temporary allocations // pertaining to cells directly (e.g. graphemes, hyperlinks). const cells: *std.MultiArrayList(RenderState.Cell) = &b.cells[vy]; - if (cells.len != b.cols) try cells.resize(b.alloc, b.cols); + if (cells.len != b.cols) { + // The cell storage (including the per-cell style data) is + // being reallocated, so the applied style cache no longer + // describes it. Clear it before the resize so an error + // can't leave it stale. + b.applied_styles[vy].clearRetainingCapacity(); + try cells.resize(b.alloc, b.cols); + } // We always copy our raw cell data. In the case we have no // managed memory, we can skip setting any other fields. @@ -1077,6 +1198,7 @@ const RowBuilder = struct { const arena_alloc = arena.allocator(); const cells_grapheme = cells_slice.items(.grapheme); const n = page_cells.len; + const runs_start = b.pending_styles.items.len; var x: usize = 0; scan: while (x < n) { // Skip runs of plain cells a group at a time. Cells that @@ -1180,7 +1302,7 @@ const RowBuilder = struct { .b = page_cell.content.color_rgb.b, } } }, .bg_color_palette => .{ .bg_color = .{ - .palette = page_cell.content.color_palette, + .palette = page_cell.content.color_palette.data, } }, else => unreachable, }; @@ -1210,14 +1332,24 @@ const RowBuilder = struct { }, } } + + // Reserve the applied style cache capacity for the runs we + // appended so that endUpdate (which cannot allocate) is able + // to record what it applies. See Row.applied_styles. + const runs_added = b.pending_styles.items.len - runs_start; + if (runs_added > 0) try b.applied_styles[vy].ensureTotalCapacity( + b.alloc, + runs_added, + ); } }; test "styled" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 80, .rows = 24, }); @@ -1234,8 +1366,9 @@ test "styled" { test "basic text" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -1270,8 +1403,9 @@ test "basic text" { test "styled text" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -1415,15 +1549,16 @@ fn testCompareStates( test "incremental updates match full rebuild" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; // Deterministic so failures are reproducible. var prng = std.Random.DefaultPrng.init(0xB0BA_CAFE); const rand = prng.random(); - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 20, .rows = 8, - .max_scrollback = 500, + .max_scrollback_bytes = 500, }); defer t.deinit(alloc); @@ -1551,8 +1686,9 @@ test "incremental updates match full rebuild" { test "begin and end update" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -1596,11 +1732,68 @@ test "begin and end update" { } } +test "endUpdate skips unchanged style runs" { + const testing = std.testing; + const alloc = testing.allocator; + const io = testing.io; + + var t = try Terminal.init(io, alloc, .{ + .cols = 10, + .rows = 3, + }); + defer t.deinit(alloc); + + var s = t.vtStream(); + defer s.deinit(); + s.nextSlice("\x1b[1mAB"); // Bold + + var state: RenderState = .empty; + defer state.deinit(alloc); + try state.update(alloc, &t); + + // The applied cache should record the bold run for row 0. + { + const row_data = state.row_data.slice(); + const applied = row_data.items(.applied_styles); + try testing.expectEqual(1, applied[0].items.len); + try testing.expect(applied[0].items[0].style.flags.bold); + try testing.expect(state.row_data.items(.cells)[0].get(0).style.flags.bold); + } + + // Rewrite the text without changing the styling: the row is + // rebuilt, the run matches the cache, and the styles must remain + // correct (the fill is skipped internally). + s.nextSlice("\x1b[1;1H\x1b[1mXY"); + try state.update(alloc, &t); + { + const cells = &state.row_data.items(.cells)[0]; + try testing.expectEqual('X', cells.get(0).raw.codepoint()); + try testing.expect(cells.get(0).style.flags.bold); + try testing.expect(cells.get(1).style.flags.bold); + } + + // Change the styling: the cache mismatches and the new styles + // must be applied and recorded. + s.nextSlice("\x1b[1;1H\x1b[0;3mZW"); // Italic + try state.update(alloc, &t); + { + const cells = &state.row_data.items(.cells)[0]; + try testing.expectEqual('Z', cells.get(0).raw.codepoint()); + try testing.expect(!cells.get(0).style.flags.bold); + try testing.expect(cells.get(0).style.flags.italic); + + const applied = state.row_data.items(.applied_styles); + try testing.expectEqual(1, applied[0].items.len); + try testing.expect(applied[0].items[0].style.flags.italic); + } +} + test "bg color cells" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -1641,8 +1834,9 @@ test "bg color cells" { test "grapheme" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -1688,8 +1882,9 @@ test "grapheme" { test "cursor state in viewport" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 5, }); @@ -1730,8 +1925,9 @@ test "cursor state in viewport" { test "cursor state out of viewport" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 2, }); @@ -1764,8 +1960,9 @@ test "cursor state out of viewport" { test "dirty state" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 5, }); @@ -1813,8 +2010,9 @@ test "dirty state" { test "colors" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 5, }); @@ -1850,8 +2048,9 @@ test "colors" { test "selection single line" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t: Terminal = try .init(alloc, .{ + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -1885,8 +2084,9 @@ test "selection single line" { test "selection multiple lines" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t: Terminal = try .init(alloc, .{ + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -1921,8 +2121,9 @@ test "selection multiple lines" { test "linkCells" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 5, }); @@ -1957,8 +2158,9 @@ test "linkCells" { test "string" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 5, .rows = 2, }); @@ -1987,14 +2189,15 @@ test "string" { test "linkCells with scrollback spanning pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; const viewport_rows: size.CellCountInt = 10; const tail_rows: size.CellCountInt = 5; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = page.std_capacity.cols, .rows = viewport_rows, - .max_scrollback = 10_000, + .max_scrollback_bytes = 10_000, }); defer t.deinit(alloc); @@ -2029,8 +2232,9 @@ test "linkCells with scrollback spanning pages" { test "linkCells with invalid viewport point" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 5, }); @@ -2067,8 +2271,9 @@ test "linkCells with invalid viewport point" { test "flattened highlights require matching page serial" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); @@ -2134,8 +2339,9 @@ test "flattened highlights require matching page serial" { test "dirty row resets highlights" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var t = try Terminal.init(alloc, .{ + var t = try Terminal.init(io, alloc, .{ .cols = 10, .rows = 3, }); diff --git a/vendor/libghostty-vt/src/terminal/search.zig b/vendor/libghostty-vt/src/terminal/search.zig index e69603c2..6913df2e 100644 --- a/vendor/libghostty-vt/src/terminal/search.zig +++ b/vendor/libghostty-vt/src/terminal/search.zig @@ -5,6 +5,7 @@ pub const options = @import("terminal_options"); pub const Active = @import("search/active.zig").ActiveSearch; pub const PageList = @import("search/pagelist.zig").PageListSearch; pub const Screen = @import("search/screen.zig").ScreenSearch; +pub const Terminal = @import("search/terminal.zig").TerminalSearch; pub const Viewport = @import("search/viewport.zig").ViewportSearch; // The search thread is not available in libghostty due to the xev dep diff --git a/vendor/libghostty-vt/src/terminal/search/Thread.zig b/vendor/libghostty-vt/src/terminal/search/Thread.zig index d7f0d562..0b781321 100644 --- a/vendor/libghostty-vt/src/terminal/search/Thread.zig +++ b/vendor/libghostty-vt/src/terminal/search/Thread.zig @@ -12,9 +12,9 @@ const std = @import("std"); const builtin = @import("builtin"); const testing = std.testing; const Allocator = std.mem.Allocator; -const ArenaAllocator = std.heap.ArenaAllocator; -const Mutex = std.Thread.Mutex; -const xev = @import("../../global.zig").xev; +const Mutex = std.Io.Mutex; +const global = @import("../../global.zig"); +const xev = global.xev; const internal_os = @import("../../os/main.zig"); const BlockingQueue = @import("../../datastruct/main.zig").BlockingQueue; const MessageData = @import("../../datastruct/main.zig").MessageData; @@ -22,11 +22,10 @@ const point = @import("../point.zig"); const FlattenedHighlight = @import("../highlight.zig").Flattened; const UntrackedHighlight = @import("../highlight.zig").Untracked; const ScreenSet = @import("../ScreenSet.zig"); -const Selection = @import("../Selection.zig"); const Terminal = @import("../Terminal.zig"); const ScreenSearch = @import("screen.zig").ScreenSearch; -const ViewportSearch = @import("viewport.zig").ViewportSearch; +const TerminalSearch = @import("terminal.zig").TerminalSearch; const log = std.log.scoped(.search_thread); @@ -71,7 +70,10 @@ refresh_active: bool = false, /// Search state. Starts as null and is populated when a search is /// started (a needle is given). -search: ?Search = null, +search: ?TerminalSearch = null, + +/// The last-notified values used to diff search state into events. +notify_state: NotifyState = .{}, /// The options used to initialize this thread. opts: Options, @@ -122,8 +124,8 @@ pub fn deinit(self: *Thread) void { self.mailbox.destroy(self.alloc); if (self.search) |*s| { - self.opts.mutex.lock(); - defer self.opts.mutex.unlock(); + self.opts.mutex.lockUncancelable(global.io()); + defer self.opts.mutex.unlock(global.io()); s.deinit(self.opts.terminal); } } @@ -182,14 +184,14 @@ fn threadMain_(self: *Thread) !void { while (true) { // If our loop is canceled then we drain our messages and quit. if (self.loop.stopped()) { - while (self.mailbox.pop()) |message| { + while (self.mailbox.pop(global.io())) |message| { log.debug("mailbox message ignored during shutdown={}", .{message}); } return; } - const s: *Search = if (self.search) |*s| s else { + const s: *TerminalSearch = if (self.search) |*s| s else { // If we're not actively searching, we can block the loop // until it does some work. try self.loop.run(.once); @@ -200,11 +202,7 @@ fn threadMain_(self: *Thread) !void { // notifications. Even if the search is complete, there may be // notifications to send. if (self.opts.event_cb) |cb| { - s.notify( - self.alloc, - cb, - self.opts.event_userdata, - ); + self.notify(s, cb, self.opts.event_userdata); } if (s.isComplete()) { @@ -225,9 +223,9 @@ fn threadMain_(self: *Thread) !void { // All searches are blocked. Let's grab the lock and feed data. .blocked => { - self.opts.mutex.lock(); - defer self.opts.mutex.unlock(); - s.feed(self.alloc, self.opts.terminal); + self.opts.mutex.lockUncancelable(global.io()); + defer self.opts.mutex.unlock(global.io()); + self.feedLocked(s); }, } @@ -240,9 +238,24 @@ fn threadMain_(self: *Thread) !void { } } +/// Feed the search from the terminal state. The terminal mutex must +/// be held by the caller. +fn feedLocked(self: *Thread, s: *TerminalSearch) void { + const t = self.opts.terminal; + + // See the `search_viewport_dirty` flag on the terminal to know + // what exactly this is for. But, if this is set, we know the renderer + // found the viewport/active area dirty, so the active area must be + // re-scanned. + const active_dirty = t.flags.search_viewport_dirty; + t.flags.search_viewport_dirty = false; + + s.feed(t, active_dirty); +} + /// Drain the mailbox. fn drainMailbox(self: *Thread) !void { - while (self.mailbox.pop()) |message| { + while (self.mailbox.pop(global.io())) |message| { log.debug("mailbox message={}", .{message}); switch (message) { .change_needle => |v| { @@ -257,54 +270,15 @@ fn drainMailbox(self: *Thread) !void { fn select(self: *Thread, sel: ScreenSearch.Select) !void { const s = if (self.search) |*s| s else return; - self.opts.mutex.lock(); - defer self.opts.mutex.unlock(); - - // A screen can be removed or replaced between refresh ticks. Reconcile - // while holding the terminal lock before touching any ScreenSearch pins. - s.feed(self.alloc, self.opts.terminal); - const screen_search = s.screens.getPtr(s.last_screen.key) orelse return; - - // Make the selection. Ignore the result because we don't - // care if the selection didn't change. - _ = try screen_search.select(sel); - - // Grab our match if we have one. If we don't have a selection - // then we do nothing. - const flattened = screen_search.selectedMatch() orelse return; - - // No matter what we reset our selected match cache. This will - // trigger a callback which will trigger the renderer to wake up - // so it can be notified the screen scrolled. - s.last_screen.selected = null; - - // Grab the current screen and see if this match is visible within - // the viewport already. If it is, we do nothing. - const screen = self.opts.terminal.screens.get( - s.last_screen.key, - ) orelse return; - - // Grab the viewport. Viewports and selections are usually small - // so this check isn't very expensive, despite appearing O(N^2), - // both Ns are usually equal to 1. - var it = screen.pages.pageIterator( - .right_down, - .{ .viewport = .{} }, - null, - ); - const hl_chunks = flattened.chunks.slice(); - while (it.next()) |chunk| { - for (0..hl_chunks.len) |i| { - const hl_chunk = hl_chunks.get(i); - if (chunk.overlaps(.{ - .node = hl_chunk.node, - .start = hl_chunk.start, - .end = hl_chunk.end, - })) return; - } - } + self.opts.mutex.lockUncancelable(global.io()); + defer self.opts.mutex.unlock(global.io()); - screen.scroll(.{ .pin = flattened.startPin() }); + if (try s.select(self.opts.terminal, sel, .if_needed)) { + // No matter what we reset our selected match cache. This will + // trigger a callback which will trigger the renderer to wake up + // so it can be notified the screen scrolled. + self.notify_state.selected = null; + } } /// Change the search term to the given value. @@ -314,14 +288,15 @@ fn changeNeedle(self: *Thread, needle: []const u8) !void { // Stop the previous search if (self.search) |*s| { // If our search is unchanged, do nothing. - if (std.ascii.eqlIgnoreCase(s.viewport.needle(), needle)) return; + if (std.ascii.eqlIgnoreCase(s.needle(), needle)) return; { - self.opts.mutex.lock(); - defer self.opts.mutex.unlock(); + self.opts.mutex.lockUncancelable(global.io()); + defer self.opts.mutex.unlock(global.io()); s.deinit(self.opts.terminal); } self.search = null; + self.notify_state = .{}; // When the search changes then we need to emit that it stopped. if (self.opts.event_cb) |cb| { @@ -345,11 +320,98 @@ fn changeNeedle(self: *Thread, needle: []const u8) !void { // Setup our search state. self.search = try .init(self.alloc, needle); + self.notify_state = .{}; // We need to grab the terminal lock and do an initial feed. - self.opts.mutex.lock(); - defer self.opts.mutex.unlock(); - self.search.?.feed(self.alloc, self.opts.terminal); + self.opts.mutex.lockUncancelable(global.io()); + defer self.opts.mutex.unlock(global.io()); + self.feedLocked(&self.search.?); +} + +/// Notify about any changes to the search state by diffing against +/// the last-notified values. +/// +/// This doesn't require any locking as it only reads search-owned state. +fn notify( + self: *Thread, + s: *TerminalSearch, + cb: EventCallback, + ud: ?*anyopaque, +) void { + const state = &self.notify_state; + + // A screen switch makes all previously notified per-screen state + // stale, so reset it to force recalculations and notifications. + if (state.key != s.active_key) { + state.key = s.active_key; + state.total = null; + state.selected = null; + } + + const screen_search = s.activeScreenSearch() orelse return; + + // Check our total match data + const total = screen_search.matchesLen(); + if (total != state.total) { + log.debug("notifying total matches={}", .{total}); + state.total = total; + cb(.{ .total_matches = total }, ud); + } + + // Check our viewport matches. If they're stale, we collect them + // now. We do this as part of notify and not tick because the + // viewport search is very fast and doesn't require ticked progress + // or feeds. + if (s.stale_viewport_matches) viewport: { + const matches = s.viewportMatches() catch |err| { + log.warn("error collecting viewport matches err={}", .{err}); + break :viewport; + }; + + log.debug("notifying viewport matches len={}", .{matches.len}); + cb(.{ .viewport_matches = matches }, ud); + } + + // Check our last selected match data. + if (screen_search.selected) |m| match: { + const flattened = screen_search.selectedMatch() orelse break :match; + const untracked = flattened.untracked(); + if (state.selected) |prev| { + if (prev.idx == m.idx and prev.highlight.eql(untracked)) { + // Same selection, don't update it. + break :match; + } + } + + // New selection, notify! + state.selected = .{ + .idx = m.idx, + .highlight = untracked, + }; + + log.debug("notifying selection updated idx={}", .{m.idx}); + cb( + .{ .selected_match = .{ + .idx = m.idx, + .highlight = flattened, + } }, + ud, + ); + } else if (state.selected != null) { + log.debug("notifying selection cleared", .{}); + state.selected = null; + cb( + .{ .selected_match = null }, + ud, + ); + } + + // Send our complete notification if we just completed. + if (!state.complete and s.isComplete()) { + log.debug("notifying search complete", .{}); + state.complete = true; + cb(.complete, ud); + } } fn startRefreshTimer(self: *Thread) void { @@ -423,9 +485,9 @@ fn refreshCallback( // Run our feed if we have a search active. if (self.search) |*s| { - self.opts.mutex.lock(); - defer self.opts.mutex.unlock(); - s.feed(self.alloc, self.opts.terminal); + self.opts.mutex.lockUncancelable(global.io()); + defer self.opts.mutex.unlock(global.io()); + self.feedLocked(s); } // Only continue if we're still active @@ -501,357 +563,30 @@ pub const Event = union(enum) { }; }; -/// Search state. -const Search = struct { - /// Active viewport search for the active screen. - viewport: ViewportSearch, - - /// The searchers for all the screens. - screens: std.EnumMap(ScreenSet.Key, ScreenSearch), +/// The last-notified values used by `notify` to diff search state +/// into events. +const NotifyState = struct { + /// The active screen key the state below was captured against. + key: ScreenSet.Key = .primary, - /// ScreenSet generations captured when each searcher was initialized. - /// Allocators may reuse a destroyed Screen address, so pointer equality - /// alone cannot distinguish replacement screens from stale handles. - screen_generations: std.EnumMap(ScreenSet.Key, usize), + /// Last notified total matches count + total: ?usize = null, - /// All state related to screen switches, collected so that when - /// we switch screens it makes everything related stale, too. - last_screen: ScreenState, + /// Last notified selected match + selected: ?Selected = null, /// True if we sent the complete notification yet. - last_complete: bool, - - /// The last viewport matches we found. - stale_viewport_matches: bool, - - const ScreenState = struct { - /// Last active screen key - key: ScreenSet.Key, - - /// Last notified total matches count - total: ?usize = null, - - /// Last notified selected match index - selected: ?SelectedMatch = null, - - const SelectedMatch = struct { - idx: usize, - highlight: UntrackedHighlight, - }; - }; - - pub fn init( - alloc: Allocator, - needle: []const u8, - ) Allocator.Error!Search { - var vp: ViewportSearch = try .init(alloc, needle); - errdefer vp.deinit(); - - // We use dirty tracking for active area changes. Start with it - // dirty so the first change is re-searched. - vp.active_dirty = true; - - return .{ - .viewport = vp, - .screens = .init(.{}), - .screen_generations = .init(.{}), - .last_screen = .{ .key = .primary }, - .last_complete = false, - .stale_viewport_matches = true, - }; - } + complete: bool = false, - pub fn deinit(self: *Search, t: *Terminal) void { - self.viewport.deinit(); - var it = self.screens.iterator(); - while (it.next()) |entry| { - if (self.screenIsValid(&t.screens, entry.key, entry.value)) { - entry.value.deinit(); - } else { - entry.value.deinitScreenInvalid(); - } - } - } - - fn screenIsValid( - self: *const Search, - screens: *const ScreenSet, - key: ScreenSet.Key, - search: *const ScreenSearch, - ) bool { - const generation = self.screen_generations.get(key) orelse return false; - if (generation != screens.generation(key)) return false; - const actual = screens.get(key) orelse return false; - return actual == search.screen; - } - - /// Returns true if all searches on all screens are complete. - pub fn isComplete(self: *Search) bool { - var it = self.screens.iterator(); - while (it.next()) |entry| { - if (!entry.value.state.isComplete()) return false; - } - - return true; - } - - pub const Tick = enum { - /// All searches are complete. - complete, - - /// Progress was made on at least one screen. - progress, - - /// All incomplete searches are blocked on feed. - blocked, + const Selected = struct { + idx: usize, + highlight: UntrackedHighlight, }; - - /// Tick the search forward as much as possible without acquiring - /// the big lock. Returns the overall tick progress. - pub fn tick(self: *Search) Tick { - var result: Tick = .complete; - var it = self.screens.iterator(); - while (it.next()) |entry| { - if (entry.value.tick()) { - result = .progress; - } else |err| switch (err) { - // Ignore... nothing we can do. - error.OutOfMemory => log.warn( - "error ticking screen search key={} err={}", - .{ entry.key, err }, - ), - - // Ignore, good for us. State remains whatever it is. - error.SearchComplete => {}, - - // Ignore, too, progressed - error.FeedRequired => switch (result) { - // If we think we're complete, we're not because we're - // blocked now (nothing made progress). - .complete => result = .blocked, - - // If we made some progress, we remain in progress - // since blocked means no progress at all. - .progress => {}, - - // If we're blocked already then we remain blocked. - .blocked => {}, - }, - } - } - - // log.debug("tick result={}", .{result}); - return result; - } - - /// Grab the mutex and update any state that requires it, such as - /// feeding additional data to the searches or updating the active screen. - pub fn feed( - self: *Search, - alloc: Allocator, - t: *Terminal, - ) void { - // Update our active screen - if (t.screens.active_key != self.last_screen.key) { - // The default values will force resets of a bunch of other - // state too to force recalculations and notifications. - self.last_screen = .{ .key = t.screens.active_key }; - } - - // Reconcile our screens with the terminal screens. Remove - // searchers for screens that no longer exist and add searchers - // for screens that do exist but we don't have yet. - { - // Remove screens we have that no longer exist or changed. - var it = self.screens.iterator(); - while (it.next()) |entry| { - const remove = !self.screenIsValid( - &t.screens, - entry.key, - entry.value, - ); - - if (remove) { - entry.value.deinitScreenInvalid(); - _ = self.screens.remove(entry.key); - _ = self.screen_generations.remove(entry.key); - } - } - } - { - // Add screens that exist but we don't have yet. - var it = t.screens.all.iterator(); - while (it.next()) |entry| { - if (self.screens.contains(entry.key)) continue; - const screen_search = ScreenSearch.init( - alloc, - entry.value.*, - self.viewport.needle(), - ) catch |err| switch (err) { - error.OutOfMemory => { - // OOM is probably going to sink the entire ship but - // we can just ignore it and wait on the next - // reconciliation to try again. - log.warn( - "error initializing screen search for key={} err={}", - .{ entry.key, err }, - ); - continue; - }, - }; - self.screens.put(entry.key, screen_search); - self.screen_generations.put( - entry.key, - t.screens.generation(entry.key), - ); - } - } - - // See the `search_viewport_dirty` flag on the terminal to know - // what exactly this is for. But, if this is set, we know the renderer - // found the viewport/active area dirty, so we should mark it as - // dirty in our viewport searcher so it forces a re-search. - if (t.flags.search_viewport_dirty) { - t.flags.search_viewport_dirty = false; - - // Mark our viewport dirty so it researches the active - self.viewport.active_dirty = true; - - // Reload our active area for our active screen - if (self.screens.getPtr(t.screens.active_key)) |screen_search| { - screen_search.reloadActive() catch |err| switch (err) { - error.OutOfMemory => log.warn( - "error reloading active area for screen key={} err={}", - .{ t.screens.active_key, err }, - ), - }; - } - } - - // Check our viewport for changes. - if (self.viewport.update(&t.screens.active.pages)) |updated| { - if (updated) self.stale_viewport_matches = true; - } else |err| switch (err) { - error.OutOfMemory => log.warn( - "error updating viewport search err={}", - .{err}, - ), - } - - // Feed data - var it = self.screens.iterator(); - while (it.next()) |entry| { - if (entry.value.state.needsFeed()) { - entry.value.feed() catch |err| switch (err) { - error.OutOfMemory => log.warn( - "error feeding screen search key={} err={}", - .{ entry.key, err }, - ), - }; - } - } - } - - /// Notify about any changes to the search state. - /// - /// This doesn't require any locking as it only reads internal state. - pub fn notify( - self: *Search, - alloc: Allocator, - cb: EventCallback, - ud: ?*anyopaque, - ) void { - const screen_search = self.screens.get(self.last_screen.key) orelse return; - - // Check our total match data - const total = screen_search.matchesLen(); - if (total != self.last_screen.total) { - log.debug("notifying total matches={}", .{total}); - self.last_screen.total = total; - cb(.{ .total_matches = total }, ud); - } - - // Check our viewport matches. If they're stale, we do the - // viewport search now. We do this as part of notify and not - // tick because the viewport search is very fast and doesn't - // require ticked progress or feeds. - if (self.stale_viewport_matches) viewport: { - // We always make stale as false. Even if we fail below - // we require a re-feed to re-search the viewport. The feed - // process will make it stale again. - self.stale_viewport_matches = false; - - var arena: ArenaAllocator = .init(alloc); - defer arena.deinit(); - const arena_alloc = arena.allocator(); - var results: std.ArrayList(FlattenedHighlight) = .empty; - while (self.viewport.next()) |hl| { - const hl_cloned = hl.clone(arena_alloc) catch continue; - results.append(arena_alloc, hl_cloned) catch |err| switch (err) { - error.OutOfMemory => { - log.warn( - "error collecting viewport matches err={}", - .{err}, - ); - - // Reset the viewport so we force an update on the - // next feed. - self.viewport.reset(); - break :viewport; - }, - }; - } - - log.debug("notifying viewport matches len={}", .{results.items.len}); - cb(.{ .viewport_matches = results.items }, ud); - } - - // Check our last selected match data. - if (screen_search.selected) |m| match: { - const flattened = screen_search.selectedMatch() orelse break :match; - const untracked = flattened.untracked(); - if (self.last_screen.selected) |prev| { - if (prev.idx == m.idx and prev.highlight.eql(untracked)) { - // Same selection, don't update it. - break :match; - } - } - - // New selection, notify! - self.last_screen.selected = .{ - .idx = m.idx, - .highlight = untracked, - }; - - log.debug("notifying selection updated idx={}", .{m.idx}); - cb( - .{ .selected_match = .{ - .idx = m.idx, - .highlight = flattened, - } }, - ud, - ); - } else if (self.last_screen.selected != null) { - log.debug("notifying selection cleared", .{}); - self.last_screen.selected = null; - cb( - .{ .selected_match = null }, - ud, - ); - } - - // Send our complete notification if we just completed. - if (!self.last_complete and self.isComplete()) { - log.debug("notifying search complete", .{}); - self.last_complete = true; - cb(.complete, ud); - } - } }; const TestUserData = struct { const Self = @This(); - reset: std.Thread.ResetEvent = .{}, + reset: std.Io.Event = .unset, total: usize = 0, selected: ?Event.SelectedMatch = null, viewport: []FlattenedHighlight = &.{}, @@ -865,7 +600,7 @@ const TestUserData = struct { const ud: *Self = @ptrCast(@alignCast(userdata.?)); switch (event) { .quit => {}, - .complete => ud.reset.set(), + .complete => ud.reset.set(global.io()), .total_matches => |v| ud.total = v, .selected_match => |v| ud.selected = v, .viewport_matches => |v| { @@ -886,8 +621,9 @@ const TestUserData = struct { test { const alloc = testing.allocator; - var mutex: std.Thread.Mutex = .{}; - var t: Terminal = try .init(alloc, .{ .cols = 20, .rows = 2 }); + const io = testing.io; + var mutex: std.Io.Mutex = .init; + var t: Terminal = try .init(io, alloc, .{ .cols = 20, .rows = 2 }); defer t.deinit(alloc); var stream = t.vtStream(); @@ -912,6 +648,7 @@ test { // Start our search _ = thread.mailbox.push( + io, .{ .change_needle = try .init( alloc, @as([]const u8, "world"), @@ -921,7 +658,7 @@ test { try thread.wakeup.notify(); // Wait for completion - try ud.reset.timedWait(100 * std.time.ns_per_ms); + try ud.reset.waitTimeout(testing.io, .{ .duration = .{ .clock = .awake, .raw = .fromMilliseconds(100) } }); // Stop the thread try thread.stop.notify(); @@ -942,32 +679,3 @@ test { } }, t.screens.active.pages.pointFromPin(.screen, sel.end).?); } } - -test "select after active screen removal" { - const alloc = testing.allocator; - var mutex: std.Thread.Mutex = .{}; - var t: Terminal = try .init(alloc, .{ .cols = 20, .rows = 2 }); - defer t.deinit(alloc); - - _ = try t.switchScreen(.alternate); - - var search: Search = try .init(alloc, "needle"); - search.feed(alloc, &t); - try testing.expectEqual(ScreenSet.Key.alternate, search.last_screen.key); - try testing.expect(search.screens.contains(.alternate)); - - var thread: Thread = undefined; - thread.search = search; - thread.opts = .{ - .mutex = &mutex, - .terminal = &t, - }; - defer if (thread.search) |*active| active.deinit(&t); - - _ = try t.switchScreen(.primary); - t.screens.remove(alloc, .alternate); - - try thread.select(.next); - try testing.expectEqual(ScreenSet.Key.primary, thread.search.?.last_screen.key); - try testing.expect(!thread.search.?.screens.contains(.alternate)); -} diff --git a/vendor/libghostty-vt/src/terminal/search/active.zig b/vendor/libghostty-vt/src/terminal/search/active.zig index b99b2357..b37f46d2 100644 --- a/vendor/libghostty-vt/src/terminal/search/active.zig +++ b/vendor/libghostty-vt/src/terminal/search/active.zig @@ -103,7 +103,8 @@ pub const ActiveSearch = struct { test "simple search" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -143,7 +144,8 @@ test "simple search" { test "clear screen and search" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var s = t.vtStream(); diff --git a/vendor/libghostty-vt/src/terminal/search/pagelist.zig b/vendor/libghostty-vt/src/terminal/search/pagelist.zig index 98a24c18..a5ee402e 100644 --- a/vendor/libghostty-vt/src/terminal/search/pagelist.zig +++ b/vendor/libghostty-vt/src/terminal/search/pagelist.zig @@ -147,7 +147,8 @@ pub const PageListSearch = struct { test "simple search" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -194,7 +195,8 @@ test "simple search" { test "feed multiple pages with matches" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -238,7 +240,8 @@ test "feed multiple pages with matches" { test "feed multiple pages no matches" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -277,7 +280,8 @@ test "feed multiple pages no matches" { test "feed iteratively through multiple matches" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 80, .rows = 24 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 80, .rows = 24 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -318,7 +322,8 @@ test "feed iteratively through multiple matches" { test "feed with match spanning page boundary" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 80, .rows = 24 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 80, .rows = 24 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -372,10 +377,11 @@ test "feed with match spanning page boundary" { test "compressed history match spanning page boundary remains compressed" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 80, .rows = 24, - .max_scrollback = 10 * 1024 * 1024, + .max_scrollback_bytes = 10 * 1024 * 1024, }); defer t.deinit(alloc); @@ -442,7 +448,8 @@ test "compressed history match spanning page boundary remains compressed" { test "feed with match spanning page boundary with newline" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 80, .rows = 24 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 80, .rows = 24 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -480,7 +487,11 @@ test "feed with pruned page" { const alloc = testing.allocator; // Zero here forces minimum max size to effectively two pages. - var p: PageList = try .init(alloc, 80, 24, 0); + var p: PageList = try .init(alloc, .{ + .cols = 80, + .rows = 24, + .max_size = 0, + }); defer p.deinit(); // Grow to capacity @@ -523,7 +534,10 @@ test "feed with pruned page" { test "feed keeps its tracked pin within a shorter page" { const alloc = testing.allocator; - var pages: PageList = try .init(alloc, 10, 2, null); + var pages: PageList = try .init(alloc, .{ + .cols = 10, + .rows = 2, + }); defer pages.deinit(); const first = pages.pages.first.?; @@ -548,3 +562,60 @@ test "feed keeps its tracked pin within a shorter page" { try testing.expectEqual(shorter, search.pin.node); try testing.expect(pages.pinIsValid(search.pin.*)); } + +test "feed discovers pages prepended after exhaustion" { + const alloc = testing.allocator; + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ + .cols = 10, + .rows = 10, + .max_scrollback_bytes = std.math.maxInt(usize), + }); + defer t.deinit(alloc); + + var s = t.vtStream(); + defer s.deinit(); + s.nextSlice("Fizz"); + + // Exhaust the list: one match on the only page and nothing left to feed. + const pages = &t.screens.active.pages; + var search: PageListSearch = try .init( + alloc, + "Fizz", + pages, + pages.pages.last.?, + ); + defer search.deinit(); + try testing.expect(search.next() != null); + try testing.expect(search.next() == null); + try testing.expect(!try search.feed()); + + // Prepend an older page the way incremental snapshot history restore + // does. The frontier pin names the page that used to be first, and + // finalize keeps that node's identity, so the pin now has a `prev`. + const old_first = pages.pages.first.?; + { + var allocation = try pages.allocatePage(.{ .cols = 10, .rows = 2 }); + defer allocation.deinit(); + const page = allocation.page(); + page.size.rows = 2; + for ("Fizz", 0..) |c, x| page.getRowAndCell(x, 1).cell.* = .init(c); + try allocation.finalize(.prepend); + } + const prepended = pages.pages.first.?; + try testing.expect(prepended != old_first); + try testing.expectEqual(old_first, search.pin.node); + + // Feeding resumes into the restored history and finds its match. + try testing.expect(try search.feed()); + try testing.expectEqual(prepended, search.pin.node); + const h = search.next().?; + const sel = h.untracked(); + try testing.expectEqual(prepended, sel.start.node); + try testing.expectEqual(prepended, sel.end.node); + try testing.expectEqual(@as(size.CellCountInt, 1), sel.start.y); + try testing.expectEqual(@as(size.CellCountInt, 0), sel.start.x); + try testing.expectEqual(@as(size.CellCountInt, 3), sel.end.x); + try testing.expect(search.next() == null); + try testing.expect(!try search.feed()); +} diff --git a/vendor/libghostty-vt/src/terminal/search/screen.zig b/vendor/libghostty-vt/src/terminal/search/screen.zig index e0cd9911..ac6d2361 100644 --- a/vendor/libghostty-vt/src/terminal/search/screen.zig +++ b/vendor/libghostty-vt/src/terminal/search/screen.zig @@ -124,7 +124,9 @@ pub const ScreenSearch = struct { .history_feed => true, // Not obvious but complete search states will prune - // stale history results on feed. + // stale history results on feed, and pick up pages + // prepended below the searched frontier since the last + // feed (incremental snapshot history restore). .complete => true, else => false, @@ -305,6 +307,11 @@ pub const ScreenSearch = struct { /// /// Feed on a complete screen search will perform some cleanup of /// potentially stale history results (pruned) and reclaim some memory. + /// + /// If pages were prepended to the PageList since the search completed + /// (incremental snapshot history restore), the feed picks them up and + /// the search resumes, since "complete" only ever means caught up with + /// the PageList as of the last feed. pub fn feed(self: *ScreenSearch) Allocator.Error!void { // Resize/reflow invalidates every flattened result, not just search // state that needs another history feed. @@ -338,9 +345,13 @@ pub const ScreenSearch = struct { // Feed goes back to searching history. .history_feed => self.state = .history, - // If we're complete then the feed call above should always - // return false and we can't reach this. - .complete => unreachable, + // A complete search had exhausted the PageList as of its last + // feed, but pages can still be prepended below that afterward + // (incremental snapshot history restore). The history searcher's + // tracked pin keeps its node identity through a prepend, so it + // just discovered those older pages and loaded them. Resume + // searching history. + .complete => self.state = .history, } } @@ -765,24 +776,33 @@ pub const ScreenSearch = struct { } } - /// Return the selected match. + /// Return the match at the given index in newest-to-oldest order + /// (0 = most recent match). Returns null if the index is out of + /// range. /// /// This does not require read/write access to the underlying screen. - pub fn selectedMatch(self: *const ScreenSearch) ?FlattenedHighlight { - const sel = self.selected orelse return null; + pub fn matchAt(self: *const ScreenSearch, idx: usize) ?FlattenedHighlight { const active_len = self.active_results.items.len; - if (sel.idx < active_len) { - return self.active_results.items[active_len - 1 - sel.idx]; + if (idx < active_len) { + return self.active_results.items[active_len - 1 - idx]; } const history_len = self.history_results.items.len; - if (sel.idx < active_len + history_len) { - return self.history_results.items[sel.idx - active_len]; + if (idx < active_len + history_len) { + return self.history_results.items[idx - active_len]; } return null; } + /// Return the selected match. + /// + /// This does not require read/write access to the underlying screen. + pub fn selectedMatch(self: *const ScreenSearch) ?FlattenedHighlight { + const sel = self.selected orelse return null; + return self.matchAt(sel.idx); + } + pub const Select = enum { /// Next selection, in reverse order (newest to oldest), /// non-wrapping. @@ -928,7 +948,8 @@ pub const ScreenSearch = struct { test "simple search" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 2 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -972,10 +993,11 @@ test "simple search" { test "simple search with history" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1013,10 +1035,11 @@ test "simple search with history" { test "reload active with history change" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1099,7 +1122,8 @@ test "reload active with history change" { test "active change contents" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 5 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -1139,7 +1163,8 @@ test "active change contents" { test "select next" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 2 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -1198,10 +1223,11 @@ test "select next" { test "select after resize resets stale flattened results" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 3, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); @@ -1232,7 +1258,8 @@ test "select after resize resets stale flattened results" { test "select in active changes contents completely" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 5 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 5 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -1296,10 +1323,11 @@ test "select in active changes contents completely" { test "select into history" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1368,7 +1396,8 @@ test "select into history" { test "select prev" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 2 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -1427,7 +1456,8 @@ test "select prev" { test "select prev then next" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 2 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -1471,10 +1501,11 @@ test "select prev then next" { test "select prev with history" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1525,10 +1556,11 @@ test "select prev wraps when all matches are in history" { // has none, so active_len == 0), selecting prev from index 0 must wrap // to the last result without underflowing `active_len - 1`. const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1561,7 +1593,8 @@ test "select after all matches disappear drops the selection" { // reload/prune empties the results, the selection is dropped, so the next // select() hits the "no matches" guard instead of the wrap arithmetic. const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 2 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -1588,10 +1621,11 @@ test "select after all matches disappear drops the selection" { test "select after partial history erase drops a pruned selection" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1620,10 +1654,11 @@ test "select after partial history erase drops a pruned selection" { test "select after history compaction ignores replaced results" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1654,10 +1689,11 @@ test "select after history compaction ignores replaced results" { test "select after partial history page erase ignores shifted results" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1687,10 +1723,11 @@ test "select after partial history page erase ignores shifted results" { test "reload defers pruning unselected history results" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1721,10 +1758,11 @@ test "reload defers pruning unselected history results" { test "reload after partial history page erase drops shifted selection first" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1759,10 +1797,11 @@ test "reload after partial history page erase drops shifted selection first" { test "select after history page split ignores moved results" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1792,10 +1831,11 @@ test "select after history page split ignores moved results" { test "screen search no scrollback has no history" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer t.deinit(alloc); @@ -1828,10 +1868,11 @@ test "reloadActive partial history cleanup on appendSlice error" { // This test verifies that when reloadActive fails at appendSlice (after // the loop), all FlattenedHighlight items are properly cleaned up. const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1875,10 +1916,11 @@ test "reloadActive partial history cleanup on loop append error" { // (after some items have been appended), all FlattenedHighlight items // are properly cleaned up. const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1921,10 +1963,11 @@ test "select after clearing scrollback" { // Regression test for: https://github.com/ghostty-org/ghostty/issues/11957 // After clearing scrollback (CSI 3J), selecting next/prev should not crash. const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2, - .max_scrollback = std.math.maxInt(usize), + .max_scrollback_bytes = std.math.maxInt(usize), }); defer t.deinit(alloc); const list: *PageList = &t.screens.active.pages; @@ -1960,3 +2003,124 @@ test "select after clearing scrollback" { _ = try search.select(.next); _ = try search.select(.prev); } + +test "feed after complete discovers prepended history pages" { + const alloc = testing.allocator; + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ + .cols = 10, + .rows = 2, + .max_scrollback_bytes = std.math.maxInt(usize), + }); + defer t.deinit(alloc); + const list: *PageList = &t.screens.active.pages; + + var s = t.vtStream(); + defer s.deinit(); + + // The screen begins with the tail of the needle so a restored page which + // soft-wraps into it can complete a match across the restore boundary. + s.nextSlice("st\r\nTest\r\n"); + while (list.totalPages() < 2) s.nextSlice("\r\n"); + for (0..list.rows) |_| s.nextSlice("\r\n"); + s.nextSlice("Test"); + const old_first = list.pages.first.?; + + // Search to completion, then select the oldest match so we can verify + // that restored (older) results never move an existing selection. + var search: ScreenSearch = try .init(alloc, t.screens.active, "Test"); + defer search.deinit(); + try search.searchAll(); + try testing.expect(search.state.isComplete()); + try testing.expectEqual(1, search.active_results.items.len); + try testing.expectEqual(1, search.history_results.items.len); + try testing.expect(try search.select(.prev)); + const selected_idx = search.selected.?.idx; + try testing.expectEqual(1, selected_idx); + const selected_before = search.selectedMatch().?.untracked(); + try testing.expectEqual(old_first, selected_before.start.node); + + // Restore the newest history page: a full match on its first row and a + // soft-wrapped last row ending in "Te" which joins the screen's "st". + { + var allocation = try list.allocatePage(.{ .cols = 10, .rows = 2 }); + defer allocation.deinit(); + const page = allocation.page(); + page.size.rows = 2; + for ("Test", 0..) |c, x| page.getRowAndCell(x, 0).cell.* = .init(c); + for ("xxxxxxxxTe", 0..) |c, x| page.getRowAndCell(x, 1).cell.* = .init(c); + page.getRow(1).wrap = true; + old_first.page().getRow(0).wrap_continuation = true; + try allocation.finalize(.prepend); + } + const newest = list.pages.first.?; + + // The completed search is fed like any periodic refresh. It must pick + // the page up and go back to searching rather than staying complete. + try search.feed(); + try testing.expect(!search.state.isComplete()); + try search.searchAll(); + try testing.expectEqual(1, search.active_results.items.len); + try testing.expectEqual(3, search.history_results.items.len); + + // New results are older than everything cached, so they append in + // newest-to-oldest order: the boundary match first, then the row above. + { + const boundary = search.history_results.items[1].untracked(); + try testing.expectEqual(newest, boundary.start.node); + try testing.expectEqual(old_first, boundary.end.node); + try testing.expectEqual(@as(size.CellCountInt, 1), boundary.start.y); + try testing.expectEqual(@as(size.CellCountInt, 8), boundary.start.x); + try testing.expectEqual(@as(size.CellCountInt, 0), boundary.end.y); + try testing.expectEqual(@as(size.CellCountInt, 1), boundary.end.x); + const str = try t.screens.active.selectionString(alloc, .{ + .sel = .init(boundary.start, boundary.end, false), + }); + defer alloc.free(str); + try testing.expectEqualStrings("Test", str); + } + { + const oldest = search.history_results.items[2].untracked(); + try testing.expectEqual(newest, oldest.start.node); + try testing.expectEqual(@as(size.CellCountInt, 0), oldest.start.y); + try testing.expectEqual(@as(size.CellCountInt, 0), oldest.start.x); + } + + // The selection kept both its index and its target. + try testing.expectEqual(selected_idx, search.selected.?.idx); + { + const selected_after = search.selectedMatch().?.untracked(); + try testing.expect(selected_before.start.eql(selected_after.start)); + try testing.expect(selected_before.end.eql(selected_after.end)); + } + + // Restore an even older page. Each incremental step is discovered from + // the frontier left behind by the previous one. + { + var allocation = try list.allocatePage(.{ .cols = 10, .rows = 2 }); + defer allocation.deinit(); + const page = allocation.page(); + page.size.rows = 2; + for ("Test", 0..) |c, x| page.getRowAndCell(x, 0).cell.* = .init(c); + try allocation.finalize(.prepend); + } + const oldest_node = list.pages.first.?; + try search.feed(); + try search.searchAll(); + try testing.expectEqual(4, search.history_results.items.len); + try testing.expectEqual( + oldest_node, + search.history_results.items[3].untracked().start.node, + ); + try testing.expectEqual(selected_idx, search.selected.?.idx); + { + const selected_after = search.selectedMatch().?.untracked(); + try testing.expect(selected_before.start.eql(selected_after.start)); + try testing.expect(selected_before.end.eql(selected_after.end)); + } + + // With nothing new, a feed on the complete search stays complete. + try search.feed(); + try testing.expect(search.state.isComplete()); + try testing.expectEqual(5, search.matchesLen()); +} diff --git a/vendor/libghostty-vt/src/terminal/search/sliding_window.zig b/vendor/libghostty-vt/src/terminal/search/sliding_window.zig index 2f858ec8..95b37d73 100644 --- a/vendor/libghostty-vt/src/terminal/search/sliding_window.zig +++ b/vendor/libghostty-vt/src/terminal/search/sliding_window.zig @@ -714,14 +714,15 @@ test "SlidingWindow empty on init" { test "SlidingWindow empty needle has no matches" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, ""); defer w.deinit(); - var s = try Screen.init(alloc, .{ + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); try s.testWriteString("hello"); @@ -733,11 +734,12 @@ test "SlidingWindow empty needle has no matches" { test "SlidingWindow single append" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "boo!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello. boo! hello. boo!"); @@ -778,11 +780,12 @@ test "SlidingWindow single append" { test "SlidingWindow single append case insensitive ASCII" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "Boo!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello. boo! hello. boo!"); @@ -823,11 +826,12 @@ test "SlidingWindow single append case insensitive ASCII" { test "SlidingWindow single append single char" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "b"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello. boo! hello. boo!"); @@ -868,11 +872,12 @@ test "SlidingWindow single append single char" { test "SlidingWindow single append no match" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "nope!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello. boo! hello. boo!"); @@ -892,11 +897,12 @@ test "SlidingWindow single append no match" { test "SlidingWindow two pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "boo!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -947,11 +953,12 @@ test "SlidingWindow two pages" { test "SlidingWindow two pages single char" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "b"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1002,11 +1009,12 @@ test "SlidingWindow two pages single char" { test "SlidingWindow two pages match across boundary" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "hello, world"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1047,11 +1055,12 @@ test "SlidingWindow two pages match across boundary" { test "SlidingWindow two pages no match across boundary with newline" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "hello, world"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1080,11 +1089,12 @@ test "SlidingWindow two pages no match across boundary with newline" { test "SlidingWindow two pages no match across boundary with newline reverse" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "hello, world"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1110,11 +1120,12 @@ test "SlidingWindow two pages no match across boundary with newline reverse" { test "SlidingWindow two pages no match prunes first page" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "nope!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1145,8 +1156,9 @@ test "SlidingWindow two pages no match prunes first page" { test "SlidingWindow two pages no match keeps both pages" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1185,11 +1197,12 @@ test "SlidingWindow two pages no match keeps both pages" { test "SlidingWindow single append across circular buffer boundary" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "abc"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("XXXXXXXXXXXXXXXXXXXboo!XXXXX"); @@ -1241,11 +1254,12 @@ test "SlidingWindow single append across circular buffer boundary" { test "SlidingWindow single append match on boundary" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "abcd"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("o!XXXXXXXXXXXXXXXXXXXbo"); @@ -1300,11 +1314,12 @@ test "SlidingWindow single append match on boundary" { test "SlidingWindow single append reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "boo!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello. boo! hello. boo!"); @@ -1345,11 +1360,12 @@ test "SlidingWindow single append reversed" { test "SlidingWindow single append no match reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "nope!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("hello. boo! hello. boo!"); @@ -1369,11 +1385,12 @@ test "SlidingWindow single append no match reversed" { test "SlidingWindow two pages reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "boo!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1424,11 +1441,12 @@ test "SlidingWindow two pages reversed" { test "SlidingWindow two pages match across boundary reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "hello, world"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1479,11 +1497,12 @@ test "SlidingWindow two pages match across boundary reversed" { test "SlidingWindow two pages no match prunes first page reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "nope!"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1514,8 +1533,9 @@ test "SlidingWindow two pages no match prunes first page reversed" { test "SlidingWindow two pages no match keeps both pages reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 1000 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 1000 }); defer s.deinit(); // Fill up the first page. The final bytes in the first page @@ -1554,11 +1574,12 @@ test "SlidingWindow two pages no match keeps both pages reversed" { test "SlidingWindow single append across circular buffer boundary reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "abc"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("XXXXXXXXXXXXXXXXXXXboo!XXXXX"); @@ -1611,11 +1632,12 @@ test "SlidingWindow single append across circular buffer boundary reversed" { test "SlidingWindow single append match on boundary reversed" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "abcd"); defer w.deinit(); - var s = try Screen.init(alloc, .{ .cols = 80, .rows = 24, .max_scrollback = 0 }); + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, .max_scrollback_bytes = 0 }); defer s.deinit(); try s.testWriteString("o!XXXXXXXXXXXXXXXXXXXbo"); @@ -1671,11 +1693,12 @@ test "SlidingWindow single append match on boundary reversed" { test "SlidingWindow single append soft wrapped" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "boo!"); defer w.deinit(); - var t: Terminal = try .init(alloc, .{ .cols = 4, .rows = 5 }); + var t: Terminal = try .init(io, alloc, .{ .cols = 4, .rows = 5 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -1708,11 +1731,12 @@ test "SlidingWindow single append soft wrapped" { test "SlidingWindow single append reversed soft wrapped" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .reverse, "boo!"); defer w.deinit(); - var t: Terminal = try .init(alloc, .{ .cols = 4, .rows = 5 }); + var t: Terminal = try .init(io, alloc, .{ .cols = 4, .rows = 5 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -1747,14 +1771,15 @@ test "SlidingWindow single append reversed soft wrapped" { test "SlidingWindow append whitespace only node" { const testing = std.testing; const alloc = testing.allocator; + const io = testing.io; var w: SlidingWindow = try .init(alloc, .forward, "x"); defer w.deinit(); - var s = try Screen.init(alloc, .{ + var s = try Screen.init(io, alloc, .{ .cols = 80, .rows = 24, - .max_scrollback = 0, + .max_scrollback_bytes = 0, }); defer s.deinit(); diff --git a/vendor/libghostty-vt/src/terminal/search/terminal.zig b/vendor/libghostty-vt/src/terminal/search/terminal.zig new file mode 100644 index 00000000..19f059c6 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/search/terminal.zig @@ -0,0 +1,767 @@ +const std = @import("std"); +const testing = std.testing; +const Allocator = std.mem.Allocator; +const point = @import("../point.zig"); +const FlattenedHighlight = @import("../highlight.zig").Flattened; +const ScreenSet = @import("../ScreenSet.zig"); +const Terminal = @import("../Terminal.zig"); + +const ScreenSearch = @import("screen.zig").ScreenSearch; +const ViewportSearch = @import("viewport.zig").ViewportSearch; + +const log = std.log.scoped(.search_terminal); + +/// Searches for a needle within an entire Terminal, orchestrating a +/// ScreenSearch per live screen plus a viewport search for the active +/// screen. +/// +/// This reconciles per-screen searchers against the terminal's active +/// ScreenSet, retains results across primary/alt screen switches, and +/// recovers from resize/reflow, resets, scrollback pruning, etc. +/// +/// The main ownership contract is that this is safe to call concurrently +/// with Terminal IO as long as you're not calling a function that takes +/// a terminal as an argument. Each function has more specific details about +/// its behavior. +pub const TerminalSearch = struct { + /// Allocator used for all search state. + alloc: Allocator, + + /// Active viewport search for the active screen. + viewport: ViewportSearch, + + /// The searchers for all the screens. + screens: std.EnumMap(ScreenSet.Key, ScreenSearch), + + /// ScreenSet generations captured when each searcher was initialized. + /// Allocators may reuse a destroyed Screen address, so pointer equality + /// alone cannot distinguish replacement screens from stale handles. + screen_generations: std.EnumMap(ScreenSet.Key, usize), + + /// The active screen key as of the last feed. All single-screen + /// reads (total matches, selection, etc.) resolve against this. + active_key: ScreenSet.Key, + + /// True when the cached viewport matches are stale and must be + /// recollected from the viewport searcher on the next + /// `viewportMatches` call. + stale_viewport_matches: bool, + + /// Cached viewport matches. The viewport search's sliding window + /// drains on read, so results are collected once per viewport + /// change and cached here. + viewport_matches: std.ArrayList(FlattenedHighlight), + + /// Overall status of the search. See `status`. + pub const Status = enum { + /// `tick` can make progress without terminal access. + running, + + /// Blocked until the next `feed`. This is also the initial + /// state, since a search that has never been fed has never + /// seen the terminal. + feed_required, + + /// Caught up with the terminal state as of the last feed. + complete, + }; + + /// Viewport scroll behavior applied by `select` when a match + /// becomes selected. + pub const SelectScroll = enum { + /// Scroll so the match is visible, only if it is not already. + if_needed, + + /// Never scroll. + none, + }; + + /// Initialize a search for the given needle. The needle is copied. + /// + /// This doesn't read any terminal state. The first `feed` does. + pub fn init( + alloc: Allocator, + needle_unowned: []const u8, + ) Allocator.Error!TerminalSearch { + var vp: ViewportSearch = try .init(alloc, needle_unowned); + errdefer vp.deinit(); + + // We use dirty tracking for active area changes. Start with it + // dirty so the first change is re-searched. + vp.active_dirty = true; + + return .{ + .alloc = alloc, + .viewport = vp, + .screens = .init(.{}), + .screen_generations = .init(.{}), + .active_key = .primary, + .stale_viewport_matches = true, + .viewport_matches = .empty, + }; + } + + /// Release all state. The terminal must be the same one given to + /// every other call, or null if the terminal has already been + /// deinitialized. When the terminal is alive this releases tracked + /// pins held within it. When it is null, those pins died with the + /// terminal's page storage, so only search-owned memory is freed. + pub fn deinit(self: *TerminalSearch, t_: ?*Terminal) void { + self.clearViewportMatches(); + self.viewport_matches.deinit(self.alloc); + self.viewport.deinit(); + var it = self.screens.iterator(); + while (it.next()) |entry| { + const valid = if (t_) |t| self.screenIsValid( + &t.screens, + entry.key, + entry.value, + ) else false; + if (valid) { + entry.value.deinit(); + } else { + entry.value.deinitScreenInvalid(); + } + } + } + + fn screenIsValid( + self: *const TerminalSearch, + screens: *const ScreenSet, + key: ScreenSet.Key, + search: *const ScreenSearch, + ) bool { + const generation = self.screen_generations.get(key) orelse return false; + if (generation != screens.generation(key)) return false; + const actual = screens.get(key) orelse return false; + return actual == search.screen; + } + + /// The needle that this search is using, borrowed. + pub fn needle(self: *const TerminalSearch) []const u8 { + return self.viewport.needle(); + } + + /// The searcher for the active screen as of the last feed. Null if + /// the search has never been fed (or the screen failed to + /// initialize). + pub fn activeScreenSearch(self: *TerminalSearch) ?*ScreenSearch { + return self.screens.getPtr(self.active_key); + } + + /// Returns true if all searches on all screens are complete. + pub fn isComplete(self: *TerminalSearch) bool { + var it = self.screens.iterator(); + while (it.next()) |entry| { + if (!entry.value.state.isComplete()) return false; + } + + return true; + } + + /// The overall search status derived from the per-screen search + /// states. + /// + /// Unlike `isComplete`, a search that has never been fed reports + /// `feed_required` rather than `complete`. An empty screen map + /// means the search has never seen the terminal, so reporting it + /// as complete would be technically true but useless to a caller + /// deciding what to do next. + pub fn status(self: *TerminalSearch) Status { + var saw_any = false; + var result: Status = .complete; + var it = self.screens.iterator(); + while (it.next()) |entry| { + saw_any = true; + switch (entry.value.state) { + // Progress is possible without a feed. + .active, .history => return .running, + + // Blocked until fed. + .history_feed => result = .feed_required, + + .complete => {}, + } + } + + if (!saw_any) return .feed_required; + return result; + } + + pub const Tick = enum { + /// All searches are complete. + complete, + + /// Progress was made on at least one screen. + progress, + + /// All incomplete searches are blocked on feed. + blocked, + }; + + /// Tick the search forward as much as possible without reading + /// any terminal state, so this is safe to call concurrently with + /// terminal IO. Returns the overall tick progress. + pub fn tick(self: *TerminalSearch) Tick { + var result: Tick = .complete; + var it = self.screens.iterator(); + while (it.next()) |entry| { + if (entry.value.tick()) { + result = .progress; + } else |err| switch (err) { + // Ignore... nothing we can do. + error.OutOfMemory => log.warn( + "error ticking screen search key={} err={}", + .{ entry.key, err }, + ), + + // Ignore, good for us. State remains whatever it is. + error.SearchComplete => {}, + + // Ignore, too, progressed + error.FeedRequired => switch (result) { + // If we think we're complete, we're not because we're + // blocked now (nothing made progress). + .complete => result = .blocked, + + // If we made some progress, we remain in progress + // since blocked means no progress at all. + .progress => {}, + + // If we're blocked already then we remain blocked. + .blocked => {}, + }, + } + } + + return result; + } + + /// Read the terminal to update any search state that requires it, + /// such as reconciling screens, feeding more data to the + /// searchers, and detecting viewport changes. + /// + /// This reads the terminal, so the caller must ensure the terminal + /// isn't modified for the duration of this call (e.g. by holding a + /// lock). Feeding is also the only way the search learns about + /// terminal changes, so callers should feed periodically while a + /// search is in use, even after it reports complete. + /// + /// `active_dirty` should be true when the active area may have + /// changed since the last feed, which triggers a re-scan of the + /// active area. Callers that know when the active area changes + /// (e.g. Ghostty's renderer-maintained dirty flag) can pass false + /// to skip the re-scan. Callers without that knowledge should + /// always pass true. That is always correct, just slightly more + /// work, and the active area search is cheap by design. + pub fn feed( + self: *TerminalSearch, + t: *Terminal, + active_dirty: bool, + ) void { + const alloc = self.alloc; + + // Update our active screen + if (t.screens.active_key != self.active_key) { + self.active_key = t.screens.active_key; + } + + // Reconcile our screens with the terminal screens. Remove + // searchers for screens that no longer exist and add searchers + // for screens that do exist but we don't have yet. + { + // Remove screens we have that no longer exist or changed. + var it = self.screens.iterator(); + while (it.next()) |entry| { + const remove = !self.screenIsValid( + &t.screens, + entry.key, + entry.value, + ); + + if (remove) { + entry.value.deinitScreenInvalid(); + _ = self.screens.remove(entry.key); + _ = self.screen_generations.remove(entry.key); + } + } + } + { + // Add screens that exist but we don't have yet. + var it = t.screens.all.iterator(); + while (it.next()) |entry| { + if (self.screens.contains(entry.key)) continue; + const screen_search = ScreenSearch.init( + alloc, + entry.value.*, + self.viewport.needle(), + ) catch |err| switch (err) { + error.OutOfMemory => { + // OOM is probably going to sink the entire ship but + // we can just ignore it and wait on the next + // reconciliation to try again. + log.warn( + "error initializing screen search for key={} err={}", + .{ entry.key, err }, + ); + continue; + }, + }; + self.screens.put(entry.key, screen_search); + self.screen_generations.put( + entry.key, + t.screens.generation(entry.key), + ); + } + } + + // The caller told us the active area may have changed, so mark + // our viewport searcher dirty (forcing a re-search) and reload + // the active area for the active screen. + if (active_dirty) { + // Mark our viewport dirty so it researches the active + self.viewport.active_dirty = true; + + // Reload our active area for our active screen + if (self.screens.getPtr(t.screens.active_key)) |screen_search| { + screen_search.reloadActive() catch |err| switch (err) { + error.OutOfMemory => log.warn( + "error reloading active area for screen key={} err={}", + .{ t.screens.active_key, err }, + ), + }; + } + } + + // Check our viewport for changes. + if (self.viewport.update(&t.screens.active.pages)) |updated| { + if (updated) self.stale_viewport_matches = true; + } else |err| switch (err) { + error.OutOfMemory => log.warn( + "error updating viewport search err={}", + .{err}, + ), + } + + // Feed data + var it = self.screens.iterator(); + while (it.next()) |entry| { + if (entry.value.state.needsFeed()) { + entry.value.feed() catch |err| switch (err) { + error.OutOfMemory => log.warn( + "error feeding screen search key={} err={}", + .{ entry.key, err }, + ), + }; + } + } + } + + /// Return the matches on the pages covering the viewport, as of + /// the last feed. Note this can include matches slightly outside + /// the visible viewport when they share a page with it. + /// + /// The results are cached, since the underlying viewport search + /// drains as it is read. Matches are collected once after a feed + /// notices a viewport change and the cached results are returned + /// otherwise. The returned slice is owned by the search and valid + /// until the next call to this function, the next feed, or deinit. + /// + /// This doesn't read any terminal state, so it is safe to call + /// concurrently with terminal IO. + pub fn viewportMatches( + self: *TerminalSearch, + ) Allocator.Error![]const FlattenedHighlight { + if (!self.stale_viewport_matches) return self.viewport_matches.items; + + // We always mark the cache fresh, even if collection fails + // below: a failed collection isn't retried until the next feed + // marks the cache stale again. + self.stale_viewport_matches = false; + + self.clearViewportMatches(); + errdefer { + self.clearViewportMatches(); + + // Reset the viewport so we force an update (and therefore a + // re-collection) on the next feed. + self.viewport.reset(); + } + while (self.viewport.next()) |hl| { + var hl_cloned = try hl.clone(self.alloc); + errdefer hl_cloned.deinit(self.alloc); + try self.viewport_matches.append(self.alloc, hl_cloned); + } + + return self.viewport_matches.items; + } + + fn clearViewportMatches(self: *TerminalSearch) void { + for (self.viewport_matches.items) |*hl| hl.deinit(self.alloc); + self.viewport_matches.clearRetainingCapacity(); + } + + /// Select the next or previous search result on the active screen, + /// wrapping around at the ends, and optionally scrolling the + /// viewport so the newly selected match is visible. + /// + /// This feeds first so the selection always works against current + /// terminal state, making it safe to call at any time relative to + /// feeds. Like `feed`, this reads (and possibly scrolls) the + /// terminal, so the caller must ensure the terminal isn't + /// otherwise being used for the duration of this call. + /// + /// Returns true if a match is selected after the operation, or + /// false if there are no matches to select. + pub fn select( + self: *TerminalSearch, + t: *Terminal, + to: ScreenSearch.Select, + scroll: SelectScroll, + ) Allocator.Error!bool { + // A screen can be removed or replaced between feeds. Reconcile + // while holding the terminal lock before touching any + // ScreenSearch pins. + self.feed(t, false); + const screen_search = self.screens.getPtr(self.active_key) orelse + return false; + + // Make the selection. Ignore the result because we don't + // care if the selection didn't change. + _ = try screen_search.select(to); + + // Grab our match if we have one. If we don't have a selection + // then there was nothing to select. + const flattened = screen_search.selectedMatch() orelse return false; + + switch (scroll) { + .none => return true, + .if_needed => {}, + } + + // Grab the current screen and see if this match is visible within + // the viewport already. If it is, we do nothing. + const screen = t.screens.get(self.active_key) orelse return true; + + // Grab the viewport. Viewports and selections are usually small + // so this check isn't very expensive, despite appearing O(N^2), + // both Ns are usually equal to 1. + var it = screen.pages.pageIterator( + .right_down, + .{ .viewport = .{} }, + null, + ); + const hl_chunks = flattened.chunks.slice(); + while (it.next()) |chunk| { + for (0..hl_chunks.len) |i| { + const hl_chunk = hl_chunks.get(i); + if (chunk.overlaps(.{ + .node = hl_chunk.node, + .start = hl_chunk.start, + .end = hl_chunk.end, + })) return true; + } + } + + screen.scroll(.{ .pin = flattened.startPin() }); + return true; + } +}; + +test "starts feed required and runs to complete" { + const alloc = testing.allocator; + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2 }); + defer t.deinit(alloc); + + var stream = t.vtStream(); + defer stream.deinit(); + stream.nextSlice("Fizz\r\nBuzz\r\nFizz\r\nBang"); + + var search: TerminalSearch = try .init(alloc, "Fizz"); + defer search.deinit(&t); + + // A fresh search has never seen the terminal, so it must report + // feed_required even though isComplete() is trivially true. + try testing.expect(search.isComplete()); + try testing.expectEqual(TerminalSearch.Status.feed_required, search.status()); + + // Pump until complete. + while (search.status() != .complete) { + switch (search.status()) { + .feed_required => search.feed(&t, true), + .running => _ = search.tick(), + .complete => unreachable, + } + } + + const screen_search = search.activeScreenSearch().?; + try testing.expectEqual(2, screen_search.matchesLen()); +} + +test "viewport matches are cached until the next feed" { + const alloc = testing.allocator; + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); + defer t.deinit(alloc); + + var stream = t.vtStream(); + defer stream.deinit(); + stream.nextSlice("Fizz\r\nBuzz\r\nFizz\r\nBang"); + + var search: TerminalSearch = try .init(alloc, "Fizz"); + defer search.deinit(&t); + search.feed(&t, true); + + // The sliding window drains on read, so a second read must return + // the cached results rather than an empty list. + try testing.expectEqual(2, (try search.viewportMatches()).len); + try testing.expectEqual(2, (try search.viewportMatches()).len); + + // A feed that observes a change refreshes the cache. + stream.nextSlice("\r\nFizz"); + search.feed(&t, true); + try testing.expectEqual(3, (try search.viewportMatches()).len); +} + +test "select after active screen removal" { + const alloc = testing.allocator; + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 20, .rows = 2 }); + defer t.deinit(alloc); + + _ = try t.switchScreen(.alternate); + + var search: TerminalSearch = try .init(alloc, "needle"); + defer search.deinit(&t); + search.feed(&t, false); + try testing.expectEqual(ScreenSet.Key.alternate, search.active_key); + try testing.expect(search.screens.contains(.alternate)); + + _ = try t.switchScreen(.primary); + t.screens.remove(alloc, .alternate); + + // The select must reconcile against the live ScreenSet before + // touching any pins from the removed screen. + _ = try search.select(&t, .next, .if_needed); + try testing.expectEqual(ScreenSet.Key.primary, search.active_key); + try testing.expect(!search.screens.contains(.alternate)); +} + +test "select scrolls the viewport only when needed" { + const alloc = testing.allocator; + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ + .cols = 10, + .rows = 2, + .max_scrollback_bytes = std.math.maxInt(usize), + }); + defer t.deinit(alloc); + + var stream = t.vtStream(); + defer stream.deinit(); + stream.nextSlice("Fizz\r\n"); + for (0..30) |_| stream.nextSlice("\r\n"); + stream.nextSlice("Fizz"); + + var search: TerminalSearch = try .init(alloc, "Fizz"); + defer search.deinit(&t); + while (search.status() != .complete) { + switch (search.status()) { + .feed_required => search.feed(&t, true), + .running => _ = search.tick(), + .complete => unreachable, + } + } + + // Whether the selected match is within the visible viewport rows. + // pointFromPin only bounds-checks the top of the region, so rows + // below the viewport must be rejected by the row count. + const Visible = struct { + fn check(term: *Terminal, s: *TerminalSearch) bool { + const pin = s.activeScreenSearch().?.selectedMatch().?.startPin(); + const pages = &term.screens.active.pages; + const pt = pages.pointFromPin(.viewport, pin) orelse return false; + return pt.viewport.y < pages.rows; + } + }; + + // First match is at the bottom which is already visible: no scroll. + try testing.expect(try search.select(&t, .next, .if_needed)); + try testing.expectEqual( + point.Point{ .active = .{ .x = 0, .y = 1 } }, + t.screens.active.pages.pointFromPin( + .active, + search.activeScreenSearch().?.selectedMatch().?.startPin(), + ).?, + ); + try testing.expect(Visible.check(&t, &search)); + + // Second match is in scrollback: selecting it must scroll the + // viewport so it becomes visible. + try testing.expect(try search.select(&t, .next, .if_needed)); + try testing.expect(Visible.check(&t, &search)); + + // With scrolling disabled the viewport must stay where it is even + // though the next selection (wrap back to the bottom) is not + // visible. + try testing.expect(try search.select(&t, .next, .none)); + try testing.expect(!Visible.check(&t, &search)); +} + +test "deinit after the terminal is gone" { + const alloc = testing.allocator; + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ + .cols = 10, + .rows = 2, + .max_scrollback_bytes = std.math.maxInt(usize), + }); + + var stream = t.vtStream(); + stream.nextSlice("Fizz\r\nBuzz\r\nFizz"); + + // Run to complete and select a match so the search holds tracked + // pins within the terminal's page storage. + var search: TerminalSearch = try .init(alloc, "Fizz"); + while (search.status() != .complete) { + switch (search.status()) { + .feed_required => search.feed(&t, true), + .running => _ = search.tick(), + .complete => unreachable, + } + } + try testing.expect(try search.select(&t, .next, .none)); + + // Deinitialize the terminal first. The pins died with the page + // storage, so deinit with a null terminal must free only + // search-owned memory without touching the terminal. + stream.deinit(); + t.deinit(alloc); + search.deinit(null); +} + +test "no matches selects nothing" { + const alloc = testing.allocator; + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2 }); + defer t.deinit(alloc); + + var search: TerminalSearch = try .init(alloc, "Fizz"); + defer search.deinit(&t); + search.feed(&t, true); + try testing.expect(!try search.select(&t, .next, .if_needed)); + try testing.expect(!try search.select(&t, .prev, .if_needed)); +} + +test "feed after complete discovers prepended snapshot history" { + const alloc = testing.allocator; + const io = testing.io; + const snapshot = @import("../snapshot/main.zig"); + + // A source terminal with several pages of scrollback where every line + // is a match, so the expected total is simply the number of lines. + var source: Terminal = try .init(io, alloc, .{ + .cols = 10, + .rows = 2, + .max_scrollback_bytes = std.math.maxInt(usize), + }); + defer source.deinit(alloc); + var needle_count: usize = 0; + { + var stream = source.vtStream(); + defer stream.deinit(); + const list = &source.screens.active.pages; + while (list.totalPages() < 4) : (needle_count += 1) { + stream.nextSlice("needle\r\n"); + } + } + + var encoded: std.Io.Writer.Allocating = .init(alloc); + defer encoded.deinit(); + try snapshot.encode(alloc, &encoded.writer, &source, .{ + .continuation = .ground, + }); + + // Restore only through READY. The terminal is usable while its history + // pages are still in flight. + var reader: std.Io.Reader = .fixed(encoded.written()); + var decoder: snapshot.Decoder = .init(&reader); + var decoded = try decoder.ready(alloc, io, .{ + .max_continuation_bytes = 0, + }); + defer decoded.deinit(alloc); + var t = decoded.toOwned(); + defer t.deinit(alloc); + const pages_at_ready = t.screens.active.pages.totalPages(); + + const Pump = struct { + fn run(search: *TerminalSearch, term: *Terminal) void { + while (search.status() != .complete) { + switch (search.status()) { + .feed_required => search.feed(term, true), + .running => _ = search.tick(), + .complete => unreachable, + } + } + } + }; + + // Search the READY state to completion and select the oldest match. + var search: TerminalSearch = try .init(alloc, "needle"); + defer search.deinit(&t); + Pump.run(&search, &t); + const partial = search.activeScreenSearch().?.matchesLen(); + try testing.expect(partial > 0); + try testing.expect(partial < needle_count); + try testing.expect(try search.select(&t, .prev, .none)); + const selected_idx = search.activeScreenSearch().?.selected.?.idx; + try testing.expectEqual(partial - 1, selected_idx); + const selected_before = search.activeScreenSearch().?.selectedMatch().?.untracked(); + + // Restore every history page below the live terminal. + var restored_pages: usize = 0; + while (try decoder.next(alloc, &t)) |progress| : (restored_pages += 1) { + try testing.expect(progress.rows > 0); + } + try testing.expect(restored_pages > 0); + try testing.expectEqual( + pages_at_ready + restored_pages, + t.screens.active.pages.totalPages(), + ); + + // Refresh the existing search the way ghostty_search_run does: feed, + // then tick to completion. It must now cover the restored history. + search.feed(&t, true); + Pump.run(&search, &t); + const screen_search = search.activeScreenSearch().?; + try testing.expectEqual(needle_count, screen_search.matchesLen()); + + // Restored results are older than everything already cached, so the + // selection keeps both its index and its target. + try testing.expectEqual(selected_idx, screen_search.selected.?.idx); + const selected_after = screen_search.selectedMatch().?.untracked(); + try testing.expect(selected_before.start.eql(selected_after.start)); + try testing.expect(selected_before.end.eql(selected_after.end)); + + // Results stay ordered newest to oldest, ending at the very first line. + const matches = try screen_search.matches(alloc); + defer alloc.free(matches); + const pages = &t.screens.active.pages; + var prev_y: ?usize = null; + for (matches) |hl| { + const pt = pages.pointFromPin(.screen, hl.startPin()).?; + if (prev_y) |y| try testing.expect(pt.screen.y < y); + prev_y = pt.screen.y; + } + try testing.expectEqual(0, prev_y.?); + + // A search created after the restore agrees with the refreshed one. + var fresh: TerminalSearch = try .init(alloc, "needle"); + defer fresh.deinit(&t); + Pump.run(&fresh, &t); + try testing.expectEqual( + needle_count, + fresh.activeScreenSearch().?.matchesLen(), + ); +} diff --git a/vendor/libghostty-vt/src/terminal/search/viewport.zig b/vendor/libghostty-vt/src/terminal/search/viewport.zig index 09d202be..93e63b88 100644 --- a/vendor/libghostty-vt/src/terminal/search/viewport.zig +++ b/vendor/libghostty-vt/src/terminal/search/viewport.zig @@ -228,7 +228,8 @@ pub const ViewportSearch = struct { test "simple search" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -271,7 +272,8 @@ test "simple search" { test "fingerprint detects node generation changes" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var search: ViewportSearch = try .init(alloc, "needle"); @@ -298,7 +300,8 @@ test "fingerprint detects node generation changes" { test "fingerprint only allocates on changes" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var failing = testing.FailingAllocator.init(alloc, .{}); @@ -320,7 +323,8 @@ test "fingerprint only allocates on changes" { test "clear screen and search" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -353,7 +357,8 @@ test "clear screen and search" { test "clear screen and search dirty tracking" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 10 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 10 }); defer t.deinit(alloc); var s = t.vtStream(); @@ -400,7 +405,8 @@ test "clear screen and search dirty tracking" { test "history search, no active area" { const alloc = testing.allocator; - var t: Terminal = try .init(alloc, .{ .cols = 10, .rows = 2 }); + const io = testing.io; + var t: Terminal = try .init(io, alloc, .{ .cols = 10, .rows = 2 }); defer t.deinit(alloc); var s = t.vtStream(); diff --git a/vendor/libghostty-vt/src/terminal/sgr.zig b/vendor/libghostty-vt/src/terminal/sgr.zig index e9b9354c..3fecd446 100644 --- a/vendor/libghostty-vt/src/terminal/sgr.zig +++ b/vendor/libghostty-vt/src/terminal/sgr.zig @@ -161,7 +161,18 @@ pub const Attribute = union(Tag) { // Largest variant is Unknown.C: 2 pointers + 2 usize = 32 bytes on 64-bit. // We use [8]u64 (64 bytes) to allow room for future expansion while // maintaining ABI compatibility. - [8]u64, + .{ + .padding = [8]u64, + .field_renames = .{ + .@"256_underline_color" = "underline_color_256", + .@"8_bg" = "bg_8", + .@"8_fg" = "fg_8", + .@"8_bright_bg" = "bright_bg_8", + .@"8_bright_fg" = "bright_fg_8", + .@"256_bg" = "bg_256", + .@"256_fg" = "fg_256", + }, + }, ); pub const Value = c_union.Value; pub const C = c_union.C; diff --git a/vendor/libghostty-vt/src/terminal/snapshot/AGENTS.md b/vendor/libghostty-vt/src/terminal/snapshot/AGENTS.md new file mode 100644 index 00000000..0ce37b5e --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/AGENTS.md @@ -0,0 +1,7 @@ +# Terminal Binary Snapshot + +- Apply the robustness principle: "be conservative in what you do, be liberal + in what you accept from others." + - Validate on encode + - Gracefully degrade on decode, for example invalid styles degrade to + ignoring the style. diff --git a/vendor/libghostty-vt/src/terminal/snapshot/checkpoint.zig b/vendor/libghostty-vt/src/terminal/snapshot/checkpoint.zig new file mode 100644 index 00000000..23ea755f --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/checkpoint.zig @@ -0,0 +1,177 @@ +//! READY and FINISH snapshot marker records. +//! +//! READY separates the renderable terminal state from the history sequences. +//! FINISH terminates one snapshot; bytes after it belong to the containing +//! transport. Both markers have empty payloads, independently framed and +//! protected by the same CRC32C as every other record. +//! +//! ## Binary Format +//! +//! READY and FINISH contain only their record headers. Their declared payload +//! length must be zero. + +const std = @import("std"); +const test_fixture = @import("fixture.zig"); +const record = @import("record.zig"); + +/// Selects one of the two marker positions in a snapshot. +pub const Kind = enum { + ready, + finish, + + fn tag(self: Kind) record.Tag { + return switch (self) { + .ready => .ready, + .finish => .finish, + }; + } +}; + +pub const EncodeError = record.Writer.FinishError; + +/// Append one empty marker record. +pub fn encode(kind: Kind, stream: *record.Writer) EncodeError!void { + _ = stream.begin(kind.tag()); + errdefer stream.cancel(); + try stream.finish(); +} + +pub const DecodeError = record.Reader.InitError || + record.Reader.FinishError || + error{ + /// The next record is valid but is not the expected marker. + UnexpectedRecordTag, + }; + +/// Decode one marker and require its payload to be empty. +pub fn decode( + kind: Kind, + source: *std.Io.Reader, +) DecodeError!void { + var record_reader: record.Reader = undefined; + try record_reader.init(source); + if (record_reader.header.tag != kind.tag()) { + return error.UnexpectedRecordTag; + } + try record_reader.finish(); +} + +const test_ready_fixture = test_fixture.parse( + @embedFile("testdata/checkpoint-ready-v1.hex"), +); + +test "READY golden encoding" { + const prefix = "abc"; + + var snapshot: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer snapshot.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &snapshot.writer, + ); + defer stream.deinit(); + try stream.writer().writeAll(prefix); + try encode(.ready, &stream); + + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/checkpoint-ready-v1.hex", + "snapshot_fixture-checkpoint-ready-v1.hex", + &test_ready_fixture, + snapshot.written(), + ); + + // Decode the checked-in record rather than the generated candidate. + var source: std.Io.Reader = .fixed(&test_ready_fixture); + try source.discardAll(prefix.len); + try decode(.ready, &source); +} + +test "READY and FINISH are empty marker records" { + const testing = std.testing; + + var snapshot: std.Io.Writer.Allocating = .init(testing.allocator); + defer snapshot.deinit(); + var stream: record.Writer = .init( + testing.allocator, + &snapshot.writer, + ); + defer stream.deinit(); + try stream.writer().writeAll("prefix"); + + const ready_offset = snapshot.written().len; + try encode(.ready, &stream); + try stream.writer().writeAll("history"); + const finish_offset = snapshot.written().len; + try encode(.finish, &stream); + + try testing.expectEqual( + record.Header.len, + finish_offset - ready_offset - "history".len, + ); + try testing.expectEqual( + record.Header.len, + snapshot.written().len - finish_offset, + ); + + var source: std.Io.Reader = .fixed(snapshot.written()); + try source.discardAll(ready_offset); + try decode(.ready, &source); + try source.discardAll("history".len); + try decode(.finish, &source); +} + +test "checkpoint rejects wrong tags and nonempty payloads" { + const testing = std.testing; + + var snapshot: std.Io.Writer.Allocating = .init(testing.allocator); + defer snapshot.deinit(); + var stream: record.Writer = .init( + testing.allocator, + &snapshot.writer, + ); + defer stream.deinit(); + try encode(.ready, &stream); + + var wrong_tag_source: std.Io.Reader = .fixed(snapshot.written()); + try testing.expectError( + error.UnexpectedRecordTag, + decode(.finish, &wrong_tag_source), + ); + + var invalid: std.Io.Writer.Allocating = .init(testing.allocator); + defer invalid.deinit(); + var invalid_stream: record.Writer = .init( + testing.allocator, + &invalid.writer, + ); + defer invalid_stream.deinit(); + const invalid_payload = invalid_stream.begin(.ready); + errdefer invalid_stream.cancel(); + try invalid_payload.writeByte(0); + try invalid_stream.finish(); + + var invalid_source: std.Io.Reader = .fixed(invalid.written()); + try testing.expectError( + error.PayloadNotExhausted, + decode(.ready, &invalid_source), + ); +} + +test "FINISH leaves continuation bytes unread" { + const testing = std.testing; + + var finished: std.Io.Writer.Allocating = .init(testing.allocator); + defer finished.deinit(); + var finished_stream: record.Writer = .init( + testing.allocator, + &finished.writer, + ); + defer finished_stream.deinit(); + try encode(.finish, &finished_stream); + try finished.writer.writeByte(0); + + var source: std.Io.Reader = .fixed(finished.written()); + try decode(.finish, &source); + try testing.expectEqual(@as(u8, 0), try source.takeByte()); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/continuation.zig b/vendor/libghostty-vt/src/terminal/snapshot/continuation.zig new file mode 100644 index 00000000..92b52da0 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/continuation.zig @@ -0,0 +1,320 @@ +//! Continuation record type. +//! +//! Continuation uses the standard record header. All trailing bytes +//! are payload bytes for the continuation state. +//! +//! A continuation's payload must be the minimal bytes to go from +//! a ground state to a non-ground state in a VT emulator. Any extra +//! bytes are treated as a validation error. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const record = @import("record.zig"); +const stream_continuation = @import("../stream_continuation.zig"); +const test_fixture = @import("fixture.zig"); + +/// A borrowed continuation value supplied to encode, or an allocator-owned +/// value stored in a decoded complete snapshot. +pub const Value = union(enum) { + /// Neither the VT parser nor UTF-8 decoder has unfinished state. + ground, + + /// Canonical replay-safe PTY bytes. Empty is equivalent to `ground`. + bytes: []const u8, +}; + +pub const ValidateError = stream_continuation.ValidateError || error{ + /// The common record's u32 payload length cannot represent these bytes. + Overflow, +}; + +/// Validate a continuation before any snapshot bytes are emitted. +pub fn validate(value: Value) ValidateError!void { + switch (value) { + .ground => {}, + .bytes => |bytes| { + if (std.math.cast(u32, bytes.len) == null) { + return error.Overflow; + } + try stream_continuation.validate(bytes); + }, + } +} + +pub const EncodeError = ValidateError || record.Writer.FinishError; + +/// Encode one complete CONTINUATION record. +pub fn encode( + value: Value, + stream: *record.Writer, +) EncodeError!void { + try validate(value); + + const payload = stream.begin(.continuation); + errdefer stream.cancel(); + switch (value) { + .ground => {}, + .bytes => |bytes| try payload.writeAll(bytes), + } + try stream.finish(); +} + +pub const DecodeError = Allocator.Error || + stream_continuation.ValidateError || + record.Reader.InitError || + record.Reader.FinishError || + std.Io.Reader.Error || + error{ + /// The next record is valid but is not CONTINUATION. + UnexpectedRecordTag, + + /// The declared payload exceeds caller policy. + ContinuationLimitExceeded, + }; + +/// Decode and validate one CONTINUATION record. +/// +/// A zero-length payload returns `ground` without allocating. A nonempty +/// payload returns allocator-owned bytes; although `Value.bytes` is a const +/// slice, the caller must eventually free it with the same allocator. +pub fn decode( + alloc: Allocator, + source: *std.Io.Reader, + max_bytes: usize, +) DecodeError!Value { + var record_reader: record.Reader = undefined; + try record_reader.init(source); + if (record_reader.header.tag != .continuation) { + return error.UnexpectedRecordTag; + } + + // Validate the header payload vs our max size since the header + // enforces it. + const len: usize = record_reader.header.payload_len; + if (len > max_bytes) return error.ContinuationLimitExceeded; + + if (len == 0) { + // Empty is the ground representation, but finish is still required: + // its CRC covers the tag and zero length even though there is no body. + try record_reader.finish(); + return .ground; + } + + // Consume the bytes + const bytes = try alloc.alloc(u8, len); + errdefer alloc.free(bytes); + try record_reader.payloadReader().readSliceAll(bytes); + try record_reader.finish(); + + // Framing is valid, so it is now safe to classify the bytes as replay + // state. Validation requires a minimal, unfinished, side-effect-free tail. + try stream_continuation.validate(bytes); + + // Ownership transfers to the returned Value only after all validation. + return .{ .bytes = bytes }; +} + +const test_ground_fixture = test_fixture.parse(@embedFile("testdata/continuation-ground-v1.hex")); +const test_utf8_fixture = test_fixture.parse(@embedFile("testdata/continuation-utf8-v1.hex")); +const test_esc_fixture = test_fixture.parse(@embedFile("testdata/continuation-esc-v1.hex")); +const test_csi_fixture = test_fixture.parse(@embedFile("testdata/continuation-csi-v1.hex")); +const test_osc_fixture = test_fixture.parse(@embedFile("testdata/continuation-osc-v1.hex")); +const test_dcs_fixture = test_fixture.parse(@embedFile("testdata/continuation-dcs-v1.hex")); +const test_apc_fixture = test_fixture.parse(@embedFile("testdata/continuation-apc-v1.hex")); + +test "continuation golden records" { + const Golden = struct { + path: []const u8, + candidate: []const u8, + value: Value, + expected: []const u8, + }; + const values = [_]Golden{ + .{ + .path = "src/terminal/snapshot/testdata/continuation-ground-v1.hex", + .candidate = "snapshot_fixture-continuation-ground-v1.hex", + .value = .ground, + .expected = &test_ground_fixture, + }, + .{ + .path = "src/terminal/snapshot/testdata/continuation-utf8-v1.hex", + .candidate = "snapshot_fixture-continuation-utf8-v1.hex", + .value = .{ .bytes = "\xF0\x9F\x98" }, + .expected = &test_utf8_fixture, + }, + .{ + .path = "src/terminal/snapshot/testdata/continuation-esc-v1.hex", + .candidate = "snapshot_fixture-continuation-esc-v1.hex", + .value = .{ .bytes = "\x1b" }, + .expected = &test_esc_fixture, + }, + .{ + .path = "src/terminal/snapshot/testdata/continuation-csi-v1.hex", + .candidate = "snapshot_fixture-continuation-csi-v1.hex", + .value = .{ .bytes = "\x1b[31" }, + .expected = &test_csi_fixture, + }, + .{ + .path = "src/terminal/snapshot/testdata/continuation-osc-v1.hex", + .candidate = "snapshot_fixture-continuation-osc-v1.hex", + .value = .{ .bytes = "\x1b]2;title" }, + .expected = &test_osc_fixture, + }, + .{ + .path = "src/terminal/snapshot/testdata/continuation-dcs-v1.hex", + .candidate = "snapshot_fixture-continuation-dcs-v1.hex", + .value = .{ .bytes = "\x1bPqdata" }, + .expected = &test_dcs_fixture, + }, + .{ + .path = "src/terminal/snapshot/testdata/continuation-apc-v1.hex", + .candidate = "snapshot_fixture-continuation-apc-v1.hex", + .value = .{ .bytes = "\x1b_Gdata" }, + .expected = &test_apc_fixture, + }, + }; + + for (values) |golden| { + var encoded: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer encoded.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &encoded.writer, + ); + defer stream.deinit(); + try encode(golden.value, &stream); + try test_fixture.expectEqual( + .bytes, + golden.path, + golden.candidate, + golden.expected, + encoded.written(), + ); + } +} + +test "continuation validates supported unfinished states" { + const values = [_][]const u8{ + "\x1b", + "\x1b[31", + "\x1b]2;title", + "\x1bPqdata", + "\x1b_Gdata", + "\xF0\x9F\x98", + }; + for (values) |bytes| try validate(.{ .bytes = bytes }); +} + +test "empty continuation bytes encode and decode as ground" { + const testing = std.testing; + try validate(.{ .bytes = "" }); + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + var stream: record.Writer = .init(testing.allocator, &encoded.writer); + defer stream.deinit(); + try encode(.{ .bytes = "" }, &stream); + try testing.expectEqualStrings(&test_ground_fixture, encoded.written()); + + var source: std.Io.Reader = .fixed(encoded.written()); + const decoded = try decode(testing.allocator, &source, 0); + try testing.expect(decoded == .ground); +} + +test "continuation rejects invalid semantic shapes" { + const testing = std.testing; + try testing.expectError( + error.NoPendingState, + validate(.{ .bytes = "\x1b[31m" }), + ); + try testing.expectError( + error.ReplayWouldCommit, + validate(.{ .bytes = "\x1b[31\x07" }), + ); + try testing.expectError( + error.NonCanonicalContinuation, + validate(.{ .bytes = "prefix\x1b[31" }), + ); + try testing.expectError( + error.NonCanonicalContinuation, + validate(.{ .bytes = "\x1b[31\x1b[4" }), + ); +} + +test "continuation record round trip and cap" { + const testing = std.testing; + const values = [_]Value{ + .ground, + .{ .bytes = "\x1b[31" }, + .{ .bytes = "\xF0\x9F" }, + }; + + for (values) |value| { + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + var stream: record.Writer = .init(testing.allocator, &encoded.writer); + defer stream.deinit(); + try encode(value, &stream); + + var source: std.Io.Reader = .fixed(encoded.written()); + const decoded = try decode(testing.allocator, &source, 1024); + defer switch (decoded) { + .ground => {}, + .bytes => |bytes| testing.allocator.free(bytes), + }; + switch (value) { + .ground => try testing.expect(decoded == .ground), + .bytes => |expected| try testing.expectEqualStrings( + expected, + decoded.bytes, + ), + } + } + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + var stream: record.Writer = .init(testing.allocator, &encoded.writer); + defer stream.deinit(); + try encode(.{ .bytes = "\x1b[31" }, &stream); + + var capped_source: std.Io.Reader = .fixed(encoded.written()); + var failing = testing.FailingAllocator.init(testing.allocator, .{ + .fail_index = 0, + }); + try testing.expectError( + error.ContinuationLimitExceeded, + decode(failing.allocator(), &capped_source, 3), + ); + try testing.expectEqual( + @as(usize, record.Header.len), + capped_source.seek, + ); +} + +test "continuation record rejects truncation, checksum, and tag" { + const testing = std.testing; + + for (0..test_csi_fixture.len) |len| { + var source: std.Io.Reader = .fixed(test_csi_fixture[0..len]); + try testing.expectError( + error.EndOfStream, + decode(testing.allocator, &source, 1024), + ); + } + + var invalid_checksum = test_csi_fixture; + invalid_checksum[6] ^= 1; + var checksum_source: std.Io.Reader = .fixed(&invalid_checksum); + try testing.expectError( + error.InvalidChecksum, + decode(testing.allocator, &checksum_source, 1024), + ); + + var wrong_tag = test_ground_fixture; + std.mem.writeInt(u16, wrong_tag[0..2], @intFromEnum(record.Tag.ready), .little); + var tag_source: std.Io.Reader = .fixed(&wrong_tag); + try testing.expectError( + error.UnexpectedRecordTag, + decode(testing.allocator, &tag_source, 1024), + ); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/envelope.zig b/vendor/libghostty-vt/src/terminal/snapshot/envelope.zig new file mode 100644 index 00000000..21fcefb9 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/envelope.zig @@ -0,0 +1,104 @@ +//! Snapshot envelope. +//! +//! Every snapshot binary blob begins with exactly one envelope at byte zero. +//! It is followed by a set of records. Each record provides their own +//! tag, payload length, CRC, etc. +//! +//! The envelope identifies the bytes as a terminal snapshot and selects the +//! single version governing the entire blob. A decoder validates both fields +//! before reading any records. +//! +//! The envelope is exactly ten bytes. All integers are unsigned and +//! little-endian. +//! +//! | Offset | Size | Field | +//! | -----: | ---: | :------------------- | +//! | 0 | 8 | Magic (`GHOSTSNP`) | +//! | 8 | 2 | Version (`u16`) | + +const std = @import("std"); +const test_fixture = @import("fixture.zig"); +const io = @import("io.zig"); + +/// Identifies a Ghostty terminal snapshot and rejects unrelated input before +/// any record decoding begins. All eight bytes are part of the wire value. +pub const magic = "GHOSTSNP"; + +/// The complete compatibility boundary for snapshot layout and behavior. +/// Version 1 readers require this value to match exactly. +pub const version: u16 = 1; + +/// Number of bytes in the fixed envelope: magic followed by version. +pub const encoded_len = computeLen(); + +comptime { + // We expect this so if it changes we should think carefully. + std.debug.assert(encoded_len == 10); +} + +pub const DecodeError = std.Io.Reader.Error || error{ + InvalidMagic, + UnsupportedVersion, +}; + +/// Encode the envelope. +pub fn encode(writer: *std.Io.Writer) std.Io.Writer.Error!void { + try writer.writeAll(magic); + try io.writeInt(writer, u16, version); +} + +/// Decode and validate the envelope. +pub fn decode(reader: *std.Io.Reader) DecodeError!void { + var actual_magic: [magic.len]u8 = undefined; + try reader.readSliceAll(&actual_magic); + if (!std.mem.eql(u8, magic, &actual_magic)) return error.InvalidMagic; + + const actual_version = try io.readInt(reader, u16); + if (actual_version != version) return error.UnsupportedVersion; +} + +fn computeLen() usize { + comptime { + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + encode(&writer) catch unreachable; + return writer.end; + } +} + +const test_golden_fixture = test_fixture.parse(@embedFile("testdata/envelope-v1.hex")); + +test "golden encoding" { + var buf: [encoded_len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try encode(&writer); + + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/envelope-v1.hex", + "snapshot_fixture-envelope-v1.hex", + &test_golden_fixture, + writer.buffered(), + ); + + var reader: std.Io.Reader = .fixed(&test_golden_fixture); + try decode(&reader); +} + +test "reject invalid magic and version" { + var invalid_magic: std.Io.Reader = .fixed("GHOSTSNX\x01\x00"); + try std.testing.expectError(error.InvalidMagic, decode(&invalid_magic)); + + var invalid_version: std.Io.Reader = .fixed("GHOSTSNP\x00\x00"); + try std.testing.expectError( + error.UnsupportedVersion, + decode(&invalid_version), + ); +} + +test "reject every truncation" { + for (0..encoded_len) |len| { + var reader: std.Io.Reader = .fixed(test_golden_fixture[0..len]); + try std.testing.expectError(error.EndOfStream, decode(&reader)); + } +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/fixture.zig b/vendor/libghostty-vt/src/terminal/snapshot/fixture.zig new file mode 100644 index 00000000..d5c86b2b --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/fixture.zig @@ -0,0 +1,479 @@ +//! Test-only support for reviewable snapshot golden fixtures. +//! +//! Snapshot compatibility references are stored as annotated hexadecimal text +//! under `testdata/`. Each byte is written as exactly two hexadecimal digits. +//! Whitespace is insignificant, and `#` begins a comment through the end of the +//! line. This permits field, record, and absolute-offset annotations without +//! changing the bytes embedded by a test. +//! +//! `parse` validates and decodes an `@embedFile` at comptime. Tests should +//! encode their native value, compare the generated bytes with that embedded +//! reference using `expectEqual`, and then decode the reference itself. This +//! keeps encoding regression coverage separate from backwards-decoding +//! coverage. +//! +//! `expectEqual` formats a fresh candidate on every run. Matching candidates +//! remain in the test's temporary directory. A mismatch copies the candidate +//! to the repository root, reports its path and first differing byte, and +//! fails without modifying the checked-in reference. After reviewing an +//! intentional wire change, a maintainer manually replaces the reference. +//! Fixture filenames include the snapshot version so a future format adds new +//! references instead of overwriting compatibility data for an older version. +//! +//! A typical fixed-value test looks like this: +//! +//! ```zig +//! const fixture = @import("fixture.zig"); +//! +//! const reference = fixture.parse( +//! @embedFile("testdata/example-v1.hex"), +//! ); +//! +//! test "example golden encoding and decoding" { +//! var encoded: [Example.len]u8 = undefined; +//! var writer: std.Io.Writer = .fixed(&encoded); +//! try Example.encode(value, &writer); +//! +//! try fixture.expectEqual( +//! .bytes, +//! "src/terminal/snapshot/testdata/example-v1.hex", +//! "snapshot_fixture-example-v1.hex", +//! &reference, +//! writer.buffered(), +//! ); +//! +//! var reader: std.Io.Reader = .fixed(&reference); +//! try std.testing.expectEqualDeep( +//! value, +//! try Example.decode(&reader), +//! ); +//! } +//! ``` +//! +//! ## Kaitai +//! +//! Every fixture has three machine-readable comment fields: +//! +//! ```text +//! # Kaitai type: page_payload +//! # Kaitai params: +//! # Kaitai offset: 0 +//! ``` +//! +//! These select a type from `snapshot.ksy`, its whitespace-separated +//! parameters, and the byte offset where parsing begins. The Kaitai verifier +//! discovers fixtures from the filesystem and uses this metadata instead of +//! maintaining a parallel filename table. Generated candidates copy the +//! metadata from their checked-in reference. +//! + +const std = @import("std"); +const envelope = @import("envelope.zig"); +const record = @import("record.zig"); + +const log = std.log.scoped(.snapshot_fixture); + +pub const Kind = enum { + bytes, + page, + snapshot, +}; + +/// Decode an annotated hexadecimal fixture at comptime. +/// +/// Whitespace is ignored. `#` begins a comment that continues through the end +/// of the line. Every other token must be exactly two hexadecimal digits. +pub fn parse(comptime source: []const u8) [decodedLen(source)]u8 { + comptime { + // Large complete-snapshot fixtures exceed Zig's default comptime + // branch budget while scanning their comments and byte tokens. + @setEvalBranchQuota(100_000_000); + + // The first pass in decodedLen gives this result its exact array type. + // Keeping it an array lets callers use the fixture in comptime slices. + var result: [decodedLen(source)]u8 = undefined; + var source_index: usize = 0; + var result_index: usize = 0; + + while (source_index < source.len) { + // Comments and layout are purely for reviewers and contribute no + // bytes to the embedded reference. + skipIgnored(source, &source_index); + if (source_index == source.len) break; + + // Every data token is one complete byte. Parse it directly rather + // than accepting variable-width integers or other hex syntax. + if (source_index + 1 >= source.len) { + @compileError("snapshot fixture ends with one hex digit"); + } + result[result_index] = + hexNibble(source[source_index]) << 4 | + hexNibble(source[source_index + 1]); + result_index += 1; + source_index += 2; + + // Requiring a separator catches accidental third digits and makes + // the accepted grammar match the formatter below. + if (source_index < source.len and + !std.ascii.isWhitespace(source[source_index]) and + source[source_index] != '#') + { + @compileError( + "snapshot fixture hex bytes must be separated by whitespace", + ); + } + } + + return result; + } +} + +/// Compare generated bytes with a checked-in reference. +/// +/// A formatted candidate is always created in a temporary directory. On a +/// mismatch it is copied into the repository root before the test fails. +pub fn expectEqual( + kind: Kind, + reference_path: []const u8, + candidate_name: []const u8, + expected: []const u8, + actual: []const u8, +) !void { + const testing = std.testing; + const alloc = testing.allocator; + + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + + // Kaitai parser selection lives in the reference itself. Read the textual + // source so generated candidates preserve that metadata even though the + // byte comparison below uses the comptime-decoded reference. + const reference_source = try std.Io.Dir.cwd().readFileAlloc( + testing.io, + reference_path, + alloc, + .unlimited, + ); + defer alloc.free(reference_source); + + // Always materialize a candidate, even when it will match. This keeps the + // generation path exercised on every test run while avoiding repository + // artifacts for the normal successful case. + { + var candidate_file = try tmp.dir.createFile( + testing.io, + candidate_name, + .{}, + ); + defer candidate_file.close(testing.io); + var write_buffer: [4096]u8 = undefined; + var candidate_writer = candidate_file.writer( + testing.io, + &write_buffer, + ); + try format( + kind, + reference_source, + actual, + &candidate_writer.interface, + ); + try candidate_writer.interface.flush(); + } + + // Matching candidates remain in the temporary directory and disappear + // with its cleanup. + if (std.mem.eql(u8, expected, actual)) return; + + // The test temp directory is not necessarily below the working directory. + // Resolve both paths before copying the candidate to the stable, + // user-visible repository-root name. + const tmp_path = try tmp.dir.realPathFileAlloc( + testing.io, + candidate_name, + alloc, + ); + defer alloc.free(tmp_path); + const cwd = try std.Io.Dir.cwd().realPathFileAlloc( + testing.io, + ".", + alloc, + ); + defer alloc.free(cwd); + const candidate_path = try std.fs.path.join( + alloc, + &.{ cwd, candidate_name }, + ); + defer alloc.free(candidate_path); + try std.Io.Dir.copyFileAbsolute( + tmp_path, + candidate_path, + testing.io, + .{}, + ); + + // Report the first useful comparison point. When one byte sequence is a + // prefix of the other, this is the shared length where they diverge. + const difference = difference: { + const shared_len = @min(expected.len, actual.len); + for ( + expected[0..shared_len], + actual[0..shared_len], + 0.., + ) |expected_byte, actual_byte, index| { + if (expected_byte != actual_byte) break :difference index; + } + break :difference shared_len; + }; + log.err( + "snapshot fixture differs reference={s} candidate={s} " ++ + "offset=0x{x} expected_len={} actual_len={}", + .{ + reference_path, + candidate_path, + difference, + expected.len, + actual.len, + }, + ); + return error.TestExpectedEqual; +} + +fn decodedLen(comptime source: []const u8) usize { + comptime { + // This is a complete validation pass, not just a count. Invalid syntax + // therefore fails before parse allocates and fills its result array. + @setEvalBranchQuota(100_000_000); + + var index: usize = 0; + var result: usize = 0; + while (index < source.len) { + skipIgnored(source, &index); + if (index == source.len) break; + + if (index + 1 >= source.len) { + @compileError("snapshot fixture ends with one hex digit"); + } + _ = hexNibble(source[index]); + _ = hexNibble(source[index + 1]); + result += 1; + index += 2; + + if (index < source.len and + !std.ascii.isWhitespace(source[index]) and + source[index] != '#') + { + @compileError( + "snapshot fixture hex bytes must be separated by whitespace", + ); + } + } + return result; + } +} + +fn skipIgnored(comptime source: []const u8, index: *usize) void { + while (index.* < source.len) { + // Layout whitespace is unrestricted so fixtures can group fields and + // keep deterministic sixteen-byte output lines. + if (std.ascii.isWhitespace(source[index.*])) { + index.* += 1; + continue; + } + + // A comment consumes the rest of its line. The newline itself is + // consumed by the whitespace case on the next iteration. + if (source[index.*] != '#') return; + while (index.* < source.len and source[index.*] != '\n') { + index.* += 1; + } + } +} + +fn hexNibble(comptime value: u8) u8 { + return switch (value) { + '0'...'9' => value - '0', + 'a'...'f' => value - 'a' + 10, + 'A'...'F' => value - 'A' + 10, + else => @compileError("snapshot fixture contains a non-hex digit"), + }; +} + +fn format( + kind: Kind, + reference_source: []const u8, + bytes: []const u8, + writer: *std.Io.Writer, +) (std.Io.Writer.Error || error{InvalidKaitaiMetadata})!void { + // Every generated candidate starts with enough maintenance context to be + // understandable when copied out of a failing test's temp directory. + try writer.writeAll("# Ghostty snapshot fixture\n"); + try writeKaitaiMetadata(reference_source, writer); + try writer.print( + "# Wire version: {}\n" ++ + "# Generated by its snapshot test; review before replacing.\n" ++ + "# On mismatch, the candidate is copied to the repository root.\n", + .{envelope.version}, + ); + + // Small values only need a hex dump. Larger compound formats get + // structure-aware section labels without changing their decoded bytes. + switch (kind) { + .bytes => try writeSection( + "encoded bytes", + bytes, + 0, + bytes.len, + writer, + ), + .page => try formatPage(bytes, writer), + .snapshot => try formatSnapshot(bytes, writer), + } +} + +fn writeKaitaiMetadata( + reference_source: []const u8, + writer: *std.Io.Writer, +) (std.Io.Writer.Error || error{InvalidKaitaiMetadata})!void { + const prefixes = [_][]const u8{ + "# Kaitai type:", + "# Kaitai params:", + "# Kaitai offset:", + }; + var found = [_]bool{false} ** prefixes.len; + + var lines = std.mem.splitScalar(u8, reference_source, '\n'); + while (lines.next()) |untrimmed| { + const line = std.mem.trim(u8, untrimmed, " \r"); + for (prefixes, &found) |prefix, *seen| { + if (!std.mem.startsWith(u8, line, prefix)) continue; + if (seen.*) return error.InvalidKaitaiMetadata; + seen.* = true; + try writer.print("{s}\n", .{line}); + break; + } + } + + for (found) |seen| { + if (!seen) return error.InvalidKaitaiMetadata; + } +} + +fn formatPage( + bytes: []const u8, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + // PAGE version 1 has a twenty-byte fixed header. The remainder is + // self-describing through the counts and dimensions in that header. + const header_end = @min(bytes.len, 20); + try writeSection("PAGE header", bytes, 0, header_end, writer); + if (header_end < bytes.len) { + try writeSection( + "style and hyperlink tables, rows, and cells", + bytes, + header_end, + bytes.len, + writer, + ); + } +} + +fn formatSnapshot( + bytes: []const u8, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + // A complete snapshot begins with its unframed envelope. Clamp this first + // section so a partially generated candidate can still be inspected. + const envelope_end = @min(bytes.len, envelope.encoded_len); + try writeSection("snapshot envelope", bytes, 0, envelope_end, writer); + + // Walk record framing only far enough to annotate boundaries. The emitted + // hex remains authoritative; this formatter never repairs candidate data. + var offset: usize = envelope_end; + while (offset < bytes.len) { + // A partial header cannot provide a safe payload boundary, so preserve + // the remaining bytes as one explicitly incomplete section. + if (bytes.len - offset < record.Header.len) { + try writeSection( + "trailing partial record", + bytes, + offset, + bytes.len, + writer, + ); + return; + } + + // Tag and payload length are the first six bytes of every record + // header. CRC validation belongs to the codec test, not this formatter. + const tag_raw = std.mem.readInt( + u16, + bytes[offset..][0..2], + .little, + ); + const payload_len: u32 = std.mem.readInt( + u32, + bytes[offset + 2 ..][0..4], + .little, + ); + + // Clamp the section to the available candidate bytes. This makes a + // truncated payload reviewable instead of indexing beyond the slice. + const record_end: usize = @min( + bytes.len, + offset + record.Header.len + @as(usize, payload_len), + ); + + // Known tags make the annotated diff readable. Preserve unknown tags + // numerically so formatter output is still useful for encoder bugs. + if (std.enums.fromInt(record.Tag, tag_raw)) |tag| { + try writer.print( + "\n# offset 0x{x:0>8}: {s} record, payload {} bytes\n", + .{ offset, @tagName(tag), payload_len }, + ); + } else { + try writer.print( + "\n# offset 0x{x:0>8}: unknown record {}, payload {} bytes\n", + .{ offset, tag_raw, payload_len }, + ); + } + try writeHex(bytes, offset, record_end, writer); + offset = record_end; + } +} + +fn writeSection( + name: []const u8, + bytes: []const u8, + start: usize, + end: usize, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + // Sections begin on a fresh line and carry their absolute wire offset. + try writer.print( + "\n# offset 0x{x:0>8}: {s}\n", + .{ start, name }, + ); + try writeHex(bytes, start, end, writer); +} + +fn writeHex( + bytes: []const u8, + start: usize, + end: usize, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + const digits = "0123456789abcdef"; + var offset = start; + while (offset < end) { + // Lowercase, sixteen-byte lines provide stable textual diffs. The + // trailing offset is a comment and is ignored by parse. + const line_end = @min(end, offset + 16); + for (bytes[offset..line_end], 0..) |byte, index| { + if (index != 0) try writer.writeByte(' '); + try writer.writeByte(digits[byte >> 4]); + try writer.writeByte(digits[byte & 0x0f]); + } + try writer.print(" # 0x{x:0>8}\n", .{offset}); + offset = line_end; + } +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/grid.zig b/vendor/libghostty-vt/src/terminal/snapshot/grid.zig new file mode 100644 index 00000000..3a890913 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/grid.zig @@ -0,0 +1,2403 @@ +//! Grid (rows, cells, and grapheme suffixes) encoding. +//! +//! A grid contains the rows and cells of a terminal page. Its dimensions are +//! supplied by the containing record rather than repeated here. The encoder +//! writes exactly `rows` row records followed by one grapheme suffix section. +//! +//! All records are tightly packed with no padding between them. All integers +//! are unsigned and little-endian. +//! +//! The layout is designed so the common case decodes with bulk copies: cells +//! are fixed-size words, trailing default cells are elided per row, and the +//! variable-length grapheme suffixes live outside the row data. +//! +//! ```text +//! +---------------------------+ +//! | Row 0 | +//! +---------------------------+ +//! | ... | +//! +---------------------------+ +//! | Row (rows - 1) | +//! +---------------------------+ +//! | Grapheme suffix section | +//! +---------------------------+ +//! ``` +//! +//! ## Row +//! +//! Each row has the following format: +//! +//! | Offset | Size | Field | +//! | -----: | ----------------: | :------------------------- | +//! | 0 | 1 | Row flags | +//! | 1 | 2 | Encoded cell count (`u16`) | +//! | 3 | `width` * `count` | Encoded cells | +//! +//! The row flag byte has the following format: +//! +//! | Bits | Field | +//! | ---: | :------------------------ | +//! | 0 | Wrap | +//! | 1 | Wrap continuation | +//! | 2-3 | Semantic prompt | +//! | 4-5 | Encoded cell width | +//! | 6-7 | Reserved, zero | +//! +//! Semantic prompt values are: +//! +//! | Value | Meaning | +//! | ----: | :------------------ | +//! | 0 | None | +//! | 1 | Prompt | +//! | 2 | Prompt continuation | +//! +//! Value 3 is not emitted in snapshot version 1. Decoders treat it as none. +//! +//! The encoded cell count must not exceed the grid's column count; it is a +//! structural field and decoders reject larger values. Cells at and beyond +//! the count are the default cell: all sixty-four bits zero, meaning an +//! empty narrow codepoint cell with the default style and no hyperlink. +//! Canonical encoders emit exactly through the row's last non-default cell, +//! so a fully default row has a zero count and no cell words. +//! +//! ## Encoded cell width +//! +//! The two width bits select how many bytes encode each of the row's +//! cells: `1 << width` is the size, so zero through three select one, two, +//! four, or eight bytes. Narrower widths are truncated transports of the +//! same cell word: a cell qualifies for a width when all of its higher +//! word bits are zero. +//! +//! | Width | Bytes | Encoded value and admitted cells | +//! | ----: | ----: | :--------------------------------------------------- | +//! | 0 | 1 | Codepoint at or below U+00FF; all other bits zero | +//! | 1 | 2 | Codepoint at or below U+FFFF; all other bits zero | +//! | 2 | 4 | Word bits 0-31: any content kind and codepoint, style | +//! | | | IDs 1-63, narrow, no flags, no hyperlink | +//! | 3 | 8 | The complete word | +//! +//! Widths zero and one store the codepoint value itself, which is word +//! bits 2-25 shifted down; the reconstructed word is the codepoint shifted +//! left by two. Width two stores the word's low half unshifted. This makes +//! every width a zero-extension on decode and a truncation on encode. +//! +//! Canonical encoders choose each row's smallest admissible width, which +//! follows directly from the bitwise OR of the row's cell words. Decoders +//! use the declared width for framing and accept rows encoded wider than +//! necessary. The width of a row with a zero cell count is canonically +//! zero and carries no meaning. +//! +//! Native row cache flags are not encoded. In particular, the Kitty virtual +//! placeholder hint is derived while decoding cells containing U+10EEEE. +//! +//! ## Cell +//! +//! Each cell is one 64-bit little-endian word, transported at the row's +//! encoded cell width as described above: +//! +//! ```text +//! bit 0 +-------------------------------+ +//! | Content kind | +//! | 2 bits | +//! bit 2 +-------------------------------+ +//! | Content | +//! | 24 bits | +//! bit 26 +-------------------------------+ +//! | Style ID | +//! | 16 bits | +//! bit 42 +-------------------------------+ +//! | Width kind | +//! | 2 bits | +//! bit 44 +-------------------------------+ +//! | Protected | +//! bit 45 +-------------------------------+ +//! | Hyperlink flag | +//! bit 46 +-------------------------------+ +//! | Semantic content | +//! | 2 bits | +//! bit 48 +-------------------------------+ +//! | Hyperlink ID | +//! | 16 bits | +//! bit 64 +-------------------------------+ +//! ``` +//! +//! Content kinds are: +//! +//! | Value | Meaning | +//! | ----: | :------------------------------- | +//! | 0 | Codepoint | +//! | 1 | Codepoint with grapheme suffix | +//! | 2 | Palette background | +//! | 3 | RGB background | +//! +//! The content kind selects the layout of the 24-bit content field. All +//! content bit positions below are relative to the start of the content +//! field, so content bit 0 is cell word bit 2. +//! +//! For codepoint content (kinds 0 and 1), the content field is a Unicode +//! scalar value. Values above U+10FFFF and surrogates decode as U+FFFD. +//! Kind 1 additionally declares that the grapheme suffix section contains +//! one entry for this cell; the cell is otherwise identical to kind 0. +//! +//! ```text +//! bit 0 +-------------------------------+ +//! | Unicode scalar value | +//! | 24 bits | +//! bit 24 +-------------------------------+ +//! ``` +//! +//! For a palette background (kind 2), the low byte is the palette index. +//! The remaining content bits are reserved, canonically zero, and ignored +//! by decoders. +//! +//! ```text +//! bit 0 +-------------------------------+ +//! | Palette index | +//! | 8 bits | +//! bit 8 +-------------------------------+ +//! | Reserved, zero | +//! | 16 bits | +//! bit 24 +-------------------------------+ +//! ``` +//! +//! For an RGB background (kind 3), the content field is one byte per +//! channel: +//! +//! ```text +//! bit 0 +-------------------------------+ +//! | Red | +//! | 8 bits | +//! bit 8 +-------------------------------+ +//! | Green | +//! | 8 bits | +//! bit 16 +-------------------------------+ +//! | Blue | +//! | 8 bits | +//! bit 24 +-------------------------------+ +//! ``` +//! +//! Width kinds are: +//! +//! | Value | Meaning | +//! | ----: | :---------- | +//! | 0 | Narrow | +//! | 1 | Wide | +//! | 2 | Spacer tail | +//! | 3 | Spacer head | +//! +//! Semantic content values are: +//! +//! | Value | Meaning | +//! | ----: | :------ | +//! | 0 | Output | +//! | 1 | Input | +//! | 2 | Prompt | +//! +//! Value 3 is not emitted in snapshot version 1. Decoders treat it as output. +//! +//! Style and hyperlink ID zero mean no style and no hyperlink. Other IDs +//! refer to entries in the containing record's separate style and hyperlink +//! tables. The hyperlink flag is set exactly when the hyperlink ID is +//! nonzero; decoders derive cell linkage from the remapped ID and ignore the +//! flag itself. +//! +//! ## Grapheme suffix section +//! +//! The section begins with an entry count followed by that many entries: +//! +//! | Offset | Size | Field | +//! | -----: | ------: | :------------------- | +//! | 0 | 4 | Entry count (`u32`) | +//! | 4 | varies | Entries | +//! +//! Each entry: +//! +//! | Offset | Size | Field | +//! | -----: | ----------: | :-------------------------- | +//! | 0 | 2 | Row (`u16`) | +//! | 2 | 2 | Column (`u16`) | +//! | 4 | 2 | Codepoint count (`u16`) | +//! | 6 | 4 * `count` | Codepoints (`u32` each) | +//! +//! Each codepoint is a Unicode scalar; invalid scalars are individually +//! ignored by decoders. Canonical encoders emit exactly one entry, with at +//! least one codepoint, for every kind 1 cell, in ascending row-then-column +//! order. Decoders consume every declared entry and ignore entries whose +//! target is out of range, is not a codepoint cell, has a zero codepoint, or +//! already received an entry. A kind 1 cell that receives no suffix +//! codepoints decodes as a plain codepoint cell. + +const std = @import("std"); +const assert = std.debug.assert; +const builtin = @import("builtin"); +const Allocator = std.mem.Allocator; +const test_fixture = @import("fixture.zig"); +const io = @import("io.zig"); +const terminal_hyperlink = @import("../hyperlink.zig"); +const terminal_page = @import("../page.zig"); +const terminal_style = @import("../style.zig"); + +/// The Kitty virtual-placement placeholder remains wire-relevant even when +/// runtime Kitty graphics support is compiled out. +const kitty_virtual_placeholder: u21 = 0x10EEEE; + +const TerminalCell = terminal_page.Cell; +const TerminalHyperlinkId = terminal_hyperlink.Id; +const TerminalPage = terminal_page.Page; +const TerminalRow = terminal_page.Row; +const TerminalStyleId = terminal_style.Id; + +/// The header before every row's encoded cells. +/// +/// The semantic prompt is a raw integer for the same reason as the wire +/// cell fields: decoders must accept its reserved value without +/// instantiating an invalid native enum. The width enum is exhaustive over +/// its two bits, so every header byte bit-casts to a valid value. +pub const Row = packed struct(u8) { + wrap: bool = false, + wrap_continuation: bool = false, + semantic_prompt: u2 = 0, + cell_width: Cell.EncodedWidth = .one, + _padding: u2 = 0, +}; + +/// The wire layout of one encoded cell. This is its own registry: the bit +/// positions and field meanings are part of the snapshot format and are +/// documented above independently of the native cell. +/// +/// Enum-like fields are raw integers because decoders must accept reserved +/// values (for example semantic content 3) without instantiating an invalid +/// native enum. +pub const Cell = packed struct(u64) { + kind: u2 = @intFromEnum(Kind.codepoint), + content: u24 = 0, + style_id: u16 = 0, + width: u2 = 0, + protected: bool = false, + hyperlink: bool = false, + semantic_content: u2 = 0, + hyperlink_id: u16 = 0, + + /// Determines how `content` is interpreted. + pub const Kind = enum(u2) { + codepoint = 0, + codepoint_grapheme = 1, + bg_color_palette = 2, + bg_color_rgb = 3, + }; + + /// The encoded cell width declared by a row header: how many bytes + /// transport each of the row's cell words. See the format + /// documentation above for the value each width transports and the + /// cells it admits. + /// + /// `truncate` and `extend` are the transport transform itself, so the + /// admission masks derive from them rather than being maintained by + /// hand: a cell word is admitted exactly when it round-trips. + pub const EncodedWidth = enum(u2) { + one = 0, + two = 1, + four = 2, + eight = 3, + + /// The number of bytes transporting one cell word. + pub fn size(self: EncodedWidth) usize { + return @as(usize, 1) << @intFromEnum(self); + } + + /// The integer type transporting one cell word. + pub fn Int(comptime self: EncodedWidth) type { + return switch (self) { + .one => u8, + .two => u16, + .four => u32, + .eight => u64, + }; + } + + /// Truncate one cell word to its transported value. Only words + /// admitted by this width round-trip; `select` proves that for + /// every cell in a row before an encoder may use it. + pub fn truncate(comptime self: EncodedWidth, word: u64) self.Int() { + return @truncate(switch (self) { + // The bare codepoint, shifted down from the content field. + .one, .two => @as(Cell, @bitCast(word)).content, + + // The word itself. + .four, .eight => word, + }); + } + + /// Widen one transported value back to its cell word. + pub fn extend(comptime self: EncodedWidth, value: self.Int()) u64 { + return switch (self) { + .one, .two => @bitCast(Cell{ .content = value }), + .four, .eight => value, + }; + } + + /// The word bits a cell may use and still round-trip through this + /// width. + pub fn mask(comptime self: EncodedWidth) u64 { + return comptime self.extend(std.math.maxInt(self.Int())); + } + + /// The smallest width admitting the word, typically the bitwise + /// OR of every cell word in a row. + pub fn select(word: u64) EncodedWidth { + inline for ([_]EncodedWidth{ .one, .two, .four }) |width| { + if (word & ~width.mask() == 0) return width; + } + return .eight; + } + }; +}; + +/// Whether the native cell's in-memory layout matches the wire cell layout +/// bit for bit, with the wire hyperlink ID occupying the native padding. +/// +/// The wire format does not require this: it is an optimization. When it +/// holds, whole rows encode and decode as bulk copies. If the native layout +/// ever diverges, the portable field-by-field codec below remains correct +/// and this constant simply becomes false. +const native_matches_wire = native: { + if (@bitSizeOf(TerminalCell) != 64) break :native false; + + // The bulk copies above are only sound if we can prove the two layouts + // agree, and we want native cell changes to demote us to the portable + // codec automatically rather than corrupt snapshots. + // + // Instead of pinning every native field offset and enum value w/ assertions + // that must be maintained by hand, each probe below pairs a native + // cell with the wire cell that must share its exact bit pattern; both + // sides bit cast to a word and any difference means some field moved + // or some enum member was renumbered. + // + // Three probes, one per content payload, cover the whole cell. + const probes = [_]struct { + native: TerminalCell, + wire: Cell, + }{ + .{ + .native = .{ + .content_tag = .codepoint_grapheme, + .content = .{ .codepoint = .{ .data = 0x10FFFF } }, + .style_id = 0xBEEF, + .wide = .spacer_tail, + .protected = true, + ._padding = 0x1D2C, + }, + .wire = .{ + .kind = 1, + .content = 0x10FFFF, + .style_id = 0xBEEF, + .width = 2, + .protected = true, + // The native padding position, canonical wire or not. + .hyperlink_id = 0x1D2C, + }, + }, + .{ + .native = .{ + .content_tag = .bg_color_palette, + .content = .{ .color_palette = .{ .data = 0xAB } }, + .wide = .spacer_head, + .hyperlink = true, + .semantic_content = .input, + }, + .wire = .{ + .kind = 2, + .content = 0xAB, + .width = 3, + .hyperlink = true, + .semantic_content = 1, + }, + }, + .{ + .native = .{ + .content_tag = .bg_color_rgb, + .content = .{ .color_rgb = .{ + .r = 0x12, + .g = 0x34, + .b = 0x56, + } }, + .wide = .wide, + .semantic_content = .prompt, + }, + .wire = .{ + .kind = 3, + .content = 0x563412, + .width = 1, + .semantic_content = 2, + }, + }, + }; + for (probes) |probe| { + const native_bits: u64 = @bitCast(probe.native); + const wire_bits: u64 = @bitCast(probe.wire); + if (native_bits != wire_bits) break :native false; + } + + break :native true; +}; + +/// Whether rows of cells can be copied between native and wire storage +/// without per-cell transformation. +const bulk_codec = native_matches_wire and + builtin.cpu.arch.endian() == .little; + +pub const EncodeError = std.Io.Writer.Error || error{ + /// Wide and spacer cells do not form a valid row. + InvalidWideCell, + + /// One cell's grapheme suffix exceeds the entry's u16 codepoint count. + TooManyGraphemes, +}; + +/// Encode every row, cell, and grapheme suffix directly from a page. +/// +/// If an error is returned, partial data may have been written. If you +/// want transactional writing, the caller is responsible for using something +/// like a seekable stream and rolling back. +pub fn encode( + page: *const TerminalPage, + writer: *std.Io.Writer, +) EncodeError!void { + defer page.assertIntegrity(); + for (0..page.size.rows) |y| { + const row = page.getRow(y); + const cells = page.getCells(row); + + // Trailing default cells decode implicitly: wide/spacer pairs and + // hyperlinked or styled cells are always nonzero, so eliding the + // zero suffix never drops encoded state. The scan also accumulates + // the OR of the row's cell words to select its encoded cell width. + const count: usize, const word_or: u64 = scanRow(cells); + + // Validate the wide state of every encoded cell so we don't encode + // corrupt data. The width bits of the OR word witness whether any + // encoded cell is non-narrow at all; rows without them, the common + // case, satisfy every pair rule vacuously. Trailing default cells + // are narrow, so checking the encoded prefix against the full row + // width covers every pair. + const wide_mask: u64 = comptime @bitCast(Cell{ .width = 3 }); + if (word_or & wide_mask != 0) { + for (cells[0..count], 0..) |*cell, x| switch (cell.wide) { + .narrow => {}, + .wide => if (x + 1 == cells.len or + cells[x + 1].wide != .spacer_tail) + { + return error.InvalidWideCell; + }, + .spacer_tail => if (x == 0 or + cells[x - 1].wide != .wide) + { + return error.InvalidWideCell; + }, + .spacer_head => if (x + 1 != cells.len or !row.wrap) { + return error.InvalidWideCell; + }, + }; + } + + // Canonical rows use the smallest admissible width. + const cell_width: Cell.EncodedWidth = .select(word_or); + + const row_header: Row = .{ + .wrap = row.wrap, + .wrap_continuation = row.wrap_continuation, + .semantic_prompt = @intFromEnum(row.semantic_prompt), + .cell_width = cell_width, + }; + + // The bulk codec emits the row header and its encoded cells directly + // into the destination's spare buffer capacity, so a row costs no + // writer call at all instead of one for the header and one per cell + // chunk. + if (comptime bulk_codec) emit: { + const words: [*]const u64 = @ptrCast(cells.ptr); + switch (cell_width) { + inline .one, .two, .four => |width| { + const size = comptime width.size(); + const needed = 3 + count * size; + if (writer.unusedCapacityLen() < needed) break :emit; + const out = writer.unusedCapacitySlice()[0..needed]; + out[0] = @bitCast(row_header); + std.mem.writeInt(u16, out[1..3], @intCast(count), .little); + encodeNarrowInto(width, words, count, out[3..]); + writer.advance(needed); + continue; + }, + .eight => { + // Hyperlink IDs live in a native side table, but we + // embed them in ours, so if we have any hyperlinks we + // need to fall back to the loop below. + const witness: Cell = @bitCast(word_or); + if (!witness.hyperlink and witness.hyperlink_id == 0) { + const needed = 3 + count * 8; + if (writer.unusedCapacityLen() < needed) break :emit; + const out = writer.unusedCapacitySlice()[0..needed]; + out[0] = @bitCast(row_header); + std.mem.writeInt( + u16, + out[1..3], + @intCast(count), + .little, + ); + @memcpy( + out[3..], + std.mem.sliceAsBytes(cells[0..count]), + ); + writer.advance(needed); + continue; + } + }, + } + } + + // Row header: flags then the encoded cell count. + { + var header_bytes: [3]u8 = undefined; + header_bytes[0] = @bitCast(row_header); + std.mem.writeInt(u16, header_bytes[1..3], @intCast(count), .little); + try writer.writeAll(&header_bytes); + } + + switch (cell_width) { + inline .one, .two, .four => |width| try encodeNarrowCells( + width, + cells[0..count], + writer, + ), + + .eight => { + // Hyperlink IDs live in a native side table, but we embed + // them in ours, so if we have any hyperlinks we need + // to fallback to the loop below. + if (comptime bulk_codec) { + const witness: Cell = @bitCast(word_or); + if (!witness.hyperlink and witness.hyperlink_id == 0) { + try writer.writeAll(std.mem.sliceAsBytes(cells[0..count])); + continue; + } + } + + for (cells[0..count]) |*cell| { + const link_id: TerminalHyperlinkId = if (cell.hyperlink) + page.lookupHyperlink(cell) orelse unreachable + else + 0; + try io.writeInt( + writer, + u64, + cellBits(cell.*, link_id), + ); + } + }, + } + } + + try encodeGraphemes(page, writer); +} + +/// The encoded cell count (through the last nonzero cell) and the bitwise +/// OR of every encoded cell word for one row. +fn scanRow(cells: []const TerminalCell) struct { usize, u64 } { + if (comptime bulk_codec) { + const words: [*]const u64 = @ptrCast(cells.ptr); + const V = @Vector(4, u64); + const VPtr = *align(@alignOf(u64)) const V; + + // Count the zero cells using vectorized instructions + var count = cells.len; + while (count >= 4) { + const tail = @as(VPtr, @ptrCast(words + count - 4)).*; + if (@reduce(.Or, tail) != 0) break; + count -= 4; + } + while (count > 0 and words[count - 1] == 0) count -= 1; + + // Accumulate the OR of the classifaction vectorized, with the + // scalar tail continuing from where the vector loop stopped. + const word_or: u64 = word_or: { + var acc: V = @splat(0); + var i: usize = 0; + while (i + 4 <= count) : (i += 4) { + acc |= @as(VPtr, @ptrCast(words + i)).*; + } + var word_or: u64 = @reduce(.Or, acc); + while (i < count) : (i += 1) word_or |= words[i]; + break :word_or word_or; + }; + + return .{ count, word_or }; + } + + // Scalar path, count backwards + var count: usize = cells.len; + while (count > 0 and cells[count - 1].isZero()) count -= 1; + var word_or: u64 = 0; + for (cells[0..count]) |*cell| word_or |= classifyWord(cell); + return .{ count, word_or }; +} + +/// The word used to select a row's encoded cell width. This is the cell's +/// wire word with the hyperlink flag reflecting the native cell, so linked +/// cells and nonzero native padding disqualify every narrow width. +inline fn classifyWord(cell: *const TerminalCell) u64 { + if (comptime native_matches_wire) return @bitCast(cell.*); + + var wire: Cell = @bitCast(cellBits(cell.*, 0)); + wire.hyperlink = cell.hyperlink; + return @bitCast(wire); +} + +/// Write one row's cells truncated to the given encoded width. +/// +/// Every admitted cell round-trips exactly because the row's width +/// selection proved every cell word survives `truncate` then `extend`. +fn encodeNarrowCells( + comptime width: Cell.EncodedWidth, + cells: []const TerminalCell, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + const size = comptime width.size(); + var chunk: [1024]u8 = undefined; + var i: usize = 0; + while (i < cells.len) { + const n = @min(cells.len - i, chunk.len / size); + for (cells[i..][0..n], 0..) |*cell, j| { + std.mem.writeInt( + width.Int(), + chunk[j * size ..][0..size], + width.truncate(classifyWord(cell)), + .little, + ); + } + try writer.writeAll(chunk[0 .. n * size]); + i += n; + } +} + +/// Truncate one row's cell words into `out` at the given encoded width. +fn encodeNarrowInto( + comptime width: Cell.EncodedWidth, + words: [*]const u64, + count: usize, + out: []u8, +) void { + comptime assert(bulk_codec); + const size = comptime width.size(); + const V = @Vector(2, u64); + const shift: V = @splat(comptime switch (width) { + .one, .two => @bitOffsetOf(Cell, "content"), + .four => 0, + .eight => unreachable, + }); + const mask: @Vector(size * 4, i32) = comptime mask: { + var mask: [size * 4]i32 = undefined; + for (0..2) |lane| { + for (0..size) |byte| { + mask[lane * size + byte] = @intCast(lane * 8 + byte); + mask[(lane + 2) * size + byte] = + ~@as(i32, @intCast(lane * 8 + byte)); + } + } + break :mask mask; + }; + + var j: usize = 0; + while (j + 4 <= count) : (j += 4) encodeNarrowStep( + width, + words, + j, + out, + shift, + mask, + ); + if (j < count) { + if (count >= 4) { + encodeNarrowStep(width, words, count - 4, out, shift, mask); + } else { + while (j < count) : (j += 1) { + std.mem.writeInt( + width.Int(), + out[j * size ..][0..size], + width.truncate(words[j]), + .little, + ); + } + } + } +} + +/// Emit four truncated cell words starting at cell index `j`. +inline fn encodeNarrowStep( + comptime width: Cell.EncodedWidth, + words: [*]const u64, + j: usize, + out: []u8, + shift: @Vector(2, u64), + mask: @Vector(width.size() * 4, i32), +) void { + const size = comptime width.size(); + const VPtr = *align(@alignOf(u64)) const @Vector(2, u64); + const lo: @Vector(16, u8) = @bitCast(@as(VPtr, @ptrCast(words + j)).* >> shift); + const hi: @Vector(16, u8) = @bitCast(@as(VPtr, @ptrCast(words + j + 2)).* >> shift); + @as( + *align(1) @Vector(size * 4, u8), + @ptrCast(out[j * size ..].ptr), + ).* = @shuffle(u8, lo, hi, mask); +} + +/// Encode the grapheme suffix section for every kind 1 cell in the grid. +fn encodeGraphemes( + page: *const TerminalPage, + writer: *std.Io.Writer, +) EncodeError!void { + // Every grapheme cell owns exactly one entry in the page's grapheme + // map, so the section header comes straight from the page without a + // counting pass over the grid. Rows without the native grapheme hint + // contain no grapheme cells in any intact page. + const entries = page.graphemeCount(); + try io.writeInt(writer, u32, @intCast(entries)); + if (entries == 0) return; + + // Entries are batched into a local buffer so hot pages perform one + // writer call per flush instead of several per entry. + var buffer: [4096]u8 = undefined; + var used: usize = 0; + var emitted: usize = 0; + for (0..page.size.rows) |y| { + const row = page.getRow(y); + if (!row.grapheme) continue; + for (page.getCells(row), 0..) |*cell, x| { + if (!cell.hasGrapheme()) continue; + const cps = page.lookupGrapheme(cell) orelse unreachable; + if (cps.len > std.math.maxInt(u16)) return error.TooManyGraphemes; + emitted += 1; + + const needed = 6 + cps.len * 4; + if (buffer.len - used < needed) { + try writer.writeAll(buffer[0..used]); + used = 0; + } + if (needed > buffer.len) { + // An entry larger than the whole buffer streams directly. + try io.writeInt(writer, u16, @intCast(y)); + try io.writeInt(writer, u16, @intCast(x)); + try io.writeInt(writer, u16, @intCast(cps.len)); + for (cps) |cp| try io.writeInt(writer, u32, cp); + continue; + } + + std.mem.writeInt(u16, buffer[used..][0..2], @intCast(y), .little); + std.mem.writeInt(u16, buffer[used + 2 ..][0..2], @intCast(x), .little); + std.mem.writeInt(u16, buffer[used + 4 ..][0..2], @intCast(cps.len), .little); + used += 6; + for (cps) |cp| { + std.mem.writeInt(u32, buffer[used..][0..4], cp, .little); + used += 4; + } + } + } + try writer.writeAll(buffer[0..used]); + + // The declared count is trusted by decoders for framing, so the grid + // must have produced exactly that many entries. + assert(emitted == entries); +} + +pub const DecodeError = std.Io.Reader.Error || error{ + /// A row declares more encoded cells than the grid has columns. + InvalidRowCellCount, +}; + +/// Maps encoded style table IDs to page-assigned style IDs. +pub const StyleRemap = Remap(TerminalStyleId); + +/// Maps encoded hyperlink table IDs to page-assigned hyperlink IDs. +pub const HyperlinkRemap = Remap(TerminalHyperlinkId); + +/// Decode every row, cell, and grapheme suffix directly into an +/// initialized, empty page. +/// +/// The grid does not encode dimensions, so `page` must already have the +/// exact row and column count expected by the containing record. Capacity +/// hints are advisory. Graphemes and cell hyperlink references that do not +/// fit are discarded without affecting the rest of the grid. +/// +/// Style and hyperlink table entries must be inserted into `page` before +/// calling this function, with their encoded and page-assigned IDs recorded +/// in `style_remap` and `hyperlink_remap`. ID zero always means the default +/// style or no hyperlink. A nonzero ID missing from its remap is also +/// treated as zero so unknown table references do not prevent the rest of +/// the grid from decoding. +/// +/// Invalid semantic data is normalized into a degraded form while +/// preserving the declared byte boundaries. Unknown semantic values use +/// their neutral variants, invalid Unicode becomes U+FFFD, invalid optional +/// data is ignored, and malformed wide-cell relationships become narrow +/// cells. The encoded cell count is the only structural field. +pub fn decode( + page: *TerminalPage, + reader: *std.Io.Reader, + style_remap: *const StyleRemap, + hyperlink_remap: *const HyperlinkRemap, +) DecodeError!void { + for (0..page.size.rows) |y| { + // Read the row header and cell count. + const row_header: Row, const count: u16 = header: { + // The staged payload path has every header buffered. + var row_header_bytes: [3]u8 = undefined; + if (reader.bufferedLen() >= 3) { + row_header_bytes = reader.buffered()[0..3].*; + reader.toss(3); + } else { + try reader.readSliceAll(&row_header_bytes); + } + + // Every bit pattern is a valid header: booleans and the exhaustive + // width enum decode directly, and the raw semantic value gets a + // default below. Reserved bits do not change the known fields. + const row_header: Row = @bitCast(row_header_bytes[0]); + const count = std.mem.readInt(u16, row_header_bytes[1..3], .little); + break :header .{ row_header, count }; + }; + + // A fully default row needs no work at all: decoded pages start + // zeroed, which is exactly the default row and cell state. + if (@as(u8, @bitCast(row_header)) == 0 and count == 0) continue; + + // Update the row fields through one load and store instead of a + // read-modify-write per packed field. + const row = page.getRow(y); + var row_value = row.*; + row_value.wrap = row_header.wrap; + row_value.wrap_continuation = row_header.wrap_continuation; + row_value.semantic_prompt = std.enums.fromInt( + TerminalRow.SemanticPrompt, + row_header.semantic_prompt, + ) orelse .none; + row.* = row_value; + + const cells = page.getCells(row); + if (count > cells.len) return error.InvalidRowCellCount; + if (count == 0) continue; + + // The encoded cell width is framing: it determines exactly how many + // bytes this row occupies. + switch (row_header.cell_width) { + inline .one, .two => |width| try decodeNarrowCells( + width, + reader, + cells[0..count], + ), + .four => _ = try decodeWordCells( + .four, + page, + row, + cells, + count, + reader, + style_remap, + hyperlink_remap, + ), + .eight => { + if (comptime bulk_codec) { + // Read the encoded words directly into page storage, + // then normalize them in place. The raw words are only + // ever touched as integers until normalization makes + // them valid cells. + const words: [*]u64 = @ptrCast(cells.ptr); + try reader.readSliceAll( + std.mem.sliceAsBytes(cells[0..count]), + ); + var row_or: u64 = 0; + for (0..count) |x| { + row_or |= words[x]; + applyCell( + page, + row, + cells, + x, + words[x], + style_remap, + hyperlink_remap, + ); + } + normalizeWideRow(.eight, row, cells, count, row_or); + } else { + _ = try decodeWordCells( + .eight, + page, + row, + cells, + count, + reader, + style_remap, + hyperlink_remap, + ); + } + + // The implicit cell after a short row is narrow, which + // resolves a trailing wide marker exactly like an explicit + // narrow neighbor. Only full-width rows can encode wide + // markers. + if (count < cells.len and cells[count - 1].wide == .wide) { + cells[count - 1].wide = .narrow; + } + }, + } + } + + try decodeGraphemes(page, reader); +} + +/// Decode one row of width-one or width-two cells. +/// +/// These widths admit only bare codepoints, so cells store directly with at +/// most Unicode scalar validation: no styles, hyperlinks, wide pairs, or +/// row hints are reachable and no normalization state is needed. +fn decodeNarrowCells( + comptime width: Cell.EncodedWidth, + reader: *std.Io.Reader, + cells: []TerminalCell, +) DecodeError!void { + const size = comptime width.size(); + + // The staged payload path has the complete row buffered, making this + // one bounds check followed by a vectorizable widening loop. + const total = cells.len * size; + if (reader.bufferedLen() >= total) { + widenCells(width, reader.buffered()[0..total], cells); + reader.toss(total); + return; + } + + // Streaming sources fall back to bounded chunks. + var chunk: [1024]u8 = undefined; + var i: usize = 0; + while (i < cells.len) { + const n = @min(cells.len - i, chunk.len / size); + try reader.readSliceAll(chunk[0 .. n * size]); + widenCells(width, chunk[0 .. n * size], cells[i..][0..n]); + i += n; + } +} + +/// Store codepoint-valued encoded cells of the given width. +fn widenCells( + comptime width: Cell.EncodedWidth, + bytes: []const u8, + cells: []TerminalCell, +) void { + const size = comptime width.size(); + + // With the bulk codec layout this is a pure widening store: sixteen + // transported bytes per step, shuffling each pair of encoded values + // into u64 lane position against a zero vector and shifting them into + // the content field. Zig 0.16 disables loop auto-vectorization, so the + // scalar loop would issue one widening store per cell. + if (comptime bulk_codec) { + const words: [*]u64 = @ptrCast(cells.ptr); + const step = 16 / size; + var i: usize = 0; + while (i + step <= cells.len) : (i += step) { + widenStep(width, bytes, words, i); + } + if (i < cells.len) { + if (cells.len >= step) { + // Reprocess the final full window with overlapping stores, + // which rewrite the same widened values. + widenStep(width, bytes, words, cells.len - step); + } else { + while (i < cells.len) : (i += 1) { + words[i] = width.extend(widenValue(width, bytes[i * size ..])); + } + } + } + return; + } + + // When the native cell matches the wire word, this is a pure widening + // loop over integers. + if (comptime native_matches_wire) { + const words: [*]u64 = @ptrCast(cells.ptr); + for (0..cells.len) |i| { + words[i] = width.extend(widenValue(width, bytes[i * size ..])); + } + return; + } + + for (cells, 0..) |*cell, i| { + storeCell(cell, width.extend(widenValue(width, bytes[i * size ..]))); + } +} + +/// Widen sixteen transported bytes into their cell words at cell index `i`: +/// shuffle each pair of encoded values into u64 lane position against a +/// zero vector, then shift the value into the content field. Width two +/// first degrades surrogate lanes to U+FFFD, matching `widenValue`. +inline fn widenStep( + comptime width: Cell.EncodedWidth, + bytes: []const u8, + words: [*]u64, + i: usize, +) void { + const size = comptime width.size(); + const step = 16 / size; + + var in: @Vector(16, u8) = @as( + *align(1) const @Vector(16, u8), + @ptrCast(bytes[i * size ..].ptr), + ).*; + + // Width two admits surrogates, which degrade to U+FFFD exactly + // like `widenValue`. Width one cannot encode an invalid scalar. + if (comptime width == .two) { + const values: @Vector(8, u16) = @bitCast(in); + const invalid = (values & @as(@Vector(8, u16), @splat(0xF800))) == + @as(@Vector(8, u16), @splat(0xD800)); + in = @bitCast(@select( + u16, + invalid, + @as(@Vector(8, u16), @splat(0xFFFD)), + values, + )); + } + + const zero: @Vector(16, u8) = @splat(0); + inline for (0..step / 2) |pair| { + const mask: @Vector(16, i32) = comptime mask: { + var mask: [16]i32 = @splat(~@as(i32, 0)); + for (0..size) |byte| { + mask[byte] = @intCast((2 * pair) * size + byte); + mask[8 + byte] = @intCast((2 * pair + 1) * size + byte); + } + break :mask mask; + }; + const lanes: @Vector(2, u64) = @bitCast( + @shuffle(u8, in, zero, mask), + ); + @as( + *align(@alignOf(u64)) @Vector(2, u64), + @ptrCast(words + i + 2 * pair), + ).* = lanes << @splat(@bitOffsetOf(Cell, "content")); + } +} + +/// Read and validate one narrow transported value. +inline fn widenValue( + comptime width: Cell.EncodedWidth, + bytes: []const u8, +) width.Int() { + const size = comptime width.size(); + const value = std.mem.readInt(width.Int(), bytes[0..size], .little); + + // Width one cannot encode an invalid scalar. Width two admits + // surrogates, which degrade exactly like their full-width form. + if (comptime width != .one) { + if (!validScalar(value)) return 0xFFFD; + } + return value; +} + +/// Decode one row of width-four or fallback full-width cells through the +/// complete per-cell normalization path. +/// +/// Returns the bitwise OR of every decoded wire word so callers can gate +/// the trailing wide-pair resolution pass without a second scan. +fn decodeWordCells( + comptime width: Cell.EncodedWidth, + page: *TerminalPage, + row: *TerminalRow, + cells: []TerminalCell, + count: usize, + reader: *std.Io.Reader, + style_remap: *const StyleRemap, + hyperlink_remap: *const HyperlinkRemap, +) DecodeError!u64 { + const size = comptime width.size(); + var row_or: u64 = 0; + + // The staged payload path has the complete row buffered. + const total = count * size; + if (reader.bufferedLen() >= total) { + const bytes = reader.buffered()[0..total]; + for (0..count) |x| { + const bits = width.extend(std.mem.readInt( + width.Int(), + bytes[x * size ..][0..size], + .little, + )); + row_or |= bits; + applyCell( + page, + row, + cells, + x, + bits, + style_remap, + hyperlink_remap, + ); + } + reader.toss(total); + normalizeWideRow(width, row, cells, count, row_or); + return row_or; + } + + // Streaming sources fall back to per-cell reads. + for (0..count) |x| { + const bits = width.extend(try io.readInt(reader, width.Int())); + row_or |= bits; + applyCell( + page, + row, + cells, + x, + bits, + style_remap, + hyperlink_remap, + ); + } + normalizeWideRow(width, row, cells, count, row_or); + return row_or; +} + +/// Resolve wide-pair relationships for one decoded row. +/// +/// Cell decoding stores every cell unresolved, so this pass applies +/// `normalizeWide` in order, which is equivalent to interleaving it with +/// the stores. Rows whose word OR carries no wide bits are already +/// normalized: every cell is narrow. Width four and narrower transports +/// cannot encode wide bits at all, so those rows skip the check entirely. +inline fn normalizeWideRow( + comptime width: Cell.EncodedWidth, + row: *const TerminalRow, + cells: []TerminalCell, + count: usize, + row_or: u64, +) void { + if (comptime width != .eight) return; + const wide_mask: u64 = comptime @bitCast(Cell{ .width = 3 }); + if (row_or & wide_mask == 0) return; + for (0..count) |x| normalizeWide(row, cells, x); +} + +/// Whether the value is a valid Unicode scalar value. +inline fn validScalar(cp: u32) bool { + return cp <= 0x10FFFF and (cp < 0xD800 or cp > 0xDFFF); +} + +/// Normalize one encoded cell word and store it at `cells[x]`. +/// +/// This owns every per-cell decode rule except grapheme suffixes and +/// wide-pair resolution: content validation, reserved-value degradation, +/// and style and hyperlink remapping with reference counting. Callers run +/// `normalizeWideRow` over the stored row afterward. +fn applyCell( + page: *TerminalPage, + row: *TerminalRow, + cells: []TerminalCell, + x: usize, + bits_wire: u64, + style_remap: *const StyleRemap, + hyperlink_remap: *const HyperlinkRemap, +) void { + const cell = &cells[x]; + + // The default cell is the common case and needs no normalization, + // reference counting, or table lookups. + if (bits_wire == 0) { + storeCell(cell, 0); + return; + } + + // Any word is a valid wire cell because its fields are raw integers, + // so all normalization below is plain field access. + var wire: Cell = @bitCast(bits_wire); + + // Hyperlink linkage is derived from the remapped ID below. The stored + // native cell starts unlinked either way. + const link_encoded = wire.hyperlink_id; + wire.hyperlink_id = 0; + wire.hyperlink = false; + + switch (@as(Cell.Kind, @enumFromInt(wire.kind))) { + // Kind 1 differs from 0 only by declaring a grapheme suffix section + // entry, which reattaches through the native grapheme APIs later. + .codepoint, .codepoint_grapheme => { + wire.kind = @intFromEnum(Cell.Kind.codepoint); + if (!validScalar(wire.content)) wire.content = 0xFFFD; + + // Kitty image and placement state is not part of this snapshot + // version, but the placeholder is still a valid Unicode scalar. + // Preserve it and derive the native row hint so later row + // operations remain correct. + if (wire.content == kitty_virtual_placeholder) { + row.kitty_virtual_placeholder = true; + } + }, + + // Only the palette index is meaningful; the remaining content bits + // are reserved and must not obscure it. + .bg_color_palette => wire.content = @as(u8, @truncate(wire.content)), + + .bg_color_rgb => {}, + } + + // Reserved semantic content degrades to plain output. + wire.semantic_content = @intFromEnum(std.enums.fromInt( + TerminalCell.SemanticContent, + wire.semantic_content, + ) orelse .output); + + // IDs belong to the encoded page. Translate them to IDs assigned by the + // destination page before storing them on cells. The table owns one + // reference and each decoded cell owns one additional reference. + if (wire.style_id != 0) { + wire.style_id = style_remap.get(wire.style_id); + if (wire.style_id != 0) { + page.styles.use(page.memory, wire.style_id); + row.styled = true; + } + } + + storeCell(cell, @bitCast(wire)); + + if (link_encoded != 0) link: { + const link_native = hyperlink_remap.get(link_encoded); + if (link_native == 0) break :link; + + // setHyperlink records the cell mapping but intentionally does not + // increment the set's reference count. If its map is full, undo our + // reference and leave this cell unlinked. + page.hyperlink_set.use(page.memory, link_native); + page.setHyperlink(row, cell, link_native) catch { + page.hyperlink_set.release(page.memory, link_native); + }; + } +} + +/// Resolve wide-pair relationships for the cell at `x` against its already +/// normalized predecessors. +fn normalizeWide(row: *const TerminalRow, cells: []TerminalCell, x: usize) void { + // A following cell resolves whether the previous wide marker owns a + // tail. Normalize the current marker immediately when possible, + // including a wide marker at the row end. + if (x > 0 and + cells[x - 1].wide == .wide and + cells[x].wide != .spacer_tail) + { + cells[x - 1].wide = .narrow; + } + switch (cells[x].wide) { + .narrow => {}, + + // A non-final wide marker remains pending until the next cell. + .wide => if (x + 1 == cells.len) { + cells[x].wide = .narrow; + }, + + .spacer_tail => if (x == 0 or + cells[x - 1].wide != .wide) + { + cells[x].wide = .narrow; + }, + + .spacer_head => if (x + 1 != cells.len or !row.wrap) { + cells[x].wide = .narrow; + }, + } +} + +/// Decode the grapheme suffix section into already decoded cells. +fn decodeGraphemes( + page: *TerminalPage, + reader: *std.Io.Reader, +) DecodeError!void { + const entries = try io.readInt(reader, u32); + for (0..entries) |_| { + // The staged and borrowed payload paths have every header buffered. + const y: u16, const x: u16, const cp_count: u16 = header: { + if (reader.bufferedLen() >= 6) { + const bytes = reader.buffered()[0..6]; + defer reader.toss(6); + break :header .{ + std.mem.readInt(u16, bytes[0..2], .little), + std.mem.readInt(u16, bytes[2..4], .little), + std.mem.readInt(u16, bytes[4..6], .little), + }; + } + break :header .{ + try io.readInt(reader, u16), + try io.readInt(reader, u16), + try io.readInt(reader, u16), + }; + }; + + // Resolve the target cell. Entries whose target cannot carry a + // suffix are optional detail: their codepoints are consumed to + // preserve framing and then dropped. + const target: ?struct { + row: *TerminalRow, + cell: *TerminalCell, + } = target: { + if (y >= page.size.rows or x >= page.size.cols) { + break :target null; + } + const row = page.getRow(y); + const cell = &page.getCells(row)[x]; + + // Cell decoding stores every kind 1 cell as a plain codepoint, + // so this also gives duplicate entries first-wins semantics. + if (cell.content_tag != .codepoint) break :target null; + if (cell.content.codepoint.data == 0) break :target null; + break :target .{ .row = row, .cell = cell }; + }; + + // Always consume every declared codepoint. Invalid scalars and NUL + // are not meaningful grapheme suffix components and are ignored. A + // dropped entry's codepoints are discarded in bulk. + const resolved = target orelse { + try reader.discardAll(@as(usize, cp_count) * 4); + continue; + }; + + var accept = true; + var index: usize = 0; + while (index < cp_count) { + const buffered = reader.buffered(); + if (buffered.len >= 4) { + const n = @min(cp_count - index, buffered.len / 4); + for (0..n) |i| applyGraphemeSuffix( + page, + resolved.row, + resolved.cell, + &accept, + std.mem.readInt(u32, buffered[i * 4 ..][0..4], .little), + ); + reader.toss(n * 4); + index += n; + } else { + applyGraphemeSuffix( + page, + resolved.row, + resolved.cell, + &accept, + try io.readInt(reader, u32), + ); + index += 1; + } + } + } +} + +/// Attach one decoded suffix codepoint to its resolved target cell. +/// +/// If native capacity is exhausted, any prefix already attached is removed +/// so the cell never exposes a truncated cluster, and `accept` latches +/// false so the entry's remaining codepoints are consumed but dropped. +fn applyGraphemeSuffix( + page: *TerminalPage, + row: *TerminalRow, + cell: *TerminalCell, + accept: *bool, + cp: u32, +) void { + if (!accept.*) return; + if (cp == 0 or !validScalar(cp)) return; + + page.appendGrapheme(row, cell, @intCast(cp)) catch { + if (cell.hasGrapheme()) { + page.clearGrapheme(cell); + page.updateRowGraphemeFlag(row); + } + accept.* = false; + }; +} + +/// The encoded word for one native cell and its hyperlink ID. +fn cellBits(cell: TerminalCell, link_id: TerminalHyperlinkId) u64 { + if (comptime native_matches_wire) { + var wire: Cell = @bitCast(cell); + wire.hyperlink = link_id != 0; + wire.hyperlink_id = link_id; + return @bitCast(wire); + } + + const wire: Cell = .{ + .kind = @intFromEnum(cell.content_tag), + .content = switch (cell.content_tag) { + .codepoint, + .codepoint_grapheme, + => cell.content.codepoint.data, + .bg_color_palette => cell.content.color_palette.data, + .bg_color_rgb => @as(u24, cell.content.color_rgb.r) | + (@as(u24, cell.content.color_rgb.g) << 8) | + (@as(u24, cell.content.color_rgb.b) << 16), + }, + .style_id = cell.style_id, + .width = switch (cell.wide) { + .narrow => 0, + .wide => 1, + .spacer_tail => 2, + .spacer_head => 3, + }, + .protected = cell.protected, + .hyperlink = link_id != 0, + .semantic_content = switch (cell.semantic_content) { + .output => 0, + .input => 1, + .prompt => 2, + }, + .hyperlink_id = link_id, + }; + return @bitCast(wire); +} + +/// Store one normalized, hyperlink-free wire word as a native cell. +fn storeCell(cell: *TerminalCell, bits: u64) void { + if (comptime native_matches_wire) { + const words: [*]u64 = @ptrCast(cell); + words[0] = bits; + return; + } + + const wire: Cell = @bitCast(bits); + var native: TerminalCell = .init(0); + switch (@as(Cell.Kind, @enumFromInt(wire.kind))) { + .codepoint, .codepoint_grapheme => native.content = .{ + .codepoint = .{ .data = @intCast(wire.content) }, + }, + .bg_color_palette => { + native.content_tag = .bg_color_palette; + native.content = .{ + .color_palette = .{ .data = @truncate(wire.content) }, + }; + }, + .bg_color_rgb => { + native.content_tag = .bg_color_rgb; + native.content = .{ .color_rgb = .{ + .r = @truncate(wire.content), + .g = @truncate(wire.content >> 8), + .b = @truncate(wire.content >> 16), + } }; + }, + } + native.style_id = wire.style_id; + native.wide = @enumFromInt(wire.width); + native.protected = wire.protected; + native.semantic_content = @enumFromInt(wire.semantic_content); + cell.* = native; +} + +/// Maps encoded table IDs to IDs assigned by the destination page. +/// +/// Build this by inserting each decoded table entry into the page, then +/// recording the encoded ID and the ID returned by the page's set. ID zero +/// is implicit and does not need an entry. Lookup must be cheap because the +/// grid decoder consults it for every styled or linked cell, so this is a +/// direct-indexed table rather than a hash map. +fn Remap(comptime Id: type) type { + return struct { + const Self = @This(); + + /// One slot for every possible encoded ID. + pub const capacity = std.math.maxInt(Id) + 1; + + /// Indexed by encoded ID; zero means unmapped or default. + entries: []Id, + + /// Tracks IDs that received an entry, including ones mapped to the + /// default, so callers can give duplicate table entries first-wins + /// semantics. + seen: std.DynamicBitSetUnmanaged, + + /// A remap with no entries at all: every lookup is unmapped. Use + /// this instead of `init` when the encoded table is empty so pages + /// without styles or hyperlinks allocate nothing. + pub const empty: Self = .{ .entries = &.{}, .seen = .{} }; + + pub fn init(alloc: Allocator) Allocator.Error!Self { + const entries = try alloc.alloc(Id, capacity); + errdefer alloc.free(entries); + @memset(entries, 0); + const seen = try std.DynamicBitSetUnmanaged.initEmpty( + alloc, + capacity, + ); + return .{ .entries = entries, .seen = seen }; + } + + pub fn deinit(self: *Self, alloc: Allocator) void { + if (self.entries.len != 0) { + alloc.free(self.entries); + self.seen.deinit(alloc); + } + self.* = undefined; + } + + /// Record one encoded-to-native mapping. Illegal on `empty`. + pub fn put(self: *Self, encoded: Id, native: Id) void { + assert(!self.seen.isSet(encoded)); + self.entries[encoded] = native; + self.seen.set(encoded); + } + + /// Whether the encoded ID already has an entry, even a default + /// one. Illegal on `empty`. + pub fn contains(self: *const Self, encoded: Id) bool { + return self.seen.isSet(encoded); + } + + /// The native ID for an encoded ID, or zero when unmapped. + pub inline fn get(self: *const Self, encoded: Id) Id { + if (self.entries.len == 0) return 0; + return self.entries[encoded]; + } + }; +} +const test_golden_fixture = test_fixture.parse( + @embedFile("testdata/grid-v1.hex"), +); + +test "cell wire layout registry" { + const testing = std.testing; + + // The wire bit positions are format constants. The shifts and masks + // derive from the packed struct, so pin the struct itself to the + // documented registry so an edit cannot silently move wire bits. + try testing.expectEqual(0, @bitOffsetOf(Cell, "kind")); + try testing.expectEqual(2, @bitOffsetOf(Cell, "content")); + try testing.expectEqual(26, @bitOffsetOf(Cell, "style_id")); + try testing.expectEqual(42, @bitOffsetOf(Cell, "width")); + try testing.expectEqual(44, @bitOffsetOf(Cell, "protected")); + try testing.expectEqual(45, @bitOffsetOf(Cell, "hyperlink")); + try testing.expectEqual(46, @bitOffsetOf(Cell, "semantic_content")); + try testing.expectEqual(48, @bitOffsetOf(Cell, "hyperlink_id")); + + // The native cell currently matches the wire registry, which enables + // the bulk row codec. If this fails, the native layout diverged: either + // restore it or accept the portable codec and update this expectation. + try testing.expect(native_matches_wire); +} + +test "encoded cell width transport registry" { + const testing = std.testing; + + // Pin the transported value and admission mask of every width against + // the documented format: widths one and two carry the bare codepoint, + // width four the low word half, width eight the complete word. + const word: u64 = @bitCast(Cell{ + .kind = 1, + .content = 0xABCDEF, + .style_id = 0x1234, + .hyperlink_id = 0x5678, + }); + try testing.expectEqual(@as(u8, 0xEF), Cell.EncodedWidth.one.truncate(word)); + try testing.expectEqual(@as(u16, 0xCDEF), Cell.EncodedWidth.two.truncate(word)); + try testing.expectEqual( + @as(u32, @truncate(word)), + Cell.EncodedWidth.four.truncate(word), + ); + try testing.expectEqual(word, Cell.EncodedWidth.eight.truncate(word)); + + try testing.expectEqual( + @as(u64, 0x0000_0000_0000_03FC), + Cell.EncodedWidth.one.mask(), + ); + try testing.expectEqual( + @as(u64, 0x0000_0000_0003_FFFC), + Cell.EncodedWidth.two.mask(), + ); + try testing.expectEqual( + @as(u64, 0x0000_0000_FFFF_FFFF), + Cell.EncodedWidth.four.mask(), + ); + try testing.expectEqual( + @as(u64, 0xFFFF_FFFF_FFFF_FFFF), + Cell.EncodedWidth.eight.mask(), + ); + + // Selection returns the smallest admissible width, and every admitted + // word round-trips through its transport. + const cases = [_]struct { cell: Cell, width: Cell.EncodedWidth }{ + .{ .cell = .{}, .width = .one }, + .{ .cell = .{ .content = 0xFF }, .width = .one }, + .{ .cell = .{ .content = 0x100 }, .width = .two }, + .{ .cell = .{ .content = 0xFFFF }, .width = .two }, + .{ .cell = .{ .content = 0x10000 }, .width = .four }, + .{ .cell = .{ .kind = 2, .content = 7 }, .width = .four }, + .{ .cell = .{ .content = 'a', .style_id = 63 }, .width = .four }, + .{ .cell = .{ .content = 'a', .style_id = 64 }, .width = .eight }, + .{ .cell = .{ .width = 1, .content = 'a' }, .width = .eight }, + .{ .cell = .{ .protected = true }, .width = .eight }, + .{ .cell = .{ .semantic_content = 1 }, .width = .eight }, + .{ .cell = .{ .hyperlink = true, .hyperlink_id = 1 }, .width = .eight }, + }; + inline for (cases) |case| { + const case_word: u64 = @bitCast(case.cell); + try testing.expectEqual( + case.width, + Cell.EncodedWidth.select(case_word), + ); + try testing.expectEqual( + case_word, + case.width.extend(case.width.truncate(case_word)), + ); + } +} + +test "grid golden encoding and decoding" { + const capacity: terminal_page.Capacity = .{ + .cols = 3, + .rows = 4, + .styles = 0, + .hyperlink_bytes = 0, + .grapheme_bytes = 64, + .string_bytes = 0, + }; + var source = try TerminalPage.init(capacity); + defer source.deinit(); + + // The first row covers semantic metadata, a wide-cell pair, and a + // multi-codepoint grapheme. + const wide = source.getRowAndCell(0, 0); + wide.row.semantic_prompt = .prompt; + wide.cell.* = .init('A'); + wide.cell.wide = .wide; + wide.cell.protected = true; + wide.cell.semantic_content = .prompt; + + const tail = source.getRowAndCell(1, 0); + tail.cell.wide = .spacer_tail; + tail.cell.semantic_content = .input; + + const grapheme = source.getRowAndCell(2, 0); + grapheme.cell.* = .init('x'); + try source.setGraphemes( + grapheme.row, + grapheme.cell, + &.{ 0x0301, 0x0302 }, + ); + + // The second row covers both background-color kinds and a wrapped spacer + // head with the remaining row semantic value. + const palette = source.getRowAndCell(0, 1); + palette.cell.content_tag = .bg_color_palette; + palette.cell.content = .{ .color_palette = .{ .data = 7 } }; + + const rgb = source.getRowAndCell(1, 1); + rgb.cell.content_tag = .bg_color_rgb; + rgb.cell.content = .{ .color_rgb = .{ + .r = 0xaa, + .g = 0xbb, + .b = 0xcc, + } }; + rgb.cell.protected = true; + + const head = source.getRowAndCell(2, 1); + head.cell.wide = .spacer_head; + head.row.wrap = true; + head.row.wrap_continuation = true; + head.row.semantic_prompt = .prompt_continuation; + + // The third row is bare ASCII text with an interior blank, which uses + // the one-byte encoded cell width. + source.getRowAndCell(0, 2).cell.* = .init('h'); + source.getRowAndCell(2, 2).cell.* = .init('i'); + + // The fourth row needs the two-byte width for a codepoint above U+00FF. + source.getRowAndCell(0, 3).cell.* = .init(0x0416); // Ж + source.getRowAndCell(1, 3).cell.* = .init('!'); + + var encoded: [160]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try encode(&source, &writer); + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/grid-v1.hex", + "snapshot_fixture-grid-v1.hex", + &test_golden_fixture, + writer.buffered(), + ); + + var destination = try TerminalPage.init(capacity); + defer destination.deinit(); + var style_remap = try StyleRemap.init(std.testing.allocator); + defer style_remap.deinit(std.testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(std.testing.allocator); + defer hyperlink_remap.deinit(std.testing.allocator); + + // Decode the checked-in reference through a one-byte reader buffer. + var fixture_reader: std.Io.Reader = .fixed(&test_golden_fixture); + var read_buffer: [1]u8 = undefined; + var limited = fixture_reader.limited(.unlimited, &read_buffer); + try decode( + &destination, + &limited.interface, + &style_remap, + &hyperlink_remap, + ); + try destination.verifyIntegrity(std.testing.allocator); + + const decoded_wide = destination.getRowAndCell(0, 0); + try std.testing.expectEqual(@as(u21, 'A'), decoded_wide.cell.codepoint()); + try std.testing.expectEqual(TerminalCell.Wide.wide, decoded_wide.cell.wide); + try std.testing.expect(decoded_wide.cell.protected); + try std.testing.expectEqual( + TerminalCell.SemanticContent.prompt, + decoded_wide.cell.semantic_content, + ); + try std.testing.expectEqual( + TerminalRow.SemanticPrompt.prompt, + decoded_wide.row.semantic_prompt, + ); + + const decoded_tail = destination.getRowAndCell(1, 0); + try std.testing.expectEqual( + TerminalCell.Wide.spacer_tail, + decoded_tail.cell.wide, + ); + try std.testing.expectEqual( + TerminalCell.SemanticContent.input, + decoded_tail.cell.semantic_content, + ); + + const decoded_grapheme = destination.getRowAndCell(2, 0); + try std.testing.expectEqualSlices( + u21, + &.{ 0x0301, 0x0302 }, + destination.lookupGrapheme(decoded_grapheme.cell).?, + ); + + const decoded_palette = destination.getRowAndCell(0, 1); + try std.testing.expectEqual( + TerminalCell.ContentTag.bg_color_palette, + decoded_palette.cell.content_tag, + ); + try std.testing.expectEqual( + @as(u8, 7), + decoded_palette.cell.content.color_palette.data, + ); + + const decoded_rgb = destination.getRowAndCell(1, 1); + try std.testing.expectEqual( + TerminalCell.ContentTag.bg_color_rgb, + decoded_rgb.cell.content_tag, + ); + try std.testing.expectEqual( + TerminalCell.RGB{ .r = 0xaa, .g = 0xbb, .b = 0xcc }, + decoded_rgb.cell.content.color_rgb, + ); + try std.testing.expect(decoded_rgb.cell.protected); + + const decoded_head = destination.getRowAndCell(2, 1); + try std.testing.expectEqual( + TerminalCell.Wide.spacer_head, + decoded_head.cell.wide, + ); + try std.testing.expect(decoded_head.row.wrap); + try std.testing.expect(decoded_head.row.wrap_continuation); + try std.testing.expectEqual( + TerminalRow.SemanticPrompt.prompt_continuation, + decoded_head.row.semantic_prompt, + ); + + try std.testing.expectEqual( + @as(u21, 'h'), + destination.getRowAndCell(0, 2).cell.codepoint(), + ); + try std.testing.expect(destination.getRowAndCell(1, 2).cell.isZero()); + try std.testing.expectEqual( + @as(u21, 'i'), + destination.getRowAndCell(2, 2).cell.codepoint(), + ); + try std.testing.expectEqual( + @as(u21, 0x0416), + destination.getRowAndCell(0, 3).cell.codepoint(), + ); + try std.testing.expectEqual( + @as(u21, '!'), + destination.getRowAndCell(1, 3).cell.codepoint(), + ); + + // A re-encode proves the decoded native page retains every wire field. + var reencoded: [160]u8 = undefined; + var rewriter: std.Io.Writer = .fixed(&reencoded); + try encode(&destination, &rewriter); + try std.testing.expectEqualStrings( + &test_golden_fixture, + rewriter.buffered(), + ); +} + +test "grid elides trailing default cells" { + const testing = std.testing; + var page = try TerminalPage.init(.{ .cols = 80, .rows = 3 }); + defer page.deinit(); + + // Row 0 is fully default. Row 1 has content in columns zero and two. + // Row 2 has one protected-only cell at column four. + const middle = page.getRowAndCell(2, 1); + middle.cell.* = .init('b'); + page.getRowAndCell(0, 1).cell.* = .init('a'); + page.getRowAndCell(4, 2).cell.protected = true; + + var encoded: [256]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try encode(&page, &writer); + + // 3 bytes per row header and cells only through the last non-default + // cell, followed by an empty grapheme section. The text row uses the + // one-byte width while the protected flag forces the full width. + try testing.expectEqual( + @as(usize, 3 + (3 + 3 * 1) + (3 + 5 * 8) + 4), + writer.buffered().len, + ); + + var destination = try TerminalPage.init(.{ .cols = 80, .rows = 3 }); + defer destination.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try decode(&destination, &reader, &style_remap, &hyperlink_remap); + try destination.verifyIntegrity(testing.allocator); + + try testing.expectEqual( + @as(u21, 'a'), + destination.getRowAndCell(0, 1).cell.codepoint(), + ); + try testing.expectEqual( + @as(u21, 'b'), + destination.getRowAndCell(2, 1).cell.codepoint(), + ); + try testing.expect(destination.getRowAndCell(4, 2).cell.protected); + try testing.expect(destination.getRowAndCell(79, 1).cell.isZero()); +} + +test "grid rejects a row cell count above the column count" { + const testing = std.testing; + var page = try TerminalPage.init(.{ .cols = 2, .rows = 1 }); + defer page.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var payload: [64]u8 = undefined; + var writer: std.Io.Writer = .fixed(&payload); + try writer.writeByte(@bitCast(Row{})); + try io.writeInt(&writer, u16, 3); + for (0..3) |_| try io.writeInt(&writer, u64, 0); + try io.writeInt(&writer, u32, 0); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try testing.expectError( + error.InvalidRowCellCount, + decode(&page, &reader, &style_remap, &hyperlink_remap), + ); +} + +test "grid normalizes incomplete wide cells" { + const testing = std.testing; + // One column puts the wide cell at the row end. Two columns put an + // ordinary narrow cell after it. Neither case supplies a spacer tail. + // Both rely on the implicit narrow cell rule when the tail is elided. + for ([_]u16{ 1, 2 }) |columns| { + const capacity: terminal_page.Capacity = .{ + .cols = columns, + .rows = 1, + }; + + // Craft the malformed row directly on the wire. Decoding keeps its + // content but makes the incomplete wide cell narrow. + var payload: [64]u8 = undefined; + var payload_writer: std.Io.Writer = .fixed(&payload); + try payload_writer.writeByte(@bitCast(Row{ .cell_width = .eight })); + try io.writeInt(&payload_writer, u16, columns); + try io.writeInt(&payload_writer, u64, @bitCast(Cell{ + .width = 1, // wide + .content = 'A', + })); + if (columns > 1) try io.writeInt(&payload_writer, u64, @bitCast(Cell{ + .content = 'B', + })); + try io.writeInt(&payload_writer, u32, 0); + + var destination = try TerminalPage.init(capacity); + defer destination.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var payload_reader: std.Io.Reader = .fixed( + payload_writer.buffered(), + ); + try decode( + &destination, + &payload_reader, + &style_remap, + &hyperlink_remap, + ); + try destination.verifyIntegrity(testing.allocator); + + try testing.expectEqual( + @as(u21, 'A'), + destination.getRowAndCell(0, 0).cell.codepoint(), + ); + try testing.expectEqual( + TerminalCell.Wide.narrow, + destination.getRowAndCell(0, 0).cell.wide, + ); + if (columns > 1) { + const second = destination.getRowAndCell(1, 0).cell; + try testing.expectEqual(@as(u21, 'B'), second.codepoint()); + try testing.expectEqual(TerminalCell.Wide.narrow, second.wide); + } + } + + // A wide marker whose spacer tail was elided by a short cell count is + // also normalized, and the trailing cells stay default. + var page = try TerminalPage.init(.{ .cols = 4, .rows = 1 }); + defer page.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var payload: [64]u8 = undefined; + var writer: std.Io.Writer = .fixed(&payload); + try writer.writeByte(@bitCast(Row{ .cell_width = .eight })); + try io.writeInt(&writer, u16, 1); + try io.writeInt(&writer, u64, @bitCast(Cell{ + .width = 1, // wide + .content = 'W', + })); + try io.writeInt(&writer, u32, 0); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try decode(&page, &reader, &style_remap, &hyperlink_remap); + try page.verifyIntegrity(testing.allocator); + try testing.expectEqual( + TerminalCell.Wide.narrow, + page.getRowAndCell(0, 0).cell.wide, + ); + try testing.expect(page.getRowAndCell(1, 0).cell.isZero()); +} + +test "grid normalizes reserved cell values" { + const testing = std.testing; + var page = try TerminalPage.init(.{ .cols = 3, .rows = 1 }); + defer page.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var payload: [64]u8 = undefined; + var writer: std.Io.Writer = .fixed(&payload); + + // Reserved row flag bits six and seven are ignored while the unknown + // semantic-prompt value degrades to none and wrap survives. Bits four + // and five select the full encoded cell width. + try writer.writeByte(0xFD); + try io.writeInt(&writer, u16, 3); + + // A surrogate codepoint with reserved semantic content 3. + try io.writeInt(&writer, u64, @bitCast(Cell{ + .content = 0xD800, + .semantic_content = 3, + })); + + // Reserved palette content bits do not obscure the palette index. + try io.writeInt(&writer, u64, @bitCast(Cell{ + .kind = 2, + .content = 0xFFFF07, + })); + + // An unknown style reference degrades to the default style, and an + // unknown hyperlink reference leaves the cell unlinked. + try io.writeInt(&writer, u64, @bitCast(Cell{ + .content = 'x', + .style_id = 5, + .hyperlink = true, + .hyperlink_id = 9, + })); + try io.writeInt(&writer, u32, 0); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try decode(&page, &reader, &style_remap, &hyperlink_remap); + try page.verifyIntegrity(testing.allocator); + + const first = page.getRowAndCell(0, 0); + try testing.expect(first.row.wrap); + try testing.expectEqual( + TerminalRow.SemanticPrompt.none, + first.row.semantic_prompt, + ); + try testing.expectEqual(@as(u21, 0xFFFD), first.cell.codepoint()); + try testing.expectEqual( + TerminalCell.SemanticContent.output, + first.cell.semantic_content, + ); + + const second = page.getRowAndCell(1, 0).cell; + try testing.expectEqual( + TerminalCell.ContentTag.bg_color_palette, + second.content_tag, + ); + try testing.expectEqual(@as(u8, 7), second.content.color_palette.data); + + const third = page.getRowAndCell(2, 0).cell; + try testing.expectEqual(@as(u21, 'x'), third.codepoint()); + try testing.expectEqual(@as(TerminalStyleId, 0), third.style_id); + try testing.expect(!third.hyperlink); + try testing.expectEqual(null, page.lookupHyperlink(third)); +} + +test "grid drops undeliverable grapheme entries" { + const testing = std.testing; + var page = try TerminalPage.init(.{ + .cols = 4, + .rows = 1, + .grapheme_bytes = 64, + }); + defer page.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var payload: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&payload); + try writer.writeByte(@bitCast(Row{ .cell_width = .eight })); + try io.writeInt(&writer, u16, 4); + // A kind 1 cell that receives a valid entry. + try io.writeInt(&writer, u64, @bitCast(Cell{ .kind = 1, .content = 'x' })); + // A kind 1 cell without an entry decodes as a plain codepoint. + try io.writeInt(&writer, u64, @bitCast(Cell{ .kind = 1, .content = 'y' })); + // A background cell cannot carry a suffix. + try io.writeInt(&writer, u64, @bitCast(Cell{ .kind = 2, .content = 7 })); + // An empty codepoint cannot carry a suffix. + try io.writeInt(&writer, u64, @bitCast(Cell{})); + + try io.writeInt(&writer, u32, 5); + // Valid entry with one invalid scalar dropped from within it. + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, 4); + try io.writeInt(&writer, u32, 0x0301); + try io.writeInt(&writer, u32, 0); + try io.writeInt(&writer, u32, 0xD800); + try io.writeInt(&writer, u32, 0x0302); + // Duplicate entry for the same cell is consumed and dropped. + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, 1); + try io.writeInt(&writer, u32, 0x0303); + // Entry for the background cell is consumed and dropped. + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, 2); + try io.writeInt(&writer, u16, 1); + try io.writeInt(&writer, u32, 0x0301); + // Entry for the empty cell is consumed and dropped. + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, 3); + try io.writeInt(&writer, u16, 1); + try io.writeInt(&writer, u32, 0x0301); + // Entry outside the grid is consumed and dropped. + try io.writeInt(&writer, u16, 7); + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, 1); + try io.writeInt(&writer, u32, 0x0301); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try decode(&page, &reader, &style_remap, &hyperlink_remap); + try page.verifyIntegrity(testing.allocator); + + const first = page.getRowAndCell(0, 0); + try testing.expectEqualSlices( + u21, + &.{ 0x0301, 0x0302 }, + page.lookupGrapheme(first.cell).?, + ); + const second = page.getRowAndCell(1, 0).cell; + try testing.expectEqual(@as(u21, 'y'), second.codepoint()); + try testing.expect(!second.hasGrapheme()); + try testing.expect(!page.getRowAndCell(2, 0).cell.hasGrapheme()); + try testing.expect(!page.getRowAndCell(3, 0).cell.hasGrapheme()); +} + +test "grid drops a complete grapheme when capacity fails mid-cluster" { + const testing = std.testing; + var page = try TerminalPage.init(.{ + .cols = 4, + .rows = 1, + .grapheme_bytes = 512, + }); + defer page.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var payload: [1024]u8 = undefined; + var writer: std.Io.Writer = .fixed(&payload); + try writer.writeByte(@bitCast(Row{ .cell_width = .eight })); + try io.writeInt(&writer, u16, 4); + for (0..4) |i| try io.writeInt( + &writer, + u64, + @bitCast(Cell{ .kind = 1, .content = @intCast('x' + i) }), + ); + + // BitmapAllocator rounds this capacity to 64 four-codepoint chunks. The + // first three cells consume 34 chunks. On the fourth cell, appending the + // 61st suffix needs 16 new chunks while the old 15-chunk slice is still + // live, exceeding capacity and forcing the complete suffix to be dropped. + try io.writeInt(&writer, u32, 4); + for ([_]u16{ 64, 64, 8, 61 }, 0..) |count, x| { + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, @intCast(x)); + try io.writeInt(&writer, u16, count); + for (0..count) |i| try io.writeInt( + &writer, + u32, + @intCast(0x0300 + i), + ); + } + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try decode(&page, &reader, &style_remap, &hyperlink_remap); + try page.verifyIntegrity(testing.allocator); + + const cell = page.getRowAndCell(3, 0); + try testing.expectEqual(@as(u21, @intCast('x' + 3)), cell.cell.codepoint()); + try testing.expect(!cell.cell.hasGrapheme()); + try testing.expect(cell.row.grapheme); + try testing.expectEqual(@as(usize, 3), page.graphemeCount()); +} + +test "grid encodes rows at their narrowest width" { + const testing = std.testing; + var page = try TerminalPage.init(.{ + .cols = 2, + .rows = 4, + .styles = 8, + }); + defer page.deinit(); + + // Width zero: bare Latin-1 text. + page.getRowAndCell(0, 0).cell.* = .init('A'); + page.getRowAndCell(1, 0).cell.* = .init(0xFF); + + // Width one: any BMP codepoint. + page.getRowAndCell(0, 1).cell.* = .init(0x0100); + + // Width two: a small style ID and a background color. + const style_id = try page.styles.add(page.memory, .{ + .flags = .{ .bold = true }, + }); + try testing.expect(style_id <= 63); + const styled = page.getRowAndCell(0, 2); + styled.cell.* = .init('s'); + styled.cell.style_id = style_id; + styled.row.styled = true; + const bg = page.getRowAndCell(1, 2); + bg.cell.content_tag = .bg_color_palette; + bg.cell.content = .{ .color_palette = .{ .data = 7 } }; + + // Width three: a protected cell. + page.getRowAndCell(0, 3).cell.protected = true; + + var encoded: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try encode(&page, &writer); + + const bytes = writer.buffered(); + try testing.expectEqual( + @as(usize, (3 + 2 * 1) + (3 + 1 * 2) + (3 + 2 * 4) + (3 + 1 * 8) + 4), + bytes.len, + ); + + // Each row header carries the expected width bits. + try testing.expectEqual(@as(u8, 0 << 4), bytes[0] & 0x30); + try testing.expectEqual(@as(u8, 1 << 4), bytes[5] & 0x30); + try testing.expectEqual(@as(u8, 2 << 4), bytes[10] & 0x30); + try testing.expectEqual(@as(u8, 3 << 4), bytes[21] & 0x30); + + var destination = try TerminalPage.init(.{ + .cols = 2, + .rows = 4, + .styles = 8, + }); + defer destination.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + const native_style = try destination.styles.add(destination.memory, .{ + .flags = .{ .bold = true }, + }); + style_remap.put(style_id, native_style); + + var reader: std.Io.Reader = .fixed(bytes); + try decode(&destination, &reader, &style_remap, &hyperlink_remap); + try destination.verifyIntegrity(testing.allocator); + + try testing.expectEqual( + @as(u21, 'A'), + destination.getRowAndCell(0, 0).cell.codepoint(), + ); + try testing.expectEqual( + @as(u21, 0xFF), + destination.getRowAndCell(1, 0).cell.codepoint(), + ); + try testing.expectEqual( + @as(u21, 0x0100), + destination.getRowAndCell(0, 1).cell.codepoint(), + ); + const decoded_styled = destination.getRowAndCell(0, 2); + try testing.expectEqual(@as(u21, 's'), decoded_styled.cell.codepoint()); + try testing.expectEqual(native_style, decoded_styled.cell.style_id); + try testing.expect(decoded_styled.row.styled); + try testing.expectEqual( + @as(u8, 7), + destination.getRowAndCell(1, 2).cell.content.color_palette.data, + ); + try testing.expect(destination.getRowAndCell(0, 3).cell.protected); +} + +test "grid decodes non-canonical cell widths" { + const testing = std.testing; + var page = try TerminalPage.init(.{ .cols = 2, .rows = 1 }); + defer page.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + // A bare ASCII row encoded at the full width is wasteful but valid. + var payload: [64]u8 = undefined; + var writer: std.Io.Writer = .fixed(&payload); + try writer.writeByte(@bitCast(Row{ .cell_width = .eight })); + try io.writeInt(&writer, u16, 2); + try io.writeInt(&writer, u64, @bitCast(Cell{ .content = 'o' })); + try io.writeInt(&writer, u64, @bitCast(Cell{ .content = 'k' })); + try io.writeInt(&writer, u32, 0); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try decode(&page, &reader, &style_remap, &hyperlink_remap); + try page.verifyIntegrity(testing.allocator); + try testing.expectEqual( + @as(u21, 'o'), + page.getRowAndCell(0, 0).cell.codepoint(), + ); + try testing.expectEqual( + @as(u21, 'k'), + page.getRowAndCell(1, 0).cell.codepoint(), + ); + + // Re-encoding canonicalizes the row back to the one-byte width. + var reencoded: [16]u8 = undefined; + var rewriter: std.Io.Writer = .fixed(&reencoded); + try encode(&page, &rewriter); + try testing.expectEqual(@as(usize, 3 + 2 + 4), rewriter.buffered().len); +} + +test "grid normalizes surrogates in two-byte cells" { + const testing = std.testing; + var page = try TerminalPage.init(.{ .cols = 2, .rows = 1 }); + defer page.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var payload: [16]u8 = undefined; + var writer: std.Io.Writer = .fixed(&payload); + try writer.writeByte(@bitCast(Row{ .cell_width = .two })); + try io.writeInt(&writer, u16, 2); + try io.writeInt(&writer, u16, 0xD800); + try io.writeInt(&writer, u16, 0x0416); + try io.writeInt(&writer, u32, 0); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try decode(&page, &reader, &style_remap, &hyperlink_remap); + try page.verifyIntegrity(testing.allocator); + try testing.expectEqual( + @as(u21, 0xFFFD), + page.getRowAndCell(0, 0).cell.codepoint(), + ); + try testing.expectEqual( + @as(u21, 0x0416), + page.getRowAndCell(1, 0).cell.codepoint(), + ); +} + +test "grid four-byte cells run full normalization" { + const testing = std.testing; + var page = try TerminalPage.init(.{ + .cols = 3, + .rows = 1, + .styles = 8, + }); + defer page.deinit(); + var style_remap = try StyleRemap.init(testing.allocator); + defer style_remap.deinit(testing.allocator); + var hyperlink_remap = try HyperlinkRemap.init(testing.allocator); + defer hyperlink_remap.deinit(testing.allocator); + + var payload: [32]u8 = undefined; + var writer: std.Io.Writer = .fixed(&payload); + try writer.writeByte(@bitCast(Row{ .cell_width = .four })); + try io.writeInt(&writer, u16, 3); + + // The Kitty placeholder does not fit two-byte cells but fits here + // and must still derive the native row hint. + try io.writeInt(&writer, u32, @truncate(@as(u64, @bitCast(Cell{ + .content = kitty_virtual_placeholder, + })))); + + // An unknown small style reference degrades to the default style. + try io.writeInt(&writer, u32, @truncate(@as(u64, @bitCast(Cell{ + .content = 'x', + .style_id = 63, + })))); + + // Reserved palette content bits are cleared at this width too. + try io.writeInt(&writer, u32, @truncate(@as(u64, @bitCast(Cell{ + .kind = 2, + .content = 0xFFFF07, + })))); + try io.writeInt(&writer, u32, 0); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try decode(&page, &reader, &style_remap, &hyperlink_remap); + try page.verifyIntegrity(testing.allocator); + + const first = page.getRowAndCell(0, 0); + try testing.expectEqual( + kitty_virtual_placeholder, + first.cell.codepoint(), + ); + try testing.expect(first.row.kitty_virtual_placeholder); + + const second = page.getRowAndCell(1, 0).cell; + try testing.expectEqual(@as(u21, 'x'), second.codepoint()); + try testing.expectEqual(@as(TerminalStyleId, 0), second.style_id); + + const third = page.getRowAndCell(2, 0).cell; + try testing.expectEqual( + TerminalCell.ContentTag.bg_color_palette, + third.content_tag, + ); + try testing.expectEqual(@as(u8, 7), third.content.color_palette.data); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/history.zig b/vendor/libghostty-vt/src/terminal/snapshot/history.zig new file mode 100644 index 00000000..90f1a749 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/history.zig @@ -0,0 +1,716 @@ +//! HISTORY record payload encoding. +//! +//! One HISTORY record describes the complete history for a previously encoded +//! screen. It is followed immediately by the number of complete PAGE records +//! declared by `page_count`. Those pages contain the history older than the +//! first page in the corresponding SCREEN sequence and are ordered from newest +//! to oldest. +//! +//! Every encoded SCREEN has one corresponding HISTORY record. A screen with no +//! older pages uses a zero page count and has no following PAGE records. +//! +//! The first SCREEN page may begin above the active area. Those incidental +//! history rows are already present at READY and are not repeated after +//! HISTORY. The corresponding SCREEN header declares the complete logical +//! history extent, including both that resident overlap and the following PAGE +//! records. +//! +//! All integers are unsigned and little-endian. +//! +//! ## Binary Format +//! +//! A HISTORY record is followed immediately by its declared PAGE records: +//! +//! ```text +//! +----------------------+ +//! | HISTORY record | +//! +----------------------+ +//! | PAGE record 0 | newest +//! +----------------------+ +//! | ... | +//! +----------------------+ +//! | PAGE record (n - 1) | oldest +//! +----------------------+ +//! +//! n = page_count +//! ``` +//! +//! Newest-to-oldest order lets a reader start showing most-recent +//! history as soon as possible which is more useful to a user. +//! +//! The PAGE sequence may be empty when all history is already included in the +//! first SCREEN page or when the screen has no history. +//! +//! ### Header +//! +//! The HISTORY payload consists only of this fixed header: +//! +//! ```text +//! 0 +--------------------------------+ +//! | Screen key (u16) | +//! 2 +--------------------------------+ +//! | Following page count (u32) | +//! 6 +--------------------------------+ +//! ``` +//! +//! A decoder uses `page_count`, rather than another record tag, to find the end +//! of the page sequence. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const test_fixture = @import("fixture.zig"); +const io = @import("io.zig"); +const page = @import("page.zig"); +const record = @import("record.zig"); +const screen = @import("screen.zig"); +const TerminalPage = @import("../page.zig").Page; +const TerminalPageList = @import("../PageList.zig"); +const TerminalScreen = @import("../Screen.zig"); +const TerminalScreenKey = @import("../ScreenSet.zig").Key; + +/// The complete fixed payload of one HISTORY record. +pub const Header = struct { + /// Number of encoded bytes in the fixed payload, calculated by its encoder. + pub const len = computeLen(); + + comptime { + // This size is part of the wire format. If it changes, the snapshot + // version must also change. + std.debug.assert(len == 6); + } + + /// Identifies the previously encoded screen that owns this history. + key: TerminalScreenKey, + + /// Number of complete PAGE records immediately following this record. + page_count: u32, + + /// Encode the fixed HISTORY payload. + pub fn encode( + self: Header, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + try io.writeInt(writer, u16, @intCast(@intFromEnum(self.key))); + try io.writeInt(writer, u32, self.page_count); + } + + pub const DecodeError = std.Io.Reader.Error || error{InvalidKey}; + + /// Decode and validate the fixed HISTORY payload. + pub fn decode(reader: *std.Io.Reader) Header.DecodeError!Header { + const raw = try io.readInt(reader, u16); + const Tag = @typeInfo(TerminalScreenKey).@"enum".tag_type; + const value = std.math.cast(Tag, raw) orelse + return error.InvalidKey; + const key = std.enums.fromInt( + TerminalScreenKey, + value, + ) orelse return error.InvalidKey; + return .{ + .key = key, + .page_count = try io.readInt(reader, u32), + }; + } + + fn computeLen() usize { + comptime { + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + const value: Header = .{ + .key = .primary, + .page_count = 0, + }; + value.encode(&writer) catch unreachable; + return writer.end; + } + } +}; + +/// Errors possible while encoding one HISTORY and its complete PAGE sequence. +pub const EncodeError = Allocator.Error || + page.EncodeError || + record.Writer.FinishError || + error{ + /// The complete historical prefix has more pages than the header fits. + PageCountOverflow, + }; + +/// Encode one screen's HISTORY and its complete historical pages. +/// +/// Pages are encoded newest-to-oldest. Compressed source pages are inspected +/// without changing their storage state. Completed records may already be +/// emitted if a later page fails. +pub fn encode( + terminal_screen: *const TerminalScreen, + key: TerminalScreenKey, + destination: *record.Writer, +) EncodeError!void { + // SCREEN begins at the page containing the active area's first row. Its + // leading rows are already resident; every previous complete page belongs + // to this HISTORY sequence. + const first = terminal_screen.pages.getTopLeft(.active).node.prev; + const page_count: usize = count: { + var page_count: usize = 0; + var node = first; + while (node) |current| : (node = current.prev) { + page_count += 1; + } + break :count page_count; + }; + + const header: Header = .{ + .key = key, + .page_count = std.math.cast( + u32, + page_count, + ) orelse return error.PageCountOverflow, + }; + + // HISTORY declares exactly how many PAGE records follow. + { + const payload = destination.begin(.history); + errdefer destination.cancel(); + try header.encode(payload); + try destination.finish(); + } + + // Walk backward so each page can be prepended by the decoder immediately. + // PreservedPage borrows resident pages and clones compressed pages without + // changing the source node's representation. + var node = first; + while (node) |current| : (node = current.prev) { + var preserved = try current.pagePreservingState(terminal_screen.alloc); + defer preserved.deinit(); + try page.encode(preserved.page(), destination); + } +} + +/// Errors possible while restoring one HISTORY and its PAGE sequence. +pub const DecodeError = Decoder.InitError || + Decoder.RestoreError || + error{ + /// The HISTORY key does not match the caller-selected screen. + UnexpectedScreenKey, + }; + +/// Errors possible while restoring one history PAGE into a native Screen. +pub const DecodePageError = Allocator.Error || + page.DecodeError || + TerminalPageList.PageAllocation.FinalizeError; + +/// Restore the next history PAGE record and prepend it to the native Screen. +/// +/// Returns the number of rows added above the screen's existing content. +/// The page is decoded directly into a detached PageList-pooled allocation +/// and prepended only after its record validates, so a failure leaves the +/// screen unchanged. A limit failure from `finalize` occurs after the record +/// bytes were fully consumed, leaving `source` aligned on the next record. +pub fn decodePage( + source: *std.Io.Reader, + alloc: Allocator, + terminal_screen: *TerminalScreen, +) DecodePageError!usize { + // PAGE exposes its exact capacity before decoding the payload, allowing + // the destination PageList to allocate the final backing memory once. + var decoder: page.Decoder = undefined; + try decoder.init(source); + var allocation = try terminal_screen.pages.allocatePage( + decoder.capacity(), + ); + defer allocation.deinit(); + try decoder.decode(allocation.page(), alloc); + + const rows = allocation.page().size.rows; + const contains_prompt = hasSemanticPrompt(allocation.page()); + try allocation.finalize(.prepend); + if (contains_prompt) terminal_screen.semantic_prompt.seen = true; + return rows; +} + +/// A decoded HISTORY manifest ready to restore its following PAGE records. +/// +/// Keeping manifest decoding separate lets the full snapshot wrapper route a +/// HISTORY sequence by its encoded key before any pages mutate a Screen. +pub const Decoder = struct { + source: *std.Io.Reader, + header: Header, + + pub const InitError = Header.DecodeError || + record.Reader.InitError || + record.Reader.FinishError || + error{ + /// The next record is valid but is not a HISTORY. + UnexpectedRecordTag, + }; + + pub const RestoreError = DecodePageError || error{ + /// The Screen already contains complete pages before its active page. + ExistingHistory, + }; + + /// Decode and finish the self-contained HISTORY manifest. + pub fn init(self: *Decoder, source: *std.Io.Reader) InitError!void { + var record_reader: record.Reader = undefined; + try record_reader.init(source); + if (record_reader.header.tag != .history) { + return error.UnexpectedRecordTag; + } + const header = try Header.decode(record_reader.payloadReader()); + try record_reader.finish(); + self.* = .{ .source = source, .header = header }; + } + + /// Restore the declared PAGE records into the selected native Screen. + pub fn decode( + self: *Decoder, + alloc: Allocator, + terminal_screen: *TerminalScreen, + ) RestoreError!void { + // A freshly restored SCREEN may carry overlap inside its first page, + // but cannot already contain a complete historical page before that + // boundary. + const active_top = terminal_screen.pages.getTopLeft(.active); + if (terminal_screen.pages.getTopLeft(.screen).node != active_top.node) { + return error.ExistingHistory; + } + + // Native row totals remain derived from the actual PAGE dimensions. + for (0..self.header.page_count) |_| { + _ = try decodePage(self.source, alloc, terminal_screen); + } + + terminal_screen.pages.assertIntegrity(); + terminal_screen.assertIntegrity(); + } +}; + +/// Restore one HISTORY and its declared PAGE records into a native Screen. +/// +/// Each PAGE is decoded directly into a detached PageList-pooled allocation and +/// prepended only after its record and native integrity are validated. If a +/// later PAGE fails, earlier successful pages remain as a contiguous recent +/// history prefix. +pub fn decode( + source: *std.Io.Reader, + alloc: Allocator, + expected_key: TerminalScreenKey, + terminal_screen: *TerminalScreen, +) DecodeError!void { + var decoder: Decoder = undefined; + try decoder.init(source); + if (decoder.header.key != expected_key) return error.UnexpectedScreenKey; + try decoder.decode(alloc, terminal_screen); +} + +fn hasSemanticPrompt(terminal_page: *const TerminalPage) bool { + const rows = terminal_page.rows.ptr(terminal_page.memory)[0..terminal_page.size.rows]; + for (rows) |row| { + if (row.semantic_prompt != .none) return true; + + const cells = row.cells.ptr( + terminal_page.memory, + )[0..terminal_page.size.cols]; + for (cells) |cell| { + if (cell.semantic_content == .prompt) return true; + } + } + return false; +} + +const test_header_fixture = test_fixture.parse(@embedFile("testdata/history-header-v1.hex")); + +test "HISTORY header golden encoding and decoding" { + const expected: Header = .{ + .key = .alternate, + .page_count = 0x01020304, + }; + var encoded: [Header.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try expected.encode(&writer); + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/history-header-v1.hex", + "snapshot_fixture-history-header-v1.hex", + &test_header_fixture, + writer.buffered(), + ); + try std.testing.expectEqual(Header.len, test_header_fixture.len); + + var reader: std.Io.Reader = .fixed(&test_header_fixture); + try std.testing.expectEqualDeep(expected, try Header.decode(&reader)); + for (0..Header.len) |fixture_len| { + var truncated: std.Io.Reader = .fixed( + test_header_fixture[0..fixture_len], + ); + try std.testing.expectError( + error.EndOfStream, + Header.decode(&truncated), + ); + } + + var invalid_key = test_header_fixture; + invalid_key[0] = 2; + var invalid_key_reader: std.Io.Reader = .fixed(&invalid_key); + try std.testing.expectError( + error.InvalidKey, + Header.decode(&invalid_key_reader), + ); +} + +test "HISTORY encodes newest first and restores complete history" { + // Choose an active height which spans two native pages, then grow until + // exactly two additional complete pages precede the active boundary. + var probe = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 80, .rows = 1, .max_scrollback_bytes = 0 }, + ); + const page_rows = probe.pages.getTopLeft(.screen).node.capacity().rows; + probe.deinit(); + const screen_rows = page_rows + 1; + + var source_screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ + .cols = 80, + .rows = screen_rows, + .max_scrollback_bytes = null, + }, + ); + defer source_screen.deinit(); + source_screen.cursorAbsolute(0, screen_rows - 1); + while (source_screen.pages.totalPages() < 4) { + try source_screen.testWriteString("\n"); + } + + const active_top = source_screen.pages.getTopLeft(.active); + const newest_history = active_top.node.prev.?; + const oldest_history = newest_history.prev.?; + try std.testing.expectEqual( + source_screen.pages.getTopLeft(.screen).node, + oldest_history, + ); + try std.testing.expectEqual(null, oldest_history.prev); + + // Mark the two complete historical pages so the wire sequence and final + // native order are visible. The oldest prompt also verifies derived state + // is updated only when that page is restored. + oldest_history.page().getRowAndCell(0, 0).cell.* = .init('A'); + oldest_history.page().getRowAndCell( + 0, + 0, + ).cell.semantic_content = .prompt; + newest_history.page().getRowAndCell(0, 0).cell.* = .init('B'); + + // Encode SCREEN before history compression, then compress eligible history + // and require HISTORY encoding to preserve each source storage state. + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try screen.encode(&source_screen, .primary, &stream); + const history_offset = destination.written().len; + + _ = source_screen.pages.compress(.full); + const oldest_storage = oldest_history.storage(); + const newest_storage = newest_history.storage(); + try encode(&source_screen, .primary, &stream); + try std.testing.expectEqual(oldest_storage, oldest_history.storage()); + try std.testing.expectEqual(newest_storage, newest_history.storage()); + + // Inspect the manifest and PAGE records independently. Newest history is + // sent first even though native PageList order is oldest-to-newest. + var history_source: std.Io.Reader = .fixed( + destination.written()[history_offset..], + ); + var history_record: record.Reader = undefined; + try history_record.init(&history_source); + try std.testing.expectEqual(record.Tag.history, history_record.header.tag); + const header = try Header.decode(history_record.payloadReader()); + try history_record.finish(); + try std.testing.expectEqual(TerminalScreenKey.primary, header.key); + try std.testing.expectEqual(@as(u32, 2), header.page_count); + + var decoded_newest = try page.decode( + &history_source, + std.testing.allocator, + ); + defer decoded_newest.deinit(); + try std.testing.expectEqual( + @as(u21, 'B'), + decoded_newest.getRowAndCell(0, 0).cell.codepoint(), + ); + var decoded_oldest = try page.decode( + &history_source, + std.testing.allocator, + ); + defer decoded_oldest.deinit(); + try std.testing.expectEqual( + @as(u21, 'A'), + decoded_oldest.getRowAndCell(0, 0).cell.codepoint(), + ); + try std.testing.expectError(error.EndOfStream, history_source.takeByte()); + + // Restore SCREEN first, then prepend HISTORY directly into that PageList. + var restore_source: std.Io.Reader = .fixed(destination.written()); + var decoded_screen = try screen.decode( + &restore_source, + std.testing.io, + std.testing.allocator, + .{ + .cols = 80, + .rows = screen_rows, + .max_scrollback_bytes = null, + }, + ); + defer decoded_screen.deinit(); + try std.testing.expectEqual( + TerminalScreenKey.primary, + decoded_screen.key, + ); + try std.testing.expectEqual( + @as(u64, @intCast(source_screen.pages.total_rows - screen_rows)), + decoded_screen.history_rows, + ); + const restored = &decoded_screen.screen; + try std.testing.expect(!restored.semantic_prompt.seen); + try decode( + &restore_source, + std.testing.allocator, + .primary, + restored, + ); + + try std.testing.expectEqual( + source_screen.pages.totalPages(), + restored.pages.totalPages(), + ); + const restored_oldest = restored.pages.getTopLeft(.screen).node; + try std.testing.expectEqual( + @as(u21, 'A'), + restored_oldest.page().getRowAndCell(0, 0).cell.codepoint(), + ); + try std.testing.expectEqual( + @as(u21, 'B'), + restored_oldest.next.? + .page().getRowAndCell(0, 0).cell.codepoint(), + ); + try std.testing.expect(restored.semantic_prompt.seen); + try std.testing.expectError(error.EndOfStream, restore_source.takeByte()); + + // Reuse a writable copy of the sequence for failure-path fixtures. + const encoded = try std.testing.allocator.dupe( + u8, + destination.written(), + ); + defer std.testing.allocator.free(encoded); + const first_page_offset = history_offset + record.Header.len + Header.len; + const first_payload_len = std.mem.readInt( + u32, + encoded[first_page_offset + 2 ..][0..4], + .little, + ); + const second_page_offset = + first_page_offset + record.Header.len + first_payload_len; + + // Truncate the first PAGE only after its header has exposed a capacity. + // Its detached allocation is discarded and the live SCREEN list remains + // completely unchanged. + var truncated_source: std.Io.Reader = .fixed( + encoded[0 .. second_page_offset - 1], + ); + var decoded_truncated = try screen.decode( + &truncated_source, + std.testing.io, + std.testing.allocator, + .{ + .cols = 80, + .rows = screen_rows, + .max_scrollback_bytes = null, + }, + ); + defer decoded_truncated.deinit(); + const truncated = &decoded_truncated.screen; + const truncated_screen_first = truncated.pages.getTopLeft(.screen).node; + const truncated_screen_page_count = truncated.pages.totalPages(); + try std.testing.expectError( + error.EndOfStream, + decode( + &truncated_source, + std.testing.allocator, + .primary, + truncated, + ), + ); + try std.testing.expectEqual( + truncated_screen_page_count, + truncated.pages.totalPages(), + ); + try std.testing.expectEqual( + truncated_screen_first, + truncated.pages.getTopLeft(.screen).node, + ); + truncated.pages.assertIntegrity(); + truncated.assertIntegrity(); + + // Corrupt only the older PAGE tag. A failure in a later PAGE keeps only + // the successfully prepended newer pages, contiguous with SCREEN. + std.mem.writeInt( + u16, + encoded[second_page_offset..][0..2], + @intFromEnum(record.Tag.screen), + .little, + ); + + var partial_source: std.Io.Reader = .fixed(encoded); + var decoded_partial = try screen.decode( + &partial_source, + std.testing.io, + std.testing.allocator, + .{ + .cols = 80, + .rows = screen_rows, + .max_scrollback_bytes = null, + }, + ); + defer decoded_partial.deinit(); + const partial = &decoded_partial.screen; + const screen_page_count = partial.pages.totalPages(); + try std.testing.expectError( + error.UnexpectedRecordTag, + decode( + &partial_source, + std.testing.allocator, + .primary, + partial, + ), + ); + try std.testing.expectEqual( + screen_page_count + 1, + partial.pages.totalPages(), + ); + try std.testing.expectEqual( + @as(u21, 'B'), + partial.pages.getTopLeft(.screen).node + .page().getRowAndCell(0, 0).cell.codepoint(), + ); + try std.testing.expect(!partial.semantic_prompt.seen); + partial.pages.assertIntegrity(); + partial.assertIntegrity(); +} + +test "HISTORY encodes and restores an empty sequence" { + var terminal_screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ + .cols = 2, + .rows = 2, + .max_scrollback_bytes = null, + }, + ); + defer terminal_screen.deinit(); + + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try encode(&terminal_screen, .primary, &stream); + + var inspect_source: std.Io.Reader = .fixed(destination.written()); + var history_record: record.Reader = undefined; + try history_record.init(&inspect_source); + const header = try Header.decode(history_record.payloadReader()); + try history_record.finish(); + try std.testing.expectEqual(@as(u32, 0), header.page_count); + try std.testing.expectError(error.EndOfStream, inspect_source.takeByte()); + + var decode_source: std.Io.Reader = .fixed(destination.written()); + const initial_first = terminal_screen.pages.getTopLeft(.screen).node; + try decode( + &decode_source, + std.testing.allocator, + .primary, + &terminal_screen, + ); + try std.testing.expectEqual( + initial_first, + terminal_screen.pages.getTopLeft(.screen).node, + ); + try std.testing.expectError(error.EndOfStream, decode_source.takeByte()); +} + +test "HISTORY rejects invalid routing and incomplete sequences" { + var terminal_screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ + .cols = 2, + .rows = 2, + .max_scrollback_bytes = null, + }, + ); + defer terminal_screen.deinit(); + + // Routing and sequence length still determine which native screen is + // mutated and where the following record begins, so they remain strict. + const Case = struct { + header: Header, + expected: anyerror, + }; + const cases = [_]Case{ + .{ + .header = .{ + .key = .alternate, + .page_count = 0, + }, + .expected = error.UnexpectedScreenKey, + }, + .{ + .header = .{ + .key = .primary, + .page_count = 1, + }, + .expected = error.EndOfStream, + }, + }; + for (cases) |case| { + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + const payload = stream.begin(.history); + errdefer stream.cancel(); + try case.header.encode(payload); + try stream.finish(); + + var source: std.Io.Reader = .fixed(destination.written()); + try std.testing.expectError( + case.expected, + decode( + &source, + std.testing.allocator, + .primary, + &terminal_screen, + ), + ); + terminal_screen.pages.assertIntegrity(); + terminal_screen.assertIntegrity(); + } +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/hyperlink.zig b/vendor/libghostty-vt/src/terminal/snapshot/hyperlink.zig new file mode 100644 index 00000000..b10d0dc8 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/hyperlink.zig @@ -0,0 +1,493 @@ +//! Snapshot hyperlink entry encoding. +//! +//! Each entry contains a URI and either an implicit numeric ID or an explicit +//! byte-string ID. Implicit IDs are generated by the terminal when the source +//! hyperlink has no explicit ID. A record can use these entries to build a +//! hyperlink table and assign indexes according to that record's format. +//! Indexing and ordering are properties of the containing record rather than +//! this codec. +//! +//! URIs and explicit IDs are non-empty arbitrary byte strings. Their lengths +//! are retained on the wire; they are not NUL-terminated and need not contain +//! UTF-8. Native page storage requires every allocated hyperlink string to +//! contain at least one byte. Encoding and standalone decoding reject empty +//! strings; PAGE decoding consumes but ignores entries it cannot represent. +//! +//! All integers are unsigned and little-endian. +//! +//! ## Implicit ID +//! +//! | Offset | Size | Field | +//! | -----: | --------: | :----------------- | +//! | 0 | 1 | Kind, `1` | +//! | 1 | 4 | Implicit ID (`u32`) | +//! | 5 | 4 | URI length (`u32`) | +//! | 9 | `uri_len` | URI bytes | +//! +//! ## Explicit ID +//! +//! | Offset | Size | Field | +//! | -----------: | --------: | :------------------------- | +//! | 0 | 1 | Kind, `2` | +//! | 1 | 4 | Explicit ID length (`u32`) | +//! | 5 | `id_len` | Explicit ID bytes | +//! | `5 + id_len` | 4 | URI length (`u32`) | +//! | `9 + id_len` | `uri_len` | URI bytes | +//! +//! Both variants have nine bytes of fixed overhead. The remaining bytes are +//! the URI plus the explicit ID, when present. +//! +//! The standalone decoder returns an allocator-owned hyperlink. PAGE decoding +//! reads strings directly into the destination page's allocator instead. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const test_fixture = @import("fixture.zig"); +const io = @import("io.zig"); +const terminal_hyperlink = @import("../hyperlink.zig"); +const terminal_page = @import("../page.zig"); +const terminal_size = @import("../size.zig"); + +const Kind = enum(u8) { + implicit = 1, + explicit = 2, +}; + +/// Errors possible while encoding one hyperlink entry. +pub const EncodeError = std.Io.Writer.Error || error{ + /// A native hyperlink URI must contain at least one byte. + InvalidUri, + + /// A native explicit hyperlink ID must contain at least one byte. + InvalidExplicitId, + + /// A string does not fit the version 1 length field. + StringTooLong, +}; + +/// Errors possible while decoding one allocator-owned hyperlink entry. +pub const DecodeError = std.Io.Reader.Error || Allocator.Error || error{ + /// The hyperlink kind is not defined by snapshot version 1. + InvalidKind, + + /// A native hyperlink URI must contain at least one byte. + InvalidUri, + + /// A native explicit hyperlink ID must contain at least one byte. + InvalidExplicitId, +}; + +/// Errors possible while decoding directly into a native page. +pub const DecodePageError = std.Io.Reader.Error || error{ + /// The hyperlink kind is not defined by snapshot version 1. + InvalidKind, +}; + +/// Encode one hyperlink entry. +pub fn encode( + value: terminal_hyperlink.Hyperlink, + writer: *std.Io.Writer, +) EncodeError!void { + // Validate the complete value before writing any part of this variable- + // length entry. This keeps callers able to roll back at entry boundaries. + if (value.uri.len == 0) return error.InvalidUri; + if (value.uri.len > std.math.maxInt(u32)) return error.StringTooLong; + if (value.id == .explicit) { + const id = value.id.explicit; + if (id.len == 0) return error.InvalidExplicitId; + if (id.len > std.math.maxInt(u32)) return error.StringTooLong; + } + + switch (value.id) { + .implicit => |id| { + try writer.writeByte(@intFromEnum(Kind.implicit)); + try io.writeInt(writer, u32, id); + try io.writeInt(writer, u32, @intCast(value.uri.len)); + try writer.writeAll(value.uri); + }, + .explicit => |id| { + try writer.writeByte(@intFromEnum(Kind.explicit)); + try io.writeInt(writer, u32, @intCast(id.len)); + try writer.writeAll(id); + try io.writeInt(writer, u32, @intCast(value.uri.len)); + try writer.writeAll(value.uri); + }, + } +} + +/// Decode one allocator-owned hyperlink entry. +/// +/// The caller owns the returned value and must call `Hyperlink.deinit`. +pub fn decode( + reader: *std.Io.Reader, + alloc: Allocator, +) DecodeError!terminal_hyperlink.Hyperlink { + const kind_raw = try reader.takeByte(); + const kind = std.enums.fromInt(Kind, kind_raw) orelse + return error.InvalidKind; + + return switch (kind) { + .implicit => implicit: { + const id = try io.readInt(reader, u32); + const uri_len: usize = @intCast(try io.readInt(reader, u32)); + if (uri_len == 0) return error.InvalidUri; + + const uri = try alloc.alloc(u8, uri_len); + errdefer alloc.free(uri); + try reader.readSliceAll(uri); + + break :implicit .{ + .id = .{ .implicit = id }, + .uri = uri, + }; + }, + + .explicit => explicit: { + const id_len: usize = @intCast(try io.readInt(reader, u32)); + if (id_len == 0) return error.InvalidExplicitId; + + const id = try alloc.alloc(u8, id_len); + errdefer alloc.free(id); + try reader.readSliceAll(id); + + const uri_len: usize = @intCast(try io.readInt(reader, u32)); + if (uri_len == 0) return error.InvalidUri; + + const uri = try alloc.alloc(u8, uri_len); + errdefer alloc.free(uri); + try reader.readSliceAll(uri); + + break :explicit .{ + .id = .{ .explicit = id }, + .uri = uri, + }; + }, + }; +} + +/// Decode one hyperlink directly into page-owned storage. +/// +/// Explicit ID and URI bytes are read into the page string allocator and the +/// completed entry is inserted into the page hyperlink set. The returned ID is +/// the native ID assigned by the destination page. Zero is returned when the +/// encoded value cannot be represented within the destination page, including +/// empty strings and insufficient advertised capacity. +pub fn decodePage( + page: *terminal_page.Page, + reader: *std.Io.Reader, +) DecodePageError!terminal_hyperlink.Id { + const kind_raw = try reader.takeByte(); + const kind = std.enums.fromInt(Kind, kind_raw) orelse + return error.InvalidKind; + + const entry: terminal_hyperlink.PageEntry = switch (kind) { + .implicit => implicit: { + const id = try io.readInt(reader, u32); + const uri_len: usize = @intCast(try io.readInt(reader, u32)); + if (uri_len == 0) return 0; + + const uri = decodePageString( + page, + reader, + uri_len, + ) catch |err| switch (err) { + error.StringsOutOfMemory => { + try reader.discardAll(uri_len); + return 0; + }, + else => |read_err| return read_err, + }; + + break :implicit .{ + .id = .{ .implicit = id }, + .uri = uri, + }; + }, + + .explicit => explicit: { + const id_len: usize = @intCast(try io.readInt(reader, u32)); + if (id_len == 0) { + const uri_len: usize = @intCast( + try io.readInt(reader, u32), + ); + try reader.discardAll(uri_len); + return 0; + } + + const id = decodePageString( + page, + reader, + id_len, + ) catch |err| switch (err) { + error.StringsOutOfMemory => { + try reader.discardAll(id_len); + const uri_len: usize = @intCast( + try io.readInt(reader, u32), + ); + try reader.discardAll(uri_len); + return 0; + }, + else => |read_err| return read_err, + }; + errdefer page.string_alloc.free( + page.memory, + id.slice(page.memory), + ); + + const uri_len: usize = @intCast(try io.readInt(reader, u32)); + if (uri_len == 0) { + page.string_alloc.free( + page.memory, + id.slice(page.memory), + ); + return 0; + } + + const uri = decodePageString( + page, + reader, + uri_len, + ) catch |err| switch (err) { + error.StringsOutOfMemory => { + try reader.discardAll(uri_len); + page.string_alloc.free( + page.memory, + id.slice(page.memory), + ); + return 0; + }, + else => |read_err| return read_err, + }; + + break :explicit .{ + .id = .{ .explicit = id }, + .uri = uri, + }; + }, + }; + return page.hyperlink_set.addContext( + page.memory, + entry, + .{ .page = page }, + ) catch { + // The entry was fully consumed, so capacity failure only loses this + // optional hyperlink. Free its strings and let cells restore unlinked. + entry.free(page); + return 0; + }; +} + +fn decodePageString( + page: *terminal_page.Page, + reader: *std.Io.Reader, + len: usize, +) (std.Io.Reader.Error || error{StringsOutOfMemory})!terminal_size.Offset(u8).Slice { + std.debug.assert(len > 0); + + // Allocate space for the string and read directly into it. + const value = page.string_alloc.alloc( + u8, + page.memory, + len, + ) catch return error.StringsOutOfMemory; + errdefer page.string_alloc.free(page.memory, value); + try reader.readSliceAll(value); + + return .{ + .len = value.len, + .offset = terminal_size.getOffset( + u8, + page.memory, + &value[0], + ), + }; +} + +const test_implicit_fixture = test_fixture.parse(@embedFile("testdata/hyperlink-implicit-v1.hex")); +const test_explicit_fixture = test_fixture.parse(@embedFile("testdata/hyperlink-explicit-v1.hex")); + +test "golden implicit encoding" { + const value: terminal_hyperlink.Hyperlink = .{ + .id = .{ .implicit = 0x01020304 }, + .uri = "uri", + }; + + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try encode(value, &writer); + + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/hyperlink-implicit-v1.hex", + "snapshot_fixture-hyperlink-implicit-v1.hex", + &test_implicit_fixture, + writer.buffered(), + ); + + var reader: std.Io.Reader = .fixed(&test_implicit_fixture); + var decoded = try decode(&reader, std.testing.allocator); + defer decoded.deinit(std.testing.allocator); + try std.testing.expectEqualDeep(value, decoded); +} + +test "golden explicit encoding" { + const value: terminal_hyperlink.Hyperlink = .{ + .id = .{ .explicit = "id" }, + .uri = "uri", + }; + + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try encode(value, &writer); + + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/hyperlink-explicit-v1.hex", + "snapshot_fixture-hyperlink-explicit-v1.hex", + &test_explicit_fixture, + writer.buffered(), + ); + + var reader: std.Io.Reader = .fixed(&test_explicit_fixture); + var decoded = try decode(&reader, std.testing.allocator); + defer decoded.deinit(std.testing.allocator); + try std.testing.expectEqualDeep(value, decoded); +} + +test "encode rejects invalid strings before writing" { + const cases = [_]struct { + value: terminal_hyperlink.Hyperlink, + expected: anyerror, + }{ + .{ + .value = .{ .id = .{ .implicit = 1 }, .uri = "" }, + .expected = error.InvalidUri, + }, + .{ + .value = .{ .id = .{ .explicit = "" }, .uri = "uri" }, + .expected = error.InvalidExplicitId, + }, + .{ + .value = .{ .id = .{ .explicit = "id" }, .uri = "" }, + .expected = error.InvalidUri, + }, + }; + + for (cases) |case| { + var encoded: [32]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try std.testing.expectError( + case.expected, + encode(case.value, &writer), + ); + try std.testing.expectEqual(@as(usize, 0), writer.end); + } +} + +test "decode rejects empty strings" { + const cases = [_]struct { + fixture: []const u8, + expected: anyerror, + }{ + .{ + .fixture = "\x01\x04\x03\x02\x01\x00\x00\x00\x00", + .expected = error.InvalidUri, + }, + .{ + .fixture = "\x02\x00\x00\x00\x00", + .expected = error.InvalidExplicitId, + }, + .{ + .fixture = "\x02\x02\x00\x00\x00id\x00\x00\x00\x00", + .expected = error.InvalidUri, + }, + }; + + for (cases) |case| { + var reader: std.Io.Reader = .fixed(case.fixture); + try std.testing.expectError( + case.expected, + decode(&reader, std.testing.allocator), + ); + } +} + +test "decodePage ignores empty strings" { + const fixtures = [_][]const u8{ + "\x01\x04\x03\x02\x01\x00\x00\x00\x00", + "\x02\x00\x00\x00\x00\x03\x00\x00\x00uri", + "\x02\x02\x00\x00\x00id\x00\x00\x00\x00", + }; + + for (fixtures) |fixture| { + var page = try terminal_page.Page.init(.{ + .cols = 1, + .rows = 1, + .hyperlink_bytes = 512, + .string_bytes = 16, + }); + defer page.deinit(); + + var reader: std.Io.Reader = .fixed(fixture); + try std.testing.expectEqual( + @as(terminal_hyperlink.Id, 0), + try decodePage(&page, &reader), + ); + try std.testing.expectEqual(@as(usize, 0), page.hyperlink_set.count()); + try std.testing.expectError(error.EndOfStream, reader.takeByte()); + } +} + +test "decode rejects invalid kinds" { + for ([_]u8{ 0, 3, std.math.maxInt(u8) }) |kind| { + var fixture: [1]u8 = .{kind}; + var reader: std.Io.Reader = .fixed(&fixture); + try std.testing.expectError( + error.InvalidKind, + decode(&reader, std.testing.allocator), + ); + } +} + +test "decode allocation failure" { + { + var reader: std.Io.Reader = .fixed(&test_implicit_fixture); + var failing = std.testing.FailingAllocator.init( + std.testing.allocator, + .{ .fail_index = 0 }, + ); + try std.testing.expectError( + error.OutOfMemory, + decode(&reader, failing.allocator()), + ); + } + + for (0..2) |fail_index| { + var failing = std.testing.FailingAllocator.init( + std.testing.allocator, + .{ .fail_index = fail_index }, + ); + var reader: std.Io.Reader = .fixed(&test_explicit_fixture); + try std.testing.expectError( + error.OutOfMemory, + decode(&reader, failing.allocator()), + ); + } +} + +test "reject every truncation" { + const fixtures: [2][]const u8 = .{ + &test_implicit_fixture, + &test_explicit_fixture, + }; + + for (fixtures) |fixture| { + for (0..fixture.len) |fixture_len| { + var reader: std.Io.Reader = .fixed(fixture[0..fixture_len]); + try std.testing.expectError( + error.EndOfStream, + decode(&reader, std.testing.allocator), + ); + } + } +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/io.zig b/vendor/libghostty-vt/src/terminal/snapshot/io.zig new file mode 100644 index 00000000..d47f96cc --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/io.zig @@ -0,0 +1,55 @@ +//! Wire-format I/O helpers. +//! +//! These are sometimes very thin layers over standard `std.Io.Writer` +//! but its to help ensure consistency in our behavior and avoid some +//! pitfalls. + +const std = @import("std"); +const testing = std.testing; + +/// writeInt ensures we write in little-endian since that is our +/// standard form. +pub fn writeInt( + writer: *std.Io.Writer, + comptime T: type, + value: T, +) std.Io.Writer.Error!void { + try writer.writeInt(T, value, .little); +} + +/// Read a little-endian integer. +/// +/// This avoids `takeInt` because at the time of writing this the underlying +/// implementation uses `takeArray` which asserts that the reader buffer +/// is as large as T. Our approach works with caller-selected buffers that +/// may be smaller. +pub fn readInt( + reader: *std.Io.Reader, + comptime T: type, +) std.Io.Reader.Error!T { + var buf: [@sizeOf(T)]u8 = undefined; + try reader.readSliceAll(&buf); + return std.mem.readInt(T, &buf, .little); +} + +test "integer round trip with a one-byte reader buffer" { + var encoded: [6]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try writeInt(&writer, u16, 0x1234); + try writeInt(&writer, u32, 0x56789abc); + + // Keep the reader buffer smaller than either integer to verify readInt + // does not inherit std.Io.Reader.takeInt's buffer-size requirement. + var source: std.Io.Reader = .fixed(&encoded); + var buf: [1]u8 = undefined; + var limited = source.limited(.unlimited, &buf); + + try testing.expectEqual( + @as(u16, 0x1234), + try readInt(&limited.interface, u16), + ); + try testing.expectEqual( + @as(u32, 0x56789abc), + try readInt(&limited.interface, u32), + ); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/main.zig b/vendor/libghostty-vt/src/terminal/snapshot/main.zig new file mode 100644 index 00000000..f69d78c9 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/main.zig @@ -0,0 +1,182 @@ +//! Terminal snapshot binary representation and codecs. +//! +//! This is NOT a full transport-ready format to implement generic replay +//! software such as multiplexers, recorders (e.g. asciinema), etc. The goal +//! of this package is to provide a documented, binary-compatible representation +//! for a terminal state. +//! +//! We call this a "snapshot." The snapshot is purposely laid out in a way +//! that prioritizes making a terminal functional as quickly as possible. +//! To do that, it sends the active terminal state followed by a READY record, +//! then complete history. +//! +//! READY denotes that enough state has been sent down to render the +//! terminal and reconstruct its unfinished VT Stream state. +//! +//! After READY, we send history pages (scrollback). Finally, the +//! snapshot ends with an empty FINISH marker. +//! +//! ## Snapshot Format +//! +//! This documents snapshot format 1. Version 1 is the work-in-progress +//! format that we intended to continue to break until we can promise +//! binary compatibility. +//! +//! A snapshot is one envelope followed by a sequence of records. The envelope +//! occurs once at byte zero. Every record is independently framed as a fixed +//! header followed by the number of payload bytes declared by that header. +//! +//! ```text +//! +------------------+ +//! | Envelope | +//! +------------------+ +//! | Record 1 header | +//! +------------------+ +//! | Record 1 payload | +//! +------------------+ +//! | Record 2 header | +//! +------------------+ +//! | Record 2 payload | +//! +------------------+ +//! | ... | +//! +------------------+ +//! ``` +//! +//! Record groups have a strict order: +//! +//! ```text +//! +----------------------------------------+ +//! | TERMINAL | +//! +----------------------------------------+ +//! | SCREEN * terminal.screen_count | +//! | PAGE * each screen.page_count | +//! +----------------------------------------+ +//! | CONTINUATION | +//! +----------------------------------------+ +//! | READY | +//! +----------------------------------------+ +//! | HISTORY * terminal.screen_count | +//! | PAGE * each history.page_count | +//! +----------------------------------------+ +//! | FINISH | +//! +----------------------------------------+ +//! ``` +//! +//! The SCREEN and HISTORY sequence groups may each appear in any key order. +//! Each key must occur exactly once in each group and must identify one of the +//! screens declared by TERMINAL. SCREEN contains the complete pages needed to +//! restore each active area. HISTORY contains the older complete pages for its +//! screen in newest-to-oldest order so they can be prepended as they arrive. +//! Every SCREEN has one corresponding HISTORY, even when its history page count +//! is zero. FINISH terminates the snapshot. Bytes after FINISH belong to the +//! containing transport and are not consumed by snapshot decoding. +//! +//! CONTINUATION contains the bytes required to bring the terminal's +//! VT parser/stream up to the same state, or no bytes if it should be +//! in the ground state. +//! +//! READY and FINISH are empty marker records. READY separates the renderable +//! active state and continuation from history; FINISH terminates the complete +//! snapshot. Every record, including both markers, is independently protected +//! by CRC32C. Each SCREEN declares its complete logical history extent, allowing +//! a client to size its scrollbar at READY even though older PAGE records arrive +//! afterward. +//! +//! ## Encoding +//! +//! Encode a complete snapshot into any writer: +//! +//! ```zig +//! var output: std.Io.Writer.Allocating = .init(alloc); +//! defer output.deinit(); +//! +//! try snapshot.encode(alloc, &output.writer, &terminal, .{ +//! .continuation = .ground, +//! }); +//! +//! const bytes = output.written(); +//! ``` +//! +//! Encoding begins at the writer's current position, so unrelated bytes may +//! precede the snapshot. The encoder buffers only the current record payload +//! to calculate its length and CRC32C; completed records stream immediately. +//! Buffering is an encoder implementation detail, not a requirement of the +//! wire format. +//! +//! A failure may leave prior complete records, or a partial record if the +//! destination itself fails. Such a prefix has no FINISH marker and +//! cannot be restored as a complete snapshot. +//! +//! Each record type usually exposes an `encode` function that encodes +//! a complete record, such as `screen.encode`. +//! +//! ## Decoding +//! +//! `snapshot.decode` consumes exactly one snapshot through FINISH and leaves +//! any following bytes unread. This permits multiple snapshots or live protocol +//! data to share a stream without waiting for the peer to close it. Transports +//! that deliver live PTY data before history finishes must multiplex that data +//! outside this ordered snapshot record sequence. +//! +//! ```zig +//! var decoded = try snapshot.decode(alloc, io, &reader, .{ +//! .max_continuation_bytes = 1024 * 1024, +//! }); +//! defer decoded.deinit(alloc); +//! var terminal = decoded.toOwned(); +//! defer terminal.deinit(alloc); +//! ``` +//! +//! Use `snapshot.decodeExact` for a bounded file or buffer that must contain +//! only one snapshot. It preserves the stricter end-of-file check, which may +//! block when used with a live stream. +//! +//! `snapshot.Decoder` decodes the same stream incrementally so the terminal +//! becomes usable at READY, before history has arrived. Each `next` call +//! applies one history page to the by-then live terminal and returning null +//! validates FINISH: +//! +//! ```zig +//! var decoder: snapshot.Decoder = .init(&reader); +//! var decoded = try decoder.ready(alloc, io, .{ +//! .max_continuation_bytes = 1024 * 1024, +//! }); +//! defer decoded.deinit(alloc); +//! +//! var terminal = decoded.toOwned(); +//! defer terminal.deinit(alloc); +//! +//! // Render, replay the continuation, process input... +//! while (try decoder.next(alloc, &terminal)) |progress| { +//! _ = progress; // Scrollback grew. +//! } +//! ``` + +pub const checkpoint = @import("checkpoint.zig"); +pub const continuation = @import("continuation.zig"); +pub const envelope = @import("envelope.zig"); +pub const grid = @import("grid.zig"); +pub const history = @import("history.zig"); +pub const hyperlink = @import("hyperlink.zig"); +pub const page = @import("page.zig"); +pub const record = @import("record.zig"); +pub const screen = @import("screen.zig"); +pub const style = @import("style.zig"); +pub const terminal = @import("terminal.zig"); + +const codec = @import("snapshot.zig"); +pub const EncodeError = codec.EncodeError; +pub const DecodeError = codec.DecodeError; +pub const DecodeExactError = codec.DecodeExactError; +pub const Continuation = codec.Continuation; +pub const EncodeOptions = codec.EncodeOptions; +pub const DecodeOptions = codec.DecodeOptions; +pub const Decoded = codec.Decoded; +pub const Decoder = codec.Decoder; +pub const encode = codec.encode; +pub const decode = codec.decode; +pub const decodeExact = codec.decodeExact; + +test { + @import("std").testing.refAllDecls(@This()); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/page.zig b/vendor/libghostty-vt/src/terminal/snapshot/page.zig new file mode 100644 index 00000000..6c8bb5ce --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/page.zig @@ -0,0 +1,1828 @@ +//! PAGE record payload encoding. +//! +//! One PAGE record represents a set of rows/columns in the terminal. +//! In libghostty, this happens to map internally to a very specific +//! data structure called a "page" (hence the name), but consumers of +//! the format don't need to reproduce that. +//! +//! The important thing is that one page is fully self-contained: it has +//! a dimension (cols x rows), a set of styles, hyperlinks, cells, etc. +//! and depends on no external state to decode that with some exceptions +//! like assets such as images. +//! +//! A page could have a dimension that doesn't match the terminal +//! dimensions, e.g. for lazy resize/reflow. Callers must be prepared for +//! that. +//! +//! ## Binary Format +//! +//! Every PAGE payload begins with a fixed header, followed by a +//! payload of styles, hyperlinks, and rows and columns. +//! +//! All integers are unsigned and little-endian. +//! +//! ### Header +//! +//! | Offset | Size | Field | +//! | -----: | ---: | :--------------------------------- | +//! | 0 | 2 | Logical columns (`u16`) | +//! | 2 | 2 | Logical rows (`u16`) | +//! | 4 | 2 | Non-default style count (`u16`) | +//! | 6 | 2 | Unique hyperlink count (`u16`) | +//! | 8 | 2 | Style capacity hint (`u16`) | +//! | 10 | 2 | Hyperlink capacity bytes (`u16`) | +//! | 12 | 4 | Grapheme capacity bytes (`u32`) | +//! | 16 | 4 | String capacity bytes (`u32`) | +//! +//! The first two fields (columns and rows) denote the dimensionality +//! of the page. The payload is guaranteed to have this dimensionality; +//! every row has exactly the columns specified. +//! +//! Next, the style count and hyperlink count denote the number of +//! styles and hyperlinks respectively that are sent with the page. +//! The default style and absence of a hyperlink are implicit at native +//! ID zero and are not included in these counts. Each encoded table entry +//! carries its nonzero native page ID. +//! +//! The final four fields are allocation hints copied from the source page. +//! These represent upper limits on what this page might contain. A decoder +//! can optionally choose to use this for preallocation or it can ignore +//! and decode and allocate dynamically. +//! +//! ### Payload +//! +//! ```text +//! +---------------------------+ +//! | Header | +//! | 20 bytes | +//! +---------------------------+ +//! | Style table | +//! | style_count entries | +//! +---------------------------+ +//! | Hyperlink table | +//! | hyperlink_count entries | +//! +---------------------------+ +//! | Grid | +//! | rows and grapheme section | +//! +---------------------------+ +//! +//! Style entry = encoded ID + style record +//! Hyperlink entry = encoded ID + hyperlink record +//! Grid = row 0 ... row (rows - 1) + grapheme suffix section +//! Row = row header + encoded cells +//! ``` +//! +//! Following the header, the payload contains exactly `style_count` style +//! records and `hyperlink_count` hyperlink records, followed by exactly +//! `rows` row records and one grapheme suffix section. There is no padding +//! between records. +//! +//! Each style begins with an ID (`u16`) followed by the typical style +//! binary representation (in style.zig). The ID is what cells will use +//! to reference the style. Decoders must maintain some kind of mapping +//! in order to associate these properly. +//! +//! Each hyperlink also begins with an ID (`u16`) with the same semantics +//! as the style ID. The hyperlink and style IDs are separate namespaces, +//! so they may collide. Following the ID, the hyperlink binary format +//! is encoded directly. +//! +//! Both style and hyperlink IDs are not guaranteed to be in any specific +//! order and may contain gaps (e.g. ID 1 and 3 but not 2). +//! +//! Rows and cells use the grid encoding documented in `grid.zig`. + +const std = @import("std"); +const build_options = @import("terminal_options"); +const Allocator = std.mem.Allocator; +const test_fixture = @import("fixture.zig"); +const grid = @import("grid.zig"); +const hyperlink = @import("hyperlink.zig"); +const io = @import("io.zig"); +const record = @import("record.zig"); +const style = @import("style.zig"); +const terminal_hyperlink = @import("../hyperlink.zig"); +const terminal_page = @import("../page.zig"); +const terminal_style = @import("../style.zig"); + +// Frequent constants we use +const TerminalHyperlink = terminal_hyperlink.Hyperlink; +const TerminalHyperlinkId = terminal_hyperlink.Id; +const TerminalHyperlinkPageEntry = terminal_hyperlink.PageEntry; +const TerminalCell = terminal_page.Cell; +const TerminalPage = terminal_page.Page; +const TerminalPageCapacity = terminal_page.Capacity; +const TerminalRow = terminal_page.Row; +const TerminalStyle = terminal_style.Style; +const TerminalStyleId = terminal_style.Id; + +const PayloadEncodeError = hyperlink.EncodeError || grid.EncodeError; + +const PayloadDecodeError = std.Io.Reader.Error || + Header.CapacityError || + grid.DecodeError || + error{ + /// The hyperlink kind is not defined by snapshot version 1. + InvalidKind, + + /// Native page backing memory could not be allocated. + OutOfMemory, + + /// The caller-provided page does not have the advertised capacity. + InvalidDestinationCapacity, + }; + +/// Errors possible while encoding a complete PAGE record. +pub const EncodeError = PayloadEncodeError || record.Writer.FinishError; + +/// Encode one complete PAGE record from a native page. +/// +/// The payload is built in the stream's reusable record buffer. If payload +/// encoding fails, no bytes from this record are emitted. +pub fn encode( + page: *const TerminalPage, + destination: *record.Writer, +) EncodeError!void { + const payload = destination.begin(.page); + errdefer destination.cancel(); + try encodePayload(page, payload); + try destination.finish(); +} + +/// Errors possible while decoding and validating a complete PAGE record. +pub const DecodeError = PayloadDecodeError || + record.Reader.InitError || + record.Reader.FinishError || + TerminalPage.IntegrityError || + error{ + /// The next record is valid but is not a PAGE. + UnexpectedRecordTag, + }; + +/// Decode and validate one complete PAGE record into a fresh native page. +/// +/// The record tag, payload boundary, CRC32C, payload contents, and final page +/// integrity are all validated before the page is returned. `alloc` is used +/// only for temporary decode remaps and integrity-check storage. +pub fn decode( + reader: *std.Io.Reader, + alloc: Allocator, +) DecodeError!TerminalPage { + var decoder: Decoder = undefined; + try decoder.init(reader); + + var page = TerminalPage.init(decoder.capacity()) catch + return error.OutOfMemory; + errdefer page.deinit(); + try decoder.decode(&page, alloc); + return page; +} + +/// PAGE record decoder where the caller is responsible for owning +/// the Page memory. This is particularly useful paired with +/// PageList.Builder so you can build up a proper PageList with +/// valid memory ownership. +pub const Decoder = struct { + record_reader: record.Reader, + header: Header, + + /// Begin decoding one PAGE record and expose its required capacity. + /// After this, call `capacity` to get the capacity to allocate + /// the page properly, then `decode` into it. + pub fn init(self: *Decoder, reader: *std.Io.Reader) DecodeError!void { + try self.record_reader.init(reader); + if (self.record_reader.header.tag != .page) { + return error.UnexpectedRecordTag; + } + + self.header = try Header.decode(self.record_reader.payloadReader()); + _ = try self.header.pageCapacity(); + } + + /// Return the exact native capacity advertised by the PAGE header. + pub fn capacity(self: *const Decoder) TerminalPageCapacity { + return self.header.pageCapacity() catch unreachable; + } + + /// Largest remaining PAGE payload staged into one contiguous buffer. + /// This comfortably covers every payload a standard-capacity page can + /// produce while keeping the allocation an untrusted length can force + /// far below the record framing's four-byte length limit. + const max_staged_payload = 8 * 1024 * 1024; + + /// Decode the remaining payload into caller-owned native page storage. + /// + /// The destination must be freshly initialized with `capacity`. `alloc` + /// is used only for temporary staging, ID remaps, and integrity-check + /// storage. + pub fn decode( + self: *Decoder, + destination: *TerminalPage, + alloc: Allocator, + ) DecodeError!void { + if (!std.meta.eql(destination.capacity, self.capacity())) { + return error.InvalidDestinationCapacity; + } + + destination.size = .{ + .cols = self.header.columns, + .rows = self.header.rows, + }; + + // `init` consumed exactly the fixed header from the declared + // payload, so this is the byte count of the tables and grid. + const remaining = self.record_reader.header.payload_len - Header.len; + + if (self.record_reader.payloadReader().bufferedLen() >= remaining) { + // The complete payload is already buffered, e.g. borrowed from + // an in-memory snapshot. Parse it in place with no staging + // copy; `finish` still enforces the CRC and exact exhaustion. + try decodePayloadBody( + self.record_reader.payloadReader(), + alloc, + destination, + self.header, + ); + } else if (remaining <= max_staged_payload) { + // Stage the payload with one bulk read. The whole payload passes + // through the checksum hasher as one update and the payload + // decoders then parse a flat buffer, which keeps per-row work free + // of stream adapters. The CRC and exact-length checks in `finish` + // are unaffected. + const staged = try alloc.alloc(u8, remaining); + defer alloc.free(staged); + try self.record_reader.payloadReader().readSliceAll(staged); + + var staged_reader: std.Io.Reader = .fixed(staged); + try decodePayloadBody( + &staged_reader, + alloc, + destination, + self.header, + ); + if (staged_reader.bufferedLen() != 0) { + return error.PayloadNotExhausted; + } + } else { + // A payload this large is either hostile or a page far beyond + // native capacities. Decode it through the streaming payload + // reader so its declared length cannot force an allocation. + try decodePayloadBody( + self.record_reader.payloadReader(), + alloc, + destination, + self.header, + ); + } + try self.record_reader.finish(); + + // The decoder normalizes every semantic value, so a complete decode + // upholds native page invariants by construction. Verifying them + // again is a defense against decoder bugs and follows the native + // page policy: full integrity verification only when slow runtime + // safety is enabled. + if (comptime build_options.slow_runtime_safety) { + try destination.verifyIntegrity(alloc); + } + } +}; + +/// Errors possible while discarding one PAGE record without decoding it. +pub const DiscardError = record.Reader.InitError || + record.Reader.FinishError || + std.Io.Reader.Error || + error{ + /// The next record is valid but is not a PAGE. + UnexpectedRecordTag, + }; + +/// Consume exactly one complete PAGE record, validating its framing and +/// CRC32C while discarding the payload without structural validation. +/// +/// This lets a decoder stay aligned with the record sequence while dropping +/// page content it can no longer apply. +pub fn discard(source: *std.Io.Reader) DiscardError!void { + var record_reader: record.Reader = undefined; + try record_reader.init(source); + if (record_reader.header.tag != .page) { + return error.UnexpectedRecordTag; + } + try record_reader.payloadReader().discardAll( + record_reader.header.payload_len, + ); + try record_reader.finish(); +} + +/// Encode a PAGE payload directly from a native page. +fn encodePayload( + page: *const TerminalPage, + writer: *std.Io.Writer, +) PayloadEncodeError!void { + // Write header + try Header.init(page).encode(writer); + + // Sparse styles + var style_it = page.styles.iterator(page.memory); + while (style_it.next()) |entry| { + try io.writeInt(writer, TerminalStyleId, entry.id); + try style.encode(entry.value_ptr.*, writer); + } + + // Sparse hyperlinks + var hyperlink_it = page.hyperlink_set.iterator(page.memory); + while (hyperlink_it.next()) |entry| { + try io.writeInt(writer, TerminalHyperlinkId, entry.id); + try hyperlink.encode(pageHyperlink(page, entry.value_ptr), writer); + } + + // Rows and cells + try grid.encode(page, writer); +} + +/// Decode a PAGE payload directly into a fresh native page. +/// +/// `alloc` is used only for the temporary native-ID remap tables. Styles, +/// hyperlinks, strings, graphemes, rows, and cells are stored in the page. +fn decodePayload( + reader: *std.Io.Reader, + alloc: Allocator, +) PayloadDecodeError!TerminalPage { + const header = try Header.decode(reader); + const capacity = try header.pageCapacity(); + var page = TerminalPage.init(capacity) catch + return error.OutOfMemory; + errdefer page.deinit(); + try decodePayloadBody(reader, alloc, &page, header); + return page; +} + +/// Decode PAGE tables and grid after the header into initialized native page +/// storage with the exact advertised capacity and dimensions. +fn decodePayloadBody( + reader: *std.Io.Reader, + alloc: Allocator, + page: *TerminalPage, + header: Header, +) PayloadDecodeError!void { + page.pauseIntegrityChecks(true); + defer page.pauseIntegrityChecks(false); + + // Pages without styles or hyperlinks, the common case for plain + // scrollback, skip the remap tables entirely: every encoded cell ID + // resolves to the default through the empty remap. + var style_remap: grid.StyleRemap = if (header.style_count > 0) + grid.StyleRemap.init(alloc) catch return error.OutOfMemory + else + .empty; + defer style_remap.deinit(alloc); + + var hyperlink_remap: grid.HyperlinkRemap = if (header.hyperlink_count > 0) + grid.HyperlinkRemap.init(alloc) catch return error.OutOfMemory + else + .empty; + defer hyperlink_remap.deinit(alloc); + + // Styles. The complete fixed-size entry is parsed from the buffered + // payload when possible so each entry costs no reader calls. + const style_entry_len = @sizeOf(TerminalStyleId) + style.len; + for (0..header.style_count) |_| { + const native_id: TerminalStyleId, const value = entry: { + if (reader.bufferedLen() >= style_entry_len) { + const bytes = reader.buffered()[0..style_entry_len]; + defer reader.toss(style_entry_len); + break :entry .{ + std.mem.readInt(TerminalStyleId, bytes[0..2], .little), + style.parseOrNull(bytes[2..][0..style.len]), + }; + } + break :entry .{ + try io.readInt(reader, TerminalStyleId), + try style.decodeOrNull(reader), + }; + }; + + // Zero is reserved for the implicit default. For a duplicate encoded + // ID, the first entry wins and this complete entry is simply ignored. + if (native_id == 0 or style_remap.contains(native_id)) continue; + + // Invalid/default styles map to the native default. `add` returns + // the existing entry for a repeated concrete value, taking one + // reference either way, while capacity failure degrades only this + // style. The references are surrendered through the remap below + // once every cell reference is installed. + const decoded_id: TerminalStyleId = if (value) |valid| decoded: { + if (valid.default()) break :decoded 0; + break :decoded page.styles.add( + page.memory, + valid, + ) catch 0; + } else 0; + style_remap.put(native_id, decoded_id); + } + + // Hyperlinks + for (0..header.hyperlink_count) |_| { + const native_id = try io.readInt(reader, TerminalHyperlinkId); + const decoded_id = try hyperlink.decodePage(page, reader); + + // As with styles, zero is reserved and the first duplicate encoded ID + // wins. Release any native entry reference created for an ignored ID. + if (native_id == 0 or hyperlink_remap.contains(native_id)) { + if (decoded_id != 0) { + page.hyperlink_set.release(page.memory, decoded_id); + } + continue; + } + + // Zero records an ignored table entry. Grid decoding treats every + // reference to it as no hyperlink. + hyperlink_remap.put(native_id, decoded_id); + } + + // Rows and cells + try grid.decode( + page, + reader, + &style_remap, + &hyperlink_remap, + ); + + // Every accepted table entry took one reference through `add` so grid + // decoding can safely attach its style to any number of cells. Unlike + // organically built pages, those references do not themselves represent + // cells. Release through the encoded-ID remap, so duplicate values + // which deduplicated to the same native ID each surrender their own + // reference; unused entries then become dead and disappear from + // canonical re-encoding. + var style_it = style_remap.seen.iterator(.{}); + while (style_it.next()) |encoded_id| { + const id = style_remap.entries[encoded_id]; + if (id != 0) page.styles.release(page.memory, id); + } + + // Hyperlink insertion likewise creates one temporary reference for every + // accepted wire table entry. Release through the encoded-ID remap rather + // than the native set so duplicate values which deduplicated to the same + // native ID each surrender their own reference. + var hyperlink_it = hyperlink_remap.seen.iterator(.{}); + while (hyperlink_it.next()) |encoded_id| { + const id = hyperlink_remap.entries[encoded_id]; + if (id != 0) page.hyperlink_set.release(page.memory, id); + } +} + +/// The fixed logical dimensions, table counts, and allocation hints at the +/// start of PAGE. +pub const Header = struct { + /// Number of bytes written by `encode`, calculated using the encoder itself + /// so this remains synchronized with the field-by-field wire format. + pub const len = computeLen(); + + comptime { + // This size is part of the wire format. If it changes, the snapshot + // version and golden fixtures must also change. + std.debug.assert(len == 20); + } + + /// Number of logical cells in every encoded row. + columns: u16, + + /// Number of logical rows encoded after the tables. + rows: u16, + + /// Number of non-default style entries following the header. + style_count: u16, + + /// Number of unique hyperlink entries following the style table. + hyperlink_count: u16, + + /// Suggested capacity for the native non-default style set. + style_capacity: u16, + + /// Suggested native hyperlink storage capacity in bytes. + hyperlink_capacity_bytes: u16, + + /// Suggested native grapheme storage capacity in bytes. + grapheme_capacity_bytes: u32, + + /// Suggested native string storage capacity in bytes. + string_capacity_bytes: u32, + + /// Encode the fixed PAGE payload header. + pub fn encode( + self: Header, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + try io.writeInt(writer, u16, self.columns); + try io.writeInt(writer, u16, self.rows); + try io.writeInt(writer, u16, self.style_count); + try io.writeInt(writer, u16, self.hyperlink_count); + try io.writeInt(writer, u16, self.style_capacity); + try io.writeInt(writer, u16, self.hyperlink_capacity_bytes); + try io.writeInt(writer, u32, self.grapheme_capacity_bytes); + try io.writeInt(writer, u32, self.string_capacity_bytes); + } + + /// Decode the fixed PAGE payload header. + /// + /// This reads field values only. The complete PAGE decoder is responsible + /// for applying configured limits before using any capacity hint. + pub fn decode(reader: *std.Io.Reader) std.Io.Reader.Error!Header { + return .{ + .columns = try io.readInt(reader, u16), + .rows = try io.readInt(reader, u16), + .style_count = try io.readInt(reader, u16), + .hyperlink_count = try io.readInt(reader, u16), + .style_capacity = try io.readInt(reader, u16), + .hyperlink_capacity_bytes = try io.readInt(reader, u16), + .grapheme_capacity_bytes = try io.readInt(reader, u32), + .string_capacity_bytes = try io.readInt(reader, u32), + }; + } + + /// Initialize a header from the current contents of a native page. + /// + /// This copies the page's existing allocation capacities. It does not + /// scan cells, allocate, or encode any bytes. + fn init(page: *const TerminalPage) Header { + return .{ + .columns = page.size.cols, + .rows = page.size.rows, + .style_count = @intCast(page.styles.count()), + .hyperlink_count = @intCast(page.hyperlink_set.count()), + .style_capacity = page.capacity.styles, + .hyperlink_capacity_bytes = page.capacity.hyperlink_bytes, + .grapheme_capacity_bytes = page.capacity.grapheme_bytes, + .string_capacity_bytes = page.capacity.string_bytes, + }; + } + + pub const CapacityError = error{ + InvalidDimensions, + }; + + /// Validate native allocation requirements and produce the page capacity. + fn pageCapacity(self: Header) CapacityError!TerminalPageCapacity { + if (self.columns == 0 or self.rows == 0) { + return error.InvalidDimensions; + } + + return .{ + .cols = self.columns, + .rows = self.rows, + .styles = self.style_capacity, + .hyperlink_bytes = self.hyperlink_capacity_bytes, + .grapheme_bytes = self.grapheme_capacity_bytes, + .string_bytes = self.string_capacity_bytes, + }; + } + + fn computeLen() usize { + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + const header: Header = .{ + .columns = 0, + .rows = 0, + .style_count = 0, + .hyperlink_count = 0, + .style_capacity = 0, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + header.encode(&writer) catch unreachable; + return writer.end; + } +}; + +fn pageHyperlink( + page: *const TerminalPage, + entry: *const TerminalHyperlinkPageEntry, +) TerminalHyperlink { + return .{ + .id = switch (entry.id) { + .implicit => |value| .{ .implicit = value }, + .explicit => |value| .{ .explicit = value.slice(page.memory) }, + }, + .uri = entry.uri.slice(page.memory), + }; +} + +const test_page_fixture = test_fixture.parse(@embedFile("testdata/page-v1.hex")); +const test_header_fixture = test_fixture.parse(@embedFile("testdata/page-header-v1.hex")); +const test_empty_framed_page_fixture = test_fixture.parse(@embedFile("testdata/page-empty-record-v1.hex")); + +test "PAGE header golden encoding and decoding" { + const header: Header = .{ + .columns = 0x0102, + .rows = 0x0304, + .style_count = 0x0506, + .hyperlink_count = 0x0708, + .style_capacity = 0x090a, + .hyperlink_capacity_bytes = 0x0b0c, + .grapheme_capacity_bytes = 0x0d0e0f10, + .string_capacity_bytes = 0x11121314, + }; + var buf: [Header.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try header.encode(&writer); + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/page-header-v1.hex", + "snapshot_fixture-page-header-v1.hex", + &test_header_fixture, + writer.buffered(), + ); + + var source: std.Io.Reader = .fixed(&test_header_fixture); + var read_buf: [1]u8 = undefined; + var limited = source.limited(.unlimited, &read_buf); + try std.testing.expectEqual(header, try Header.decode(&limited.interface)); +} + +test "reject every truncation" { + for (0..Header.len) |len| { + var reader: std.Io.Reader = .fixed(test_header_fixture[0..len]); + try std.testing.expectError(error.EndOfStream, Header.decode(&reader)); + } +} + +test "framed PAGE encode and decode a sparse native page" { + const capacity: TerminalPageCapacity = .{ + .cols = 3, + .rows = 2, + .styles = 8, + .hyperlink_bytes = 512, + .grapheme_bytes = 128, + .string_bytes = 256, + }; + var page = try TerminalPage.init(capacity); + defer page.deinit(); + + // Create holes in both source tables so the encoded IDs exercise sparse + // table remapping rather than coincidentally matching decoded IDs. + const style_a = try page.styles.add(page.memory, .{ + .flags = .{ .bold = true }, + }); + const dead_style = try page.styles.add(page.memory, .{ + .flags = .{ .italic = true }, + }); + const style_b = try page.styles.add(page.memory, .{ + .bg_color = .{ .palette = 42 }, + }); + page.styles.release(page.memory, dead_style); + + const first = page.getRowAndCell(0, 0); + first.cell.style_id = style_a; + first.row.styled = true; + + const second = page.getRowAndCell(1, 0); + second.cell.style_id = style_b; + second.row.styled = true; + + const third = page.getRowAndCell(2, 0); + page.styles.use(page.memory, style_a); + third.cell.style_id = style_a; + third.row.styled = true; + + const hyperlink_a = try page.insertHyperlink(.{ + .id = .{ .explicit = "a" }, + .uri = "alpha", + }); + const dead_hyperlink = try page.insertHyperlink(.{ + .id = .{ .explicit = "dead-id" }, + .uri = "dead-uri", + }); + const hyperlink_b = try page.insertHyperlink(.{ + .id = .{ .implicit = 0x01020304 }, + .uri = "beta", + }); + page.hyperlink_set.release(page.memory, dead_hyperlink); + + page.hyperlink_set.use(page.memory, hyperlink_a); + try page.setHyperlink(first.row, first.cell, hyperlink_a); + try page.setHyperlink(second.row, second.cell, hyperlink_b); + try page.setHyperlink(third.row, third.cell, hyperlink_a); + + // Cover graphemes, wide-cell pairs, colors, protection, and semantic + // metadata in one compact two-row page. + const grapheme = page.getRowAndCell(0, 1); + grapheme.cell.* = .init('x'); + try page.setGraphemes( + grapheme.row, + grapheme.cell, + &.{ 0x0301, 0x0302 }, + ); + + first.cell.content = .{ .codepoint = .{ .data = 'A' } }; + first.cell.wide = .wide; + first.cell.protected = true; + first.cell.semantic_content = .prompt; + first.row.semantic_prompt = .prompt; + + second.cell.wide = .spacer_tail; + second.cell.semantic_content = .input; + + third.cell.content_tag = .bg_color_palette; + third.cell.content = .{ .color_palette = .{ .data = 7 } }; + + const rgb = page.getRowAndCell(1, 1); + rgb.cell.content_tag = .bg_color_rgb; + rgb.cell.content = .{ .color_rgb = .{ + .r = 0xaa, + .g = 0xbb, + .b = 0xcc, + } }; + rgb.cell.protected = true; + + const spacer_head = page.getRowAndCell(2, 1); + spacer_head.cell.wide = .spacer_head; + spacer_head.row.wrap = true; + spacer_head.row.wrap_continuation = true; + spacer_head.row.semantic_prompt = .prompt_continuation; + + const header: Header = .{ + .columns = 3, + .rows = 2, + .style_count = 2, + .hyperlink_count = 2, + .style_capacity = 8, + .hyperlink_capacity_bytes = 512, + .grapheme_capacity_bytes = 128, + .string_capacity_bytes = 256, + }; + try std.testing.expectEqual(header, Header.init(&page)); + + // Lock the payload bytes and independently verify the counting writer sees + // the same encoded length. + var counter: std.Io.Writer.Discarding = .init(&.{}); + try encodePayload(&page, &counter.writer); + + var encoded: [512]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try encodePayload(&page, &writer); + + try test_fixture.expectEqual( + .page, + "src/terminal/snapshot/testdata/page-v1.hex", + "snapshot_fixture-page-v1.hex", + &test_page_fixture, + writer.buffered(), + ); + try std.testing.expectEqual( + @as(u64, test_page_fixture.len), + counter.count, + ); + + // Decode the checked-in reference rather than the just-generated bytes. + // A one-byte backing buffer exercises streaming reads across every field. + var source: std.Io.Reader = .fixed(&test_page_fixture); + var read_buf: [1]u8 = undefined; + var limited = source.limited(.unlimited, &read_buf); + var decoded = try decodePayload( + &limited.interface, + std.testing.allocator, + ); + defer decoded.deinit(); + + try std.testing.expectEqual(header, Header.init(&decoded)); + try decoded.verifyIntegrity(std.testing.allocator); + + // Decoding compacts the sparse source IDs while preserving table values + // and all cell references through the remap tables. + var style_it = decoded.styles.iterator(decoded.memory); + const decoded_style_a = style_it.next().?; + try std.testing.expectEqual(@as(TerminalStyleId, 1), decoded_style_a.id); + try std.testing.expect((TerminalStyle{ + .flags = .{ .bold = true }, + }).eql(decoded_style_a.value_ptr.*)); + const decoded_style_b = style_it.next().?; + try std.testing.expectEqual(@as(TerminalStyleId, 2), decoded_style_b.id); + try std.testing.expect((TerminalStyle{ + .bg_color = .{ .palette = 42 }, + }).eql(decoded_style_b.value_ptr.*)); + try std.testing.expectEqual(null, style_it.next()); + try std.testing.expectEqual( + @as(u16, 2), + decoded.styles.refCount(decoded.memory, decoded_style_a.id), + ); + try std.testing.expectEqual( + @as(u16, 1), + decoded.styles.refCount(decoded.memory, decoded_style_b.id), + ); + + var hyperlink_it = decoded.hyperlink_set.iterator(decoded.memory); + const decoded_link_a = hyperlink_it.next().?; + try std.testing.expectEqual(@as(TerminalHyperlinkId, 1), decoded_link_a.id); + const decoded_hyperlink_a = pageHyperlink(&decoded, decoded_link_a.value_ptr); + try std.testing.expectEqualStrings("a", decoded_hyperlink_a.id.explicit); + try std.testing.expectEqualStrings("alpha", decoded_hyperlink_a.uri); + const decoded_link_b = hyperlink_it.next().?; + try std.testing.expectEqual(@as(TerminalHyperlinkId, 2), decoded_link_b.id); + const decoded_hyperlink_b = pageHyperlink(&decoded, decoded_link_b.value_ptr); + try std.testing.expectEqual(@as(u32, 0x01020304), decoded_hyperlink_b.id.implicit); + try std.testing.expectEqualStrings("beta", decoded_hyperlink_b.uri); + try std.testing.expectEqual(null, hyperlink_it.next()); + + const decoded_first = decoded.getRowAndCell(0, 0); + try std.testing.expectEqual(@as(TerminalStyleId, 1), decoded_first.cell.style_id); + try std.testing.expectEqual( + @as(TerminalHyperlinkId, 1), + decoded.lookupHyperlink(decoded_first.cell).?, + ); + + const decoded_second = decoded.getRowAndCell(1, 0); + try std.testing.expectEqual(@as(TerminalStyleId, 2), decoded_second.cell.style_id); + try std.testing.expectEqual( + @as(TerminalHyperlinkId, 2), + decoded.lookupHyperlink(decoded_second.cell).?, + ); + + // The first re-encode reflects compacted IDs; subsequent round trips must + // stabilize byte-for-byte. + var reencoded: [512]u8 = undefined; + var rewriter: std.Io.Writer = .fixed(&reencoded); + try encodePayload(&decoded, &rewriter); + try std.testing.expect(!std.mem.eql( + u8, + writer.buffered(), + rewriter.buffered(), + )); + + var reencoded_reader: std.Io.Reader = .fixed(rewriter.buffered()); + var decoded_again = try decodePayload( + &reencoded_reader, + std.testing.allocator, + ); + defer decoded_again.deinit(); + try decoded_again.verifyIntegrity(std.testing.allocator); + + var reencoded_again: [512]u8 = undefined; + var rewriter_again: std.Io.Writer = .fixed(&reencoded_again); + try encodePayload(&decoded_again, &rewriter_again); + try std.testing.expectEqualStrings( + rewriter.buffered(), + rewriter_again.buffered(), + ); +} + +test "framed PAGE golden empty record" { + const capacity: TerminalPageCapacity = .{ + .cols = 1, + .rows = 1, + .styles = 0, + .hyperlink_bytes = 0, + .grapheme_bytes = 0, + .string_bytes = 0, + }; + var page = try TerminalPage.init(capacity); + defer page.deinit(); + + var destination: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try encode(&page, &stream); + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/page-empty-record-v1.hex", + "snapshot_fixture-page-empty-record-v1.hex", + &test_empty_framed_page_fixture, + destination.written(), + ); + + var source: std.Io.Reader = .fixed(&test_empty_framed_page_fixture); + var decoded = try decode(&source, std.testing.allocator); + defer decoded.deinit(); + try std.testing.expectEqual( + Header.init(&page), + Header.init(&decoded), + ); +} + +test "framed PAGE rejects incomplete wide cells transactionally" { + const testing = std.testing; + + // One column puts the wide cell at the row end. Two columns put an + // ordinary narrow cell after it. Neither case supplies a spacer tail. + for ([_]u16{ 1, 2 }) |columns| { + var page = try TerminalPage.init(.{ + .cols = columns, + .rows = 1, + }); + defer page.deinit(); + const first = page.getRowAndCell(0, 0).cell; + first.* = .init('A'); + first.wide = .wide; + if (columns > 1) { + page.getRowAndCell(1, 0).cell.* = .init('B'); + } + + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + try destination.writer.writeAll("prefix"); + var stream: record.Writer = .init( + testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try testing.expectError( + error.InvalidWideCell, + encode(&page, &stream), + ); + try testing.expectEqualStrings("prefix", destination.written()); + } +} + +test "framed PAGE rejects a different record tag" { + var wrong_tag = test_empty_framed_page_fixture; + std.mem.writeInt( + u16, + wrong_tag[0..2], + @intFromEnum(record.Tag.screen), + .little, + ); + var reader: std.Io.Reader = .fixed(&wrong_tag); + try std.testing.expectError( + error.UnexpectedRecordTag, + decode(&reader, std.testing.allocator), + ); +} + +test "framed PAGE preserves following bytes" { + var source: std.Io.Reader = .fixed( + test_empty_framed_page_fixture ++ "next", + ); + var decoded = try decode(&source, std.testing.allocator); + defer decoded.deinit(); + try std.testing.expectEqualStrings("next", try source.take(4)); +} + +test "decode sparse page rejects every truncation" { + for (0..test_page_fixture.len) |len| { + var reader: std.Io.Reader = .fixed(test_page_fixture[0..len]); + try std.testing.expectError( + error.EndOfStream, + decodePayload(&reader, std.testing.allocator), + ); + } +} + +test "decode accepts unordered sparse style IDs and ignores zero" { + const header: Header = .{ + .columns = 2, + .rows = 1, + .style_count = 2, + .hyperlink_count = 0, + .style_capacity = 8, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + + var descending: [ + Header.len + + 2 * (2 + style.len) + + 3 + 2 * 8 + 4 + ]u8 = undefined; + var descending_writer: std.Io.Writer = .fixed(&descending); + try header.encode(&descending_writer); + try io.writeInt(&descending_writer, TerminalStyleId, 3); + try style.encode(.{ .flags = .{ .bold = true } }, &descending_writer); + try io.writeInt(&descending_writer, TerminalStyleId, 2); + try style.encode(.{ .flags = .{ .italic = true } }, &descending_writer); + try descending_writer.writeByte(@bitCast(grid.Row{ .cell_width = .eight })); + try io.writeInt(&descending_writer, u16, 2); // cell count + try io.writeInt(&descending_writer, u64, @bitCast(grid.Cell{ + .content = 'A', + .style_id = 3, + })); + try io.writeInt(&descending_writer, u64, @bitCast(grid.Cell{ + .content = 'B', + .style_id = 2, + })); + try io.writeInt(&descending_writer, u32, 0); // grapheme section + + var descending_reader: std.Io.Reader = .fixed( + descending_writer.buffered(), + ); + var decoded_descending = try decodePayload( + &descending_reader, + std.testing.allocator, + ); + defer decoded_descending.deinit(); + try std.testing.expectEqual(@as(usize, 2), decoded_descending.styles.count()); + try std.testing.expectEqual( + @as(TerminalStyleId, 1), + decoded_descending.getRowAndCell(0, 0).cell.style_id, + ); + try std.testing.expectEqual( + @as(TerminalStyleId, 2), + decoded_descending.getRowAndCell(1, 0).cell.style_id, + ); + + const one_header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 1, + .hyperlink_count = 0, + .style_capacity = 8, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + + var zero: [Header.len + 2 + style.len + 7]u8 = undefined; + var zero_writer: std.Io.Writer = .fixed(&zero); + try one_header.encode(&zero_writer); + try io.writeInt(&zero_writer, TerminalStyleId, 0); + try style.encode(.{ .flags = .{ .bold = true } }, &zero_writer); + + var zero_grid = try TerminalPage.init(.{ .cols = 1, .rows = 1 }); + defer zero_grid.deinit(); + try grid.encode(&zero_grid, &zero_writer); + + var zero_reader: std.Io.Reader = .fixed(zero_writer.buffered()); + var decoded_zero = try decodePayload( + &zero_reader, + std.testing.allocator, + ); + defer decoded_zero.deinit(); + try std.testing.expectEqual(@as(usize, 0), decoded_zero.styles.count()); +} + +test "decode accepts unordered sparse hyperlink IDs" { + const header: Header = .{ + .columns = 2, + .rows = 1, + .style_count = 0, + .hyperlink_count = 2, + .style_capacity = 0, + .hyperlink_capacity_bytes = 512, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 6, + }; + + const first: TerminalHyperlink = .{ + .id = .{ .implicit = 1 }, + .uri = "one", + }; + const second: TerminalHyperlink = .{ + .id = .{ .implicit = 2 }, + .uri = "two", + }; + + var encoded: [ + Header.len + + 2 * 14 + + 3 + 2 * 8 + 4 + ]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try header.encode(&writer); + try io.writeInt(&writer, TerminalHyperlinkId, 3); + try hyperlink.encode(first, &writer); + try io.writeInt(&writer, TerminalHyperlinkId, 2); + try hyperlink.encode(second, &writer); + try writer.writeByte(@bitCast(grid.Row{ .cell_width = .eight })); + try io.writeInt(&writer, u16, 2); // cell count + try io.writeInt(&writer, u64, @bitCast(grid.Cell{ + .content = 'A', + .hyperlink = true, + .hyperlink_id = 3, + })); + try io.writeInt(&writer, u64, @bitCast(grid.Cell{ + .content = 'B', + .hyperlink = true, + .hyperlink_id = 2, + })); + try io.writeInt(&writer, u32, 0); // grapheme section + + var reader: std.Io.Reader = .fixed(writer.buffered()); + var decoded = try decodePayload( + &reader, + std.testing.allocator, + ); + defer decoded.deinit(); + try std.testing.expectEqual( + @as(usize, 2), + decoded.hyperlink_set.count(), + ); + const first_id = decoded.lookupHyperlink( + decoded.getRowAndCell(0, 0).cell, + ).?; + const second_id = decoded.lookupHyperlink( + decoded.getRowAndCell(1, 0).cell, + ).?; + try std.testing.expectEqual( + @as(u16, 1), + decoded.hyperlink_set.refCount(decoded.memory, first_id), + ); + try std.testing.expectEqual( + @as(u16, 1), + decoded.hyperlink_set.refCount(decoded.memory, second_id), + ); + try std.testing.expectEqualStrings( + "one", + decoded.hyperlink_set.get( + decoded.memory, + first_id, + ).uri.slice(decoded.memory), + ); + try std.testing.expectEqualStrings( + "two", + decoded.hyperlink_set.get( + decoded.memory, + second_id, + ).uri.slice(decoded.memory), + ); +} + +test "decode defaults missing sparse cell references" { + // An unknown style ID falls back to the default style. + const style_header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 0, + .hyperlink_count = 0, + .style_capacity = 8, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + + var style_encoded: [Header.len + 3 + 8 + 4]u8 = undefined; + var style_writer: std.Io.Writer = .fixed(&style_encoded); + try style_header.encode(&style_writer); + try style_writer.writeByte(0x30); // row flags, full cell width + try io.writeInt(&style_writer, u16, 1); // cell count + try io.writeInt(&style_writer, u64, @bitCast(grid.Cell{ + .style_id = 1, + })); + try io.writeInt(&style_writer, u32, 0); // grapheme section + + var style_reader: std.Io.Reader = .fixed(style_writer.buffered()); + var style_page = try decodePayload( + &style_reader, + std.testing.allocator, + ); + defer style_page.deinit(); + try std.testing.expectEqual( + @as(TerminalStyleId, 0), + style_page.getRowAndCell(0, 0).cell.style_id, + ); + + // An unknown hyperlink ID likewise falls back to no hyperlink. + const hyperlink_header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 0, + .hyperlink_count = 0, + .style_capacity = 0, + .hyperlink_capacity_bytes = 512, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + + var hyperlink_encoded: [Header.len + 3 + 8 + 4]u8 = undefined; + var hyperlink_writer: std.Io.Writer = .fixed(&hyperlink_encoded); + try hyperlink_header.encode(&hyperlink_writer); + try hyperlink_writer.writeByte(0x30); // row flags, full cell width + try io.writeInt(&hyperlink_writer, u16, 1); // cell count + try io.writeInt(&hyperlink_writer, u64, @bitCast(grid.Cell{ + .hyperlink = true, + .hyperlink_id = 1, + })); + try io.writeInt(&hyperlink_writer, u32, 0); // grapheme section + + var hyperlink_reader: std.Io.Reader = .fixed( + hyperlink_writer.buffered(), + ); + var hyperlink_page = try decodePayload( + &hyperlink_reader, + std.testing.allocator, + ); + defer hyperlink_page.deinit(); + const hyperlink_cell = hyperlink_page.getRowAndCell(0, 0).cell; + try std.testing.expect(!hyperlink_cell.hyperlink); + try std.testing.expectEqual( + null, + hyperlink_page.lookupHyperlink(hyperlink_cell), + ); +} + +test "decode normalizes invalid grid semantics" { + const header: Header = .{ + .columns = 3, + .rows = 1, + .style_count = 0, + .hyperlink_count = 0, + .style_capacity = 0, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + + var encoded: [Header.len + 3 + 3 * 8 + 4 + 10]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try header.encode(&writer); + + // Preserve wrap while degrading the unknown semantic-prompt value to none + // and ignoring all reserved row bits. + try writer.writeByte(0xFD); + try io.writeInt(&writer, u16, 3); + + // A text cell replaces an invalid Unicode scalar with U+FFFD while its + // reserved semantic content degrades to output. + try io.writeInt(&writer, u64, @bitCast(grid.Cell{ + .content = 0xD800, + .semantic_content = 3, + })); + + // A declared grapheme suffix cannot fit the advertised zero capacity, + // so decoding preserves the base character and drops the suffix. + try io.writeInt(&writer, u64, @bitCast(grid.Cell{ + .kind = 1, + .content = 'x', + })); + + // Reserved palette content bits do not obscure the valid low palette + // byte. + try io.writeInt(&writer, u64, @bitCast(grid.Cell{ + .kind = 2, + .content = 0xFFFF07, + })); + + // The grapheme section carries the suffix for the second cell. + try io.writeInt(&writer, u32, 1); + try io.writeInt(&writer, u16, 0); + try io.writeInt(&writer, u16, 1); + try io.writeInt(&writer, u16, 1); + try io.writeInt(&writer, u32, 0x0301); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + var page = try decodePayload(&reader, std.testing.allocator); + defer page.deinit(); + try page.verifyIntegrity(std.testing.allocator); + + const first = page.getRowAndCell(0, 0); + try std.testing.expect(first.row.wrap); + try std.testing.expectEqual( + terminal_page.Row.SemanticPrompt.none, + first.row.semantic_prompt, + ); + try std.testing.expectEqual(@as(u21, 0xFFFD), first.cell.codepoint()); + try std.testing.expectEqual( + terminal_page.Cell.Wide.narrow, + first.cell.wide, + ); + try std.testing.expectEqual( + terminal_page.Cell.SemanticContent.output, + first.cell.semantic_content, + ); + try std.testing.expect(!first.cell.protected); + try std.testing.expect(!first.cell.hasGrapheme()); + + const second = page.getRowAndCell(1, 0).cell; + try std.testing.expectEqual(@as(u21, 'x'), second.codepoint()); + try std.testing.expect(!second.hasGrapheme()); + + const third = page.getRowAndCell(2, 0).cell; + try std.testing.expectEqual( + terminal_page.Cell.ContentTag.bg_color_palette, + third.content_tag, + ); + try std.testing.expectEqual( + @as(u8, 7), + third.content.color_palette.data, + ); + try std.testing.expect(!third.hasGrapheme()); +} + +test "decode validates dimensions" { + const cases = [_]Header{ + .{ + .columns = 0, + .rows = 24, + .style_count = 0, + .hyperlink_count = 0, + .style_capacity = 0, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }, + .{ + .columns = 80, + .rows = 0, + .style_count = 0, + .hyperlink_count = 0, + .style_capacity = 0, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }, + }; + + for (cases) |header| { + var encoded: [Header.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try header.encode(&writer); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + try std.testing.expectError( + error.InvalidDimensions, + decodePayload(&reader, std.testing.allocator), + ); + } +} + +test "decode propagates a style table read failure" { + const FailOnceReader = struct { + source: std.Io.Reader, + interface: std.Io.Reader, + bytes_before_failure: usize, + failed: bool = false, + + fn init(bytes: []const u8, bytes_before_failure: usize) @This() { + return .{ + .source = .fixed(bytes), + .interface = .{ + .vtable = &.{ .stream = stream }, + .buffer = &.{}, + .seek = 0, + .end = 0, + }, + .bytes_before_failure = bytes_before_failure, + }; + } + + fn stream( + reader: *std.Io.Reader, + writer: *std.Io.Writer, + limit: std.Io.Limit, + ) std.Io.Reader.StreamError!usize { + const self: *@This() = @fieldParentPtr("interface", reader); + if (self.bytes_before_failure == 0 and !self.failed) { + self.failed = true; + return error.ReadFailed; + } + + const read_limit = if (self.failed) + limit + else + limit.min(.limited(self.bytes_before_failure)); + const n = try self.source.stream(writer, read_limit); + if (!self.failed) self.bytes_before_failure -= n; + return n; + } + }; + + const header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 1, + .hyperlink_count = 0, + .style_capacity = 8, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + + // If the style read error is swallowed, its sixteen zero bytes are then + // misread as a valid empty grid and the unframed payload appears to decode. + var encoded: [Header.len + @sizeOf(TerminalStyleId) + style.len]u8 = + @splat(0); + var writer: std.Io.Writer = .fixed(&encoded); + try header.encode(&writer); + try io.writeInt(&writer, TerminalStyleId, 1); + try writer.splatByteAll(0, style.len); + + var source = FailOnceReader.init( + writer.buffered(), + Header.len + @sizeOf(TerminalStyleId), + ); + var decoded = decodePayload( + &source.interface, + std.testing.allocator, + ) catch |err| { + try std.testing.expectEqual(error.ReadFailed, err); + return; + }; + defer decoded.deinit(); + try std.testing.expect(false); +} + +test "decode normalizes duplicate default and invalid style entries" { + const header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 2, + .hyperlink_count = 0, + .style_capacity = 16, + .hyperlink_capacity_bytes = 512, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 256, + }; + const duplicate_style: TerminalStyle = .{ + .flags = .{ .bold = true }, + }; + + // Generate a valid grid whose cell references a sparse style ID. The PAGE + // table below can then give that ID duplicate, default, or invalid contents + // without hand-authoring the cell wire format. + var grid_page = try TerminalPage.init(.{ + .cols = 1, + .rows = 1, + .styles = 8, + }); + defer grid_page.deinit(); + const released_style_a = try grid_page.styles.add( + grid_page.memory, + .{ .flags = .{ .italic = true } }, + ); + const released_style_b = try grid_page.styles.add( + grid_page.memory, + .{ .bg_color = .{ .palette = 1 } }, + ); + const encoded_style_id = try grid_page.styles.add( + grid_page.memory, + duplicate_style, + ); + grid_page.styles.release(grid_page.memory, released_style_a); + grid_page.styles.release(grid_page.memory, released_style_b); + const grid_cell = grid_page.getRowAndCell(0, 0); + grid_cell.cell.* = .init('A'); + grid_cell.cell.style_id = encoded_style_id; + grid_cell.row.styled = true; + + var grid_bytes: [64]u8 = undefined; + var grid_writer: std.Io.Writer = .fixed(&grid_bytes); + try grid.encode(&grid_page, &grid_writer); + + var encoded: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer encoded.deinit(); + try header.encode(&encoded.writer); + try io.writeInt(&encoded.writer, TerminalStyleId, 1); + try style.encode(duplicate_style, &encoded.writer); + try io.writeInt(&encoded.writer, TerminalStyleId, encoded_style_id); + try style.encode(duplicate_style, &encoded.writer); + try encoded.writer.writeAll(grid_writer.buffered()); + + var reader: std.Io.Reader = .fixed(encoded.written()); + var duplicate_decoded = try decodePayload( + &reader, + std.testing.allocator, + ); + defer duplicate_decoded.deinit(); + try std.testing.expectEqual(@as(usize, 1), duplicate_decoded.styles.count()); + const duplicate_cell = duplicate_decoded.getRowAndCell(0, 0).cell; + try std.testing.expect(duplicate_cell.style_id != 0); + try std.testing.expect( + duplicate_decoded.styles.get( + duplicate_decoded.memory, + duplicate_cell.style_id, + ).flags.bold, + ); + + const default_header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 1, + .hyperlink_count = 0, + .style_capacity = 16, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + var default_encoded: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer default_encoded.deinit(); + try default_header.encode(&default_encoded.writer); + try io.writeInt( + &default_encoded.writer, + TerminalStyleId, + encoded_style_id, + ); + try style.encode(.{}, &default_encoded.writer); + try default_encoded.writer.writeAll(grid_writer.buffered()); + + var default_reader: std.Io.Reader = .fixed(default_encoded.written()); + var default_decoded = try decodePayload( + &default_reader, + std.testing.allocator, + ); + defer default_decoded.deinit(); + try std.testing.expectEqual( + @as(TerminalStyleId, 0), + default_decoded.getRowAndCell(0, 0).cell.style_id, + ); + + // The strict style codec rejects this kind, but PAGE owns the fixed entry + // boundary and can safely map the encoded ID to the default style. A zero + // capacity hint also remains advisory. + var invalid_header = default_header; + invalid_header.style_capacity = 0; + var invalid_encoded: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer invalid_encoded.deinit(); + try invalid_header.encode(&invalid_encoded.writer); + try io.writeInt( + &invalid_encoded.writer, + TerminalStyleId, + encoded_style_id, + ); + try invalid_encoded.writer.writeByte(3); + try invalid_encoded.writer.splatByteAll(0, style.len - 1); + try invalid_encoded.writer.writeAll(grid_writer.buffered()); + + var invalid_reader: std.Io.Reader = .fixed(invalid_encoded.written()); + var invalid_decoded = try decodePayload( + &invalid_reader, + std.testing.allocator, + ); + defer invalid_decoded.deinit(); + try std.testing.expectEqual( + @as(TerminalStyleId, 0), + invalid_decoded.getRowAndCell(0, 0).cell.style_id, + ); +} + +test "decode releases unused style table references" { + const header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 1, + .hyperlink_count = 0, + .style_capacity = 8, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + + var empty_page = try TerminalPage.init(.{ .cols = 1, .rows = 1 }); + defer empty_page.deinit(); + var grid_bytes: [32]u8 = undefined; + var grid_writer: std.Io.Writer = .fixed(&grid_bytes); + try grid.encode(&empty_page, &grid_writer); + + var encoded: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer encoded.deinit(); + try header.encode(&encoded.writer); + try io.writeInt(&encoded.writer, TerminalStyleId, 7); + try style.encode(.{ .flags = .{ .bold = true } }, &encoded.writer); + try encoded.writer.writeAll(grid_writer.buffered()); + + var reader: std.Io.Reader = .fixed(encoded.written()); + var decoded = try decodePayload(&reader, std.testing.allocator); + defer decoded.deinit(); + try std.testing.expectEqual(@as(usize, 0), decoded.styles.count()); + + // The dead table-only entry is absent from the very first re-encode. + var canonical: [64]u8 = undefined; + var canonical_writer: std.Io.Writer = .fixed(&canonical); + try encodePayload(&decoded, &canonical_writer); + var canonical_reader: std.Io.Reader = .fixed(canonical_writer.buffered()); + const canonical_header = try Header.decode(&canonical_reader); + try std.testing.expectEqual(@as(u16, 0), canonical_header.style_count); +} + +test "decode reuses duplicate hyperlinks" { + const header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 0, + .hyperlink_count = 2, + .style_capacity = 0, + .hyperlink_capacity_bytes = 512, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 16, + }; + const duplicate: TerminalHyperlink = .{ + .id = .{ .explicit = "id" }, + .uri = "uri", + }; + + // As with styles above, use a sparse native ID to make grid.encode produce + // the cell reference whose table value this test deliberately duplicates. + var grid_page = try TerminalPage.init(.{ + .cols = 1, + .rows = 1, + .hyperlink_bytes = 512, + .string_bytes = 64, + }); + defer grid_page.deinit(); + const released_hyperlink_a = try grid_page.insertHyperlink(.{ + .id = .{ .implicit = 1 }, + .uri = "one", + }); + const released_hyperlink_b = try grid_page.insertHyperlink(.{ + .id = .{ .implicit = 2 }, + .uri = "two", + }); + const encoded_hyperlink_id = try grid_page.insertHyperlink(.{ + .id = .{ .implicit = 3 }, + .uri = "three", + }); + grid_page.hyperlink_set.release( + grid_page.memory, + released_hyperlink_a, + ); + grid_page.hyperlink_set.release( + grid_page.memory, + released_hyperlink_b, + ); + const grid_cell = grid_page.getRowAndCell(0, 0); + grid_cell.cell.* = .init('A'); + try grid_page.setHyperlink( + grid_cell.row, + grid_cell.cell, + encoded_hyperlink_id, + ); + + var grid_bytes: [64]u8 = undefined; + var grid_writer: std.Io.Writer = .fixed(&grid_bytes); + try grid.encode(&grid_page, &grid_writer); + + var encoded: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer encoded.deinit(); + try header.encode(&encoded.writer); + try io.writeInt(&encoded.writer, TerminalHyperlinkId, 1); + try hyperlink.encode(duplicate, &encoded.writer); + try io.writeInt( + &encoded.writer, + TerminalHyperlinkId, + encoded_hyperlink_id, + ); + try hyperlink.encode(duplicate, &encoded.writer); + try encoded.writer.writeAll(grid_writer.buffered()); + + var reader: std.Io.Reader = .fixed(encoded.written()); + var decoded = try decodePayload( + &reader, + std.testing.allocator, + ); + defer decoded.deinit(); + try std.testing.expectEqual(@as(usize, 1), decoded.hyperlink_set.count()); + const cell = decoded.getRowAndCell(0, 0).cell; + try std.testing.expect(cell.hyperlink); + const id = decoded.lookupHyperlink(cell).?; + const entry = decoded.hyperlink_set.get(decoded.memory, id); + try std.testing.expectEqual( + @as(u16, 1), + decoded.hyperlink_set.refCount(decoded.memory, id), + ); + try std.testing.expectEqualStrings( + "uri", + entry.uri.slice(decoded.memory), + ); + + // Overwriting the sole linked cell must make the deduplicated entry dead. + // Any surviving reference belongs to the decode-time wire table, not the + // native page. + decoded.clearCells(decoded.getRow(0), 0, 1); + try std.testing.expectEqual(@as(usize, 0), decoded.hyperlink_set.count()); +} + +test "discard consumes exactly one PAGE record" { + const testing = std.testing; + + // Discard validates framing only, so an arbitrary payload keeps this test + // focused on record consumption rather than page structure. + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + var stream: record.Writer = .init(testing.allocator, &encoded.writer); + defer stream.deinit(); + { + const payload = stream.begin(.page); + errdefer stream.cancel(); + try payload.writeAll("undecodable page payload"); + try stream.finish(); + } + const record_len = encoded.written().len; + try encoded.writer.writeAll("next"); + + var source: std.Io.Reader = .fixed(encoded.written()); + try discard(&source); + try testing.expectEqualStrings("next", try source.take(4)); + + // Only PAGE records may be discarded; the tag remains strict. + var wrong_tag: std.Io.Writer.Allocating = .init(testing.allocator); + defer wrong_tag.deinit(); + var wrong_tag_stream: record.Writer = .init( + testing.allocator, + &wrong_tag.writer, + ); + defer wrong_tag_stream.deinit(); + { + const payload = wrong_tag_stream.begin(.history); + errdefer wrong_tag_stream.cancel(); + try payload.writeAll("payload"); + try wrong_tag_stream.finish(); + } + var wrong_tag_source: std.Io.Reader = .fixed(wrong_tag.written()); + try testing.expectError( + error.UnexpectedRecordTag, + discard(&wrong_tag_source), + ); + + // Discarded bytes are still covered by the record CRC. + const corrupted = try testing.allocator.dupe( + u8, + encoded.written()[0..record_len], + ); + defer testing.allocator.free(corrupted); + corrupted[corrupted.len - 1] ^= 1; + var corrupted_source: std.Io.Reader = .fixed(corrupted); + try testing.expectError(error.InvalidChecksum, discard(&corrupted_source)); + + // A truncated payload is detected before `finish`. + var truncated_source: std.Io.Reader = .fixed( + encoded.written()[0 .. record_len - 1], + ); + try testing.expectError(error.EndOfStream, discard(&truncated_source)); +} + +test "decode ignores empty hyperlink strings" { + const header: Header = .{ + .columns = 1, + .rows = 1, + .style_count = 0, + .hyperlink_count = 1, + .style_capacity = 0, + .hyperlink_capacity_bytes = 0, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 16, + }; + const fixtures = [_][]const u8{ + // Empty implicit URI. + "\x01\x01\x00\x00\x00\x00\x00\x00\x00", + // Empty explicit ID. + "\x02\x00\x00\x00\x00\x03\x00\x00\x00uri", + // Empty explicit URI. + "\x02\x02\x00\x00\x00id\x00\x00\x00\x00", + // Valid contents that cannot fit the zero-capacity native set. + "\x01\x01\x00\x00\x00\x03\x00\x00\x00uri", + }; + + // Only the invalid hyperlink entry itself is hand-authored above. Generate + // its cell reference through the native grid encoder. + var grid_page = try TerminalPage.init(.{ + .cols = 1, + .rows = 1, + .hyperlink_bytes = 256, + .string_bytes = 16, + }); + defer grid_page.deinit(); + const encoded_hyperlink_id = try grid_page.insertHyperlink(.{ + .id = .{ .implicit = 1 }, + .uri = "uri", + }); + const grid_cell = grid_page.getRowAndCell(0, 0); + grid_cell.cell.* = .init('A'); + try grid_page.setHyperlink( + grid_cell.row, + grid_cell.cell, + encoded_hyperlink_id, + ); + var grid_bytes: [64]u8 = undefined; + var grid_writer: std.Io.Writer = .fixed(&grid_bytes); + try grid.encode(&grid_page, &grid_writer); + + for (fixtures) |fixture| { + var encoded: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer encoded.deinit(); + try header.encode(&encoded.writer); + try io.writeInt( + &encoded.writer, + TerminalHyperlinkId, + encoded_hyperlink_id, + ); + try encoded.writer.writeAll(fixture); + + // The cell refers to the ignored table entry. Its absent native + // remapping must degrade to no hyperlink while preserving the cell. + try encoded.writer.writeAll(grid_writer.buffered()); + + var reader: std.Io.Reader = .fixed(encoded.written()); + var decoded = try decodePayload( + &reader, + std.testing.allocator, + ); + defer decoded.deinit(); + + try std.testing.expectEqual( + @as(usize, 0), + decoded.hyperlink_set.count(), + ); + const cell = decoded.getRowAndCell(0, 0).cell; + try std.testing.expectEqual(@as(u21, 'A'), cell.codepoint()); + try std.testing.expect(!cell.hyperlink); + try std.testing.expectEqual(null, decoded.lookupHyperlink(cell)); + } +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/record.zig b/vendor/libghostty-vt/src/terminal/snapshot/record.zig new file mode 100644 index 00000000..397d00cb --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/record.zig @@ -0,0 +1,601 @@ +//! Snapshot record framing. +//! +//! Records begin immediately after the single snapshot envelope and continue +//! back-to-back until FINISH. Each record has one fixed-size header followed +//! by exactly `payload_len` bytes. The payload length limits the record reader +//! so malformed payloads cannot consume bytes from the following record. +//! +//! The CRC covers the first six header bytes (tag and payload length) +//! followed by the payload. The CRC field itself is not included for obvious +//! reasons. All integers are unsigned and little-endian. +//! +//! | Offset | Size | Field | +//! | -----: | ------------: | :--------------------- | +//! | 0 | 2 | Tag (`u16`) | +//! | 2 | 4 | Payload length (`u32`) | +//! | 6 | 4 | CRC32C (`u32`) | +//! | 10 | `payload_len` | Payload | +//! +//! Supported tags are in `Tag`. + +const std = @import("std"); +const assert = std.debug.assert; +const Allocator = std.mem.Allocator; +const test_fixture = @import("fixture.zig"); +const io = @import("io.zig"); + +/// CRC32C as specified by the snapshot format. This is the parameter set +/// Zig's standard library names after its iSCSI use, backed by dedicated +/// CRC32C instructions where the target has them. +pub const Crc32c = @import("../../crc32c.zig").Crc32c; + +/// Identifies the layout and meaning of a record payload. +/// The current snapshot version rejects every value not listed here. +pub const Tag = enum(u16) { + /// Terminal-wide live state and configuration. + terminal = 1, + + /// One live screen and its page sequence. + screen = 2, + + /// One complete logical terminal page. + page = 3, + + /// One screen's complete history manifest and page sequence. + history = 4, + + /// Marker separating the renderable terminal state from history. + ready = 5, + + /// Marker terminating the complete snapshot blob. + finish = 6, + + /// Canonical unfinished standard TerminalStream input. + continuation = 7, +}; + +/// The fixed framing that precedes every record payload. +pub const Header = struct { + /// Number of bytes written by `encode`, calculated using the encoder itself + /// so this remains synchronized with the field-by-field wire format. + pub const len = computeLen(); + + comptime { + // This size is part of the wire format. If it changes, the snapshot + // version and golden fixtures must also change. + assert(len == 10); + } + + /// Determines how the payload is decoded. + tag: Tag, + + /// Number of payload bytes immediately following this header. + payload_len: u32, + + /// CRC32C over the encoded tag, payload length, and payload. + crc32c: u32, + + /// Errors possible while decoding a record header. + pub const DecodeError = std.Io.Reader.Error || error{InvalidTag}; + + /// Encode the fixed record header. + pub fn encode( + self: Header, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + try encodeChecksumPrefix(self.tag, self.payload_len, writer); + try io.writeInt(writer, u32, self.crc32c); + } + + /// Decode a fixed record header and reject unknown tags. + /// Payload length and CRC validation occur while decoding the payload. + pub fn decode(reader: *std.Io.Reader) DecodeError!Header { + const tag_raw = try io.readInt(reader, u16); + const tag = std.enums.fromInt(Tag, tag_raw) orelse { + return error.InvalidTag; + }; + + return .{ + .tag = tag, + .payload_len = try io.readInt(reader, u32), + .crc32c = try io.readInt(reader, u32), + }; + } + + /// Computes the required header length at comptime. + fn computeLen() usize { + comptime { + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + const header: Header = .{ + .tag = .terminal, + .payload_len = 0, + .crc32c = 0, + }; + header.encode(&writer) catch unreachable; + return writer.end; + } + } +}; + +/// A writer that calculates a record checksum without retaining payload bytes. +/// The checksum is initialized with the encoded tag and payload length. +pub const Checksum = struct { + hashing: std.Io.Writer.Hashing(Crc32c), + + /// Begin a checksum with the encoded tag and payload length already added. + pub fn init(tag: Tag, payload_len: u32) Checksum { + var result: Checksum = .{ + .hashing = .initHasher(.init(), &.{}), + }; + encodeChecksumPrefix( + tag, + payload_len, + &result.hashing.writer, + ) catch unreachable; + return result; + } + + /// Return the writer through which the complete payload must be encoded. + pub fn writer(self: *Checksum) *std.Io.Writer { + return &self.hashing.writer; + } + + /// Finish and return the checksum stored in the record header. + pub fn final(self: *Checksum) u32 { + self.hashing.writer.flush() catch unreachable; + return self.hashing.hasher.final(); + } +}; + +/// Streams complete records while retaining only one payload at a time. +/// +/// The scratch allocation is retained between records so a stream's peak +/// memory is the largest record payload rather than the complete snapshot. +pub const Writer = struct { + destination: *std.Io.Writer, + scratch: std.Io.Writer.Allocating, + active_tag: ?Tag, + + pub fn init( + alloc: Allocator, + destination: *std.Io.Writer, + ) Writer { + return .{ + .destination = destination, + .scratch = .init(alloc), + .active_tag = null, + }; + } + + pub fn deinit(self: *Writer) void { + assert(self.active_tag == null); + assert(self.scratch.writer.end == 0); + self.scratch.deinit(); + self.* = undefined; + } + + /// Return the destination writer for unframed snapshot bytes. + pub fn writer(self: *Writer) *std.Io.Writer { + assert(self.active_tag == null); + return self.destination; + } + + /// Begin one record and return its reusable payload writer. + pub fn begin(self: *Writer, tag: Tag) *std.Io.Writer { + assert(self.active_tag == null); + assert(self.scratch.writer.end == 0); + self.active_tag = tag; + return &self.scratch.writer; + } + + pub const FinishError = std.Io.Writer.Error || error{ + /// The payload cannot be represented by the record's `u32` length. + PayloadTooLarge, + }; + + /// Finish and emit the active record's header followed by its payload. + /// + /// Payload validation failures occur before this function and emit no part + /// of the record. A destination failure here may have emitted a prefix. + pub fn finish(self: *Writer) FinishError!void { + assert(self.active_tag != null); + const tag = self.active_tag.?; + defer self.cancel(); + + const payload = self.scratch.written(); + const payload_len = std.math.cast( + u32, + payload.len, + ) orelse return error.PayloadTooLarge; + + // The CRC prefix contains the payload length, so checksum the retained + // payload only after its final length is known. + var checksum: Checksum = .init(tag, payload_len); + checksum.writer().writeAll(payload) catch unreachable; + + const header: Header = .{ + .tag = tag, + .payload_len = payload_len, + .crc32c = checksum.final(), + }; + var header_bytes: [Header.len]u8 = undefined; + var header_writer: std.Io.Writer = .fixed(&header_bytes); + header.encode(&header_writer) catch unreachable; + + try self.destination.writeAll(&header_bytes); + try self.destination.writeAll(payload); + } + + /// Discard the active record without emitting any bytes. + /// + /// This is idempotent so an outer error cleanup may call it after `finish` + /// has already cleared state following a destination failure. + pub fn cancel(self: *Writer) void { + self.scratch.shrinkRetainingCapacity(0); + self.active_tag = null; + } +}; + +/// Reads one complete record. +/// +/// `init` decodes the header, `payloadReader` returns a reader limited to the +/// declared payload, and `finish` verifies exact exhaustion and the CRC32C. +pub const Reader = struct { + header: Header, + + // The limited reader normally streams directly into the checksum reader's + // buffer. One byte is enough for operations that require it to buffer, + // such as peek and discard. + limited_buffer: [1]u8, + + // Fixed-header decoding performs several small reads. 256 bytes batches + // them while CRC32C is calculated without making Reader large on the + // stack. Bulk payload reads bypass this buffer entirely. + hashing_buffer: [256]u8, + + limited: std.Io.Reader.Limited, + hashing: std.Io.Reader.Hashed(Crc32c), + + /// When the source already has the complete payload buffered, for + /// example an in-memory snapshot, the payload is borrowed straight + /// from the source buffer instead of streaming through the limited + /// and hashing adapters. The checksum is then verified with one bulk + /// update in `finish`, and the source is not advanced until `finish`. + borrowed: ?Borrowed, + + const Borrowed = struct { + source: *std.Io.Reader, + payload: std.Io.Reader, + }; + + pub const InitError = Header.DecodeError; + + /// Errors detected after a payload decoder returns. + pub const FinishError = error{ + /// The payload bytes do not match the CRC in the record header. + InvalidChecksum, + + /// The decoder did not consume exactly `payload_len` bytes. + PayloadNotExhausted, + }; + + /// Decode a record header and initialize its payload reader. + /// + /// `self` must remain at a stable address until `finish` returns. Decode + /// the payload only through the reader returned by `payloadReader`. + pub fn init( + self: *Reader, + source: *std.Io.Reader, + ) InitError!void { + self.* = undefined; + self.header = try Header.decode(source); + + // The complete payload is already sitting in the source buffer: + // borrow it in place. Payload decoders read from a fixed reader + // over the borrowed bytes and `finish` checksums them in one pass. + if (source.bufferedLen() >= self.header.payload_len) { + self.borrowed = .{ + .source = source, + .payload = .fixed( + source.buffered()[0..self.header.payload_len], + ), + }; + return; + } + + self.borrowed = null; + self.limited = .init( + source, + .limited(self.header.payload_len), + &self.limited_buffer, + ); + + const checksum: Checksum = .init( + self.header.tag, + self.header.payload_len, + ); + self.hashing = .init( + &self.limited.interface, + checksum.hashing.hasher, + &self.hashing_buffer, + ); + } + + /// Return the length-limited, checksum-updating payload reader. + pub fn payloadReader(self: *Reader) *std.Io.Reader { + if (self.borrowed) |*borrowed| return &borrowed.payload; + return &self.hashing.reader; + } + + /// Require exact payload exhaustion and validate its CRC32C. + pub fn finish(self: *Reader) FinishError!void { + if (self.borrowed) |*borrowed| { + if (borrowed.payload.bufferedLen() != 0) { + return error.PayloadNotExhausted; + } + + var checksum: Checksum = .init( + self.header.tag, + self.header.payload_len, + ); + checksum.writer().writeAll( + borrowed.payload.buffer[0..borrowed.payload.end], + ) catch unreachable; + if (checksum.final() != self.header.crc32c) { + return error.InvalidChecksum; + } + + // The borrowed bytes validated, so consume them from the + // source only now, leaving it positioned at the next record. + borrowed.source.toss(self.header.payload_len); + return; + } + + if (self.hashing.reader.bufferedLen() != 0 or + self.limited.remaining != .nothing) + { + return error.PayloadNotExhausted; + } + + if (self.hashing.hasher.final() != self.header.crc32c) { + return error.InvalidChecksum; + } + } +}; + +/// Encode the portion of a record header covered by CRC32C. +fn encodeChecksumPrefix( + tag: Tag, + payload_len: u32, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + try io.writeInt(writer, u16, @intFromEnum(tag)); + try io.writeInt(writer, u32, payload_len); +} + +const test_page_header_fixture = test_fixture.parse( + @embedFile("testdata/record-page-header-v1.hex"), +); + +test "golden PAGE record header and checksum" { + const page_header = + "\x50\x00\x18\x00\x00\x00\x00\x00" ++ + "\x00\x00\x00\x00\x00\x00\x00\x00" ++ + "\x00\x00\x00\x00\x00\x00\x00\x00"; + + var checksum: Checksum = .init(.page, page_header.len); + try checksum.writer().writeAll(page_header); + try std.testing.expectEqual(@as(u32, 0x7178441b), checksum.final()); + + const header: Header = .{ + .tag = .page, + .payload_len = page_header.len, + .crc32c = 0x7178441b, + }; + var buf: [Header.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try header.encode(&writer); + + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/record-page-header-v1.hex", + "snapshot_fixture-record-page-header-v1.hex", + &test_page_header_fixture, + writer.buffered(), + ); + + var reader: std.Io.Reader = .fixed(&test_page_header_fixture); + try std.testing.expectEqual(header, try Header.decode(&reader)); +} + +test "reject invalid tags" { + for ([_]u16{ 0, 8, std.math.maxInt(u16) }) |tag| { + var fixture = [_]u8{0} ** Header.len; + std.mem.writeInt(u16, fixture[0..2], tag, .little); + var reader: std.Io.Reader = .fixed(&fixture); + try std.testing.expectError(error.InvalidTag, Header.decode(&reader)); + } +} + +test "reject every header truncation" { + for (0..Header.len) |len| { + var reader: std.Io.Reader = .fixed( + test_page_header_fixture[0..len], + ); + try std.testing.expectError(error.EndOfStream, Header.decode(&reader)); + } +} + +test "record reader verifies exhaustion and checksum" { + const payload = "snapshot payload"; + var checksum: Checksum = .init(.page, payload.len); + try checksum.writer().writeAll(payload); + const header: Header = .{ + .tag = .page, + .payload_len = payload.len, + .crc32c = checksum.final(), + }; + + var encoded: [Header.len + payload.len + 4]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try header.encode(&writer); + try writer.writeAll(payload ++ "next"); + + var source: std.Io.Reader = .fixed(writer.buffered()); + var record_reader: Reader = undefined; + try record_reader.init(&source); + var decoded: [payload.len]u8 = undefined; + try record_reader.payloadReader().readSliceAll(&decoded); + try record_reader.finish(); + try std.testing.expectEqualStrings(payload, &decoded); + try std.testing.expectEqualStrings("next", try source.take(4)); +} + +test "record reader rejects remaining bytes and invalid checksum" { + const payload = "payload"; + const header: Header = .{ + .tag = .page, + .payload_len = payload.len, + .crc32c = 0, + }; + + var encoded: [Header.len + payload.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try header.encode(&writer); + try writer.writeAll(payload); + + { + var source: std.Io.Reader = .fixed(writer.buffered()); + var record_reader: Reader = undefined; + try record_reader.init(&source); + _ = try record_reader.payloadReader().takeByte(); + try std.testing.expectError( + error.PayloadNotExhausted, + record_reader.finish(), + ); + } + + { + var source: std.Io.Reader = .fixed(writer.buffered()); + var record_reader: Reader = undefined; + try record_reader.init(&source); + try record_reader.payloadReader().discardAll(payload.len); + try std.testing.expectError( + error.InvalidChecksum, + record_reader.finish(), + ); + } +} + +test "payload limit does not consume the next record" { + const payload = "ab"; + var checksum: Checksum = .init(.page, payload.len); + try checksum.writer().writeAll(payload); + const header: Header = .{ + .tag = .page, + .payload_len = payload.len, + .crc32c = checksum.final(), + }; + + var encoded: [Header.len + payload.len + 4]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try header.encode(&writer); + try writer.writeAll(payload ++ "next"); + + var source: std.Io.Reader = .fixed(writer.buffered()); + var record_reader: Reader = undefined; + try record_reader.init(&source); + var too_long: [3]u8 = undefined; + try std.testing.expectError( + error.EndOfStream, + record_reader.payloadReader().readSliceAll(&too_long), + ); + try record_reader.finish(); + try std.testing.expectEqualStrings("next", try source.take(4)); +} + +test "record writer buffers a payload and appends framing" { + var destination: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer destination.deinit(); + try destination.writer.writeAll("prefix"); + var stream: Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + + const payload = stream.begin(.page); + errdefer stream.cancel(); + try payload.writeAll("payload"); + try stream.finish(); + + const encoded = destination.written(); + try std.testing.expectEqualStrings("prefix", encoded[0..6]); + + var source: std.Io.Reader = .fixed(encoded[6..]); + var record_reader: Reader = undefined; + try record_reader.init(&source); + try std.testing.expectEqual(Tag.page, record_reader.header.tag); + try std.testing.expectEqual( + @as(u32, 7), + record_reader.header.payload_len, + ); + try std.testing.expectEqualStrings( + "payload", + try record_reader.payloadReader().take(7), + ); + try record_reader.finish(); +} + +test "record writer cancel preserves preceding bytes" { + var destination: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer destination.deinit(); + try destination.writer.writeAll("prefix"); + var stream: Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + + const partial = stream.begin(.page); + errdefer stream.cancel(); + try partial.writeAll("partial"); + const scratch_capacity = stream.scratch.writer.buffer.len; + stream.cancel(); + + try std.testing.expectEqualStrings("prefix", destination.written()); + try std.testing.expectEqual( + scratch_capacity, + stream.scratch.writer.buffer.len, + ); + + // Cancel retains the allocation but leaves it ready for the next record. + const next = stream.begin(.page); + try next.writeAll("next"); + try stream.finish(); + + var source: std.Io.Reader = .fixed(destination.written()[6..]); + var record_reader: Reader = undefined; + try record_reader.init(&source); + try std.testing.expectEqualStrings( + "next", + try record_reader.payloadReader().take(4), + ); + try record_reader.finish(); +} + +test "record writer clears active state after destination failure" { + // The header fits but the payload does not, leaving a permitted partial + // record in the destination while the reusable writer becomes idle again. + var destination_bytes: [Header.len]u8 = undefined; + var destination: std.Io.Writer = .fixed(&destination_bytes); + var stream: Writer = .init(std.testing.allocator, &destination); + defer stream.deinit(); + + const payload = stream.begin(.page); + try payload.writeByte(0); + try std.testing.expectError(error.WriteFailed, stream.finish()); + try std.testing.expectEqual(@as(?Tag, null), stream.active_tag); + try std.testing.expectEqual(@as(usize, 0), stream.scratch.writer.end); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/screen.zig b/vendor/libghostty-vt/src/terminal/snapshot/screen.zig new file mode 100644 index 00000000..d4ef16f7 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/screen.zig @@ -0,0 +1,2460 @@ +//! SCREEN record payload encoding. +//! +//! One SCREEN record represents the live state for one terminal screen. The +//! record is followed immediately by the number of complete PAGE records +//! declared by `page_count`. They are the minimal suffix of native pages needed +//! to cover the active area and are ordered from oldest to newest. A decoder +//! uses the declared count, rather than another record tag, to find the end of +//! the screen's page sequence. +//! +//! The final screen-height rows are the active area. When its boundary falls +//! inside the first encoded page, that page also contains an incidental history +//! prefix. `history_rows` declares the complete logical history extent so a +//! client can size its scrollbar at READY without waiting for HISTORY. A client +//! may expose or ignore the resident overlap carried by SCREEN. The extent is +//! advisory; native row totals remain derived from decoded PAGE records. +//! +//! Screen dimensions are terminal-wide state and are not repeated here. Page +//! capacities, native page IDs and pointers, history availability, selection, +//! viewport, dirty state, and derived semantic-prompt state are also omitted. +//! +//! All integers are unsigned and little-endian. +//! +//! ## Binary Format +//! +//! A SCREEN record is followed immediately by its declared PAGE records: +//! +//! ```text +//! +----------------------+ +//! | SCREEN record | +//! +----------------------+ +//! | PAGE record 0 | +//! +----------------------+ +//! | ... | +//! +----------------------+ +//! | PAGE record (n - 1) | +//! +----------------------+ +//! +//! n = page_count +//! ``` +//! +//! The HISTORY record for this screen declares and sends the older complete +//! pages after the terminal becomes ready. The incidental prefix is already +//! present in SCREEN and is not sent again. +//! +//! The SCREEN payload begins with a fixed header. When the header says there is +//! no saved cursor, the payload is: +//! +//! ```text +//! 0 +----------------------+ +//! | Header | +//! 53 +----------------------+ +//! | Cursor hyperlink | +//! | variable | +//! end +----------------------+ +//! ``` +//! +//! When a saved cursor is present, it is inserted before the cursor hyperlink: +//! +//! ```text +//! 0 +----------------------+ +//! | Header | +//! 53 +----------------------+ +//! | Saved cursor | +//! 76 +----------------------+ +//! | Cursor hyperlink | +//! | variable | +//! end +----------------------+ +//! ``` +//! +//! The cursor hyperlink begins with a one-byte kind. Zero means no hyperlink +//! and has no following bytes. Kinds one and two use the implicit and explicit +//! hyperlink encodings documented in `hyperlink.zig`. +//! +//! ### Header +//! +//! ```text +//! 0 +----------------------------------+ +//! | Screen key (u16) | +//! 2 +----------------------------------+ +//! | Page count (u16) | +//! 4 +----------------------------------+ +//! | Logical history rows (u64) | +//! 12 +----------------------------------+ +//! | Cursor x (u16) | +//! 14 +----------------------------------+ +//! | Cursor y (u16) | +//! 16 +----------------------------------+ +//! | Cursor visual style (u8) | +//! 17 +----------------------------------+ +//! | Cursor flags (u8) | +//! 18 +----------------------------------+ +//! | Concrete cursor pen (Style) | +//! 34 +----------------------------------+ +//! | Hyperlink implicit counter (u32) | +//! 38 +----------------------------------+ +//! | Current charset (CharsetState) | +//! 40 +----------------------------------+ +//! | Protected mode (u8) | +//! 41 +----------------------------------+ +//! | Kitty keyboard stack index (u8) | +//! 42 +----------------------------------+ +//! | Kitty keyboard stack flags | +//! | 8 * u8 | +//! 50 +----------------------------------+ +//! | Semantic-click kind (u8) | +//! 51 +----------------------------------+ +//! | Semantic-click value (u8) | +//! 52 +----------------------------------+ +//! | Saved-cursor-present (u8) | +//! 53 +----------------------------------+ +//! ``` +//! +//! The concrete cursor pen uses the fixed style encoding from `style.zig`. +//! +//! ### Cursor flags +//! +//! ```text +//! bit 0 +--------------------------------+ +//! | Pending wrap | +//! bit 1 +--------------------------------+ +//! | Protected | +//! bit 2 +--------------------------------+ +//! | Semantic content | +//! | 2 bits | +//! bit 4 +--------------------------------+ +//! | Semantic-content-clear-EOL | +//! bit 5 +--------------------------------+ +//! | Reserved, zero | +//! | 3 bits | +//! bit 8 +--------------------------------+ +//! ``` +//! +//! ### Charset state +//! +//! The current and saved cursor charset states share this packed layout: +//! +//! ```text +//! bit 0 +-------------------------------+ +//! | G0 charset | +//! | 2 bits | +//! bit 2 +-------------------------------+ +//! | G1 charset | +//! | 2 bits | +//! bit 4 +-------------------------------+ +//! | G2 charset | +//! | 2 bits | +//! bit 6 +-------------------------------+ +//! | G3 charset | +//! | 2 bits | +//! bit 8 +-------------------------------+ +//! | GL slot | +//! | 2 bits | +//! bit 10 +-------------------------------+ +//! | GR slot | +//! | 2 bits | +//! bit 12 +-------------------------------+ +//! | Single shift | +//! | 3 bits | +//! bit 15 +-------------------------------+ +//! | Reserved, zero | +//! bit 16 +-------------------------------+ +//! ``` +//! +//! The Kitty keyboard stack contains its current index followed by all eight +//! flag entries, including disabled entries. +//! +//! ### Saved cursor +//! +//! ```text +//! 0 +-----------------------------+ +//! | Saved cursor x (u16) | +//! 2 +-----------------------------+ +//! | Saved cursor y (u16) | +//! 4 +-----------------------------+ +//! | Saved cursor pen (Style) | +//! 20 +-----------------------------+ +//! | Saved cursor flags (u8) | +//! 21 +-----------------------------+ +//! | Saved charset state | +//! 23 +-----------------------------+ +//! ``` +//! +//! ### Saved cursor flags +//! +//! ```text +//! bit 0 +---------------------------+ +//! | Protected | +//! bit 1 +---------------------------+ +//! | Pending wrap | +//! bit 2 +---------------------------+ +//! | Origin | +//! bit 3 +---------------------------+ +//! | Reserved, zero | +//! | 5 bits | +//! bit 8 +---------------------------+ +//! ``` +//! +//! Native enum values used below are stable terminal registries. Their wire +//! widths are fixed independently by this payload. Changing those values or +//! any packed bit assignment requires a snapshot version bump. + +const std = @import("std"); +const build_options = @import("terminal_options"); +const Allocator = std.mem.Allocator; +const hyperlink = @import("hyperlink.zig"); +const test_fixture = @import("fixture.zig"); +const io = @import("io.zig"); +const grid = @import("grid.zig"); +const page = @import("page.zig"); +const record = @import("record.zig"); +const style = @import("style.zig"); +const terminal_ansi = @import("../ansi.zig"); +const terminal_charsets = @import("../charsets.zig"); +const terminal_hyperlink = @import("../hyperlink.zig"); +const terminal_kitty = @import("../kitty.zig"); +const terminal_osc = @import("../osc.zig"); +const terminal_page = @import("../page.zig"); +const TerminalPageList = @import("../PageList.zig"); +const TerminalScreen = @import("../Screen.zig"); +const TerminalScreenKey = @import("../ScreenSet.zig").Key; +const terminal_style = @import("../style.zig"); + +const TerminalHyperlink = terminal_hyperlink.Hyperlink; +const TerminalStyle = terminal_style.Style; + +/// Errors possible while encoding fixed SCREEN payload fields. +const PayloadEncodeError = hyperlink.EncodeError || error{ + InvalidCursorFlags, + InvalidCharsetState, + InvalidKittyKeyboardIndex, + InvalidKittyKeyboardFlags, + InvalidSavedCursorFlags, +}; + +/// Errors possible while encoding a SCREEN and its complete PAGE sequence. +pub const EncodeError = Allocator.Error || PayloadEncodeError || page.EncodeError || error{ + /// The active area spans more pages than the SCREEN header can declare. + PageCountOverflow, +}; + +/// Encode one SCREEN and its minimal suffix of complete native pages. +/// +/// The suffix begins with the page containing the active area's first row and +/// ends with the newest page. Completed records may already be emitted if a +/// later record fails; the missing READY marker makes that prefix invalid. +pub fn encode( + screen: *const TerminalScreen, + key: TerminalScreenKey, + destination: *record.Writer, +) EncodeError!void { + // The active top may fall inside this page, leaving an incidental history + // prefix. Every earlier complete page is history and is omitted. + const first = screen.pages.getTopLeft(.active).node; + var page_count: usize = 0; + var node: ?@TypeOf(first) = first; + while (node) |current| : (node = current.next) page_count += 1; + const encoded_page_count = std.math.cast( + u16, + page_count, + ) orelse return error.PageCountOverflow; + + // SCREEN declares exactly how many immediately following PAGE records + // belong to it. + { + const payload = destination.begin(.screen); + errdefer destination.cancel(); + try encodePayload( + screen, + key, + encoded_page_count, + payload, + ); + try destination.finish(); + } + + // Active pages are resident today, but use the representation-safe access + // path so a future PageList compression-policy change cannot turn this + // wire encoder's optimization invariant into undefined behavior. + node = first; + while (node) |current| : (node = current.next) { + var preserved = try current.pagePreservingState(screen.alloc); + defer preserved.deinit(); + try page.encode(preserved.page(), destination); + } +} + +/// Errors possible while restoring a SCREEN and its declared PAGE sequence. +pub const DecodeError = PayloadDecodeError || + page.DecodeError || + record.Reader.InitError || + record.Reader.FinishError || + TerminalPageList.Builder.FinishError || + TerminalPageList.IncreaseCapacityError || + error{ + /// The next record is valid but is not a SCREEN. + UnexpectedRecordTag, + + /// A SCREEN must declare at least one PAGE. + InvalidPageCount, + }; + +/// One decoded SCREEN sequence and the native screen identified by its header. +pub const Decoded = struct { + key: TerminalScreenKey, + /// Expected history extent at READY, including resident SCREEN overlap. + history_rows: u64, + screen: TerminalScreen, + + /// Release the decoded native screen before ownership is transferred. + pub fn deinit(self: *Decoded) void { + self.screen.deinit(); + self.* = undefined; + } +}; + +/// Restore one SCREEN and its declared PAGE records into native terminal state. +/// +/// The caller supplies terminal-wide dimensions and policy. The record sequence +/// is decoded transactionally: on failure, all page, pin, hyperlink, and +/// temporary allocations are released and no partially restored Screen escapes. +/// The returned key selects the ScreenSet slot that owns the decoded screen. +pub fn decode( + source: *std.Io.Reader, + io_: std.Io, + alloc: Allocator, + options: TerminalScreen.Options, +) DecodeError!Decoded { + // Decode and finish the self-contained SCREEN record before consuming any + // of the PAGE records which follow it. + var record_reader: record.Reader = undefined; + try record_reader.init(source); + if (record_reader.header.tag != .screen) { + return error.UnexpectedRecordTag; + } + + // The optional saved cursor and cursor hyperlink are both part of the + // SCREEN payload. Keep ownership of the decoded hyperlink locally until it + // has been copied into the native Screen. + const payload_reader = record_reader.payloadReader(); + const header = try Header.decode(payload_reader); + const saved_cursor = if (header.saved_cursor_present) + try SavedCursor.decode(payload_reader) + else + null; + var cursor_hyperlink = try decodeCursorHyperlink( + payload_reader, + alloc, + ); + defer if (cursor_hyperlink) |*value| value.deinit(alloc); + try record_reader.finish(); + + const key = header.key; + if (header.page_count == 0) return error.InvalidPageCount; + + // Dimensions are terminal-wide state supplied by the enclosing snapshot. + // Validate them, and all cursor invariants which depend on them, before + // allocating the declared pages. + if (options.cols == 0 or options.rows == 0) { + return error.InvalidDimensions; + } + + // Build the native page list transactionally. Alternate screens never + // retain scrollback, while primary screens inherit the caller's limits. + var result: TerminalScreen = result: { + var builder = try TerminalPageList.Builder.init(alloc, .{ + .cols = options.cols, + .rows = options.rows, + .max_size = if (key == .alternate) + 0 + else + options.max_scrollback_bytes, + .max_lines = if (key == .alternate) + 0 + else + options.max_scrollback_lines, + }); + defer builder.deinit(); + + // Allocate each PAGE at its declared capacity and decode directly into + // builder-owned storage, avoiding an intermediate page representation. + for (0..header.page_count) |_| { + var decoder: page.Decoder = undefined; + try decoder.init(source); + const native_page = try builder.allocatePage(decoder.capacity()); + try decoder.decode(native_page, alloc); + } + + // Finishing validates that the decoded suffix covers the active area + // and establishes the PageList's active viewport. + var pages = try builder.finish(); + errdefer pages.deinit(); + + // Convert the payload-relative cursor position into the tracked native + // pin and page-local row/cell pointers required by TerminalScreen. + const cursor: TerminalScreen.Cursor = cursor: { + const y = @min(header.cursor_y, options.rows - 1); + + // The active area can temporarily contain mixed-width pages after + // a reflow. Locate the row at column zero, then clamp the encoded + // x coordinate to the width of that physical page. + const row_pin = pages.pin(.{ .active = .{ .y = y } }) orelse unreachable; + const x = @min(header.cursor_x, row_pin.node.cols() - 1); + const pin = try pages.trackPin(.{ + .node = row_pin.node, + .x = x, + .y = row_pin.y, + }); + const rac = pin.rowAndCell(); + + break :cursor .{ + .x = x, + .y = y, + .cursor_style = header.cursor_style, + .pending_wrap = header.cursor_flags.pending_wrap and + x == row_pin.node.cols() - 1, + .protected = header.cursor_flags.protected, + .style = header.cursor_pen, + .hyperlink_implicit_id = header.hyperlink_implicit_id, + .semantic_content = header.cursor_flags.semantic_content, + .semantic_content_clear_eol = header.cursor_flags + .semantic_content_clear_eol, + .page_pin = pin, + .page_row = rac.row, + .page_cell = rac.cell, + }; + }; + + // Assemble the remaining native state from stable snapshot registries. + // Derived state and page-local table references are repaired below. + break :result .{ + .io = io_, + .alloc = alloc, + .pages = pages, + .no_scrollback = key == .alternate or + options.max_scrollback_bytes == 0, + .cursor = cursor, + .saved_cursor = if (saved_cursor) |value| + value.terminal(options.cols, options.rows) + else + null, + .charset = header.charset, + .protected_mode = header.protected_mode, + .kitty_keyboard = header.kitty_keyboard.terminal(), + .semantic_prompt = .{ + .seen = false, + .click = header.semantic_click, + }, + }; + }; + errdefer result.deinit(); + + // Reinsert the concrete cursor style into its page-local style table. This + // existing Screen path grows or splits the page when the decoded table is + // already full. + try result.manualStyleUpdate(); + + // Reinsert the cursor hyperlink into its page-local hyperlink table. For an + // implicit link, temporarily seed the counter with the encoded link ID, + // then restore the independent next-ID counter from the header. + if (cursor_hyperlink) |value| { + switch (value.id) { + .explicit => |id| try result.startHyperlink(value.uri, id), + .implicit => |id| { + result.cursor.hyperlink_implicit_id = id; + try result.startHyperlink(value.uri, null); + result.cursor.hyperlink_implicit_id = + header.hyperlink_implicit_id; + }, + } + } + + // `seen` is derived state. Only resident prompt metadata participates + // because omitted history is outside the restored Screen model. + result.semantic_prompt.seen = seen: { + if (result.cursor.semantic_content == .prompt) break :seen true; + + var node = result.pages.getTopLeft(.screen).node; + while (true) { + const native_page = node.pageAssumeResident(); + const rows = native_page.rows.ptr( + native_page.memory, + )[0..native_page.size.rows]; + for (rows) |row| { + if (row.semantic_prompt != .none) break :seen true; + + const cells = row.cells.ptr( + native_page.memory, + )[0..native_page.size.cols]; + for (cells) |cell| { + if (cell.semantic_content == .prompt) break :seen true; + } + } + + node = node.next orelse break :seen false; + } + + unreachable; + }; + + // Kitty image payloads are outside this record, but the restored Screen + // must still receive the caller's terminal-wide storage policy. + if (comptime build_options.kitty_graphics) { + result.kitty_images.setLimit( + io_, + alloc, + &result, + options.kitty_image_storage_limit, + ); + result.kitty_images.image_limits = options.kitty_image_loading_limits; + } + + // All fallible reconstruction is complete; verify the native invariants + // before transferring ownership to the caller. + result.pages.assertIntegrity(); + result.assertIntegrity(); + return .{ + .key = key, + .history_rows = header.history_rows, + .screen = result, + }; +} + +/// Errors possible while decoding fixed SCREEN payload fields. +const PayloadDecodeError = std.Io.Reader.Error || error{InvalidKey}; + +/// Flags encoded after the cursor's visual shape. +pub const CursorFlags = packed struct(u8) { + pending_wrap: bool = false, + protected: bool = false, + semantic_content: terminal_page.Cell.SemanticContent = .output, + semantic_content_clear_eol: bool = false, + _padding: u3 = 0, + + fn init(cursor: *const TerminalScreen.Cursor) CursorFlags { + return .{ + .pending_wrap = cursor.pending_wrap, + .protected = cursor.protected, + .semantic_content = cursor.semantic_content, + .semantic_content_clear_eol = cursor + .semantic_content_clear_eol, + }; + } + + /// Decode the cursor flag registry, normalizing unknown semantic state. + pub fn decode(reader: *std.Io.Reader) std.Io.Reader.Error!CursorFlags { + const raw = try reader.takeByte(); + const bits: CursorFlags = @bitCast(raw); + const semantic_content_raw: u2 = @truncate(raw >> @bitOffsetOf(CursorFlags, "semantic_content")); + const semantic_content = std.enums.fromInt( + terminal_page.Cell.SemanticContent, + semantic_content_raw, + ) orelse .output; + + return .{ + .pending_wrap = bits.pending_wrap, + .protected = bits.protected, + .semantic_content = semantic_content, + .semantic_content_clear_eol = bits.semantic_content_clear_eol, + }; + } +}; + +/// The packed wire bits shared by current and saved cursor charset state. +/// +/// Native enum values are stable, but their backing type is `c_int` in C ABI +/// builds. The wire therefore stores only checked integer values at its fixed +/// bit widths. +const CharsetBits = packed struct(u16) { + g0: u2 = 0, + g1: u2 = 0, + g2: u2 = 0, + g3: u2 = 0, + gl: u2 = 0, + gr: u2 = 2, + single_shift: u3 = 0, + _padding: u1 = 0, +}; + +fn enumFromInt(comptime T: type, raw: anytype) ?T { + const Tag = @typeInfo(T).@"enum".tag_type; + const value = std.math.cast(Tag, raw) orelse return null; + return std.enums.fromInt(T, value); +} + +fn encodeCharsetState( + value: TerminalScreen.CharsetState, +) error{InvalidCharsetState}!u16 { + const g0 = std.math.cast( + u2, + @intFromEnum(value.charsets.get(.G0)), + ) orelse return error.InvalidCharsetState; + const g1 = std.math.cast( + u2, + @intFromEnum(value.charsets.get(.G1)), + ) orelse return error.InvalidCharsetState; + const g2 = std.math.cast( + u2, + @intFromEnum(value.charsets.get(.G2)), + ) orelse return error.InvalidCharsetState; + const g3 = std.math.cast( + u2, + @intFromEnum(value.charsets.get(.G3)), + ) orelse return error.InvalidCharsetState; + const gl = std.math.cast( + u2, + @intFromEnum(value.gl), + ) orelse return error.InvalidCharsetState; + const gr = std.math.cast( + u2, + @intFromEnum(value.gr), + ) orelse return error.InvalidCharsetState; + const single_shift: u3 = if (value.single_shift) |slot| single_shift: { + const raw = std.math.cast( + u2, + @intFromEnum(slot), + ) orelse return error.InvalidCharsetState; + break :single_shift @as(u3, raw) + 1; + } else 0; + + const bits: CharsetBits = .{ + .g0 = g0, + .g1 = g1, + .g2 = g2, + .g3 = g3, + .gl = gl, + .gr = gr, + .single_shift = single_shift, + }; + return @bitCast(bits); +} + +fn decodeCharsetState( + raw: u16, +) TerminalScreen.CharsetState { + const bits: CharsetBits = @bitCast(raw); + + var result: TerminalScreen.CharsetState = .{}; + result.gl = enumFromInt( + terminal_charsets.Slots, + bits.gl, + ) orelse result.gl; + result.gr = enumFromInt( + terminal_charsets.Slots, + bits.gr, + ) orelse result.gr; + result.single_shift = if (bits.single_shift == 0) + null + else if (bits.single_shift <= 4) + enumFromInt( + terminal_charsets.Slots, + bits.single_shift - 1, + ) + else + null; + result.charsets.set( + .G0, + enumFromInt( + terminal_charsets.Charset, + bits.g0, + ) orelse .utf8, + ); + result.charsets.set( + .G1, + enumFromInt( + terminal_charsets.Charset, + bits.g1, + ) orelse .utf8, + ); + result.charsets.set( + .G2, + enumFromInt( + terminal_charsets.Charset, + bits.g2, + ) orelse .utf8, + ); + result.charsets.set( + .G3, + enumFromInt( + terminal_charsets.Charset, + bits.g3, + ) orelse .utf8, + ); + return result; +} + +/// The complete fixed-size Kitty keyboard stack. +pub const KittyKeyboard = struct { + index: u8 = 0, + flags: [8]Flags = @splat(.{}), + + /// One byte in the fixed Kitty keyboard flag stack. + pub const Flags = packed struct(u8) { + disambiguate: bool = false, + report_events: bool = false, + report_alternates: bool = false, + report_all: bool = false, + report_associated: bool = false, + _padding: u3 = 0, + + /// Decode one Kitty keyboard flag byte, ignoring reserved bits. + pub fn decode(reader: *std.Io.Reader) std.Io.Reader.Error!Flags { + var result: Flags = @bitCast(try reader.takeByte()); + result._padding = 0; + return result; + } + }; + + fn init(value: terminal_kitty.KeyFlagStack) KittyKeyboard { + var result: KittyKeyboard = .{ .index = value.idx }; + for (value.flags, &result.flags) |native, *snapshot| { + snapshot.* = .{ + .disambiguate = native.disambiguate, + .report_events = native.report_events, + .report_alternates = native.report_alternates, + .report_all = native.report_all, + .report_associated = native.report_associated, + }; + } + return result; + } + + fn terminal(self: KittyKeyboard) terminal_kitty.KeyFlagStack { + var result: terminal_kitty.KeyFlagStack = .{ + .idx = @intCast(self.index), + }; + for (self.flags, &result.flags) |snapshot, *native| { + native.* = .{ + .disambiguate = snapshot.disambiguate, + .report_events = snapshot.report_events, + .report_alternates = snapshot.report_alternates, + .report_all = snapshot.report_all, + .report_associated = snapshot.report_associated, + }; + } + return result; + } + + /// Decode the complete fixed Kitty keyboard stack. + pub fn decode(reader: *std.Io.Reader) std.Io.Reader.Error!KittyKeyboard { + const index_raw = try reader.takeByte(); + const index = if (index_raw < 8) index_raw else 0; + + var flags: [8]Flags = undefined; + for (&flags) |*entry| entry.* = try Flags.decode(reader); + return .{ .index = index, .flags = flags }; + } +}; + +/// Decode semantic-click state, falling back to disabled for unknown values. +fn decodeSemanticClick( + reader: *std.Io.Reader, +) std.Io.Reader.Error!TerminalScreen.SemanticPrompt.SemanticClick { + const kind = enumFromInt( + TerminalScreen.SemanticPrompt.SemanticClickKind, + try reader.takeByte(), + ); + const value = try reader.takeByte(); + + return switch (kind orelse return .none) { + .none => .none, + .click_events => .{ .click_events = enumFromInt( + terminal_osc.semantic_prompt.ClickEvents, + value, + ) orelse return .none }, + .cl => .{ .cl = enumFromInt( + terminal_osc.semantic_prompt.Click, + value, + ) orelse return .none }, + }; +} + +/// The optional fixed-size saved cursor state. +pub const SavedCursor = struct { + /// Number of bytes written by `encode`, calculated using the encoder. + pub const len = computeLen(); + + comptime { + std.debug.assert(len == 23); + } + + x: u16, + y: u16, + pen: TerminalStyle, + flags: Flags, + charset: TerminalScreen.CharsetState, + + /// Flags encoded with an optional saved cursor. + pub const Flags = packed struct(u8) { + protected: bool = false, + pending_wrap: bool = false, + origin: bool = false, + _padding: u5 = 0, + + /// Decode saved cursor flags, ignoring reserved bits. + pub fn decode(reader: *std.Io.Reader) std.Io.Reader.Error!Flags { + var result: Flags = @bitCast(try reader.takeByte()); + result._padding = 0; + return result; + } + }; + + fn init(value: TerminalScreen.SavedCursor) SavedCursor { + return .{ + .x = value.x, + .y = value.y, + .pen = value.style, + .flags = .{ + .protected = value.protected, + .pending_wrap = value.pending_wrap, + .origin = value.origin, + }, + .charset = value.charset, + }; + } + + fn terminal( + self: SavedCursor, + cols: u16, + rows: u16, + ) TerminalScreen.SavedCursor { + const x = @min(self.x, cols - 1); + return .{ + .x = x, + .y = @min(self.y, rows - 1), + .style = self.pen, + .protected = self.flags.protected, + .pending_wrap = self.flags.pending_wrap and x == cols - 1, + .origin = self.flags.origin, + .charset = self.charset, + }; + } + + /// Encode one optional saved cursor value. + pub fn encode( + self: SavedCursor, + writer: *std.Io.Writer, + ) PayloadEncodeError!void { + if (self.flags._padding != 0) { + return error.InvalidSavedCursorFlags; + } + const charset = try encodeCharsetState(self.charset); + + try io.writeInt(writer, u16, self.x); + try io.writeInt(writer, u16, self.y); + try style.encode(self.pen, writer); + try writer.writeByte(@bitCast(self.flags)); + try io.writeInt(writer, u16, charset); + } + + /// Decode and validate one optional saved cursor value. + pub fn decode(reader: *std.Io.Reader) PayloadDecodeError!SavedCursor { + return .{ + .x = try io.readInt(reader, u16), + .y = try io.readInt(reader, u16), + .pen = (try style.decodeOrNull(reader)) orelse .{}, + .flags = try Flags.decode(reader), + .charset = decodeCharsetState( + try io.readInt(reader, u16), + ), + }; + } + + fn computeLen() usize { + comptime { + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + const value: SavedCursor = .{ + .x = 0, + .y = 0, + .pen = .{}, + .flags = .{}, + .charset = .{}, + }; + value.encode(&writer) catch unreachable; + return writer.end; + } + } +}; + +/// The fixed fields at the start of every SCREEN payload. +pub const Header = struct { + /// Number of bytes written by `encode`, calculated using the encoder. + pub const len = computeLen(); + + comptime { + std.debug.assert(len == 53); + } + + key: TerminalScreenKey, + /// Complete pages in the minimal suffix covering the active area. + page_count: u16, + /// Advisory logical rows before the active area, including resident + /// SCREEN overlap. + history_rows: u64, + cursor_x: u16, + cursor_y: u16, + cursor_style: TerminalScreen.CursorStyle, + cursor_flags: CursorFlags, + cursor_pen: TerminalStyle, + hyperlink_implicit_id: u32, + charset: TerminalScreen.CharsetState, + protected_mode: terminal_ansi.ProtectedMode, + kitty_keyboard: KittyKeyboard, + semantic_click: TerminalScreen.SemanticPrompt.SemanticClick, + saved_cursor_present: bool, + + /// Initialize the fixed header from one native screen. + fn init( + screen: *const TerminalScreen, + key: TerminalScreenKey, + page_count: u16, + ) Header { + return .{ + .key = key, + .page_count = page_count, + .history_rows = @intCast( + screen.pages.total_rows - screen.pages.rows, + ), + .cursor_x = screen.cursor.x, + .cursor_y = screen.cursor.y, + .cursor_style = screen.cursor.cursor_style, + .cursor_flags = .init(&screen.cursor), + .cursor_pen = screen.cursor.style, + .hyperlink_implicit_id = screen.cursor.hyperlink_implicit_id, + .charset = screen.charset, + .protected_mode = screen.protected_mode, + .kitty_keyboard = .init(screen.kitty_keyboard), + .semantic_click = screen.semantic_prompt.click, + .saved_cursor_present = screen.saved_cursor != null, + }; + } + + /// Encode the fixed SCREEN payload header field by field. + pub fn encode( + self: Header, + writer: *std.Io.Writer, + ) PayloadEncodeError!void { + // Validate packed cursor state before writing any bytes. + if (self.cursor_flags._padding != 0) { + return error.InvalidCursorFlags; + } + + // Validate and narrow native charset enum values before writing. + const charset = try encodeCharsetState(self.charset); + + // Validate the complete Kitty keyboard stack. + if (self.kitty_keyboard.index >= self.kitty_keyboard.flags.len) { + return error.InvalidKittyKeyboardIndex; + } + for (self.kitty_keyboard.flags) |flags| { + if (flags._padding != 0) { + return error.InvalidKittyKeyboardFlags; + } + } + + // Screen identity, expected history extent, and cursor position. + try io.writeInt(writer, u16, @intCast(@intFromEnum(self.key))); + try io.writeInt(writer, u16, self.page_count); + try io.writeInt(writer, u64, self.history_rows); + try io.writeInt(writer, u16, self.cursor_x); + try io.writeInt(writer, u16, self.cursor_y); + + // Current cursor rendering state. + try writer.writeByte(@intCast(@intFromEnum(self.cursor_style))); + try writer.writeByte(@bitCast(self.cursor_flags)); + try style.encode(self.cursor_pen, writer); + try io.writeInt(writer, u32, self.hyperlink_implicit_id); + + // Charset and selective-erase state. + try io.writeInt(writer, u16, charset); + try writer.writeByte(@intCast(@intFromEnum(self.protected_mode))); + + // Keyboard modes and semantic-click state. + try writer.writeByte(self.kitty_keyboard.index); + for (self.kitty_keyboard.flags) |flags| { + try writer.writeByte(@bitCast(flags)); + } + try writer.writeByte(@intCast(@intFromEnum( + std.meta.activeTag(self.semantic_click), + ))); + switch (self.semantic_click) { + .none => try writer.writeByte(0), + .click_events => |value| try writer.writeByte( + @intCast(@intFromEnum(value)), + ), + .cl => |value| try writer.writeByte( + @intCast(@intFromEnum(value)), + ), + } + + // Presence of the optional saved-cursor suffix. + try writer.writeByte(@intFromBool(self.saved_cursor_present)); + } + + /// Decode the fixed SCREEN payload header. + /// + /// The screen key remains structural because it routes the following PAGE + /// sequence. Unknown semantic fields use native defaults instead. + pub fn decode(reader: *std.Io.Reader) PayloadDecodeError!Header { + // Screen identity, expected history extent, and cursor position. + const key = enumFromInt( + TerminalScreenKey, + try io.readInt(reader, u16), + ) orelse return error.InvalidKey; + const page_count = try io.readInt(reader, u16); + const history_rows = try io.readInt(reader, u64); + const cursor_x = try io.readInt(reader, u16); + const cursor_y = try io.readInt(reader, u16); + + // Current cursor rendering state. + const cursor_style = enumFromInt( + TerminalScreen.CursorStyle, + try reader.takeByte(), + ) orelse .block; + const cursor_flags = try CursorFlags.decode(reader); + const cursor_pen: TerminalStyle = + (try style.decodeOrNull(reader)) orelse .{}; + const hyperlink_implicit_id = try io.readInt(reader, u32); + + // Charset and selective-erase state. + const charset = decodeCharsetState( + try io.readInt(reader, u16), + ); + const protected_mode = enumFromInt( + terminal_ansi.ProtectedMode, + try reader.takeByte(), + ) orelse .off; + + // Keyboard modes and semantic-click state. + const kitty_keyboard = try KittyKeyboard.decode(reader); + const semantic_click = try decodeSemanticClick(reader); + + // Presence of the optional saved-cursor suffix. + const saved_cursor_present = try reader.takeByte() != 0; + + return .{ + .key = key, + .page_count = page_count, + .history_rows = history_rows, + .cursor_x = cursor_x, + .cursor_y = cursor_y, + + .cursor_style = cursor_style, + .cursor_flags = cursor_flags, + .cursor_pen = cursor_pen, + .hyperlink_implicit_id = hyperlink_implicit_id, + + .charset = charset, + .protected_mode = protected_mode, + + .kitty_keyboard = kitty_keyboard, + .semantic_click = semantic_click, + + .saved_cursor_present = saved_cursor_present, + }; + } + + fn computeLen() usize { + comptime { + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + const value: Header = .{ + .key = .primary, + .page_count = 0, + .history_rows = 0, + .cursor_x = 0, + .cursor_y = 0, + .cursor_style = .bar, + .cursor_flags = .{}, + .cursor_pen = .{}, + .hyperlink_implicit_id = 0, + .charset = .{}, + .protected_mode = .off, + .kitty_keyboard = .{}, + .semantic_click = .none, + .saved_cursor_present = false, + }; + value.encode(&writer) catch unreachable; + return writer.end; + } + } +}; + +/// Encode the SCREEN payload, excluding its following PAGE records. +fn encodePayload( + screen: *const TerminalScreen, + key: TerminalScreenKey, + page_count: u16, + writer: *std.Io.Writer, +) PayloadEncodeError!void { + try Header.init(screen, key, page_count).encode(writer); + + if (screen.saved_cursor) |saved_cursor| { + try SavedCursor.init(saved_cursor).encode(writer); + } + + const cursor_hyperlink: ?TerminalHyperlink = + if (screen.cursor.hyperlink) |entry| entry.* else null; + try encodeCursorHyperlink(cursor_hyperlink, writer); +} + +/// The variable cursor hyperlink at the end of every SCREEN payload. +pub const CursorHyperlink = ?TerminalHyperlink; + +/// Encode the optional cursor hyperlink at the end of a SCREEN payload. +pub fn encodeCursorHyperlink( + value: CursorHyperlink, + writer: *std.Io.Writer, +) hyperlink.EncodeError!void { + if (value) |entry| return hyperlink.encode(entry, writer); + try writer.writeByte(0); +} + +/// Decode one allocator-owned optional cursor hyperlink. +/// +/// The caller owns a non-null returned value and must call `Hyperlink.deinit`. +pub fn decodeCursorHyperlink( + reader: *std.Io.Reader, + alloc: Allocator, +) std.Io.Reader.Error!CursorHyperlink { + if (try reader.peekByte() == 0) { + _ = try reader.takeByte(); + return null; + } + + return hyperlink.decode(reader, alloc) catch |err| switch (err) { + error.ReadFailed => error.ReadFailed, + + // The cursor hyperlink is the final SCREEN payload field, so its + // record boundary lets us discard an invalid or unrepresentable value + // without losing the following PAGE sequence. + error.EndOfStream, + error.OutOfMemory, + error.InvalidKind, + error.InvalidUri, + error.InvalidExplicitId, + => { + _ = try reader.discardRemaining(); + return null; + }, + }; +} + +const test_header_fixture = test_fixture.parse(@embedFile("testdata/screen-header-v1.hex")); +const test_saved_cursor_fixture = test_fixture.parse(@embedFile("testdata/screen-saved-cursor-v1.hex")); + +fn testCharsetState() TerminalScreen.CharsetState { + var result: TerminalScreen.CharsetState = .{ + .gl = .G1, + .gr = .G3, + .single_shift = .G2, + }; + result.charsets.set(.G0, .utf8); + result.charsets.set(.G1, .ascii); + result.charsets.set(.G2, .british); + result.charsets.set(.G3, .dec_special); + return result; +} + +fn testHeader() Header { + return .{ + .key = .alternate, + .page_count = 0x0203, + .history_rows = 0x0102030405060708, + .cursor_x = 0x0405, + .cursor_y = 0x0607, + .cursor_style = .block_hollow, + .cursor_flags = .{ + .pending_wrap = true, + .semantic_content = .prompt, + .semantic_content_clear_eol = true, + }, + .cursor_pen = .{ + .fg_color = .none, + .bg_color = .{ .palette = 0x7f }, + .underline_color = .{ .rgb = .{ + .r = 0x12, + .g = 0x34, + .b = 0x56, + } }, + .flags = .{ + .bold = true, + .italic = true, + .faint = true, + .blink = true, + .inverse = true, + .invisible = true, + .strikethrough = true, + .overline = true, + .underline = .curly, + }, + }, + .hyperlink_implicit_id = 0x0a0b0c0d, + .charset = testCharsetState(), + .protected_mode = .dec, + .kitty_keyboard = .{ + .index = 7, + .flags = .{ + .{ .disambiguate = true }, + .{ .report_events = true }, + .{ .report_alternates = true }, + .{ .report_all = true }, + .{ .report_associated = true }, + .{ + .disambiguate = true, + .report_events = true, + .report_alternates = true, + .report_all = true, + .report_associated = true, + }, + .{}, + .{ + .disambiguate = true, + .report_associated = true, + }, + }, + }, + .semantic_click = .{ .cl = .smart_vertical }, + .saved_cursor_present = true, + }; +} + +fn testSavedCursor() SavedCursor { + return .{ + .x = 0x0102, + .y = 0x0304, + .pen = .{}, + .flags = .{ + .protected = true, + .pending_wrap = true, + .origin = true, + }, + .charset = testCharsetState(), + }; +} + +test "SCREEN header golden encoding and decoding" { + var encoded: [Header.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try testHeader().encode(&writer); + + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/screen-header-v1.hex", + "snapshot_fixture-screen-header-v1.hex", + &test_header_fixture, + writer.buffered(), + ); + try std.testing.expectEqual(Header.len, test_header_fixture.len); + + var source: std.Io.Reader = .fixed(&test_header_fixture); + var buffer: [1]u8 = undefined; + var limited = source.limited(.unlimited, &buffer); + + try std.testing.expectEqualDeep( + testHeader(), + try Header.decode(&limited.interface), + ); +} + +test "native enum values used by the SCREEN format" { + const keys = [_]TerminalScreenKey{ .primary, .alternate }; + for (keys, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } + + const cursor_styles = [_]TerminalScreen.CursorStyle{ + .bar, + .block, + .underline, + .block_hollow, + }; + for (cursor_styles, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } + + const semantic_contents = [_]terminal_page.Cell.SemanticContent{ + .output, + .input, + .prompt, + }; + for (semantic_contents, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } + + const charsets = [_]terminal_charsets.Charset{ + .utf8, + .ascii, + .british, + .dec_special, + }; + for (charsets, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } + + const slots = [_]terminal_charsets.Slots{ .G0, .G1, .G2, .G3 }; + for (slots, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } + + const protected_modes = [_]terminal_ansi.ProtectedMode{ + .off, + .iso, + .dec, + }; + for (protected_modes, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } + + const click_kinds = [_]TerminalScreen.SemanticPrompt.SemanticClickKind{ + .none, + .click_events, + .cl, + }; + for (click_kinds, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } + + const click_events = [_]terminal_osc.semantic_prompt.ClickEvents{ + .absolute, + .relative, + }; + for (click_events, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } + + const clicks = [_]terminal_osc.semantic_prompt.Click{ + .line, + .multiple, + .conservative_vertical, + .smart_vertical, + }; + for (clicks, 0..) |value, expected| { + try std.testing.expectEqual( + expected, + @as(usize, @intCast(@intFromEnum(value))), + ); + } +} + +test "cursor, Kitty, and saved cursor flag bit layouts" { + const CursorCase = struct { + value: CursorFlags, + expected: u8, + }; + const cursor_cases = [_]CursorCase{ + .{ .value = .{ .pending_wrap = true }, .expected = 1 << 0 }, + .{ .value = .{ .protected = true }, .expected = 1 << 1 }, + .{ + .value = .{ .semantic_content = .input }, + .expected = 1 << 2, + }, + .{ + .value = .{ .semantic_content = .prompt }, + .expected = 2 << 2, + }, + .{ + .value = .{ .semantic_content_clear_eol = true }, + .expected = 1 << 4, + }, + }; + for (cursor_cases) |case| { + try std.testing.expectEqual( + case.expected, + @as(u8, @bitCast(case.value)), + ); + } + + const KittyCase = struct { + value: KittyKeyboard.Flags, + expected: u8, + }; + const kitty_cases = [_]KittyCase{ + .{ .value = .{ .disambiguate = true }, .expected = 1 << 0 }, + .{ .value = .{ .report_events = true }, .expected = 1 << 1 }, + .{ + .value = .{ .report_alternates = true }, + .expected = 1 << 2, + }, + .{ .value = .{ .report_all = true }, .expected = 1 << 3 }, + .{ + .value = .{ .report_associated = true }, + .expected = 1 << 4, + }, + }; + for (kitty_cases) |case| { + try std.testing.expectEqual( + case.expected, + @as(u8, @bitCast(case.value)), + ); + } + + const SavedCase = struct { + value: SavedCursor.Flags, + expected: u8, + }; + const saved_cases = [_]SavedCase{ + .{ .value = .{ .protected = true }, .expected = 1 << 0 }, + .{ .value = .{ .pending_wrap = true }, .expected = 1 << 1 }, + .{ .value = .{ .origin = true }, .expected = 1 << 2 }, + }; + for (saved_cases) |case| { + try std.testing.expectEqual( + case.expected, + @as(u8, @bitCast(case.value)), + ); + } +} + +test "header encoding rejects invalid state" { + var encoded: [Header.len]u8 = undefined; + + var invalid_cursor_flags = testHeader(); + invalid_cursor_flags.cursor_flags._padding = 1; + var cursor_flags_writer: std.Io.Writer = .fixed(&encoded); + try std.testing.expectError( + error.InvalidCursorFlags, + invalid_cursor_flags.encode(&cursor_flags_writer), + ); + + var invalid_kitty_index = testHeader(); + invalid_kitty_index.kitty_keyboard.index = 8; + var kitty_index_writer: std.Io.Writer = .fixed(&encoded); + try std.testing.expectError( + error.InvalidKittyKeyboardIndex, + invalid_kitty_index.encode(&kitty_index_writer), + ); + + var invalid_kitty_flags = testHeader(); + invalid_kitty_flags.kitty_keyboard.flags[3]._padding = 1; + var kitty_flags_writer: std.Io.Writer = .fixed(&encoded); + try std.testing.expectError( + error.InvalidKittyKeyboardFlags, + invalid_kitty_flags.encode(&kitty_flags_writer), + ); +} + +test "header decoding rejects structural values" { + const valid = [_]u8{0} ** Header.len; + + var invalid_key = valid; + invalid_key[0] = 2; + var key_reader: std.Io.Reader = .fixed(&invalid_key); + try std.testing.expectError(error.InvalidKey, Header.decode(&key_reader)); +} + +test "header decoding normalizes unknown semantic values" { + var fixture = [_]u8{0} ** Header.len; + + fixture[16] = 4; // Unknown cursor style. + fixture[17] = 0xFF; // Known flags, unknown semantic value, reserved bits. + fixture[18] = 3; // Unknown cursor foreground color kind. + fixture[39] = 0xD0; // Invalid single shift plus a reserved bit. + fixture[40] = 3; // Unknown protected mode. + fixture[41] = 8; // Out-of-range Kitty keyboard index. + @memset(fixture[42..50], 0xFF); // Known flags plus reserved bits. + fixture[50] = 3; // Unknown semantic-click kind. + fixture[51] = 0xFF; + fixture[52] = 2; // Noncanonical true. + + var reader: std.Io.Reader = .fixed(&fixture); + const decoded = try Header.decode(&reader); + + try std.testing.expectEqual( + TerminalScreen.CursorStyle.block, + decoded.cursor_style, + ); + try std.testing.expect(decoded.cursor_pen.default()); + try std.testing.expect(decoded.cursor_flags.pending_wrap); + try std.testing.expect(decoded.cursor_flags.protected); + try std.testing.expectEqual( + terminal_page.Cell.SemanticContent.output, + decoded.cursor_flags.semantic_content, + ); + try std.testing.expect( + decoded.cursor_flags.semantic_content_clear_eol, + ); + try std.testing.expectEqual(@as(u3, 0), decoded.cursor_flags._padding); + + try std.testing.expectEqual(null, decoded.charset.single_shift); + try std.testing.expectEqual(terminal_charsets.Slots.G0, decoded.charset.gr); + try std.testing.expectEqual( + terminal_ansi.ProtectedMode.off, + decoded.protected_mode, + ); + try std.testing.expectEqual(@as(u8, 0), decoded.kitty_keyboard.index); + for (decoded.kitty_keyboard.flags) |flags| { + try std.testing.expect(flags.disambiguate); + try std.testing.expect(flags.report_events); + try std.testing.expect(flags.report_alternates); + try std.testing.expect(flags.report_all); + try std.testing.expect(flags.report_associated); + try std.testing.expectEqual(@as(u3, 0), flags._padding); + } + try std.testing.expectEqual( + TerminalScreen.SemanticPrompt.SemanticClick.none, + decoded.semantic_click, + ); + try std.testing.expect(decoded.saved_cursor_present); + + // Known semantic-click kinds with unknown or noncanonical values also + // degrade to disabled while consuming both fixed bytes. + const invalid_semantic_clicks = [_][2]u8{ + .{ 0, 1 }, + .{ 1, 2 }, + .{ 2, 4 }, + }; + for (invalid_semantic_clicks) |invalid| { + var semantic_fixture = [_]u8{0} ** Header.len; + semantic_fixture[50] = invalid[0]; + semantic_fixture[51] = invalid[1]; + var semantic_reader: std.Io.Reader = .fixed(&semantic_fixture); + const semantic_decoded = try Header.decode(&semantic_reader); + try std.testing.expectEqual( + TerminalScreen.SemanticPrompt.SemanticClick.none, + semantic_decoded.semantic_click, + ); + } +} + +test "header decoding rejects every truncation" { + for (0..Header.len) |fixture_len| { + var reader: std.Io.Reader = .fixed( + test_header_fixture[0..fixture_len], + ); + try std.testing.expectError(error.EndOfStream, Header.decode(&reader)); + } +} + +test "native SCREEN payload omits absent optional state" { + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + + var encoded: [Header.len + 1]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try encodePayload(&screen, .primary, 1, &writer); + try std.testing.expectEqual(encoded.len, writer.end); + + var reader: std.Io.Reader = .fixed(writer.buffered()); + const header = try Header.decode(&reader); + try std.testing.expectEqual(TerminalScreenKey.primary, header.key); + try std.testing.expectEqual(@as(u16, 1), header.page_count); + try std.testing.expectEqual(@as(u64, 0), header.history_rows); + try std.testing.expect(!header.saved_cursor_present); + try std.testing.expectEqual( + null, + try decodeCursorHyperlink(&reader, std.testing.allocator), + ); +} + +test "saved cursor golden encoding and decoding" { + var encoded: [SavedCursor.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try testSavedCursor().encode(&writer); + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/screen-saved-cursor-v1.hex", + "snapshot_fixture-screen-saved-cursor-v1.hex", + &test_saved_cursor_fixture, + writer.buffered(), + ); + try std.testing.expectEqual( + SavedCursor.len, + test_saved_cursor_fixture.len, + ); + + var source: std.Io.Reader = .fixed(&test_saved_cursor_fixture); + var buffer: [1]u8 = undefined; + var limited = source.limited(.unlimited, &buffer); + try std.testing.expectEqualDeep( + testSavedCursor(), + try SavedCursor.decode(&limited.interface), + ); +} + +test "saved cursor encoding rejects and decoding normalizes invalid state" { + var encoded: [SavedCursor.len]u8 = undefined; + + var invalid_flags = testSavedCursor(); + invalid_flags.flags._padding = 1; + var flags_writer: std.Io.Writer = .fixed(&encoded); + try std.testing.expectError( + error.InvalidSavedCursorFlags, + invalid_flags.encode(&flags_writer), + ); + + var invalid = [_]u8{0} ** SavedCursor.len; + invalid[4] = 3; // Unknown saved-cursor foreground color kind. + invalid[20] = 0xF9; // Protected plus reserved bits. + invalid[22] = 0xD0; // Invalid single shift plus a reserved bit. + var reader: std.Io.Reader = .fixed(&invalid); + const decoded = try SavedCursor.decode(&reader); + + try std.testing.expect(decoded.pen.default()); + try std.testing.expect(decoded.flags.protected); + try std.testing.expect(!decoded.flags.pending_wrap); + try std.testing.expect(!decoded.flags.origin); + try std.testing.expectEqual(@as(u5, 0), decoded.flags._padding); + try std.testing.expectEqual(null, decoded.charset.single_shift); + try std.testing.expectEqual( + terminal_charsets.Slots.G0, + decoded.charset.gr, + ); +} + +test "saved cursor decoding rejects every truncation" { + for (0..SavedCursor.len) |fixture_len| { + var reader: std.Io.Reader = .fixed( + test_saved_cursor_fixture[0..fixture_len], + ); + try std.testing.expectError( + error.EndOfStream, + SavedCursor.decode(&reader), + ); + } +} + +test "cursor hyperlink null encoding and decoding" { + var none_encoded: [1]u8 = undefined; + var none_writer: std.Io.Writer = .fixed(&none_encoded); + try encodeCursorHyperlink(null, &none_writer); + try std.testing.expectEqualStrings("\x00", none_writer.buffered()); + + var none_reader: std.Io.Reader = .fixed(none_writer.buffered()); + try std.testing.expectEqual( + null, + try decodeCursorHyperlink(&none_reader, std.testing.allocator), + ); +} + +test "framed native SCREEN and PAGE sequence" { + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 8, .rows = 8, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + + // Configure the live cursor state represented by the fixed header. + screen.cursorAbsolute(7, 6); + screen.cursor.cursor_style = .block_hollow; + screen.cursor.pending_wrap = true; + screen.cursor.protected = false; + screen.cursor.semantic_content = .prompt; + screen.cursor.semantic_content_clear_eol = true; + screen.cursor.style = testHeader().cursor_pen; + screen.cursor.hyperlink_implicit_id = 0x0a0b0c0d; + + // Configure the screen-wide modes and packed charset state. + var charset: TerminalScreen.CharsetState = .{ + .gl = .G1, + .gr = .G3, + .single_shift = .G2, + }; + charset.charsets.set(.G0, .utf8); + charset.charsets.set(.G1, .ascii); + charset.charsets.set(.G2, .british); + charset.charsets.set(.G3, .dec_special); + screen.charset = charset; + screen.protected_mode = .dec; + screen.kitty_keyboard = .{ + .idx = 7, + .flags = .{ + .{ .disambiguate = true }, + .{ .report_events = true }, + .{ .report_alternates = true }, + .{ .report_all = true }, + .{ .report_associated = true }, + .{ + .disambiguate = true, + .report_events = true, + .report_alternates = true, + .report_all = true, + .report_associated = true, + }, + .{}, + .{ + .disambiguate = true, + .report_associated = true, + }, + }, + }; + screen.semantic_prompt = .{ + .seen = true, + .click = .{ .cl = .smart_vertical }, + }; + + // Configure the optional cursor state encoded after the fixed header. + screen.saved_cursor = .{ + .x = 0x0102, + .y = 0x0304, + .style = .{}, + .protected = true, + .pending_wrap = true, + .origin = true, + .charset = charset, + }; + try screen.startHyperlink("cursor-uri", null); + + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + try destination.writer.writeAll("prefix"); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try encode(&screen, .alternate, &stream); + + try std.testing.expectEqualStrings( + "prefix", + destination.written()[0..6], + ); + + // The complete sequence restores directly into native Screen/PageList + // state, including page-local cursor references. + var restore_source: std.Io.Reader = .fixed(destination.written()[6..]); + var decoded = try decode( + &restore_source, + std.testing.io, + std.testing.allocator, + .{ .cols = 8, .rows = 8, .max_scrollback_bytes = 0 }, + ); + defer decoded.deinit(); + try std.testing.expectEqual(TerminalScreenKey.alternate, decoded.key); + const restored = &decoded.screen; + + try std.testing.expect(restored.no_scrollback); + try std.testing.expectEqual(@as(u16, 7), restored.cursor.x); + try std.testing.expectEqual(@as(u16, 6), restored.cursor.y); + try std.testing.expectEqual( + TerminalScreen.CursorStyle.block_hollow, + restored.cursor.cursor_style, + ); + try std.testing.expect(restored.cursor.pending_wrap); + try std.testing.expectEqual( + terminal_page.Cell.SemanticContent.prompt, + restored.cursor.semantic_content, + ); + try std.testing.expect( + restored.cursor.style.eql(testHeader().cursor_pen), + ); + try std.testing.expect(restored.cursor.style_id != terminal_style.default_id); + try std.testing.expectEqual( + @as(u32, 0x0a0b0c0e), + restored.cursor.hyperlink_implicit_id, + ); + const restored_hyperlink = restored.cursor.hyperlink.?.*; + try std.testing.expectEqualStrings( + "cursor-uri", + restored_hyperlink.uri, + ); + switch (restored_hyperlink.id) { + .implicit => |id| try std.testing.expectEqual( + @as(u32, 0x0a0b0c0d), + id, + ), + .explicit => try std.testing.expect(false), + } + + const restored_saved = restored.saved_cursor.?; + try std.testing.expectEqual(@as(u16, 7), restored_saved.x); + try std.testing.expectEqual(@as(u16, 7), restored_saved.y); + try std.testing.expect(restored_saved.protected); + try std.testing.expect(restored_saved.pending_wrap); + try std.testing.expect(restored_saved.origin); + try std.testing.expectEqual( + terminal_charsets.Charset.ascii, + restored.charset.charsets.get(.G1), + ); + try std.testing.expectEqual( + terminal_ansi.ProtectedMode.dec, + restored.protected_mode, + ); + try std.testing.expectEqual(@as(u8, 7), restored.kitty_keyboard.idx); + try std.testing.expect(restored.semantic_prompt.seen); + try std.testing.expectEqual( + TerminalScreen.SemanticPrompt.SemanticClick{ + .cl = .smart_vertical, + }, + restored.semantic_prompt.click, + ); + + // Restored page-local cursor IDs are immediately usable by normal writes. + try restored.testWriteString("Z"); + const written = restored.pages.getCell(.{ .active = .{ + .x = 0, + .y = 7, + } }).?; + try std.testing.expectEqual(@as(u21, 'Z'), written.cell.codepoint()); + try std.testing.expectEqual( + restored.cursor.style_id, + written.cell.style_id, + ); + try std.testing.expectEqual( + restored.cursor.hyperlink_id, + written.node.page().lookupHyperlink(written.cell).?, + ); + try std.testing.expectEqual( + terminal_page.Cell.SemanticContent.prompt, + written.cell.semantic_content, + ); + try std.testing.expectError(error.EndOfStream, restore_source.takeByte()); +} + +test "SCREEN encodes the minimal complete-page active suffix" { + var probe = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 80, .rows = 1, .max_scrollback_bytes = 0 }, + ); + const page_rows = probe.pages.getTopLeft(.screen).node.capacity().rows; + probe.deinit(); + const screen_rows = page_rows + 1; + + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ + .cols = 80, + .rows = screen_rows, + .max_scrollback_bytes = null, + }, + ); + defer screen.deinit(); + + // Create one complete historical page followed by a mixed page whose + // first row is history and whose remaining rows begin the active area. + screen.cursorAbsolute(0, screen_rows - 1); + for (0..screen_rows) |_| try screen.testWriteString("\n"); + try std.testing.expectEqual(@as(usize, 3), screen.pages.totalPages()); + + const active_top = screen.pages.getTopLeft(.active); + const historical = screen.pages.getTopLeft(.screen).node; + const mixed = historical.next.?; + const newest = mixed.next.?; + try std.testing.expectEqual(mixed, active_top.node); + try std.testing.expectEqual(@as(u16, 1), active_top.y); + try std.testing.expectEqual(null, newest.next); + + // Mark each native page so the encoded sequence proves both omission and + // oldest-to-newest ordering. The mixed page's marker is in its incidental + // history prefix and must survive because complete pages are encoded. + historical.page().getRowAndCell(0, 0).cell.* = .init('A'); + mixed.page().getRowAndCell(0, 0).cell.* = .init('B'); + newest.page().getRowAndCell(0, 0).cell.* = .init('C'); + + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try encode(&screen, .primary, &stream); + + var source: std.Io.Reader = .fixed(destination.written()); + var screen_record: record.Reader = undefined; + try screen_record.init(&source); + try std.testing.expectEqual(record.Tag.screen, screen_record.header.tag); + + const payload_reader = screen_record.payloadReader(); + const header = try Header.decode(payload_reader); + try std.testing.expectEqual(TerminalScreenKey.primary, header.key); + try std.testing.expectEqual(@as(u16, 2), header.page_count); + try std.testing.expectEqual( + @as(u64, @intCast(screen.pages.total_rows - screen.pages.rows)), + header.history_rows, + ); + try std.testing.expect(!header.saved_cursor_present); + try std.testing.expectEqual( + null, + try decodeCursorHyperlink(payload_reader, std.testing.allocator), + ); + try screen_record.finish(); + + var decoded_mixed = try page.decode(&source, std.testing.allocator); + defer decoded_mixed.deinit(); + try std.testing.expectEqual( + @as(u21, 'B'), + decoded_mixed.getRowAndCell(0, 0).cell.codepoint(), + ); + + var decoded_newest = try page.decode(&source, std.testing.allocator); + defer decoded_newest.deinit(); + try std.testing.expectEqual( + @as(u21, 'C'), + decoded_newest.getRowAndCell(0, 0).cell.codepoint(), + ); + try std.testing.expectError(error.EndOfStream, source.takeByte()); + + // Native restoration keeps the incidental history prefix and positions + // the active top within the first decoded page. + var restore_source: std.Io.Reader = .fixed(destination.written()); + var decoded = try decode( + &restore_source, + std.testing.io, + std.testing.allocator, + .{ + .cols = 80, + .rows = screen_rows, + .max_scrollback_bytes = null, + }, + ); + defer decoded.deinit(); + try std.testing.expectEqual(TerminalScreenKey.primary, decoded.key); + try std.testing.expectEqual(header.history_rows, decoded.history_rows); + const restored = &decoded.screen; + + try std.testing.expectEqual(@as(usize, 2), restored.pages.totalPages()); + const restored_top = restored.pages.getTopLeft(.active); + try std.testing.expectEqual(@as(u16, 1), restored_top.y); + try std.testing.expectEqual( + @as(usize, restored_top.y), + restored.pages.total_rows - restored.pages.rows, + ); + try std.testing.expect( + decoded.history_rows > @as( + u64, + @intCast(restored.pages.total_rows - restored.pages.rows), + ), + ); + try std.testing.expectEqual( + @as(u21, 'B'), + restored.pages.getTopLeft(.screen).node + .page().getRowAndCell(0, 0).cell.codepoint(), + ); + try std.testing.expectEqual( + @as(u21, 'C'), + restored.pages.getTopLeft(.screen).node.next.? + .page().getRowAndCell(0, 0).cell.codepoint(), + ); + + // The restored PageList resumes ordinary scrolling and row growth. + try restored.testWriteString("\n"); + restored.pages.assertIntegrity(); + restored.assertIntegrity(); + try std.testing.expectError(error.EndOfStream, restore_source.takeByte()); +} + +test "SCREEN encoding preserves a compressed suffix page" { + const testing = std.testing; + const compression = @import("../compress.zig"); + + var screen = try TerminalScreen.init( + testing.io, + testing.allocator, + .{ .cols = 8, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + screen.pages.getCell(.{ .active = .{} }).?.cell.* = .init('A'); + + // Force the active suffix into the representation that current PageList + // policy normally reserves for history. This models a future policy change + // and makes pageAssumeResident an invalid tagged-union access. + const node = screen.pages.getTopLeft(.active).node; + const resident = node.pageAssumeResident(); + const scratch = try testing.allocator.alloc( + u8, + try compression.Page.requiredScratch(resident.memory.len), + ); + defer testing.allocator.free(scratch); + var table: compression.lz4.HashTable = undefined; + const compressed = (try compression.Page.init( + testing.allocator, + resident, + scratch, + &table, + )).?; + node.data = .{ .compressed = compressed }; + try testing.expectEqual(.compressed, node.storage()); + + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + var stream: record.Writer = .init(testing.allocator, &destination.writer); + defer stream.deinit(); + try encode(&screen, .primary, &stream); + + // Encoding borrows or clones through PreservedPage and never changes the + // source node's storage representation. + try testing.expectEqual(.compressed, node.storage()); + + var source: std.Io.Reader = .fixed(destination.written()); + var decoded = try decode( + &source, + testing.io, + testing.allocator, + .{ .cols = 8, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer decoded.deinit(); + try testing.expectEqual( + @as(u21, 'A'), + decoded.screen.pages.getCell(.{ .active = .{} }).?.cell.codepoint(), + ); +} + +test "SCREEN restoration normalizes invalid cursor positions" { + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + + const Case = struct { + x: u16, + y: u16, + pending_wrap: bool, + expected_x: u16, + expected_y: u16, + expected_pending_wrap: bool, + }; + const cases = [_]Case{ + // Out-of-range coordinates clamp to the bottom-right. Pending wrap is + // valid there and can be retained. + .{ + .x = std.math.maxInt(u16), + .y = std.math.maxInt(u16), + .pending_wrap = true, + .expected_x = 1, + .expected_y = 1, + .expected_pending_wrap = true, + }, + // Pending wrap away from the final column would trip native printing + // assertions, so retain the position and clear only that flag. + .{ + .x = 0, + .y = 0, + .pending_wrap = true, + .expected_x = 0, + .expected_y = 0, + .expected_pending_wrap = false, + }, + }; + + for (cases) |case| { + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + + var header = Header.init(&screen, .primary, 1); + header.cursor_x = case.x; + header.cursor_y = case.y; + header.cursor_flags.pending_wrap = case.pending_wrap; + header.saved_cursor_present = false; + + const screen_payload = stream.begin(.screen); + errdefer stream.cancel(); + try header.encode(screen_payload); + try screen_payload.writeByte(0); + try stream.finish(); + + const native_page = screen.pages.getTopLeft(.active).node + .pageAssumeResident(); + try page.encode(native_page, &stream); + + var source: std.Io.Reader = .fixed(destination.written()); + var decoded = try decode( + &source, + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer decoded.deinit(); + + try std.testing.expectEqual(case.expected_x, decoded.screen.cursor.x); + try std.testing.expectEqual(case.expected_y, decoded.screen.cursor.y); + try std.testing.expectEqual( + case.expected_pending_wrap, + decoded.screen.cursor.pending_wrap, + ); + decoded.screen.assertIntegrity(); + } +} + +test "SCREEN validates pending wrap against a mixed-width cursor page" { + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 8, .rows = 1, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + + // Model a lazily reflowed active page that is narrower than the terminal. + // Column three is its physical final column even though it is not column + // seven of the current terminal dimensions. + var narrow_page = try terminal_page.Page.init(.{ .cols = 4, .rows = 1 }); + defer narrow_page.deinit(); + + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + + var header = Header.init(&screen, .primary, 1); + header.cursor_x = 3; + header.cursor_y = 0; + header.cursor_flags.pending_wrap = true; + header.saved_cursor_present = false; + + const screen_payload = stream.begin(.screen); + errdefer stream.cancel(); + try header.encode(screen_payload); + try screen_payload.writeByte(0); + try stream.finish(); + try page.encode(&narrow_page, &stream); + + var source: std.Io.Reader = .fixed(destination.written()); + var decoded = try decode( + &source, + std.testing.io, + std.testing.allocator, + .{ .cols = 8, .rows = 1, .max_scrollback_bytes = 0 }, + ); + defer decoded.deinit(); + + try std.testing.expectEqual(@as(u16, 4), decoded.screen.cursor.page_pin.node.cols()); + try std.testing.expectEqual(@as(u16, 3), decoded.screen.cursor.x); + try std.testing.expect(decoded.screen.cursor.pending_wrap); + decoded.screen.assertIntegrity(); +} + +test "SCREEN clamps a decoded saved cursor to terminal dimensions" { + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + + var header = Header.init(&screen, .primary, 1); + header.saved_cursor_present = true; + const screen_payload = stream.begin(.screen); + try header.encode(screen_payload); + try (SavedCursor{ + .x = std.math.maxInt(u16), + .y = std.math.maxInt(u16), + .pen = .{}, + .flags = .{ .pending_wrap = true }, + .charset = .{}, + }).encode(screen_payload); + try screen_payload.writeByte(0); + try stream.finish(); + try page.encode( + screen.pages.getTopLeft(.active).node.pageAssumeResident(), + &stream, + ); + + var source: std.Io.Reader = .fixed(destination.written()); + var decoded = try decode( + &source, + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer decoded.deinit(); + + const saved = decoded.screen.saved_cursor.?; + try std.testing.expectEqual(@as(u16, 1), saved.x); + try std.testing.expectEqual(@as(u16, 1), saved.y); + try std.testing.expect(saved.pending_wrap); +} + +test "SCREEN restoration rejects invalid and incomplete sequences" { + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try encode(&screen, .primary, &stream); + + // Every truncation must fail without leaking a partially restored + // PageList, cursor pin, or cursor-owned state. + for (0..destination.written().len) |fixture_len| { + var source: std.Io.Reader = .fixed( + destination.written()[0..fixture_len], + ); + var restored = decode( + &source, + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ) catch continue; + restored.deinit(); + try std.testing.expect(false); + } + + // Native PageList construction is transactional on allocation failure. + { + var failing = std.testing.FailingAllocator.init( + std.testing.allocator, + .{ .fail_index = 0 }, + ); + var source: std.Io.Reader = .fixed(destination.written()); + try std.testing.expectError( + error.OutOfMemory, + decode( + &source, + std.testing.io, + failing.allocator(), + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ), + ); + } + + // A syntactically valid SCREEN cannot declare an empty PAGE sequence. + var empty_sequence: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer empty_sequence.deinit(); + var empty_stream: record.Writer = .init( + std.testing.allocator, + &empty_sequence.writer, + ); + defer empty_stream.deinit(); + { + const payload = empty_stream.begin(.screen); + errdefer empty_stream.cancel(); + try Header.init(&screen, .primary, 0).encode( + payload, + ); + try payload.writeByte(0); + try empty_stream.finish(); + } + var empty_source: std.Io.Reader = .fixed(empty_sequence.written()); + try std.testing.expectError( + error.InvalidPageCount, + decode( + &empty_source, + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ), + ); +} + +test "SCREEN sequence failure preserves preceding bytes" { + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + try destination.writer.writeAll("prefix"); + + var failing = std.testing.FailingAllocator.init( + std.testing.allocator, + .{ .fail_index = 0 }, + ); + var stream: record.Writer = .init( + failing.allocator(), + &destination.writer, + ); + defer stream.deinit(); + + try std.testing.expectError( + error.WriteFailed, + encode(&screen, .primary, &stream), + ); + try std.testing.expectEqualStrings("prefix", destination.written()); +} + +test "SCREEN decode ignores an invalid cursor hyperlink" { + var screen = try TerminalScreen.init( + std.testing.io, + std.testing.allocator, + .{ .cols = 1, .rows = 1, .max_scrollback_bytes = 0 }, + ); + defer screen.deinit(); + + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + + var header = testHeader(); + header.key = .primary; + header.page_count = 1; + header.cursor_x = 0; + header.cursor_y = 0; + header.cursor_flags.pending_wrap = false; + header.saved_cursor_present = false; + + const screen_payload = stream.begin(.screen); + errdefer stream.cancel(); + try header.encode(screen_payload); + try screen_payload.writeByte(1); // Implicit hyperlink. + try io.writeInt(screen_payload, u32, 1); + try io.writeInt(screen_payload, u32, 0); // Empty URI. + try stream.finish(); + + try page.encode( + screen.pages.getTopLeft(.active).node.pageAssumeResident(), + &stream, + ); + + var source: std.Io.Reader = .fixed(destination.written()); + var decoded = try decode( + &source, + std.testing.io, + std.testing.allocator, + .{ .cols = 1, .rows = 1 }, + ); + defer decoded.deinit(); + try std.testing.expectEqual(null, decoded.screen.cursor.hyperlink); +} + +test "SCREEN decode ignores a PAGE with an empty hyperlink URI" { + var destination: std.Io.Writer.Allocating = .init( + std.testing.allocator, + ); + defer destination.deinit(); + var stream: record.Writer = .init( + std.testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + + var header = testHeader(); + header.key = .primary; + header.page_count = 1; + header.cursor_x = 0; + header.cursor_y = 0; + header.cursor_flags.pending_wrap = false; + header.saved_cursor_present = false; + + const screen_payload = stream.begin(.screen); + errdefer stream.cancel(); + try header.encode(screen_payload); + try screen_payload.writeByte(0); + try stream.finish(); + + const page_header: page.Header = .{ + .columns = 1, + .rows = 1, + .style_count = 0, + .hyperlink_count = 1, + .style_capacity = 16, + .hyperlink_capacity_bytes = 512, + .grapheme_capacity_bytes = 0, + .string_capacity_bytes = 0, + }; + const page_payload = stream.begin(.page); + errdefer stream.cancel(); + try page_header.encode(page_payload); + try io.writeInt( + page_payload, + terminal_hyperlink.Id, + 1, + ); + try page_payload.writeByte(1); // Implicit hyperlink. + try io.writeInt(page_payload, u32, 1); + try io.writeInt(page_payload, u32, 0); // Empty URI. + + // One narrow codepoint cell refers to the hyperlink table entry above. + // Since that entry is ignored, the cell must restore without a hyperlink. + try page_payload.writeByte(0x30); // row flags, full cell width + try io.writeInt(page_payload, u16, 1); // cell count + try io.writeInt(page_payload, u64, @bitCast(grid.Cell{ + .content = 'A', + .hyperlink = true, + .hyperlink_id = 1, + })); + try io.writeInt(page_payload, u32, 0); // grapheme section + try stream.finish(); + + var source: std.Io.Reader = .fixed(destination.written()); + var decoded = try decode( + &source, + std.testing.io, + std.testing.allocator, + .{ .cols = 1, .rows = 1 }, + ); + defer decoded.deinit(); + + try std.testing.expectEqual( + @as(u21, 'A'), + decoded.screen.cursor.page_cell.codepoint(), + ); + try std.testing.expect(!decoded.screen.cursor.page_cell.hyperlink); + + // Resizing copies the restored cells into a wider page. The ignored + // hyperlink must not leave a zero-length PageEntry to duplicate later. + try decoded.screen.resize(.{ .cols = 2, .rows = 1 }); + try std.testing.expect(!decoded.screen.cursor.page_cell.hyperlink); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/snapshot.ksy b/vendor/libghostty-vt/src/terminal/snapshot/snapshot.ksy new file mode 100644 index 00000000..2da917ea --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/snapshot.ksy @@ -0,0 +1,1073 @@ +meta: + id: ghostty_snapshot + title: Ghostty terminal snapshot + application: Ghostty + license: MIT + endian: le +doc: | + Ghostty terminal snapshot format version 1. + + A complete snapshot contains an envelope, terminal-wide state, one or two + renderable screen sequences, one raw standard-Stream CONTINUATION, a READY + marker, matching history sequences, and a FINISH marker. SCREEN pages + are oldest-to-newest. HISTORY pages are newest-to-oldest. FINISH terminates the + snapshot; bytes that follow belong to the containing transport and are outside + this schema. Each SCREEN declares its complete logical history extent before + READY. + + Record CRC32C values are represented here but cannot be calculated by + portable Kaitai Struct expressions. The adjacent verify-kaitai.py script + validates those values after parsing. +seq: + - id: envelope + type: envelope + - id: terminal + type: terminal_record + - id: screens + type: screen_sequence + repeat: expr + repeat-expr: terminal.payload.header.screen_count + - id: continuation + type: continuation_record + - id: ready + type: checkpoint_record(5) + - id: histories + type: history_sequence + repeat: expr + repeat-expr: terminal.payload.header.screen_count + - id: finish + type: checkpoint_record(6) +enums: + record_tag: + 1: terminal + 2: screen + 3: page + 4: history + 5: ready + 6: finish + 7: continuation + screen_key: + 0: primary + 1: alternate + cursor_style: + 0: bar + 1: block + 2: underline + 3: block_hollow + status_display: + 0: main + 1: status_line + shell_redraw: + 0: full + 1: none + 2: last + mouse_event: + 0: none + 1: x10 + 2: normal + 3: button + 4: any + mouse_format: + 0: x10 + 1: utf8 + 2: sgr + 3: urxvt + 4: sgr_pixels + mouse_shape: + 0: default + 1: context_menu + 2: help + 3: pointer + 4: progress + 5: wait + 6: cell + 7: crosshair + 8: text + 9: vertical_text + 10: alias + 11: copy + 12: move + 13: no_drop + 14: not_allowed + 15: grab + 16: grabbing + 17: all_scroll + 18: col_resize + 19: row_resize + 20: n_resize + 21: e_resize + 22: s_resize + 23: w_resize + 24: ne_resize + 25: nw_resize + 26: se_resize + 27: sw_resize + 28: ew_resize + 29: ns_resize + 30: nesw_resize + 31: nwse_resize + 32: zoom_in + 33: zoom_out + protected_mode: + 0: "off" + 1: iso + 2: dec + semantic_click_kind: + 0: none + 1: click_events + 2: cl + color_kind: + 0: none + 1: palette + 2: rgb + hyperlink_kind: + 0: none + 1: implicit + 2: explicit + cell_content_kind: + 0: codepoint + 1: background_palette + 2: background_rgb + cell_width: + 0: narrow + 1: wide + 2: spacer_tail + 3: spacer_head + underline: + 0: none + 1: single + 2: double + 3: curly + 4: dotted + 5: dashed + semantic_content: + 0: output + 1: input + 2: prompt + semantic_prompt: + 0: none + 1: prompt + 2: prompt_continuation +types: + envelope: + doc: Fixed ten-byte snapshot identification and version header. + seq: + - id: magic + contents: [0x47, 0x48, 0x4f, 0x53, 0x54, 0x53, 0x4e, 0x50] + - id: version + type: u2 + valid: 1 + + record_header: + doc: | + Common record framing. crc32c covers the encoded tag, payload length, + and payload bytes, excluding the crc32c field itself. + params: + - id: expected_tag + type: u2 + seq: + - id: tag + type: u2 + valid: expected_tag + - id: payload_length + type: u4 + - id: crc32c + type: u4 + + terminal_record: + seq: + - id: header + type: record_header(1) + - id: payload + type: terminal_payload + size: header.payload_length + + screen_record: + seq: + - id: header + type: record_header(2) + - id: payload + type: screen_payload + size: header.payload_length + + page_record: + seq: + - id: header + type: record_header(3) + - id: payload + type: page_payload + size: header.payload_length + + history_record: + seq: + - id: header + type: record_header(4) + - id: payload + type: history_payload + size: header.payload_length + + continuation_record: + doc: | + Raw canonical standard TerminalStream continuation bytes. An empty + payload explicitly represents ground state. + seq: + - id: header + type: record_header(7) + - id: payload + size: header.payload_length + + checkpoint_record: + doc: READY and FINISH are empty marker records. + params: + - id: expected_tag + type: u2 + seq: + - id: header + type: record_header(expected_tag) + - id: payload + size: header.payload_length + valid: + expr: _.size == 0 + + screen_sequence: + doc: SCREEN followed by its declared PAGE records, oldest-to-newest. + seq: + - id: screen + type: screen_record + - id: pages + type: page_record + repeat: expr + repeat-expr: screen.payload.header.page_count + + history_sequence: + doc: HISTORY followed by its declared PAGE records, newest-to-oldest. + seq: + - id: history + type: history_record + - id: pages + type: page_record + repeat: expr + repeat-expr: history.payload.page_count + + terminal_payload: + seq: + - id: header + type: terminal_header + - id: tab_stops + type: tab_stops(header.columns) + - id: original_palette + type: rgb + repeat: expr + repeat-expr: 256 + - id: palette_override_mask + size: 32 + - id: palette_overrides + type: palette_override(palette_override_mask, _index) + repeat: expr + repeat-expr: 256 + - id: len_pwd + type: u4 + - id: pwd + size: len_pwd + - id: len_title + type: u4 + - id: title + size: len_title + - id: trailing_data + size-eos: true + valid: + expr: _.size == 0 + + terminal_header: + seq: + - id: columns + type: u2 + valid: + min: 1 + - id: rows + type: u2 + valid: + min: 1 + - id: width_px + type: u4 + - id: height_px + type: u4 + - id: scrolling_region_top + type: u2 + - id: scrolling_region_bottom + type: u2 + valid: + expr: _ >= scrolling_region_top and _ < rows + - id: scrolling_region_left + type: u2 + - id: scrolling_region_right + type: u2 + valid: + expr: _ >= scrolling_region_left and _ < columns + - id: status_display + type: u1 + enum: status_display + valid: + expr: _.to_i <= 1 + - id: active_screen_key + type: u2 + enum: screen_key + valid: + expr: _.to_i <= 1 + - id: screen_count + type: u2 + valid: + expr: (_ == 1 or _ == 2) and (active_screen_key.to_i == 0 or _ == 2) + - id: previous_codepoint + type: u4 + valid: + expr: _ == 0xffffffff or (_ <= 0x10ffff and not (_ >= 0xd800 and _ <= 0xdfff)) + - id: cursor_is_default + type: u1 + valid: + max: 1 + - id: cursor_default_style + type: u1 + enum: cursor_style + valid: + expr: _.to_i <= 3 + - id: cursor_default_blink + type: u1 + valid: + max: 2 + - id: shell_redraw + type: u1 + enum: shell_redraw + valid: + expr: _.to_i <= 2 + - id: modify_other_keys_2 + type: u1 + valid: + max: 1 + - id: mouse_event + type: u1 + enum: mouse_event + valid: + expr: _.to_i <= 4 + - id: mouse_format + type: u1 + enum: mouse_format + valid: + expr: _.to_i <= 4 + - id: mouse_shift_capture + type: u1 + valid: + max: 2 + - id: mouse_shape + type: u1 + enum: mouse_shape + valid: + expr: _.to_i <= 33 + - id: password_input + type: u1 + valid: + max: 1 + - id: current_modes + type: mode_set + - id: saved_modes + type: mode_set + - id: default_modes + type: mode_set + - id: background + type: dynamic_rgb + - id: foreground + type: dynamic_rgb + - id: cursor_color + type: dynamic_rgb + - id: max_scrollback_bytes + type: u8 + - id: max_scrollback_rows + type: u8 + + mode_set: + doc: | + The stable packed registry shared by current, saved, and default modes. + Each named instance exposes one bit from the little-endian integer. + Arithmetic division is used instead of bitwise operations because the + JavaScript target implements those operations with signed 32-bit values. + All values remain exact because the registry occupies only 43 bits, + within JavaScript's 53-bit safe integer range. + seq: + - id: raw + type: u8 + valid: + max: 8796093022207 + instances: + disable_keyboard: + value: (raw / 1) % 2 != 0 + insert: + value: (raw / 2) % 2 != 0 + send_receive_mode: + value: (raw / 4) % 2 != 0 + linefeed: + value: (raw / 8) % 2 != 0 + cursor_keys: + value: (raw / 16) % 2 != 0 + column_132: + value: (raw / 32) % 2 != 0 + slow_scroll: + value: (raw / 64) % 2 != 0 + reverse_colors: + value: (raw / 128) % 2 != 0 + origin: + value: (raw / 256) % 2 != 0 + wraparound: + value: (raw / 512) % 2 != 0 + autorepeat: + value: (raw / 1024) % 2 != 0 + mouse_event_x10: + value: (raw / 2048) % 2 != 0 + cursor_blinking: + value: (raw / 4096) % 2 != 0 + cursor_visible: + value: (raw / 8192) % 2 != 0 + enable_mode_3: + value: (raw / 16384) % 2 != 0 + reverse_wrap: + value: (raw / 32768) % 2 != 0 + alt_screen_legacy: + value: (raw / 65536) % 2 != 0 + keypad_keys: + value: (raw / 131072) % 2 != 0 + backarrow_key_mode: + value: (raw / 262144) % 2 != 0 + enable_left_and_right_margin: + value: (raw / 524288) % 2 != 0 + mouse_event_normal: + value: (raw / 1048576) % 2 != 0 + mouse_event_button: + value: (raw / 2097152) % 2 != 0 + mouse_event_any: + value: (raw / 4194304) % 2 != 0 + focus_event: + value: (raw / 8388608) % 2 != 0 + mouse_format_utf8: + value: (raw / 16777216) % 2 != 0 + mouse_format_sgr: + value: (raw / 33554432) % 2 != 0 + mouse_alternate_scroll: + value: (raw / 67108864) % 2 != 0 + mouse_format_urxvt: + value: (raw / 134217728) % 2 != 0 + mouse_format_sgr_pixels: + value: (raw / 268435456) % 2 != 0 + ignore_keypad_with_numlock: + value: (raw / 536870912) % 2 != 0 + alt_esc_prefix: + value: (raw / 1073741824) % 2 != 0 + alt_sends_escape: + value: (raw / 2147483648) % 2 != 0 + reverse_wrap_extended: + value: (raw / 4294967296) % 2 != 0 + alt_screen: + value: (raw / 8589934592) % 2 != 0 + save_cursor: + value: (raw / 17179869184) % 2 != 0 + alt_screen_save_cursor_clear_enter: + value: (raw / 34359738368) % 2 != 0 + bracketed_paste: + value: (raw / 68719476736) % 2 != 0 + synchronized_output: + value: (raw / 137438953472) % 2 != 0 + grapheme_cluster: + value: (raw / 274877906944) % 2 != 0 + report_color_scheme: + value: (raw / 549755813888) % 2 != 0 + report_visibility: + value: (raw / 1099511627776) % 2 != 0 + in_band_size_reports: + value: (raw / 2199023255552) % 2 != 0 + kitty_paste_events: + value: (raw / 4398046511104) % 2 != 0 + + tab_stops: + params: + - id: columns + type: u2 + seq: + - id: complete_bytes + size: columns / 8 + - id: partial_byte + type: u1 + if: columns % 8 != 0 + valid: + expr: _ < (1 << (columns % 8)) + + rgb: + seq: + - id: red + type: u1 + - id: green + type: u1 + - id: blue + type: u1 + + dynamic_rgb: + seq: + - id: default_present + type: u1 + valid: + max: 1 + - id: default_red + type: u1 + valid: + expr: default_present == 1 or _ == 0 + - id: default_green + type: u1 + valid: + expr: default_present == 1 or _ == 0 + - id: default_blue + type: u1 + valid: + expr: default_present == 1 or _ == 0 + - id: override_present + type: u1 + valid: + max: 1 + - id: override_red + type: u1 + valid: + expr: override_present == 1 or _ == 0 + - id: override_green + type: u1 + valid: + expr: override_present == 1 or _ == 0 + - id: override_blue + type: u1 + valid: + expr: override_present == 1 or _ == 0 + + palette_override: + params: + - id: mask + type: bytes + - id: index + type: u2 + seq: + - id: color + type: rgb + if: (mask[index / 8] & (1 << (index % 8))) != 0 + + screen_payload: + seq: + - id: header + type: screen_header + - id: saved_cursor + type: saved_cursor + if: header.saved_cursor_present == 1 + - id: cursor_hyperlink + type: hyperlink(true, false) + - id: trailing_data + size-eos: true + valid: + expr: _.size == 0 + + screen_header: + seq: + - id: key + type: u2 + enum: screen_key + valid: + expr: _.to_i <= 1 + - id: page_count + type: u2 + valid: + min: 1 + - id: history_rows + type: u8 + - id: cursor_x + type: u2 + - id: cursor_y + type: u2 + - id: cursor_style + type: u1 + enum: cursor_style + valid: + expr: _.to_i <= 3 + - id: cursor_flags + type: cursor_flags + - id: cursor_pen + type: style + - id: hyperlink_implicit_id + type: u4 + - id: charset + type: charset_state + - id: protected_mode + type: u1 + enum: protected_mode + valid: + expr: _.to_i <= 2 + - id: kitty_keyboard_index + type: u1 + valid: + max: 7 + - id: kitty_keyboard_flags + type: kitty_keyboard_flags + repeat: expr + repeat-expr: 8 + - id: semantic_click_kind + type: u1 + enum: semantic_click_kind + valid: + expr: _.to_i <= 2 + - id: semantic_click_value + type: u1 + valid: + expr: | + semantic_click_kind.to_i == 0 ? _ == 0 : + semantic_click_kind.to_i == 1 ? _ <= 1 : + _ <= 3 + - id: saved_cursor_present + type: u1 + valid: + max: 1 + + cursor_flags: + seq: + - id: raw + type: u1 + valid: + expr: (_ & 0xe0) == 0 and ((_ >> 2) & 0x3) <= 2 + instances: + pending_wrap: + value: (raw & (1 << 0)) != 0 + protected: + value: (raw & (1 << 1)) != 0 + semantic_content: + value: (raw >> 2) & 0x3 + semantic_content_clear_eol: + value: (raw & (1 << 4)) != 0 + + charset_state: + doc: | + Four selected character sets, the GL and GR slots, and an optional + single-shift slot. single_shift is zero for none or one plus a slot. + seq: + - id: raw + type: u2 + valid: + expr: (_ & 0x8000) == 0 and ((_ >> 12) & 0x7) <= 4 + instances: + g0: + value: (raw >> 0) & 0x3 + g1: + value: (raw >> 2) & 0x3 + g2: + value: (raw >> 4) & 0x3 + g3: + value: (raw >> 6) & 0x3 + gl: + value: (raw >> 8) & 0x3 + gr: + value: (raw >> 10) & 0x3 + single_shift: + value: (raw >> 12) & 0x7 + + kitty_keyboard_flags: + seq: + - id: raw + type: u1 + valid: + expr: (_ & 0xe0) == 0 + instances: + disambiguate: + value: (raw & (1 << 0)) != 0 + report_events: + value: (raw & (1 << 1)) != 0 + report_alternates: + value: (raw & (1 << 2)) != 0 + report_all: + value: (raw & (1 << 3)) != 0 + report_associated: + value: (raw & (1 << 4)) != 0 + + saved_cursor: + seq: + - id: x + type: u2 + - id: y + type: u2 + - id: pen + type: style + - id: flags + type: saved_cursor_flags + - id: charset + type: charset_state + + saved_cursor_flags: + seq: + - id: raw + type: u1 + valid: + expr: (_ & 0xf8) == 0 + instances: + protected: + value: (raw & (1 << 0)) != 0 + pending_wrap: + value: (raw & (1 << 1)) != 0 + origin: + value: (raw & (1 << 2)) != 0 + + history_payload: + seq: + - id: key + type: u2 + enum: screen_key + valid: + expr: _.to_i <= 1 + - id: page_count + type: u4 + - id: trailing_data + size-eos: true + valid: + expr: _.size == 0 + + page_payload: + seq: + - id: header + type: page_header + - id: styles + type: style_table_entry + repeat: expr + repeat-expr: header.style_count + - id: hyperlinks + type: hyperlink_table_entry + repeat: expr + repeat-expr: header.hyperlink_count + - id: grid + type: grid(header.rows, header.columns) + - id: trailing_data + size-eos: true + valid: + expr: _.size == 0 + + page_header: + seq: + - id: columns + type: u2 + valid: + min: 1 + - id: rows + type: u2 + valid: + min: 1 + - id: style_count + type: u2 + - id: hyperlink_count + type: u2 + - id: style_capacity + type: u2 + - id: hyperlink_capacity_bytes + type: u2 + - id: grapheme_capacity_bytes + type: u4 + - id: string_capacity_bytes + type: u4 + + style_table_entry: + seq: + - id: encoded_id + type: u2 + valid: + min: 1 + - id: value + type: style + + hyperlink_table_entry: + seq: + - id: encoded_id + type: u2 + valid: + min: 1 + - id: value + type: hyperlink(false, true) + + style: + seq: + - id: foreground + type: style_color + - id: background + type: style_color + - id: underline_color + type: style_color + - id: flags + type: style_flags + - id: reserved + type: u2 + valid: 0 + + style_flags: + seq: + - id: raw + type: u2 + valid: + expr: (_ & 0xf800) == 0 and ((_ >> 8) & 0x7) <= 5 + instances: + bold: + value: (raw & (1 << 0)) != 0 + italic: + value: (raw & (1 << 1)) != 0 + faint: + value: (raw & (1 << 2)) != 0 + blink: + value: (raw & (1 << 3)) != 0 + inverse: + value: (raw & (1 << 4)) != 0 + invisible: + value: (raw & (1 << 5)) != 0 + strikethrough: + value: (raw & (1 << 6)) != 0 + overline: + value: (raw & (1 << 7)) != 0 + underline: + value: (raw >> 8) & 0x7 + + style_color: + seq: + - id: kind + type: u1 + enum: color_kind + valid: + expr: _.to_i <= 2 + - id: first + type: u1 + valid: + expr: kind.to_i != 0 or _ == 0 + - id: second + type: u1 + valid: + expr: kind.to_i == 2 or _ == 0 + - id: third + type: u1 + valid: + expr: kind.to_i == 2 or _ == 0 + + hyperlink: + params: + - id: allow_none + type: bool + - id: allow_empty + type: bool + seq: + - id: kind + type: u1 + enum: hyperlink_kind + valid: + expr: _.to_i <= 2 and (allow_none or _.to_i != 0) + - id: implicit + type: implicit_hyperlink(allow_empty) + if: kind.to_i == 1 + - id: explicit + type: explicit_hyperlink(allow_empty) + if: kind.to_i == 2 + + implicit_hyperlink: + params: + - id: allow_empty + type: bool + seq: + - id: id + type: u4 + - id: len_uri + type: u4 + valid: + expr: allow_empty or _ >= 1 + - id: uri + size: len_uri + + explicit_hyperlink: + params: + - id: allow_empty + type: bool + seq: + - id: len_id + type: u4 + valid: + expr: allow_empty or _ >= 1 + - id: id + size: len_id + - id: len_uri + type: u4 + valid: + expr: allow_empty or _ >= 1 + - id: uri + size: len_uri + + grid: + params: + - id: num_rows + type: u2 + - id: columns + type: u2 + seq: + - id: rows + type: grid_row(columns) + repeat: expr + repeat-expr: num_rows + - id: num_grapheme_entries + type: u4 + - id: grapheme_entries + type: grapheme_entry(num_rows, columns) + repeat: expr + repeat-expr: num_grapheme_entries + + grid_row: + params: + - id: columns + type: u2 + seq: + - id: flags + type: grid_row_flags + - id: cell_count + type: u2 + valid: + expr: _ <= columns + - id: cells + type: + switch-on: flags.width_log2 + cases: + 0: grid_cell_1 + 1: grid_cell_2 + 2: grid_cell_4 + 3: grid_cell(_index, columns, flags.wrap) + repeat: expr + repeat-expr: cell_count + + grid_row_flags: + seq: + - id: raw + type: u1 + valid: + expr: (_ & 0xc0) == 0 and ((_ >> 2) & 0x3) <= 2 + instances: + wrap: + value: (raw & 1) != 0 + wrap_continuation: + value: (raw & 2) != 0 + semantic_prompt: + value: (raw >> 2) & 0x3 + width_log2: + value: (raw >> 4) & 0x3 + + grid_cell_1: + doc: One-byte encoded cell; the value is a codepoint at or below U+00FF. + seq: + - id: codepoint + type: u1 + + grid_cell_2: + doc: | + Two-byte encoded cell; the value is a codepoint at or below U+FFFF. + Canonical encoders never emit surrogates. + seq: + - id: codepoint + type: u2 + valid: + expr: not (_ >= 0xd800 and _ <= 0xdfff) + + grid_cell_4: + doc: | + Four-byte encoded cell holding the low half of the cell word: any + content kind and codepoint, style IDs one through sixty-three, and no + width, flag, or hyperlink bits. + seq: + - id: raw + type: u4 + valid: + expr: | + (content_kind >= 2 or + (content <= 0x10ffff and + not (content >= 0xd800 and content <= 0xdfff))) and + (content_kind != 2 or content <= 0xff) + instances: + content_kind: + value: raw % 4 + content: + value: (raw / 4) % 16777216 + style_id: + value: raw / 67108864 + + grid_cell: + doc: | + One 64-bit little-endian cell word. The word is parsed as two 32-bit + halves so every derived field stays within JavaScript's safe integer + range, following the same approach as mode_set. + + Canonical rules validated here: reserved semantic content is not + emitted, the hyperlink flag matches a nonzero hyperlink ID, codepoint + content is a Unicode scalar value, palette content uses only its low + eight bits, and spacer relationships match the preceding cell. Wide + markers cannot be validated forward because their spacer tail may be + the next cell or the implicit narrow cell after a short row. + params: + - id: index + type: u2 + - id: columns + type: u2 + - id: row_wrap + type: bool + seq: + - id: lo + type: u4 + - id: hi + type: u4 + valid: + expr: | + semantic_content <= 2 and + hyperlink == (hyperlink_id != 0) and + (content_kind >= 2 or + (content <= 0x10ffff and + not (content >= 0xd800 and content <= 0xdfff))) and + (content_kind != 2 or content <= 0xff) and + (width != 2 or + (index > 0 and + _parent.cells[index - 1].as.width == 1)) and + (width != 3 or (index + 1 == columns and row_wrap)) + instances: + content_kind: + value: lo % 4 + content: + value: (lo / 4) % 16777216 + style_id: + value: (lo / 67108864) + (hi % 1024) * 64 + width: + value: (hi / 1024) % 4 + protected: + value: (hi / 4096) % 2 != 0 + hyperlink: + value: (hi / 8192) % 2 != 0 + semantic_content: + value: (hi / 16384) % 4 + hyperlink_id: + value: hi / 65536 + + grapheme_entry: + seq: + - id: row + type: u2 + valid: + expr: _ < num_rows + - id: col + type: u2 + valid: + expr: _ < columns + - id: num_codepoints + type: u2 + valid: + min: 1 + - id: codepoints + type: u4 + repeat: expr + repeat-expr: num_codepoints + valid: + expr: _ <= 0x10ffff and not (_ >= 0xd800 and _ <= 0xdfff) + params: + - id: num_rows + type: u2 + - id: columns + type: u2 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/snapshot.zig b/vendor/libghostty-vt/src/terminal/snapshot/snapshot.zig new file mode 100644 index 00000000..9c6dc96d --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/snapshot.zig @@ -0,0 +1,2100 @@ +//! Complete terminal snapshot encoding and restoration. + +const std = @import("std"); +const build_options = @import("terminal_options"); +const assert = std.debug.assert; +const Allocator = std.mem.Allocator; +const checkpoint = @import("checkpoint.zig"); +const continuation = @import("continuation.zig"); +const envelope = @import("envelope.zig"); +const test_fixture = @import("fixture.zig"); +const history = @import("history.zig"); +const page = @import("page.zig"); +const record = @import("record.zig"); +const screen = @import("screen.zig"); +const size = @import("../size.zig"); +const terminal = @import("terminal.zig"); +const Terminal = @import("../Terminal.zig"); +const TerminalStream = @import("../stream_terminal.zig").Stream; +const terminal_kitty = @import("../kitty.zig"); +const TerminalPageList = @import("../PageList.zig"); +const TerminalScreen = @import("../Screen.zig"); +const TerminalScreenKey = @import("../ScreenSet.zig").Key; + +/// Re-export continuation to make it a bit more ergonomic to reference. +pub const Continuation = continuation.Value; + +/// Errors possible while encoding one complete terminal snapshot. +pub const EncodeError = terminal.EncodeError || + screen.EncodeError || + history.EncodeError || + checkpoint.EncodeError || + continuation.EncodeError; + +pub const EncodeOptions = struct { + continuation: Continuation, +}; + +/// Encode one complete terminal snapshot. +/// +/// Encoding starts at the destination's current position. Only one record +/// payload is buffered at a time; completed records stream immediately. On +/// failure, the destination may contain a snapshot prefix without its required +/// FINISH marker, and an output failure may have written part of a record. +pub fn encode( + alloc: Allocator, + destination: *std.Io.Writer, + t: *const Terminal, + options: EncodeOptions, +) EncodeError!void { + // Continuation errors must not emit even the snapshot envelope. + try continuation.validate(options.continuation); + + // A measured 512-byte buffer handles our smallest records on the stack + // before larger records fall back to the heap. + var stack_alloc = std.heap.stackFallback(512, alloc); + var stream: record.Writer = .init(stack_alloc.get(), destination); + defer stream.deinit(); + + // 1. Envelope + try envelope.encode(stream.writer()); + + // 2. Terminal + try terminal.encode(t, &stream); + + // 3. Primary and alt screen + try screen.encode( + t.screens.get(.primary).?, + .primary, + &stream, + ); + if (t.screens.get(.alternate)) |alternate| try screen.encode( + alternate, + .alternate, + &stream, + ); + + // 4. Standard Stream continuation. + try continuation.encode(options.continuation, &stream); + + // 5. Ready marker. + try checkpoint.encode(.ready, &stream); + + // 6. History + try history.encode( + t.screens.get(.primary).?, + .primary, + &stream, + ); + if (t.screens.get(.alternate)) |alternate| try history.encode( + alternate, + .alternate, + &stream, + ); + + // 7. Finish + try checkpoint.encode(.finish, &stream); +} + +/// Errors possible while restoring one complete terminal snapshot. +pub const DecodeError = Decoder.ReadyError || Decoder.NextError; + +pub const DecodeOptions = struct { + /// Largest non-ground continuation the decoder may allocate and return. + /// Set this to zero when only ground-state snapshots are acceptable. + max_continuation_bytes: usize, +}; + +/// One decoded Terminal and the bytes needed to resume its Stream. +/// +/// `decode` returns this with all history present; `Decoder.ready` returns +/// it at READY, when the terminal is complete for interactive use but its +/// scrollback is still arriving through `Decoder.next`. +/// +/// A successful decode owns both values. Keep this result alive until the +/// Terminal's Stream has been restored, and always finish by calling `deinit`. +/// The usual restoration sequence is: +/// +/// 1. Inspect `continuation` and use its length to size continuation tracking. +/// 2. Call `toOwned` once and store the returned Terminal at its final address. +/// 3. Create the persistent, read-only standard TerminalStream for that +/// address. If `continuation` contains bytes, feed them exactly once and +/// verify that re-exporting the continuation returns the same bytes. +/// 4. Call `deinit` to release the decoded continuation. The transferred +/// Terminal and its Stream remain owned by the caller. +/// 5. Process PTY bytes that came after the snapshot cut. +/// +/// Do not create a Stream against the address of `terminal` in this struct. +/// `toOwned` moves the Terminal, which would leave such a Stream pointing at +/// its old address. +pub const Decoded = struct { + /// Present until `toOwned` transfers the Terminal to the caller. Callers + /// may inspect it, but must transfer it before attaching a persistent + /// TerminalStream. + terminal: ?Terminal, + + /// For decoded results, nonempty bytes are allocator-owned and remain + /// valid until `deinit`. Ground needs no replay. Bytes must be replayed + /// exactly once after the Terminal has reached its final address. + continuation: Continuation, + + /// Advisory complete logical history extent declared by each restored + /// screen, including the resident overlap already carried by that + /// screen's own pages. A client may size scrollback UI from this at + /// READY, before HISTORY pages arrive; native row totals always remain + /// derived from the pages actually applied. + history_rows: std.EnumMap(TerminalScreenKey, u64), + + /// Destroy an untransferred Terminal and always free continuation bytes. + /// After `toOwned`, this leaves the caller-owned Terminal untouched. + pub fn deinit(self: *Decoded, alloc: Allocator) void { + if (self.terminal) |*value| value.deinit(alloc); + switch (self.continuation) { + .ground => {}, + .bytes => |bytes| alloc.free(bytes), + } + self.* = undefined; + } + + /// Transfer the Terminal while retaining the continuation for replay. + /// + /// This may be called exactly once. Store the returned value directly at + /// its final address before creating the TerminalStream that will replay + /// `continuation`. + pub fn toOwned(self: *Decoded) Terminal { + const result = self.terminal.?; + self.terminal = null; + return result; + } +}; + +/// Incrementally restore one snapshot: renderable state first, then history. +/// +/// The snapshot record order makes a terminal renderable at the READY +/// marker, after only a small prefix of the stream. `Decoder` exposes +/// that boundary: `ready` returns a complete, integrity-checked `Decoded` while +/// scrollback is still in flight, and each `next` call then applies one +/// history PAGE record off the critical path. `snapshot.decode` is the +/// one-shot form and is implemented over this type. +/// +/// ```zig +/// var decoder: snapshot.Decoder = .init(&reader); +/// var decoded = try decoder.ready(alloc, io, .{ +/// .max_continuation_bytes = 1024 * 1024, +/// }); +/// defer decoded.deinit(alloc); +/// +/// var terminal = decoded.toOwned(); +/// defer terminal.deinit(alloc); +/// // The terminal is usable now: restore its Stream from the decoded +/// // continuation and size scrollback UI from `decoded.history_rows`. +/// +/// while (try decoder.next(alloc, &terminal)) |progress| { +/// _ = progress; // Scrollback grew; e.g. refresh the scrollbar. +/// } +/// // FINISH validated: the complete snapshot has been decoded. +/// ``` +/// +/// The terminal is live between `next` calls: the caller may render it and +/// even feed it PTY bytes that arrived after the snapshot cut. `next` +/// re-validates its destination on every call and degrades to discarding, +/// consuming and validating wire bytes without applying them, when live +/// changes have made a page inapplicable. See `next` for the drop rules. +/// +/// The decoder owns no allocations and needs no deinit. It retains the +/// source pointer given to `init`; the source must stay valid, and must not +/// be read elsewhere, until decoding ends. The decoder itself is movable +/// between calls. After any returned error both the decoder and the stream +/// position are invalid: abandon the decoder, and destroy an already +/// transferred Terminal or keep it with partial history, but do not resume. +pub const Decoder = struct { + source: *std.Io.Reader, + state: State, + + const State = union(enum) { + /// `ready` has not been called. + start, + + /// READY validated; HISTORY sequences remain before FINISH. + history: History, + + /// FINISH validated. `next` returns null. + finished, + + /// A call returned an error; the stream position is unknown. + failed, + }; + + const History = struct { + /// The generation observed at READY for every screen the snapshot + /// declared. Presence is the declaration check for HISTORY routing; + /// the value detects screens removed or replaced by live terminal + /// use between calls. + generations: std.EnumMap(TerminalScreenKey, usize), + + /// Keys whose HISTORY sequence was already consumed. + routed: std.EnumSet(TerminalScreenKey), + + /// HISTORY sequences that have not begun streaming yet. + pending: usize, + + /// The sequence currently streaming PAGE records. + current: ?Sequence, + + /// Terminal width at READY. Encoded pages assume this width and are + /// dropped rather than mixed into a screen reflowed to another. + cols: size.CellCountInt, + }; + + const Sequence = struct { + key: TerminalScreenKey, + + /// PAGE records not yet consumed from this sequence. + remaining: u32, + + /// False once any page in this sequence was dropped. Later pages + /// are older, and applying one above a gap would corrupt scrollback + /// order, so a single drop discards the remainder of the sequence. + apply: bool, + }; + + /// The decoder begins reading only when `ready` is called. + pub fn init(source: *std.Io.Reader) Decoder { + return .{ + .source = source, + .state = .start, + }; + } + + /// Errors possible while decoding the renderable prefix through READY. + pub const ReadyError = envelope.DecodeError || + terminal.DecodeError || + screen.DecodeError || + continuation.DecodeError || + checkpoint.DecodeError || + error{ + /// A SCREEN names a key not declared by TERMINAL. + UnexpectedScreenKey, + + /// More than one SCREEN names the same key. + DuplicateScreen, + }; + + /// Decode through the READY marker and return the renderable state. + /// + /// The result is transactional and complete for interactive use: the + /// Terminal, its continuation, and the advisory history extents are all + /// validated through READY. Scrollback history is not present yet and + /// arrives through `next`. Asserts this is the first call. + pub fn ready( + self: *Decoder, + alloc: Allocator, + io_: std.Io, + options: DecodeOptions, + ) ReadyError!Decoded { + assert(self.state == .start); + errdefer self.state = .failed; + const reader = self.source; + + // Read the envelope, which is currently just a verification step. + try envelope.decode(reader); + + // TERMINAL establishes terminal-wide state and allocates empty + // screen slots with their final routing. SCREEN values replace those + // slots in place so ScreenSet pointers, including the active + // pointer, stay valid. + var result = try terminal.decode(reader, io_, alloc); + errdefer result.deinit(alloc); + + // TERMINAL initializes exactly the number of screen slots it + // declared. Decode that many SCREEN sequences and route each one by + // its encoded key. + const screen_count = result.screens.all.count(); + const screen_options: TerminalScreen.Options = options: { + const primary = result.screens.get(.primary).?; + const explicit_bytes = primary.pages.limits.bytes.explicit; + const explicit_lines = primary.pages.limits.lines.explicit; + break :options .{ + .cols = result.cols, + .rows = result.rows, + .max_scrollback_bytes = if (explicit_bytes == std.math.maxInt(usize)) + null + else + explicit_bytes, + .max_scrollback_lines = if (explicit_lines == std.math.maxInt(usize)) + null + else + explicit_lines, + }; + }; + + var routed: std.EnumSet(TerminalScreenKey) = .initEmpty(); + var history_rows: std.EnumMap(TerminalScreenKey, u64) = .init(.{}); + for (0..screen_count) |_| { + var decoded = try screen.decode( + reader, + io_, + alloc, + screen_options, + ); + errdefer decoded.deinit(); + + const slot = result.screens.get(decoded.key) orelse + return error.UnexpectedScreenKey; + if (routed.contains(decoded.key)) return error.DuplicateScreen; + routed.insert(decoded.key); + history_rows.put(decoded.key, decoded.history_rows); + + slot.deinit(); + slot.* = decoded.screen; + decoded.screen = undefined; + } + + // CONTINUATION is required after every active screen. Nonempty bytes + // are owned locally until the READY prefix validates. + const decoded_continuation = try continuation.decode( + alloc, + reader, + options.max_continuation_bytes, + ); + errdefer switch (decoded_continuation) { + .ground => {}, + .bytes => |bytes| alloc.free(bytes), + }; + + // READY marks the exact envelope-through-CONTINUATION boundary. + try checkpoint.decode(.ready, self.source); + + // Record where history may be applied. The declared keys route the + // HISTORY sequences, and the generations detect declared screens + // that live terminal use destroys or replaces before their history + // arrives. + var generations: std.EnumMap(TerminalScreenKey, usize) = .init(.{}); + var it = result.screens.all.iterator(); + while (it.next()) |entry| generations.put( + entry.key, + result.screens.generation(entry.key), + ); + + self.state = .{ .history = .{ + .generations = generations, + .routed = .initEmpty(), + .pending = screen_count, + .current = null, + .cols = result.cols, + } }; + return .{ + .terminal = result, + .continuation = decoded_continuation, + .history_rows = history_rows, + }; + } + + /// One history PAGE record consumed by `next`. + pub const Progress = struct { + /// The screen whose HISTORY sequence contained the decoded page. + key: TerminalScreenKey, + + /// Rows prepended above that screen's existing content, or zero + /// when the page was consumed and validated but dropped. + rows: usize, + + /// PAGE records still pending in the same HISTORY sequence. + remaining: u32, + }; + + /// Errors possible while decoding history records through FINISH. + pub const NextError = checkpoint.DecodeError || + history.Decoder.InitError || + page.DiscardError || + page.DecodeError || + Allocator.Error || + error{ + /// `next` was called before `ready` completed successfully. + DecoderNotReady, + + /// A prior `next` call failed and invalidated the stream position. + DecoderFailed, + + /// A HISTORY names a key not declared by TERMINAL. + UnexpectedHistoryKey, + + /// More than one HISTORY names the same key. + DuplicateHistory, + + /// A history PAGE cannot join a native PageList. + /// These propagate from `nextPage`'s non-limit finalize errors; + /// only limit failures are deliberately converted into drops. + InvalidPageDimensions, + RowCountOverflow, + PageSizeOverflow, + }; + + /// Decode one history PAGE record and prepend it to its screen in `t`. + /// + /// `t` must be the Terminal restored by this decoder's `ready` call, + /// wherever the caller now stores it. Returns null once FINISH has + /// validated, which completes the snapshot; the source is + /// left positioned exactly after it. Further calls return null. + /// + /// Wire validation stays strict: framing, CRCs, record order, routing, + /// and both marker records are always enforced. Application is + /// forgiving, because the terminal is live and may have changed since + /// READY. A page is consumed but dropped, reported as zero rows, when: + /// + /// * its screen was removed, or removed and recreated, since READY + /// (detected through ScreenSet generations); + /// * the terminal was resized since READY, since encoded pages assume + /// the encoded width; + /// * prepending it would exceed the screen's scrollback limits, e.g. + /// because post-cut output already consumed that budget, making + /// immediate eviction the only alternative. + /// + /// Once a page in a sequence is dropped, the rest of that sequence is + /// discarded too: later pages are older, and applying one above a gap + /// would corrupt scrollback ordering. History for other screens is + /// unaffected. + pub fn next( + self: *Decoder, + alloc: Allocator, + t: *Terminal, + ) NextError!?Progress { + switch (self.state) { + // `ready` must succeed before history exists to decode, and a + // failed decoder no longer knows its stream position. + .start => return error.DecoderNotReady, + .failed => return error.DecoderFailed, + .finished => return null, + .history => {}, + } + errdefer self.state = .failed; + const state = &self.state.history; + + while (true) { + // Stream the current sequence's next PAGE if one remains. + if (state.current) |*sequence| { + if (sequence.remaining > 0) { + return try self.nextPage(alloc, t, sequence); + } + state.current = null; + } + + // All sequences are consumed: FINISH ends the snapshot, leaving + // trailing transport bytes unread. + if (state.pending == 0) { + try checkpoint.decode(.finish, self.source); + if (comptime build_options.slow_runtime_safety) { + var it = state.generations.iterator(); + while (it.next()) |entry| { + const restored = t.screens.get(entry.key) orelse + continue; + restored.pages.assertIntegrity(); + restored.assertIntegrity(); + } + } + self.state = .finished; + return null; + } + + // Begin the next HISTORY sequence. Keys route the sequences + // order-independently, but each must name a declared screen and + // may not repeat. + state.pending -= 1; + var manifest: history.Decoder = undefined; + try manifest.init(self.source); + const key = manifest.header.key; + if (state.generations.get(key) == null) { + return error.UnexpectedHistoryKey; + } + if (state.routed.contains(key)) return error.DuplicateHistory; + state.routed.insert(key); + state.current = .{ + .key = key, + .remaining = manifest.header.page_count, + .apply = true, + }; + } + } + + /// Consume one PAGE record from the current sequence, applying it to + /// the live terminal when it is still applicable and discarding it + /// otherwise. + fn nextPage( + self: *Decoder, + alloc: Allocator, + t: *Terminal, + sequence: *Sequence, + ) NextError!Progress { + const state = &self.state.history; + + // Application is re-decided on every page so live terminal changes + // between calls degrade to discarding instead of failing decoding. + const destination: ?*TerminalScreen = destination: { + if (!sequence.apply) break :destination null; + if (t.cols != state.cols) break :destination null; + const restored = t.screens.get(sequence.key) orelse + break :destination null; + if (t.screens.generation(sequence.key) != + state.generations.get(sequence.key).?) + { + break :destination null; + } + break :destination restored; + }; + + sequence.remaining -= 1; + const rows: usize = rows: { + const restored = destination orelse { + // The record must still be consumed so decoding remains + // aligned with the following records and FINISH marker. + sequence.apply = false; + try page.discard(self.source); + break :rows 0; + }; + // The one-shot history decoder rejects a Screen which already has + // complete history. This incremental path intentionally bypasses + // that guard: the terminal has been live since READY and may have + // accumulated new history. Generation and width checks above keep + // the destination compatible, while per-page finalization enforces + // its current scrollback limits. + break :rows history.decodePage( + self.source, + alloc, + restored, + ) catch |err| switch (err) { + // The page no longer fits the screen's scrollback limits, + // e.g. because post-cut output consumed the budget, so it + // would be evicted immediately anyway. Its record was fully + // consumed, leaving the stream aligned on the next record. + error.MaxSizeExceeded, + error.MaxLinesExceeded, + => drop: { + sequence.apply = false; + break :drop 0; + }, + else => |other| return other, + }; + }; + + return .{ + .key = sequence.key, + .rows = rows, + .remaining = sequence.remaining, + }; + } +}; + +/// Restore one complete snapshot into a native Terminal and continuation. +/// +/// This consumes one snapshot through FINISH and leaves any following bytes +/// in the reader for the containing transport. Restoration is transactional: +/// the returned result is either complete and ready or not (error return). +/// Individual record codecs normalize optional semantic state, and history +/// beyond the declared scrollback limits is dropped rather than rejected, +/// while framing, markers, declared sequence counts, and unique +/// cross-record screen routing remain strict. +/// +/// This is the one-shot form of `Decoder`, which can additionally hand the +/// renderable terminal to the caller at READY, before history arrives. +pub fn decode( + alloc: Allocator, + io_: std.Io, + source: *std.Io.Reader, + options: DecodeOptions, +) DecodeError!Decoded { + var decoder: Decoder = .init(source); + var result = try decoder.ready(alloc, io_, options); + errdefer result.deinit(alloc); + + // The Terminal has not been transferred, so history applies in place. + const t = &result.terminal.?; + while (try decoder.next(alloc, t)) |_| {} + return result; +} + +/// Errors possible while restoring a snapshot that must end at end-of-file. +pub const DecodeExactError = DecodeError || std.Io.Reader.Error || error{ + /// FINISH was followed by additional bytes. + TrailingData, +}; + +/// Restore one snapshot and require FINISH to be followed by end-of-file. +/// +/// This is intended for bounded snapshot files and buffers. On a live stream, +/// checking for end-of-file may block; use `decode` to stop at FINISH instead. +pub fn decodeExact( + alloc: Allocator, + io_: std.Io, + source: *std.Io.Reader, + options: DecodeOptions, +) DecodeExactError!Decoded { + var result = try decode(alloc, io_, source, options); + errdefer result.deinit(alloc); + + _ = source.peekByte() catch |err| switch (err) { + error.EndOfStream => return result, + else => return err, + }; + return error.TrailingData; +} + +const test_encode_options: EncodeOptions = .{ .continuation = .ground }; +const test_decode_options: DecodeOptions = .{ .max_continuation_bytes = 1024 }; +const test_complete_fixture = test_fixture.parse(@embedFile("testdata/complete-v1.hex")); + +/// Build a 2x3 test terminal whose primary screen carries `history_pages` +/// complete two-row pages above a three-row active page. The top-left cell +/// of each page is marked 'A', 'B', ... from oldest history to active. +fn testHistoryTerminal(history_pages: u8) !Terminal { + const testing = std.testing; + var t = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 2, + .rows = 3, + .max_scrollback_bytes = null, + .max_scrollback_lines = null, + }); + errdefer t.deinit(testing.allocator); + + const primary = t.screens.get(.primary).?; + var replacement: TerminalScreen = replacement: { + var builder = try TerminalPageList.Builder.init( + testing.allocator, + .{ + .cols = t.cols, + .rows = t.rows, + .max_size = null, + .max_lines = null, + }, + ); + defer builder.deinit(); + + for (0..history_pages) |index| { + const built = try builder.allocatePage(.{ .cols = 2, .rows = 2 }); + built.size.rows = 2; + built.getRowAndCell(0, 0).cell.* = .init( + @intCast('A' + index), + ); + } + const active = try builder.allocatePage(.{ .cols = 2, .rows = 3 }); + active.size.rows = 3; + active.getRowAndCell(0, 0).cell.* = .init( + @intCast('A' + @as(usize, history_pages)), + ); + + var pages = try builder.finish(); + errdefer pages.deinit(); + const cursor_pin = try pages.trackPin( + pages.pin(.{ .active = .{} }).?, + ); + const cursor_rac = cursor_pin.rowAndCell(); + break :replacement .{ + .io = testing.io, + .alloc = testing.allocator, + .pages = pages, + .cursor = .{ + .page_pin = cursor_pin, + .page_row = cursor_rac.row, + .page_cell = cursor_rac.cell, + }, + }; + }; + primary.deinit(); + primary.* = replacement; + replacement = undefined; + return t; +} + +/// The top-left screen cell, which lands in the oldest applied history page. +fn testTopLeftCodepoint(terminal_screen: *const TerminalScreen) u21 { + return terminal_screen.pages.getTopLeft(.screen).node + .page().getRowAndCell(0, 0).cell.codepoint(); +} + +test "complete snapshot round trip with history and alternate screen" { + const testing = std.testing; + + var t = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 2, + .rows = 3, + .max_scrollback_bytes = null, + .max_scrollback_lines = null, + }); + defer t.deinit(testing.allocator); + + // Exercise terminal-wide state. + t.width_px = 800; + t.height_px = 600; + t.colors.palette.set(7, .{ .r = 1, .g = 2, .b = 3 }); + t.modes.values.bracketed_paste = true; + try t.setPwd("file:///tmp/snapshot"); + try t.setTitle("complete snapshot"); + + const primary = t.screens.get(.primary).?; + + // Use small exact capacities so this compound golden remains practical to + // review while still containing two complete history pages and one active + // page. Replacing the Screen in place preserves ScreenSet routing. + var replacement: TerminalScreen = replacement: { + var builder = try TerminalPageList.Builder.init( + testing.allocator, + .{ + .cols = t.cols, + .rows = t.rows, + .max_size = null, + .max_lines = null, + }, + ); + defer builder.deinit(); + + const oldest = try builder.allocatePage(.{ .cols = 2, .rows = 2 }); + oldest.size.rows = 2; + oldest.getRowAndCell(0, 0).cell.* = .init('A'); + + const recent = try builder.allocatePage(.{ .cols = 2, .rows = 2 }); + recent.size.rows = 2; + recent.getRowAndCell(0, 0).cell.* = .init('B'); + + const active = try builder.allocatePage(.{ .cols = 2, .rows = 3 }); + active.size.rows = 3; + active.getRowAndCell(0, 0).cell.* = .init('C'); + active.getRowAndCell(0, 1).cell.* = .init('D'); + active.getRowAndCell(0, 2).cell.* = .init('E'); + + var pages = try builder.finish(); + errdefer pages.deinit(); + + const cursor_pin = try pages.trackPin( + pages.pin(.{ .active = .{} }).?, + ); + const cursor_rac = cursor_pin.rowAndCell(); + break :replacement .{ + .io = testing.io, + .alloc = testing.allocator, + .pages = pages, + .cursor = .{ + .page_pin = cursor_pin, + .page_row = cursor_rac.row, + .page_cell = cursor_rac.cell, + }, + }; + }; + primary.deinit(); + primary.* = replacement; + replacement = undefined; + + try testing.expect(primary.pages.scrollbar().total > t.rows); + + // Compression is an internal source representation and must remain + // unchanged while the complete history is inspected for encoding. + _ = primary.pages.compress(.full); + const source_memory = primary.pages.memoryStats(); + + // The optional alternate screen participates in both phases and remains + // the active screen after restoration. + _ = try t.switchScreen(.alternate); + try t.printString("alternate"); + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + try testing.expectEqualDeep(source_memory, primary.pages.memoryStats()); + try test_fixture.expectEqual( + .snapshot, + "src/terminal/snapshot/testdata/complete-v1.hex", + "snapshot_fixture-complete-v1.hex", + &test_complete_fixture, + encoded.written(), + ); + + // A complete snapshot can stream through a non-allocating destination. + // Independently hash that output so both its length and complete byte + // sequence are checked without retaining a second snapshot copy. + var discard: std.Io.Writer.Discarding = .init(&.{}); + var hashing = discard.writer.hashed(record.Crc32c.init(), &.{}); + try encode(testing.allocator, &hashing.writer, &t, test_encode_options); + try testing.expectEqual( + @as(u64, test_complete_fixture.len), + discard.fullCount(), + ); + try testing.expectEqual( + record.Crc32c.hash(&test_complete_fixture), + hashing.hasher.final(), + ); + + // Restore the checked-in reference rather than the just-generated bytes. + var encoded_source: std.Io.Reader = .fixed(&test_complete_fixture); + var source_buffer: [1]u8 = undefined; + var limited = encoded_source.limited(.unlimited, &source_buffer); + var restored = try decode( + testing.allocator, + testing.io, + &limited.interface, + test_decode_options, + ); + defer restored.deinit(testing.allocator); + const restored_terminal = &restored.terminal.?; + + try testing.expectEqual( + TerminalScreenKey.alternate, + restored_terminal.screens.active_key, + ); + try testing.expectEqual( + restored_terminal.screens.get(.alternate).?, + restored_terminal.screens.active, + ); + try testing.expectEqualStrings( + "file:///tmp/snapshot", + restored_terminal.getPwd().?, + ); + try testing.expectEqualStrings( + "complete snapshot", + restored_terminal.getTitle().?, + ); + try testing.expectEqual( + primary.pages.scrollbar().total, + restored_terminal.screens.get(.primary).?.pages.scrollbar().total, + ); + + // Re-encoding is a compact semantic equality check over all TERMINAL, + // SCREEN, PAGE, and HISTORY fields and both marker boundaries. + var reencoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer reencoded.deinit(); + try encode( + testing.allocator, + &reencoded.writer, + restored_terminal, + test_encode_options, + ); + try testing.expectEqualStrings( + &test_complete_fixture, + reencoded.written(), + ); + + // SCREEN and HISTORY keys make both sequence groups order independent. + var reversed: std.Io.Writer.Allocating = .init(testing.allocator); + defer reversed.deinit(); + var reversed_stream: record.Writer = .init( + testing.allocator, + &reversed.writer, + ); + defer reversed_stream.deinit(); + try envelope.encode(reversed_stream.writer()); + try terminal.encode(&t, &reversed_stream); + try screen.encode( + t.screens.get(.alternate).?, + .alternate, + &reversed_stream, + ); + try screen.encode(primary, .primary, &reversed_stream); + try continuation.encode(.ground, &reversed_stream); + try checkpoint.encode(.ready, &reversed_stream); + try history.encode( + t.screens.get(.alternate).?, + .alternate, + &reversed_stream, + ); + try history.encode(primary, .primary, &reversed_stream); + try checkpoint.encode(.finish, &reversed_stream); + + var reversed_source: std.Io.Reader = .fixed(reversed.written()); + var reversed_restored = try decode( + testing.allocator, + testing.io, + &reversed_source, + test_decode_options, + ); + defer reversed_restored.deinit(testing.allocator); + const reversed_terminal = &reversed_restored.terminal.?; + try testing.expectEqual( + TerminalScreenKey.alternate, + reversed_terminal.screens.active_key, + ); + try testing.expectEqual( + @as(usize, 0), + reversed_terminal.screens.generation(.primary), + ); + try testing.expectEqual( + @as(usize, 0), + reversed_terminal.screens.generation(.alternate), + ); +} + +test "complete snapshot restores a canonical Stream continuation" { + const testing = std.testing; + + var source_terminal = try Terminal.init( + testing.io, + testing.allocator, + .{ .cols = 8, .rows = 2 }, + ); + defer source_terminal.deinit(testing.allocator); + var source_stream = TerminalStream.init(.{ + .allocator = testing.allocator, + .handler = .init(&source_terminal), + .continuation_max_bytes = 1024, + }); + defer source_stream.deinit(); + + source_stream.nextSlice("A\x1b[31"); + var exported_bytes: [1024]u8 = undefined; + var exported: std.Io.Writer = .fixed(&exported_bytes); + try source_stream.writeContinuation(&exported); + try testing.expectEqualStrings("\x1b[31", exported.buffered()); + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &source_terminal, .{ + .continuation = .{ .bytes = exported.buffered() }, + }); + + var encoded_source: std.Io.Reader = .fixed(encoded.written()); + var decoded = try decode( + testing.allocator, + testing.io, + &encoded_source, + .{ .max_continuation_bytes = 1024 }, + ); + defer decoded.deinit(testing.allocator); + try testing.expectEqualStrings( + exported.buffered(), + decoded.continuation.bytes, + ); + + var restored_terminal = decoded.toOwned(); + defer restored_terminal.deinit(testing.allocator); + try testing.expect(decoded.terminal == null); + var restored_stream = TerminalStream.init(.{ + .allocator = testing.allocator, + .handler = .init(&restored_terminal), + .continuation_max_bytes = 1024, + }); + defer restored_stream.deinit(); + + restored_stream.nextSlice(decoded.continuation.bytes); + var reexported_bytes: [1024]u8 = undefined; + var reexported: std.Io.Writer = .fixed(&reexported_bytes); + try restored_stream.writeContinuation(&reexported); + try testing.expectEqualStrings(exported.buffered(), reexported.buffered()); + + // The identical post-cut bytes may use different feed chunking without + // changing terminal semantics. + source_stream.nextSlice("mB"); + restored_stream.next('m'); + restored_stream.next('B'); + const source_text = try source_terminal.plainString(testing.allocator); + defer testing.allocator.free(source_text); + const restored_text = try restored_terminal.plainString(testing.allocator); + defer testing.allocator.free(restored_text); + try testing.expectEqualStrings(source_text, restored_text); + try testing.expectEqual( + source_terminal.screens.active.cursor.style_id, + restored_terminal.screens.active.cursor.style_id, + ); +} + +test "complete snapshot validates continuation before writing" { + const testing = std.testing; + var t = try Terminal.init( + testing.io, + testing.allocator, + .{ .cols = 2, .rows = 1 }, + ); + defer t.deinit(testing.allocator); + + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + try destination.writer.writeAll("prefix"); + try testing.expectError( + error.NoPendingState, + encode(testing.allocator, &destination.writer, &t, .{ + .continuation = .{ .bytes = "\x1b[31m" }, + }), + ); + try testing.expectEqualStrings("prefix", destination.written()); + + var tail = [_]u8{ 0x1b, '[', '3', '1' }; + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, .{ + .continuation = .{ .bytes = &tail }, + }); + tail[2] = '4'; + + var source: std.Io.Reader = .fixed(encoded.written()); + var decoded = try decode( + testing.allocator, + testing.io, + &source, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + try testing.expectEqualStrings("\x1b[31", decoded.continuation.bytes); +} + +test "complete snapshot waits for continuation tracking recovery" { + const testing = std.testing; + var t = try Terminal.init( + testing.io, + testing.allocator, + .{ .cols = 2, .rows = 1 }, + ); + defer t.deinit(testing.allocator); + var stream = TerminalStream.init(.{ + .allocator = testing.allocator, + .handler = .init(&t), + .continuation_max_bytes = 4, + }); + defer stream.deinit(); + + stream.nextSlice("\x1b[123"); + var unavailable_bytes: [4]u8 = undefined; + var unavailable: std.Io.Writer = .fixed(&unavailable_bytes); + try testing.expectError( + error.ContinuationUnavailable, + stream.writeContinuation(&unavailable), + ); + + // A new replay start replaces the lost suffix and makes a later cut + // publishable without affecting normal terminal parsing. + stream.nextSlice("\x1b["); + var recovered_bytes: [4]u8 = undefined; + var recovered: std.Io.Writer = .fixed(&recovered_bytes); + try stream.writeContinuation(&recovered); + try testing.expectEqualStrings("\x1b[", recovered.buffered()); + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, .{ + .continuation = .{ .bytes = recovered.buffered() }, + }); +} + +test "complete snapshot preserves every supported continuation cut" { + const testing = std.testing; + const corpora = [_][]const u8{ + "plain \xF0\x9F\x98\x84 utf8", + "bad \xE0\xA0\xF0\x9F\x98\x84 utf8", + "\x1b[1\x07;2mstyled\x1b[0m", + "\x1b]2;window title\x1b\\text", + "\x1bP$qm\x1b\\text", + "\x1b_Ga=q;payload\x1b\\text", + "\x1b_25a1;s\x1b\\text", + "\x1b]2;first\x1b\\\x1b_Gsecond", + "\x1b[12\x9D2;title\x1b\\text", + "\x1b[12\x18text\x1b[1\x1Atext", + }; + + for (corpora) |corpus| for (0..corpus.len + 1) |cut| { + var source_terminal = try Terminal.init( + testing.io, + testing.allocator, + .{ .cols = 20, .rows = 4 }, + ); + defer source_terminal.deinit(testing.allocator); + var source_stream = TerminalStream.init(.{ + .allocator = testing.allocator, + .handler = .init(&source_terminal), + .continuation_max_bytes = 4096, + }); + defer source_stream.deinit(); + source_stream.nextSlice(corpus[0..cut]); + + var cut_bytes: [4096]u8 = undefined; + var cut_writer: std.Io.Writer = .fixed(&cut_bytes); + try source_stream.writeContinuation(&cut_writer); + const cut_continuation: Continuation = if (cut_writer.end == 0) + .ground + else + .{ .bytes = cut_writer.buffered() }; + + var snapshot_bytes: std.Io.Writer.Allocating = .init(testing.allocator); + defer snapshot_bytes.deinit(); + try encode( + testing.allocator, + &snapshot_bytes.writer, + &source_terminal, + .{ .continuation = cut_continuation }, + ); + + var snapshot_source: std.Io.Reader = .fixed(snapshot_bytes.written()); + var decoded = try decode( + testing.allocator, + testing.io, + &snapshot_source, + .{ .max_continuation_bytes = 4096 }, + ); + defer decoded.deinit(testing.allocator); + var restored_terminal = decoded.toOwned(); + defer restored_terminal.deinit(testing.allocator); + var restored_stream = TerminalStream.init(.{ + .allocator = testing.allocator, + .handler = .init(&restored_terminal), + .continuation_max_bytes = 4096, + }); + defer restored_stream.deinit(); + switch (decoded.continuation) { + .ground => {}, + .bytes => |bytes| restored_stream.nextSlice(bytes), + } + + var reexport_bytes: [4096]u8 = undefined; + var reexport_writer: std.Io.Writer = .fixed(&reexport_bytes); + try restored_stream.writeContinuation(&reexport_writer); + try testing.expectEqualStrings( + cut_writer.buffered(), + reexport_writer.buffered(), + ); + + source_stream.nextSlice(corpus[cut..]); + var offset = cut; + var partition = cut +% corpus.len +% 1; + while (offset < corpus.len) { + partition = partition *% 1664525 +% 1013904223; + const len = @min(1 + partition % 7, corpus.len - offset); + restored_stream.nextSlice(corpus[offset..][0..len]); + offset += len; + } + + var source_final_bytes: [4096]u8 = undefined; + var source_final_writer: std.Io.Writer = .fixed(&source_final_bytes); + try source_stream.writeContinuation(&source_final_writer); + var restored_final_bytes: [4096]u8 = undefined; + var restored_final_writer: std.Io.Writer = .fixed(&restored_final_bytes); + try restored_stream.writeContinuation(&restored_final_writer); + try testing.expectEqualStrings( + source_final_writer.buffered(), + restored_final_writer.buffered(), + ); + + const final_continuation: Continuation = if (source_final_writer.end == 0) + .ground + else + .{ .bytes = source_final_writer.buffered() }; + var source_final_snapshot: std.Io.Writer.Allocating = .init( + testing.allocator, + ); + defer source_final_snapshot.deinit(); + try encode( + testing.allocator, + &source_final_snapshot.writer, + &source_terminal, + .{ .continuation = final_continuation }, + ); + var restored_final_snapshot: std.Io.Writer.Allocating = .init( + testing.allocator, + ); + defer restored_final_snapshot.deinit(); + try encode( + testing.allocator, + &restored_final_snapshot.writer, + &restored_terminal, + .{ .continuation = final_continuation }, + ); + try testing.expectEqualStrings( + source_final_snapshot.written(), + restored_final_snapshot.written(), + ); + }; +} + +test "complete snapshot preserves Kitty virtual placeholders" { + if (comptime !build_options.kitty_graphics) return error.SkipZigTest; + + const testing = std.testing; + var t = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 2, + .rows = 1, + }); + defer t.deinit(testing.allocator); + + // Register a real virtual placement, then write its grid representation: + // U+10EEEE followed by row and column diacritics. The image and placement + // registry is intentionally omitted, but the grid content must remain + // decodable. + try t.screens.active.kitty_images.addImage( + testing.io, + testing.allocator, + t.screens.active, + .{ .id = 1 }, + ); + try t.screens.active.kitty_images.addPlacement( + testing.io, + testing.allocator, + t.screens.active, + 1, + 0, + .{ + .location = .{ .virtual = {} }, + .columns = 1, + .rows = 1, + }, + ); + try t.setAttribute(.{ .@"256_fg" = 1 }); + try t.printString("\u{10EEEE}\u{0305}\u{0305}"); + const source_cell = t.screens.active.pages.getCell(.{ + .screen = .{}, + }).?; + try testing.expectEqual( + terminal_kitty.graphics.unicode.placeholder, + source_cell.cell.codepoint(), + ); + try testing.expect(source_cell.row.kitty_virtual_placeholder); + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + + var encoded_source: std.Io.Reader = .fixed(encoded.written()); + var restored = try decode( + testing.allocator, + testing.io, + &encoded_source, + test_decode_options, + ); + defer restored.deinit(testing.allocator); + const restored_terminal = &restored.terminal.?; + + const restored_cell = restored_terminal.screens.active.pages.getCell(.{ + .screen = .{}, + }).?; + try testing.expectEqual( + terminal_kitty.graphics.unicode.placeholder, + restored_cell.cell.codepoint(), + ); + try testing.expect(restored_cell.cell.hasGrapheme()); + try testing.expect(restored_cell.row.kitty_virtual_placeholder); + try testing.expectEqual( + @as(usize, 0), + restored_terminal.screens.active.kitty_images.images.count(), + ); + try testing.expectEqual( + @as(usize, 0), + restored_terminal.screens.active.kitty_images.placements.count(), + ); +} + +test "complete snapshot encoding streams from the current writer position" { + const testing = std.testing; + + var t = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 2, + .rows = 1, + }); + defer t.deinit(testing.allocator); + + // Encoding begins with this call's envelope, independent of bytes that + // were already present in the destination. + var nonempty: std.Io.Writer.Allocating = .init(testing.allocator); + defer nonempty.deinit(); + try nonempty.writer.writeAll("prefix"); + const snapshot_offset = nonempty.written().len; + try encode(testing.allocator, &nonempty.writer, &t, test_encode_options); + try testing.expectEqualStrings( + "prefix", + nonempty.written()[0..snapshot_offset], + ); + var appended_source: std.Io.Reader = .fixed( + nonempty.written()[snapshot_offset..], + ); + var appended = try decode( + testing.allocator, + testing.io, + &appended_source, + test_decode_options, + ); + appended.deinit(testing.allocator); + + // Payload validation happens in the record-local scratch allocation. The + // already-streamed envelope remains, but no partial TERMINAL is emitted. + t.colors.palette.current[7] = .{ .r = 1, .g = 2, .b = 3 }; + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + try destination.writer.writeAll("prefix"); + try testing.expectError( + error.InvalidPalette, + encode( + testing.allocator, + &destination.writer, + &t, + test_encode_options, + ), + ); + var expected_envelope: [envelope.encoded_len]u8 = undefined; + var envelope_writer: std.Io.Writer = .fixed(&expected_envelope); + try envelope.encode(&envelope_writer); + try testing.expectEqualStrings( + &expected_envelope, + destination.written()["prefix".len..], + ); +} + +test "complete snapshot rejects ordering and invalid markers" { + const testing = std.testing; + + var t = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 2, + .rows = 1, + }); + defer t.deinit(testing.allocator); + const primary = t.screens.get(.primary).?; + + // Old version-1 snapshots proceeded directly from SCREEN to READY. READY + // is individually valid here, but CONTINUATION is now required first. + var old_order: std.Io.Writer.Allocating = .init(testing.allocator); + defer old_order.deinit(); + var old_order_stream: record.Writer = .init( + testing.allocator, + &old_order.writer, + ); + defer old_order_stream.deinit(); + try envelope.encode(old_order_stream.writer()); + try terminal.encode(&t, &old_order_stream); + try screen.encode(primary, .primary, &old_order_stream); + try checkpoint.encode(.ready, &old_order_stream); + var old_order_source: std.Io.Reader = .fixed(old_order.written()); + try testing.expectError( + error.UnexpectedRecordTag, + decode( + testing.allocator, + testing.io, + &old_order_source, + test_decode_options, + ), + ); + + // A second CONTINUATION is rejected where READY is required. + var duplicate_continuation: std.Io.Writer.Allocating = .init( + testing.allocator, + ); + defer duplicate_continuation.deinit(); + var duplicate_continuation_stream: record.Writer = .init( + testing.allocator, + &duplicate_continuation.writer, + ); + defer duplicate_continuation_stream.deinit(); + try envelope.encode(duplicate_continuation_stream.writer()); + try terminal.encode(&t, &duplicate_continuation_stream); + try screen.encode(primary, .primary, &duplicate_continuation_stream); + try continuation.encode(.ground, &duplicate_continuation_stream); + try continuation.encode(.ground, &duplicate_continuation_stream); + var duplicate_continuation_source: std.Io.Reader = .fixed( + duplicate_continuation.written(), + ); + try testing.expectError( + error.UnexpectedRecordTag, + decode( + testing.allocator, + testing.io, + &duplicate_continuation_source, + test_decode_options, + ), + ); + + // HISTORY is individually valid here, but the full decoder requires the + // primary SCREEN before READY. + var reordered: std.Io.Writer.Allocating = .init(testing.allocator); + defer reordered.deinit(); + var reordered_stream: record.Writer = .init( + testing.allocator, + &reordered.writer, + ); + defer reordered_stream.deinit(); + try envelope.encode(reordered_stream.writer()); + try terminal.encode(&t, &reordered_stream); + try history.encode(primary, .primary, &reordered_stream); + var reordered_source: std.Io.Reader = .fixed(reordered.written()); + try testing.expectError( + error.UnexpectedRecordTag, + decode( + testing.allocator, + testing.io, + &reordered_source, + test_decode_options, + ), + ); + + // Construct a correctly framed READY with a nonempty payload so the full + // driver rejects a marker that does not match the required empty shape. + var invalid_ready: std.Io.Writer.Allocating = .init(testing.allocator); + defer invalid_ready.deinit(); + var invalid_ready_stream: record.Writer = .init( + testing.allocator, + &invalid_ready.writer, + ); + defer invalid_ready_stream.deinit(); + try envelope.encode(invalid_ready_stream.writer()); + try terminal.encode(&t, &invalid_ready_stream); + try screen.encode(primary, .primary, &invalid_ready_stream); + try continuation.encode(.ground, &invalid_ready_stream); + const ready_payload = invalid_ready_stream.begin(.ready); + errdefer invalid_ready_stream.cancel(); + try ready_payload.writeByte(0); + try invalid_ready_stream.finish(); + var invalid_ready_source: std.Io.Reader = .fixed( + invalid_ready.written(), + ); + try testing.expectError( + error.PayloadNotExhausted, + decode( + testing.allocator, + testing.io, + &invalid_ready_source, + test_decode_options, + ), + ); + + // A SCREEN key must name one of the slots declared by TERMINAL. + var undeclared: std.Io.Writer.Allocating = .init(testing.allocator); + defer undeclared.deinit(); + var undeclared_stream: record.Writer = .init( + testing.allocator, + &undeclared.writer, + ); + defer undeclared_stream.deinit(); + try envelope.encode(undeclared_stream.writer()); + try terminal.encode(&t, &undeclared_stream); + try screen.encode(primary, .alternate, &undeclared_stream); + var undeclared_source: std.Io.Reader = .fixed(undeclared.written()); + try testing.expectError( + error.UnexpectedScreenKey, + decode( + testing.allocator, + testing.io, + &undeclared_source, + test_decode_options, + ), + ); + + // HISTORY sequences are also routed by key, which must name a declared + // screen even when the sequence contains no PAGE records. + var undeclared_history: std.Io.Writer.Allocating = .init(testing.allocator); + defer undeclared_history.deinit(); + var undeclared_history_stream: record.Writer = .init( + testing.allocator, + &undeclared_history.writer, + ); + defer undeclared_history_stream.deinit(); + try envelope.encode(undeclared_history_stream.writer()); + try terminal.encode(&t, &undeclared_history_stream); + try screen.encode(primary, .primary, &undeclared_history_stream); + try continuation.encode(.ground, &undeclared_history_stream); + try checkpoint.encode(.ready, &undeclared_history_stream); + try history.encode(primary, .alternate, &undeclared_history_stream); + var undeclared_history_source: std.Io.Reader = .fixed( + undeclared_history.written(), + ); + try testing.expectError( + error.UnexpectedHistoryKey, + decode( + testing.allocator, + testing.io, + &undeclared_history_source, + test_decode_options, + ), + ); + + // The declared count cannot be satisfied by repeating the same key. + _ = try t.switchScreen(.alternate); + var duplicate: std.Io.Writer.Allocating = .init(testing.allocator); + defer duplicate.deinit(); + var duplicate_stream: record.Writer = .init( + testing.allocator, + &duplicate.writer, + ); + defer duplicate_stream.deinit(); + try envelope.encode(duplicate_stream.writer()); + try terminal.encode(&t, &duplicate_stream); + try screen.encode(primary, .primary, &duplicate_stream); + try screen.encode(primary, .primary, &duplicate_stream); + var duplicate_source: std.Io.Reader = .fixed(duplicate.written()); + try testing.expectError( + error.DuplicateScreen, + decode( + testing.allocator, + testing.io, + &duplicate_source, + test_decode_options, + ), + ); + + // The declared count cannot be satisfied by repeating one HISTORY key. + var duplicate_history: std.Io.Writer.Allocating = .init(testing.allocator); + defer duplicate_history.deinit(); + var duplicate_history_stream: record.Writer = .init( + testing.allocator, + &duplicate_history.writer, + ); + defer duplicate_history_stream.deinit(); + try envelope.encode(duplicate_history_stream.writer()); + try terminal.encode(&t, &duplicate_history_stream); + try screen.encode(primary, .primary, &duplicate_history_stream); + try screen.encode( + t.screens.get(.alternate).?, + .alternate, + &duplicate_history_stream, + ); + try continuation.encode(.ground, &duplicate_history_stream); + try checkpoint.encode(.ready, &duplicate_history_stream); + try history.encode(primary, .primary, &duplicate_history_stream); + try history.encode(primary, .primary, &duplicate_history_stream); + var duplicate_history_source: std.Io.Reader = .fixed( + duplicate_history.written(), + ); + try testing.expectError( + error.DuplicateHistory, + decode( + testing.allocator, + testing.io, + &duplicate_history_source, + test_decode_options, + ), + ); +} + +test "complete snapshot leaves continuation bytes unread" { + const testing = std.testing; + + var t = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 2, + .rows = 1, + }); + defer t.deinit(testing.allocator); + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + const snapshot_len = encoded.written().len; + try encoded.writer.writeAll("pty"); + + var source: std.Io.Reader = .fixed(encoded.written()); + var restored = try decode( + testing.allocator, + testing.io, + &source, + test_decode_options, + ); + defer restored.deinit(testing.allocator); + + var trailing: [3]u8 = undefined; + try source.readSliceAll(&trailing); + try testing.expectEqualStrings("pty", &trailing); + + var exact_source: std.Io.Reader = .fixed(encoded.written()); + try testing.expectError( + error.TrailingData, + decodeExact( + testing.allocator, + testing.io, + &exact_source, + test_decode_options, + ), + ); + + var bounded_source: std.Io.Reader = .fixed( + encoded.written()[0..snapshot_len], + ); + var bounded = try decodeExact( + testing.allocator, + testing.io, + &bounded_source, + test_decode_options, + ); + defer bounded.deinit(testing.allocator); +} + +test "complete snapshots decode sequentially from one reader" { + const testing = std.testing; + + var t = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 2, + .rows = 1, + }); + defer t.deinit(testing.allocator); + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + + var source: std.Io.Reader = .fixed(encoded.written()); + var first = try decode( + testing.allocator, + testing.io, + &source, + test_decode_options, + ); + defer first.deinit(testing.allocator); + var second = try decode( + testing.allocator, + testing.io, + &source, + test_decode_options, + ); + defer second.deinit(testing.allocator); + try testing.expectError(error.EndOfStream, source.takeByte()); +} + +test "complete snapshot decode allocation failures are transactional" { + const testing = std.testing; + const S = struct { + fn exercise(bytes: []const u8) !void { + var baseline = testing.FailingAllocator.init(testing.allocator, .{ + .fail_index = std.math.maxInt(usize), + }); + var baseline_source: std.Io.Reader = .fixed(bytes); + var baseline_decoded = try decode( + baseline.allocator(), + testing.io, + &baseline_source, + test_decode_options, + ); + baseline_decoded.deinit(baseline.allocator()); + const allocation_count = baseline.alloc_index; + try testing.expect(allocation_count > 0); + + var saw_out_of_memory = false; + for (0..allocation_count) |fail_index| { + var failing = testing.FailingAllocator.init( + testing.allocator, + .{ .fail_index = fail_index }, + ); + var source: std.Io.Reader = .fixed(bytes); + var decoded = decode( + failing.allocator(), + testing.io, + &source, + test_decode_options, + ) catch |err| switch (err) { + error.OutOfMemory => { + saw_out_of_memory = true; + continue; + }, + else => return err, + }; + decoded.deinit(failing.allocator()); + } + try testing.expect(saw_out_of_memory); + } + }; + + // The complete golden exercises Terminal, both screens, and history. + try S.exercise(&test_complete_fixture); + + // A non-ground component additionally exercises owned continuation bytes. + var t = try Terminal.init( + testing.io, + testing.allocator, + .{ .cols = 2, .rows = 1 }, + ); + defer t.deinit(testing.allocator); + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, .{ + .continuation = .{ .bytes = "\x1b[31" }, + }); + try S.exercise(encoded.written()); +} + +test "incremental decode restores a renderable terminal at READY" { + const testing = std.testing; + + // Trailing transport bytes must remain unread after FINISH. + var transport: std.Io.Writer.Allocating = .init(testing.allocator); + defer transport.deinit(); + try transport.writer.writeAll(&test_complete_fixture); + try transport.writer.writeAll("pty"); + + var source: std.Io.Reader = .fixed(transport.written()); + var decoder: Decoder = .init(&source); + var decoded = try decoder.ready( + testing.allocator, + testing.io, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + + // READY restored the complete interactive state, so the Terminal can + // move to its final address before any history has been decoded. + var restored = decoded.toOwned(); + defer restored.deinit(testing.allocator); + try testing.expectEqual( + TerminalScreenKey.alternate, + restored.screens.active_key, + ); + try testing.expectEqualStrings( + "complete snapshot", + restored.getTitle().?, + ); + + // The advisory extents size scrollback UI immediately even though only + // the resident SCREEN pages exist. The alternate screen's extent is + // entirely resident overlap, so it must match its live row count. + const primary = restored.screens.get(.primary).?; + const alternate = restored.screens.get(.alternate).?; + try testing.expectEqual(3, primary.pages.scrollbar().total); + try testing.expectEqual(4, decoded.history_rows.get(.primary).?); + try testing.expectEqual( + alternate.pages.scrollbar().total - restored.rows, + decoded.history_rows.get(.alternate).?, + ); + + // Each call prepends exactly one history page, newest first. + const first = (try decoder.next(testing.allocator, &restored)).?; + try testing.expectEqualDeep(Decoder.Progress{ + .key = .primary, + .rows = 2, + .remaining = 1, + }, first); + try testing.expectEqual(5, primary.pages.scrollbar().total); + try testing.expectEqual(@as(u21, 'B'), testTopLeftCodepoint(primary)); + + const second = (try decoder.next(testing.allocator, &restored)).?; + try testing.expectEqualDeep(Decoder.Progress{ + .key = .primary, + .rows = 2, + .remaining = 0, + }, second); + try testing.expectEqual(7, primary.pages.scrollbar().total); + try testing.expectEqual(@as(u21, 'A'), testTopLeftCodepoint(primary)); + + // The alternate screen's empty sequence produces no event: the next + // call validates FINISH, and further calls remain null. + try testing.expect(try decoder.next(testing.allocator, &restored) == null); + try testing.expect(try decoder.next(testing.allocator, &restored) == null); + try testing.expectEqualStrings("pty", try source.take(3)); + + // The incremental path restores the same complete terminal as the + // one-shot decoder, byte for byte. + var reencoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer reencoded.deinit(); + try encode( + testing.allocator, + &reencoded.writer, + &restored, + test_encode_options, + ); + try testing.expectEqualStrings(&test_complete_fixture, reencoded.written()); +} + +test "incremental next reports invalid decoder states" { + const testing = std.testing; + var source: std.Io.Reader = .fixed(""); + var decoder: Decoder = .init(&source); + var terminal_value = try Terminal.init( + testing.io, + testing.allocator, + .{ .cols = 1, .rows = 1 }, + ); + defer terminal_value.deinit(testing.allocator); + + try testing.expectError( + error.DecoderNotReady, + decoder.next(testing.allocator, &terminal_value), + ); +} + +test "incremental decode applies history below live PTY output" { + const testing = std.testing; + + var t = try testHistoryTerminal(2); + defer t.deinit(testing.allocator); + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + + var source: std.Io.Reader = .fixed(encoded.written()); + var decoder: Decoder = .init(&source); + var decoded = try decoder.ready( + testing.allocator, + testing.io, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + var restored = decoded.toOwned(); + defer restored.deinit(testing.allocator); + + // The terminal is interactive at READY: post-cut PTY bytes append to + // the bottom while snapshot history is still pending. + var live = restored.vtStream(); + defer live.deinit(); + live.nextSlice("1\r\n2\r\n3\r\n4\r\n5\r\n"); + const primary = restored.screens.get(.primary).?; + const live_total = primary.pages.scrollbar().total; + try testing.expect(live_total > 3); + + // History still lands above both the snapshot content and the live + // output, preserving complete scrollback ordering. + while (try decoder.next(testing.allocator, &restored)) |progress| { + try testing.expectEqual(TerminalScreenKey.primary, progress.key); + try testing.expectEqual(2, progress.rows); + } + try testing.expectEqual(live_total + 4, primary.pages.scrollbar().total); + try testing.expectEqual(@as(u21, 'A'), testTopLeftCodepoint(primary)); + const newest_history = primary.pages.getTopLeft(.screen).node.next.?; + try testing.expectEqual( + @as(u21, 'B'), + newest_history.page().getRowAndCell(0, 0).cell.codepoint(), + ); + + // The page below the applied history is the snapshot's active page, + // whose top-left cell the live output already overwrote. + try testing.expectEqual( + @as(u21, '1'), + newest_history.next.?.page().getRowAndCell(0, 0).cell.codepoint(), + ); +} + +test "incremental decode discards history for screens changed since READY" { + const testing = std.testing; + + var t = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 2, + .rows = 3, + }); + defer t.deinit(testing.allocator); + _ = try t.switchScreen(.alternate); + try t.printString("alt"); + _ = try t.switchScreen(.primary); + + // The encoder never produces alternate-screen history, but the format + // permits it, so the decoder must stay stream-aligned even when the + // pages can no longer be applied. Order independence lets the discarded + // sequence come first. + var crafted: std.Io.Writer.Allocating = .init(testing.allocator); + defer crafted.deinit(); + var stream: record.Writer = .init(testing.allocator, &crafted.writer); + defer stream.deinit(); + try envelope.encode(stream.writer()); + try terminal.encode(&t, &stream); + try screen.encode(t.screens.get(.primary).?, .primary, &stream); + try screen.encode(t.screens.get(.alternate).?, .alternate, &stream); + try continuation.encode(.ground, &stream); + try checkpoint.encode(.ready, &stream); + { + const payload = stream.begin(.history); + errdefer stream.cancel(); + try (history.Header{ .key = .alternate, .page_count = 1 }).encode( + payload, + ); + try stream.finish(); + } + try page.encode( + t.screens.get(.alternate).?.pages + .getTopLeft(.screen).node.pageAssumeResident(), + &stream, + ); + try history.encode(t.screens.get(.primary).?, .primary, &stream); + try checkpoint.encode(.finish, &stream); + + // An unchanged declared screen accepts crafted history, establishing + // the baseline that the following runs degrade from. + { + var source: std.Io.Reader = .fixed(crafted.written()); + var decoder: Decoder = .init(&source); + var decoded = try decoder.ready( + testing.allocator, + testing.io, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + const restored = &decoded.terminal.?; + + const progress = (try decoder.next(testing.allocator, restored)).?; + try testing.expectEqual(TerminalScreenKey.alternate, progress.key); + try testing.expect(progress.rows > 0); + try testing.expect(try decoder.next(testing.allocator, restored) == null); + try testing.expectEqual( + @as(usize, 2), + restored.screens.get(.alternate).?.pages.totalPages(), + ); + } + + // A screen removed since READY discards its history. FINISH still + // validates, proving discarded records leave the stream aligned. + { + var source: std.Io.Reader = .fixed(crafted.written()); + var decoder: Decoder = .init(&source); + var decoded = try decoder.ready( + testing.allocator, + testing.io, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + const restored = &decoded.terminal.?; + restored.screens.remove(testing.allocator, .alternate); + + const progress = (try decoder.next(testing.allocator, restored)).?; + try testing.expectEqualDeep(Decoder.Progress{ + .key = .alternate, + .rows = 0, + .remaining = 0, + }, progress); + try testing.expect(try decoder.next(testing.allocator, restored) == null); + } + + // A screen removed and recreated since READY has a new generation, so + // stale history never mixes into its replacement's content. + { + var source: std.Io.Reader = .fixed(crafted.written()); + var decoder: Decoder = .init(&source); + var decoded = try decoder.ready( + testing.allocator, + testing.io, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + const restored = &decoded.terminal.?; + restored.screens.remove(testing.allocator, .alternate); + _ = try restored.switchScreen(.alternate); + + const progress = (try decoder.next(testing.allocator, restored)).?; + try testing.expectEqual(TerminalScreenKey.alternate, progress.key); + try testing.expectEqual(0, progress.rows); + try testing.expect(try decoder.next(testing.allocator, restored) == null); + try testing.expectEqual( + @as(usize, 1), + restored.screens.get(.alternate).?.pages.totalPages(), + ); + } +} + +test "incremental decode discards history after a resize" { + const testing = std.testing; + + var t = try testHistoryTerminal(2); + defer t.deinit(testing.allocator); + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + + var source: std.Io.Reader = .fixed(encoded.written()); + var decoder: Decoder = .init(&source); + var decoded = try decoder.ready( + testing.allocator, + testing.io, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + var restored = decoded.toOwned(); + defer restored.deinit(testing.allocator); + + // Encoded pages assume the READY width. After a live resize reflows the + // restored screen, pending history is consumed but never applied. + try restored.resize(testing.allocator, .{ .cols = 3, .rows = 3 }); + const primary = restored.screens.get(.primary).?; + const resized_total = primary.pages.scrollbar().total; + + var pages: usize = 0; + while (try decoder.next(testing.allocator, &restored)) |progress| { + try testing.expectEqual(TerminalScreenKey.primary, progress.key); + try testing.expectEqual(0, progress.rows); + pages += 1; + } + try testing.expectEqual(2, pages); + try testing.expectEqual(resized_total, primary.pages.scrollbar().total); +} + +test "decode drops history beyond the declared scrollback limits" { + const testing = std.testing; + + // Declare a byte limit the complete history cannot satisfy. The limit + // is validated at page granularity on decode, so the snapshot itself + // remains well formed and fully validated. + var t = try testHistoryTerminal(3); + defer t.deinit(testing.allocator); + t.screens.get(.primary).?.pages.limits.set(.bytes, 1); + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + + // Incremental: the newest page fits the effective floor, the next page + // exceeds it, and one drop discards the remainder of the sequence so no + // ordering gap can form. + { + var source: std.Io.Reader = .fixed(encoded.written()); + var decoder: Decoder = .init(&source); + var decoded = try decoder.ready( + testing.allocator, + testing.io, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + const restored = &decoded.terminal.?; + + var rows: [3]usize = undefined; + var pages: usize = 0; + while (try decoder.next(testing.allocator, restored)) |progress| { + rows[pages] = progress.rows; + pages += 1; + } + try testing.expectEqual(3, pages); + try testing.expectEqualSlices(usize, &.{ 2, 0, 0 }, &rows); + + const primary = restored.screens.get(.primary).?; + try testing.expectEqual(@as(usize, 2), primary.pages.totalPages()); + try testing.expectEqual(@as(u21, 'C'), testTopLeftCodepoint(primary)); + } + + // The one-shot decoder degrades identically instead of rejecting the + // snapshot. + { + var source: std.Io.Reader = .fixed(encoded.written()); + var decoded = try decode( + testing.allocator, + testing.io, + &source, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + const primary = decoded.terminal.?.screens.get(.primary).?; + try testing.expectEqual(@as(usize, 2), primary.pages.totalPages()); + try testing.expectEqual(@as(u21, 'C'), testTopLeftCodepoint(primary)); + } +} + +test "incremental decode failure leaves applied history usable" { + const testing = std.testing; + + var t = try testHistoryTerminal(2); + defer t.deinit(testing.allocator); + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + try encode(testing.allocator, &encoded.writer, &t, test_encode_options); + + // Truncate FINISH. READY and both history pages decode, then the + // failure surfaces on the call that would have completed the snapshot. + var source: std.Io.Reader = .fixed( + encoded.written()[0 .. encoded.written().len - 1], + ); + var decoder: Decoder = .init(&source); + var decoded = try decoder.ready( + testing.allocator, + testing.io, + test_decode_options, + ); + defer decoded.deinit(testing.allocator); + var restored = decoded.toOwned(); + defer restored.deinit(testing.allocator); + + for (0..2) |_| { + const progress = (try decoder.next(testing.allocator, &restored)).?; + try testing.expectEqual(2, progress.rows); + } + try testing.expectError( + error.EndOfStream, + decoder.next(testing.allocator, &restored), + ); + try testing.expectError( + error.DecoderFailed, + decoder.next(testing.allocator, &restored), + ); + + // The transferred terminal was never owned by the decoder: it remains + // valid with the contiguous history prefix that did apply; only the + // incomplete snapshot stream is lost. + const primary = restored.screens.get(.primary).?; + try testing.expectEqual(@as(usize, 3), primary.pages.totalPages()); + try testing.expectEqual(@as(u21, 'A'), testTopLeftCodepoint(primary)); + primary.pages.assertIntegrity(); + primary.assertIntegrity(); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/style.zig b/vendor/libghostty-vt/src/terminal/snapshot/style.zig new file mode 100644 index 00000000..bd0d445c --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/style.zig @@ -0,0 +1,423 @@ +//! Snapshot style entry encoding. +//! +//! Each entry describes one terminal style. A record can use these entries to +//! build a style table and assign indexes according to that record's format. +//! Indexing, ordering, etc. are properties of the containing record. +//! +//! Styles are encoded field by field from the terminal's native `Style` type. +//! The native packed representation is deliberately not part of the snapshot +//! format. This gives flexibility for changing one side or the other. +//! +//! All integers are unsigned and little-endian. +//! +//! | Offset | Size | Field | +//! | -----: | ---: | :------------------------ | +//! | 0 | 4 | Foreground color | +//! | 4 | 4 | Background color | +//! | 8 | 4 | Underline color | +//! | 12 | 2 | Style flags (`u16`) | +//! | 14 | 2 | Reserved, must be zero | +//! +//! The trailing reserved field is explicit wire padding that rounds each +//! style entry from 14 bytes to 16 bytes. This makes entry offsets and byte +//! counts straightforward. +//! +//! Each color begins with a one-byte kind followed by three data bytes: +//! +//! | Kind | Meaning | Data bytes | +//! | ---: | :------ | :--------------------------------- | +//! | 0 | None | All zero | +//! | 1 | Palette | Palette index, then two zero bytes | +//! | 2 | RGB | Red, green, blue | +//! +//! Style flag bits 0 through 7 are bold, italic, faint, blink, inverse, +//! invisible, strikethrough, and overline. Bits 8 through 10 contain the +//! underline kind. Bits 11 through 15 must be zero. +//! +//! | Underline | Meaning | +//! | --------: | :------ | +//! | 0 | None | +//! | 1 | Single | +//! | 2 | Double | +//! | 3 | Curly | +//! | 4 | Dotted | +//! | 5 | Dashed | +//! +//! Underline values 6 and 7 are invalid in snapshot version 1. + +const std = @import("std"); +const test_fixture = @import("fixture.zig"); +const sgr = @import("../sgr.zig"); +const terminal_style = @import("../style.zig"); + +/// Number of bytes in one encoded style entry. This size is part of the +/// wire format: the codec reads and writes fixed offsets within an entry +/// of exactly this size, so if the layout changes, the snapshot version +/// and golden fixtures must also change. +pub const len = 16; + +const Flags = packed struct(u16) { + bold: bool = false, + italic: bool = false, + faint: bool = false, + blink: bool = false, + inverse: bool = false, + invisible: bool = false, + strikethrough: bool = false, + overline: bool = false, + underline: u3 = 0, + reserved: u5 = 0, +}; + +const ColorKind = enum(u8) { + none = 0, + palette = 1, + rgb = 2, +}; + +/// Semantic validation errors for one complete style entry buffer. +const ParseError = error{ + /// A color kind is not defined by snapshot version 1. + InvalidColorKind, + + /// Bytes unused by the selected color kind are not zero. + InvalidColor, + + /// The encoded underline kind is not defined by snapshot version 1. + InvalidUnderline, + + /// One or more reserved style flag bits are set. + InvalidFlags, + + /// The trailing reserved field is not zero. + InvalidReserved, +}; + +/// Errors possible while decoding one style entry. +pub const DecodeError = std.Io.Reader.Error || ParseError; + +/// Encode one terminal style as a fixed-size snapshot style entry. +/// +/// The entry is assembled in a fixed buffer and written once, so hot +/// encoders perform a single writer call per style. +pub fn encode( + value: terminal_style.Style, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + var encoded: [len]u8 = @splat(0); + encodeColorBuf(encoded[0..4], value.fg_color); + encodeColorBuf(encoded[4..8], value.bg_color); + encodeColorBuf(encoded[8..12], value.underline_color); + + const flags: Flags = .{ + .bold = value.flags.bold, + .italic = value.flags.italic, + .faint = value.flags.faint, + .blink = value.flags.blink, + .inverse = value.flags.inverse, + .invisible = value.flags.invisible, + .strikethrough = value.flags.strikethrough, + .overline = value.flags.overline, + .underline = @intFromEnum(value.flags.underline), + }; + std.mem.writeInt(u16, encoded[12..14], @bitCast(flags), .little); + try writer.writeAll(&encoded); +} + +/// Decode and validate one fixed-size snapshot style entry. +pub fn decode(reader: *std.Io.Reader) DecodeError!terminal_style.Style { + var encoded: [len]u8 = undefined; + try reader.readSliceAll(&encoded); + return parse(&encoded); +} + +/// Decode and validate one complete fixed-size style entry buffer. +fn parse(encoded: *const [len]u8) ParseError!terminal_style.Style { + const fg_color = try parseColor(encoded[0..4]); + const bg_color = try parseColor(encoded[4..8]); + const underline_color = try parseColor(encoded[8..12]); + + const flags: Flags = @bitCast( + std.mem.readInt(u16, encoded[12..14], .little), + ); + if (flags.reserved != 0) return error.InvalidFlags; + + const underline = std.enums.fromInt( + sgr.Attribute.Underline, + flags.underline, + ) orelse return error.InvalidUnderline; + + const reserved = std.mem.readInt(u16, encoded[14..16], .little); + if (reserved != 0) return error.InvalidReserved; + + return .{ + .fg_color = fg_color, + .bg_color = bg_color, + .underline_color = underline_color, + .flags = .{ + .bold = flags.bold, + .italic = flags.italic, + .faint = flags.faint, + .blink = flags.blink, + .inverse = flags.inverse, + .invisible = flags.invisible, + .strikethrough = flags.strikethrough, + .overline = flags.overline, + .underline = underline, + }, + }; +} + +/// Decode strictly after consuming one complete fixed-size style entry. +/// +/// Unlike `decode`, a semantic error leaves `reader` at the next entry. This +/// lets an enclosing codec catch the error and choose its own fallback without +/// losing the surrounding payload boundary. +pub fn decodeOrDiscard( + reader: *std.Io.Reader, +) DecodeError!terminal_style.Style { + var encoded: [len]u8 = undefined; + try reader.readSliceAll(&encoded); + return parse(&encoded); +} + +/// Decode one complete entry, returning null for invalid semantic contents. +/// +/// Reader errors remain structural and are always propagated. This is the +/// lenient entry point for enclosing codecs which can safely replace an +/// invalid fixed-size style without treating truncation as invalid styling. +pub fn decodeOrNull( + reader: *std.Io.Reader, +) std.Io.Reader.Error!?terminal_style.Style { + return decodeOrDiscard(reader) catch |err| switch (err) { + error.ReadFailed => return error.ReadFailed, + error.EndOfStream => return error.EndOfStream, + + error.InvalidColorKind, + error.InvalidColor, + error.InvalidUnderline, + error.InvalidFlags, + error.InvalidReserved, + => null, + }; +} + +/// `decodeOrNull` over one already-buffered entry, for enclosing codecs +/// that parse many entries from a flat payload without reader calls. +pub fn parseOrNull(encoded: *const [len]u8) ?terminal_style.Style { + return parse(encoded) catch null; +} + +fn encodeColorBuf( + encoded: *[4]u8, + value: terminal_style.Style.Color, +) void { + switch (value) { + .none => encoded[0] = @intFromEnum(ColorKind.none), + .palette => |index| { + encoded[0] = @intFromEnum(ColorKind.palette); + encoded[1] = index; + }, + .rgb => |rgb| { + encoded[0] = @intFromEnum(ColorKind.rgb); + encoded[1] = rgb.r; + encoded[2] = rgb.g; + encoded[3] = rgb.b; + }, + } +} + +fn parseColor( + encoded: *const [4]u8, +) ParseError!terminal_style.Style.Color { + // Kind must be something we know about. + const kind = std.enums.fromInt(ColorKind, encoded[0]) orelse { + return error.InvalidColorKind; + }; + + return switch (kind) { + .none => if (encoded[1] == 0 and encoded[2] == 0 and encoded[3] == 0) + .none + else + error.InvalidColor, + .palette => if (encoded[2] == 0 and encoded[3] == 0) + .{ .palette = encoded[1] } + else + error.InvalidColor, + .rgb => .{ .rgb = .{ + .r = encoded[1], + .g = encoded[2], + .b = encoded[3], + } }, + }; +} + +const test_golden_fixture = test_fixture.parse(@embedFile("testdata/style-v1.hex")); + +test "golden encoding and decoding" { + const value: terminal_style.Style = .{ + .fg_color = .none, + .bg_color = .{ .palette = 0x7f }, + .underline_color = .{ .rgb = .{ + .r = 0x12, + .g = 0x34, + .b = 0x56, + } }, + .flags = .{ + .bold = true, + .italic = true, + .faint = true, + .blink = true, + .inverse = true, + .invisible = true, + .strikethrough = true, + .overline = true, + .underline = .curly, + }, + }; + var buf: [len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try encode(value, &writer); + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/style-v1.hex", + "snapshot_fixture-style-v1.hex", + &test_golden_fixture, + writer.buffered(), + ); + + var source: std.Io.Reader = .fixed(&test_golden_fixture); + var read_buf: [1]u8 = undefined; + var limited = source.limited(.unlimited, &read_buf); + try std.testing.expect(value.eql(try decode(&limited.interface))); +} + +test "flag bit layout" { + const Case = struct { + value: Flags, + expected: u16, + }; + const cases = [_]Case{ + .{ .value = .{ .bold = true }, .expected = 1 << 0 }, + .{ .value = .{ .italic = true }, .expected = 1 << 1 }, + .{ .value = .{ .faint = true }, .expected = 1 << 2 }, + .{ .value = .{ .blink = true }, .expected = 1 << 3 }, + .{ .value = .{ .inverse = true }, .expected = 1 << 4 }, + .{ .value = .{ .invisible = true }, .expected = 1 << 5 }, + .{ .value = .{ .strikethrough = true }, .expected = 1 << 6 }, + .{ .value = .{ .overline = true }, .expected = 1 << 7 }, + .{ + .value = .{ + .underline = @intFromEnum(sgr.Attribute.Underline.single), + }, + .expected = 1 << 8, + }, + .{ + .value = .{ + .underline = @intFromEnum(sgr.Attribute.Underline.double), + }, + .expected = 2 << 8, + }, + .{ + .value = .{ + .underline = @intFromEnum(sgr.Attribute.Underline.curly), + }, + .expected = 3 << 8, + }, + .{ + .value = .{ + .underline = @intFromEnum(sgr.Attribute.Underline.dotted), + }, + .expected = 4 << 8, + }, + .{ + .value = .{ + .underline = @intFromEnum(sgr.Attribute.Underline.dashed), + }, + .expected = 5 << 8, + }, + .{ .value = .{ .reserved = 1 }, .expected = 1 << 11 }, + }; + + for (cases) |case| { + try std.testing.expectEqual( + case.expected, + @as(u16, @bitCast(case.value)), + ); + } +} + +test "reject invalid colors" { + for ([_]usize{ 0, 4, 8 }) |offset| { + var invalid_kind: [len]u8 = @splat(0); + invalid_kind[offset] = 3; + var reader: std.Io.Reader = .fixed(&invalid_kind); + try std.testing.expectError(error.InvalidColorKind, decode(&reader)); + } + + var invalid_none: [len]u8 = @splat(0); + invalid_none[1] = 1; + var none_reader: std.Io.Reader = .fixed(&invalid_none); + try std.testing.expectError(error.InvalidColor, decode(&none_reader)); + + var invalid_palette: [len]u8 = @splat(0); + invalid_palette[0] = @intFromEnum(ColorKind.palette); + invalid_palette[2] = 1; + var palette_reader: std.Io.Reader = .fixed(&invalid_palette); + try std.testing.expectError(error.InvalidColor, decode(&palette_reader)); +} + +test "reject invalid flags and reserved field" { + for ([_]u16{ 6, 7 }) |underline| { + var invalid_underline: [len]u8 = @splat(0); + std.mem.writeInt( + u16, + invalid_underline[12..14], + underline << 8, + .little, + ); + var reader: std.Io.Reader = .fixed(&invalid_underline); + try std.testing.expectError(error.InvalidUnderline, decode(&reader)); + } + + var invalid_flags: [len]u8 = @splat(0); + std.mem.writeInt(u16, invalid_flags[12..14], 1 << 11, .little); + var flags_reader: std.Io.Reader = .fixed(&invalid_flags); + try std.testing.expectError(error.InvalidFlags, decode(&flags_reader)); + + var invalid_reserved: [len]u8 = @splat(0); + std.mem.writeInt(u16, invalid_reserved[14..16], 1, .little); + var reserved_reader: std.Io.Reader = .fixed(&invalid_reserved); + try std.testing.expectError( + error.InvalidReserved, + decode(&reserved_reader), + ); +} + +test "decodeOrDiscard preserves the next entry boundary" { + var fixture: [len + 1]u8 = @splat(0); + fixture[0] = 3; + fixture[len] = 0xFF; + + var reader: std.Io.Reader = .fixed(&fixture); + try std.testing.expectError(error.InvalidColorKind, decodeOrDiscard(&reader)); + try std.testing.expectEqual(@as(u8, 0xFF), try reader.takeByte()); +} + +test "decodeOrNull distinguishes semantic errors from truncation" { + var invalid: [len]u8 = @splat(0); + invalid[0] = 3; + var invalid_reader: std.Io.Reader = .fixed(&invalid); + try std.testing.expectEqual(null, try decodeOrNull(&invalid_reader)); + + var truncated: std.Io.Reader = .fixed(invalid[0 .. len - 1]); + try std.testing.expectError(error.EndOfStream, decodeOrNull(&truncated)); +} + +test "reject every truncation" { + const fixture = [_]u8{0} ** len; + for (0..len) |fixture_len| { + var reader: std.Io.Reader = .fixed(fixture[0..fixture_len]); + try std.testing.expectError(error.EndOfStream, decode(&reader)); + } +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/terminal.zig b/vendor/libghostty-vt/src/terminal/snapshot/terminal.zig new file mode 100644 index 00000000..19305372 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/terminal.zig @@ -0,0 +1,1850 @@ +//! TERMINAL record payload encoding. +//! +//! One TERMINAL record contains terminal-wide state shared by every screen. It +//! is the first record in a snapshot and declares which SCREEN sequences follow +//! it. Screen contents, cursors, and history are encoded by `screen.zig` and +//! `history.zig`. +//! +//! Snapshot version 1 supports the primary screen and an optional alternate +//! screen. `screen_count` is therefore one or two. SCREEN records identify +//! their destination by key and may appear in either order. Canonical encoders +//! write primary first, then alternate when present. `active_screen_key` must +//! name one of those declared screens. +//! +//! All integers are unsigned and little-endian. +//! +//! ## Binary Format +//! +//! The payload begins with a fixed header followed by terminal-wide variable +//! state. The enclosing record supplies the payload boundary. +//! +//! ```text +//! 0 +--------------------------+ +//! | Header | +//! 103 +--------------------------+ +//! | Tab stops | +//! | ceil(columns / 8) bytes | +//! +--------------------------+ +//! | Original palette | +//! | 256 * RGB | +//! +--------------------------+ +//! | Palette override mask | +//! | 32 bytes | +//! +--------------------------+ +//! | Palette overrides | +//! | 3 * popcount(mask) bytes | +//! +--------------------------+ +//! | PWD length (u32) | +//! +--------------------------+ +//! | PWD bytes | +//! +--------------------------+ +//! | Title length (u32) | +//! +--------------------------+ +//! | Title bytes | +//! end +--------------------------+ +//! ``` +//! +//! ### Header +//! +//! ```text +//! 0 +-----------------------------------+ +//! | Columns (u16) | +//! 2 +-----------------------------------+ +//! | Rows (u16) | +//! 4 +-----------------------------------+ +//! | Pixel width (u32) | +//! 8 +-----------------------------------+ +//! | Pixel height (u32) | +//! 12 +-----------------------------------+ +//! | Scrolling-region top (u16) | +//! 14 +-----------------------------------+ +//! | Scrolling-region bottom (u16) | +//! 16 +-----------------------------------+ +//! | Scrolling-region left (u16) | +//! 18 +-----------------------------------+ +//! | Scrolling-region right (u16) | +//! 20 +-----------------------------------+ +//! | Status display (u8) | +//! 21 +-----------------------------------+ +//! | Active screen key (u16) | +//! 23 +-----------------------------------+ +//! | Screen count (u16) | +//! 25 +-----------------------------------+ +//! | Previous codepoint (u32) | +//! 29 +-----------------------------------+ +//! | Cursor is-default (u8) | +//! 30 +-----------------------------------+ +//! | Cursor default visual style (u8) | +//! 31 +-----------------------------------+ +//! | Cursor default blink policy (u8) | +//! 32 +-----------------------------------+ +//! | Shell-redraw policy (u8) | +//! 33 +-----------------------------------+ +//! | Modify-other-keys level 2 (u8) | +//! 34 +-----------------------------------+ +//! | Mouse event (u8) | +//! 35 +-----------------------------------+ +//! | Mouse format (u8) | +//! 36 +-----------------------------------+ +//! | Mouse shift-capture policy (u8) | +//! 37 +-----------------------------------+ +//! | Mouse shape (u8) | +//! 38 +-----------------------------------+ +//! | Password-input flag (u8) | +//! 39 +-----------------------------------+ +//! | Current modes (ModePacked) | +//! 47 +-----------------------------------+ +//! | Saved modes (ModePacked) | +//! 55 +-----------------------------------+ +//! | Default modes (ModePacked) | +//! 63 +-----------------------------------+ +//! | Background color (DynamicRGB) | +//! 71 +-----------------------------------+ +//! | Foreground color (DynamicRGB) | +//! 79 +-----------------------------------+ +//! | Cursor color (DynamicRGB) | +//! 87 +-----------------------------------+ +//! | Maximum scrollback bytes (u64) | +//! 95 +-----------------------------------+ +//! | Maximum scrollback rows (u64) | +//! 103 +-----------------------------------+ +//! ``` +//! +//! The previous codepoint is a Unicode scalar value or `0xffffffff` when there +//! is no previous character. Boolean fields are zero or one. +//! Cursor blink and mouse shift-capture policies encode null as zero, false as +//! one, and true as two. +//! Canonical encoders use only the documented enum and boolean values. +//! Decoders replace unknown semantic values with neutral native defaults, +//! discard invalid previous codepoints, and reset each invalid scrolling axis +//! to the terminal's full extent. Dimensions and screen count remain +//! structural. +//! +//! A scrollback limit of `0xffffffffffffffff` means unlimited. Every other +//! value is finite, including zero. The values are the source primary +//! PageList's explicit policies, not its dimension-adjusted effective limits. +//! Alternate-screen no-scrollback behavior is derived from the screen key. +//! +//! ### Tab stops +//! +//! One bit is encoded for each column, least-significant bit first within each +//! byte. Bit zero of the first byte is column zero. Unused high bits in the last +//! byte are canonically zero and ignored by decoders. +//! +//! ### Dynamic RGB +//! +//! The three terminal dynamic colors share this layout: +//! +//! ```text +//! 0 +----------------------------+ +//! | Default present (u8) | +//! 1 +----------------------------+ +//! | Default RGB | +//! 4 +----------------------------+ +//! | Override present (u8) | +//! 5 +----------------------------+ +//! | Override RGB | +//! 8 +----------------------------+ +//! ``` +//! +//! Presence values are zero or one. The corresponding RGB bytes must be zero +//! when a value is absent. Decoders treat every other presence value as absent +//! and ignore RGB bytes belonging to an absent value. +//! +//! ### Palette +//! +//! The complete original 256-color palette is encoded first in index order. +//! Each mask bit then states whether the current palette entry overrides that +//! original value. Bit zero of the first mask byte is palette index zero. +//! Override RGB values follow in increasing index order with no index or count +//! fields. +//! +//! ### Modes +//! +//! Current, saved, and default modes use the same stable bit registry: +//! +//! ```text +//! bit 0 disable_keyboard +//! bit 1 insert +//! bit 2 send_receive_mode +//! bit 3 linefeed +//! bit 4 cursor_keys +//! bit 5 132_column +//! bit 6 slow_scroll +//! bit 7 reverse_colors +//! bit 8 origin +//! bit 9 wraparound +//! bit 10 autorepeat +//! bit 11 mouse_event_x10 +//! bit 12 cursor_blinking +//! bit 13 cursor_visible +//! bit 14 enable_mode_3 +//! bit 15 reverse_wrap +//! bit 16 alt_screen_legacy +//! bit 17 keypad_keys +//! bit 18 backarrow_key_mode +//! bit 19 enable_left_and_right_margin +//! bit 20 mouse_event_normal +//! bit 21 mouse_event_button +//! bit 22 mouse_event_any +//! bit 23 focus_event +//! bit 24 mouse_format_utf8 +//! bit 25 mouse_format_sgr +//! bit 26 mouse_alternate_scroll +//! bit 27 mouse_format_urxvt +//! bit 28 mouse_format_sgr_pixels +//! bit 29 ignore_keypad_with_numlock +//! bit 30 alt_esc_prefix +//! bit 31 alt_sends_escape +//! bit 32 reverse_wrap_extended +//! bit 33 alt_screen +//! bit 34 save_cursor +//! bit 35 alt_screen_save_cursor_clear_enter +//! bit 36 bracketed_paste +//! bit 37 synchronized_output +//! bit 38 grapheme_cluster +//! bit 39 report_color_scheme +//! bit 40 report_visibility +//! bit 41 in_band_size_reports +//! bit 42 kitty_paste_events +//! bits 43-63 reserved, zero +//! ``` +//! +//! This is the packed field order of native `ModePacked`. Its layout is +//! well-defined and is the snapshot registry. Moving or adding a native mode +//! therefore requires a snapshot version bump. Decoders ignore reserved bits. +//! +//! ## Field classification +//! +//! The TERMINAL payload encodes terminal dimensions, pixel dimensions, +//! scrolling region, status display, tab stops, PWD, title, color state, +//! previous character, all three mode sets, cursor defaults, mouse behavior, +//! password-input state, source scrollback policies, the active screen key, and +//! the set of declared screens. +//! +//! SCREEN and HISTORY sequences encode each Screen's pages, cursor, saved +//! cursor, charset, protected mode, Kitty keyboard stack, semantic-click state, +//! and complete history. The semantic-prompt `seen` bit is derived from +//! restored resident content. +//! +//! Native allocators, I/O implementations, pools, pointers, page IDs, byte +//! accounting, tracked pins, and ScreenSet generations are reconstructed. +//! PageList row totals and the active viewport are derived from decoded pages. +//! The destination surface supplies its own focus state. Selections, scrolled +//! viewports, selection-scroll activity, dirty flags, search flags, hyperlink +//! hover state, and incremental compression state are presentation or cache +//! state and reset during restore. +//! +//! Kitty image state and glyph glossary registrations are unsupported by this +//! snapshot version and are ignored during capture. Unicode virtual placeholder +//! cells are preserved as grid content, but their image and placement state is +//! not restored. Build-time terminal behavior, Unicode width policy, parser +//! continuation, and external callbacks are version-level or caller-local +//! requirements. +//! +//! Native enum declaration indices and mode bit positions used by this format +//! are snapshot-version registries. Changing any of them requires a snapshot +//! version bump. + +const std = @import("std"); +const Allocator = std.mem.Allocator; +const test_fixture = @import("fixture.zig"); +const io = @import("io.zig"); +const record = @import("record.zig"); +const terminal_ansi = @import("../ansi.zig"); +const terminal_color = @import("../color.zig"); +const terminal_modes = @import("../modes.zig"); +const terminal_mouse = @import("../mouse.zig"); +const terminal_osc = @import("../osc.zig"); +const Terminal = @import("../Terminal.zig"); +const TerminalScreen = @import("../Screen.zig"); +const TerminalScreenKey = @import("../ScreenSet.zig").Key; +const TerminalTabstops = @import("../Tabstops.zig"); +const ModeBits = @typeInfo( + terminal_modes.ModePacked, +).@"struct".backing_integer.?; + +/// Reuse the terminal's semantic RGB value without adopting its memory layout +/// as a wire encoding. +pub const RGB = terminal_color.RGB; + +/// Reuse the terminal's semantic dynamic color without adopting its memory +/// layout as a wire encoding. +pub const DynamicRGB = terminal_color.DynamicRGB; + +const ValidationError = error{ + InvalidDimensions, + InvalidScrollingRegion, + InvalidScreenCount, + InvalidActiveScreenKey, + InvalidPreviousCodepoint, + InvalidScrollbackLimit, +}; + +const HeaderInitError = error{ + ScrollbackLimitOverflow, +}; + +/// Errors possible while encoding a TERMINAL payload. +const PayloadEncodeError = std.Io.Writer.Error || + ValidationError || + error{ + InvalidTabStops, + InvalidPalette, + StringTooLong, + }; + +/// Errors possible while decoding a TERMINAL payload. +const PayloadDecodeError = std.Io.Reader.Error || + Allocator.Error || + error{ + /// Terminal dimensions control every following native allocation. + InvalidDimensions, + + /// The count determines how many SCREEN and HISTORY sequences follow. + InvalidScreenCount, + }; + +/// The fixed fields at the start of every TERMINAL payload. +pub const Header = struct { + /// Number of bytes written by `encode`, calculated using the encoder. + pub const len = computeLen(); + + comptime { + std.debug.assert(len == 103); + } + + // Terminal geometry and its current scrolling region. + columns: u16, + rows: u16, + width_px: u32, + height_px: u32, + + scrolling_region_top: u16, + scrolling_region_bottom: u16, + scrolling_region_left: u16, + scrolling_region_right: u16, + + // Status display, screen routing, and character context. + status_display: terminal_ansi.StatusDisplay, + active_screen_key: TerminalScreenKey, + screen_count: u16, + previous_codepoint: ?u21, + + // Cursor presentation defaults shared by the screens. + cursor_is_default: bool, + cursor_default_style: TerminalScreen.CursorStyle, + cursor_default_blink: ?bool, + + // Terminal input, semantic redraw, and pointer behavior. + shell_redraw: terminal_osc.semantic_prompt.Redraw, + modify_other_keys_2: bool, + mouse_event: terminal_mouse.Event, + mouse_format: terminal_mouse.Format, + mouse_shift_capture: ?bool, + mouse_shape: terminal_mouse.Shape, + password_input: bool, + + // Runtime, saved, and reset mode sets. + current_modes: terminal_modes.ModePacked, + saved_modes: terminal_modes.ModePacked, + default_modes: terminal_modes.ModePacked, + + // Terminal-wide dynamic color state. + background: DynamicRGB, + foreground: DynamicRGB, + cursor_color: DynamicRGB, + + // Explicit primary-screen scrollback policies. + max_scrollback_bytes: ?u64, + max_scrollback_rows: ?u64, + + /// Capture the terminal-wide native state represented by this header. + fn init(terminal: *const Terminal) HeaderInitError!Header { + const primary = terminal.screens.get(.primary).?; + + return .{ + // Terminal geometry and its current scrolling region. + .columns = terminal.cols, + .rows = terminal.rows, + .width_px = terminal.width_px, + .height_px = terminal.height_px, + .scrolling_region_top = terminal.scrolling_region.top, + .scrolling_region_bottom = terminal.scrolling_region.bottom, + .scrolling_region_left = terminal.scrolling_region.left, + .scrolling_region_right = terminal.scrolling_region.right, + + // Status display, screen routing, and character context. + .status_display = terminal.status_display, + .active_screen_key = terminal.screens.active_key, + .screen_count = if (terminal.screens.get(.alternate) == null) + 1 + else + 2, + .previous_codepoint = terminal.previous_char, + + // Cursor presentation defaults shared by the screens. + .cursor_is_default = terminal.cursor.is_default, + .cursor_default_style = terminal.cursor.default_style, + .cursor_default_blink = terminal.cursor.default_blink, + + // Terminal input, semantic redraw, and pointer behavior. + .shell_redraw = terminal.flags.shell_redraws_prompt, + .modify_other_keys_2 = terminal.flags.modify_other_keys_2, + .mouse_event = terminal.flags.mouse_event, + .mouse_format = terminal.flags.mouse_format, + .mouse_shift_capture = switch (terminal.flags.mouse_shift_capture) { + .null => null, + .false => false, + .true => true, + }, + .mouse_shape = terminal.mouse_shape, + .password_input = terminal.flags.password_input, + + // Runtime, saved, and reset mode sets. + .current_modes = terminal.modes.values, + .saved_modes = terminal.modes.saved, + .default_modes = terminal.modes.default, + + // Terminal-wide dynamic color state. + .background = terminal.colors.background, + .foreground = terminal.colors.foreground, + .cursor_color = terminal.colors.cursor, + + // Explicit primary-screen scrollback policies. + .max_scrollback_bytes = try encodeScrollbackLimit( + primary.pages.limits.bytes.explicit, + ), + .max_scrollback_rows = try encodeScrollbackLimit( + primary.pages.limits.lines.explicit, + ), + }; + } + + /// Encode the fixed TERMINAL payload header field by field. + pub fn encode( + self: Header, + writer: *std.Io.Writer, + ) PayloadEncodeError!void { + try self.validate(); + + // Terminal geometry and its current scrolling region. + try io.writeInt(writer, u16, self.columns); + try io.writeInt(writer, u16, self.rows); + try io.writeInt(writer, u32, self.width_px); + try io.writeInt(writer, u32, self.height_px); + try io.writeInt(writer, u16, self.scrolling_region_top); + try io.writeInt(writer, u16, self.scrolling_region_bottom); + try io.writeInt(writer, u16, self.scrolling_region_left); + try io.writeInt(writer, u16, self.scrolling_region_right); + + // Status display, screen routing, and character context. + try writer.writeByte(@intCast(@intFromEnum(self.status_display))); + try io.writeInt( + writer, + u16, + @intCast(@intFromEnum(self.active_screen_key)), + ); + try io.writeInt(writer, u16, self.screen_count); + try io.writeInt( + writer, + u32, + if (self.previous_codepoint) |value| + @intCast(value) + else + std.math.maxInt(u32), + ); + + // Cursor presentation defaults shared by the screens. + try writer.writeByte(@intFromBool(self.cursor_is_default)); + try writer.writeByte( + @intCast(@intFromEnum(self.cursor_default_style)), + ); + try writer.writeByte(encodeOptionalBool(self.cursor_default_blink)); + + // Terminal input, semantic redraw, and pointer behavior. + try writer.writeByte(@intCast(@intFromEnum(self.shell_redraw))); + try writer.writeByte(@intFromBool(self.modify_other_keys_2)); + try writer.writeByte(@intCast(@intFromEnum(self.mouse_event))); + try writer.writeByte(@intCast(@intFromEnum(self.mouse_format))); + try writer.writeByte(encodeOptionalBool(self.mouse_shift_capture)); + try writer.writeByte(@intCast(@intFromEnum(self.mouse_shape))); + try writer.writeByte(@intFromBool(self.password_input)); + + // Runtime, saved, and reset mode sets. ModePacked occupies 43 bits; + // its eight-byte wire slots zero-extend the native packed value. + const mode_values = [_]terminal_modes.ModePacked{ + self.current_modes, + self.saved_modes, + self.default_modes, + }; + for (mode_values) |value| { + const bits: ModeBits = @bitCast(value); + try io.writeInt(writer, u64, @intCast(bits)); + } + + // Terminal-wide dynamic color state. + try encodeDynamicRGB(self.background, writer); + try encodeDynamicRGB(self.foreground, writer); + try encodeDynamicRGB(self.cursor_color, writer); + + // Explicit primary-screen scrollback policies. + try io.writeInt( + writer, + u64, + self.max_scrollback_bytes orelse std.math.maxInt(u64), + ); + try io.writeInt( + writer, + u64, + self.max_scrollback_rows orelse std.math.maxInt(u64), + ); + } + + /// Decode the fixed TERMINAL payload header, normalizing semantic values. + pub fn decode(reader: *std.Io.Reader) PayloadDecodeError!Header { + // Terminal geometry and its current scrolling region. + const columns = try io.readInt(reader, u16); + const rows = try io.readInt(reader, u16); + const width_px = try io.readInt(reader, u32); + const height_px = try io.readInt(reader, u32); + const scrolling_region_top = try io.readInt(reader, u16); + const scrolling_region_bottom = try io.readInt(reader, u16); + const scrolling_region_left = try io.readInt(reader, u16); + const scrolling_region_right = try io.readInt(reader, u16); + + // Status display, screen routing, and character context. + const status_display = enumFromInt( + terminal_ansi.StatusDisplay, + try reader.takeByte(), + ) orelse .main; + const active_screen_key_raw = try io.readInt(reader, u16); + const screen_count = try io.readInt(reader, u16); + const previous_raw = try io.readInt(reader, u32); + const previous_codepoint: ?u21 = previous: { + if (previous_raw == std.math.maxInt(u32)) break :previous null; + const value = std.math.cast(u21, previous_raw) orelse + break :previous null; + if (value > 0x10FFFF or + (value >= 0xD800 and value <= 0xDFFF)) + { + break :previous null; + } + break :previous value; + }; + + // Cursor presentation defaults shared by the screens. + const cursor_is_default = switch (try reader.takeByte()) { + 0 => false, + 1 => true, + else => true, + }; + const cursor_default_style = enumFromInt( + TerminalScreen.CursorStyle, + try reader.takeByte(), + ) orelse .block; + const cursor_default_blink: ?bool = switch (try reader.takeByte()) { + 0 => null, + 1 => false, + 2 => true, + else => null, + }; + + // Terminal input, semantic redraw, and pointer behavior. + const shell_redraw = enumFromInt( + terminal_osc.semantic_prompt.Redraw, + try reader.takeByte(), + ) orelse .true; + const modify_other_keys_2 = switch (try reader.takeByte()) { + 0 => false, + 1 => true, + else => false, + }; + const mouse_event = enumFromInt( + terminal_mouse.Event, + try reader.takeByte(), + ) orelse .none; + const mouse_format = enumFromInt( + terminal_mouse.Format, + try reader.takeByte(), + ) orelse .x10; + const mouse_shift_capture: ?bool = switch (try reader.takeByte()) { + 0 => null, + 1 => false, + 2 => true, + else => null, + }; + const mouse_shape = enumFromInt( + terminal_mouse.Shape, + try reader.takeByte(), + ) orelse .text; + const password_input = switch (try reader.takeByte()) { + 0 => false, + 1 => true, + else => false, + }; + + // Runtime, saved, and reset mode sets. + var mode_values: [3]terminal_modes.ModePacked = undefined; + for (&mode_values) |*value| { + const raw = try io.readInt(reader, u64); + const bits: ModeBits = @truncate(raw); + value.* = @bitCast(bits); + } + + // Terminal-wide dynamic color state. + const background = try decodeDynamicRGB(reader); + const foreground = try decodeDynamicRGB(reader); + const cursor_color = try decodeDynamicRGB(reader); + + // Explicit primary-screen scrollback policies. + const max_scrollback_bytes = decodeOptionalLimit( + try io.readInt(reader, u64), + ); + const max_scrollback_rows = decodeOptionalLimit( + try io.readInt(reader, u64), + ); + + // Dimensions and sequence count are the only header fields that define + // following allocation or record boundaries. + if (columns == 0 or rows == 0) return error.InvalidDimensions; + if (screen_count != 1 and screen_count != 2) { + return error.InvalidScreenCount; + } + + const scrolling_region_vertical_valid = + scrolling_region_top <= scrolling_region_bottom and + scrolling_region_bottom < rows; + const scrolling_region_horizontal_valid = + scrolling_region_left <= scrolling_region_right and + scrolling_region_right < columns; + const active_screen_key: TerminalScreenKey = active: { + const decoded = enumFromInt( + TerminalScreenKey, + active_screen_key_raw, + ) orelse break :active .primary; + if (decoded == .alternate and screen_count != 2) { + break :active .primary; + } + break :active decoded; + }; + + const result: Header = .{ + .columns = columns, + .rows = rows, + .width_px = width_px, + .height_px = height_px, + + .scrolling_region_top = if (scrolling_region_vertical_valid) + scrolling_region_top + else + 0, + .scrolling_region_bottom = if (scrolling_region_vertical_valid) + scrolling_region_bottom + else + rows - 1, + .scrolling_region_left = if (scrolling_region_horizontal_valid) + scrolling_region_left + else + 0, + .scrolling_region_right = if (scrolling_region_horizontal_valid) + scrolling_region_right + else + columns - 1, + + .status_display = status_display, + .active_screen_key = active_screen_key, + .screen_count = screen_count, + .previous_codepoint = previous_codepoint, + + .cursor_is_default = cursor_is_default, + .cursor_default_style = cursor_default_style, + .cursor_default_blink = cursor_default_blink, + + .shell_redraw = shell_redraw, + .modify_other_keys_2 = modify_other_keys_2, + .mouse_event = mouse_event, + .mouse_format = mouse_format, + .mouse_shift_capture = mouse_shift_capture, + .mouse_shape = mouse_shape, + .password_input = password_input, + + .current_modes = mode_values[0], + .saved_modes = mode_values[1], + .default_modes = mode_values[2], + + .background = background, + .foreground = foreground, + .cursor_color = cursor_color, + + .max_scrollback_bytes = max_scrollback_bytes, + .max_scrollback_rows = max_scrollback_rows, + }; + return result; + } + + fn validate(self: Header) ValidationError!void { + if (self.columns == 0 or self.rows == 0) { + return error.InvalidDimensions; + } + if (self.scrolling_region_top > self.scrolling_region_bottom or + self.scrolling_region_bottom >= self.rows or + self.scrolling_region_left > self.scrolling_region_right or + self.scrolling_region_right >= self.columns) + { + return error.InvalidScrollingRegion; + } + + if (self.screen_count != 1 and self.screen_count != 2) { + return error.InvalidScreenCount; + } + if (self.active_screen_key == .alternate and self.screen_count != 2) { + return error.InvalidActiveScreenKey; + } + + if (self.previous_codepoint) |value| { + if (value > 0x10FFFF or + (value >= 0xD800 and value <= 0xDFFF)) + { + return error.InvalidPreviousCodepoint; + } + } + + if (self.max_scrollback_bytes == std.math.maxInt(u64) or + self.max_scrollback_rows == std.math.maxInt(u64)) + { + return error.InvalidScrollbackLimit; + } + } + + fn computeLen() usize { + comptime { + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + const value: Header = .{ + .columns = 1, + .rows = 1, + .width_px = 0, + .height_px = 0, + .scrolling_region_top = 0, + .scrolling_region_bottom = 0, + .scrolling_region_left = 0, + .scrolling_region_right = 0, + .status_display = .main, + .active_screen_key = .primary, + .screen_count = 1, + .previous_codepoint = null, + .cursor_is_default = true, + .cursor_default_style = .block, + .cursor_default_blink = null, + .shell_redraw = .true, + .modify_other_keys_2 = false, + .mouse_event = .none, + .mouse_format = .x10, + .mouse_shift_capture = null, + .mouse_shape = .text, + .password_input = false, + .current_modes = .{}, + .saved_modes = .{}, + .default_modes = .{}, + .background = .unset, + .foreground = .unset, + .cursor_color = .unset, + .max_scrollback_bytes = null, + .max_scrollback_rows = null, + }; + value.encode(&writer) catch unreachable; + return writer.end; + } + } +}; + +/// Allocator-owned semantic state decoded from one TERMINAL payload. +/// +/// The fixed header is a value. `tabstops`, `palette`, `pwd`, and `title` +/// own allocations and are released by `deinit`. +const DecodedPayload = struct { + header: Header, + tabstops: TerminalTabstops, + palette: terminal_color.DynamicPalette, + pwd: []u8, + title: []u8, + + fn deinit(self: *DecodedPayload, alloc: Allocator) void { + self.tabstops.deinit(alloc); + self.palette.deinit(alloc); + alloc.free(self.pwd); + alloc.free(self.title); + self.* = undefined; + } +}; + +/// Encode one complete TERMINAL payload without record framing. +fn encodePayload( + header: Header, + tabstops: *const TerminalTabstops, + palette: *const terminal_color.DynamicPalette, + pwd: []const u8, + title: []const u8, + writer: *std.Io.Writer, +) PayloadEncodeError!void { + // Validate the complete semantic value before writing any bytes. + try header.validate(); + if (tabstops.cols != @as(usize, header.columns)) { + return error.InvalidTabStops; + } + if (pwd.len > std.math.maxInt(u32) or + title.len > std.math.maxInt(u32)) + { + return error.StringTooLong; + } + for (palette.original, palette.current, 0..) |original, current, index| { + if (!palette.mask.isSet(index) and !original.eql(current)) { + return error.InvalidPalette; + } + } + + try header.encode(writer); + + // Tab stops are packed least-significant bit first. + const tabstop_len = (@as(usize, header.columns) + 7) / 8; + for (0..tabstop_len) |byte_index| { + var raw: u8 = 0; + for (0..8) |bit| { + const column = byte_index * 8 + bit; + if (column >= header.columns) break; + if (tabstops.get(column)) { + raw |= @as(u8, 1) << @intCast(bit); + } + } + try writer.writeByte(raw); + } + + // Encode the original palette, then a compact mask and only the active + // overrides in increasing palette-index order. + for (palette.original) |value| try encodeRGB(value, writer); + for (0..32) |byte_index| { + var raw: u8 = 0; + for (0..8) |bit| { + if (palette.mask.isSet(byte_index * 8 + bit)) { + raw |= @as(u8, 1) << @intCast(bit); + } + } + try writer.writeByte(raw); + } + for (palette.current, 0..) |value, index| { + if (palette.mask.isSet(index)) try encodeRGB(value, writer); + } + + // Length-prefixed terminal strings end the payload. + try io.writeInt(writer, u32, @intCast(pwd.len)); + try writer.writeAll(pwd); + try io.writeInt(writer, u32, @intCast(title.len)); + try writer.writeAll(title); +} + +/// Decode one complete allocator-owned TERMINAL payload. +/// +/// The enclosing record reader remains responsible for verifying exact payload +/// exhaustion. On success, the caller owns the result and must call `deinit`. +fn decodePayload( + reader: *std.Io.Reader, + alloc: Allocator, +) PayloadDecodeError!DecodedPayload { + const header = try Header.decode(reader); + + // Restore the native tab-stop representation from its packed wire bits. + var tabstops = try TerminalTabstops.init(alloc, header.columns, 0); + errdefer tabstops.deinit(alloc); + const tabstop_len = (@as(usize, header.columns) + 7) / 8; + for (0..tabstop_len) |byte_index| { + const raw = try reader.takeByte(); + for (0..8) |bit| { + const mask = @as(u8, 1) << @intCast(bit); + const column = byte_index * 8 + bit; + if (column < header.columns and raw & mask != 0) { + tabstops.set(column); + } + } + } + + // Reconstruct the dynamic palette from its original values and sparse + // current-value overrides. + var original: terminal_color.Palette = undefined; + for (&original) |*value| value.* = try decodeRGB(reader); + var override_mask: [32]u8 = undefined; + try reader.readSliceAll(&override_mask); + + var palette: terminal_color.DynamicPalette = try .init(alloc, original); + errdefer palette.deinit(alloc); + for (0..256) |index| { + const mask = @as(u8, 1) << @intCast(index % 8); + if (override_mask[index / 8] & mask != 0) { + palette.set(@intCast(index), try decodeRGB(reader)); + } + } + + // Read the two allocator-owned terminal strings last so all earlier + // fixed-size validation happens before allocating them. + const pwd_len: usize = @intCast(try io.readInt(reader, u32)); + const pwd = try alloc.alloc(u8, pwd_len); + errdefer alloc.free(pwd); + try reader.readSliceAll(pwd); + + const title_len: usize = @intCast(try io.readInt(reader, u32)); + const title = try alloc.alloc(u8, title_len); + errdefer alloc.free(title); + try reader.readSliceAll(title); + + return .{ + .header = header, + .tabstops = tabstops, + .palette = palette, + .pwd = pwd, + .title = title, + }; +} + +/// Errors possible while encoding one native TERMINAL record. +pub const EncodeError = HeaderInitError || + PayloadEncodeError || + record.Writer.FinishError; + +/// Encode terminal-wide native state as one framed TERMINAL record. +/// +/// State not represented by this snapshot version is ignored. Payload failures +/// emit no part of the TERMINAL record. +pub fn encode( + terminal: *const Terminal, + destination: *record.Writer, +) EncodeError!void { + const header = try Header.init(terminal); + + const payload = destination.begin(.terminal); + errdefer destination.cancel(); + try encodePayload( + header, + &terminal.tabstops, + &terminal.colors.palette, + terminal.getPwd() orelse "", + terminal.getTitle() orelse "", + payload, + ); + try destination.finish(); +} + +/// Errors possible while decoding one native TERMINAL record. +pub const DecodeError = PayloadDecodeError || + record.Reader.InitError || + record.Reader.FinishError || + error{ + /// The next record is valid but is not a TERMINAL. + UnexpectedRecordTag, + }; + +/// Decode one framed TERMINAL record directly into native terminal state. +/// +/// The returned terminal owns every decoded allocation and contains empty +/// primary and optional alternate screens with the declared routing. Later +/// SCREEN records replace those screens in place. On failure, all decoded and +/// partially initialized native state is released. +pub fn decode( + source: *std.Io.Reader, + io_: std.Io, + alloc: Allocator, +) DecodeError!Terminal { + // Finish the complete record before constructing native terminal state. + var payload: DecodedPayload = payload: { + var record_reader: record.Reader = undefined; + try record_reader.init(source); + if (record_reader.header.tag != .terminal) { + return error.UnexpectedRecordTag; + } + + var payload = try decodePayload(record_reader.payloadReader(), alloc); + errdefer payload.deinit(alloc); + try record_reader.finish(); + break :payload payload; + }; + defer payload.deinit(alloc); + + const header = payload.header; + const max_scrollback_bytes = nativeScrollbackLimit( + header.max_scrollback_bytes, + ); + const max_scrollback_lines = nativeScrollbackLimit( + header.max_scrollback_rows, + ); + + // Terminal.init establishes native pools, pins, defaults, and ownership. + var result = try Terminal.init(io_, alloc, .{ + .cols = header.columns, + .rows = header.rows, + .max_scrollback_bytes = max_scrollback_bytes, + .max_scrollback_lines = max_scrollback_lines, + .colors = .{ + .background = header.background, + .foreground = header.foreground, + .cursor = header.cursor_color, + .palette = payload.palette, + }, + .default_modes = header.default_modes, + .default_cursor_style = header.cursor_default_style, + .default_cursor_blink = header.cursor_default_blink, + }); + errdefer result.deinit(alloc); + + // The terminal owns the decoded palette now, so the payload must not + // release it. + payload.palette = .default; + + // Recreate the optional screen now so ScreenSet routing is complete before + // its empty screens are replaced by the following SCREEN records. + if (header.screen_count == 2) { + _ = try result.screens.getInit(io_, alloc, .alternate, .{ + .cols = header.columns, + .rows = header.rows, + .max_scrollback_bytes = 0, + }); + } + result.screens.switchTo(header.active_screen_key); + + // Restore terminal-wide runtime state that is not an initialization + // policy. Presentation-only flags retain Terminal.init defaults. + result.width_px = header.width_px; + result.height_px = header.height_px; + result.scrolling_region = .{ + .top = header.scrolling_region_top, + .bottom = header.scrolling_region_bottom, + .left = header.scrolling_region_left, + .right = header.scrolling_region_right, + }; + result.status_display = header.status_display; + result.previous_char = header.previous_codepoint; + result.cursor = .{ + .is_default = header.cursor_is_default, + .default_style = header.cursor_default_style, + .default_blink = header.cursor_default_blink, + }; + result.modes = .{ + .values = header.current_modes, + .saved = header.saved_modes, + .default = header.default_modes, + }; + result.flags.shell_redraws_prompt = header.shell_redraw; + result.flags.modify_other_keys_2 = header.modify_other_keys_2; + result.flags.mouse_event = header.mouse_event; + result.flags.mouse_format = header.mouse_format; + result.flags.mouse_shift_capture = if (header.mouse_shift_capture) |value| + if (value) .true else .false + else + .null; + result.flags.password_input = header.password_input; + result.mouse_shape = header.mouse_shape; + + // Native strings retain trailing NUL sentinels. Setters construct those + // without exposing that detail in the wire format. + try result.setPwd(payload.pwd); + try result.setTitle(payload.title); + + // Transfer decoded tab-stop ownership last. The deferred payload cleanup + // releases the table originally allocated by Terminal.init. + const initialized_tabstops = result.tabstops; + result.tabstops = payload.tabstops; + payload.tabstops = initialized_tabstops; + + result.screens.active.pages.assertIntegrity(); + return result; +} + +fn enumFromInt(comptime T: type, raw: anytype) ?T { + const Tag = @typeInfo(T).@"enum".tag_type; + const value = std.math.cast(Tag, raw) orelse return null; + return std.enums.fromInt(T, value); +} + +fn encodeOptionalBool(value: ?bool) u8 { + return if (value) |present| + if (present) 2 else 1 + else + 0; +} + +fn encodeRGB( + value: RGB, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + try writer.writeByte(value.r); + try writer.writeByte(value.g); + try writer.writeByte(value.b); +} + +fn decodeRGB(reader: *std.Io.Reader) std.Io.Reader.Error!RGB { + return .{ + .r = try reader.takeByte(), + .g = try reader.takeByte(), + .b = try reader.takeByte(), + }; +} + +fn encodeDynamicRGB( + value: DynamicRGB, + writer: *std.Io.Writer, +) std.Io.Writer.Error!void { + if (value.default) |rgb| { + try writer.writeByte(1); + try encodeRGB(rgb, writer); + } else { + try writer.writeByte(0); + try writer.splatByteAll(0, 3); + } + + if (value.override) |rgb| { + try writer.writeByte(1); + try encodeRGB(rgb, writer); + } else { + try writer.writeByte(0); + try writer.splatByteAll(0, 3); + } +} + +fn decodeDynamicRGB( + reader: *std.Io.Reader, +) std.Io.Reader.Error!DynamicRGB { + const default_present = try reader.takeByte(); + const default_rgb = try decodeRGB(reader); + const default: ?RGB = if (default_present == 1) default_rgb else null; + + const override_present = try reader.takeByte(); + const override_rgb = try decodeRGB(reader); + const override: ?RGB = if (override_present == 1) override_rgb else null; + + return .{ .default = default, .override = override }; +} + +fn decodeOptionalLimit(raw: u64) ?u64 { + return if (raw == std.math.maxInt(u64)) null else raw; +} + +fn encodeScrollbackLimit(value: usize) HeaderInitError!?u64 { + if (value == std.math.maxInt(usize)) return null; + const result = std.math.cast(u64, value) orelse + return error.ScrollbackLimitOverflow; + if (result == std.math.maxInt(u64)) { + return error.ScrollbackLimitOverflow; + } + return result; +} + +fn nativeScrollbackLimit( + value: ?u64, +) ?usize { + const present = value orelse return null; + return @intCast(@min( + present, + @as(u64, std.math.maxInt(usize) - 1), + )); +} + +const test_header: Header = header: { + var current_modes = std.mem.zeroes(terminal_modes.ModePacked); + current_modes.disable_keyboard = true; + var saved_modes = std.mem.zeroes(terminal_modes.ModePacked); + saved_modes.in_band_size_reports = true; + var default_modes = current_modes; + default_modes.in_band_size_reports = true; + + break :header .{ + .columns = 0x0102, + .rows = 0x0304, + .width_px = 0x05060708, + .height_px = 0x090a0b0c, + + .scrolling_region_top = 1, + .scrolling_region_bottom = 2, + .scrolling_region_left = 3, + .scrolling_region_right = 4, + + .status_display = .status_line, + .active_screen_key = .alternate, + .screen_count = 2, + .previous_codepoint = 'A', + + .cursor_is_default = true, + .cursor_default_style = .block_hollow, + .cursor_default_blink = true, + + .shell_redraw = .last, + .modify_other_keys_2 = true, + .mouse_event = .any, + .mouse_format = .sgr_pixels, + .mouse_shift_capture = false, + .mouse_shape = .zoom_out, + .password_input = true, + + .current_modes = current_modes, + .saved_modes = saved_modes, + .default_modes = default_modes, + + .background = .{ + .default = .{ .r = 1, .g = 2, .b = 3 }, + .override = null, + }, + .foreground = .{ + .default = null, + .override = .{ .r = 4, .g = 5, .b = 6 }, + }, + .cursor_color = .{ + .default = .{ .r = 7, .g = 8, .b = 9 }, + .override = .{ .r = 10, .g = 11, .b = 12 }, + }, + + .max_scrollback_bytes = null, + .max_scrollback_rows = 0x0102030405060708, + }; +}; + +const test_header_fixture = test_fixture.parse( + @embedFile("testdata/terminal-header-v1.hex"), +); + +test "TERMINAL mode bit layout" { + try std.testing.expectEqual( + @as(usize, 43), + @bitSizeOf(terminal_modes.ModePacked), + ); + + var first: terminal_modes.ModePacked = std.mem.zeroes( + terminal_modes.ModePacked, + ); + first.disable_keyboard = true; + try std.testing.expectEqual( + @as(u43, 1) << 0, + @as(u43, @bitCast(first)), + ); + + var visibility: terminal_modes.ModePacked = std.mem.zeroes( + terminal_modes.ModePacked, + ); + visibility.report_visibility = true; + try std.testing.expectEqual( + @as(u43, 1) << 40, + @as(u43, @bitCast(visibility)), + ); + + var size_reports: terminal_modes.ModePacked = std.mem.zeroes( + terminal_modes.ModePacked, + ); + size_reports.in_band_size_reports = true; + try std.testing.expectEqual( + @as(u43, 1) << 41, + @as(u43, @bitCast(size_reports)), + ); + + var last: terminal_modes.ModePacked = std.mem.zeroes( + terminal_modes.ModePacked, + ); + last.kitty_paste_events = true; + try std.testing.expectEqual( + @as(u43, 1) << 42, + @as(u43, @bitCast(last)), + ); +} + +test "TERMINAL header golden encoding and decoding" { + const testing = std.testing; + + var encoded: [Header.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try test_header.encode(&writer); + try test_fixture.expectEqual( + .bytes, + "src/terminal/snapshot/testdata/terminal-header-v1.hex", + "snapshot_fixture-terminal-header-v1.hex", + &test_header_fixture, + writer.buffered(), + ); + try testing.expectEqual(Header.len, test_header_fixture.len); + + // Exercise the streaming path with less buffered data than every integer. + var source: std.Io.Reader = .fixed(&test_header_fixture); + var buffer: [1]u8 = undefined; + var limited = source.limited(.unlimited, &buffer); + try testing.expectEqualDeep( + test_header, + try Header.decode(&limited.interface), + ); + + for (0..Header.len) |fixture_len| { + var truncated: std.Io.Reader = .fixed( + test_header_fixture[0..fixture_len], + ); + try testing.expectError( + error.EndOfStream, + Header.decode(&truncated), + ); + } +} + +test "TERMINAL header encoding rejects noncanonical values" { + const testing = std.testing; + + // Semantic values are validated before encoding writes anything. + var invalid_header = test_header; + invalid_header.columns = 0; + var encoded: [Header.len]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try testing.expectError( + error.InvalidDimensions, + invalid_header.encode(&writer), + ); + try testing.expectEqual(@as(usize, 0), writer.end); + + invalid_header = test_header; + invalid_header.max_scrollback_bytes = std.math.maxInt(u64); + writer = .fixed(&encoded); + try testing.expectError( + error.InvalidScrollbackLimit, + invalid_header.encode(&writer), + ); + try testing.expectEqual(@as(usize, 0), writer.end); +} + +test "TERMINAL header decoding rejects structural values" { + const testing = std.testing; + + var invalid_dimensions = test_header_fixture; + invalid_dimensions[0] = 0; + invalid_dimensions[1] = 0; + var dimensions_reader: std.Io.Reader = .fixed(&invalid_dimensions); + try testing.expectError( + error.InvalidDimensions, + Header.decode(&dimensions_reader), + ); + + var invalid_screen_count = test_header_fixture; + invalid_screen_count[23] = 3; + var screen_count_reader: std.Io.Reader = .fixed(&invalid_screen_count); + try testing.expectError( + error.InvalidScreenCount, + Header.decode(&screen_count_reader), + ); +} + +test "TERMINAL header decoding normalizes semantic values" { + const testing = std.testing; + + var fixture = test_header_fixture; + + // Unknown enum and boolean encodings use their neutral native defaults. + fixture[20] = 2; + fixture[21] = 2; + fixture[22] = 0; + fixture[29] = 2; + fixture[30] = 4; + fixture[31] = 3; + fixture[32] = 3; + fixture[33] = 2; + fixture[34] = 5; + fixture[35] = 5; + fixture[36] = 3; + fixture[37] = 34; + fixture[38] = 2; + + // Reserved mode bits are ignored while known low bits survive. + fixture[44] = 4; + + // Unknown presence and nonzero absent-value bytes both become absent. + fixture[63] = 2; + fixture[68] = 1; + fixture[72] = 0xAA; + + // An invalid scrolling region becomes the full terminal region. + fixture[14] = 0x04; + fixture[15] = 0x03; + + // Surrogates cannot become native previous-character state. + fixture[25] = 0x00; + fixture[26] = 0xD8; + fixture[27] = 0x00; + fixture[28] = 0x00; + + var reader: std.Io.Reader = .fixed(&fixture); + const decoded = try Header.decode(&reader); + try testing.expectEqual(terminal_ansi.StatusDisplay.main, decoded.status_display); + try testing.expectEqual(TerminalScreenKey.primary, decoded.active_screen_key); + try testing.expectEqual(@as(u16, 0), decoded.scrolling_region_top); + try testing.expectEqual(test_header.rows - 1, decoded.scrolling_region_bottom); + try testing.expectEqual(test_header.scrolling_region_left, decoded.scrolling_region_left); + try testing.expectEqual(test_header.scrolling_region_right, decoded.scrolling_region_right); + try testing.expectEqual(null, decoded.previous_codepoint); + try testing.expect(decoded.cursor_is_default); + try testing.expectEqual(TerminalScreen.CursorStyle.block, decoded.cursor_default_style); + try testing.expectEqual(null, decoded.cursor_default_blink); + try testing.expectEqual(terminal_osc.semantic_prompt.Redraw.true, decoded.shell_redraw); + try testing.expect(!decoded.modify_other_keys_2); + try testing.expectEqual(terminal_mouse.Event.none, decoded.mouse_event); + try testing.expectEqual(terminal_mouse.Format.x10, decoded.mouse_format); + try testing.expectEqual(null, decoded.mouse_shift_capture); + try testing.expectEqual(terminal_mouse.Shape.text, decoded.mouse_shape); + try testing.expect(!decoded.password_input); + try testing.expect(decoded.current_modes.disable_keyboard); + try testing.expectEqualDeep(DynamicRGB.unset, decoded.background); + try testing.expectEqual(null, decoded.foreground.default); + try testing.expectEqual( + RGB{ .r = 4, .g = 5, .b = 6 }, + decoded.foreground.override.?, + ); + + // Each scrolling axis is normalized independently. + var invalid_horizontal = test_header_fixture; + invalid_horizontal[18] = 0x02; + invalid_horizontal[19] = 0x01; + var horizontal_reader: std.Io.Reader = .fixed(&invalid_horizontal); + const horizontal = try Header.decode(&horizontal_reader); + try testing.expectEqual( + test_header.scrolling_region_top, + horizontal.scrolling_region_top, + ); + try testing.expectEqual( + test_header.scrolling_region_bottom, + horizontal.scrolling_region_bottom, + ); + try testing.expectEqual(@as(u16, 0), horizontal.scrolling_region_left); + try testing.expectEqual( + test_header.columns - 1, + horizontal.scrolling_region_right, + ); + + // Alternate cannot be active when only the primary screen is declared. + var missing_active_screen = test_header_fixture; + missing_active_screen[23] = 1; + var active_screen_reader: std.Io.Reader = .fixed(&missing_active_screen); + try testing.expectEqual( + TerminalScreenKey.primary, + (try Header.decode(&active_screen_reader)).active_screen_key, + ); + + const largest_finite = std.math.maxInt(u64) - 1; + try testing.expectEqual( + @as(usize, @intCast(@min( + largest_finite, + @as(u64, std.math.maxInt(usize) - 1), + ))), + nativeScrollbackLimit(largest_finite).?, + ); +} + +test "TERMINAL payload round trip" { + const testing = std.testing; + + // Include a partial final tab-stop byte so its bit order and padding are + // both exercised. + var tabstops = try TerminalTabstops.init( + testing.allocator, + test_header.columns, + 0, + ); + defer tabstops.deinit(testing.allocator); + tabstops.set(0); + tabstops.set(8); + tabstops.set(test_header.columns - 1); + + // Use overrides at both ends of the palette to make ordering explicit. + var palette: terminal_color.DynamicPalette = .default; + palette.set(0, .{ .r = 1, .g = 2, .b = 3 }); + palette.set(255, .{ .r = 4, .g = 5, .b = 6 }); + + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + try encodePayload( + test_header, + &tabstops, + &palette, + "file:///tmp/example", + "snapshot title", + &destination.writer, + ); + + // Tab stops and the sparse palette mask both use least-significant-bit + // first ordering. + const bytes = destination.written(); + const tabstop_len = (@as(usize, test_header.columns) + 7) / 8; + try testing.expectEqual(@as(u8, 0x01), bytes[Header.len]); + try testing.expectEqual(@as(u8, 0x01), bytes[Header.len + 1]); + try testing.expectEqual( + @as(u8, 0x02), + bytes[Header.len + tabstop_len - 1], + ); + const palette_mask_offset = Header.len + tabstop_len + 256 * 3; + try testing.expectEqual(@as(u8, 0x01), bytes[palette_mask_offset]); + try testing.expectEqual( + @as(u8, 0x80), + bytes[palette_mask_offset + 31], + ); + + // Decode through a one-byte reader buffer to cover streaming of the large + // fixed palette and allocator-owned strings. + var source: std.Io.Reader = .fixed(bytes); + var buffer: [1]u8 = undefined; + var limited = source.limited(.unlimited, &buffer); + var decoded = try decodePayload( + &limited.interface, + testing.allocator, + ); + defer decoded.deinit(testing.allocator); + + try testing.expectEqualDeep(test_header, decoded.header); + for (0..test_header.columns) |column| { + try testing.expectEqual( + tabstops.get(column), + decoded.tabstops.get(column), + ); + } + try testing.expectEqualDeep(palette, decoded.palette); + try testing.expectEqualStrings("file:///tmp/example", decoded.pwd); + try testing.expectEqualStrings("snapshot title", decoded.title); +} + +test "TERMINAL payload rejects noncanonical state" { + const testing = std.testing; + + // A tab-stop set for different dimensions cannot represent this header. + var tabstops = try TerminalTabstops.init( + testing.allocator, + test_header.columns - 1, + 0, + ); + defer tabstops.deinit(testing.allocator); + var palette: terminal_color.DynamicPalette = .default; + var encoded: [2048]u8 = undefined; + var writer: std.Io.Writer = .fixed(&encoded); + try testing.expectError( + error.InvalidTabStops, + encodePayload( + test_header, + &tabstops, + &palette, + "", + "", + &writer, + ), + ); + try testing.expectEqual(@as(usize, 0), writer.end); + + // Unmasked current colors must remain equal to their originals. + tabstops.cols = test_header.columns; + palette.current[7] = .{ .r = 1, .g = 2, .b = 3 }; + writer = .fixed(&encoded); + try testing.expectError( + error.InvalidPalette, + encodePayload( + test_header, + &tabstops, + &palette, + "", + "", + &writer, + ), + ); + try testing.expectEqual(@as(usize, 0), writer.end); +} + +test "TERMINAL payload ignores tab-stop padding" { + const testing = std.testing; + + var tabstops = try TerminalTabstops.init( + testing.allocator, + test_header.columns, + 0, + ); + defer tabstops.deinit(testing.allocator); + var palette: terminal_color.DynamicPalette = .default; + palette.set(1, .{ .r = 1, .g = 2, .b = 3 }); + + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + try encodePayload( + test_header, + &tabstops, + &palette, + "pwd", + "title", + &destination.writer, + ); + + // Only two bits in the last byte correspond to terminal columns. + const tabstop_len = (@as(usize, test_header.columns) + 7) / 8; + const last_tabstop = Header.len + tabstop_len - 1; + const invalid_padding = try testing.allocator.dupe( + u8, + destination.written(), + ); + defer testing.allocator.free(invalid_padding); + invalid_padding[last_tabstop] |= 1 << 7; + var padding_reader: std.Io.Reader = .fixed(invalid_padding); + var decoded = try decodePayload( + &padding_reader, + testing.allocator, + ); + defer decoded.deinit(testing.allocator); + for (0..test_header.columns) |column| { + try testing.expectEqual( + tabstops.get(column), + decoded.tabstops.get(column), + ); + } +} + +test "TERMINAL record encodes native terminal state" { + const testing = std.testing; + + var terminal = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 10, + .rows = 3, + .max_scrollback_bytes = 12_345, + .max_scrollback_lines = 67, + }); + defer terminal.deinit(testing.allocator); + + // Exercise every native header section plus sentinel-backed strings. + terminal.width_px = 800; + terminal.height_px = 600; + terminal.scrolling_region = .{ + .top = 1, + .bottom = 2, + .left = 2, + .right = 8, + }; + terminal.status_display = .status_line; + terminal.previous_char = 'X'; + terminal.cursor = .{ + .is_default = false, + .default_style = .underline, + .default_blink = true, + }; + terminal.flags.shell_redraws_prompt = .last; + terminal.flags.modify_other_keys_2 = true; + terminal.flags.mouse_event = .button; + terminal.flags.mouse_format = .sgr; + terminal.flags.mouse_shift_capture = .true; + terminal.flags.password_input = true; + terminal.mouse_shape = .pointer; + terminal.modes.values.disable_keyboard = true; + terminal.modes.saved.in_band_size_reports = true; + terminal.modes.default.bracketed_paste = true; + terminal.colors.background = .{ + .default = .{ .r = 1, .g = 2, .b = 3 }, + .override = .{ .r = 4, .g = 5, .b = 6 }, + }; + terminal.colors.palette.set(7, .{ .r = 7, .g = 8, .b = 9 }); + terminal.tabstops.reset(0); + terminal.tabstops.set(1); + terminal.tabstops.set(9); + try terminal.setPwd("file:///tmp/native"); + try terminal.setTitle("native title"); + + // Encode and restore through the public record codec. + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + var stream: record.Writer = .init( + testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try encode(&terminal, &stream); + + var source: std.Io.Reader = .fixed(destination.written()); + var restored = try decode( + &source, + testing.io, + testing.allocator, + ); + defer restored.deinit(testing.allocator); + + try testing.expectEqual(@as(u16, 10), restored.cols); + try testing.expectEqual(@as(u16, 3), restored.rows); + try testing.expectEqual(@as(u32, 800), restored.width_px); + try testing.expectEqual(@as(u32, 600), restored.height_px); + try testing.expectEqual(terminal.scrolling_region, restored.scrolling_region); + try testing.expectEqual(terminal.status_display, restored.status_display); + try testing.expectEqual(.primary, restored.screens.active_key); + try testing.expect(restored.screens.get(.alternate) == null); + try testing.expectEqual(@as(?u21, 'X'), restored.previous_char); + try testing.expectEqualDeep(terminal.cursor, restored.cursor); + try testing.expectEqual( + terminal.flags.shell_redraws_prompt, + restored.flags.shell_redraws_prompt, + ); + try testing.expectEqual( + terminal.flags.modify_other_keys_2, + restored.flags.modify_other_keys_2, + ); + try testing.expectEqual( + terminal.flags.mouse_event, + restored.flags.mouse_event, + ); + try testing.expectEqual( + terminal.flags.mouse_format, + restored.flags.mouse_format, + ); + try testing.expectEqual( + terminal.flags.mouse_shift_capture, + restored.flags.mouse_shift_capture, + ); + try testing.expectEqual( + terminal.flags.password_input, + restored.flags.password_input, + ); + try testing.expectEqual(terminal.mouse_shape, restored.mouse_shape); + try testing.expectEqualDeep(terminal.modes, restored.modes); + try testing.expectEqualDeep(terminal.colors, restored.colors); + try testing.expectEqual( + @as(usize, 12_345), + restored.screens.active.pages.limits.bytes.explicit, + ); + try testing.expectEqual( + @as(usize, 67), + restored.screens.active.pages.limits.lines.explicit, + ); + try testing.expect(restored.tabstops.get(1)); + try testing.expect(restored.tabstops.get(9)); + try testing.expectEqualStrings( + "file:///tmp/native", + restored.getPwd().?, + ); + try testing.expectEqualStrings("native title", restored.getTitle().?); +} + +test "TERMINAL record declares an initialized alternate screen" { + const testing = std.testing; + + var terminal = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 10, + .rows = 3, + }); + defer terminal.deinit(testing.allocator); + _ = try terminal.switchScreen(.alternate); + + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + var stream: record.Writer = .init( + testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + try encode(&terminal, &stream); + + var source: std.Io.Reader = .fixed(destination.written()); + var restored = try decode( + &source, + testing.io, + testing.allocator, + ); + defer restored.deinit(testing.allocator); + + const primary = restored.screens.get(.primary).?; + const alternate = restored.screens.get(.alternate).?; + try testing.expectEqual(.alternate, restored.screens.active_key); + try testing.expectEqual(alternate, restored.screens.active); + try testing.expect(primary != alternate); + try testing.expectEqual(@as(u16, 10), alternate.pages.cols); + try testing.expectEqual(@as(u16, 3), alternate.pages.rows); + try testing.expectEqual( + @as(usize, 0), + alternate.pages.limits.bytes.explicit, + ); +} + +test "TERMINAL payload failure emits no incomplete record" { + const testing = std.testing; + + var terminal = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 10, + .rows = 3, + }); + defer terminal.deinit(testing.allocator); + + var destination: std.Io.Writer.Allocating = .init(testing.allocator); + defer destination.deinit(); + try destination.writer.writeAll("prefix"); + var stream: record.Writer = .init( + testing.allocator, + &destination.writer, + ); + defer stream.deinit(); + + // Payload validation occurs in the reusable record scratch buffer. The + // invalid TERMINAL is never emitted to the destination. + terminal.colors.palette.current[7] = .{ .r = 1, .g = 2, .b = 3 }; + try testing.expectError( + error.InvalidPalette, + encode(&terminal, &stream), + ); + try testing.expectEqualStrings("prefix", destination.written()); +} + +test "TERMINAL record decoding rejects malformed input transactionally" { + const testing = std.testing; + + // A valid record of another type is not accepted as TERMINAL state. + var wrong_tag: std.Io.Writer.Allocating = .init(testing.allocator); + defer wrong_tag.deinit(); + var wrong_tag_stream: record.Writer = .init( + testing.allocator, + &wrong_tag.writer, + ); + defer wrong_tag_stream.deinit(); + { + _ = wrong_tag_stream.begin(.screen); + errdefer wrong_tag_stream.cancel(); + try wrong_tag_stream.finish(); + } + var wrong_tag_source: std.Io.Reader = .fixed(wrong_tag.written()); + try testing.expectError( + error.UnexpectedRecordTag, + decode(&wrong_tag_source, testing.io, testing.allocator), + ); + + // Build one canonical record for exhaustive truncation and allocation + // failure checks below. + var terminal = try Terminal.init(testing.io, testing.allocator, .{ + .cols = 10, + .rows = 3, + }); + defer terminal.deinit(testing.allocator); + try terminal.setPwd("pwd"); + try terminal.setTitle("title"); + + var encoded: std.Io.Writer.Allocating = .init(testing.allocator); + defer encoded.deinit(); + var encoded_stream: record.Writer = .init( + testing.allocator, + &encoded.writer, + ); + defer encoded_stream.deinit(); + try encode(&terminal, &encoded_stream); + + for (0..encoded.written().len) |fixture_len| { + var source: std.Io.Reader = .fixed( + encoded.written()[0..fixture_len], + ); + var restored = decode( + &source, + testing.io, + testing.allocator, + ) catch continue; + restored.deinit(testing.allocator); + try testing.expect(false); + } + + var failing = testing.FailingAllocator.init( + testing.allocator, + .{ .fail_index = 0 }, + ); + var failing_source: std.Io.Reader = .fixed(encoded.written()); + try testing.expectError( + error.OutOfMemory, + decode(&failing_source, testing.io, failing.allocator()), + ); +} diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/checkpoint-ready-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/checkpoint-ready-v1.hex new file mode 100644 index 00000000..3ff7c5d0 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/checkpoint-ready-v1.hex @@ -0,0 +1,10 @@ +# Ghostty snapshot fixture +# Kaitai type: checkpoint_record +# Kaitai params: 5 +# Kaitai offset: 3 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +61 62 63 05 00 00 00 00 00 e4 20 ef 0a # 0x00000000 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/complete-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/complete-v1.hex new file mode 100644 index 00000000..1fa59f61 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/complete-v1.hex @@ -0,0 +1,120 @@ +# Ghostty snapshot fixture +# Kaitai type: ghostty_snapshot +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: snapshot envelope +47 48 4f 53 54 53 4e 50 01 00 # 0x00000000 + +# offset 0x0000000a: terminal record, payload 952 bytes +01 00 b8 03 00 00 e3 5f 8b cc 02 00 03 00 20 03 # 0x0000000a +00 00 58 02 00 00 00 00 02 00 00 00 01 00 00 01 # 0x0000001a +00 02 00 65 00 00 00 01 01 01 00 00 00 00 00 08 # 0x0000002a +00 04 22 00 64 10 00 00 00 04 22 00 64 00 00 00 # 0x0000003a +00 04 22 00 64 00 00 00 00 00 00 00 00 00 00 00 # 0x0000004a +00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # 0x0000005a +00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff # 0x0000006a +ff 00 1d 1f 21 cc 66 66 b5 bd 68 f0 c6 74 81 a2 # 0x0000007a +be b2 94 bb 8a be b7 c5 c8 c6 66 66 66 d5 4e 53 # 0x0000008a +b9 ca 4a e7 c5 47 7a a6 da c3 97 d8 70 c0 b1 ea # 0x0000009a +ea ea 00 00 00 00 00 5f 00 00 87 00 00 af 00 00 # 0x000000aa +d7 00 00 ff 00 5f 00 00 5f 5f 00 5f 87 00 5f af # 0x000000ba +00 5f d7 00 5f ff 00 87 00 00 87 5f 00 87 87 00 # 0x000000ca +87 af 00 87 d7 00 87 ff 00 af 00 00 af 5f 00 af # 0x000000da +87 00 af af 00 af d7 00 af ff 00 d7 00 00 d7 5f # 0x000000ea +00 d7 87 00 d7 af 00 d7 d7 00 d7 ff 00 ff 00 00 # 0x000000fa +ff 5f 00 ff 87 00 ff af 00 ff d7 00 ff ff 5f 00 # 0x0000010a +00 5f 00 5f 5f 00 87 5f 00 af 5f 00 d7 5f 00 ff # 0x0000011a +5f 5f 00 5f 5f 5f 5f 5f 87 5f 5f af 5f 5f d7 5f # 0x0000012a +5f ff 5f 87 00 5f 87 5f 5f 87 87 5f 87 af 5f 87 # 0x0000013a +d7 5f 87 ff 5f af 00 5f af 5f 5f af 87 5f af af # 0x0000014a +5f af d7 5f af ff 5f d7 00 5f d7 5f 5f d7 87 5f # 0x0000015a +d7 af 5f d7 d7 5f d7 ff 5f ff 00 5f ff 5f 5f ff # 0x0000016a +87 5f ff af 5f ff d7 5f ff ff 87 00 00 87 00 5f # 0x0000017a +87 00 87 87 00 af 87 00 d7 87 00 ff 87 5f 00 87 # 0x0000018a +5f 5f 87 5f 87 87 5f af 87 5f d7 87 5f ff 87 87 # 0x0000019a +00 87 87 5f 87 87 87 87 87 af 87 87 d7 87 87 ff # 0x000001aa +87 af 00 87 af 5f 87 af 87 87 af af 87 af d7 87 # 0x000001ba +af ff 87 d7 00 87 d7 5f 87 d7 87 87 d7 af 87 d7 # 0x000001ca +d7 87 d7 ff 87 ff 00 87 ff 5f 87 ff 87 87 ff af # 0x000001da +87 ff d7 87 ff ff af 00 00 af 00 5f af 00 87 af # 0x000001ea +00 af af 00 d7 af 00 ff af 5f 00 af 5f 5f af 5f # 0x000001fa +87 af 5f af af 5f d7 af 5f ff af 87 00 af 87 5f # 0x0000020a +af 87 87 af 87 af af 87 d7 af 87 ff af af 00 af # 0x0000021a +af 5f af af 87 af af af af af d7 af af ff af d7 # 0x0000022a +00 af d7 5f af d7 87 af d7 af af d7 d7 af d7 ff # 0x0000023a +af ff 00 af ff 5f af ff 87 af ff af af ff d7 af # 0x0000024a +ff ff d7 00 00 d7 00 5f d7 00 87 d7 00 af d7 00 # 0x0000025a +d7 d7 00 ff d7 5f 00 d7 5f 5f d7 5f 87 d7 5f af # 0x0000026a +d7 5f d7 d7 5f ff d7 87 00 d7 87 5f d7 87 87 d7 # 0x0000027a +87 af d7 87 d7 d7 87 ff d7 af 00 d7 af 5f d7 af # 0x0000028a +87 d7 af af d7 af d7 d7 af ff d7 d7 00 d7 d7 5f # 0x0000029a +d7 d7 87 d7 d7 af d7 d7 d7 d7 d7 ff d7 ff 00 d7 # 0x000002aa +ff 5f d7 ff 87 d7 ff af d7 ff d7 d7 ff ff ff 00 # 0x000002ba +00 ff 00 5f ff 00 87 ff 00 af ff 00 d7 ff 00 ff # 0x000002ca +ff 5f 00 ff 5f 5f ff 5f 87 ff 5f af ff 5f d7 ff # 0x000002da +5f ff ff 87 00 ff 87 5f ff 87 87 ff 87 af ff 87 # 0x000002ea +d7 ff 87 ff ff af 00 ff af 5f ff af 87 ff af af # 0x000002fa +ff af d7 ff af ff ff d7 00 ff d7 5f ff d7 87 ff # 0x0000030a +d7 af ff d7 d7 ff d7 ff ff ff 00 ff ff 5f ff ff # 0x0000031a +87 ff ff af ff ff d7 ff ff ff 08 08 08 12 12 12 # 0x0000032a +1c 1c 1c 26 26 26 30 30 30 3a 3a 3a 44 44 44 4e # 0x0000033a +4e 4e 58 58 58 62 62 62 6c 6c 6c 76 76 76 80 80 # 0x0000034a +80 8a 8a 8a 94 94 94 9e 9e 9e a8 a8 a8 b2 b2 b2 # 0x0000035a +bc bc bc c6 c6 c6 d0 d0 d0 da da da e4 e4 e4 ee # 0x0000036a +ee ee 80 00 00 00 00 00 00 00 00 00 00 00 00 00 # 0x0000037a +00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # 0x0000038a +00 00 01 02 03 14 00 00 00 66 69 6c 65 3a 2f 2f # 0x0000039a +2f 74 6d 70 2f 73 6e 61 70 73 68 6f 74 11 00 00 # 0x000003aa +00 63 6f 6d 70 6c 65 74 65 20 73 6e 61 70 73 68 # 0x000003ba +6f 74 # 0x000003ca + +# offset 0x000003cc: screen record, payload 54 bytes +02 00 36 00 00 00 67 31 0e c6 00 00 01 00 04 00 # 0x000003cc +00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 00 # 0x000003dc +00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # 0x000003ec +00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # 0x000003fc + +# offset 0x0000040c: page record, payload 36 bytes +03 00 24 00 00 00 a2 4f 26 d1 02 00 03 00 00 00 # 0x0000040c +00 00 10 00 c0 00 00 04 00 00 00 08 00 00 00 01 # 0x0000041c +00 43 00 01 00 44 00 01 00 45 00 00 00 00 # 0x0000042c + +# offset 0x0000043a: screen record, payload 54 bytes +02 00 36 00 00 00 ce 1f 9f 08 01 00 01 00 00 00 # 0x0000043a +00 00 00 00 00 00 01 00 02 00 01 00 00 00 00 00 # 0x0000044a +00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # 0x0000045a +00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # 0x0000046a + +# offset 0x0000047a: page record, payload 38 bytes +03 00 26 00 00 00 62 1c ab 7b 02 00 03 00 00 00 # 0x0000047a +00 00 80 00 c0 00 00 02 00 00 00 08 00 00 03 02 # 0x0000048a +00 72 6e 03 02 00 61 74 02 01 00 65 00 00 00 00 # 0x0000049a + +# offset 0x000004aa: continuation record, payload 0 bytes +07 00 00 00 00 00 27 80 63 d1 # 0x000004aa + +# offset 0x000004b4: ready record, payload 0 bytes +05 00 00 00 00 00 e4 20 ef 0a # 0x000004b4 + +# offset 0x000004be: history record, payload 6 bytes +04 00 06 00 00 00 20 32 ed e1 00 00 02 00 00 00 # 0x000004be + +# offset 0x000004ce: page record, payload 31 bytes +03 00 1f 00 00 00 4a ed 2f c2 02 00 02 00 00 00 # 0x000004ce +00 00 10 00 c0 00 00 04 00 00 00 08 00 00 00 01 # 0x000004de +00 42 00 00 00 00 00 00 00 # 0x000004ee + +# offset 0x000004f7: page record, payload 31 bytes +03 00 1f 00 00 00 23 6a 6b 19 02 00 02 00 00 00 # 0x000004f7 +00 00 10 00 c0 00 00 04 00 00 00 08 00 00 00 01 # 0x00000507 +00 41 00 00 00 00 00 00 00 # 0x00000517 + +# offset 0x00000520: history record, payload 6 bytes +04 00 06 00 00 00 b8 7a ba b1 01 00 00 00 00 00 # 0x00000520 + +# offset 0x00000530: finish record, payload 0 bytes +06 00 00 00 00 00 3e eb 53 3e # 0x00000530 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-apc-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-apc-v1.hex new file mode 100644 index 00000000..4e3054bb --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-apc-v1.hex @@ -0,0 +1,8 @@ +# Ghostty snapshot fixture +# Kaitai type: continuation_record +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 + +# Incomplete Kitty APC payload. +07 00 07 00 00 00 bd ff a8 ff 1b 5f 47 64 61 74 61 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-csi-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-csi-v1.hex new file mode 100644 index 00000000..b83aceca --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-csi-v1.hex @@ -0,0 +1,8 @@ +# Ghostty snapshot fixture +# Kaitai type: continuation_record +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 + +# Incomplete SGR CSI. +07 00 04 00 00 00 30 d5 20 17 1b 5b 33 31 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-dcs-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-dcs-v1.hex new file mode 100644 index 00000000..e8e3ebda --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-dcs-v1.hex @@ -0,0 +1,8 @@ +# Ghostty snapshot fixture +# Kaitai type: continuation_record +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 + +# Incomplete supported DCS payload. +07 00 07 00 00 00 63 ae 0a 1d 1b 50 71 64 61 74 61 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-esc-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-esc-v1.hex new file mode 100644 index 00000000..0ca1d5b1 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-esc-v1.hex @@ -0,0 +1,8 @@ +# Ghostty snapshot fixture +# Kaitai type: continuation_record +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 + +# Bare ESC leaves the VT parser unfinished. +07 00 01 00 00 00 1c d9 1d 17 1b diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-ground-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-ground-v1.hex new file mode 100644 index 00000000..42463c35 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-ground-v1.hex @@ -0,0 +1,8 @@ +# Ghostty snapshot fixture +# Kaitai type: continuation_record +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 + +# Empty payload is the explicit ground-state assertion. +07 00 00 00 00 00 27 80 63 d1 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-osc-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-osc-v1.hex new file mode 100644 index 00000000..10d6aef4 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-osc-v1.hex @@ -0,0 +1,8 @@ +# Ghostty snapshot fixture +# Kaitai type: continuation_record +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 + +# Incomplete title OSC. +07 00 09 00 00 00 39 7d 6e 08 1b 5d 32 3b 74 69 74 6c 65 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-utf8-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-utf8-v1.hex new file mode 100644 index 00000000..dc07c803 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/continuation-utf8-v1.hex @@ -0,0 +1,8 @@ +# Ghostty snapshot fixture +# Kaitai type: continuation_record +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 + +# Partial four-byte UTF-8 codepoint. +07 00 03 00 00 00 b8 07 2a 22 f0 9f 98 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/envelope-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/envelope-v1.hex new file mode 100644 index 00000000..04a7eb6d --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/envelope-v1.hex @@ -0,0 +1,10 @@ +# Ghostty snapshot fixture +# Kaitai type: envelope +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +47 48 4f 53 54 53 4e 50 01 00 # 0x00000000 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/grid-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/grid-v1.hex new file mode 100644 index 00000000..b9a74af3 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/grid-v1.hex @@ -0,0 +1,15 @@ +# Ghostty snapshot fixture +# Kaitai type: grid +# Kaitai params: 4 3 +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +34 03 00 04 01 00 00 00 94 00 00 00 00 00 00 00 # 0x00000000 +48 00 00 e1 01 00 00 00 00 00 00 3b 03 00 1e 00 # 0x00000010 +00 00 00 00 00 00 ab ee 32 03 00 10 00 00 00 00 # 0x00000020 +00 00 00 0c 00 00 00 03 00 68 00 69 10 02 00 16 # 0x00000030 +04 21 00 01 00 00 00 00 00 02 00 02 00 01 03 00 # 0x00000040 +00 02 03 00 00 # 0x00000050 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/history-header-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/history-header-v1.hex new file mode 100644 index 00000000..dc8333e5 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/history-header-v1.hex @@ -0,0 +1,10 @@ +# Ghostty snapshot fixture +# Kaitai type: history_payload +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +01 00 04 03 02 01 # 0x00000000 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/hyperlink-explicit-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/hyperlink-explicit-v1.hex new file mode 100644 index 00000000..7643b203 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/hyperlink-explicit-v1.hex @@ -0,0 +1,10 @@ +# Ghostty snapshot fixture +# Kaitai type: hyperlink +# Kaitai params: false false +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +02 02 00 00 00 69 64 03 00 00 00 75 72 69 # 0x00000000 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/hyperlink-implicit-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/hyperlink-implicit-v1.hex new file mode 100644 index 00000000..cb15cf12 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/hyperlink-implicit-v1.hex @@ -0,0 +1,10 @@ +# Ghostty snapshot fixture +# Kaitai type: hyperlink +# Kaitai params: false false +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +01 04 03 02 01 03 00 00 00 75 72 69 # 0x00000000 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-empty-record-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-empty-record-v1.hex new file mode 100644 index 00000000..cda4f1d9 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-empty-record-v1.hex @@ -0,0 +1,12 @@ +# Ghostty snapshot fixture +# Kaitai type: page_record +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +03 00 1b 00 00 00 5e 0d 0a d4 01 00 01 00 00 00 # 0x00000000 +00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # 0x00000010 +00 00 00 00 00 # 0x00000020 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-header-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-header-v1.hex new file mode 100644 index 00000000..b7b00765 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-header-v1.hex @@ -0,0 +1,11 @@ +# Ghostty snapshot fixture +# Kaitai type: page_header +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +02 01 04 03 06 05 08 07 0a 09 0c 0b 10 0f 0e 0d # 0x00000000 +14 13 12 11 # 0x00000010 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-v1.hex new file mode 100644 index 00000000..78018ebb --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/page-v1.hex @@ -0,0 +1,22 @@ +# Ghostty snapshot fixture +# Kaitai type: page_payload +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: PAGE header +03 00 02 00 02 00 02 00 08 00 00 02 80 00 00 00 # 0x00000000 +00 01 00 00 # 0x00000010 + +# offset 0x00000014: style and hyperlink tables, rows, and cells +01 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 # 0x00000014 +00 00 03 00 00 00 00 00 01 2a 00 00 00 00 00 00 # 0x00000024 +00 00 00 00 01 00 02 01 00 00 00 61 05 00 00 00 # 0x00000034 +61 6c 70 68 61 03 00 01 04 03 02 01 04 00 00 00 # 0x00000044 +62 65 74 61 34 03 00 04 01 00 04 00 b4 01 00 00 # 0x00000054 +00 00 0c 00 68 03 00 1e 00 00 04 00 20 01 00 3b # 0x00000064 +03 00 e1 01 00 00 00 00 00 00 ab ee 32 03 00 10 # 0x00000074 +00 00 00 00 00 00 00 0c 00 00 01 00 00 00 01 00 # 0x00000084 +00 00 02 00 01 03 00 00 02 03 00 00 # 0x00000094 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/record-page-header-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/record-page-header-v1.hex new file mode 100644 index 00000000..28e6269a --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/record-page-header-v1.hex @@ -0,0 +1,10 @@ +# Ghostty snapshot fixture +# Kaitai type: record_header +# Kaitai params: 3 +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +03 00 18 00 00 00 1b 44 78 71 # 0x00000000 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/screen-header-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/screen-header-v1.hex new file mode 100644 index 00000000..7a29df76 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/screen-header-v1.hex @@ -0,0 +1,13 @@ +# Ghostty snapshot fixture +# Kaitai type: screen_header +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +01 00 03 02 08 07 06 05 04 03 02 01 05 04 07 06 # 0x00000000 +03 19 00 00 00 00 01 7f 00 00 02 12 34 56 ff 03 # 0x00000010 +00 00 0d 0c 0b 0a e4 3d 02 07 01 02 04 08 10 1f # 0x00000020 +00 11 02 03 01 # 0x00000030 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/screen-saved-cursor-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/screen-saved-cursor-v1.hex new file mode 100644 index 00000000..f4bd7e3a --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/screen-saved-cursor-v1.hex @@ -0,0 +1,11 @@ +# Ghostty snapshot fixture +# Kaitai type: saved_cursor +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +02 01 04 03 00 00 00 00 00 00 00 00 00 00 00 00 # 0x00000000 +00 00 00 00 07 e4 3d # 0x00000010 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/style-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/style-v1.hex new file mode 100644 index 00000000..0e834f87 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/style-v1.hex @@ -0,0 +1,10 @@ +# Ghostty snapshot fixture +# Kaitai type: style +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +00 00 00 00 01 7f 00 00 02 12 34 56 ff 03 00 00 # 0x00000000 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/testdata/terminal-header-v1.hex b/vendor/libghostty-vt/src/terminal/snapshot/testdata/terminal-header-v1.hex new file mode 100644 index 00000000..9c9eb4bf --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/testdata/terminal-header-v1.hex @@ -0,0 +1,16 @@ +# Ghostty snapshot fixture +# Kaitai type: terminal_header +# Kaitai params: +# Kaitai offset: 0 +# Wire version: 1 +# Generated by its snapshot test; review before replacing. +# On mismatch, the candidate is copied to the repository root. + +# offset 0x00000000: encoded bytes +02 01 04 03 08 07 06 05 0c 0b 0a 09 01 00 02 00 # 0x00000000 +03 00 04 00 01 01 00 02 00 41 00 00 00 01 03 02 # 0x00000010 +02 01 04 04 01 21 01 01 00 00 00 00 00 00 00 00 # 0x00000020 +00 00 00 00 02 00 00 01 00 00 00 00 02 00 00 01 # 0x00000030 +01 02 03 00 00 00 00 00 00 00 00 01 04 05 06 01 # 0x00000040 +07 08 09 01 0a 0b 0c ff ff ff ff ff ff ff ff 08 # 0x00000050 +07 06 05 04 03 02 01 # 0x00000060 diff --git a/vendor/libghostty-vt/src/terminal/snapshot/verify-kaitai.py b/vendor/libghostty-vt/src/terminal/snapshot/verify-kaitai.py new file mode 100755 index 00000000..65df64c9 --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/snapshot/verify-kaitai.py @@ -0,0 +1,400 @@ +#!/usr/bin/env python3 +"""Compile the snapshot Kaitai schema and parse every checked-in fixture. + +Run this from a Ghostty development shell: + + src/terminal/snapshot/verify-kaitai.py + +To turn an annotated fixture into a binary suitable for the Kaitai Web IDE: + + src/terminal/snapshot/verify-kaitai.py \ + --write-binary \ + src/terminal/snapshot/testdata/complete-v1.hex \ + /tmp/complete-v1.bin + +Then load `snapshot.ksy` and the generated binary into +https://ide.kaitai.io/. + +The generated Python parser exists only in a temporary directory. This keeps +snapshot.ksy as the source of truth and ensures this check cannot accidentally +pass against stale generated code. After structural parsing, the script checks +record CRC32C values and cross-record invariants that portable Kaitai +expressions cannot represent. +""" + +from __future__ import annotations + +import argparse +import importlib +import io +import shutil +import struct +import subprocess +import sys +import tempfile +from dataclasses import dataclass +from pathlib import Path +from typing import Any + +try: + from kaitaistruct import KaitaiStream +except ImportError as error: + raise SystemExit( + "missing Kaitai verifier dependencies; run this inside " + "`nix develop`" + ) from error + + +SNAPSHOT_DIR = Path(__file__).resolve().parent +SCHEMA_PATH = SNAPSHOT_DIR / "snapshot.ksy" +TESTDATA_DIR = SNAPSHOT_DIR / "testdata" +SNAPSHOT_ENVELOPE_SIZE = 10 +RECORD_HEADER_SIZE = struct.calcsize(" Fixture: + """Load one self-describing annotated hexadecimal fixture.""" + metadata: dict[str, str] = {} + chunks: list[str] = [] + for line in path.read_text(encoding="utf-8").splitlines(): + content, separator, comment = line.partition("#") + chunks.extend(content.split()) + + if not separator: + continue + comment = comment.strip() + if not comment.startswith("Kaitai "): + continue + key, colon, value = comment.removeprefix("Kaitai ").partition(":") + if not colon or key not in {"type", "params", "offset"}: + raise ValueError(f"{path}: invalid Kaitai metadata: {comment}") + if key in metadata: + raise ValueError(f"{path}: duplicate Kaitai {key} metadata") + metadata[key] = value.strip() + + try: + data = bytes.fromhex(" ".join(chunks)) + except ValueError as error: + raise ValueError(f"{path}: invalid annotated hexadecimal") from error + + required = {"type", "params", "offset"} + if metadata.keys() != required: + missing = sorted(required - metadata.keys()) + extra = sorted(metadata.keys() - required) + raise ValueError( + f"{path}: Kaitai metadata drift: missing={missing}, extra={extra}" + ) + if not metadata["type"]: + raise ValueError(f"{path}: empty Kaitai type") + + params: list[object] = [] + for value in metadata["params"].split(): + if value == "true": + params.append(True) + elif value == "false": + params.append(False) + else: + try: + params.append(int(value, 0)) + except ValueError as error: + raise ValueError( + f"{path}: invalid Kaitai parameter {value!r}" + ) from error + + try: + offset = int(metadata["offset"], 0) + except ValueError as error: + raise ValueError(f"{path}: invalid Kaitai offset") from error + if offset < 0 or offset > len(data): + raise ValueError(f"{path}: Kaitai offset is outside the fixture") + + return Fixture( + path=path, + type_name=metadata["type"], + params=tuple(params), + offset=offset, + data=data, + ) + + +def parse_type( + parser_type: type[Any], + data: bytes, + *params: object, +) -> Any: + """Construct one generated Kaitai type and require exact exhaustion.""" + stream = KaitaiStream(io.BytesIO(data)) + result = parser_type(*params, stream) + if not stream.is_eof(): + raise ValueError( + f"{parser_type.__name__} left " + f"{stream.size() - stream.pos()} trailing bytes" + ) + return result + + +def crc32c(data: bytes) -> int: + """Return CRC32C using the reflected Castagnoli polynomial.""" + result = 0xFFFFFFFF + for byte in data: + result ^= byte + for _ in range(8): + result = ( + (result >> 1) ^ 0x82F63B78 + if result & 1 + else result >> 1 + ) + return result ^ 0xFFFFFFFF + + +def validate_record(record: Any, data: bytes, offset: int) -> int: + """Validate one parsed record's source bytes and CRC32C.""" + payload = getattr(record, "_raw_payload", record.payload) + header = record.header + if header.payload_length != len(payload): + raise ValueError( + f"record at 0x{offset:x}: declared {header.payload_length} " + f"payload bytes but parsed {len(payload)}" + ) + + encoded_header = data[offset : offset + RECORD_HEADER_SIZE] + expected_header = struct.pack( + " list[Any]: + """Return complete-snapshot records in wire order.""" + records = [snapshot.terminal] + for sequence in snapshot.screens: + records.append(sequence.screen) + records.extend(sequence.pages) + records.append(snapshot.continuation) + records.append(snapshot.ready) + for sequence in snapshot.histories: + records.append(sequence.history) + records.extend(sequence.pages) + records.append(snapshot.finish) + return records + + +def validate_complete_snapshot(snapshot: Any, data: bytes) -> None: + """Validate ordering relationships, record CRCs, and markers.""" + screen_keys = [ + sequence.screen.payload.header.key for sequence in snapshot.screens + ] + history_keys = [ + sequence.history.payload.key for sequence in snapshot.histories + ] + if len(set(screen_keys)) != len(screen_keys): + raise ValueError("complete snapshot contains a duplicate SCREEN key") + if len(set(history_keys)) != len(history_keys): + raise ValueError("complete snapshot contains a duplicate HISTORY key") + if set(screen_keys) != set(history_keys): + raise ValueError("SCREEN and HISTORY keys do not match") + + terminal_header = snapshot.terminal.payload.header + expected_key_values = ( + {0} if terminal_header.screen_count == 1 else {0, 1} + ) + screen_key_values = {key.value for key in screen_keys} + if screen_key_values != expected_key_values: + raise ValueError( + f"TERMINAL declares screen keys {expected_key_values}, " + f"but SCREEN records contain {screen_key_values}" + ) + + screens_by_key = { + sequence.screen.payload.header.key: sequence + for sequence in snapshot.screens + } + for sequence in snapshot.screens: + header = sequence.screen.payload.header + if ( + header.cursor_x >= terminal_header.columns + or header.cursor_y >= terminal_header.rows + or ( + header.cursor_flags.pending_wrap + and header.cursor_x != terminal_header.columns - 1 + ) + ): + raise ValueError(f"SCREEN key {header.key}: invalid cursor position") + + for sequence in snapshot.histories: + payload = sequence.history.payload + screen = screens_by_key[payload.key] + screen_rows = sum( + page.payload.header.rows for page in screen.pages + ) + if screen_rows < terminal_header.rows: + raise ValueError( + f"SCREEN key {payload.key}: PAGE rows do not cover " + "the active area" + ) + overlap_rows = screen_rows - terminal_header.rows + history_rows = overlap_rows + sum( + page.payload.header.rows for page in sequence.pages + ) + if history_rows != screen.screen.payload.header.history_rows: + raise ValueError( + f"SCREEN key {payload.key}: history_rows " + f"{screen.screen.payload.header.history_rows} " + f"does not match {history_rows}" + ) + + for record in all_snapshot_records(snapshot): + if not hasattr(record, "payload"): + continue + payload = record.payload + if not hasattr(payload, "styles"): + continue + style_ids = [entry.encoded_id for entry in payload.styles] + hyperlink_ids = [entry.encoded_id for entry in payload.hyperlinks] + if len(set(style_ids)) != len(style_ids): + raise ValueError("PAGE contains a duplicate style ID") + if len(set(hyperlink_ids)) != len(hyperlink_ids): + raise ValueError("PAGE contains a duplicate hyperlink ID") + + offset = SNAPSHOT_ENVELOPE_SIZE + for record in all_snapshot_records(snapshot): + offset = validate_record(record, data, offset) + + if offset != len(data): + raise ValueError( + f"complete snapshot accounted for {offset} of {len(data)} bytes" + ) + + +def compile_schema(output_dir: Path) -> type[Any]: + """Compile snapshot.ksy and import its temporary Python parser.""" + compiler = shutil.which("kaitai-struct-compiler") or shutil.which("ksc") + if compiler is None: + raise SystemExit( + "kaitai-struct-compiler is not available; run this inside " + "`nix develop`" + ) + + subprocess.run( + [ + compiler, + "--target=python", + f"--outdir={output_dir}", + str(SCHEMA_PATH), + ], + check=True, + ) + + sys.path.insert(0, str(output_dir)) + try: + module = importlib.import_module("ghostty_snapshot") + finally: + sys.path.pop(0) + return module.GhosttySnapshot + + +def resolve_type(parser: type[Any], type_name: str) -> type[Any]: + """Resolve one KSY snake-case type name on the generated parser.""" + root_type_name = "".join( + f"_{character.lower()}" if character.isupper() else character + for character in parser.__name__ + ).lstrip("_") + if type_name == root_type_name: + return parser + + class_name = "".join( + part[:1].upper() + part[1:] for part in type_name.split("_") + ) + try: + return getattr(parser, class_name) + except AttributeError as error: + raise ValueError(f"snapshot.ksy has no type {type_name!r}") from error + + +def main() -> int: + cli = argparse.ArgumentParser(description=__doc__) + cli.add_argument( + "--write-binary", + nargs=2, + metavar=("FIXTURE", "OUTPUT"), + type=Path, + help="decode one annotated fixture into a raw binary", + ) + args = cli.parse_args() + + if args.write_binary: + fixture_path, output_path = args.write_binary + fixture = load_fixture(fixture_path) + output_path.write_bytes(fixture.data) + print(f"wrote {len(fixture.data)} bytes to {output_path}") + return 0 + + with tempfile.TemporaryDirectory(prefix="ghostty-kaitai-") as temp: + parser = compile_schema(Path(temp)) + + fixture_paths = sorted(TESTDATA_DIR.rglob("*.hex")) + if not fixture_paths: + raise ValueError(f"{TESTDATA_DIR}: no snapshot fixtures") + + for path in fixture_paths: + fixture = load_fixture(path) + parser_type = resolve_type(parser, fixture.type_name) + fixture_label = path.relative_to(TESTDATA_DIR) + try: + parsed = parse_type( + parser_type, + fixture.data[fixture.offset:], + *fixture.params, + ) + except Exception as error: + raise ValueError( + f"{fixture_label}: Kaitai parse failed" + ) from error + + if parser_type is parser: + if fixture.offset != 0: + raise ValueError( + f"{fixture_label}: root parser must begin at offset zero" + ) + validate_complete_snapshot(parsed, fixture.data) + elif hasattr(parsed, "_raw_payload"): + record_end = validate_record( + parsed, + fixture.data, + fixture.offset, + ) + if record_end != len(fixture.data): + raise ValueError( + f"{fixture_label}: record has trailing data" + ) + print(f"ok {fixture_label}") + + print(f"validated {len(fixture_paths)} snapshot fixtures") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/vendor/libghostty-vt/src/terminal/stream.zig b/vendor/libghostty-vt/src/terminal/stream.zig index efb06ecd..29bdde7d 100644 --- a/vendor/libghostty-vt/src/terminal/stream.zig +++ b/vendor/libghostty-vt/src/terminal/stream.zig @@ -16,6 +16,7 @@ const kitty = @import("kitty.zig"); const modes = @import("modes.zig"); const osc = @import("osc.zig"); const sgr = @import("sgr.zig"); +const continuationpkg = @import("stream_continuation.zig"); const UTF8Decoder = @import("UTF8Decoder.zig"); const MouseShape = @import("mouse.zig").Shape; @@ -109,7 +110,7 @@ pub const Action = union(Key) { dcs_put: u8, dcs_unhook, apc_start, - apc_end, + apc_end: ApcEnd, apc_put: u8, apc_put_slice: ApcPutSlice, end_hyperlink, @@ -127,6 +128,8 @@ pub const Action = union(Key) { kitty_color_report: kitty.color.OSC, color_operation: ColorOperation, semantic_prompt: SemanticPrompt, + kitty_clipboard: KittyClipboard, + kitty_dnd: KittyDnd, pub const Key = lib.Enum( lib.target, @@ -226,6 +229,8 @@ pub const Action = union(Key) { "kitty_color_report", "color_operation", "semantic_prompt", + "kitty_clipboard", + "kitty_dnd", }, ); @@ -235,7 +240,7 @@ pub const Action = union(Key) { @This(), // TODO: Before shipping an ABI-compatible libghostty, verify this. // This was just arbitrarily chosen for now. - [16]u64, + .{ .padding = [16]u64 }, ); pub const Tag = c_union.Tag; pub const Value = c_union.Value; @@ -290,6 +295,11 @@ pub const Action = union(Key) { } }; + pub const ApcEnd = extern struct { + /// False when CAN, SUB, or another aborting transition ended the APC. + terminated: bool, + }; + pub const InvokeCharset = lib.Struct(lib.target, struct { bank: charsets.ActiveSlot, charset: charsets.Slots, @@ -406,6 +416,7 @@ pub const Action = union(Key) { pub const ClipboardContents = struct { kind: u8, data: []const u8, + terminator: osc.Terminator, pub const C = extern struct { kind: u8, @@ -438,6 +449,10 @@ pub const Action = union(Key) { }; pub const SemanticPrompt = osc.Command.SemanticPrompt; + + pub const KittyClipboard = osc.Command.KittyClipboardProtocol; + + pub const KittyDnd = osc.Command.KittyDndProtocol; }; /// Returns a type that can process a stream of tty control characters. @@ -476,46 +491,177 @@ pub fn Stream(comptime H: type) type { handler: Handler, parser: Parser, utf8decoder: UTF8Decoder, + continuation: ?continuationpkg.Tracker, + + pub const Options = struct { + /// The handler initial value, must be set. + handler: Handler, + + /// Allocator to use. If this is not set then the stream + /// will be fully allocation free. There are some operations + /// that will be dropped in this case such as OSC 52 clipboard + /// ops. + allocator: ?Allocator = null, + + /// Maximum size in bytes of the continuation suffix. If this is + /// null or zero then continuation tracking is disabled. This is + /// only applied when `allocator` is non-null; without an allocator + /// continuation tracking is disabled. Feeding this continuation + /// suffix into an equivalent stream at ground reconstructs the + /// unfinished state without repeating committed terminal effects. + /// Continuation tracking is only supported by TerminalStream. + continuation_max_bytes: ?usize = null, + }; - /// Initialize an allocation-free stream. This will preallocate various - /// sizes as necessary and anything over that will be dropped. If you - /// want to support more dynamic behavior use initAlloc instead. + /// Initialize a stream. Without an allocator, operations that require + /// heap allocation are dropped. /// /// As a concrete example of something that requires heap allocation, /// consider OSC 52 (clipboard operations) which can be arbitrarily /// large. /// - /// If you want to limit allocation size, use an allocator with - /// a size limit with initAlloc. - /// /// This takes ownership of the handler and will call deinit /// when the stream is deinitialized. - pub fn init(h: Handler) Self { + pub fn init(options: Options) Self { + // Initialize the parser + var parser: Parser = .init(); + if (options.allocator) |alloc| parser.osc_parser.alloc = alloc; + + // Initialize the continuation tracker if one is requested. + var tracker: ?continuationpkg.Tracker = null; + if (options.allocator) |alloc| { + if (options.continuation_max_bytes) |max_bytes| { + if (max_bytes > 0) tracker = .init(alloc, max_bytes); + } + } + return .{ - .handler = h, - .parser = .init(), + .handler = options.handler, + .parser = parser, .utf8decoder = .{}, + .continuation = tracker, }; } - /// Initialize the stream that supports heap allocation as necessary. - pub fn initAlloc(alloc: Allocator, h: Handler) Self { - var self: Self = .init(h); - self.parser.osc_parser.alloc = alloc; - return self; - } - pub fn deinit(self: *Self) void { + if (self.continuation) |*tracker| tracker.deinit(); self.parser.deinit(); self.handler.deinit(); } + /// Write the current continuation suffix directly to a caller-owned + /// writer. The caller must pause and serialize access to this Stream. + pub fn writeContinuation( + self: *const Self, + writer: *std.Io.Writer, + ) (std.Io.Writer.Error || error{ + ContinuationDisabled, + ContinuationUnavailable, + })!void { + const tracker = self.continuation orelse + return error.ContinuationDisabled; + if (tracker.broken) + return error.ContinuationUnavailable; + try tracker.write(writer); + } + + /// True when no continuation suffix is needed to reproduce the + /// stream's current parsing state. + pub inline fn ground(self: *const Self) bool { + // Parser ground alone is not sufficient because the UTF-8 + // decoder may have some state. + return self.parser.state == .ground and self.utf8decoder.state == 0; + } + + /// Update the continuation suffix after one complete feed call. + /// Must only be called when tracking is enabled. + fn trackContinuation(self: *Self, input: []const u8) void { + const tracker = &self.continuation.?; + + // If we're in a ground state, we have no continuation suffix + // to track by definition. + if (self.ground()) { + tracker.reset(); + return; + } + + // Retain the part of this feed needed to replay the unfinished + // state. When the parser is grounded here, the feed must have + // ended inside a UTF-8 codepoint instead, because the ground + // check above covers both state machines. + tracker.append( + if (self.parser.state != .ground) .vt else .utf8, + input, + ); + } + /// Process a string of characters. pub inline fn nextSlice(self: *Self, input: []const u8) void { - // Disable SIMD optimizations if build requests it or if our - // manual debug mode is on. - if (comptime debug or !build_options.simd) { - for (input) |c| self.next(c); + self.nextSliceUntracked(input); + + // Continuation tracking is opt-in and this branch predicts + // perfectly, so disabled streams pay nothing else here. + if (self.continuation != null) self.trackContinuation(input); + } + + /// Process a string of characters, but only the shortest prefix + /// needed to reach the ground state. + /// + /// The ground state is when the stream isn't in the middle of any + /// type of sequence: UTF-8, ESC, CSI, OSC, etc. It is the stateless + /// point of the stream. + /// + /// If the stream is already at ground then this consumes nothing + /// and returns zero. A non-null return is the number of bytes consumed + /// before reaching ground, including the byte that reaches it. A null + /// return means the full slice was consumed without reaching ground. + /// + /// This is anywhere from 1% to 5% slower than nextSlice, depending + /// on the types of inputs provided (e.g. OSC vs APC, whether it + /// ever reaches ground, etc.). + pub inline fn nextSliceUntilGround( + self: *Self, + input: []const u8, + ) ?usize { + const consumed = self.nextSliceUntilGroundUntracked(input); + const consumed_len = consumed orelse input.len; + if (self.continuation != null and consumed_len > 0) { + self.trackContinuation(input[0..consumed_len]); + } + + return consumed; + } + + inline fn nextSliceUntilGroundUntracked( + self: *Self, + input: []const u8, + ) ?usize { + if (self.ground()) return 0; + + // Process UTF-8 if we're within that state. + var offset: usize = 0; + while (self.utf8decoder.state != 0) { + if (offset >= input.len) return null; + self.nextUtf8(input[offset]); + offset += 1; + } + + // Process non-UTF-8 + if (self.parser.state != .ground) { + offset += self.consumeUntilGround(input[offset..]); + } + + return if (self.ground()) offset else null; + } + + inline fn nextSliceUntracked(self: *Self, input: []const u8) void { + // Byte-at-a-time parsing in debug mode only. Even without + // SIMD support (build_options.simd == false, e.g. wasm), the + // batched path below is much faster than per-byte dispatch + // because it decodes UTF-8 in bulk (via scalar fallbacks) and + // hands printable runs to the handler as print_slice actions. + if (comptime debug) { + for (input) |c| self.nextUntracked(c); return; } @@ -572,15 +718,57 @@ pub fn Stream(comptime H: type) type { var i: usize = 0; while (i < cps.len) { const cp = cps[i]; - if (cp <= 0xF) { + // C0 and UTF-8-decoded C1 controls never enter + // printable runs. A codepoint is one of those + // exactly when it has no bit set outside 0x9F: + // bits 0-4 and bit 7 cover 0x00-0x1F and + // 0x80-0x9F and nothing else, so a single + // AND-test classifies both ranges. + if ((cp & ~@as(u32, 0x9F)) == 0) { @branchHint(.unlikely); - self.execute(@intCast(cp)); + if (cp <= 0x1F) { + // C0 controls execute rather than print. + self.execute(@intCast(cp)); + } else { + // C1 controls decoded from UTF-8 are ignored. + logUnsupportedOnce( + "ignoring UTF-8-decoded C1 controls, first: 0x{x}", + .{cp}, + 0x80, + ); + } i += 1; continue; } + // Find the end of the printable run. This is an + // early-exit search loop that LLVM won't + // auto-vectorize, and printable runs dominate real + // input, so scan several codepoints at a time + // manually (same idiom as the printSliceFill run + // scan). var end = i + 1; - while (end < cps.len and cps[end] > 0xF) end += 1; + scan: { + if (simd.lanes(u32)) |lanes| { + const V = @Vector(lanes, u32); + // Non-printable (C0 or decoded C1) is + // equivalent to "no bit outside 0x9F is + // set": one AND-test per lane. + const mask: V = @splat(~@as(u32, 0x9F)); + const zero: V = @splat(0); + while (end + lanes <= cps.len) { + const v: V = cps[end..][0..lanes].*; + const stop = (v & mask) == zero; + if (@reduce(.Or, stop)) { + const bits: std.meta.Int(.unsigned, lanes) = @bitCast(stop); + end += @ctz(bits); + break :scan; + } + end += lanes; + } + } + while (end < cps.len and (cps[end] & ~@as(u32, 0x9F)) != 0) end += 1; + } self.handler.vt(.print_slice, .{ .cps = cps[i..end] }); i = end; } @@ -665,8 +853,70 @@ pub fn Stream(comptime H: type) type { if (self.parser.state == .sos_pm_apc_string) { offset += self.consumeApcString(input[offset..]); if (offset >= input.len) return input.len; - // The next byte exits the string state; let - // nextNonUtf8 below handle it. + + // Fast-path normal string termination. This matches + // Parser.next's exit and entry actions while avoiding + // the generic action loop for every completed APC. + switch (input[offset]) { + std.ascii.control_code.esc => { + self.parser.clear(); + self.parser.state = .escape; + self.handler.vt(.apc_end, .{ .terminated = true }); + offset += 1; + continue; + }, + 0x9C => { + self.parser.state = .ground; + self.handler.vt(.apc_end, .{ .terminated = true }); + offset += 1; + continue; + }, + else => {}, + } + + // Aborting transitions need the scalar path so the + // handler can distinguish them from terminators. + } + + // Bulk-consume OSC string bytes into the OSC parser. + // OSC payloads (e.g. OSC 52 clipboard operations and + // the kitty clipboard protocol) can be megabytes of + // base64 data, so per-byte dispatch is far too slow. + // This can't be used for handlers with a vtRaw hook + // because the bytes never produce osc_put actions. + if (self.parser.state == .osc_string) { + offset += self.consumeOscString(input[offset..]); + if (offset >= input.len) return input.len; + + // Fast-path normal string termination. This matches + // Parser.next's exit and entry actions while avoiding + // the generic action loop for every completed OSC. + switch (input[offset]) { + // The sequence should be terminated by a full + // ST ("ESC \"); the "\" that should follow is + // dispatched as a normal escape sequence. + std.ascii.control_code.esc => { + if (self.parser.osc_parser.end( + std.ascii.control_code.esc, + )) |cmd| self.oscDispatch(cmd.*); + self.parser.clear(); + self.parser.state = .escape; + offset += 1; + continue; + }, + // BEL terminates the string directly. + std.ascii.control_code.bel => { + if (self.parser.osc_parser.end( + std.ascii.control_code.bel, + )) |cmd| self.oscDispatch(cmd.*); + self.parser.state = .ground; + offset += 1; + continue; + }, + // CAN/SUB abort and ignored C0 bytes go + // through the state machine below. + else => {}, + } } } @@ -822,10 +1072,52 @@ pub fn Stream(comptime H: type) type { return end; } + /// Bulk-consume OSC string bytes into the OSC parser. Returns + /// the number of bytes consumed. Stops at the first byte that + /// is not an osc_put byte in the parse table, leaving it for + /// the caller to process through the state machine. Every byte + /// >= 0x20 is an osc_put byte; bytes below either terminate or + /// abort the string (BEL, CAN, SUB, ESC) or are ignored. + /// + /// Must not be used by handlers with a vtRaw hook because the + /// consumed bytes never produce osc_put actions. + fn consumeOscString(self: *Self, input: []const u8) usize { + comptime assert(!@hasDecl(T, "vtRaw")); + assert(self.parser.state == .osc_string); + + var end: usize = 0; + if (comptime std.simd.suggestVectorLength(u8)) |vector_len| { + const ByteVector = @Vector(vector_len, u8); + while (end + vector_len <= input.len) { + const bytes: ByteVector = input[end..][0..vector_len].*; + const stop = bytes < @as(ByteVector, @splat(0x20)); + if (@reduce(.Or, stop)) break; + end += vector_len; + } + } + while (end < input.len) { + switch (input[end]) { + // Not osc_put bytes: BEL/CAN/SUB/ESC terminate or + // abort the state; other C0 bytes are ignored by it. + 0x00...0x1F => break, + // Everything else is an osc_put byte. + else => end += 1, + } + } + + if (end > 0) self.parser.osc_parser.nextSlice(input[0..end]); + return end; + } + /// Like nextSlice but takes one byte and is necessarily a scalar /// operation that can't use SIMD. Prefer nextSlice if you can and /// try to get multiple bytes at once. pub inline fn next(self: *Self, c: u8) void { + self.nextUntracked(c); + if (self.continuation != null) self.trackContinuation(&.{c}); + } + + inline fn nextUntracked(self: *Self, c: u8) void { // The scalar path can be responsible for decoding UTF-8. if (self.parser.state == .ground) { self.nextUtf8(c); @@ -871,20 +1163,36 @@ pub fn Stream(comptime H: type) type { // We need to increase the eval branch limit because a lot of // tests end up running almost completely at comptime due to // a chain of inline functions. - @setEvalBranchQuota(100_000); - - // C0 control - if (c <= 0xF) { + @setEvalBranchQuota(200_000); + + // C0 control or a C1 control decoded from UTF-8: exactly + // the codepoints with no bit set outside 0x9F (bits 0-4 + // and bit 7 cover 0x00-0x1F and 0x80-0x9F and nothing + // else), so the printable fast path stays a single + // AND-test. + if ((c & ~@as(u21, 0x9F)) == 0) { @branchHint(.unlikely); + + // ESC + if (c == 0x1B) { + self.parser.state = .escape; + self.parser.clear(); + return; + } + + // Ignore C1 that came via UTF-8 decoding, matching xterm. + if (c > 0x1F) { + logUnsupportedOnce( + "ignoring UTF-8-decoded C1 controls, first: 0x{x}", + .{c}, + 0x80, + ); + return; + } + self.execute(@intCast(c)); return; } - // ESC - if (c == 0x1B) { - self.parser.state = .escape; - self.parser.clear(); - return; - } self.print(@intCast(c)); } @@ -1003,7 +1311,9 @@ pub fn Stream(comptime H: type) type { .dcs_unhook => self.handler.vt(.dcs_unhook, {}), .apc_start => self.handler.vt(.apc_start, {}), .apc_put => |code| self.handler.vt(.apc_put, code), - .apc_end => self.handler.vt(.apc_end, {}), + .apc_end => self.handler.vt(.apc_end, .{ + .terminated = c == std.ascii.control_code.esc or c == 0x9C, + }), } } } @@ -1323,7 +1633,7 @@ pub fn Stream(comptime H: type) type { const mode_: ?csi.EraseDisplay = switch (input.params.len) { 0 => .below, - 1 => std.meta.intToEnum(csi.EraseDisplay, input.params[0]) catch null, + 1 => std.enums.fromInt(csi.EraseDisplay, input.params[0]), else => null, }; @@ -1652,7 +1962,7 @@ pub fn Stream(comptime H: type) type { 'g' => switch (input.intermediates.len) { 0 => { const mode: csi.TabClear = switch (input.params.len) { - 1 => std.meta.intToEnum(csi.TabClear, input.params[0]) catch { + 1 => std.enums.fromInt(csi.TabClear, input.params[0]) orelse { log.warn("invalid tab clear mode: {}", .{input.params[0]}); return; }, @@ -2309,6 +2619,7 @@ pub fn Stream(comptime H: type) type { self.handler.vt(.clipboard_contents, .{ .kind = clip.kind, .data = clip.data, + .terminator = clip.terminator, }); }, @@ -2364,6 +2675,14 @@ pub fn Stream(comptime H: type) type { self.handler.vt(.progress_report, v); }, + .kitty_clipboard_protocol => |v| { + self.handler.vt(.kitty_clipboard, v); + }, + + .kitty_dnd_protocol => |v| { + self.handler.vt(.kitty_dnd, v); + }, + .conemu_sleep, .conemu_show_message_box, .conemu_change_tab_title, @@ -2374,8 +2693,7 @@ pub fn Stream(comptime H: type) type { .conemu_output_environment_variable, .conemu_run_process, .kitty_text_sizing, - .kitty_clipboard_protocol, - .kitty_dnd_protocol, + .kitty_desktop_notification, .context_signal, => { log.debug("unimplemented OSC callback: {}", .{cmd}); @@ -2780,7 +3098,7 @@ test "stream: print" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.next('x'); try testing.expectEqual(@as(u21, 'x'), s.handler.c.?); } @@ -2802,7 +3120,7 @@ test "simd: print invalid utf-8" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice(&.{0xFF}); try testing.expectEqual(@as(u21, 0xFFFD), s.handler.c.?); } @@ -2824,7 +3142,7 @@ test "simd: complete incomplete utf-8" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice(&.{0xE0}); // 3 byte try testing.expect(s.handler.c == null); s.nextSlice(&.{0xA0}); // still incomplete @@ -2833,6 +3151,166 @@ test "simd: complete incomplete utf-8" { try testing.expectEqual(@as(u21, 0x800), s.handler.c.?); } +test "stream: ground state C0 controls are executed, not printed" { + const H = struct { + buf: [128]u21 = undefined, + len: usize = 0, + + pub fn vt( + self: *@This(), + comptime action: Action.Tag, + value: Action.Value(action), + ) void { + switch (action) { + .print => { + self.buf[self.len] = value.cp; + self.len += 1; + }, + .print_slice => for (value.cps) |cp| { + self.buf[self.len] = @intCast(cp); + self.len += 1; + }, + else => {}, + } + } + }; + + // Every C0 control except ESC must never produce a print action + // in the ground state. ESC is excluded because it begins an + // escape sequence rather than executing. + for (0..0x20) |c| { + if (c == 0x1B) continue; + + // Scalar path. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + s.next('A'); + s.next(@intCast(c)); + s.next('B'); + try testing.expectEqual(@as(usize, 2), s.handler.len); + try testing.expectEqual(@as(u21, 'A'), s.handler.buf[0]); + try testing.expectEqual(@as(u21, 'B'), s.handler.buf[1]); + } + + // Batched path. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + s.nextSlice(&.{ 'A', 'B', @intCast(c), 'C', 'D' }); + try testing.expectEqual(@as(usize, 4), s.handler.len); + for ("ABCD", 0..) |expected, i| { + try testing.expectEqual(@as(u21, expected), s.handler.buf[i]); + } + } + + // Batched path with a run long enough to exercise the + // vectorized printable-run scan on either side of the control. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + var input: [65]u8 = @splat('A'); + input[32] = @intCast(c); + s.nextSlice(&input); + try testing.expectEqual(@as(usize, 64), s.handler.len); + for (s.handler.buf[0..s.handler.len]) |cp| { + try testing.expectEqual(@as(u21, 'A'), cp); + } + } + } +} + +test "stream: ground state UTF-8-decoded C1 controls are ignored" { + const H = struct { + buf: [128]u21 = undefined, + len: usize = 0, + + pub fn vt( + self: *@This(), + comptime action: Action.Tag, + value: Action.Value(action), + ) void { + switch (action) { + .print => { + self.buf[self.len] = value.cp; + self.len += 1; + }, + .print_slice => for (value.cps) |cp| { + self.buf[self.len] = @intCast(cp); + self.len += 1; + }, + else => {}, + } + } + }; + + // Every C1 control that arrives as well-formed UTF-8 (a two byte + // 0xC2-lead sequence) must be dropped: not printed and not + // interpreted as a control (e.g. U+009B must not start a CSI). + for (0x80..0xA0) |c| { + const enc: [2]u8 = .{ 0xC2, @intCast(c) }; + + // Scalar path. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + s.next('A'); + s.next(enc[0]); + s.next(enc[1]); + s.next('B'); + try testing.expectEqual(@as(usize, 2), s.handler.len); + try testing.expectEqual(@as(u21, 'A'), s.handler.buf[0]); + try testing.expectEqual(@as(u21, 'B'), s.handler.buf[1]); + } + + // Batched path. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + s.nextSlice(&.{ 'A', 'B', enc[0], enc[1], 'C', 'D' }); + try testing.expectEqual(@as(usize, 4), s.handler.len); + for ("ABCD", 0..) |expected, i| { + try testing.expectEqual(@as(u21, expected), s.handler.buf[i]); + } + } + + // Batched path with a run long enough to exercise the + // vectorized printable-run scan on either side of the control. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + var input: [66]u8 = @splat('A'); + input[32] = enc[0]; + input[33] = enc[1]; + s.nextSlice(&input); + try testing.expectEqual(@as(usize, 64), s.handler.len); + for (s.handler.buf[0..s.handler.len]) |cp| { + try testing.expectEqual(@as(u21, 'A'), cp); + } + } + } + + // U+00A0 (NBSP), just past the C1 range, must still print. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + s.nextSlice(&.{ 0xC2, 0xA0 }); + try testing.expectEqual(@as(usize, 1), s.handler.len); + try testing.expectEqual(@as(u21, 0xA0), s.handler.buf[0]); + } + + // A codepoint whose continuation byte falls in the C1 range must + // still print: "Ü" is 0xC3 0x9C. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + s.nextSlice("Ü"); + try testing.expectEqual(@as(usize, 1), s.handler.len); + try testing.expectEqual(@as(u21, 0xDC), s.handler.buf[0]); + } + + // A raw C1 byte is ill-formed UTF-8, not a decoded C1: it must + // still produce a U+FFFD replacement. + { + var s: Stream(H) = .init(.{ .handler = .{} }); + s.nextSlice(&.{0x9B}); + try testing.expectEqual(@as(usize, 1), s.handler.len); + try testing.expectEqual(@as(u21, 0xFFFD), s.handler.buf[0]); + } +} + test "stream: cursor right (CUF)" { const H = struct { amount: u16 = 0, @@ -2849,7 +3327,7 @@ test "stream: cursor right (CUF)" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1B[C"); try testing.expectEqual(@as(u16, 1), s.handler.amount); @@ -2882,7 +3360,7 @@ test "stream: dec set mode (SM) and reset mode (RM)" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1B[?6h"); try testing.expectEqual(@as(modes.Mode, .origin), s.handler.mode); @@ -2911,7 +3389,7 @@ test "stream: ansi set mode (SM) and reset mode (RM)" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1B[4h"); try testing.expectEqual(@as(modes.Mode, .insert), s.handler.mode.?); @@ -2942,7 +3420,7 @@ test "stream: ansi set mode (SM) and reset mode (RM) with unknown value" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1B[6h"); try testing.expect(s.handler.mode == null); @@ -2968,7 +3446,7 @@ test "stream: restore mode" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); for ("\x1B[?42r") |c| s.next(c); try testing.expect(!s.handler.called); } @@ -2990,7 +3468,7 @@ test "stream: pop kitty keyboard with no params defaults to 1" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); for ("\x1B[2s"); try testing.expect(s.handler.escape == null); @@ -3318,18 +3796,203 @@ test "stream: change window title with invalid utf-8" { }; { - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b]2;abc\x1b\\"); try testing.expect(s.handler.seen); } { - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b]2;abc\xc0\x1b\\"); try testing.expect(!s.handler.seen); } } +test "stream: osc 52 large payload in chunks" { + const alloc = testing.allocator; + + const H = struct { + const Self = @This(); + data: std.ArrayListUnmanaged(u8) = .empty, + kind: u8 = 0, + terminator: ?osc.Terminator = null, + count: usize = 0, + + pub fn vt( + self: *Self, + comptime action: Action.Tag, + value: Action.Value(action), + ) void { + switch (action) { + .clipboard_contents => { + self.count += 1; + self.kind = value.kind; + self.terminator = value.terminator; + self.data.appendSlice( + testing.allocator, + value.data, + ) catch @panic("OOM"); + }, + else => {}, + } + } + }; + + // A payload that far exceeds the fixed buffer so the allocating + // capture is used, fed in chunk sizes that don't align with the + // sequence so the bulk path sees every kind of boundary. + const prefix = "\x1b]52;c;"; + const payload_len = 150_000; + var input: std.ArrayListUnmanaged(u8) = .empty; + defer input.deinit(alloc); + try input.appendSlice(alloc, prefix); + for (0..payload_len) |i| try input.append( + alloc, + 'A' + @as(u8, @intCast(i % 26)), + ); + try input.appendSlice(alloc, "\x1b\\"); + + var s: Stream(H) = .init(.{ .handler = .{}, .allocator = alloc }); + defer s.parser.deinit(); + defer s.handler.data.deinit(alloc); + + var i: usize = 0; + while (i < input.items.len) { + const end = @min(i + 4093, input.items.len); + s.nextSlice(input.items[i..end]); + i = end; + } + + try testing.expectEqual(@as(usize, 1), s.handler.count); + try testing.expectEqual(@as(u8, 'c'), s.handler.kind); + try testing.expectEqual(osc.Terminator.st, s.handler.terminator.?); + try testing.expectEqualSlices( + u8, + input.items[prefix.len .. prefix.len + payload_len], + s.handler.data.items, + ); +} + +test "stream: osc bulk path matches per-byte path" { + const alloc = testing.allocator; + + // Records every dispatch relevant to OSC processing in a + // normalized text form so streams fed different ways can be + // compared byte-for-byte. + const H = struct { + const Self = @This(); + alloc: Allocator, + journal: std.ArrayListUnmanaged(u8) = .empty, + + fn record(self: *Self, comptime fmt: []const u8, args: anytype) void { + const s = std.fmt.allocPrint( + self.alloc, + fmt, + args, + ) catch @panic("OOM"); + defer self.alloc.free(s); + self.journal.appendSlice(self.alloc, s) catch @panic("OOM"); + } + + pub fn vt( + self: *Self, + comptime action: Action.Tag, + value: Action.Value(action), + ) void { + switch (action) { + .clipboard_contents => self.record( + "clip kind={c} term={s} data={s}\n", + .{ value.kind, @tagName(value.terminator), value.data }, + ), + .kitty_clipboard => self.record( + "kitty meta={s} payload={?s}\n", + .{ value.metadata, value.payload }, + ), + .window_title => self.record( + "title {s}\n", + .{value.title}, + ), + // Normalize prints to per-codepoint so the per-byte + // and slice paths journal identically. + .print => self.record("print {u}\n", .{value.cp}), + .print_slice => for (value.cps) |cp| self.record( + "print {u}\n", + .{@as(u21, @intCast(cp))}, + ), + else => {}, + } + } + }; + + const cases = [_][]const u8{ + "\x1b]52;c;aGVsbG8=\x1b\\", + "\x1b]52;c;aGVsbG8=\x07", + "\x1b]52;;aGVsbG8=\x07", + // Ignored C0 byte embedded in the payload. + "\x1b]52;c;aGVs\x01bG8=\x07", + // CAN and SUB aborts. + "\x1b]52;c;aGVsbG8=\x18", + "\x1b]52;c;aGVsbG8=\x1a", + // C1 byte embedded in the payload is data. + "\x1b]0;ab\x9ccd\x07", + // Terminated with trailing printable text. + "\x1b]0;a title\x07x", + // Back-to-back sequences. + "\x1b]2;another title\x1b\\\x1b]0;t2\x07", + "\x1b]5522;type=write;aGVsbG8=\x1b\\", + // Invalid OSC number. + "\x1b]999;junk\x07", + // Exceeds the fixed buffer: allocating capture. + "\x1b]52;c;" ++ "y" ** 3000 ++ "\x1b\\", + // Exceeds the fixed buffer: overflow, no dispatch. + "\x1b]0;" ++ "x" ** 3000 ++ "\x07", + }; + + for (cases) |case| { + // Reference: byte-at-a-time. + var ref: Stream(H) = .init(.{ + .handler = .{ .alloc = alloc }, + .allocator = alloc, + }); + defer ref.parser.deinit(); + defer ref.handler.journal.deinit(alloc); + for (case) |c| ref.next(c); + + // The whole slice at once. + { + var s: Stream(H) = .init(.{ + .handler = .{ .alloc = alloc }, + .allocator = alloc, + }); + defer s.parser.deinit(); + defer s.handler.journal.deinit(alloc); + s.nextSlice(case); + try testing.expectEqualSlices( + u8, + ref.handler.journal.items, + s.handler.journal.items, + ); + } + + // Split into two slices at every possible boundary. + for (0..case.len + 1) |split| { + var s: Stream(H) = .init(.{ + .handler = .{ .alloc = alloc }, + .allocator = alloc, + }); + defer s.parser.deinit(); + defer s.handler.journal.deinit(alloc); + s.nextSlice(case[0..split]); + s.nextSlice(case[split..]); + try testing.expectEqualSlices( + u8, + ref.handler.journal.items, + s.handler.journal.items, + ); + } + } +} + test "stream: insert characters" { const H = struct { const Self = @This(); @@ -3348,7 +4011,7 @@ test "stream: insert characters" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); for ("\x1B[42@") |c| s.next(c); try testing.expect(s.handler.called); @@ -3374,7 +4037,7 @@ test "stream: insert characters explicit zero clamps to 1" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); for ("\x1B[0@") |c| s.next(c); try testing.expectEqual(@as(usize, 1), s.handler.value.?); } @@ -3398,7 +4061,7 @@ test "stream: SCOSC" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); for ("\x1B[s") |c| s.next(c); try testing.expect(s.handler.called); } @@ -3421,7 +4084,7 @@ test "stream: SCORC" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); for ("\x1B[u") |c| s.next(c); try testing.expect(s.handler.called); } @@ -3442,7 +4105,7 @@ test "stream: too many csi params" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1B[1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1C"); } @@ -3459,7 +4122,7 @@ test "stream: csi param too long" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1B[1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111C"); } @@ -3479,7 +4142,7 @@ test "stream: send report with CSI t" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[14t"); try testing.expectEqual(csi.SizeReportStyle.csi_14_t, s.handler.style); @@ -3513,7 +4176,7 @@ test "stream: invalid CSI t" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[19t"); try testing.expectEqual(null, s.handler.style); @@ -3535,7 +4198,7 @@ test "stream: CSI t push title" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[22;0t"); try testing.expectEqual(@as(u16, 0), s.handler.index.?); @@ -3557,7 +4220,7 @@ test "stream: CSI t push title with explicit window" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[22;2t"); try testing.expectEqual(@as(u16, 0), s.handler.index.?); @@ -3579,7 +4242,7 @@ test "stream: CSI t push title with explicit icon" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[22;1t"); try testing.expectEqual(null, s.handler.index); @@ -3601,7 +4264,7 @@ test "stream: CSI t push title with index" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[22;0;5t"); try testing.expectEqual(@as(u16, 5), s.handler.index.?); @@ -3623,7 +4286,7 @@ test "stream: CSI t pop title" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[23;0t"); try testing.expectEqual(@as(u16, 0), s.handler.index.?); @@ -3645,7 +4308,7 @@ test "stream: CSI t pop title with explicit window" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[23;2t"); try testing.expectEqual(@as(u16, 0), s.handler.index.?); @@ -3667,7 +4330,7 @@ test "stream: CSI t pop title with explicit icon" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[23;1t"); try testing.expectEqual(null, s.handler.index); @@ -3689,7 +4352,7 @@ test "stream: CSI t pop title with index" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[23;0;5t"); try testing.expectEqual(@as(u16, 5), s.handler.index.?); @@ -3709,7 +4372,7 @@ test "stream CSI W clear tab stops" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[2W"); try testing.expectEqual(Action.Key.tab_clear_current, s.handler.action.?); @@ -3732,7 +4395,7 @@ test "stream CSI W tab set" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[W"); try testing.expectEqual(Action.Key.tab_set, s.handler.action.?); @@ -3764,7 +4427,7 @@ test "stream CSI ? W reset tab stops" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); s.nextSlice("\x1b[?2W"); try testing.expect(s.handler.action == null); @@ -3798,7 +4461,7 @@ test "stream: SGR with 17+ parameters for underline color" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); // Kakoune-style SGR with underline color as 17th parameter // This tests the fix where param 17 was being dropped @@ -3826,7 +4489,7 @@ test "stream: tab clear with overflowing param" { } }; - var s: Stream(H) = .init(.{}); + var s: Stream(H) = .init(.{ .handler = .{} }); // This is the exact input from the fuzz crash (minus the mode byte): // CSI with a huge numeric param that saturates to 65535, followed by 'g'. s.nextSlice("\x1b[388888888888888888888888888888888888g\x1b[0m"); @@ -3866,7 +4529,7 @@ const ApcTestHandler = struct { }; test "stream: apc bulk slice" { - var s: Stream(ApcTestHandler) = .init(.{}); + var s: Stream(ApcTestHandler) = .init(.{ .handler = .{} }); s.nextSlice("\x1b_Gf=24,s=10,v=20;aGVsbG8=\x1b\\"); try testing.expectEqual(@as(usize, 1), s.handler.started); @@ -3883,8 +4546,20 @@ test "stream: apc bulk slice" { } } +test "stream: apc bulk slice C1 ST" { + var s: Stream(ApcTestHandler) = .init(.{ .handler = .{} }); + s.nextSlice("\x1b_Gpayload\x9c"); + + try testing.expectEqual(@as(usize, 1), s.handler.started); + try testing.expectEqual(@as(usize, 1), s.handler.ended); + try testing.expectEqualStrings( + "Gpayload", + s.handler.buf[0..s.handler.len], + ); +} + test "stream: apc bulk slice split across inputs" { - var s: Stream(ApcTestHandler) = .init(.{}); + var s: Stream(ApcTestHandler) = .init(.{ .handler = .{} }); s.nextSlice("\x1b_Gf=24,s=10"); s.nextSlice(",v=20;aGVs"); s.nextSlice("bG8=\x1b\\"); @@ -3898,7 +4573,7 @@ test "stream: apc bulk slice split across inputs" { } test "stream: apc bulk slice keeps C0 bytes as data" { - var s: Stream(ApcTestHandler) = .init(.{}); + var s: Stream(ApcTestHandler) = .init(.{ .handler = .{} }); // BEL does not terminate an APC string; it is payload data. s.nextSlice("\x1b_Gx\x07y\x1b\\"); @@ -3907,7 +4582,7 @@ test "stream: apc bulk slice keeps C0 bytes as data" { } test "stream: apc aborted by CAN" { - var s: Stream(ApcTestHandler) = .init(.{}); + var s: Stream(ApcTestHandler) = .init(.{ .handler = .{} }); // CAN (0x18) aborts the APC string via the anywhere => ground // transition. Exiting the sos_pm_apc_string state emits apc_end, // and the trailing bytes are printed, not treated as APC data. @@ -3922,7 +4597,7 @@ test "stream: apc aborted by CAN" { } test "stream: apc scalar path matches" { - var s: Stream(ApcTestHandler) = .init(.{}); + var s: Stream(ApcTestHandler) = .init(.{ .handler = .{} }); for ("\x1b_Gf=24;aGVsbG8=\x1b\\") |c| s.next(c); try testing.expectEqual(@as(usize, 1), s.handler.started); @@ -3945,9 +4620,9 @@ test "stream: apc vector boundaries match scalar path" { input[4 + position] = '\\'; const bytes = input[0 .. 5 + position]; - var bulk: Stream(ApcTestHandler) = .init(.{}); + var bulk: Stream(ApcTestHandler) = .init(.{ .handler = .{} }); bulk.nextSlice(bytes); - var scalar: Stream(ApcTestHandler) = .init(.{}); + var scalar: Stream(ApcTestHandler) = .init(.{ .handler = .{} }); for (bytes) |byte| scalar.next(byte); try testing.expectEqual(scalar.handler.started, bulk.handler.started); @@ -3958,3 +4633,720 @@ test "stream: apc vector boundaries match scalar path" { ); }; } + +const ContinuationTestHandler = struct { + committed: usize = 0, + apc_active: bool = false, + apc_buf: [256]u8 = undefined, + apc_len: usize = 0, + dcs_active: bool = false, + + pub fn deinit(_: *@This()) void {} + + pub fn vt( + self: *@This(), + comptime action: Action.Tag, + value: Action.Value(action), + ) void { + switch (action) { + .apc_start => self.apc_active = true, + .apc_put => { + self.apc_buf[self.apc_len] = value; + self.apc_len += 1; + }, + .apc_put_slice => { + @memcpy( + self.apc_buf[self.apc_len..][0..value.bytes.len], + value.bytes, + ); + self.apc_len += value.bytes.len; + }, + .dcs_hook => self.dcs_active = true, + .dcs_put => {}, + .apc_end => { + self.apc_active = false; + self.apc_len = 0; + self.committed += 1; + }, + .dcs_unhook => { + self.dcs_active = false; + self.committed += 1; + }, + .print => self.committed += 1, + .print_slice => self.committed += value.cps.len, + .print_repeat => self.committed += value, + else => self.committed += 1, + } + } +}; + +const ContinuationNullHandler = struct { + pub fn deinit(_: *@This()) void {} + + pub fn vt( + _: *@This(), + comptime _: Action.Tag, + _: anytype, + ) void {} +}; + +test "stream: nextSliceUntilGround stops at the earliest boundary" { + const S = Stream(ContinuationTestHandler); + var stream: S = .init(.{ .handler = .{} }); + defer stream.deinit(); + + stream.nextSlice("\x1b[31"); + try testing.expect(!stream.ground()); + stream.handler.committed = 0; + + const input = "mABC\x1b["; + const consumed = stream.nextSliceUntilGround(input).?; + try testing.expectEqual(@as(usize, 1), consumed); + try testing.expect(stream.ground()); + try testing.expectEqual(@as(usize, 1), stream.handler.committed); + + // The suffix was not inspected by the handler and can be processed after + // the caller performs work at the boundary. + stream.nextSlice(input[consumed..]); + try testing.expect(!stream.ground()); + try testing.expectEqual(Parser.State.csi_entry, stream.parser.state); + try testing.expectEqual(@as(usize, 4), stream.handler.committed); +} + +test "stream: nextSliceUntilGround consumes all input without a boundary" { + const S = Stream(ContinuationNullHandler); + var stream: S = .init(.{ .handler = .{} }); + defer stream.deinit(); + + try testing.expectEqual( + @as(?usize, 0), + stream.nextSliceUntilGround("unprocessed"), + ); + try testing.expect(stream.ground()); + + stream.nextSlice("\x1b["); + try testing.expectEqual( + @as(?usize, null), + stream.nextSliceUntilGround("123"), + ); + try testing.expect(!stream.ground()); + + // A boundary on the final byte is distinguishable from exhausting the + // input while still pending. + try testing.expectEqual( + @as(?usize, 1), + stream.nextSliceUntilGround("m"), + ); + try testing.expect(stream.ground()); +} + +test "stream: nextSliceUntilGround handles UTF-8 boundaries" { + const S = Stream(ContinuationTestHandler); + + // A completed codepoint is committed synchronously, and the printable + // suffix remains untouched. + var valid: S = .init(.{ .handler = .{} }); + defer valid.deinit(); + valid.nextSlice(&.{0xF0}); + const valid_input = [_]u8{ 0x9F, 0x98, 0x84, 'X' }; + try testing.expectEqual( + @as(?usize, 3), + valid.nextSliceUntilGround(&valid_input), + ); + try testing.expect(valid.ground()); + try testing.expectEqual(@as(usize, 1), valid.handler.committed); + + // A malformed continuation emits the replacement codepoint and retries + // the same byte. Ground is observed after that complete byte operation. + var malformed: S = .init(.{ .handler = .{} }); + defer malformed.deinit(); + malformed.nextSlice(&.{ 0xE0, 0xA0 }); + try testing.expectEqual( + @as(?usize, 1), + malformed.nextSliceUntilGround("A!"), + ); + try testing.expect(malformed.ground()); + try testing.expectEqual(@as(usize, 2), malformed.handler.committed); + + // If the retried byte is ESC, the stream has begun VT state at the end of + // that byte and must continue to the following ground boundary. + var retry_escape: S = .init(.{ .handler = .{} }); + defer retry_escape.deinit(); + retry_escape.nextSlice(&.{ 0xE0, 0xA0 }); + try testing.expectEqual( + @as(?usize, 3), + retry_escape.nextSliceUntilGround("\x1b[mX"), + ); + try testing.expect(retry_escape.ground()); +} + +test "stream: nextSliceUntilGround handles aborts and bulk strings" { + const S = Stream(ContinuationTestHandler); + + var aborted: S = .init(.{ .handler = .{} }); + defer aborted.deinit(); + aborted.nextSlice("\x1b[123"); + try testing.expectEqual( + @as(?usize, 1), + aborted.nextSliceUntilGround(&.{ 0x18, 'X' }), + ); + try testing.expect(aborted.ground()); + + var apc: S = .init(.{ .handler = .{} }); + defer apc.deinit(); + apc.nextSlice("\x1b_Gseed"); + var input: [131]u8 = undefined; + @memset(input[0..128], 'a'); + input[128..130].* = "\x1b\\".*; + input[130] = 'X'; + try testing.expectEqual( + @as(?usize, 130), + apc.nextSliceUntilGround(&input), + ); + try testing.expect(apc.ground()); +} + +test "stream: nextSliceUntilGround tracks only the consumed prefix" { + const S = Stream(ContinuationNullHandler); + var stream: S = .init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 64, + }); + defer stream.deinit(); + + stream.nextSlice("\x1b[31"); + try testing.expectEqual( + @as(?usize, 1), + stream.nextSliceUntilGround("mX\x1b["), + ); + + var buf: [64]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try stream.writeContinuation(&writer); + try testing.expectEqual(@as(usize, 0), writer.buffered().len); + + stream.nextSlice("\x1b["); + try testing.expectEqual( + @as(?usize, null), + stream.nextSliceUntilGround("123"), + ); + var pending_writer: std.Io.Writer = .fixed(&buf); + try stream.writeContinuation(&pending_writer); + try testing.expectEqualStrings("\x1b[123", pending_writer.buffered()); +} + +test "stream: continuation lifecycle" { + const S = Stream(ContinuationTestHandler); + + var disabled: S = .init(.{ .handler = .{} }); + defer disabled.deinit(); + var disabled_buf: [1]u8 = undefined; + var disabled_writer: std.Io.Writer = .fixed(&disabled_buf); + try testing.expectError( + error.ContinuationDisabled, + disabled.writeContinuation(&disabled_writer), + ); + + var zero_capacity: S = .init(.{ + .handler = .{}, + .continuation_max_bytes = 0, + }); + defer zero_capacity.deinit(); + var zero_capacity_buf: [1]u8 = undefined; + var zero_capacity_writer: std.Io.Writer = .fixed(&zero_capacity_buf); + try testing.expectError( + error.ContinuationDisabled, + zero_capacity.writeContinuation(&zero_capacity_writer), + ); + var no_allocator: S = .init(.{ + .handler = .{}, + .continuation_max_bytes = 64, + }); + defer no_allocator.deinit(); + var no_allocator_buf: [1]u8 = undefined; + var no_allocator_writer: std.Io.Writer = .fixed(&no_allocator_buf); + try testing.expectError( + error.ContinuationDisabled, + no_allocator.writeContinuation(&no_allocator_writer), + ); + + var tracked = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 64, + }); + defer tracked.deinit(); + + tracked.nextSlice("complete input"); + var empty_buf: [1]u8 = undefined; + var empty_writer: std.Io.Writer = .fixed(&empty_buf); + try tracked.writeContinuation(&empty_writer); + try testing.expectEqual(@as(usize, 0), empty_writer.end); + + tracked.nextSlice("\x1b["); + var short_buf: [1]u8 = undefined; + var short_writer: std.Io.Writer = .fixed(&short_buf); + try testing.expectError( + error.WriteFailed, + tracked.writeContinuation(&short_writer), + ); + + var failing = testing.FailingAllocator.init(testing.allocator, .{ + .fail_index = 0, + }); + var failing_stream = Stream(ContinuationNullHandler).init(.{ + .allocator = failing.allocator(), + .handler = .{}, + .continuation_max_bytes = 64, + }); + defer failing_stream.deinit(); + failing_stream.nextSlice("\x1b["); + var unavailable_buf: [1]u8 = undefined; + var unavailable_writer: std.Io.Writer = .fixed(&unavailable_buf); + try testing.expectError( + error.ContinuationUnavailable, + failing_stream.writeContinuation(&unavailable_writer), + ); +} + +test "stream: continuation suffixes are replay safe" { + const Case = struct { + input: []const u8, + expected: []const u8, + }; + const cases = [_]Case{ + .{ .input = "text\x1b", .expected = "\x1b" }, + .{ .input = "text\x1b[12;", .expected = "\x1b[12;" }, + .{ .input = "text\x1b[1\x07;2", .expected = "\x1b[1;2" }, + .{ .input = "text\x1b]2;hello", .expected = "\x1b]2;hello" }, + .{ .input = "text\x1b_Gabc", .expected = "\x1b_Gabc" }, + .{ .input = "text\x1bP+qabc", .expected = "\x1bP+qabc" }, + .{ .input = "text\xE0\xA0\xF0", .expected = "\xF0" }, + .{ .input = "text\x1b[12\x1b", .expected = "\x1b" }, + .{ + .input = "text\x1b[12\x9D2;title", + .expected = "\x1b[12\x9D2;title", + }, + }; + + const S = Stream(ContinuationTestHandler); + for (cases) |case| { + var stream = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer stream.deinit(); + stream.nextSlice(case.input); + + var buf: [1024]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try stream.writeContinuation(&writer); + try testing.expectEqualStrings(case.expected, writer.buffered()); + } +} + +test "stream: continuation reconstructs every unfinished VT state" { + const Case = struct { + input: []const u8, + state: Parser.State, + }; + const cases = [_]Case{ + .{ .input = "\x1b", .state = .escape }, + .{ .input = "\x1b(", .state = .escape_intermediate }, + .{ .input = "\x1b[", .state = .csi_entry }, + .{ .input = "\x1b[1", .state = .csi_param }, + .{ .input = "\x1b[1$", .state = .csi_intermediate }, + .{ .input = "\x1b[:", .state = .csi_ignore }, + .{ .input = "\x1bP", .state = .dcs_entry }, + .{ .input = "\x1bP1", .state = .dcs_param }, + .{ .input = "\x1bP1$", .state = .dcs_intermediate }, + .{ .input = "\x1bP1q", .state = .dcs_passthrough }, + .{ .input = "\x1bP:", .state = .dcs_ignore }, + .{ .input = "\x1b]2;title", .state = .osc_string }, + .{ .input = "\x1b_Gpayload", .state = .sos_pm_apc_string }, + }; + const S = Stream(ContinuationTestHandler); + + for (cases) |case| { + var source = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer source.deinit(); + source.nextSlice(case.input); + try testing.expectEqual(case.state, source.parser.state); + + var buf: [1024]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try source.writeContinuation(&writer); + + var restored = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer restored.deinit(); + restored.nextSlice(writer.buffered()); + try testing.expectEqual(@as(usize, 0), restored.handler.committed); + try testing.expectEqual(source.parser.state, restored.parser.state); + try testing.expectEqual(source.utf8decoder.state, restored.utf8decoder.state); + } + + // Parser ground is still unfinished while the UTF-8 decoder is waiting + // for the remaining bytes of a codepoint. + var utf8 = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 4, + }); + defer utf8.deinit(); + utf8.next(0xF0); + try testing.expectEqual(Parser.State.ground, utf8.parser.state); + try testing.expect(utf8.utf8decoder.state != 0); + var utf8_buf: [4]u8 = undefined; + var utf8_writer: std.Io.Writer = .fixed(&utf8_buf); + try utf8.writeContinuation(&utf8_writer); + try testing.expectEqualSlices(u8, &.{0xF0}, utf8_writer.buffered()); +} + +test "stream: continuation is chunking-independent and idempotent" { + const S = Stream(ContinuationTestHandler); + const input = "committed\x1b[1\x07;2"; + + var bulk = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer bulk.deinit(); + bulk.nextSlice(input); + + var scalar = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer scalar.deinit(); + for (input) |c| scalar.next(c); + + var bulk_buf: [1024]u8 = undefined; + var bulk_writer: std.Io.Writer = .fixed(&bulk_buf); + try bulk.writeContinuation(&bulk_writer); + var scalar_buf: [1024]u8 = undefined; + var scalar_writer: std.Io.Writer = .fixed(&scalar_buf); + try scalar.writeContinuation(&scalar_writer); + try testing.expectEqualStrings( + bulk_writer.buffered(), + scalar_writer.buffered(), + ); + + var restored = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer restored.deinit(); + restored.nextSlice(bulk_writer.buffered()); + try testing.expectEqual(@as(usize, 0), restored.handler.committed); + + var restored_buf: [1024]u8 = undefined; + var restored_writer: std.Io.Writer = .fixed(&restored_buf); + try restored.writeContinuation(&restored_writer); + try testing.expectEqualStrings( + bulk_writer.buffered(), + restored_writer.buffered(), + ); + + bulk.handler.committed = 0; + restored.handler.committed = 0; + bulk.nextSlice("mZ"); + restored.next('m'); + restored.next('Z'); + try testing.expectEqual(bulk.handler.committed, restored.handler.committed); +} + +test "stream: continuation rebuilds APC handler input" { + const S = Stream(ContinuationTestHandler); + var source = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer source.deinit(); + source.nextSlice("committed\x1b_Gabc"); + + var continuation: [1024]u8 = undefined; + var writer: std.Io.Writer = .fixed(&continuation); + try source.writeContinuation(&writer); + + var restored = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer restored.deinit(); + restored.nextSlice(writer.buffered()); + try testing.expectEqual(@as(usize, 0), restored.handler.committed); + try testing.expect(restored.handler.apc_active); + try testing.expectEqualStrings( + source.handler.apc_buf[0..source.handler.apc_len], + restored.handler.apc_buf[0..restored.handler.apc_len], + ); + + source.handler.committed = 0; + restored.handler.committed = 0; + source.nextSlice("\x1b\\"); + restored.nextSlice("\x1b\\"); + try testing.expectEqual(source.handler.committed, restored.handler.committed); + try testing.expect(!source.handler.apc_active); + try testing.expect(!restored.handler.apc_active); +} + +test "stream: continuation cap and recovery" { + const S = Stream(ContinuationTestHandler); + + // The raw feed exceeds the cap, but only the unfinished three-byte + // CSI suffix is retained. + var seeded = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 4, + }); + defer seeded.deinit(); + seeded.nextSlice("committed text\x1b[1"); + var seeded_buf: [4]u8 = undefined; + var seeded_writer: std.Io.Writer = .fixed(&seeded_buf); + try seeded.writeContinuation(&seeded_writer); + try testing.expectEqualStrings("\x1b[1", seeded_writer.buffered()); + + var exceeded = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 4, + }); + defer exceeded.deinit(); + exceeded.nextSlice("\x1b[123"); + var unavailable_buf: [1]u8 = undefined; + var unavailable_writer: std.Io.Writer = .fixed(&unavailable_buf); + try testing.expectError( + error.ContinuationUnavailable, + exceeded.writeContinuation(&unavailable_writer), + ); + + // Completing the CSI reaches ground and recovers without rebuilding the + // Stream. A later unfinished sequence is tracked normally. + exceeded.nextSlice("mtext\x1b["); + var recovered_buf: [4]u8 = undefined; + var recovered_writer: std.Io.Writer = .fixed(&recovered_buf); + try exceeded.writeContinuation(&recovered_writer); + try testing.expectEqualStrings("\x1b[", recovered_writer.buffered()); + + // A fresh ESC seed also recovers broken tracking even when the stream + // never reaches ground: the ESC abandons the previous unfinished + // state and everything after it is retained. + exceeded.nextSlice("\x1b[123"); + var reexceeded_buf: [1]u8 = undefined; + var reexceeded_writer: std.Io.Writer = .fixed(&reexceeded_buf); + try testing.expectError( + error.ContinuationUnavailable, + exceeded.writeContinuation(&reexceeded_writer), + ); + exceeded.nextSlice("\x1b]0;"); + var seed_buf: [4]u8 = undefined; + var seed_writer: std.Io.Writer = .fixed(&seed_buf); + try exceeded.writeContinuation(&seed_writer); + try testing.expectEqualStrings("\x1b]0;", seed_writer.buffered()); +} + +test "stream: continuation spans multiple bulk feeds" { + const S = Stream(ContinuationTestHandler); + + // An unfinished APC grows across feeds that contain no new seed. + var apc = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer apc.deinit(); + apc.nextSlice("text\x1b_Gab"); + apc.nextSlice("cd"); + apc.nextSlice("ef"); + var apc_buf: [16]u8 = undefined; + var apc_writer: std.Io.Writer = .fixed(&apc_buf); + try apc.writeContinuation(&apc_writer); + try testing.expectEqualStrings("\x1b_Gabcdef", apc_writer.buffered()); + + // An incomplete UTF-8 sequence grows across feeds of its + // continuation bytes. + var utf8 = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 1024, + }); + defer utf8.deinit(); + utf8.nextSlice("text\xF0"); + utf8.nextSlice("\x9F"); + var utf8_buf: [4]u8 = undefined; + var utf8_writer: std.Io.Writer = .fixed(&utf8_buf); + try utf8.writeContinuation(&utf8_writer); + try testing.expectEqualSlices(u8, "\xF0\x9F", utf8_writer.buffered()); + + // A later feed with its own seed drops everything retained earlier. + utf8.nextSlice("\x98\x84 done \x1b[38;5"); + var seed_buf: [8]u8 = undefined; + var seed_writer: std.Io.Writer = .fixed(&seed_buf); + try utf8.writeContinuation(&seed_writer); + try testing.expectEqualStrings("\x1b[38;5", seed_writer.buffered()); +} + +test "stream: continuation exact cap and large unfinished string" { + const S = Stream(ContinuationNullHandler); + + var exact = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 5, + }); + defer exact.deinit(); + exact.nextSlice("\x1b[123"); + var exact_buf: [5]u8 = undefined; + var exact_writer: std.Io.Writer = .fixed(&exact_buf); + try exact.writeContinuation(&exact_writer); + try testing.expectEqualStrings("\x1b[123", exact_writer.buffered()); + + const payload_len = 12 * 1024; + const input = try testing.allocator.alloc(u8, payload_len); + defer testing.allocator.free(input); + input[0..3].* = "\x1b_G".*; + @memset(input[3..], 'A'); + + var large = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = payload_len, + }); + defer large.deinit(); + large.nextSlice(input); + const large_buf = try testing.allocator.alloc(u8, payload_len); + defer testing.allocator.free(large_buf); + var large_writer: std.Io.Writer = .fixed(large_buf); + try large.writeContinuation(&large_writer); + try testing.expectEqualSlices(u8, input, large_writer.buffered()); +} + +test "stream: continuation allocation failure recovers" { + var failing = testing.FailingAllocator.init(testing.allocator, .{}); + const alloc = failing.allocator(); + const S = Stream(ContinuationNullHandler); + var stream = S.init(.{ + .allocator = alloc, + .handler = .{}, + .continuation_max_bytes = 16 * 1024, + }); + defer stream.deinit(); + + var input = try testing.allocator.alloc(u8, 12 * 1024); + defer testing.allocator.free(input); + input[0..2].* = "\x1b[".*; + @memset(input[2..], '1'); + + failing.fail_index = failing.alloc_index; + stream.nextSlice(input); + var unavailable_buf: [1]u8 = undefined; + var unavailable_writer: std.Io.Writer = .fixed(&unavailable_buf); + try testing.expectError( + error.ContinuationUnavailable, + stream.writeContinuation(&unavailable_writer), + ); + + failing.fail_index = std.math.maxInt(usize); + stream.next('m'); + var recovered_buf: [1]u8 = undefined; + var recovered_writer: std.Io.Writer = .fixed(&recovered_buf); + try stream.writeContinuation(&recovered_writer); + try testing.expectEqual(@as(usize, 0), recovered_writer.end); +} + +test "stream: continuation every-byte cuts preserve future behavior" { + const corpora = [_][]const u8{ + "plain \xF0\x9F\x98\x84 utf8", + "bad \xE0\xA0\xF0\x9F\x98\x84 utf8", + "\x1b[1\x07;2mstyled\x1b[0m", + "\x1b]2;window title\x1b\\text", + "\x1bP$qm\x1b\\text", + "\x1b_Ga=q;payload\x1b\\text", + "\x1b_25a1;s\x1b\\text", + "\x1b]2;first\x1b\\\x1b_Gsecond", + "\x1b[12\x9D2;title\x1b\\text", + "\x1b[12\x18text\x1b[1\x1Atext", + }; + const S = Stream(ContinuationTestHandler); + + for (corpora) |corpus| for (0..corpus.len + 1) |cut| { + var source = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 64 * 1024, + }); + defer source.deinit(); + source.nextSlice(corpus[0..cut]); + + var continuation: [64 * 1024]u8 = undefined; + var continuation_writer: std.Io.Writer = .fixed(&continuation); + try source.writeContinuation(&continuation_writer); + + var restored = S.init(.{ + .allocator = testing.allocator, + .handler = .{}, + .continuation_max_bytes = 64 * 1024, + }); + defer restored.deinit(); + restored.nextSlice(continuation_writer.buffered()); + try testing.expectEqual(@as(usize, 0), restored.handler.committed); + try testing.expectEqual(source.handler.apc_active, restored.handler.apc_active); + try testing.expectEqual(source.handler.dcs_active, restored.handler.dcs_active); + if (source.handler.apc_active) { + try testing.expectEqualStrings( + source.handler.apc_buf[0..source.handler.apc_len], + restored.handler.apc_buf[0..restored.handler.apc_len], + ); + } + + var reexport: [64 * 1024]u8 = undefined; + var reexport_writer: std.Io.Writer = .fixed(&reexport); + try restored.writeContinuation(&reexport_writer); + try testing.expectEqualStrings( + continuation_writer.buffered(), + reexport_writer.buffered(), + ); + + source.handler.committed = 0; + restored.handler.committed = 0; + source.nextSlice(corpus[cut..]); + var offset = cut; + var partition = cut +% corpus.len +% 1; + while (offset < corpus.len) { + partition = partition *% 1664525 +% 1013904223; + const len = @min(1 + partition % 7, corpus.len - offset); + restored.nextSlice(corpus[offset..][0..len]); + offset += len; + } + try testing.expectEqual(source.handler.committed, restored.handler.committed); + try testing.expectEqual(source.handler.apc_active, restored.handler.apc_active); + try testing.expectEqual(source.handler.dcs_active, restored.handler.dcs_active); + + var source_final: [64]u8 = undefined; + var source_final_writer: std.Io.Writer = .fixed(&source_final); + try source.writeContinuation(&source_final_writer); + var restored_final: [64]u8 = undefined; + var restored_final_writer: std.Io.Writer = .fixed(&restored_final); + try restored.writeContinuation(&restored_final_writer); + try testing.expectEqualStrings( + source_final_writer.buffered(), + restored_final_writer.buffered(), + ); + }; +} diff --git a/vendor/libghostty-vt/src/terminal/stream_continuation.zig b/vendor/libghostty-vt/src/terminal/stream_continuation.zig new file mode 100644 index 00000000..bd6b8f0f --- /dev/null +++ b/vendor/libghostty-vt/src/terminal/stream_continuation.zig @@ -0,0 +1,606 @@ +const std = @import("std"); +const assert = @import("../quirks.zig").inlineAssert; +const Allocator = std.mem.Allocator; +const Parser = @import("Parser.zig"); +const UTF8Decoder = @import("UTF8Decoder.zig"); + +/// Errors possible while validating a snapshot continuation. +pub const ValidateError = error{ + /// Nonempty input left both the VT parser and UTF-8 decoder at ground. + /// + /// A continuation exists only to reconstruct state that was unfinished at + /// the snapshot cut. Input that returns to ground is a complete PTY + /// fragment, not a continuation. Replaying it would repeat work already + /// represented by the Terminal snapshot. For example, `ESC [ 3 1 m` + /// completes an SGR command and must not be stored as a continuation. + NoPendingState, + + /// The input does not begin at its effective replay start. + /// + /// A later ESC supersedes earlier VT parser state, and a pending UTF-8 + /// codepoint begins at its lead byte. Any prefix before that effective + /// start is unnecessary and would disappear when the restored Stream + /// exports its continuation again. Rejecting the prefix preserves the + /// byte-identical re-export invariant and prevents unrelated prior input + /// from being replayed. + NonCanonicalContinuation, + + /// Replaying the input would perform handler-visible work. + /// + /// The Terminal snapshot already contains every mutation committed before + /// its capture cut. A continuation may rebuild unfinished parser or + /// builder state, but it must not mutate the Terminal or repeat an external + /// effect while doing so. For example, BEL inside an unfinished CSI would + /// ring again even though the CSI itself remains pending. + ReplayWouldCommit, +}; + +/// Validate continuation bytes. +/// +/// Ensure that: +/// +/// - replay ends with either VT or UTF-8 state unfinished +/// - byte zero is the effective start needed to reconstruct that state +/// - replay commits no Terminal mutation or external handler effect +pub fn validate(bytes: []const u8) ValidateError!void { + // Empty explicitly requests ground state and needs no replay. + if (bytes.len == 0) return; + + // We need the final parser and decoder states to classify the input, so a + // committed byte cannot return early. Remember it while scanning the rest. + var scanner: BoundaryScanner = .init(); + var committed_work = false; + for (bytes) |byte| { + if (scanner.next(byte) != .uncommitted) committed_work = true; + } + + // Nonempty continuation bytes must leave state that future input needs. + if (scanner.ground()) return error.NoPendingState; + + // The tracker exports the minimal suffix beginning at the effective replay + // start. A different prefix would not survive byte-identical re-export. + const replay_start = if (scanner.parser.state != .ground) + findVTReplayStart(bytes) + else + findUtf8ReplayStart(bytes); + if (replay_start != 0) return error.NonCanonicalContinuation; + + // At this point the input is unfinished and minimal, but replay must also + // be inert with respect to the already-restored Terminal and its effects. + if (committed_work) return error.ReplayWouldCommit; +} + +/// Retains the input needed to reconstruct unfinished Stream parser state. +/// +/// A feed is one chunk of bytes given to a Stream. It can end in the middle of +/// a VT sequence or a UTF-8 codepoint. To continue in another Stream, that +/// Stream starts from ground (with no sequence in progress) and reads the +/// saved bytes again. The first saved byte is called the replay start. +/// +/// Stream updates this tracker once per feed based on where that replay start +/// is: +/// +/// - Ground (parser and UTF-8 decoder): `reset`, no suffix is needed. +/// - Anything unfinished: `append`, which replaces the suffix with the +/// bytes from the replay start onward when that start is inside this +/// feed, and otherwise extends the suffix with this whole feed because +/// the unfinished state began in an earlier one. +/// +/// This keeps the retained bytes minimal: they always begin at the replay +/// start, so the feed path never needs to parse or trim old input. The suffix +/// may still contain a byte whose visible terminal effect already happened, +/// such as BEL inside an unfinished CSI sequence. `write` leaves out those +/// bytes so replay does not perform the same effect twice. +/// +/// If the suffix exceeds `max_bytes`, or retaining it fails, `broken` +/// is set until a later feed ends at ground (`reset`) or contains a new +/// replay start (`replace`), both of which need nothing that was lost. +/// +/// Replay semantics are guaranteed for the standard TerminalStream handler. +/// Custom handlers, including handlers that intercept vtRaw, are unsupported. +pub const Tracker = struct { + const initial_capacity = 4096; + + alloc: Allocator, + max_bytes: usize, + bytes: std.ArrayList(u8) = .empty, + broken: bool = false, + + /// Initialize a tracker. + pub fn init(alloc: Allocator, max_bytes: usize) Tracker { + var result: Tracker = .{ + .alloc = alloc, + .max_bytes = max_bytes, + }; + result.bytes.ensureTotalCapacity( + alloc, + @min(max_bytes, initial_capacity), + ) catch { + // We ignore memory errors here. They'll mark the tracker + // as broken in a future append. + + }; + return result; + } + + pub fn deinit(self: *Tracker) void { + self.bytes.deinit(self.alloc); + } + + /// Clear the current continuation suffix and broken state while keeping + /// its allocation. Stream calls this after reaching ground because no + /// earlier input is needed to reconstruct the next unfinished state. + pub fn reset(self: *Tracker) void { + self.broken = false; + self.bytes.clearRetainingCapacity(); + } + + /// Which state machine is unfinished at the end of a feed. When the VT + /// parser is outside ground the unfinished state is an escape sequence. + /// When it is grounded, only the UTF-8 decoder can be unfinished, with + /// an incomplete codepoint ending the feed. + pub const Pending = enum { vt, utf8 }; + + /// Retain the part of `input` needed to replay a feed that ended with + /// `pending` state unfinished. + /// + /// When the input contains the replay start for that state, it replaces + /// the retained suffix outright because nothing earlier is needed. The + /// new suffix is complete on its own, so this also repairs a broken + /// tracker. Otherwise the unfinished state began in an earlier feed and + /// this whole input extends the current suffix. + pub fn append(self: *Tracker, pending: Pending, input: []const u8) void { + const start = switch (pending) { + .vt => findVTReplayStart(input), + .utf8 => findUtf8ReplayStart(input), + } orelse { + self.extend(input); + return; + }; + self.replace(input[start..]); + } + + /// Replace the continuation suffix with one that has a new replay start. + fn replace(self: *Tracker, suffix: []const u8) void { + self.bytes.clearRetainingCapacity(); + if (suffix.len > self.max_bytes) { + self.markBroken(); + return; + } + self.bytes.appendSlice(self.alloc, suffix) catch { + self.markBroken(); + return; + }; + self.broken = false; + } + + /// Extend the continuation suffix with a fragment that contains no replay + /// start of its own. The stream stayed unfinished for the whole fragment, + /// so the existing suffix plus this fragment reproduces the current state. + fn extend(self: *Tracker, fragment: []const u8) void { + if (self.broken) return; + if (fragment.len > self.max_bytes -| self.bytes.items.len) { + self.markBroken(); + return; + } + self.bytes.appendSlice(self.alloc, fragment) catch self.markBroken(); + } + + /// Write the replay-safe continuation suffix to `writer`. The retained + /// bytes already begin at the replay start; this omits only bytes that + /// would repeat committed terminal effects without contributing to the + /// unfinished parser state (e.g. a BEL inside an unfinished CSI). The + /// tracker must not be broken. + pub fn write( + self: *const Tracker, + writer: *std.Io.Writer, + ) std.Io.Writer.Error!void { + var scanner: BoundaryScanner = .init(); + for (self.bytes.items) |c| { + if (scanner.next(c) == .omittable) continue; + try writer.writeByte(c); + } + } + + fn markBroken(self: *Tracker) void { + self.broken = true; + self.bytes.clearRetainingCapacity(); + } +}; + +/// Find where replay must begin when a feed ends inside a VT sequence. +/// +/// VT parsers treat ESC specially: it abandons the previous parser state and +/// starts a fresh escape state no matter what was being parsed. Because of +/// that rule, feeding the bytes from the last ESC onward into a grounded +/// Stream recreates the unfinished state at the end of `input`. +/// +/// The caller must only use this when the VT parser ended outside ground. +/// The returned value is the index of the last ESC in `input`. A null result +/// means the sequence began in an earlier feed, so this entire input must be +/// appended to the continuation bytes already retained. +fn findVTReplayStart(input: []const u8) ?usize { + const esc: u8 = 0x1B; + var rem: usize = input.len; + + // Escape sequences are short, so when the stream ends inside one the + // replay start is usually within the last few bytes. Scan backward in + // vector chunks; pure payload inputs (no ESC at all) still scan quickly. + if (comptime std.simd.suggestVectorLength(u8)) |lanes| { + const V = @Vector(lanes, u8); + const Bits = std.meta.Int(.unsigned, lanes); + const needle: V = @splat(esc); + + // Process several vectors per iteration so long inputs without ESC + // (e.g. huge string payloads) stay memory-bound rather than + // loop-bound. On a match, rescan the group precisely. + const unroll = 4; + while (rem >= lanes * unroll) { + const start = rem - lanes * unroll; + var match: @Vector(lanes, bool) = @splat(false); + inline for (0..unroll) |block| { + const v: V = input[start + block * lanes ..][0..lanes].*; + match = match | (v == needle); + } + if (@reduce(.Or, match)) { + inline for (0..unroll) |i| { + const block = unroll - 1 - i; + const v: V = input[start + block * lanes ..][0..lanes].*; + const bits: Bits = @bitCast(v == needle); + if (bits != 0) { + return start + block * lanes + (lanes - 1 - @clz(bits)); + } + } + unreachable; + } + rem = start; + } + + while (rem >= lanes) { + const start = rem - lanes; + const v: V = input[start..][0..lanes].*; + const bits: Bits = @bitCast(v == needle); + if (bits != 0) return start + (lanes - 1 - @clz(bits)); + rem = start; + } + } + + while (rem > 0) { + rem -= 1; + if (input[rem] == esc) return rem; + } + return null; +} + +/// Find where replay must begin when a feed ends inside a UTF-8 codepoint. +/// +/// A UTF-8 codepoint starts with a lead byte and may have up to three +/// continuation bytes. If the lead byte is in this input, replay must begin +/// there so the decoder sees the complete partial codepoint again. Since an +/// incomplete codepoint can contain at most three bytes, only the final three +/// input bytes need to be searched. +/// +/// The caller must only use this when the VT parser is at ground and the +/// UTF-8 decoder ended mid-codepoint. The returned value is the lead byte's +/// index. A null result means the lead byte was in an earlier feed, so this +/// entire input must be appended to the continuation bytes already retained. +fn findUtf8ReplayStart(input: []const u8) ?usize { + const max_pending = 3; + var idx = input.len; + while (idx > 0 and input.len - idx < max_pending) { + idx -= 1; + if (input[idx] >= 0xC0) return idx; + } + return null; +} + +/// Classifies bytes of a continuation suffix for export. +/// +/// The retained suffix reconstructs unfinished state exactly, but it can +/// contain bytes whose handler-visible work already committed the first +/// time (e.g. a C0 control executed inside an unfinished CSI sequence). +/// This implements a minimal VT stream processor (to avoid circular imports +/// with stream.zig) so `Tracker.write` can omit those bytes and replay +/// never repeats a terminal effect. It only runs at export time, never on +/// the feed path. +/// +/// This is allocation-free. +const BoundaryScanner = struct { + parser: Parser, + utf8decoder: UTF8Decoder, + + /// Initialize both state machines at runtime. Parser initialization may + /// inspect the runtime environment, so it cannot be a struct field default + /// that Zig requires to be comptime-known. + fn init() BoundaryScanner { + return .{ + .parser = .init(), + .utf8decoder = .{}, + }; + } + + /// How one byte affects committed work and the continuation suffix. + const Effect = enum { + /// The byte commits no handler-visible work. It must remain because it + /// may still build the unfinished VT, UTF-8, APC, or DCS state. + uncommitted, + + /// The byte commits handler-visible work but also changes a parser + /// state tag or reaches ground. It cannot be omitted in isolation; + /// boundary analysis decides whether the surrounding prefix is safe. + committed, + + /// The byte commits handler-visible work without changing either + /// state-machine tag or reaching ground. It can be omitted without + /// changing the unfinished state, avoiding a duplicate effect. + omittable, + }; + + /// True only when neither state machine needs prior bytes to continue. + fn ground(self: *const BoundaryScanner) bool { + return self.parser.state == .ground and self.utf8decoder.state == 0; + } + + /// Consume one byte using the same scalar UTF-8 retry and VT transitions + /// as Stream, then classify its effect on continuation construction. + fn next(self: *BoundaryScanner, c: u8) Effect { + // Preserve the state tags so we can recognize committed controls that + // do not contribute to the unfinished sequence. + const parser_state = self.parser.state; + const utf8_state = self.utf8decoder.state; + + // A byte is committed when replaying it would repeat handler-visible + // work already captured outside the continuation suffix. Actions that + // only build unfinished APC or DCS state are not committed because + // replay must reconstruct that state. + const committed = committed: { + if (self.parser.state == .ground) { + // Match Stream's scalar UTF-8 path, including retrying a byte that + // follows a malformed sequence. + var committed = false; + const res = self.utf8decoder.next(c); + if (res[0]) |cp| committed = self.codepoint(cp) or committed; + if (!res[1]) { + const retry = self.utf8decoder.next(c); + assert(retry[1]); + if (retry[0]) |cp| committed = self.codepoint(cp) or committed; + } + + break :committed committed; + } + + const actions = self.parser.next(c); + for (actions) |action_opt| { + const action = action_opt orelse continue; + switch (action) { + // These actions only build standard handler state. Their + // matching end/unhook actions are committed work. + .dcs_hook, + .dcs_put, + .apc_start, + .apc_put, + => {}, + else => break :committed true, + } + } + + break :committed false; + }; + + if (!committed) return .uncommitted; + + // A committed byte is independently omittable only while the stream + // remains unfinished and neither state-machine tag changes. + const state_changed = parser_state != self.parser.state or + utf8_state != self.utf8decoder.state; + return if (!state_changed and !self.ground()) + .omittable + else + .committed; + } + + /// Apply the Stream.handleCodepoint behavior relevant to replay. Returns + /// whether the accepted codepoint would commit handler-visible work. + fn codepoint(self: *BoundaryScanner, cp: u21) bool { + if (cp == 0x1B) { + self.parser.state = .escape; + self.parser.clear(); + return false; + } + + // Match Stream.handleCodepoint: all other accepted codepoints have + // already caused a handler-visible action (including supported C0s). + return true; + } +}; + +test "boundary scanner classifies effects and ground" { + const testing = std.testing; + var scanner: BoundaryScanner = .init(); + + try testing.expect(scanner.ground()); + try testing.expectEqual(BoundaryScanner.Effect.committed, scanner.next('A')); + try testing.expect(scanner.ground()); + + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, scanner.next(0x1B)); + try testing.expect(!scanner.ground()); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, scanner.next('[')); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, scanner.next('1')); + + // BEL commits an execute action without changing the unfinished CSI. + try testing.expectEqual(BoundaryScanner.Effect.omittable, scanner.next(0x07)); + try testing.expect(!scanner.ground()); + + try testing.expectEqual(BoundaryScanner.Effect.committed, scanner.next('m')); + try testing.expect(scanner.ground()); +} + +test "boundary scanner preserves unfinished builder actions" { + const testing = std.testing; + + var apc: BoundaryScanner = .init(); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, apc.next(0x1B)); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, apc.next('_')); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, apc.next('G')); + try testing.expectEqual(BoundaryScanner.Effect.committed, apc.next(0x1B)); + + var dcs: BoundaryScanner = .init(); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, dcs.next(0x1B)); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, dcs.next('P')); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, dcs.next('q')); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, dcs.next('x')); + try testing.expectEqual(BoundaryScanner.Effect.committed, dcs.next(0x1B)); +} + +test "boundary scanner handles UTF-8 and malformed retries" { + const testing = std.testing; + + var valid: BoundaryScanner = .init(); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, valid.next(0xF0)); + try testing.expect(!valid.ground()); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, valid.next(0x9F)); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, valid.next(0x98)); + try testing.expectEqual(BoundaryScanner.Effect.committed, valid.next(0x84)); + try testing.expect(valid.ground()); + + var malformed: BoundaryScanner = .init(); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, malformed.next(0xE0)); + try testing.expectEqual(BoundaryScanner.Effect.uncommitted, malformed.next(0xA0)); + try testing.expect(malformed.next(0xF0) != .uncommitted); + try testing.expect(!malformed.ground()); +} + +test "findVTReplayStart finds the latest ESC across vector boundaries" { + const testing = std.testing; + + try testing.expectEqual(@as(?usize, null), findVTReplayStart("")); + try testing.expectEqual(@as(?usize, null), findVTReplayStart("no escapes here")); + try testing.expectEqual(@as(?usize, 0), findVTReplayStart("\x1b")); + try testing.expectEqual(@as(?usize, 5), findVTReplayStart("\x1b[1m\x20\x1b[2")); + + // Exercise both the vector loop and the scalar remainder with replay + // starts at every position of a buffer larger than any vector width. + var buf: [193]u8 = undefined; + @memset(&buf, 'a'); + try testing.expectEqual(@as(?usize, null), findVTReplayStart(&buf)); + for (0..buf.len) |idx| { + @memset(&buf, 'a'); + buf[idx] = 0x1B; + try testing.expectEqual(@as(?usize, idx), findVTReplayStart(&buf)); + + // The latest ESC wins. + if (idx > 0) { + buf[idx - 1] = 0x1B; + try testing.expectEqual(@as(?usize, idx), findVTReplayStart(&buf)); + } + } +} + +test "findUtf8ReplayStart finds the pending lead byte" { + const testing = std.testing; + + try testing.expectEqual(@as(?usize, null), findUtf8ReplayStart("")); + try testing.expectEqual(@as(?usize, 4), findUtf8ReplayStart("text\xF0")); + try testing.expectEqual(@as(?usize, 4), findUtf8ReplayStart("text\xF0\x9F")); + try testing.expectEqual(@as(?usize, 4), findUtf8ReplayStart("text\xF0\x9F\x98")); + try testing.expectEqual(@as(?usize, 0), findUtf8ReplayStart("\xE0\xA0")); + + // A malformed prefix rejected earlier doesn't hide the pending lead. + try testing.expectEqual(@as(?usize, 2), findUtf8ReplayStart("\xE0\xA0\xF0")); + + // Continuation bytes of a sequence that started in an earlier input have + // no replay start of their own. + try testing.expectEqual(@as(?usize, null), findUtf8ReplayStart("\x9F")); + try testing.expectEqual(@as(?usize, null), findUtf8ReplayStart("\x9F\x98")); +} + +test "tracker retains and normalizes replay-safe bytes" { + const testing = std.testing; + var tracker = Tracker.init(testing.allocator, 64); + defer tracker.deinit(); + + tracker.append(.vt, "committed\x1b[1\x07"); + tracker.append(.vt, ";2"); + try testing.expect(!tracker.broken); + + var buf: [64]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try tracker.write(&writer); + try testing.expectEqualStrings("\x1b[1;2", writer.buffered()); + + // A feed with a later replay start drops the previous suffix entirely. + tracker.append(.vt, "committed\x1b]0;t"); + var replaced_buf: [64]u8 = undefined; + var replaced_writer: std.Io.Writer = .fixed(&replaced_buf); + try tracker.write(&replaced_writer); + try testing.expectEqualStrings("\x1b]0;t", replaced_writer.buffered()); + + // An incomplete UTF-8 codepoint seeds at its lead byte and grows with + // continuation bytes from later feeds. + tracker.append(.utf8, "committed\xF0"); + tracker.append(.utf8, "\x9F"); + var utf8_buf: [4]u8 = undefined; + var utf8_writer: std.Io.Writer = .fixed(&utf8_buf); + try tracker.write(&utf8_writer); + try testing.expectEqualSlices(u8, "\xF0\x9F", utf8_writer.buffered()); +} + +test "tracker cap, reset, and broken recovery" { + const testing = std.testing; + var tracker = Tracker.init(testing.allocator, 4); + defer tracker.deinit(); + + tracker.append(.vt, "\x1b[123"); + try testing.expect(tracker.broken); + + // Feeds without a replay start are dropped while broken. + tracker.append(.vt, "4"); + try testing.expect(tracker.broken); + try testing.expectEqual(@as(usize, 0), tracker.bytes.items.len); + + // Suffixes that grow past the cap break tracking. + tracker.reset(); + tracker.append(.vt, "\x1b[1"); + tracker.append(.vt, "23"); + try testing.expect(tracker.broken); + + // A new replay start that fits recovers without a reset. + tracker.append(.vt, "\x1b["); + try testing.expect(!tracker.broken); + var buf: [4]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try tracker.write(&writer); + try testing.expectEqualStrings("\x1b[", writer.buffered()); + + tracker.append(.vt, "\x1b[123"); + try testing.expect(tracker.broken); + tracker.reset(); + try testing.expect(!tracker.broken); + var empty_buf: [1]u8 = undefined; + var empty_writer: std.Io.Writer = .fixed(&empty_buf); + try tracker.write(&empty_writer); + try testing.expectEqual(@as(usize, 0), empty_writer.end); +} + +test "tracker reports writer failure and defers allocation failure" { + const testing = std.testing; + var tracker = Tracker.init(testing.allocator, 64); + defer tracker.deinit(); + tracker.append(.vt, "\x1b["); + + var buf: [1]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buf); + try testing.expectError(error.WriteFailed, tracker.write(&writer)); + + var failing = testing.FailingAllocator.init(testing.allocator, .{ + .fail_index = 0, + }); + var failing_tracker = Tracker.init(failing.allocator(), 64); + defer failing_tracker.deinit(); + try testing.expect(!failing_tracker.broken); + + // The best-effort initial reservation failed, so the first required + // retention retries allocation and marks the tracker broken. + failing_tracker.append(.vt, "\x1b["); + try testing.expect(failing_tracker.broken); +} diff --git a/vendor/libghostty-vt/src/terminal/stream_terminal.zig b/vendor/libghostty-vt/src/terminal/stream_terminal.zig index 91cfb21c..61172423 100644 --- a/vendor/libghostty-vt/src/terminal/stream_terminal.zig +++ b/vendor/libghostty-vt/src/terminal/stream_terminal.zig @@ -1,9 +1,11 @@ const std = @import("std"); +const Allocator = std.mem.Allocator; const build_options = @import("terminal_options"); const testing = std.testing; const apc = @import("apc.zig"); const clipboard = @import("clipboard.zig"); const csi = @import("csi.zig"); +const dcs = @import("dcs.zig"); const device_attributes = @import("device_attributes.zig"); const device_status = @import("device_status.zig"); const stream = @import("stream.zig"); @@ -13,8 +15,13 @@ const color = @import("color.zig"); const modes = @import("modes.zig"); const osc = @import("osc.zig"); const osc_color = @import("osc/parsers/color.zig"); +const kitty_clipboard = @import("kitty/clipboard.zig"); const kitty_color = @import("kitty/color.zig"); +const paste_pkg = @import("paste.zig"); +const kitty_dnd = @import("kitty/dnd.zig"); const size_report = @import("size_report.zig"); +const simd = @import("../simd/main.zig"); +const terminfo = @import("../terminfo/main.zig"); const Terminal = @import("Terminal.zig"); const log = std.log.scoped(.stream_terminal); @@ -34,33 +41,97 @@ pub const Handler = struct { /// The terminal state to modify. terminal: *Terminal, + /// True after an error prevented a terminal-owned semantic update. + /// + /// When an error happens during terminal processing, streams continue + /// forward and remain best-effort. A terminal can't really stop in + /// the middle it must go on. But this is flagged to true to let + /// consumers know some sort of unhandle-able error state happened + /// (e.g. an allocation failure). + /// + /// Only non-handled outcomes set this. Gracefully handled outcomes + /// that don't meaningfully negatively impact the terminal state + /// such as hitting Kitty image limits, failure to write a response, + /// do not flag this. + semantic_failure: bool = false, + /// Callbacks for certain effects that handlers may have. These /// may or may not fully replace internal handling of certain effects, /// but they allow for the handler to trigger or query external /// effects. effects: Effects = .readonly, + /// Whether CSI 21 t may report the terminal title. This is disabled by + /// default because reporting an attacker-controlled title to the pty can + /// inject text into the input stream of the foreground process. + title_report: bool = false, + /// The APC command handler maintains the APC state. APC is like /// CSI or OSC, but it is a private escape sequence that is used /// to send commands to the terminal emulator. This is used by /// the kitty graphics protocol. apc_handler: apc.Handler = .{}, - /// Default cursor style used by DECSCUSR reset (CSI 0 q). - default_cursor: bool = true, - default_cursor_style: Screen.CursorStyle = .block, - default_cursor_blink: bool = false, + /// The DCS command handler maintains state for DCS queries. + dcs_handler: dcs.Handler = .{}, + + /// The in-flight Kitty clipboard protocol (OSC 5522) write + /// transaction, if any. Null means no transaction is active. + /// Heap-allocated since transactions are rare and short-lived. + kitty_clipboard_write: ?*kitty_clipboard.WriteState = null, + + /// Kitty clipboard protocol (OSC 5522) session password grants, + /// recorded when a clipboard_read or clipboard_write reply asks to + /// remember the user's decision. + kitty_clipboard_grants: kitty_clipboard.Grants = .{}, + + /// Maximum total decoded bytes accumulated by one Kitty clipboard + /// protocol (OSC 5522) write transaction, captured when the + /// transaction begins. Data beyond the limit fails the transaction + /// with EFBIG. + kitty_clipboard_write_max_bytes: usize = kitty_clipboard.max_write_size, + + /// Called for sequence identifiers not supported by this library. + /// Currently, only APC is reported. Content is borrowed and only valid + /// for the duration of the callback. Set `apc_handler.unknown_max_bytes` + /// before starting the Stream to enable APC capture. + unknown_sequence: ?*const fn (*Handler, UnknownSequence) void = null, + + /// The name of the terminfo entry this terminal runs as, reported in + /// response to an XTGETTCAP query for "TN". + /// + /// The memory must remain valid for the lifetime of the handler. + /// Empty names and names longer than `max_terminfo_name_bytes` are + /// silently ignored. + terminfo_name: ?[]const u8 = null, + + /// Maximum byte length accepted for `terminfo_name`. + pub const max_terminfo_name_bytes = 128; pub const Effects = struct { /// Called when the terminal needs to write data back to the pty, /// e.g. in response to a DECRQM query. The data is only valid /// during the lifetime of the call so callers must copy it /// if it needs to be stored or used after the call returns. - write_pty: ?*const fn (*Handler, [:0]const u8) void, + write_pty: ?*const fn (*Handler, []const u8) void, /// Called when the bell is rung (BEL). bell: ?*const fn (*Handler) void, + /// Called when the running program requests a desktop notification + /// via OSC 9 or OSC 777. The title and body are borrowed and only + /// valid for the duration of the callback. + desktop_notification: ?*const fn (*Handler, Action.ShowDesktopNotification) void, + + /// Called when drag and drop protocol state changes in a way the + /// embedder may need to act on: the running program registering + /// or unregistering to accept drops, answering a drag, or + /// concluding a drop. The event says what changed; the details + /// are read from `handler.terminal.kitty_dnd` (Kitty's OSC 72 is + /// the only drag and drop protocol today). Native drag events + /// flow the other way, by calling `kitty.dnd.State` directly. + drag_and_drop: ?*const fn (*Handler, kitty_dnd.Event) void, + /// Called in response to a color scheme DSR query (CSI ? 996 n). /// Returns the current color scheme. Return null to silently /// ignore the query. @@ -76,9 +147,10 @@ pub const Handler = struct { /// valid for the lifetime of the call. enquiry: ?*const fn (*Handler) []const u8, - /// Called in response to XTWINOPS size queries (CSI 14/16/18 t). - /// Returns the current terminal geometry used for encoding. - /// Return null to silently ignore the query. + /// Called for XTWINOPS size queries (CSI 14/16/18 t) and when VT input + /// enables in-band size reports (mode 2048). Returns the current + /// terminal geometry used for encoding. Return null to suppress the + /// XTWINOPS response or mode 2048 report. size: ?*const fn (*Handler) ?size_report.Size, /// Called when the terminal title changes via escape sequences @@ -91,19 +163,37 @@ pub const Handler = struct { /// handler.terminal.getPwd(). pwd_changed: ?*const fn (*Handler) void, - /// Called when the running program writes to a clipboard. The write - /// has a normalized destination and one or more decoded MIME - /// representations. All request, MIME, and data memory is borrowed - /// and only valid for the duration of the callback. + /// Called when the running program reports progress via OSC 9;4. + progress_report: ?*const fn (*Handler, osc.Command.ProgressReport) void, + + /// Called when the running program writes to a clipboard. + clipboard_write: ?*const fn (*Handler, clipboard.Write) void, + + /// Called when the running program requests clipboard contents + /// (OSC 52 with a "?" payload, or a Kitty clipboard (OSC 5522) + /// read). Answering one lets the program read the user's + /// clipboard, so the embedder is expected to mediate consent. /// - /// A write with no contents clears the destination. A content entry - /// with empty data is a distinct empty representation. + /// Reads are synchronous: the callback must answer through + /// `read.reply` before it returns, so an embedder that needs to + /// ask the user must block (e.g. run a modal prompt) while the + /// stream waits. Returning without a reply, or replying with any + /// failure, answers the program with an empty clipboard (OSC 52) + /// or the matching protocol status (OSC 5522) so it doesn't hang. + /// If this is null, OSC 52 reads are ignored and OSC 5522 reads + /// are refused with EPERM. /// - /// Clipboard read requests (OSC 52 with a "?" payload) are never - /// forwarded: answering one would let any program running in the - /// terminal silently read the user's clipboard, and a VT state - /// library has no way to mediate that with user consent. - clipboard_write: ?*const fn (*Handler, clipboard.Write) clipboard.WriteResult, + /// OSC 5522 requests carry the program's MIME list, name, and + /// password grant state; a reply that sets `remember` records a + /// session grant so later requests with the same password arrive + /// with `granted` set. Kitty itself serves a request for only the + /// targets listing (`list` with no `mimes`) without prompting. + /// + /// Installing this also enables Kitty paste events (mode 5522): + /// `paste` sends the program an event instead of the text, and + /// the program's follow-up read arrives here with `granted` set + /// since the user already pasted. See `paste`. + clipboard_read: ?*const fn (*Handler, clipboard.Read) void, /// Called in response to an XTVERSION query. Returns the version /// string to report (e.g. "ghostty 1.2.3"). The returned memory @@ -116,10 +206,14 @@ pub const Handler = struct { /// effects beyond that. pub const readonly: Effects = .{ .bell = null, + .clipboard_read = null, .clipboard_write = null, .color_scheme = null, + .desktop_notification = null, .device_attributes = null, + .drag_and_drop = null, .enquiry = null, + .progress_report = null, .size = null, .title_changed = null, .pwd_changed = null, @@ -128,6 +222,18 @@ pub const Handler = struct { }; }; + /// A sequence unsupported by the active handler. Payload data is borrowed + /// only for the duration of the handler callback. + pub const UnknownSequence = union(enum) { + apc: String, + + /// Content between a string sequence's introducer and terminator. + pub const String = struct { + content: []const u8, + truncated: bool, + }; + }; + pub fn init(terminal: *Terminal) Handler { return .{ .terminal = terminal, @@ -135,7 +241,99 @@ pub const Handler = struct { } pub fn deinit(self: *Handler) void { + self.kittyClipboardAbort(); + self.kitty_clipboard_grants.deinit(self.terminal.gpa()); self.apc_handler.deinit(); + self.dcs_handler.deinit(); + } + + /// Resize the terminal and apply any side effects (if supported) + /// as a result of that. + /// + /// This is different than a direct `Terminal.resize` operation + /// because it also handles the side effects like mode 2048 in-band + /// size reports if write_pty is set. + pub fn resize(self: *Handler, value: Terminal.Resize) !void { + try self.terminal.resize(self.terminal.gpa(), value); + + // Mode 2048 reports require complete, current cell pixel geometry. + const cell_size = value.cell_size_px orelse return; + + // If we have no in-band size reports enabled then do nothing. + if (!self.terminal.modes.get(.in_band_size_reports)) return; + + // If we have no write_pty effect, do nothing. + const write_pty = self.effects.write_pty orelse return; + + // The maximum mode-2048 response is covered by size_report's maximum + // value test. Reserve the final byte for the callback's sentinel. + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(buf[0 .. buf.len - 1]); + size_report.encode(&writer, .mode_2048, .{ + .rows = value.rows, + .columns = value.cols, + .cell_width = cell_size.width, + .cell_height = cell_size.height, + }) catch unreachable; + buf[writer.end] = 0; + write_pty(self, buf[0..writer.end :0]); + } + + /// A paste request; see `paste`. + pub const Paste = paste_pkg.Request; + + pub const PasteError = Allocator.Error || std.Io.RandomSecureError || error{ + /// The data could inject commands and allow_unsafe was false. + /// Nothing was written. + UnsafePaste, + + /// The contents reader failed. Nothing was written. + ReadFailed, + + /// No write_pty effect is set, so nothing can be written. + NoWritePty, + }; + + /// The size of the chunks a paste streams to write_pty in. + pub const paste_chunk_size = 4096; + + /// Paste into the terminal, applying the terminal's current state + /// as necessary to owner mode 5522, bracketed paste, unsafe paste, etc. + /// Returns true if anything was written to the pty. + /// + /// The output streams to write_pty in chunks of `paste_chunk_size`. + /// The contents are read at most once and only the pasted text + /// representation is ever read, buffered whole while it is checked + /// and encoded; see `terminal.paste`. + pub fn paste(self: *Handler, req: Paste) PasteError!bool { + if (self.effects.write_pty == null) return error.NoWritePty; + + var buf: [paste_chunk_size]u8 = undefined; + var pty: PtyWriter = .init(self, &buf); + // Delivered on error too: a partial paste has its frame closed + // and the program must see that. + defer pty.writer.flush() catch unreachable; + + return paste_pkg.paste(.{ + .terminal = self.terminal, + .alloc = self.terminal.gpa(), + // Paste events need the program's follow-up Kitty + // clipboard read served. + .kitty_clipboard = if (self.effects.clipboard_read != null) .{ + .grants = &self.kitty_clipboard_grants, + .io = self.terminal.io(), + } else null, + .writer = &pty.writer, + }, req) catch |err| switch (err) { + // The pty writer never fails. + error.WriteFailed => unreachable, + error.ReadFailed, + error.OutOfMemory, + error.UnsafePaste, + error.EntropyUnavailable, + error.Canceled, + => |e| e, + }; } pub fn vt( @@ -144,10 +342,16 @@ pub const Handler = struct { value: Action.Value(action), ) void { self.vtFallible(action, value) catch |err| { + self.semantic_failure = true; log.warn("error handling VT action action={} err={}", .{ action, err }); }; } + fn unknownSequence(self: *Handler, value: UnknownSequence) void { + const func = self.unknown_sequence orelse return; + func(self, value); + } + inline fn vtFallible( self: *Handler, comptime action: Action.Tag, @@ -181,26 +385,7 @@ pub const Handler = struct { self.terminal.screens.active.cursor.y + 1 +| value.value, self.terminal.screens.active.cursor.x + 1, ), - .cursor_style => { - self.default_cursor = false; - - const blink = switch (value) { - .default => self.default_cursor_blink, - .steady_block, .steady_bar, .steady_underline => false, - .blinking_block, .blinking_bar, .blinking_underline => true, - }; - const style: Screen.CursorStyle = switch (value) { - .default => style: { - self.default_cursor = true; - break :style self.default_cursor_style; - }, - .blinking_block, .steady_block => .block, - .blinking_bar, .steady_bar => .bar, - .blinking_underline, .steady_underline => .underline, - }; - self.terminal.modes.set(.cursor_blinking, blink); - self.terminal.screens.active.cursor.cursor_style = style; - }, + .cursor_style => self.terminal.setCursorStyle(value), .erase_display_below => self.terminal.eraseDisplay(.below, value), .erase_display_above => self.terminal.eraseDisplay(.above, value), .erase_display_complete => self.terminal.eraseDisplay(.complete, value), @@ -245,7 +430,7 @@ pub const Handler = struct { .configure_charset => self.terminal.configureCharset(value.slot, value.charset), .set_attribute => switch (value) { .unknown => {}, - else => self.terminal.setAttribute(value) catch {}, + else => try self.terminal.setAttribute(value), }, .protected_mode_off => self.terminal.setProtectedMode(.off), .protected_mode_iso => self.terminal.setProtectedMode(.iso), @@ -267,25 +452,40 @@ pub const Handler = struct { .decaln => try self.terminal.decaln(), .full_reset => { self.terminal.fullReset(); - self.default_cursor = true; - self.terminal.modes.set(.cursor_blinking, self.default_cursor_blink); - self.terminal.screens.active.cursor.cursor_style = self.default_cursor_style; + + // Full reset clears grants + self.kitty_clipboard_grants.deinit(self.terminal.gpa()); + self.kitty_clipboard_grants = .{}; + + // Clear the progress bar + self.progressReport(.{ .state = .remove }); }, .start_hyperlink => try self.terminal.screens.active.startHyperlink(value.uri, value.id), .end_hyperlink => self.terminal.screens.active.endHyperlink(), .semantic_prompt => try self.terminal.semanticPrompt(value), .mouse_shape => self.terminal.mouse_shape = value, - .color_operation => try self.colorOperation(&value.requests, value.terminator), - .kitty_color_report => try self.kittyColorOperation(value), + .color_operation => self.colorOperation( + &value.requests, + value.terminator, + ) catch |err| log.warn("error reporting OSC color err={}", .{err}), + .kitty_color_report => self.kittyColorOperation(value) catch |err| { + log.warn("error reporting Kitty colors err={}", .{err}); + }, + .kitty_clipboard => self.kittyClipboard(value) catch |err| { + // Clipboard operations are external effects, not terminal + // state; a failed transaction was already answered. + log.warn("error handling kitty clipboard err={}", .{err}); + }, // APC .apc_start => self.apc_handler.start(), .apc_put => self.apc_handler.feed(self.terminal.gpa(), value), .apc_put_slice => self.apc_handler.feedSlice(self.terminal.gpa(), value.bytes), - .apc_end => self.apcEnd(), + .apc_end => self.apcEnd(value.terminated), // Effect-based handlers .bell => self.bell(), + .show_desktop_notification => self.desktopNotification(value), .device_attributes => self.reportDeviceAttributes(value), .device_status => self.deviceStatus(value.request), .enquiry => self.reportEnquiry(), @@ -293,73 +493,828 @@ pub const Handler = struct { .request_mode => self.requestMode(value.mode), .request_mode_unknown => self.requestModeUnknown(value.mode, value.ansi), .size_report => self.reportSize(value), - .window_title => self.windowTitle(value.title), - .report_pwd => self.reportPwd(value.url), + .window_title => try self.windowTitle(value.title), + .report_pwd => try self.reportPwd(value.url), + .progress_report => self.progressReport(value), .xtversion => self.reportXtversion(), - .clipboard_contents => try self.clipboardContents(value.kind, value.data), + .clipboard_contents => self.clipboardContents( + value.kind, + value.data, + value.terminator, + ) catch |err| { + // Clipboard operations are external effects, not terminal state. + log.warn("error handling clipboard operation err={}", .{err}); + }, + .kitty_dnd => self.kittyDnd(value) catch |err| { + // Drag and drop is a self-contained subsystem: an OOM + // updating its state or a failure writing a response + // degrades it without corrupting terminal state, so we log. + log.warn("error handling kitty dnd err={}", .{err}); + }, - // No supported DCS commands have any terminal-modifying effects, - // but they may in the future. For now we just ignore it. - .dcs_hook, - .dcs_put, - .dcs_unhook, - => {}, + .dcs_hook => try self.dcsHook(value), + .dcs_put => try self.dcsPut(value), + .dcs_unhook => try self.dcsUnhook(), // Have no terminal-modifying effect - .show_desktop_notification, - .progress_report, .title_push, .title_pop, => {}, } } - inline fn writePty(self: *Handler, data: [:0]const u8) void { + inline fn writePty(self: *Handler, data: []const u8) void { const func = self.effects.write_pty orelse return; func(self, data); } + fn dcsHook(self: *Handler, value: Action.Value(.dcs_hook)) !void { + var cmd = self.dcs_handler.hook( + self.terminal.gpa(), + value, + ) orelse return; + defer cmd.deinit(); + try self.dcsCommand(&cmd); + } + + fn dcsPut(self: *Handler, value: u8) !void { + var cmd = self.dcs_handler.put(value) orelse return; + defer cmd.deinit(); + try self.dcsCommand(&cmd); + } + + fn dcsUnhook(self: *Handler) !void { + var cmd = self.dcs_handler.unhook() orelse return; + defer cmd.deinit(); + try self.dcsCommand(&cmd); + } + + fn dcsCommand(self: *Handler, cmd: *dcs.Command) !void { + switch (cmd.*) { + .decrqss => |request| { + var response: [ + dcs.Command.DECRQSS.max_response_bytes + 1 + ]u8 = undefined; + const encoded = try request.encode( + self.terminal, + response[0 .. response.len - 1], + ); + response[encoded.len] = 0; + self.writePty(response[0..encoded.len :0]); + }, + + .xtgettcap => |*gettcap| { + if (self.effects.write_pty == null) return; + const map = comptime terminfo.ghostty.xtgettcapMap(); + while (gettcap.next()) |key| { + if (std.mem.eql(u8, key, encoded_tn_key)) { + self.writeTerminfoName(); + continue; + } + self.writePty(map.get(key) orelse continue); + } + }, + + .tmux => {}, + } + } + + // Hex-encoded "TN", the XTGETTCAP key naming the terminfo entry. + // The static map also carries this key with Ghostty's own name, so + // it is intercepted before the lookup: an embedder that never + // configured a name must not be reported as Ghostty's entry. + const encoded_tn_key = &std.fmt.bytesToHex("TN", .upper); + + /// Answer an XTGETTCAP "TN" query from the configured terminfo name. + /// Unset, empty, or over-long names leave the query unanswered. + fn writeTerminfoName(self: *Handler) void { + const name = self.terminfo_name orelse return; + if (name.len == 0 or name.len > max_terminfo_name_bytes) return; + + // Fixed upper bound for an encoded "TN" reply calculated + // at comptime from our max terminfo size. + const max_tn_response_bytes = + comptime "\x1bP1+r".len + encoded_tn_key.len + "=".len + + (max_terminfo_name_bytes * 2) + "\x1b\\".len + + 1; // null terminator + + // Values are hex-encoded uppercase, matching the static map. The + // buffer fits any name allowed above, so the print cannot fail. + var buf: [max_tn_response_bytes]u8 = undefined; + self.writePty(std.fmt.bufPrintZ( + &buf, + "\x1bP1+r" ++ encoded_tn_key ++ "={X}\x1b\\", + .{name}, + ) catch unreachable); + } + fn bell(self: *Handler) void { const func = self.effects.bell orelse return; func(self); } - fn clipboardContents(self: *Handler, kind: u8, data: []const u8) !void { - const func = self.effects.clipboard_write orelse return; + fn desktopNotification( + self: *Handler, + notification: Action.ShowDesktopNotification, + ) void { + const func = self.effects.desktop_notification orelse return; + func(self, notification); + } - // Read requests are deliberately not forwarded; see the effect docs. - if (data.len == 1 and data[0] == '?') return; + fn progressReport(self: *Handler, report: osc.Command.ProgressReport) void { + const func = self.effects.progress_report orelse return; + func(self, report); + } + fn clipboardContents( + self: *Handler, + kind: u8, + data: []const u8, + terminator: osc.Terminator, + ) !void { const location: clipboard.Location = switch (kind) { 's' => .selection, 'p' => .primary, else => .standard, }; + // OSC 52 uses a "?" payload to request the clipboard contents. + if (data.len == 1 and data[0] == '?') { + self.clipboardRead(location, terminator); + return; + } + + const func = self.effects.clipboard_write orelse return; + // OSC 52 uses an empty payload to clear the selected clipboard. if (data.len == 0) { - _ = func(self, .{ + func(self, .{ .location = location, .contents = &.{}, + .name = "", + .granted = false, + .can_remember = false, + .reply_ctx = self, + .reply_fn = &ignoreWriteReply, }); return; } - const decoder = std.base64.standard.Decoder; - const decoded_len = try decoder.calcSizeForSlice(data); + // Decode the base64 payload with the SIMD decoder (the same one + // used for Kitty clipboard payloads) rather than the scalar std + // implementation; clipboard payloads can be megabytes. The + // Kitty clipboard spec governs OSC 52 base64 handling too: a + // request with characters outside the base64 alphabet is + // discarded entirely (never partially decoded), while a + // missing-padding tail is tolerated since OSC 52 has no way + // to report errors to the client. const alloc = self.terminal.gpa(); - const decoded = try alloc.alloc(u8, decoded_len); - defer alloc.free(decoded); - try decoder.decode(decoded, data); + const buf = try alloc.alloc(u8, simd.base64.maxLen(data)); + defer alloc.free(buf); + const decoded = simd.base64.decodeStrict(data, buf, .optional) catch { + log.warn("OSC 52 clipboard write is not valid base64, ignoring", .{}); + return; + }; const contents = [_]clipboard.Content{.{ .mime = "text/plain", .data = decoded, }}; - _ = func(self, .{ + func(self, .{ .location = location, .contents = &contents, + .name = "", + .granted = false, + .can_remember = false, + .reply_ctx = self, + .reply_fn = &ignoreWriteReply, + }); + } + + /// Reply target for clipboard writes on protocols without a write + /// acknowledgement (OSC 52, OSC 1337 Copy): the reply is accepted + /// and discarded. + fn ignoreWriteReply(_: *anyopaque, _: clipboard.Write.Result) void {} + + fn clipboardRead( + self: *Handler, + location: clipboard.Location, + terminator: osc.Terminator, + ) void { + const func = self.effects.clipboard_read orelse return; + + var state: ClipboardReadState = .{ + .handler = self, + .location = location, + .terminator = terminator, + }; + func(self, .{ + .location = location, + .mimes = &.{"text/plain"}, + .list = false, + .name = "", + .granted = false, + .can_remember = false, + .reply_ctx = &state, + .reply_fn = &ClipboardReadState.reply, + }); + + // The program is waiting on us, so a callback that returned + // without a (successful) reply gets an empty clipboard rather + // than silence. + if (!state.replied) state.respond("") catch |err| { + log.warn("error replying to clipboard read err={}", .{err}); + }; + } + + /// Reply state for one synchronous clipboard read. This lives on the + /// clipboardRead stack frame, so it is only valid during the callback. + const ClipboardReadState = struct { + handler: *Handler, + location: clipboard.Location, + terminator: osc.Terminator, + replied: bool = false, + + fn reply(ctx: *anyopaque, result: clipboard.Read.Result) void { + const self: *ClipboardReadState = @ptrCast(@alignCast(ctx)); + if (self.replied) { + log.warn("clipboard read replied more than once, ignoring", .{}); + return; + } + + // OSC 52 carries a single text value. + const data: []const u8 = switch (result) { + .denied, .unsupported, .busy, .io_error => "", + .success => |s| for (s.contents) |c| { + if (clipboard.isTextMime(c.mime)) break c.data; + } else "", + }; + + self.respond(data) catch |err| { + // Leave replied unset so clipboardRead falls back to the + // empty reply. + log.warn("error replying to clipboard read err={}", .{err}); + return; + }; + self.replied = true; + } + + fn respond( + self: *ClipboardReadState, + data: []const u8, + ) error{ OutOfMemory, WriteFailed }!void { + const handler = self.handler; + var stack = std.heap.stackFallback(256, handler.terminal.gpa()); + const alloc = stack.get(); + + var aw: std.Io.Writer.Allocating = .init(alloc); + defer aw.deinit(); + const kind: u8 = switch (self.location) { + .selection => 's', + .primary => 'p', + .standard, _ => 'c', + }; + try aw.writer.print("\x1b]52;{c};", .{kind}); + try std.base64.standard.Encoder.encodeWriter(&aw.writer, data); + try aw.writer.writeAll(self.terminator.string()); + + const written = try aw.toOwnedSliceSentinel(0); + defer alloc.free(written); + handler.writePty(written); + } + }; + + /// Handle one Kitty clipboard protocol (OSC 5522) packet. + fn kittyClipboard( + self: *Handler, + v: Action.Value(.kitty_clipboard), + ) error{OutOfMemory}!void { + // Decode and validate the metadata. + var arena: std.heap.ArenaAllocator = .init(self.terminal.gpa()); + defer arena.deinit(); + const meta = (kitty_clipboard.Metadata.parse( + arena.allocator(), + v.metadata, + ) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.InvalidValue => { + const state = self.kitty_clipboard_write orelse return; + switch (kitty_clipboard.Metadata.operation(v.metadata) orelse return) { + .wdata, .walias => self.kittyClipboardFinish( + state, + .EINVAL, + v.terminator, + ), + .read, .write => {}, + } + return; + }, + }) orelse return; + + const payload = v.payload orelse ""; + switch (meta.op) { + .read => try self.kittyClipboardRead(&meta, payload, v.terminator), + .write => try self.kittyClipboardWriteBegin(&meta, v.terminator), + .wdata => try self.kittyClipboardData(&meta, payload, v.terminator), + .walias => try self.kittyClipboardAlias(&meta, payload, v.terminator), + } + } + + fn kittyClipboardRead( + self: *Handler, + meta: *const kitty_clipboard.Metadata, + payload: []const u8, + terminator: osc.Terminator, + ) error{OutOfMemory}!void { + // The payload is the requested MIME list. Kitty drops a read + // request with an undecodable payload without any response. + const alloc = self.terminal.gpa(); + const decoded = kitty_clipboard.Payload.init( + alloc, + payload, + ) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + error.Invalid => return, + }; + defer decoded.deinit(alloc); + if (!decoded.isValidUtf8()) return; + + // Without a clipboard_read effect nothing can serve the read. + // EPERM is the protocol's denial so clients degrade gracefully. + const func = self.effects.clipboard_read orelse { + self.kittyClipboardRespond(&.{ + .op = .read, + .status = .EPERM, + .id = meta.id, + .terminator = terminator, + }); + return; + }; + + // The targets type ('.') asks for the listing of available + // types rather than data. Requested types beyond the cap are + // dropped and simply never served, which is how the protocol + // reports an unavailable type anyway. + var mimes_buf: [kitty_clipboard.max_read_mimes][]const u8 = undefined; + const mimes, const list = mimes: { + var targets = false; + var len: usize = 0; + var it = decoded.mimeIterator(); + while (it.next()) |mime| { + if (std.mem.eql(u8, mime, kitty_clipboard.targets_mime)) { + targets = true; + continue; + } + if (len == mimes_buf.len) continue; + mimes_buf[len] = mime; + len += 1; + } + break :mimes .{ mimes_buf[0..len], targets }; + }; + + // Per the spec a password without a name is no password. A + // stored grant for it lets the embedder skip its prompt. A + // prompt-exempt request never consults the grants: the + // embedder serves it without a prompt anyway, and consuming a + // one-time paste password on a listing would burn the grant + // before the follow-up data read. + const pw: []const u8 = if (meta.name.len > 0) meta.pw else ""; + const granted = !kitty_clipboard.readPromptExempt(mimes.len) and + self.kitty_clipboard_grants.use(alloc, pw, .read); + + var state: KittyClipboardReadState = .{ + .handler = self, + .primary = meta.loc == .primary, + .id = meta.id, + .pw = pw, + .mimes = mimes, + .list = list, + .terminator = terminator, + }; + func(self, .{ + .location = meta.loc, + .mimes = mimes, + .list = list, + .name = meta.name, + .granted = granted, + .can_remember = pw.len > 0, + .reply_ctx = &state, + .reply_fn = &KittyClipboardReadState.reply, + }); + + // The program is waiting on us, so a callback that returned + // without a reply is answered as a denial rather than silence. + if (!state.replied) state.respondStatus(.EPERM); + } + + /// Reply state for one synchronous Kitty clipboard read. This lives + /// on the kittyClipboardRead stack frame, so it is only valid during + /// the callback. + const KittyClipboardReadState = struct { + handler: *Handler, + primary: bool, + id: []const u8, + + /// The effective password, empty when the request had none. + pw: []const u8, + + /// The requested types; only these are served from a reply. + mimes: []const []const u8, + list: bool, + terminator: osc.Terminator, + replied: bool = false, + + fn reply(ctx: *anyopaque, result: clipboard.Read.Result) void { + const self: *KittyClipboardReadState = @ptrCast(@alignCast(ctx)); + if (self.replied) { + log.warn("clipboard read replied more than once, ignoring", .{}); + return; + } + self.replied = true; + + const success = switch (result) { + .denied => return self.respondStatus(.EPERM), + .unsupported => return self.respondStatus(.ENOSYS), + .busy => return self.respondStatus(.EBUSY), + .io_error => return self.respondStatus(.EIO), + .success => |s| s, + }; + + // Remembering is only offered when the request carried a + // usable password. + if (success.remember and self.pw.len > 0) { + self.handler.kitty_clipboard_grants.grant( + self.handler.terminal.gpa(), + self.pw, + .read, + false, + ) catch |err| { + log.warn("error recording clipboard grant err={}", .{err}); + }; + } + + self.respondSuccess(&success) catch |err| { + log.warn("error replying to clipboard read err={}", .{err}); + self.respondStatus(.EIO); + }; + } + + /// Answer with a single status packet. + fn respondStatus( + self: *const KittyClipboardReadState, + status: kitty_clipboard.Status, + ) void { + self.handler.kittyClipboardRespond(&.{ + .op = .read, + .status = status, + .id = self.id, + .terminator = self.terminator, + }); + } + + /// Answer with the full success sequence (OK, listing, DATA + /// chunks, DONE), serving only the requested representations + /// in request order. + fn respondSuccess( + self: *const KittyClipboardReadState, + success: *const clipboard.Read.Result.Success, + ) error{ OutOfMemory, WriteFailed }!void { + const handler = self.handler; + if (handler.effects.write_pty == null) return; + + var served_buf: [kitty_clipboard.max_read_mimes]clipboard.Content = undefined; + var served_len: usize = 0; + for (self.mimes) |mime| { + for (success.contents) |content| { + if (!std.mem.eql(u8, content.mime, mime)) continue; + served_buf[served_len] = content; + served_len += 1; + break; + } + } + + // Status packets fit on the stack; DATA packets carry the + // clipboard contents and fall back to the heap. + var stack = std.heap.stackFallback(1024, handler.terminal.gpa()); + const alloc = stack.get(); + var aw: std.Io.Writer.Allocating = .init(alloc); + defer aw.deinit(); + try (kitty_clipboard.ReadSuccess{ + .primary = self.primary, + .id = self.id, + .list = self.list, + .available = success.available, + .contents = served_buf[0..served_len], + .terminator = self.terminator, + }).encode(&aw.writer); + + const written = try aw.toOwnedSliceSentinel(0); + defer alloc.free(written); + handler.writePty(written); + } + }; + + fn kittyClipboardWriteBegin( + self: *Handler, + meta: *const kitty_clipboard.Metadata, + terminator: osc.Terminator, + ) error{OutOfMemory}!void { + // A new write silently replaces any in-flight transaction. + self.kittyClipboardAbort(); + + // Without a clipboard_write effect a commit can never succeed, + // so fail the transaction up front instead of spooling data + // we'd only throw away. Later wdata packets are ignored. + if (self.effects.clipboard_write == null) { + self.kittyClipboardRespond(&.{ + .op = .write, + .status = .ENOSYS, + .id = meta.id, + .terminator = terminator, + }); + return; + } + + // Setup our write state + const alloc = self.terminal.gpa(); + const state = try alloc.create(kitty_clipboard.WriteState); + errdefer alloc.destroy(state); + state.* = try .init(alloc, meta, .{ + .max_size = self.kitty_clipboard_write_max_bytes, + }); + self.kitty_clipboard_write = state; + } + + fn kittyClipboardData( + self: *Handler, + meta: *const kitty_clipboard.Metadata, + payload: []const u8, + terminator: osc.Terminator, + ) error{OutOfMemory}!void { + // Data without a transaction is silently ignored. + const state = self.kitty_clipboard_write orelse return; + + // A wdata packet without a MIME type commits the transaction. + if (meta.mime.len == 0) return self.kittyClipboardCommit( + state, + terminator, + ); + + state.data( + self.terminal.gpa(), + meta, + payload, + ) catch |err| switch (err) { + // Failing to spool matches kitty's EIO for a failed buffer + // write. + error.OutOfMemory => { + self.kittyClipboardFinish( + state, + .EIO, + terminator, + ); + return error.OutOfMemory; + }, + + // Data over the write limit aborts the transaction and is + // reported to the client. + error.TooLarge => self.kittyClipboardFinish( + state, + .EFBIG, + terminator, + ), + + // An invalid base64 payload stream aborts the transaction. + error.Invalid => self.kittyClipboardFinish( + state, + .EINVAL, + terminator, + ), + }; + } + + fn kittyClipboardAlias( + self: *Handler, + meta: *const kitty_clipboard.Metadata, + payload: []const u8, + terminator: osc.Terminator, + ) error{OutOfMemory}!void { + // Aliases without a transaction are silently ignored. Once a + // transaction exists, a missing target MIME type is invalid and + // aborts the transaction. + const state = self.kitty_clipboard_write orelse return; + if (meta.mime.len == 0) return self.kittyClipboardFinish( + state, + .EINVAL, + terminator, + ); + + state.alias( + self.terminal.gpa(), + meta, + payload, + ) catch |err| switch (err) { + error.OutOfMemory => { + self.kittyClipboardFinish( + state, + .EIO, + terminator, + ); + return error.OutOfMemory; + }, + + // An undecodable alias payload aborts the transaction. + error.Invalid => self.kittyClipboardFinish( + state, + .EINVAL, + terminator, + ), + }; + } + + fn kittyClipboardCommit( + self: *Handler, + state: *kitty_clipboard.WriteState, + terminator: osc.Terminator, + ) error{OutOfMemory}!void { + const alloc = self.terminal.gpa(); + const committed = state.commit(alloc) catch |err| switch (err) { + error.OutOfMemory => { + self.kittyClipboardFinish(state, .EIO, terminator); + return error.OutOfMemory; + }, + + // The last MIME type's payload stream was not correctly + // padded, which aborts the transaction. + error.Invalid => { + self.kittyClipboardFinish(state, .EINVAL, terminator); + return; + }, + }; + defer committed.deinit(alloc); + + // The effect can't be null here (checked when the transaction + // began) but if an embedder cleared it mid-transaction that's + // ENOSYS. + const func = self.effects.clipboard_write orelse { + self.kittyClipboardFinish(state, .ENOSYS, terminator); + return; + }; + + // Per the spec a password without a name is no password. A + // stored grant for it lets the embedder skip its prompt. + const pw: []const u8 = if (committed.name.len > 0) committed.pw else ""; + const granted = self.kitty_clipboard_grants.use(alloc, pw, .write); + + var reply_state: KittyClipboardWriteReplyState = .{ + .handler = self, + .pw = pw, + }; + func(self, .{ + .location = committed.loc, + .contents = committed.contents, + .name = committed.name, + .granted = granted, + .can_remember = pw.len > 0, + .reply_ctx = &reply_state, + .reply_fn = &KittyClipboardWriteReplyState.reply, + }); + + // The program is waiting on the commit status, so a callback + // that returned without a reply is answered as a denial rather + // than silence. + self.kittyClipboardFinish( + state, + reply_state.status orelse .EPERM, + terminator, + ); + } + + /// Reply state for one synchronous Kitty clipboard write. This lives + /// on the kittyClipboardCommit stack frame, so it is only valid + /// during the callback. + const KittyClipboardWriteReplyState = struct { + handler: *Handler, + + /// The effective password, empty when the request had none. + pw: []const u8, + + /// The replied commit status, mapped 1:1 from the reply result; + /// null until the callback replies. + status: ?kitty_clipboard.Status = null, + + fn reply(ctx: *anyopaque, result: clipboard.Write.Result) void { + const self: *KittyClipboardWriteReplyState = @ptrCast(@alignCast(ctx)); + if (self.status != null) { + log.warn("clipboard write replied more than once, ignoring", .{}); + return; + } + self.status = switch (result) { + .denied => .EPERM, + .unsupported => .ENOSYS, + .busy => .EBUSY, + .invalid_data => .EINVAL, + .io_error => .EIO, + .success => |success| status: { + // Remembering is only offered when the request + // carried a usable password. + if (success.remember and self.pw.len > 0) { + self.handler.kitty_clipboard_grants.grant( + self.handler.terminal.gpa(), + self.pw, + .write, + false, + ) catch |err| { + log.warn("error recording clipboard grant err={}", .{err}); + }; + } + break :status .DONE; + }, + }; + } + }; + + /// Answer a write transaction with its final status and drop it. + /// The id echoed is the one from the transaction's opening write + /// packet, matching kitty. + fn kittyClipboardFinish( + self: *Handler, + state: *const kitty_clipboard.WriteState, + status: kitty_clipboard.Status, + terminator: osc.Terminator, + ) void { + self.kittyClipboardRespond(&.{ + .op = .write, + .status = status, + .id = state.id, + .terminator = terminator, }); + self.kittyClipboardAbort(); + } + + /// Drop any in-flight write transaction without responding. + fn kittyClipboardAbort(self: *Handler) void { + if (self.kitty_clipboard_write) |state| { + const alloc = self.terminal.gpa(); + state.deinit(alloc); + alloc.destroy(state); + self.kitty_clipboard_write = null; + } + } + + /// Encode and write a single response packet. Unlike kitty, which + /// always terminates responses with ST, we echo the terminator of + /// the request being answered, matching our other OSC responses. + fn kittyClipboardRespond( + self: *Handler, + response: *const kitty_clipboard.Response, + ) void { + if (self.effects.write_pty == null) return; + + // Our responses carry at most a status and the echoed id so + // they virtually always fit on the stack. + var stack = std.heap.stackFallback(1024, self.terminal.gpa()); + const alloc = stack.get(); + var aw: std.Io.Writer.Allocating = .init(alloc); + defer aw.deinit(); + response.encode(&aw.writer) catch return; + const resp = aw.toOwnedSliceSentinel(0) catch return; + defer alloc.free(resp); + self.writePty(resp); + } + + /// Handle an OSC 72 drag and drop command. + fn kittyDnd( + self: *Handler, + v: Action.KittyDnd, + ) (Allocator.Error || std.Io.Writer.Error)!void { + // Responses are usually small (queries, errors) but data + // serving can produce many chunks, so fall back to the heap. + var stack = std.heap.stackFallback(512, self.terminal.gpa()); + const response_alloc = stack.get(); + var aw: std.Io.Writer.Allocating = .init(response_alloc); + defer aw.deinit(); + + // The state is allocated on registration and owned by the + // terminal, so it uses the terminal's allocator, not the + // response's. + const event = try kitty_dnd.handleCommand( + &self.terminal.kitty_dnd, + self.terminal.gpa(), + &aw.writer, + v, + ); + + if (aw.written().len > 0) { + const written = aw.toOwnedSliceSentinel(0) catch return; + defer response_alloc.free(written); + self.writePty(written); + } + + if (event) |ev| { + const func = self.effects.drag_and_drop orelse return; + func(self, ev); + } } fn reportDeviceAttributes(self: *Handler, req: device_attributes.Req) void { @@ -412,9 +1367,24 @@ pub const Handler = struct { buf[writer.end] = 0; self.writePty(buf[0..writer.end :0]); }, + + .visibility => self.sendVisibilityReport(), } } + fn sendVisibilityReport(self: *Handler) void { + const write_pty = self.effects.write_pty orelse return; + + var buf: [device_status.max_visibility_report_encode_size + 1]u8 = undefined; + var writer: std.Io.Writer = .fixed(buf[0..device_status.max_visibility_report_encode_size]); + device_status.encodeVisibilityReport( + &writer, + if (self.terminal.flags.visible) .potentially_visible else .not_visible, + ) catch return; + buf[writer.end] = 0; + write_pty(self, buf[0..writer.end :0]); + } + fn reportEnquiry(self: *Handler) void { const func = self.effects.enquiry orelse return; const response = func(self); @@ -453,6 +1423,7 @@ pub const Handler = struct { // Build the response. switch (style) { .csi_21_t => { + if (!self.title_report) return; const title = self.terminal.getTitle() orelse ""; aw.writer.print("\x1b]l{s}\x1b\\", .{title}) catch return; }, @@ -482,7 +1453,18 @@ pub const Handler = struct { self.writePty(resp); } - fn windowTitle(self: *Handler, title_raw: []const u8) void { + fn reportMode2048(self: *Handler) void { + const get_size = self.effects.size orelse return; + const current = get_size(self) orelse return; + + var buf: [128]u8 = undefined; + var writer: std.Io.Writer = .fixed(buf[0 .. buf.len - 1]); + size_report.encode(&writer, .mode_2048, current) catch return; + buf[writer.end] = 0; + self.writePty(buf[0..writer.end :0]); + } + + fn windowTitle(self: *Handler, title_raw: []const u8) !void { // Prevent DoS attacks by limiting title length. const max_title_len = 1024; const title = if (title_raw.len > max_title_len) title: { @@ -493,16 +1475,13 @@ pub const Handler = struct { break :title title_raw[0..max_title_len]; } else title_raw; - self.terminal.setTitle(title) catch |err| { - log.warn("error setting title err={}", .{err}); - return; - }; + try self.terminal.setTitle(title); const func = self.effects.title_changed orelse return; func(self); } - fn reportPwd(self: *Handler, url_raw: []const u8) void { + fn reportPwd(self: *Handler, url_raw: []const u8) !void { // Prevent DoS attacks by limiting url length. Headroom for // Linux PATH_MAX (4096) plus URI scheme/host and percent-encoding. const max_url_len = 4096; @@ -516,17 +1495,23 @@ pub const Handler = struct { // We store the raw payload unparsed. Embedders read it via // getPwd() and are responsible for decoding any URI scheme. - self.terminal.setPwd(url) catch |err| { - log.warn("error setting pwd err={}", .{err}); - return; - }; + try self.terminal.setPwd(url); const func = self.effects.pwd_changed orelse return; func(self); } fn requestMode(self: *Handler, mode: modes.Mode) void { - const report = self.terminal.modes.getReport(.fromMode(mode)); + var report = self.terminal.modes.getReport(.fromMode(mode)); + + // Kitty paste events (mode 5522) can't work without a clipboard + // read effect, so if that isn't set mark it as unrecognized. + if (mode == .kitty_paste_events and + self.effects.clipboard_read == null) + { + report.state = .not_recognized; + } + self.sendModeReport(report); } @@ -611,10 +1596,16 @@ pub const Handler = struct { .synchronized_output, .linefeed, - .in_band_size_reports, .focus_event, => {}, + // Enabling mode 2048 reports already-committed pixel geometry. + // Waiting for the next resize leaves late-enabling clients without + // the dimensions they need for their first image frame. + .in_band_size_reports => if (enabled) self.reportMode2048(), + + .report_visibility => if (enabled) self.sendVisibilityReport(), + .mouse_event_x10 => { if (enabled) { self.terminal.flags.mouse_event = .x10; @@ -834,14 +1825,21 @@ pub const Handler = struct { } } - fn apcEnd(self: *Handler) void { + fn apcEnd(self: *Handler, terminated: bool) void { + const io = self.terminal.io(); const alloc = self.terminal.gpa(); - var cmd = self.apc_handler.end() orelse return; - defer cmd.deinit(alloc); - - switch (cmd) { + var result = self.apc_handler.end() orelse return; + defer result.deinit(alloc); + switch (result) { + .unknown => |*unknown| { + if (terminated) self.unknownSequence(.{ .apc = .{ + .content = unknown.content, + .truncated = unknown.truncated, + } }); + }, .kitty => |*kitty_cmd| if (comptime build_options.kitty_graphics) { if (self.terminal.kittyGraphics( + io, alloc, kitty_cmd, )) |resp| resp: { @@ -859,7 +1857,7 @@ pub const Handler = struct { } }, - .glyph => |*glyph_req| { + .glyph => |*glyph_req| if (comptime build_options.glyph_protocol) { const resp = self.terminal.glyphProtocol(alloc, glyph_req); if (resp) |r| resp_block: { // Don't waste time encoding if we can't write responses @@ -881,11 +1879,291 @@ pub const Handler = struct { } }; +/// A writer that delivers everything through the write_pty effect: +/// the buffer as it fills, and data that doesn't fit it directly. +/// Never fails, since the effect can't. +const PtyWriter = struct { + handler: *Handler, + writer: std.Io.Writer, + + fn init(handler: *Handler, buffer: []u8) PtyWriter { + return .{ + .handler = handler, + .writer = .{ + .vtable = &.{ .drain = drain }, + .buffer = buffer, + }, + }; + } + + fn drain( + w: *std.Io.Writer, + data: []const []const u8, + splat: usize, + ) std.Io.Writer.Error!usize { + const self: *PtyWriter = @alignCast(@fieldParentPtr("writer", w)); + + // Buffered bytes go first to keep the order. + if (w.end > 0) { + self.handler.writePty(w.buffer[0..w.end]); + w.end = 0; + } + + var consumed: usize = 0; + for (data[0 .. data.len - 1]) |slice| { + if (slice.len > 0) self.handler.writePty(slice); + consumed += slice.len; + } + + const pattern = data[data.len - 1]; + for (0..splat) |_| { + if (pattern.len > 0) self.handler.writePty(pattern); + consumed += pattern.len; + } + return consumed; + } +}; + +test "resize clears synchronized output on unchanged cell dimensions" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); + defer s.deinit(); + + t.modes.set(.synchronized_output, true); + try s.handler.resize(.{ + .cols = 80, + .rows = 24, + .cell_size_px = .{ .width = 9, .height = 18 }, + }); + + try testing.expect(!t.modes.get(.synchronized_output)); + try testing.expectEqual(@as(u32, 720), t.width_px); + try testing.expectEqual(@as(u32, 432), t.height_px); +} + +test "unknown APC effect callback" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = struct { + var count: usize = 0; + var content: [16]u8 = undefined; + var content_len: usize = undefined; + var truncated: bool = undefined; + + fn unknownSequence(_: *Handler, value: Handler.UnknownSequence) void { + switch (value) { + .apc => |apc_value| { + content_len = apc_value.content.len; + @memcpy(content[0..apc_value.content.len], apc_value.content); + truncated = apc_value.truncated; + }, + } + count += 1; + } + }; + S.count = 0; + + var handler: Handler = .init(&t); + handler.unknown_sequence = &S.unknownSequence; + handler.apc_handler.unknown_max_bytes = 8; + var s: Stream = .init(.{ + .allocator = testing.allocator, + .handler = handler, + }); + defer s.deinit(); + + // Unknown OSC commands retain their legacy behavior and are ignored. + s.nextSlice("\x1B]999;abcdef\x07"); + s.nextSlice("\x1B_abcd;payload\x1B\\"); + + try testing.expectEqual(@as(usize, 1), S.count); + try testing.expectEqualStrings("abcd;pay", S.content[0..S.content_len]); + try testing.expect(S.truncated); + + // Aborted unknown APCs are suppressed. + s.nextSlice("\x1B_Xpayload\x18"); + try testing.expectEqual(@as(usize, 1), S.count); +} + +test "resize reports mode 2048 geometry" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = struct { + var response: [128]u8 = undefined; + var response_len: usize = 0; + + fn writePty(_: *Handler, data: []const u8) void { + @memcpy(response[0..data.len], data); + response_len = data.len; + } + }; + S.response_len = 0; + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + t.modes.set(.in_band_size_reports, true); + try s.handler.resize(.{ + .cols = 100, + .rows = 40, + .cell_size_px = .{ .width = 9, .height = 18 }, + }); + + try testing.expectEqualStrings( + "\x1B[48;40;100;720;900t", + S.response[0..S.response_len], + ); +} + +test "resize suppresses mode 2048 reports" { + const S = struct { + var calls: usize = 0; + + fn writePty(_: *Handler, _: []const u8) void { + calls += 1; + } + }; + S.calls = 0; + + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Disabled mode suppresses a report even with pixels and a callback. + try s.handler.resize(.{ + .cols = 80, + .rows = 24, + .cell_size_px = .{ .width = 9, .height = 18 }, + }); + try testing.expectEqual(@as(usize, 0), S.calls); + + // Missing pixel geometry suppresses a report even with the mode enabled. + t.modes.set(.in_band_size_reports, true); + try s.handler.resize(.{ .cols = 80, .rows = 24 }); + try testing.expectEqual(@as(usize, 0), S.calls); + + // A read-only stream has no write effect and remains successful. + var readonly_terminal: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer readonly_terminal.deinit(testing.allocator); + readonly_terminal.modes.set(.in_band_size_reports, true); + var readonly_stream: Stream = .init(.{ + .allocator = testing.allocator, + .handler = .init(&readonly_terminal), + }); + defer readonly_stream.deinit(); + try readonly_stream.handler.resize(.{ + .cols = 80, + .rows = 24, + .cell_size_px = .{ .width = 9, .height = 18 }, + }); + try testing.expectEqual(@as(usize, 0), S.calls); +} + +test "resize failure preserves terminal state and does not write" { + var failing = testing.FailingAllocator.init(testing.allocator, .{}); + const alloc = failing.allocator(); + var t: Terminal = try .init(testing.io, alloc, .{ .cols = 10, .rows = 1 }); + defer t.deinit(alloc); + + const S = struct { + var called: bool = false; + + fn writePty(_: *Handler, _: []const u8) void { + called = true; + } + }; + S.called = false; + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = alloc, .handler = handler }); + defer s.deinit(); + + t.modes.set(.synchronized_output, true); + t.modes.set(.in_band_size_reports, true); + failing.fail_index = failing.alloc_index; + try testing.expectError(error.OutOfMemory, s.handler.resize(.{ + .cols = 513, + .rows = 1, + .cell_size_px = .{ .width = 9, .height = 18 }, + })); + + try testing.expect(t.modes.get(.synchronized_output)); + try testing.expect(!S.called); + try testing.expectEqual(@as(@TypeOf(t.cols), 10), t.cols); + try testing.expectEqual(@as(u32, 0), t.width_px); + try testing.expectEqual(@as(u32, 0), t.height_px); +} + +test "resize effects do not change canonical terminal state" { + var authoritative: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 10, .rows = 5 }, + ); + defer authoritative.deinit(testing.allocator); + var readonly: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 10, .rows = 5 }, + ); + defer readonly.deinit(testing.allocator); + + const S = struct { + fn writePty(_: *Handler, _: []const u8) void {} + }; + var authoritative_handler: Handler = .init(&authoritative); + authoritative_handler.effects.write_pty = &S.writePty; + var authoritative_stream: Stream = .init(.{ + .allocator = testing.allocator, + .handler = authoritative_handler, + }); + defer authoritative_stream.deinit(); + var readonly_stream: Stream = .init(.{ + .allocator = testing.allocator, + .handler = .init(&readonly), + }); + defer readonly_stream.deinit(); + + authoritative.modes.set(.in_band_size_reports, true); + readonly.modes.set(.in_band_size_reports, true); + const value: Terminal.Resize = .{ + .cols = 20, + .rows = 10, + .cell_size_px = .{ .width = 9, .height = 18 }, + }; + try authoritative_stream.handler.resize(value); + try readonly_stream.handler.resize(value); + + try testing.expectEqual(authoritative.cols, readonly.cols); + try testing.expectEqual(authoritative.rows, readonly.rows); + try testing.expectEqual(authoritative.width_px, readonly.width_px); + try testing.expectEqual(authoritative.height_px, readonly.height_px); + try testing.expect(std.meta.eql(authoritative.modes, readonly.modes)); + try testing.expect(std.meta.eql( + authoritative.scrolling_region, + readonly.scrolling_region, + )); +} + test "basic print" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); s.nextSlice("Hello"); @@ -897,11 +2175,44 @@ test "basic print" { try testing.expectEqualStrings("Hello", str); } +test "semantic failure is sticky while processing continues" { + var failing = testing.FailingAllocator.init(testing.allocator, .{}); + const alloc = failing.allocator(); + var t: Terminal = try .init(testing.io, alloc, .{ .cols = 10, .rows = 2 }); + defer t.deinit(alloc); + + var s: Stream = .init(.{ .allocator = alloc, .handler = .init(&t) }); + defer s.deinit(); + try testing.expect(!s.handler.semantic_failure); + + // Setting the title is a terminal-owned semantic update. Force its + // allocation to fail at the central vtFallible boundary. + failing.fail_index = failing.alloc_index; + s.nextSlice("\x1B]2;unavailable\x1B\\"); + try testing.expect(s.handler.semantic_failure); + + // Later input and RIS remain best-effort and never clear the diagnostic. + failing.fail_index = std.math.maxInt(usize); + s.nextSlice("ignored"); + s.nextSlice("\x1Bc"); + s.nextSlice("OK"); + try testing.expect(s.handler.semantic_failure); + + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("OK", str); + + // A new execution root starts without inheriting the diagnostic. + var fresh = Handler.init(&t); + defer fresh.deinit(); + try testing.expect(!fresh.semantic_failure); +} + test "cursor movement" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Move cursor using escape sequences @@ -916,10 +2227,10 @@ test "cursor movement" { } test "erase operations" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 20, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 20, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Print some text @@ -937,10 +2248,10 @@ test "erase operations" { } test "tabs" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); s.nextSlice("A\tB"); @@ -952,10 +2263,10 @@ test "tabs" { } test "modes" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Test wraparound mode @@ -967,10 +2278,10 @@ test "modes" { } test "scrolling regions" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set scrolling region from line 5 to 20 @@ -982,10 +2293,10 @@ test "scrolling regions" { } test "charsets" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Configure G0 as DEC special graphics @@ -998,10 +2309,10 @@ test "charsets" { } test "alt screen" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 5 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 5 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Write to primary screen @@ -1025,10 +2336,10 @@ test "alt screen" { } test "cursor save and restore" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Move cursor to 10,15 @@ -1051,10 +2362,10 @@ test "cursor save and restore" { } test "attributes" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set bold and write text @@ -1066,11 +2377,258 @@ test "attributes" { try testing.expectEqualStrings("Bold", str); } +test "DECRQSS responses" { + var t: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer t.deinit(testing.allocator); + + const S = struct { + var response: [dcs.Command.DECRQSS.max_response_bytes]u8 = undefined; + var response_len: usize = 0; + var calls: usize = 0; + + fn reset() void { + response_len = 0; + calls = 0; + } + + fn writePty(_: *Handler, data: []const u8) void { + @memcpy(response[0..data.len], data); + response_len = data.len; + calls += 1; + } + + fn expectResponse(expected: []const u8) !void { + try testing.expectEqual(@as(usize, 1), calls); + try testing.expectEqualStrings( + expected, + response[0..response_len], + ); + reset(); + } + }; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // SGR + s.nextSlice("\x1B[1m\x1BP$qm\x1B\\"); + try S.expectResponse("\x1BP1$r0;1m\x1B\\"); + + // Overline + s.nextSlice("\x1B[0;53m\x1BP$qm\x1B\\"); + try S.expectResponse("\x1BP1$r0;53m\x1B\\"); + + // Requests larger than the parser's fixed request buffer are ignored, + // and the next DCS command must still be processed normally. + s.nextSlice("\x1BP$qfoo\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.calls); + try testing.expect(!s.handler.semantic_failure); + s.nextSlice("\x1BP$qm\x1B\\"); + try S.expectResponse("\x1BP1$r0;53m\x1B\\"); +} + +test "DECRQSS without write effect is ignored" { + var t: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer t.deinit(testing.allocator); + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); + defer s.deinit(); + + s.nextSlice("\x1BP$qm\x1B\\"); + try testing.expect(!s.handler.semantic_failure); +} + +test "XTGETTCAP responses" { + var t: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer t.deinit(testing.allocator); + + const S = struct { + var response: [128]u8 = undefined; + var response_len: usize = 0; + var calls: usize = 0; + + fn reset() void { + response_len = 0; + calls = 0; + } + + fn writePty(_: *Handler, data: []const u8) void { + @memcpy(response[0..data.len], data); + response_len = data.len; + calls += 1; + } + + fn expectResponse(expected: []const u8) !void { + try testing.expectEqual(@as(usize, 1), calls); + try testing.expectEqualStrings( + expected, + response[0..response_len], + ); + reset(); + } + }; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // The full capability table comes from the static terminfo map; this + // checks the wiring for a valued and a valueless (boolean) capability. + s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("Co", .upper) ++ "\x1B\\"); + try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("Co", .upper) ++ "=" ++ + std.fmt.bytesToHex("256", .upper) ++ "\x1B\\"); + s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("am", .upper) ++ "\x1B\\"); + try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("am", .upper) ++ "\x1B\\"); + + // One response per requested key; lowercase hex is normalized by the + // DCS parser. The capture holds the last ("Co") reply. + s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("am", .lower) ++ ";" ++ + std.fmt.bytesToHex("Co", .lower) ++ "\x1B\\"); + try testing.expectEqual(@as(usize, 2), S.calls); + try testing.expectEqualStrings( + "\x1BP1+r" ++ std.fmt.bytesToHex("Co", .upper) ++ "=" ++ + std.fmt.bytesToHex("256", .upper) ++ "\x1B\\", + S.response[0..S.response_len], + ); + S.reset(); + + // Unknown and malformed keys are skipped without an error. + s.nextSlice("\x1BP+qWHO;5;GG\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.calls); + try testing.expect(!s.handler.semantic_failure); +} + +test "XTGETTCAP without write effect is ignored" { + var t: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer t.deinit(testing.allocator); + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); + defer s.deinit(); + + s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("TN", .upper) ++ ";" ++ + std.fmt.bytesToHex("am", .upper) ++ "\x1B\\"); + try testing.expect(!s.handler.semantic_failure); +} + +test "XTGETTCAP TN responses" { + var t: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer t.deinit(testing.allocator); + + const S = struct { + var response: [512]u8 = undefined; + var response_len: usize = 0; + var calls: usize = 0; + + fn reset() void { + response_len = 0; + calls = 0; + } + + fn writePty(_: *Handler, data: []const u8) void { + @memcpy(response[0..data.len], data); + response_len = data.len; + calls += 1; + } + + fn expectResponse(expected: []const u8) !void { + try testing.expectEqual(@as(usize, 1), calls); + try testing.expectEqualStrings( + expected, + response[0..response_len], + ); + reset(); + } + }; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + const tn_query = "\x1BP+q" ++ std.fmt.bytesToHex("TN", .upper) ++ "\x1B\\"; + + // While no name is configured the query goes unanswered. + s.nextSlice(tn_query); + try testing.expectEqual(@as(usize, 0), S.calls); + + // A configured name is reported hex-encoded. + s.handler.terminfo_name = "xterm-256color"; + s.nextSlice(tn_query); + try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("TN", .upper) ++ "=" ++ + std.fmt.bytesToHex("xterm-256color", .upper) ++ "\x1B\\"); + + // A maximum-length name is still reported in full. + const max_name = "a" ** Handler.max_terminfo_name_bytes; + s.handler.terminfo_name = max_name; + s.nextSlice(tn_query); + try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("TN", .upper) ++ "=" ++ + std.fmt.bytesToHex(max_name.*, .upper) ++ "\x1B\\"); + + // An empty name is silent; "Co" is still answered. + s.handler.terminfo_name = ""; + s.nextSlice("\x1BP+q" ++ std.fmt.bytesToHex("TN", .upper) ++ ";" ++ + std.fmt.bytesToHex("Co", .upper) ++ "\x1B\\"); + try S.expectResponse("\x1BP1+r" ++ std.fmt.bytesToHex("Co", .upper) ++ "=" ++ + std.fmt.bytesToHex("256", .upper) ++ "\x1B\\"); + + // As are names beyond the maximum length. + s.handler.terminfo_name = "a" ** (Handler.max_terminfo_name_bytes + 1); + s.nextSlice(tn_query); + try testing.expectEqual(@as(usize, 0), S.calls); + try testing.expect(!s.handler.semantic_failure); +} + +test "DCS command memory is released" { + var t: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer t.deinit(testing.allocator); + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); + + // A completed command transfers its allocation to Command; dcsCommand + // must release it even when there is no write effect. + s.nextSlice("\x1BP+q536D756C78\x1B\\"); + + // An incomplete command remains owned by the handler and must be released + // when the stream is deinitialized. testing.allocator detects either leak. + s.nextSlice("\x1BP+q536D756C78"); + s.deinit(); +} + test "DECALN screen alignment" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 3 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 3 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Run DECALN @@ -1087,10 +2645,10 @@ test "DECALN screen alignment" { } test "full reset" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Make some changes @@ -1114,12 +2672,14 @@ test "full reset" { } test "glyph protocol APC with write_pty callback" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + if (comptime !build_options.glyph_protocol) return error.SkipZigTest; + + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var last_response: ?[:0]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (last_response) |old| testing.allocator.free(old); last_response = testing.allocator.dupeZ(u8, data) catch @panic("OOM"); } @@ -1130,7 +2690,7 @@ test "glyph protocol APC with write_pty callback" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x1B_25a1;s\x1B\\"); @@ -1142,10 +2702,10 @@ test "glyph protocol APC with write_pty callback" { } test "ignores query actions" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // These should be ignored without error @@ -1154,6 +2714,13 @@ test "ignores query actions" { s.nextSlice("\x1B[6n"); // Cursor position report s.nextSlice("\x1B]4;0;?\x1B\\"); // OSC color query s.nextSlice("\x1B]21;foreground=?\x1B\\"); // Kitty color query + s.nextSlice("\x1B]52;c;%%%invalid-base64%%%\x1B\\"); + s.nextSlice("\x1B_Ga=p,i=999\x1B\\"); // Missing Kitty image + s.nextSlice("\x1B_25a1;r;cp=41;%%%invalid%%%\x1B\\"); // Rejected glyph + + // Query, malformed input, protocol failure responses, and external-effect + // failures do not imply that terminal-owned semantic state diverged. + try testing.expect(!s.handler.semantic_failure); // Terminal should still be functional s.nextSlice("Test"); @@ -1163,10 +2730,10 @@ test "ignores query actions" { } test "OSC 4 set and reset palette" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Save default color @@ -1186,10 +2753,10 @@ test "OSC 4 set and reset palette" { } test "OSC 104 reset all palette colors" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set multiple colors @@ -1211,10 +2778,10 @@ test "OSC 104 reset all palette colors" { } test "OSC 10 set and reset foreground color" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Initially unset @@ -1233,12 +2800,15 @@ test "OSC 10 set and reset foreground color" { } test "OSC 11 set and reset background color" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); + const default: color.RGB = .{ .r = 0x10, .g = 0x20, .b = 0x30 }; + t.colors.background.default = default; + // Set background to green s.nextSlice("\x1b]11;rgb:00/ff/00\x1b\\"); const bg = t.colors.background.get().?; @@ -1248,14 +2818,20 @@ test "OSC 11 set and reset background color" { // Reset background s.nextSlice("\x1b]111\x1b\\"); - try testing.expect(t.colors.background.get() == null); + try testing.expectEqual(default, t.colors.background.get().?); + try testing.expectEqual(null, t.colors.background.override); + + // A reset color continues to follow later configuration changes. + const updated: color.RGB = .{ .r = 0x40, .g = 0x50, .b = 0x60 }; + t.colors.background.default = updated; + try testing.expectEqual(updated, t.colors.background.get().?); } test "OSC 12 set and reset cursor color" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set cursor to blue @@ -1271,7 +2847,7 @@ test "OSC 12 set and reset cursor color" { } test "OSC color query responses" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); const S = struct { @@ -1282,7 +2858,7 @@ test "OSC color query responses" { last_response = null; } - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { reset(); last_response = testing.allocator.dupeZ(u8, data) catch @panic("OOM"); } @@ -1293,7 +2869,7 @@ test "OSC color query responses" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x1b]10;?\x1b\\"); @@ -1328,10 +2904,10 @@ test "OSC color query responses" { } test "kitty color protocol set palette" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set palette color 5 to magenta using kitty protocol @@ -1344,10 +2920,10 @@ test "kitty color protocol set palette" { } test "kitty color protocol reset palette" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set and then reset palette color @@ -1361,10 +2937,10 @@ test "kitty color protocol reset palette" { } test "kitty color protocol set foreground" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set foreground using kitty protocol @@ -1376,10 +2952,10 @@ test "kitty color protocol set foreground" { } test "kitty color protocol set background" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set background using kitty protocol @@ -1391,10 +2967,10 @@ test "kitty color protocol set background" { } test "kitty color protocol set cursor" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set cursor using kitty protocol @@ -1406,10 +2982,10 @@ test "kitty color protocol set cursor" { } test "kitty color protocol reset foreground" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set and reset foreground @@ -1422,7 +2998,7 @@ test "kitty color protocol reset foreground" { } test "kitty color protocol query responses" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); const S = struct { @@ -1433,7 +3009,7 @@ test "kitty color protocol query responses" { last_response = null; } - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { reset(); last_response = testing.allocator.dupeZ(u8, data) catch @panic("OOM"); } @@ -1444,7 +3020,7 @@ test "kitty color protocol query responses" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x1b]21;background=?\x1b\\"); @@ -1462,10 +3038,10 @@ test "kitty color protocol query responses" { } test "palette dirty flag set on color change" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Clear dirty flag @@ -1487,10 +3063,10 @@ test "palette dirty flag set on color change" { } test "semantic prompt fresh line" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); s.nextSlice("Hello"); @@ -1500,10 +3076,10 @@ test "semantic prompt fresh line" { } test "semantic prompt fresh line new prompt" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Write some text and then send OSC 133;A (fresh_line_new_prompt) @@ -1524,10 +3100,10 @@ test "semantic prompt fresh line new prompt" { } test "semantic prompt end of input, then start output" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Write some text and then send OSC 133;A (fresh_line_new_prompt) @@ -1541,10 +3117,10 @@ test "semantic prompt end of input, then start output" { } test "semantic prompt prompt_start" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Write some text @@ -1558,10 +3134,10 @@ test "semantic prompt prompt_start" { } test "semantic prompt new_command" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Write some text @@ -1576,10 +3152,10 @@ test "semantic prompt new_command" { } test "semantic prompt new_command at column zero" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // OSC 133;N when already at column 0 should stay on same line @@ -1590,10 +3166,10 @@ test "semantic prompt new_command at column zero" { } test "semantic prompt end_prompt_start_input_terminate_eol clears on linefeed" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Set input terminated by EOL @@ -1606,12 +3182,12 @@ test "semantic prompt end_prompt_start_input_terminate_eol clears on linefeed" { } test "bell effect callback" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); // Test bell with null callback (default readonly effects) doesn't crash { - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); s.nextSlice("\x07"); @@ -1638,7 +3214,7 @@ test "bell effect callback" { var handler: Handler = .init(&t); handler.effects.bell = &S.bell; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x07"); @@ -1649,162 +3225,1351 @@ test "bell effect callback" { } } -test "clipboard_write effect callback" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); +test "desktop_notification effect callback" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - // A null callback (the default readonly effects) silently ignores writes. + // A null callback (the default readonly effects) silently ignores + // notifications and leaves the terminal usable. { - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); - s.nextSlice("\x1B]52;c;aGVsbG8=\x1B\\"); - - // Terminal should still be functional after the ignored sequence - s.nextSlice("AfterClipboard"); + s.nextSlice("\x1B]9;Ignored\x1B\\AfterNotification"); const str = try t.plainString(testing.allocator); defer testing.allocator.free(str); - try testing.expectEqualStrings("AfterClipboard", str); + try testing.expectEqualStrings("AfterNotification", str); } t.fullReset(); const S = struct { var count: usize = 0; - var result: clipboard.WriteResult = .success; - var last_location: clipboard.Location = .standard; - var last_contents_len: usize = 0; - var last_mime: ?[]u8 = null; - var last_data: ?[]u8 = null; + var last_title: []const u8 = ""; + var last_body: []const u8 = ""; - fn clearCapture() void { - if (last_mime) |value| testing.allocator.free(value); - if (last_data) |value| testing.allocator.free(value); - last_mime = null; - last_data = null; - last_contents_len = 0; + fn desktopNotification( + _: *Handler, + notification: Action.ShowDesktopNotification, + ) void { + count += 1; + last_title = notification.title; + last_body = notification.body; } + }; + S.count = 0; + S.last_title = ""; + S.last_body = ""; - fn clipboardWrite(_: *Handler, write: clipboard.Write) clipboard.WriteResult { - clearCapture(); + var handler: Handler = .init(&t); + handler.effects.desktop_notification = &S.desktopNotification; + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // OSC 9 is split across writes and carries only a body. + s.nextSlice("\x1B]9;Build "); + try testing.expectEqual(@as(usize, 0), S.count); + s.nextSlice("complete\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.count); + try testing.expectEqualStrings("", S.last_title); + try testing.expectEqualStrings("Build complete", S.last_body); + + // OSC 777 preserves its separate title and body fields. + s.nextSlice("\x1B]777;notify;Codex;Needs attention\x07"); + try testing.expectEqual(@as(usize, 2), S.count); + try testing.expectEqualStrings("Codex", S.last_title); + try testing.expectEqualStrings("Needs attention", S.last_body); +} + +test "progress_report effect callback" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + // A null callback (the default readonly effects) silently ignores reports. + { + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); + defer s.deinit(); + s.nextSlice("\x1B]9;4;1;25\x1B\\"); + } + + const S = struct { + var count: usize = 0; + var last_state: osc.Command.ProgressReport.State = .remove; + var last_progress: ?u8 = null; + + fn progressReport(_: *Handler, report: osc.Command.ProgressReport) void { count += 1; - last_location = write.location; - last_contents_len = write.contents.len; - if (write.contents.len > 0) { - last_mime = testing.allocator.dupe(u8, write.contents[0].mime) catch - @panic("failed to capture clipboard MIME type"); - last_data = testing.allocator.dupe(u8, write.contents[0].data) catch - @panic("failed to capture clipboard data"); - } - return result; + last_state = report.state; + last_progress = report.progress; } }; S.count = 0; - S.result = .denied; - S.clearCapture(); - defer S.clearCapture(); + S.last_state = .remove; + S.last_progress = null; var handler: Handler = .init(&t); - handler.effects.clipboard_write = &S.clipboardWrite; + handler.effects.progress_report = &S.progressReport; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); - // Selectors are normalized and payloads are decoded before the callback. const cases = [_]struct { sequence: []const u8, - location: clipboard.Location, - data: []const u8, + state: osc.Command.ProgressReport.State, + progress: ?u8, }{ - .{ .sequence = "\x1B]52;c;aGVsbG8=\x1B\\", .location = .standard, .data = "hello" }, + .{ .sequence = "\x1B]9;4;0;\x1B\\", .state = .remove, .progress = null }, + .{ .sequence = "\x1B]9;4;1;42\x07", .state = .set, .progress = 42 }, + .{ .sequence = "\x1B]9;4;2;7\x1B\\", .state = .@"error", .progress = 7 }, + .{ .sequence = "\x1B]9;4;3\x1B\\", .state = .indeterminate, .progress = null }, + .{ .sequence = "\x1B]9;4;4;75\x1B\\", .state = .pause, .progress = 75 }, + }; + + for (cases, 1..) |case, expected_count| { + // Split each sequence to verify parsing survives PTY read boundaries. + const midpoint = case.sequence.len / 2; + s.nextSlice(case.sequence[0..midpoint]); + try testing.expectEqual(expected_count - 1, S.count); + s.nextSlice(case.sequence[midpoint..]); + try testing.expectEqual(expected_count, S.count); + try testing.expectEqual(case.state, S.last_state); + try testing.expectEqual(case.progress, S.last_progress); + } + + // A full reset (RIS) removes any active progress bar. + s.nextSlice("\x1B]9;4;1;50\x1B\\"); + try testing.expectEqual(@as(usize, cases.len + 1), S.count); + try testing.expectEqual(osc.Command.ProgressReport.State.set, S.last_state); + s.nextSlice("\x1Bc"); + try testing.expectEqual(@as(usize, cases.len + 2), S.count); + try testing.expectEqual(osc.Command.ProgressReport.State.remove, S.last_state); + try testing.expectEqual(@as(?u8, null), S.last_progress); +} + +test "clipboard_write effect callback" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + // A null callback (the default readonly effects) silently ignores writes. + { + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); + defer s.deinit(); + + s.nextSlice("\x1B]52;c;aGVsbG8=\x1B\\"); + + // Terminal should still be functional after the ignored sequence + s.nextSlice("AfterClipboard"); + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("AfterClipboard", str); + } + + t.fullReset(); + + const S = struct { + var count: usize = 0; + var result: clipboard.Write.Result = .{ .success = .{} }; + var last_location: clipboard.Location = .standard; + var last_contents_len: usize = 0; + var last_mime: ?[]u8 = null; + var last_data: ?[]u8 = null; + + fn clearCapture() void { + if (last_mime) |value| testing.allocator.free(value); + if (last_data) |value| testing.allocator.free(value); + last_mime = null; + last_data = null; + last_contents_len = 0; + } + + fn clipboardWrite(_: *Handler, write: clipboard.Write) void { + clearCapture(); + count += 1; + last_location = write.location; + last_contents_len = write.contents.len; + if (write.contents.len > 0) { + last_mime = testing.allocator.dupe(u8, write.contents[0].mime) catch + @panic("failed to capture clipboard MIME type"); + last_data = testing.allocator.dupe(u8, write.contents[0].data) catch + @panic("failed to capture clipboard data"); + } + write.reply(result); + } + }; + S.count = 0; + S.result = .denied; + S.clearCapture(); + defer S.clearCapture(); + + var handler: Handler = .init(&t); + handler.effects.clipboard_write = &S.clipboardWrite; + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Selectors are normalized and payloads are decoded before the callback. + const cases = [_]struct { + sequence: []const u8, + location: clipboard.Location, + data: []const u8, + }{ + .{ .sequence = "\x1B]52;c;aGVsbG8=\x1B\\", .location = .standard, .data = "hello" }, .{ .sequence = "\x1B]52;s;d29ybGQ=\x07", .location = .selection, .data = "world" }, .{ .sequence = "\x1B]52;p;cHJpbWFyeQ==\x1B\\", .location = .primary, .data = "primary" }, .{ .sequence = "\x1B]52;0;Y3V0\x1B\\", .location = .standard, .data = "cut" }, .{ .sequence = "\x1B]52;x;ZmFsbGJhY2s=\x1B\\", .location = .standard, .data = "fallback" }, .{ .sequence = "\x1B]52;c;YQBi\x1B\\", .location = .standard, .data = "a\x00b" }, + // Missing padding is tolerated for OSC 52 since it has no way + // to report errors to the client, matching kitty. + .{ .sequence = "\x1B]52;c;dW5wYWRkZWQ\x1B\\", .location = .standard, .data = "unpadded" }, + }; + + for (cases, 1..) |case, expected_count| { + s.nextSlice(case.sequence); + try testing.expectEqual(expected_count, S.count); + try testing.expectEqual(case.location, S.last_location); + try testing.expectEqual(@as(usize, 1), S.last_contents_len); + try testing.expectEqualStrings("text/plain", S.last_mime.?); + try testing.expectEqualSlices(u8, case.data, S.last_data.?); + } + + // Empty data is a clear, represented by an empty contents slice. + s.nextSlice("\x1B]52;s;\x1B\\"); + try testing.expectEqual(@as(usize, cases.len + 1), S.count); + try testing.expectEqual(clipboard.Location.selection, S.last_location); + try testing.expectEqual(@as(usize, 0), S.last_contents_len); + try testing.expect(S.last_mime == null); + try testing.expect(S.last_data == null); + + // Reads and malformed base64 are ignored. The whole request is + // discarded on invalid characters (including whitespace) rather + // than decoding around them, per the Kitty clipboard spec that + // governs OSC 52 base64 handling. + s.nextSlice("\x1B]52;c;?\x1B\\"); + s.nextSlice("\x1B]52;c;***\x1B\\"); + s.nextSlice("\x1B]52;c;SGVs!!!bG8=\x1B\\"); + s.nextSlice("\x1B]52;c;aGVs bG8=\x1B\\"); + try testing.expectEqual(@as(usize, cases.len + 1), S.count); + + // OSC 1337 Copy shares the normalized clipboard write path. + s.nextSlice("\x1B]1337;Copy=:aVRlcm0y\x1B\\"); + try testing.expectEqual(@as(usize, cases.len + 2), S.count); + try testing.expectEqual(clipboard.Location.standard, S.last_location); + try testing.expectEqualStrings("text/plain", S.last_mime.?); + try testing.expectEqualStrings("iTerm2", S.last_data.?); + + // Parsing across write boundaries still invokes exactly one atomic write. + s.nextSlice("\x1B]52;p;ZnJh"); + s.nextSlice("Z21lbnRlZA==\x1B"); + s.nextSlice("\\"); + try testing.expectEqual(@as(usize, cases.len + 3), S.count); + try testing.expectEqual(clipboard.Location.primary, S.last_location); + try testing.expectEqualStrings("text/plain", S.last_mime.?); + try testing.expectEqualStrings("fragmented", S.last_data.?); + + // Reply results are intentionally ignored for protocols without a + // write acknowledgement. The denied reply above did not stop later writes. + try testing.expect(S.result == .denied); +} + +test "clipboard_read effect callback" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = struct { + var written: std.ArrayList(u8) = .empty; + var count: usize = 0; + var last_location: clipboard.Location = .standard; + var last_mimes: []const []const u8 = &.{}; + var last_list: bool = true; + var last_name: []const u8 = "unset"; + var last_granted: bool = true; + var last_can_remember: bool = true; + var result: ?clipboard.Read.Result = .{ .success = .{ .contents = &.{.{ + .mime = "text/plain", + .data = "hello", + }} } }; + var reply_twice: bool = false; + + fn writePty(_: *Handler, data: []const u8) void { + written.appendSlice(testing.allocator, data) catch @panic("OOM"); + } + + fn clipboardRead(_: *Handler, read: clipboard.Read) void { + count += 1; + last_location = read.location; + last_mimes = read.mimes; + last_list = read.list; + last_name = read.name; + last_granted = read.granted; + last_can_remember = read.can_remember; + if (result) |r| read.reply(r); + if (reply_twice) read.reply(.{ .success = .{ .contents = &.{.{ + .mime = "text/plain", + .data = "again", + }} } }); + } + }; + defer S.written.deinit(testing.allocator); + + // A null callback (the default readonly effects) silently ignores reads. + { + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + s.nextSlice("\x1B]52;c;?\x1B\\"); + try testing.expectEqual(0, S.written.items.len); + } + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Success echoes the normalized selector and request terminator. + s.nextSlice("\x1B]52;c;?\x1B\\"); + try testing.expectEqual(1, S.count); + try testing.expectEqual(clipboard.Location.standard, S.last_location); + try testing.expectEqual(1, S.last_mimes.len); + try testing.expectEqualStrings("text/plain", S.last_mimes[0]); + try testing.expect(!S.last_list); + try testing.expectEqualStrings("", S.last_name); + try testing.expect(!S.last_granted); + try testing.expect(!S.last_can_remember); + try testing.expectEqualStrings("\x1B]52;c;aGVsbG8=\x1B\\", S.written.items); + + S.written.clearRetainingCapacity(); + s.nextSlice("\x1B]52;p;?\x07"); + try testing.expectEqual(clipboard.Location.primary, S.last_location); + try testing.expectEqualStrings("\x1B]52;p;aGVsbG8=\x07", S.written.items); + + // Only the first text representation is used. + S.written.clearRetainingCapacity(); + S.result = .{ + .success = .{ + .contents = &.{ + .{ .mime = "image/png", .data = "\x89PNG" }, + .{ .mime = "UTF8_STRING", .data = "hi" }, + }, + // OSC 52 has no session passwords, so remember is ignored. + .remember = true, + }, + }; + s.nextSlice("\x1B]52;s;?\x1B\\"); + try testing.expectEqual(clipboard.Location.selection, S.last_location); + try testing.expectEqualStrings("\x1B]52;s;aGk=\x1B\\", S.written.items); + + // Every failure, no text, and no reply all answer with an empty + // clipboard. + for ([_]?clipboard.Read.Result{ + .denied, + .unsupported, + .busy, + .io_error, + .{ .success = .{} }, + null, + }) |result| { + S.written.clearRetainingCapacity(); + S.result = result; + s.nextSlice("\x1B]52;c;?\x1B\\"); + try testing.expectEqualStrings("\x1B]52;c;\x1B\\", S.written.items); + } + + // A second reply is ignored. + S.written.clearRetainingCapacity(); + S.result = .{ .success = .{ .contents = &.{.{ .mime = "text/plain", .data = "hello" }} } }; + S.reply_twice = true; + s.nextSlice("\x1B]52;c;?\x1B\\"); + try testing.expectEqualStrings("\x1B]52;c;aGVsbG8=\x1B\\", S.written.items); +} + +test "clipboard_write allocation failure is ignored" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = struct { + var count: usize = 0; + + fn clipboardWrite(_: *Handler, write: clipboard.Write) void { + count += 1; + write.reply(.{ .success = .{} }); + } + }; + S.count = 0; + + var handler: Handler = .init(&t); + handler.effects.clipboard_write = &S.clipboardWrite; + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Only the decoded scratch data uses the terminal allocator here. Swap in + // an allocator that always fails, then restore it before terminal teardown. + { + const alloc = t.screens.active.alloc; + t.screens.active.alloc = testing.failing_allocator; + defer t.screens.active.alloc = alloc; + s.nextSlice("\x1B]52;c;aGVsbG8=\x1B\\"); + } + try testing.expectEqual(@as(usize, 0), S.count); + try testing.expect(!s.handler.semantic_failure); +} + +/// Shared capture state for the Kitty clipboard (OSC 5522) tests below: +/// records every pty response and the most recent clipboard write. +const KittyClipboardCapture = struct { + var responses: [1024]u8 = undefined; + var responses_len: usize = 0; + + // Write capture. A null write_result returns without replying. + var write_count: usize = 0; + var write_result: ?clipboard.Write.Result = .{ .success = .{} }; + var write_reply_twice: bool = false; + var last_location: clipboard.Location = .standard; + var last_contents_len: usize = 0; + var last_mimes: [8][64]u8 = undefined; + var last_mime_lens: [8]usize = @splat(0); + var last_data: [8][256]u8 = undefined; + var last_data_lens: [8]usize = @splat(0); + var last_write_name: [64]u8 = undefined; + var last_write_name_len: usize = 0; + var last_write_granted: bool = false; + var last_write_can_remember: bool = false; + + // Read capture. A null read_result returns without replying. + var read_count: usize = 0; + var read_result: ?clipboard.Read.Result = null; + var read_reply_twice: bool = false; + var last_read_location: clipboard.Location = .standard; + var last_read_mimes: [8][64]u8 = undefined; + var last_read_mime_lens: [8]usize = @splat(0); + var last_read_mimes_len: usize = 0; + var last_read_list: bool = false; + var last_read_name: [64]u8 = undefined; + var last_read_name_len: usize = 0; + var last_read_granted: bool = false; + var last_read_can_remember: bool = false; + + fn reset() void { + responses_len = 0; + write_count = 0; + write_result = .{ .success = .{} }; + write_reply_twice = false; + last_location = .standard; + last_contents_len = 0; + last_mime_lens = @splat(0); + last_data_lens = @splat(0); + last_write_name_len = 0; + last_write_granted = false; + last_write_can_remember = false; + read_count = 0; + read_result = null; + read_reply_twice = false; + last_read_location = .standard; + last_read_mime_lens = @splat(0); + last_read_mimes_len = 0; + last_read_list = false; + last_read_name_len = 0; + last_read_granted = false; + last_read_can_remember = false; + } + + fn writePty(_: *Handler, data: []const u8) void { + @memcpy(responses[responses_len..][0..data.len], data); + responses_len += data.len; + } + + fn clipboardWrite(_: *Handler, write: clipboard.Write) void { + write_count += 1; + last_location = write.location; + last_contents_len = write.contents.len; + for (write.contents[0..@min(write.contents.len, last_mimes.len)], 0..) |content, i| { + last_mime_lens[i] = content.mime.len; + @memcpy(last_mimes[i][0..content.mime.len], content.mime); + last_data_lens[i] = content.data.len; + @memcpy(last_data[i][0..content.data.len], content.data); + } + last_write_name_len = write.name.len; + @memcpy(last_write_name[0..write.name.len], write.name); + last_write_granted = write.granted; + last_write_can_remember = write.can_remember; + if (write_result) |r| write.reply(r); + if (write_reply_twice) write.reply(.io_error); + } + + fn clipboardRead(_: *Handler, read: clipboard.Read) void { + read_count += 1; + last_read_location = read.location; + last_read_mimes_len = read.mimes.len; + for (read.mimes[0..@min(read.mimes.len, last_read_mimes.len)], 0..) |mime, i| { + last_read_mime_lens[i] = mime.len; + @memcpy(last_read_mimes[i][0..mime.len], mime); + } + last_read_list = read.list; + last_read_name_len = read.name.len; + @memcpy(last_read_name[0..read.name.len], read.name); + last_read_granted = read.granted; + last_read_can_remember = read.can_remember; + if (read_result) |r| read.reply(r); + if (read_reply_twice) read.reply(.denied); + } + + fn responseSlice() []const u8 { + return responses[0..responses_len]; + } + + fn readMimeAt(i: usize) []const u8 { + return last_read_mimes[i][0..last_read_mime_lens[i]]; + } + + fn readName() []const u8 { + return last_read_name[0..last_read_name_len]; + } + + fn writeName() []const u8 { + return last_write_name[0..last_write_name_len]; + } + + fn mimeAt(i: usize) []const u8 { + return last_mimes[i][0..last_mime_lens[i]]; + } + + fn dataAt(i: usize) []const u8 { + return last_data[i][0..last_data_lens[i]]; + } +}; + +test "kitty clipboard write transaction round trip" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Begin a write, stream two MIME types (one chunked), alias the + // plain text, and commit. Only the commit produces a response. + s.nextSlice("\x1B]5522;type=write:id=42\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); // "Ghost" + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;dHk=\x1B\\"); // "ty" + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9odG1s;PGI+aGk8L2I+\x1B\\"); // "hi" + // Alias "TEXT UTF8_STRING" -> text/plain. + s.nextSlice("\x1B]5522;type=walias:mime=dGV4dC9wbGFpbg==;VEVYVCBVVEY4X1NUUklORw==\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqual(@as(usize, 0), S.responses_len); + + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqual(clipboard.Location.standard, S.last_location); + try testing.expectEqual(@as(usize, 4), S.last_contents_len); + try testing.expectEqualStrings("text/plain", S.mimeAt(0)); + try testing.expectEqualStrings("Ghostty", S.dataAt(0)); + try testing.expectEqualStrings("text/html", S.mimeAt(1)); + try testing.expectEqualStrings("hi", S.dataAt(1)); + try testing.expectEqualStrings("TEXT", S.mimeAt(2)); + try testing.expectEqualStrings("Ghostty", S.dataAt(2)); + try testing.expectEqualStrings("UTF8_STRING", S.mimeAt(3)); + try testing.expectEqualStrings("Ghostty", S.dataAt(3)); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=DONE:id=42\x1B\\", + S.responseSlice(), + ); + + // A commit with no transaction in flight is silently ignored. + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.write_count); +} + +test "kitty clipboard write result maps to response status" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + const cases = [_]struct { + result: ?clipboard.Write.Result, + response: []const u8, + }{ + .{ .result = .{ .success = .{} }, .response = "\x1B]5522;type=write:status=DONE\x1B\\" }, + .{ .result = .denied, .response = "\x1B]5522;type=write:status=EPERM\x1B\\" }, + .{ .result = .unsupported, .response = "\x1B]5522;type=write:status=ENOSYS\x1B\\" }, + .{ .result = .busy, .response = "\x1B]5522;type=write:status=EBUSY\x1B\\" }, + .{ .result = .invalid_data, .response = "\x1B]5522;type=write:status=EINVAL\x1B\\" }, + .{ .result = .io_error, .response = "\x1B]5522;type=write:status=EIO\x1B\\" }, + // No reply at all is a denial rather than silence. + .{ .result = null, .response = "\x1B]5522;type=write:status=EPERM\x1B\\" }, + }; + + for (cases) |case| { + S.reset(); + S.write_result = case.result; + + // An immediately-committed write with no data is a clear. + s.nextSlice("\x1B]5522;type=write\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqual(@as(usize, 0), S.last_contents_len); + try testing.expectEqualStrings(case.response, S.responseSlice()); + } + + // A second reply is ignored. + S.reset(); + S.write_reply_twice = true; + s.nextSlice("\x1B]5522;type=write\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=DONE\x1B\\", + S.responseSlice(), + ); + + // The response echoes the request terminator, unlike kitty which + // always uses ST. + S.reset(); + s.nextSlice("\x1B]5522;type=write:loc=primary\x07"); + s.nextSlice("\x1B]5522;type=wdata\x07"); + try testing.expectEqual(clipboard.Location.primary, S.last_location); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=DONE\x07", + S.responseSlice(), + ); + + // A system without a primary selection answers a loc=primary write + // with ENOSYS, echoing the id. + S.reset(); + S.write_result = .unsupported; + s.nextSlice("\x1B]5522;type=write:loc=primary:id=p1\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(clipboard.Location.primary, S.last_location); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=ENOSYS:id=p1\x1B\\", + S.responseSlice(), + ); +} + +test "kitty clipboard write without clipboard effect responds ENOSYS" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // The transaction fails as soon as it begins; the rest of it is + // ignored without further responses. + s.nextSlice("\x1B]5522;type=write:id=x\x1B\\"); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=ENOSYS:id=x\x1B\\", + S.responseSlice(), + ); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=ENOSYS:id=x\x1B\\", + S.responseSlice(), + ); +} + +test "kitty clipboard read without effect is denied with EPERM" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // The denial never includes loc (only OK responses do) and echoes + // the sanitized id. + s.nextSlice("\x1B]5522;type=read:loc=primary:id=*4 2*;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expectEqualStrings( + "\x1B]5522;type=read:status=EPERM:id=42\x1B\\", + S.responseSlice(), + ); + + // A missing payload is an empty MIME list, still answered. + S.reset(); + s.nextSlice("\x1B]5522;type=read\x07"); + try testing.expectEqualStrings( + "\x1B]5522;type=read:status=EPERM\x07", + S.responseSlice(), + ); + + // An undecodable payload is dropped without a response. + S.reset(); + s.nextSlice("\x1B]5522;type=read;!!!\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.responses_len); +} + +test "kitty clipboard read round trip" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + S.read_result = .{ + .success = .{ + .contents = &.{ + // Unrequested representations are never served, and the + // served ones follow request order, not reply order. + .{ .mime = "image/png", .data = "\x89PNG" }, + .{ .mime = "text/html", .data = "hi" }, + .{ .mime = "text/plain", .data = "Ghostty" }, + }, + .available = &.{ "text/plain", "text/html" }, + }, + }; + + // Request the targets listing plus two types from the primary + // selection: ". text/plain text/html". + s.nextSlice("\x1B]5522;type=read:loc=primary:id=r1;LiB0ZXh0L3BsYWluIHRleHQvaHRtbA==\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expectEqual(clipboard.Location.primary, S.last_read_location); + try testing.expectEqual(@as(usize, 2), S.last_read_mimes_len); + try testing.expectEqualStrings("text/plain", S.readMimeAt(0)); + try testing.expectEqualStrings("text/html", S.readMimeAt(1)); + try testing.expect(S.last_read_list); + try testing.expectEqualStrings("", S.readName()); + try testing.expect(!S.last_read_granted); + try testing.expect(!S.last_read_can_remember); + try testing.expectEqualStrings( + "\x1B]5522;type=read:status=OK:loc=primary:id=r1\x1B\\" ++ + "\x1B]5522;type=read:status=DATA:id=r1:mime=Lg==;dGV4dC9wbGFpbiB0ZXh0L2h0bWwK\x1B\\" ++ + "\x1B]5522;type=read:status=DATA:id=r1:mime=dGV4dC9wbGFpbg==;R2hvc3R0eQ==\x1B\\" ++ + "\x1B]5522;type=read:status=DATA:id=r1:mime=dGV4dC9odG1s;PGI+aGk8L2I+\x1B\\" ++ + "\x1B]5522;type=read:status=DONE:id=r1\x1B\\", + S.responseSlice(), + ); + + // Without the listing request `available` is ignored. The response + // echoes the request terminator. + S.responses_len = 0; + s.nextSlice("\x1B]5522;type=read:id=r2;dGV4dC9wbGFpbg==\x07"); + try testing.expectEqual(clipboard.Location.standard, S.last_read_location); + try testing.expectEqual(@as(usize, 1), S.last_read_mimes_len); + try testing.expect(!S.last_read_list); + try testing.expectEqualStrings( + "\x1B]5522;type=read:status=OK:id=r2\x07" ++ + "\x1B]5522;type=read:status=DATA:id=r2:mime=dGV4dC9wbGFpbg==;R2hvc3R0eQ==\x07" ++ + "\x1B]5522;type=read:status=DONE:id=r2\x07", + S.responseSlice(), + ); + + // A listing-only request carries no types. + S.responses_len = 0; + s.nextSlice("\x1B]5522;type=read;Lg==\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.last_read_mimes_len); + try testing.expect(S.last_read_list); + try testing.expectEqualStrings( + "\x1B]5522;type=read:status=OK\x1B\\" ++ + "\x1B]5522;type=read:status=DATA:mime=Lg==;dGV4dC9wbGFpbiB0ZXh0L2h0bWwK\x1B\\" ++ + "\x1B]5522;type=read:status=DONE\x1B\\", + S.responseSlice(), + ); +} + +test "kitty clipboard read result maps to response status" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + const cases = [_]struct { + result: ?clipboard.Read.Result, + response: []const u8, + }{ + .{ .result = .denied, .response = "\x1B]5522;type=read:status=EPERM:id=x\x1B\\" }, + .{ .result = .unsupported, .response = "\x1B]5522;type=read:status=ENOSYS:id=x\x1B\\" }, + .{ .result = .busy, .response = "\x1B]5522;type=read:status=EBUSY:id=x\x1B\\" }, + .{ .result = .io_error, .response = "\x1B]5522;type=read:status=EIO:id=x\x1B\\" }, + // No reply at all is a denial rather than silence. + .{ .result = null, .response = "\x1B]5522;type=read:status=EPERM:id=x\x1B\\" }, + // A success with nothing to serve is still OK then DONE. + .{ .result = .{ .success = .{} }, .response = "\x1B]5522;type=read:status=OK:id=x\x1B\\" ++ + "\x1B]5522;type=read:status=DONE:id=x\x1B\\" }, + }; + + for (cases) |case| { + S.reset(); + S.read_result = case.result; + s.nextSlice("\x1B]5522;type=read:id=x;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expectEqualStrings(case.response, S.responseSlice()); + } + + // A second reply is ignored. + S.reset(); + S.read_result = .{ .success = .{ .contents = &.{.{ .mime = "text/plain", .data = "hello" }} } }; + S.read_reply_twice = true; + s.nextSlice("\x1B]5522;type=read;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expectEqualStrings( + "\x1B]5522;type=read:status=OK\x1B\\" ++ + "\x1B]5522;type=read:status=DATA:mime=dGV4dC9wbGFpbg==;aGVsbG8=\x1B\\" ++ + "\x1B]5522;type=read:status=DONE\x1B\\", + S.responseSlice(), + ); +} + +test "kitty clipboard read caps requested types" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // "a/0 a/1 a/2 a/3 a/4 a/5 .": extras are dropped but the listing + // request after them still counts. + S.read_result = .{ .success = .{} }; + s.nextSlice("\x1B]5522;type=read;YS8wIGEvMSBhLzIgYS8zIGEvNCBhLzUgLg==\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expectEqual(kitty_clipboard.max_read_mimes, S.last_read_mimes_len); + try testing.expectEqualStrings("a/0", S.readMimeAt(0)); + try testing.expectEqualStrings("a/3", S.readMimeAt(kitty_clipboard.max_read_mimes - 1)); + try testing.expect(S.last_read_list); +} + +test "kitty clipboard read password grants" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Every read requests a data type ("text/plain"): a request with + // no data types never consults the grants at all. + // + // pw="secret", name="app": the first request isn't granted but the + // reply may ask to remember it. + S.read_result = .{ .success = .{ .remember = true } }; + s.nextSlice("\x1B]5522;type=read:pw=c2VjcmV0:name=YXBw;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expectEqualStrings("app", S.readName()); + try testing.expect(!S.last_read_granted); + try testing.expect(S.last_read_can_remember); + + // The same password is now granted; a different one is not. + S.read_result = .{ .success = .{} }; + s.nextSlice("\x1B]5522;type=read:pw=c2VjcmV0:name=YXBw;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expect(S.last_read_granted); + s.nextSlice("\x1B]5522;type=read:pw=b3RoZXI=:name=YXBw;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expect(!S.last_read_granted); + try testing.expect(S.last_read_can_remember); + + // A password without a name doesn't count: it is neither granted + // nor rememberable, even if the reply asks. + S.read_result = .{ .success = .{ .remember = true } }; + s.nextSlice("\x1B]5522;type=read:pw=c2VjcmV0;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expectEqualStrings("", S.readName()); + try testing.expect(!S.last_read_granted); + try testing.expect(!S.last_read_can_remember); + s.nextSlice("\x1B]5522;type=read:pw=b3RoZXI=;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expect(!S.last_read_can_remember); + S.read_result = .{ .success = .{} }; + s.nextSlice("\x1B]5522;type=read:pw=b3RoZXI=:name=YXBw;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expect(!S.last_read_granted); + + // A grant is advisory: the request is still forwarded and the + // embedder may deny it. + S.responses_len = 0; + S.read_result = .denied; + s.nextSlice("\x1B]5522;type=read:id=d:pw=c2VjcmV0:name=YXBw;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expect(S.last_read_granted); + try testing.expectEqualStrings( + "\x1B]5522;type=read:status=EPERM:id=d\x1B\\", + S.responseSlice(), + ); + + // Grants are freed with the stream (the testing allocator catches + // the leak otherwise). +} + +test "kitty clipboard read targets-only never consumes a one-time grant" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // A one-time read grant, as minted for a paste event. + try s.handler.kitty_clipboard_grants.grant(testing.allocator, "otp", .read, true); + + // A targets-only read (payload ".") is prompt-exempt so it never + // consults, and must not burn, the one-time password. + S.read_result = .{ .success = .{} }; + s.nextSlice("\x1B]5522;type=read:pw=b3Rw:name=YXBw;Lg==\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expect(S.last_read_list); + try testing.expectEqual(@as(usize, 0), S.last_read_mimes_len); + try testing.expect(!S.last_read_granted); + + // The follow-up data read still consumes the grant, exactly once. + s.nextSlice("\x1B]5522;type=read:pw=b3Rw:name=YXBw;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expect(S.last_read_granted); + s.nextSlice("\x1B]5522;type=read:pw=b3Rw:name=YXBw;dGV4dC9wbGFpbg==\x1B\\"); + try testing.expect(!S.last_read_granted); +} + +test "kitty clipboard write password grants" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // pw="secret", name="app": the first commit isn't granted but the + // reply may ask to remember it. + S.write_result = .{ .success = .{ .remember = true } }; + s.nextSlice("\x1B]5522;type=write:pw=c2VjcmV0:name=YXBw\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqualStrings("app", S.writeName()); + try testing.expect(!S.last_write_granted); + try testing.expect(S.last_write_can_remember); + + // The same password is now granted; a different one is not. + S.write_result = .{ .success = .{} }; + s.nextSlice("\x1B]5522;type=write:pw=c2VjcmV0:name=YXBw\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expect(S.last_write_granted); + s.nextSlice("\x1B]5522;type=write:pw=b3RoZXI=:name=YXBw\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expect(!S.last_write_granted); + try testing.expect(S.last_write_can_remember); + + // Directions are independent: a write grant doesn't satisfy reads. + S.read_result = .{ .success = .{} }; + s.nextSlice("\x1B]5522;type=read:pw=c2VjcmV0:name=YXBw\x1B\\"); + try testing.expect(!S.last_read_granted); + + // A password without a name doesn't count: it is neither granted + // nor rememberable, even if the reply asks. + S.write_result = .{ .success = .{ .remember = true } }; + s.nextSlice("\x1B]5522;type=write:pw=c2VjcmV0\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqualStrings("", S.writeName()); + try testing.expect(!S.last_write_granted); + try testing.expect(!S.last_write_can_remember); + + // A grant is advisory: the request is still forwarded and the + // embedder may deny it. + S.responses_len = 0; + S.write_result = .denied; + s.nextSlice("\x1B]5522;type=write:id=d:pw=c2VjcmV0:name=YXBw\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expect(S.last_write_granted); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EPERM:id=d\x1B\\", + S.responseSlice(), + ); + + // Grants are freed with the stream (the testing allocator catches + // the leak otherwise). +} + +test "kitty clipboard malformed packets are silently dropped" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Missing type, unknown type, bare metadata record, invalid mime + // base64, and orphaned transaction packets all drop silently. + s.nextSlice("\x1B]5522;loc=primary\x1B\\"); + s.nextSlice("\x1B]5522;type=bobr\x1B\\"); + s.nextSlice("\x1B]5522;type=read:bare\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=!!!;R2hvc3Q=\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); + s.nextSlice("\x1B]5522;type=walias:mime=dGV4dC9wbGFpbg==;VEVYVA==\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqual(@as(usize, 0), S.responses_len); + try testing.expect(!s.handler.semantic_failure); + + // The terminal is still functional afterwards. + s.nextSlice("ok"); + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("ok", str); +} + +test "kitty clipboard new write replaces in-flight transaction" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + s.nextSlice("\x1B]5522;type=write:id=old\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;b2xk\x1B\\"); // "old" + s.nextSlice("\x1B]5522;type=write:id=new\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;bmV3\x1B\\"); // "new" + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqual(@as(usize, 1), S.last_contents_len); + try testing.expectEqualStrings("new", S.dataAt(0)); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=DONE:id=new\x1B\\", + S.responseSlice(), + ); +} + +test "kitty clipboard invalid write packets abort with EINVAL" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + const invalid_packets = [_][]const u8{ + // Alias payload decodes to a non-UTF-8 byte. + "\x1B]5522;type=walias:mime=dGV4dC9wbGFpbg==;/w==\x1B\\", + // Alias has no target MIME type. + "\x1B]5522;type=walias;VEVYVA==\x1B\\", + // Alias target MIME decodes to non-UTF-8 bytes. + "\x1B]5522;type=walias:mime=//4=;VEVYVA==\x1B\\", + // Write data MIME decodes to non-UTF-8 bytes. + "\x1B]5522;type=wdata:mime=//4=;R2hvc3Q=\x1B\\", }; - for (cases, 1..) |case, expected_count| { - s.nextSlice(case.sequence); - try testing.expectEqual(expected_count, S.count); - try testing.expectEqual(case.location, S.last_location); - try testing.expectEqual(@as(usize, 1), S.last_contents_len); - try testing.expectEqualStrings("text/plain", S.last_mime.?); - try testing.expectEqualSlices(u8, case.data, S.last_data.?); + for (invalid_packets) |packet| { + S.responses_len = 0; + s.nextSlice("\x1B]5522;type=write:id=w\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); + s.nextSlice(packet); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EINVAL:id=w\x1B\\", + S.responseSlice(), + ); + try testing.expect(!s.handler.semantic_failure); + + // The transaction is gone: a commit does nothing further. + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EINVAL:id=w\x1B\\", + S.responseSlice(), + ); } +} - // Empty data is a clear, represented by an empty contents slice. - s.nextSlice("\x1B]52;s;\x1B\\"); - try testing.expectEqual(@as(usize, cases.len + 1), S.count); - try testing.expectEqual(clipboard.Location.selection, S.last_location); - try testing.expectEqual(@as(usize, 0), S.last_contents_len); - try testing.expect(S.last_mime == null); - try testing.expect(S.last_data == null); +test "kitty clipboard invalid read text does not abort a write" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); - // Reads and malformed base64 are ignored. - s.nextSlice("\x1B]52;c;?\x1B\\"); - s.nextSlice("\x1B]52;c;***\x1B\\"); - try testing.expectEqual(@as(usize, cases.len + 1), S.count); + const S = KittyClipboardCapture; + S.reset(); + S.read_result = .{ .success = .{} }; - // OSC 1337 Copy shares the normalized clipboard write path. - s.nextSlice("\x1B]1337;Copy=:aVRlcm0y\x1B\\"); - try testing.expectEqual(@as(usize, cases.len + 2), S.count); - try testing.expectEqual(clipboard.Location.standard, S.last_location); - try testing.expectEqualStrings("text/plain", S.last_mime.?); - try testing.expectEqualStrings("iTerm2", S.last_data.?); + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); - // Parsing across write boundaries still invokes exactly one atomic write. - s.nextSlice("\x1B]52;p;ZnJh"); - s.nextSlice("Z21lbnRlZA==\x1B"); - s.nextSlice("\\"); - try testing.expectEqual(@as(usize, cases.len + 3), S.count); - try testing.expectEqual(clipboard.Location.primary, S.last_location); - try testing.expectEqualStrings("text/plain", S.last_mime.?); - try testing.expectEqualStrings("fragmented", S.last_data.?); + s.nextSlice("\x1B]5522;type=write:id=w\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); + + // The read payload decodes to a non-UTF-8 byte. It is dropped without + // invoking the clipboard effect or disturbing the write transaction. + s.nextSlice("\x1B]5522;type=read;/w==\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.read_count); + try testing.expectEqual(@as(usize, 0), S.responses_len); - // Callback results are intentionally ignored for protocols without a - // write acknowledgement. The denied result above did not stop later writes. - try testing.expectEqual(clipboard.WriteResult.denied, S.result); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=DONE:id=w\x1B\\", + S.responseSlice(), + ); } -test "clipboard_write allocation failure is ignored" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); +test "kitty clipboard oversized text write aborts with EFBIG" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - const S = struct { - var count: usize = 0; + const S = KittyClipboardCapture; + S.reset(); - fn clipboardWrite(_: *Handler, _: clipboard.Write) clipboard.WriteResult { - count += 1; - return .success; - } - }; - S.count = 0; + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Shrink the limit so the test doesn't have to stream the + // default 64MiB. + s.handler.kitty_clipboard_write_max_bytes = 4; + + s.nextSlice("\x1B]5522;type=write:id=w\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;SGVsbA==\x1B\\"); // "Hell" + try testing.expectEqual(@as(usize, 0), S.responses_len); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;bw==\x1B\\"); // "o" + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EFBIG:id=w\x1B\\", + S.responseSlice(), + ); + try testing.expect(!s.handler.semantic_failure); + + // The transaction is gone: later data and the commit do nothing. + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;IQ==\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EFBIG:id=w\x1B\\", + S.responseSlice(), + ); +} + +test "kitty clipboard invalid wdata chunk aborts with EINVAL" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + s.nextSlice("\x1B]5522;type=write:id=w\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;SGVsbG8=\x1B\\"); // "Hello" + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;!!!bad!!!\x1B\\"); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EINVAL:id=w\x1B\\", + S.responseSlice(), + ); + try testing.expect(!s.handler.semantic_failure); + + // The transaction is gone: later data and the commit do nothing. + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;V29ybGQ=\x1B\\"); // "World" + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EINVAL:id=w\x1B\\", + S.responseSlice(), + ); +} + +test "kitty clipboard wdata chunks split one base64 stream" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); - var s: Stream = .initAlloc(testing.allocator, handler); + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); - // Only the decoded scratch data uses the terminal allocator here. Swap in - // an allocator that always fails, then restore it before terminal teardown. + // "some data" encoded as one stream, split at non-group + // boundaries across packets. + s.nextSlice("\x1B]5522;type=write\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;c29\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;tZSBk\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;YXRh\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqualStrings("some data", S.dataAt(0)); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=DONE\x1B\\", + S.responseSlice(), + ); +} + +test "kitty clipboard unpadded wdata stream aborts at commit" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // "Hello" without its final padding byte: every packet decodes, + // but the stream ends mid-group so the commit reports EINVAL. + s.nextSlice("\x1B]5522;type=write:id=w\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;SGVsbG8\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.responses_len); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqualStrings( + "\x1B]5522;type=write:status=EINVAL:id=w\x1B\\", + S.responseSlice(), + ); + try testing.expect(!s.handler.semantic_failure); +} + +test "kitty clipboard in-flight transaction is freed on deinit" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Never committed: stream deinit must free the transaction (the + // testing allocator catches the leak otherwise). + s.nextSlice("\x1B]5522;type=write\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.write_count); +} + +test "kitty clipboard allocation failure is ignored" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Only transaction state uses the terminal allocator here. Swap in + // an allocator that always fails, then restore it before teardown. { const alloc = t.screens.active.alloc; t.screens.active.alloc = testing.failing_allocator; defer t.screens.active.alloc = alloc; - s.nextSlice("\x1B]52;c;aGVsbG8=\x1B\\"); + s.nextSlice("\x1B]5522;type=write\x1B\\"); } - try testing.expectEqual(@as(usize, 0), S.count); + + // Clipboard writes are external effects, best-effort like OSC 52; + // the failed transaction never started and is not a semantic + // failure. + try testing.expect(!s.handler.semantic_failure); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqual(@as(usize, 0), S.responses_len); +} + +test "kitty clipboard without write_pty still commits writes" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = KittyClipboardCapture; + S.reset(); + + var handler: Handler = .init(&t); + handler.effects.clipboard_write = &S.clipboardWrite; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + s.nextSlice("\x1B]5522;type=write\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata:mime=dGV4dC9wbGFpbg==;R2hvc3Q=\x1B\\"); + s.nextSlice("\x1B]5522;type=wdata\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqualStrings("Ghost", S.dataAt(0)); + + // Reads are dropped without a way to respond. + s.nextSlice("\x1B]5522;type=read\x1B\\"); + try testing.expectEqual(@as(usize, 0), S.responses_len); } test "request mode DECRQM with write_pty callback" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); // Without callback, DECRQM should not crash { - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // DECRQM for mode 7 (wraparound) — should be silently ignored @@ -1817,7 +4582,7 @@ test "request mode DECRQM with write_pty callback" { { const S = struct { var last_response: ?[:0]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (last_response) |old| testing.allocator.free(old); last_response = testing.allocator.dupeZ(u8, data) catch @panic("OOM"); } @@ -1828,7 +4593,7 @@ test "request mode DECRQM with write_pty callback" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Wraparound mode (7) is set by default @@ -1843,27 +4608,31 @@ test "request mode DECRQM with write_pty callback" { // Query an unknown mode s.nextSlice("\x1B[?9999$p"); try testing.expectEqualStrings("\x1B[?9999;0$y", S.last_response.?); + + // Query DECECM, which Ghostty recognizes but does not allow changing + s.nextSlice("\x1B[?117$p"); + try testing.expectEqualStrings("\x1B[?117;4$y", S.last_response.?); } } test "stream: CSI W with intermediate but no params" { // Regression test from AFL++ crash. CSI ? W without // parameters caused an out-of-bounds access on input.params[0]. - var t: Terminal = try .init(testing.allocator, .{ + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24, - .max_scrollback = 100, + .max_scrollback_bytes = 100, }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); s.nextSlice("\x1b[?W"); } test "window_title effect is called" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { @@ -1877,7 +4646,7 @@ test "window_title effect is called" { var handler: Handler = .init(&t); handler.effects.title_changed = &S.titleChanged; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Set window title via OSC 2 @@ -1887,10 +4656,10 @@ test "window_title effect is called" { } test "window_title effect not called without callback" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // Should not crash when no callback is set @@ -1907,7 +4676,7 @@ test "window_title effect not called without callback" { } test "window_title effect with empty title" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { @@ -1921,7 +4690,7 @@ test "window_title effect with empty title" { var handler: Handler = .init(&t); handler.effects.title_changed = &S.titleChanged; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Set empty window title @@ -1931,13 +4700,16 @@ test "window_title effect with empty title" { } test "kitty_keyboard_query" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { - var written: ?[:0]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { - written = data; + var written: ?[]const u8 = null; + var written_buf: [64]u8 = undefined; + fn writePty(_: *Handler, data: []const u8) void { + std.debug.assert(data.len <= written_buf.len); + @memcpy(written_buf[0..data.len], data); + written = written_buf[0..data.len]; } }; S.written = null; @@ -1945,7 +4717,7 @@ test "kitty_keyboard_query" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Default kitty keyboard flags should be 0 @@ -1960,13 +4732,16 @@ test "kitty_keyboard_query" { } test "xtversion default" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { - var written: ?[:0]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { - written = data; + var written: ?[]const u8 = null; + var written_buf: [64]u8 = undefined; + fn writePty(_: *Handler, data: []const u8) void { + std.debug.assert(data.len <= written_buf.len); + @memcpy(written_buf[0..data.len], data); + written = written_buf[0..data.len]; } }; S.written = null; @@ -1974,7 +4749,7 @@ test "xtversion default" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Without xtversion effect set, should report "libghostty" @@ -1983,13 +4758,16 @@ test "xtversion default" { } test "xtversion with effect" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { - var written: ?[:0]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { - written = data; + var written: ?[]const u8 = null; + var written_buf: [64]u8 = undefined; + fn writePty(_: *Handler, data: []const u8) void { + std.debug.assert(data.len <= written_buf.len); + @memcpy(written_buf[0..data.len], data); + written = written_buf[0..data.len]; } fn xtversion(_: *Handler) []const u8 { return "ghostty 1.2.3"; @@ -2001,7 +4779,7 @@ test "xtversion with effect" { handler.effects.write_pty = &S.writePty; handler.effects.xtversion = &S.xtversion; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x1b[>0q"); @@ -2009,13 +4787,16 @@ test "xtversion with effect" { } test "xtversion with empty string effect" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { - var written: ?[:0]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { - written = data; + var written: ?[]const u8 = null; + var written_buf: [64]u8 = undefined; + fn writePty(_: *Handler, data: []const u8) void { + std.debug.assert(data.len <= written_buf.len); + @memcpy(written_buf[0..data.len], data); + written = written_buf[0..data.len]; } fn xtversion(_: *Handler) []const u8 { return ""; @@ -2027,7 +4808,7 @@ test "xtversion with empty string effect" { handler.effects.write_pty = &S.writePty; handler.effects.xtversion = &S.xtversion; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Empty string from effect should fall back to "libghostty" @@ -2036,12 +4817,12 @@ test "xtversion with empty string effect" { } test "size report csi_14_t with effect" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } fn getSize(_: *Handler) ?size_report.Size { @@ -2054,7 +4835,7 @@ test "size report csi_14_t with effect" { handler.effects.write_pty = &S.writePty; handler.effects.size = &S.getSize; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // CSI 14 t - report text area size in pixels @@ -2063,13 +4844,104 @@ test "size report csi_14_t with effect" { try testing.expectEqualStrings("\x1b[4;432;720t", S.written.?); } +test "mode 2048 enable reports current geometry and disable is silent" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = struct { + var response: [128]u8 = undefined; + var response_len: usize = 0; + var calls: usize = 0; + + fn writePty(_: *Handler, data: []const u8) void { + @memcpy(response[0..data.len], data); + response_len = data.len; + calls += 1; + } + + fn getSize(_: *Handler) ?size_report.Size { + return .{ .rows = 24, .columns = 80, .cell_width = 8, .cell_height = 16 }; + } + }; + S.response_len = 0; + S.calls = 0; + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.size = &S.getSize; + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + s.nextSlice("\x1b[?2048h"); + s.nextSlice("\x1b[?2048h"); + + try testing.expectEqual(@as(usize, 2), S.calls); + try testing.expectEqualStrings("\x1b[48;24;80;384;640t", S.response[0..S.response_len]); + + s.nextSlice("\x1b[?2048l"); + try testing.expectEqual(@as(usize, 2), S.calls); + try testing.expect(!t.modes.get(.in_band_size_reports)); +} + +test "mode 2048 enable tolerates missing effects" { + const S = struct { + var calls: usize = 0; + + fn writePty(_: *Handler, _: []const u8) void { + calls += 1; + } + + fn getSize(_: *Handler) ?size_report.Size { + return .{ .rows = 24, .columns = 80, .cell_width = 8, .cell_height = 16 }; + } + }; + S.calls = 0; + + var no_size_terminal: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer no_size_terminal.deinit(testing.allocator); + var no_size_handler: Handler = .init(&no_size_terminal); + no_size_handler.effects.write_pty = &S.writePty; + var no_size_stream: Stream = .init(.{ + .allocator = testing.allocator, + .handler = no_size_handler, + }); + defer no_size_stream.deinit(); + + no_size_stream.nextSlice("\x1b[?2048h"); + try testing.expect(no_size_terminal.modes.get(.in_band_size_reports)); + try testing.expectEqual(@as(usize, 0), S.calls); + + var no_write_terminal: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer no_write_terminal.deinit(testing.allocator); + var no_write_handler: Handler = .init(&no_write_terminal); + no_write_handler.effects.size = &S.getSize; + var no_write_stream: Stream = .init(.{ + .allocator = testing.allocator, + .handler = no_write_handler, + }); + defer no_write_stream.deinit(); + + no_write_stream.nextSlice("\x1b[?2048h"); + try testing.expect(no_write_terminal.modes.get(.in_band_size_reports)); + try testing.expectEqual(@as(usize, 0), S.calls); +} + test "size report csi_16_t with effect" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } fn getSize(_: *Handler) ?size_report.Size { @@ -2082,7 +4954,7 @@ test "size report csi_16_t with effect" { handler.effects.write_pty = &S.writePty; handler.effects.size = &S.getSize; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // CSI 16 t - report cell size in pixels @@ -2092,12 +4964,12 @@ test "size report csi_16_t with effect" { } test "size report csi_18_t with effect" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } fn getSize(_: *Handler) ?size_report.Size { @@ -2110,7 +4982,7 @@ test "size report csi_18_t with effect" { handler.effects.write_pty = &S.writePty; handler.effects.size = &S.getSize; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // CSI 18 t - report text area size in characters @@ -2120,12 +4992,12 @@ test "size report csi_18_t with effect" { } test "size report no effect callback" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } }; @@ -2134,7 +5006,7 @@ test "size report no effect callback" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Without size effect, size reports should be silently ignored @@ -2142,13 +5014,39 @@ test "size report no effect callback" { try testing.expect(S.written == null); } -test "size report csi_21_t title" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); +test "size report csi_21_t title disabled by default" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = struct { + var written: ?[]const u8 = null; + fn writePty(_: *Handler, data: []const u8) void { + written = testing.allocator.dupe(u8, data) catch @panic("OOM"); + } + }; + S.written = null; + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Set a title first + s.nextSlice("\x1b]2;My Title\x1b\\"); + + // CSI 21 t - report title (no size effect needed) + s.nextSlice("\x1b[21t"); + try testing.expect(S.written == null); +} + +test "size report csi_21_t title enabled" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } }; @@ -2156,8 +5054,9 @@ test "size report csi_21_t title" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; + handler.title_report = true; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Set a title first @@ -2170,12 +5069,12 @@ test "size report csi_21_t title" { } test "enquiry no effect" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } }; @@ -2184,7 +5083,7 @@ test "enquiry no effect" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // ENQ without enquiry effect should not write anything @@ -2193,12 +5092,12 @@ test "enquiry no effect" { } test "enquiry with effect" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } fn enquiry(_: *Handler) []const u8 { @@ -2211,7 +5110,7 @@ test "enquiry with effect" { handler.effects.write_pty = &S.writePty; handler.effects.enquiry = &S.enquiry; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x05"); @@ -2220,12 +5119,12 @@ test "enquiry with effect" { } test "enquiry with empty response" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } fn enquiry(_: *Handler) []const u8 { @@ -2238,7 +5137,7 @@ test "enquiry with empty response" { handler.effects.write_pty = &S.writePty; handler.effects.enquiry = &S.enquiry; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Empty enquiry response should not write anything @@ -2247,12 +5146,12 @@ test "enquiry with empty response" { } test "device status: operating status" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2263,7 +5162,7 @@ test "device status: operating status" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // CSI 5 n — operating status report @@ -2272,12 +5171,12 @@ test "device status: operating status" { } test "device status: cursor position" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2288,7 +5187,7 @@ test "device status: cursor position" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Default position is 0,0 — reported as 1,1 @@ -2302,12 +5201,12 @@ test "device status: cursor position" { } test "device status: cursor position with origin mode" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2318,7 +5217,7 @@ test "device status: cursor position with origin mode" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Set scroll region rows 5-20 @@ -2334,12 +5233,12 @@ test "device status: cursor position with origin mode" { } test "device status: color scheme dark" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2354,7 +5253,7 @@ test "device status: color scheme dark" { handler.effects.write_pty = &S.writePty; handler.effects.color_scheme = &S.colorScheme; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // CSI ? 996 n — color scheme query @@ -2363,12 +5262,12 @@ test "device status: color scheme dark" { } test "device status: color scheme light" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2383,7 +5282,7 @@ test "device status: color scheme light" { handler.effects.write_pty = &S.writePty; handler.effects.color_scheme = &S.colorScheme; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // CSI ? 996 n — color scheme query @@ -2392,12 +5291,12 @@ test "device status: color scheme light" { } test "device status: color scheme without callback" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2408,7 +5307,7 @@ test "device status: color scheme without callback" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Without color_scheme effect, query should be silently ignored @@ -2416,17 +5315,69 @@ test "device status: color scheme without callback" { try testing.expect(S.written == null); } +test "visibility reports" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = struct { + var written: ?[]const u8 = null; + var count: usize = 0; + + fn writePty(_: *Handler, data: []const u8) void { + if (written) |old| testing.allocator.free(old); + written = testing.allocator.dupe(u8, data) catch @panic("OOM"); + count += 1; + } + }; + S.written = null; + S.count = 0; + defer if (S.written) |old| testing.allocator.free(old); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Mode 2033 is supported and initially disabled. + s.nextSlice("\x1B[?2033$p"); + try testing.expectEqualStrings("\x1B[?2033;2$y", S.written.?); + + // A one-shot query reports the current state without enabling the mode. + s.nextSlice("\x1B[?998n"); + try testing.expectEqualStrings("\x1B[?999;1n", S.written.?); + try testing.expect(!t.modes.get(.report_visibility)); + + // Enabling always sends an immediate report, even when already enabled. + t.flags.visible = false; + s.nextSlice("\x1B[?2033h"); + try testing.expectEqualStrings("\x1B[?999;2n", S.written.?); + const count = S.count; + s.nextSlice("\x1B[?2033h"); + try testing.expectEqual(count + 1, S.count); + + // Disabling sends no report. + s.nextSlice("\x1B[?2033l"); + try testing.expectEqual(count + 1, S.count); + + // A terminal reset preserves the view's externally owned visibility. + s.nextSlice("\x1Bc"); + s.nextSlice("\x1B[?998n"); + try testing.expectEqualStrings("\x1B[?999;2n", S.written.?); +} + test "device status: readonly ignores all" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // All device status queries should be silently ignored without effects s.nextSlice("\x1B[5n"); s.nextSlice("\x1B[6n"); s.nextSlice("\x1B[?996n"); + s.nextSlice("\x1B[?998n"); // Terminal should still be functional s.nextSlice("Test"); @@ -2436,12 +5387,12 @@ test "device status: readonly ignores all" { } test "device attributes: primary DA" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2456,7 +5407,7 @@ test "device attributes: primary DA" { handler.effects.write_pty = &S.writePty; handler.effects.device_attributes = &S.da; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x1B[c"); @@ -2464,12 +5415,12 @@ test "device attributes: primary DA" { } test "device attributes: secondary DA" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2484,7 +5435,7 @@ test "device attributes: secondary DA" { handler.effects.write_pty = &S.writePty; handler.effects.device_attributes = &S.da; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x1B[>c"); @@ -2492,12 +5443,12 @@ test "device attributes: secondary DA" { } test "device attributes: tertiary DA" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2512,7 +5463,7 @@ test "device attributes: tertiary DA" { handler.effects.write_pty = &S.writePty; handler.effects.device_attributes = &S.da; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x1B[=c"); @@ -2520,10 +5471,10 @@ test "device attributes: tertiary DA" { } test "device attributes: readonly ignores" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); - var s: Stream = .initAlloc(testing.allocator, .init(&t)); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); defer s.deinit(); // All DA queries should be silently ignored without effects @@ -2539,12 +5490,12 @@ test "device attributes: readonly ignores" { } test "device attributes: custom response" { - var t: Terminal = try .init(testing.allocator, .{ .cols = 80, .rows = 24 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2568,7 +5519,7 @@ test "device attributes: custom response" { handler.effects.write_pty = &S.writePty; handler.effects.device_attributes = &S.da; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); s.nextSlice("\x1B[c"); @@ -2581,12 +5532,12 @@ test "device attributes: custom response" { test "kitty graphics APC response" { if (comptime !build_options.kitty_graphics) return error.SkipZigTest; - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); const S = struct { var written: ?[]const u8 = null; - fn writePty(_: *Handler, data: [:0]const u8) void { + fn writePty(_: *Handler, data: []const u8) void { if (written) |old| testing.allocator.free(old); written = testing.allocator.dupe(u8, data) catch @panic("OOM"); } @@ -2597,7 +5548,7 @@ test "kitty graphics APC response" { var handler: Handler = .init(&t); handler.effects.write_pty = &S.writePty; - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Send a kitty graphics transmit command with image id 1 @@ -2610,11 +5561,11 @@ test "kitty graphics APC response" { test "kitty graphics via APC" { if (comptime !build_options.kitty_graphics) return error.SkipZigTest; - var t: Terminal = try .init(testing.allocator, .{ .cols = 10, .rows = 10 }); + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 10, .rows = 10 }); defer t.deinit(testing.allocator); const handler: Handler = .init(&t); - var s: Stream = .initAlloc(testing.allocator, handler); + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); defer s.deinit(); // Send a kitty graphics transmit command via APC: @@ -2626,3 +5577,962 @@ test "kitty graphics via APC" { const img = storage.imageById(1).?; try testing.expectEqual(.rgb, img.format); } + +test "continuation reconstructs standard stream without duplicate effects" { + const S = struct { + var bell_count: usize = 0; + var title_count: usize = 0; + var write_count: usize = 0; + var notification_count: usize = 0; + var clipboard_count: usize = 0; + + fn bell(_: *Handler) void { + bell_count += 1; + } + + fn titleChanged(_: *Handler) void { + title_count += 1; + } + + fn writePty(_: *Handler, _: []const u8) void { + write_count += 1; + } + + fn desktopNotification( + _: *Handler, + _: Action.ShowDesktopNotification, + ) void { + notification_count += 1; + } + + fn clipboardWrite( + _: *Handler, + write: clipboard.Write, + ) void { + clipboard_count += 1; + write.reply(.{ .success = .{} }); + } + + fn reset() void { + bell_count = 0; + title_count = 0; + write_count = 0; + notification_count = 0; + clipboard_count = 0; + } + }; + S.reset(); + + const committed = "A\n\x07" ++ + "\x1b]2;title\x1b\\" ++ + "\x1b[5n" ++ + "\x1b]9;body\x1b\\" ++ + "\x1b]52;c;aA==\x1b\\"; + + var source_terminal: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer source_terminal.deinit(testing.allocator); + + var source_handler: Handler = .init(&source_terminal); + source_handler.effects.bell = &S.bell; + source_handler.effects.title_changed = &S.titleChanged; + source_handler.effects.write_pty = &S.writePty; + source_handler.effects.desktop_notification = &S.desktopNotification; + source_handler.effects.clipboard_write = &S.clipboardWrite; + var source = Stream.init(.{ + .allocator = testing.allocator, + .handler = source_handler, + .continuation_max_bytes = 1024, + }); + defer source.deinit(); + + // Terminal mutation and all callbacks have already committed. The + // unfinished CSI is the only input needed to recreate the stream state. + source.nextSlice(committed ++ "\x1b[31"); + try testing.expectEqual(@as(usize, 1), S.bell_count); + try testing.expectEqual(@as(usize, 1), S.title_count); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqual(@as(usize, 1), S.notification_count); + try testing.expectEqual(@as(usize, 1), S.clipboard_count); + + var continuation_buf: [1024]u8 = undefined; + var continuation_writer: std.Io.Writer = .fixed(&continuation_buf); + try source.writeContinuation(&continuation_writer); + try testing.expectEqualStrings("\x1b[31", continuation_writer.buffered()); + + var restored_terminal: Terminal = try .init( + testing.io, + testing.allocator, + .{ .cols = 80, .rows = 24 }, + ); + defer restored_terminal.deinit(testing.allocator); + + // Stand in for restoring the already-committed terminal snapshot. + { + var snapshot_stream: Stream = .init(.{ + .allocator = testing.allocator, + .handler = .init(&restored_terminal), + }); + defer snapshot_stream.deinit(); + snapshot_stream.nextSlice(committed); + } + + const before = try restored_terminal.plainString(testing.allocator); + defer testing.allocator.free(before); + const before_x = restored_terminal.screens.active.cursor.x; + const before_y = restored_terminal.screens.active.cursor.y; + const before_style = restored_terminal.screens.active.cursor.style_id; + const before_title = restored_terminal.getTitle().?; + + var restored_handler: Handler = .init(&restored_terminal); + restored_handler.effects.bell = &S.bell; + restored_handler.effects.title_changed = &S.titleChanged; + restored_handler.effects.write_pty = &S.writePty; + restored_handler.effects.desktop_notification = &S.desktopNotification; + restored_handler.effects.clipboard_write = &S.clipboardWrite; + var restored = Stream.init(.{ + .allocator = testing.allocator, + .handler = restored_handler, + .continuation_max_bytes = 1024, + }); + defer restored.deinit(); + + S.reset(); + restored.nextSlice(continuation_writer.buffered()); + try testing.expectEqual(@as(usize, 0), S.bell_count); + try testing.expectEqual(@as(usize, 0), S.title_count); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqual(@as(usize, 0), S.notification_count); + try testing.expectEqual(@as(usize, 0), S.clipboard_count); + const after = try restored_terminal.plainString(testing.allocator); + defer testing.allocator.free(after); + try testing.expectEqualStrings(before, after); + try testing.expectEqual(before_x, restored_terminal.screens.active.cursor.x); + try testing.expectEqual(before_y, restored_terminal.screens.active.cursor.y); + try testing.expectEqual(before_style, restored_terminal.screens.active.cursor.style_id); + try testing.expectEqualStrings(before_title, restored_terminal.getTitle().?); + + source.nextSlice("mB"); + restored.nextSlice("mB"); + const source_text = try source_terminal.plainString(testing.allocator); + defer testing.allocator.free(source_text); + const restored_text = try restored_terminal.plainString(testing.allocator); + defer testing.allocator.free(restored_text); + try testing.expectEqualStrings(source_text, restored_text); + try testing.expectEqual( + source_terminal.screens.active.cursor.style_id, + restored_terminal.screens.active.cursor.style_id, + ); +} + +test "kitty dnd: query response" { + const S = struct { + var pty: std.ArrayListUnmanaged(u8) = .empty; + fn writePty(_: *Handler, data: []const u8) void { + pty.appendSlice(testing.allocator, data) catch unreachable; + } + }; + S.pty = .empty; + defer S.pty.deinit(testing.allocator); + + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + s.nextSlice("\x1B]72;t=q:i=3\x1B\\"); + try testing.expectEqualStrings("\x1b]72;t=q:i=3\x1b\\", S.pty.items); +} + +test "kitty dnd: register, drop, and serve data" { + const S = struct { + var pty: std.ArrayListUnmanaged(u8) = .empty; + fn writePty(_: *Handler, data: []const u8) void { + pty.appendSlice(testing.allocator, data) catch unreachable; + } + }; + S.pty = .empty; + defer S.pty.deinit(testing.allocator); + + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Client registers to accept drops. + s.nextSlice("\x1B]72;t=a;text/plain text/uri-list\x1B\\"); + try testing.expectEqualStrings("", S.pty.items); + try testing.expect(t.kitty_dnd != null); + + // A native drop arrives; the embedder feeds it to the terminal + // state and delivers the produced event bytes itself. + { + var aw: std.Io.Writer.Allocating = .init(testing.allocator); + defer aw.deinit(); + try t.kitty_dnd.?.dragDrop(testing.allocator, &aw.writer, .{ + .cell_x = 2, + .cell_y = 1, + .pixel_x = 20, + .pixel_y = 18, + .operations = .{ .copy = true }, + }, &.{ + .{ .mime = "text/plain", .data = "hello" }, + }); + try testing.expectEqualStrings( + "\x1b]72;t=M:x=2:y=1:X=20:Y=18:o=1:m=0;text/plain \x1b\\", + aw.written(), + ); + } + + // The client requests the data and concludes. + s.nextSlice("\x1B]72;t=r:x=1\x1B\\"); + try testing.expectEqualStrings( + "\x1b]72;t=r:x=1:m=0;aGVsbG8=\x1b\\" ++ "\x1b]72;t=r:x=1\x1b\\", + S.pty.items, + ); + S.pty.clearRetainingCapacity(); + + s.nextSlice("\x1B]72;t=r\x1B\\"); + try testing.expectEqualStrings("", S.pty.items); + try testing.expect(t.kitty_dnd.?.drop.items == null); +} + +test "kitty dnd: state updates work without write_pty effect" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = .init(&t) }); + defer s.deinit(); + + // Queries produce no output (nowhere to write) but registration + // state still updates. + s.nextSlice("\x1B]72;t=q\x1B\\"); + s.nextSlice("\x1B]72;t=a\x1B\\"); + try testing.expect(t.kitty_dnd != null); + + // The terminal remains functional. + s.nextSlice("ok"); + const str = try t.plainString(testing.allocator); + defer testing.allocator.free(str); + try testing.expectEqualStrings("ok", str); +} + +test "kitty dnd: registration survives terminal reset" { + const S = struct { + var pty: std.ArrayListUnmanaged(u8) = .empty; + fn writePty(_: *Handler, data: []const u8) void { + pty.appendSlice(testing.allocator, data) catch unreachable; + } + }; + S.pty = .empty; + defer S.pty.deinit(testing.allocator); + + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Start a chunked command, then reset mid-chunk. + s.nextSlice("\x1B]72;t=a:i=5\x1B\\"); + s.nextSlice("\x1B]72;t=m:o=1:m=1;text/pl\x1B\\"); + s.nextSlice("\x1Bc"); + + // Registration survives (matching kitty), chunking was interrupted + // so a new command is not treated as a continuation. + try testing.expect(t.kitty_dnd != null); + try testing.expect(!t.kitty_dnd.?.chunking.active); + s.nextSlice("\x1B]72;t=q\x1B\\"); + try testing.expectEqualStrings("\x1b]72;t=q\x1b\\", S.pty.items); +} + +test "kitty dnd: effect reports registration, acceptance, and conclusion" { + const S = struct { + var events: std.ArrayListUnmanaged(kitty_dnd.Event) = .empty; + var mimes: std.ArrayListUnmanaged(u8) = .empty; + + fn clear() void { + events.deinit(testing.allocator); + events = .empty; + mimes.deinit(testing.allocator); + mimes = .empty; + } + + fn dragAndDrop(handler: *Handler, ev: kitty_dnd.Event) void { + events.append(testing.allocator, ev) catch unreachable; + // Registration details are read from the terminal state. + if (ev == .registration) { + mimes.clearRetainingCapacity(); + const state = handler.terminal.kitty_dnd orelse return; + var it = state.registeredMimes(); + while (it.next()) |m| { + mimes.appendSlice(testing.allocator, m) catch unreachable; + mimes.append(testing.allocator, ',') catch unreachable; + } + } + } + }; + S.clear(); + defer S.clear(); + + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + var handler: Handler = .init(&t); + handler.effects.drag_and_drop = &S.dragAndDrop; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // Registration with a MIME list, read back from the state. + s.nextSlice("\x1B]72;t=a;image/png text/plain\x1B\\"); + try testing.expectEqual(@as(usize, 1), S.events.items.len); + try testing.expect(S.events.items[0] == .registration); + try testing.expectEqualStrings("image/png,text/plain,", S.mimes.items); + + // A native drag and the client's answer. + { + var aw: std.Io.Writer.Allocating = .init(testing.allocator); + defer aw.deinit(); + try t.kitty_dnd.?.dragDrop(testing.allocator, &aw.writer, .{ + .cell_x = 0, + .cell_y = 0, + .pixel_x = 0, + .pixel_y = 0, + .operations = .{ .copy = true }, + }, &.{.{ .mime = "text/plain", .data = "x" }}); + } + s.nextSlice("\x1B]72;t=m:o=2;text/plain\x1B\\"); + try testing.expectEqual(@as(usize, 2), S.events.items.len); + try testing.expect(S.events.items[1] == .acceptance); + + // Conclusion carries the performed operation. + s.nextSlice("\x1B]72;t=r:o=2\x1B\\"); + try testing.expectEqual(@as(usize, 3), S.events.items.len); + try testing.expectEqual(kitty_dnd.Event.concluded_move, S.events.items[2]); + + // Unregistration reports with the state gone. + s.nextSlice("\x1B]72;t=A\x1B\\"); + try testing.expectEqual(@as(usize, 4), S.events.items.len); + try testing.expect(S.events.items[3] == .registration); + try testing.expect(t.kitty_dnd == null); + try testing.expectEqualStrings("", S.mimes.items); +} + +/// Capture state for the Handler.paste tests below: every pty write and +/// the clipboard reads the program makes afterwards. +const PasteCapture = struct { + var written: std.ArrayList(u8) = .empty; + var write_count: usize = 0; + var read_count: usize = 0; + var read_granted_count: usize = 0; + var last_read_granted: bool = false; + var last_read_name: [64]u8 = undefined; + var last_read_name_len: usize = 0; + + fn reset() void { + written.clearRetainingCapacity(); + write_count = 0; + read_count = 0; + read_granted_count = 0; + last_read_granted = false; + last_read_name_len = 0; + } + + fn deinit() void { + written.deinit(testing.allocator); + written = .empty; + } + + fn writePty(_: *Handler, data: []const u8) void { + written.appendSlice(testing.allocator, data) catch @panic("OOM"); + write_count += 1; + } + + fn clipboardRead(_: *Handler, read: clipboard.Read) void { + read_count += 1; + last_read_granted = read.granted; + if (read.granted) read_granted_count += 1; + last_read_name_len = read.name.len; + @memcpy(last_read_name[0..read.name.len], read.name); + read.reply(.{ .success = .{ .contents = &.{.{ + .mime = "text/plain", + .data = "Ghostty", + }} } }); + } + + fn readName() []const u8 { + return last_read_name[0..last_read_name_len]; + } +}; + +test "paste: no write_pty effect is an error" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + var handler: Handler = .init(&t); + defer handler.deinit(); + try testing.expectError(error.NoWritePty, handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hello" }} }, + })); +} + +test "paste: plain text converts newlines and strips unsafe bytes" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + + // Newlines are unsafe unbracketed; the embedder confirmed. + try testing.expect(try handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hel\x1blo\nwor\x00ld" }} }, + .allow_unsafe = true, + })); + try testing.expectEqualStrings("hel lo\rwor ld", S.written.items); + try testing.expectEqual(@as(usize, 1), S.write_count); + + // The first text representation is used; others are ignored. + S.reset(); + try testing.expect(try handler.paste(.{ + .contents = .{ .memory = &.{ + .{ .mime = "image/png", .data = "\x89PNG" }, + .{ .mime = "UTF8_STRING", .data = "hi" }, + .{ .mime = "text/plain", .data = "ignored" }, + } }, + })); + try testing.expectEqualStrings("hi", S.written.items); + try testing.expectEqual(@as(usize, 1), S.write_count); +} + +test "paste: unsafe text is refused unless allowed" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + + try testing.expectError(error.UnsafePaste, handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "rm -rf /\n" }} }, + })); + try testing.expectEqual(@as(usize, 0), S.write_count); + + try testing.expect(try handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "rm -rf /\n" }} }, + .allow_unsafe = true, + })); + try testing.expectEqualStrings("rm -rf /\r", S.written.items); +} + +test "paste: bracketed paste frames the text" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + t.modes.set(.bracketed_paste, true); + + // Newlines are safe inside the frame and are preserved. + try testing.expect(try handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hello\nworld" }} }, + })); + try testing.expectEqualStrings("\x1b[200~hello\nworld\x1b[201~", S.written.items); + try testing.expectEqual(@as(usize, 1), S.write_count); + + // The frame terminator is not. + S.reset(); + try testing.expectError(error.UnsafePaste, handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "he\x1b[201~llo" }} }, + })); + try testing.expectEqual(@as(usize, 0), S.write_count); + + // Allowed, the stripper still defuses it: ESC becomes a space. + try testing.expect(try handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "he\x1b[201~llo" }} }, + .allow_unsafe = true, + })); + try testing.expectEqualStrings("\x1b[200~he [201~llo\x1b[201~", S.written.items); +} + +test "paste: no text representation writes nothing" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + + try testing.expect(!try handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "image/png", .data = "\x89PNG" }} }, + })); + try testing.expect(!try handler.paste(.{ + .contents = .{ .memory = &.{} }, + })); + try testing.expect(!try handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "" }} }, + })); + try testing.expectEqual(@as(usize, 0), S.write_count); +} + +test "paste: large text streams to the pty in chunks" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + t.modes.set(.bracketed_paste, true); + + // Two full chunks and a partial one with the frame, never the + // whole thing at once. + const data = "x" ** 10_000; + try testing.expect(try handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = data }} }, + })); + const total = data.len + "\x1b[200~\x1b[201~".len; + try testing.expectEqual( + @as(usize, (total + Handler.paste_chunk_size - 1) / Handler.paste_chunk_size), + S.write_count, + ); + try testing.expectEqual(total, S.written.items.len); + try testing.expect(std.mem.startsWith(u8, S.written.items, "\x1b[200~xxx")); + try testing.expect(std.mem.endsWith(u8, S.written.items, "xxx\x1b[201~")); +} + +/// A paste contents reader for the tests: serves fixed data per MIME +/// type in pieces, counting the reads of each representation. +const PasteReader = struct { + mimes: []const []const u8, + data: []const []const u8, + piece: usize = 3, + reads: [4]usize = @splat(0), + /// Fail after this many bytes of a read. + fail_after: ?usize = null, + + fn contents(self: *PasteReader) paste_pkg.Contents { + return .{ .reader = .{ + .mimes = self.mimes, + .read = .{ .ctx = self, .read_fn = &read }, + } }; + } + + fn read(ctx: ?*anyopaque, mime: []const u8, sink: *std.Io.Writer) clipboard.MimeReader.Error!void { + const self: *PasteReader = @ptrCast(@alignCast(ctx.?)); + const index: usize = for (self.mimes, 0..) |m, i| { + if (std.mem.eql(u8, m, mime)) break i; + } else return error.ReadFailed; + self.reads[index] += 1; + const data = self.data[index]; + var offset: usize = 0; + while (offset < data.len) { + if (self.fail_after) |limit| if (offset >= limit) return error.ReadFailed; + const n = @min(self.piece, data.len - offset); + try sink.writeAll(data[offset..][0..n]); + offset += n; + } + } +}; + +test "paste: reader contents are read on demand" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + + // Unsafe text is refused from one read with nothing written; the + // image is never read. + var reader: PasteReader = .{ + .mimes = &.{ "image/png", "text/plain" }, + .data = &.{ "\x89PNG", "echo hi\nrm -rf /\n" }, + }; + try testing.expectError(error.UnsafePaste, handler.paste(.{ + .contents = reader.contents(), + })); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqual(@as(usize, 0), reader.reads[0]); + try testing.expectEqual(@as(usize, 1), reader.reads[1]); + + // Allowed, the text is read once, encoded. + reader.reads = @splat(0); + try testing.expect(try handler.paste(.{ + .contents = reader.contents(), + .allow_unsafe = true, + })); + try testing.expectEqualStrings("echo hi\rrm -rf /\r", S.written.items); + try testing.expectEqual(@as(usize, 0), reader.reads[0]); + try testing.expectEqual(@as(usize, 1), reader.reads[1]); + + // Safe text is read once too: buffered for the check, then written. + S.reset(); + reader = .{ + .mimes = &.{"text/plain"}, + .data = &.{"hello world"}, + }; + try testing.expect(try handler.paste(.{ .contents = reader.contents() })); + try testing.expectEqualStrings("hello world", S.written.items); + try testing.expectEqual(@as(usize, 1), reader.reads[0]); + + // Empty text is nothing to paste, found on the one read. + S.reset(); + reader = .{ + .mimes = &.{"text/plain"}, + .data = &.{""}, + }; + try testing.expect(!try handler.paste(.{ .contents = reader.contents() })); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqual(@as(usize, 1), reader.reads[0]); + + // A paste event lists the types and reads nothing at all. + S.reset(); + t.modes.set(.kitty_paste_events, true); + reader = .{ + .mimes = &.{ "text/plain", "image/png" }, + .data = &.{ "secret", "\x89PNG" }, + }; + try testing.expect(try handler.paste(.{ .contents = reader.contents() })); + try testing.expect(std.mem.indexOf(u8, S.written.items, ";dGV4dC9wbGFpbiBpbWFnZS9wbmcK\x1b\\") != null); + try testing.expect(std.mem.indexOf(u8, S.written.items, "secret") == null); + try testing.expectEqual(@as(usize, 0), reader.reads[0]); + try testing.expectEqual(@as(usize, 0), reader.reads[1]); +} + +test "paste: reader failure writes nothing" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + t.modes.set(.bracketed_paste, true); + + // The read is buffered whole before anything is written, so a + // mid-read failure discards the buffer, checked or not. + var reader: PasteReader = .{ + .mimes = &.{"text/plain"}, + .data = &.{"hello world"}, + .fail_after = 6, + }; + try testing.expectError(error.ReadFailed, handler.paste(.{ + .contents = reader.contents(), + })); + try testing.expectEqual(@as(usize, 0), S.write_count); + + try testing.expectError(error.ReadFailed, handler.paste(.{ + .contents = reader.contents(), + .allow_unsafe = true, + })); + try testing.expectEqual(@as(usize, 0), S.write_count); +} + +test "paste: mode 5522 sends an event the program can read with" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + t.modes.set(.kitty_paste_events, true); + t.modes.set(.bracketed_paste, true); + + // Every representation is listed, the data is never written, and + // the one-time password rides on every packet. + try testing.expect(try s.handler.paste(.{ + .contents = .{ .memory = &.{ + .{ .mime = "text/plain", .data = "secret" }, + .{ .mime = "image/png", .data = "" }, + } }, + })); + try testing.expectEqual(@as(usize, 1), S.write_count); + try testing.expectEqual(@as(usize, 3), std.mem.count(u8, S.written.items, "\x1b]5522;")); + try testing.expect(std.mem.indexOf(u8, S.written.items, "secret") == null); + try testing.expect(std.mem.indexOf(u8, S.written.items, "\x1b[200~") == null); + + // OK packet: parse the (base64) password out. + const ok_prefix = "\x1b]5522;type=read:status=OK:pw="; + try testing.expect(std.mem.startsWith(u8, S.written.items, ok_prefix)); + const pw_end = std.mem.indexOfPos(u8, S.written.items, ok_prefix.len, "\x1b\\").?; + // Copied out since the capture buffer is reused below. + var pw_buf: [64]u8 = undefined; + const pw_b64 = pw_buf[0 .. pw_end - ok_prefix.len]; + @memcpy(pw_b64, S.written.items[ok_prefix.len..pw_end]); + try testing.expectEqual( + std.base64.standard.Encoder.calcSize(kitty_clipboard.otp_len), + pw_b64.len, + ); + + // Listing packet: base64 of "text/plain image/png\n". + var expected_buf: [256]u8 = undefined; + const expected = try std.fmt.bufPrint( + &expected_buf, + "\x1b]5522;type=read:status=OK:pw={s}\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=Lg==:pw={s};dGV4dC9wbGFpbiBpbWFnZS9wbmcK\x1b\\" ++ + "\x1b]5522;type=read:status=DONE:pw={s}\x1b\\", + .{ pw_b64, pw_b64, pw_b64 }, + ); + try testing.expectEqualStrings(expected, S.written.items); + + // The program reads with the password and the name "Paste event": + // the read arrives granted, exactly once. + var read_buf: [256]u8 = undefined; + const read = try std.fmt.bufPrint( + &read_buf, + "\x1b]5522;type=read:pw={s}:name=UGFzdGUgZXZlbnQ=;dGV4dC9wbGFpbg==\x1b\\", + .{pw_b64}, + ); + S.reset(); + s.nextSlice(read); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expect(S.last_read_granted); + try testing.expectEqualStrings("Paste event", S.readName()); + try testing.expectEqualStrings( + "\x1b]5522;type=read:status=OK\x1b\\" ++ + "\x1b]5522;type=read:status=DATA:mime=dGV4dC9wbGFpbg==;R2hvc3R0eQ==\x1b\\" ++ + "\x1b]5522;type=read:status=DONE\x1b\\", + S.written.items, + ); + + // The password was one-time: a second read is not granted. + S.reset(); + s.nextSlice(read); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expect(!S.last_read_granted); + try testing.expectEqual(@as(usize, 0), S.read_granted_count); + + // Every event mints a fresh password. + S.reset(); + try testing.expect(try s.handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "secret" }} }, + })); + try testing.expect(std.mem.indexOf(u8, S.written.items, pw_b64) == null); +} + +test "paste: mode 5522 reports the selection as primary" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + t.modes.set(.kitty_paste_events, true); + + for ([_]clipboard.Location{ .primary, .selection }) |location| { + S.reset(); + try testing.expect(try handler.paste(.{ + .source = .{ .clipboard = location }, + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "x" }} }, + })); + try testing.expect(std.mem.startsWith( + u8, + S.written.items, + "\x1b]5522;type=read:status=OK:loc=primary:pw=", + )); + // Only on the OK packet. + try testing.expectEqual(@as(usize, 1), std.mem.count(u8, S.written.items, "loc=primary")); + } + + S.reset(); + try testing.expect(try handler.paste(.{ + .source = .{ .clipboard = .standard }, + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "x" }} }, + })); + try testing.expect(std.mem.indexOf(u8, S.written.items, "loc=") == null); +} + +test "paste: mode 5522 without clipboard_read pastes text" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + t.modes.set(.kitty_paste_events, true); + + try testing.expect(try handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hello" }} }, + })); + try testing.expectEqualStrings("hello", S.written.items); + try testing.expectEqual(@as(usize, 0), handler.kitty_clipboard_grants.entries.items.len); +} + +test "paste: text source never becomes an event" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + t.modes.set(.kitty_paste_events, true); + + try testing.expect(try handler.paste(.{ + .source = .text, + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "committed" }} }, + })); + try testing.expectEqualStrings("committed", S.written.items); + try testing.expectEqual(@as(usize, 0), handler.kitty_clipboard_grants.entries.items.len); +} + +test "paste: mode 5522 without entropy fails and records no grant" { + var t: Terminal = try .init(std.Io.failing, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + defer handler.deinit(); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + t.modes.set(.kitty_paste_events, true); + + try testing.expectError(error.EntropyUnavailable, handler.paste(.{ + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "secret" }} }, + })); + try testing.expectEqual(@as(usize, 0), S.write_count); + try testing.expectEqual(@as(usize, 0), handler.kitty_clipboard_grants.entries.items.len); + + // Text pastes need no entropy and still work. + try testing.expect(try handler.paste(.{ + .source = .text, + .contents = .{ .memory = &.{.{ .mime = "text/plain", .data = "hello" }} }, + })); + try testing.expectEqualStrings("hello", S.written.items); +} + +test "paste: mode 5522 DECRQM requires clipboard read effect" { + if (comptime build_options.artifact != .lib) return error.SkipZigTest; + + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + // A paste event is unusable if its follow-up read can't be served, so + // the mode isn't advertised without a clipboard read effect. + s.nextSlice("\x1B[?5522$p"); + try testing.expectEqualStrings("\x1B[?5522;0$y", S.written.items); + + S.reset(); + s.nextSlice("\x1B[?5522h"); + try testing.expect(t.modes.get(.kitty_paste_events)); + s.nextSlice("\x1B[?5522$p"); + try testing.expectEqualStrings("\x1B[?5522;0$y", S.written.items); + + // Installing the effect makes the current mode state reportable. + S.reset(); + s.handler.effects.clipboard_read = &S.clipboardRead; + s.nextSlice("\x1B[?5522$p"); + try testing.expectEqualStrings("\x1B[?5522;1$y", S.written.items); + + S.reset(); + s.nextSlice("\x1B[?5522l"); + try testing.expect(!t.modes.get(.kitty_paste_events)); + s.nextSlice("\x1B[?5522$p"); + try testing.expectEqualStrings("\x1B[?5522;2$y", S.written.items); + + // Removing the effect stops advertising the capability again. + S.reset(); + s.handler.effects.clipboard_read = null; + s.nextSlice("\x1B[?5522$p"); + try testing.expectEqualStrings("\x1B[?5522;0$y", S.written.items); +} + +test "full reset drops kitty clipboard grants" { + var t: Terminal = try .init(testing.io, testing.allocator, .{ .cols = 80, .rows = 24 }); + defer t.deinit(testing.allocator); + + const S = PasteCapture; + S.reset(); + defer S.deinit(); + + var handler: Handler = .init(&t); + handler.effects.write_pty = &S.writePty; + handler.effects.clipboard_read = &S.clipboardRead; + var s: Stream = .init(.{ .allocator = testing.allocator, .handler = handler }); + defer s.deinit(); + + try s.handler.kitty_clipboard_grants.grant(testing.allocator, "pw", .read, false); + try testing.expectEqual(@as(usize, 1), s.handler.kitty_clipboard_grants.entries.items.len); + + s.nextSlice("\x1Bc"); + try testing.expectEqual(@as(usize, 0), s.handler.kitty_clipboard_grants.entries.items.len); + + // A read with the old password is no longer granted, and the + // handler keeps working (grants can be recorded again). + S.reset(); + s.nextSlice("\x1b]5522;type=read:pw=cHc=:name=YXBw;dGV4dC9wbGFpbg==\x1b\\"); + try testing.expectEqual(@as(usize, 1), S.read_count); + try testing.expect(!S.last_read_granted); + try s.handler.kitty_clipboard_grants.grant(testing.allocator, "pw", .read, false); +} diff --git a/vendor/libghostty-vt/src/terminal/style.zig b/vendor/libghostty-vt/src/terminal/style.zig index 720789bb..701356e9 100644 --- a/vendor/libghostty-vt/src/terminal/style.zig +++ b/vendor/libghostty-vt/src/terminal/style.zig @@ -1,5 +1,7 @@ const std = @import("std"); +const builtin = @import("builtin"); const assert = @import("../quirks.zig").inlineAssert; +const fastprint = @import("../fastprint.zig"); const color = @import("color.zig"); const sgr = @import("sgr.zig"); const page = @import("page.zig"); @@ -88,11 +90,29 @@ pub const Style = struct { /// True if the style is the default style. pub inline fn default(self: Style) bool { + // On wasm, eql converts both sides to packed form; the default + // side is comptime-known, so bake it and convert only self. + // This is called on every SGR change so it's worth it. + if (comptime builtin.cpu.arch.isWasm()) { + const d: u128 = comptime @bitCast(PackedStyle.fromStyle(.{})); + return @as(u128, @bitCast(PackedStyle.fromStyle(self))) == d; + } + return self.eql(.{}); } /// True if the style is equal to another style. pub fn eql(self: Style, other: Style) bool { + // On wasm, comparing packed forms wins (~13% on SGR-heavy streams). + // On native, the branchy early-exit field compare below measures + // ~5% faster (unequal styles usually differ in the first + // field or two), so each target keeps its own strategy. + if (comptime builtin.cpu.arch.isWasm()) { + const a: u128 = @bitCast(PackedStyle.fromStyle(self)); + const b: u128 = @bitCast(PackedStyle.fromStyle(other)); + return a == b; + } + return self.flags == other.flags and self.fg_color.eql(other.fg_color) and self.bg_color.eql(other.bg_color) and @@ -111,7 +131,7 @@ pub const Style = struct { palette: *const color.Palette, ) ?color.RGB { return switch (cell.content_tag) { - .bg_color_palette => palette[cell.content.color_palette], + .bg_color_palette => palette[cell.content.color_palette.data], .bg_color_rgb => rgb: { const rgb = cell.content.color_rgb; break :rgb .{ .r = rgb.r, .g = rgb.g, .b = rgb.b }; @@ -212,7 +232,7 @@ pub const Style = struct { .none => null, .palette => |idx| .{ .content_tag = .bg_color_palette, - .content = .{ .color_palette = idx }, + .content = .{ .color_palette = .{ .data = idx } }, }, .rgb => |rgb| .{ .content_tag = .bg_color_rgb, @@ -331,63 +351,141 @@ pub const Style = struct { self: VTFormatter, writer: *std.Io.Writer, ) !void { + // Style emission is a hot path when formatting styled terminal + // contents, so all of the sequences are assembled in a buffer + // and written in one call rather than going through the + // (slower) format string machinery with a write per sequence. + // + // Worst case: `\x1b[0m` (4) + 7 flags (28) + `\x1b[53m` (5) + + // `\x1b[4:2m` (6) + 3 RGB colors (19 each = 57) = 100. + var buf: [128]u8 = undefined; + // Always reset the style. Styles are fully self-contained. // Even if this style is empty, then that means we want to go // back to the default. - try writer.writeAll("\x1b[0m"); + buf[0..4].* = "\x1b[0m".*; + var len: usize = 4; // Our flags - if (self.style.flags.bold) try writer.writeAll("\x1b[1m"); - if (self.style.flags.faint) try writer.writeAll("\x1b[2m"); - if (self.style.flags.italic) try writer.writeAll("\x1b[3m"); - if (self.style.flags.blink) try writer.writeAll("\x1b[5m"); - if (self.style.flags.inverse) try writer.writeAll("\x1b[7m"); - if (self.style.flags.invisible) try writer.writeAll("\x1b[8m"); - if (self.style.flags.strikethrough) try writer.writeAll("\x1b[9m"); - if (self.style.flags.overline) try writer.writeAll("\x1b[53m"); + if (self.style.flags.bold) { + buf[len..][0..4].* = "\x1b[1m".*; + len += 4; + } + if (self.style.flags.faint) { + buf[len..][0..4].* = "\x1b[2m".*; + len += 4; + } + if (self.style.flags.italic) { + buf[len..][0..4].* = "\x1b[3m".*; + len += 4; + } + if (self.style.flags.blink) { + buf[len..][0..4].* = "\x1b[5m".*; + len += 4; + } + if (self.style.flags.inverse) { + buf[len..][0..4].* = "\x1b[7m".*; + len += 4; + } + if (self.style.flags.invisible) { + buf[len..][0..4].* = "\x1b[8m".*; + len += 4; + } + if (self.style.flags.strikethrough) { + buf[len..][0..4].* = "\x1b[9m".*; + len += 4; + } + if (self.style.flags.overline) { + buf[len..][0..5].* = "\x1b[53m".*; + len += 5; + } switch (self.style.flags.underline) { .none => {}, - .single => try writer.writeAll("\x1b[4m"), - .double => try writer.writeAll("\x1b[4:2m"), - .curly => try writer.writeAll("\x1b[4:3m"), - .dotted => try writer.writeAll("\x1b[4:4m"), - .dashed => try writer.writeAll("\x1b[4:5m"), + .single => { + buf[len..][0..4].* = "\x1b[4m".*; + len += 4; + }, + .double => { + buf[len..][0..6].* = "\x1b[4:2m".*; + len += 6; + }, + .curly => { + buf[len..][0..6].* = "\x1b[4:3m".*; + len += 6; + }, + .dotted => { + buf[len..][0..6].* = "\x1b[4:4m".*; + len += 6; + }, + .dashed => { + buf[len..][0..6].* = "\x1b[4:5m".*; + len += 6; + }, } - // Various RGB colors. - try self.formatColor(writer, 38, self.style.fg_color); - try self.formatColor(writer, 48, self.style.bg_color); - try self.formatColor(writer, 58, self.style.underline_color); + // Various colors. + len += self.appendColor(buf[len..], 38, self.style.fg_color); + len += self.appendColor(buf[len..], 48, self.style.bg_color); + len += self.appendColor(buf[len..], 58, self.style.underline_color); + + try writer.writeAll(buf[0..len]); } - fn formatColor( + /// Appends a standalone `\x1b[{prefix};5;{idx}m` or + /// `\x1b[{prefix};2;{r};{g};{b}m` sequence to buf, returning the + /// length written. + fn appendColor( self: VTFormatter, - writer: *std.Io.Writer, + buf: []u8, prefix: u8, value: Color, - ) !void { + ) usize { switch (value) { - .none => {}, + .none => return 0, + .palette => |idx| { - if (self.palette) |p| { - const rgb = p[idx]; - try writer.print( - "\x1b[{d};2;{d};{d};{d}m", - .{ prefix, rgb.r, rgb.g, rgb.b }, - ); - } else { - try writer.print( - "\x1b[{d};5;{d}m", - .{ prefix, idx }, - ); - } + // Direct RGB: `\x1b[{prefix};2;{r};{g};{b}m` + if (self.palette) |p| return appendColorRgb( + buf, + prefix, + p[idx], + ); + + // Palette reference: `\x1b[{prefix};5;{idx}m` + buf[0..2].* = "\x1b[".*; + var len: usize = 2; + len += fastprint.printDecimal(u8, buf[len..], prefix); + buf[len..][0..3].* = ";5;".*; + len += 3; + len += fastprint.printDecimal(u8, buf[len..], idx); + buf[len] = 'm'; + len += 1; + return len; }, - .rgb => |rgb| try writer.print( - "\x1b[{d};2;{d};{d};{d}m", - .{ prefix, rgb.r, rgb.g, rgb.b }, - ), + + .rgb => |rgb| return appendColorRgb(buf, prefix, rgb), } } + + /// Appends `\x1b[{prefix};2;{r};{g};{b}m` to buf, returning the + /// length written. + fn appendColorRgb(buf: []u8, prefix: u8, rgb: color.RGB) usize { + buf[0..2].* = "\x1b[".*; + var len: usize = 2; + len += fastprint.printDecimal(u8, buf[len..], prefix); + buf[len..][0..3].* = ";2;".*; + len += 3; + len += fastprint.printDecimal(u8, buf[len..], rgb.r); + buf[len] = ';'; + len += 1; + len += fastprint.printDecimal(u8, buf[len..], rgb.g); + buf[len] = ';'; + len += 1; + len += fastprint.printDecimal(u8, buf[len..], rgb.b); + buf[len] = 'm'; + len += 1; + return len; + } }; const HtmlFormatter = struct { @@ -444,18 +542,52 @@ pub const Style = struct { property: []const u8, c: Color, ) !void { + // Style emission is a hot path when formatting styled terminal + // contents, so the values are assembled in a buffer and written + // in one call rather than going through the (slower) format + // string machinery. + var buf: [32]u8 = undefined; + var len: usize = 0; switch (c) { - .none => {}, + .none => return, + + // `{property}: rgb({r}, {g}, {b});` .palette => |idx| { if (self.palette) |p| { - const rgb = p[idx]; - try writer.print("{s}: rgb({d}, {d}, {d});", .{ property, rgb.r, rgb.g, rgb.b }); + len = formatColorRgb(&buf, p[idx]); } else { - try writer.print("{s}: var(--vt-palette-{d});", .{ property, idx }); + // `{property}: var(--vt-palette-{idx});` + const prefix = ": var(--vt-palette-"; + buf[0..prefix.len].* = prefix.*; + len = prefix.len; + len += fastprint.printDecimal(u8, buf[len..], idx); + buf[len..][0..2].* = ");".*; + len += 2; } }, - .rgb => |rgb| try writer.print("{s}: rgb({d}, {d}, {d});", .{ property, rgb.r, rgb.g, rgb.b }), + + .rgb => |rgb| len = formatColorRgb(&buf, rgb), } + + try writer.writeAll(property); + try writer.writeAll(buf[0..len]); + } + + /// Writes `: rgb({r}, {g}, {b});` into buf, returning the length + /// written. + fn formatColorRgb(buf: *[32]u8, rgb: color.RGB) usize { + buf[0..6].* = ": rgb(".*; + var len: usize = 6; + len += fastprint.printDecimal(u8, buf[len..], rgb.r); + buf[len..][0..2].* = ", ".*; + len += 2; + len += fastprint.printDecimal(u8, buf[len..], rgb.g); + buf[len..][0..2].* = ", ".*; + len += 2; + len += fastprint.printDecimal(u8, buf[len..], rgb.b); + buf[len..][0..2].* = ");".*; + len += 2; + return len; } }; diff --git a/vendor/libghostty-vt/src/terminal/sys.zig b/vendor/libghostty-vt/src/terminal/sys.zig index f0c64da5..3f6b5017 100644 --- a/vendor/libghostty-vt/src/terminal/sys.zig +++ b/vendor/libghostty-vt/src/terminal/sys.zig @@ -52,3 +52,51 @@ fn decodePngWuffs( .data = result.data, }; } + +/// Fill a buffer with cryptographically secure random bytes. If null, +/// the terminal's `std.Io` (`randomSecure`) is used. This is an override +/// for embedders whose Io has no entropy source (e.g. wasm32-freestanding, +/// where TinyIo degrades to `std.Io.failing`) or that want to control +/// the source; when set it is used on every target. +/// +/// This is used for secrets, so it must be a real CSPRNG. An error +/// makes the operation that needed the entropy fail; nothing falls back +/// to weaker randomness. +pub var random_secure: ?RandomSecureFn = null; + +pub const RandomSecureError = error{EntropyUnavailable}; +pub const RandomSecureFn = *const fn ([]u8) RandomSecureError!void; + +/// Fill `buffer` with secure random bytes from `random_secure` if set, +/// otherwise from `io`. Every use of secure entropy in the terminal +/// package goes through this so the override applies uniformly. +pub fn randomSecure(io: std.Io, buffer: []u8) std.Io.RandomSecureError!void { + if (random_secure) |func| return func(buffer); + return io.randomSecure(buffer); +} + +test "randomSecure: override is preferred over the Io" { + const testing = std.testing; + const S = struct { + fn fill(buffer: []u8) RandomSecureError!void { + @memset(buffer, 0xAB); + } + fn fail(_: []u8) RandomSecureError!void { + return error.EntropyUnavailable; + } + }; + + // Without the override a failing Io fails. + var buf: [8]u8 = @splat(0); + try testing.expectError(error.EntropyUnavailable, randomSecure(std.Io.failing, &buf)); + + // With it, the Io is never consulted. + random_secure = &S.fill; + defer random_secure = null; + try randomSecure(std.Io.failing, &buf); + try testing.expect(std.mem.allEqual(u8, &buf, 0xAB)); + + // An override failure surfaces as the Io's error. + random_secure = &S.fail; + try testing.expectError(error.EntropyUnavailable, randomSecure(testing.io, &buf)); +} diff --git a/vendor/libghostty-vt/src/terminal/tmux/control.zig b/vendor/libghostty-vt/src/terminal/tmux/control.zig index 3b94eb16..b06560dd 100644 --- a/vendor/libghostty-vt/src/terminal/tmux/control.zig +++ b/vendor/libghostty-vt/src/terminal/tmux/control.zig @@ -117,7 +117,7 @@ pub const Parser = struct { const line = written[idx..]; if (parseBlockTerminator(line)) |terminator| { - const output = std.mem.trimRight( + const output = std.mem.trimEnd( u8, written[0..idx], "\r\n", diff --git a/vendor/libghostty-vt/src/terminal/tmux/output.zig b/vendor/libghostty-vt/src/terminal/tmux/output.zig index 6b8073e4..9e21703b 100644 --- a/vendor/libghostty-vt/src/terminal/tmux/output.zig +++ b/vendor/libghostty-vt/src/terminal/tmux/output.zig @@ -70,23 +70,17 @@ pub fn format( /// format variables. This can be used with `parseFormatStruct` to /// parse an output string into a format struct. pub fn FormatStruct(comptime vars: []const Variable) type { - var fields: [vars.len]std.builtin.Type.StructField = undefined; - for (vars, &fields) |variable, *field| { - field.* = .{ - .name = @tagName(variable), - .type = variable.Type(), - .default_value_ptr = null, - .is_comptime = false, - .alignment = @alignOf(variable.Type()), - }; + var names: [vars.len][]const u8 = undefined; + var types: [vars.len]type = undefined; + var attrs: [vars.len]std.builtin.Type.StructField.Attributes = undefined; + + for (vars, &names, &types, &attrs) |variable, *name, *ty, *attr| { + name.* = @tagName(variable); + ty.* = variable.Type(); + attr.* = .{ .@"align" = @alignOf(variable.Type()) }; } - return @Type(.{ .@"struct" = .{ - .layout = .auto, - .fields = &fields, - .decls = &.{}, - .is_tuple = false, - } }); + return @Struct(.auto, null, &names, &types, &attrs); } /// Possible variables in a tmux format string that we support. diff --git a/vendor/libghostty-vt/src/terminal/tmux/viewer.zig b/vendor/libghostty-vt/src/terminal/tmux/viewer.zig index 585c9540..5f080089 100644 --- a/vendor/libghostty-vt/src/terminal/tmux/viewer.zig +++ b/vendor/libghostty-vt/src/terminal/tmux/viewer.zig @@ -156,6 +156,9 @@ const COMMAND_QUEUE_INITIAL = 8; /// session. /// pub const Viewer = struct { + /// I/O implementation used for all internal state. + io: std.Io, + /// Allocator used for all internal state. alloc: Allocator, @@ -265,12 +268,13 @@ pub const Viewer = struct { /// /// The given allocator is used for all internal state. You must /// call deinit when you're done with the viewer to free it. - pub fn init(alloc: Allocator) Allocator.Error!Viewer { + pub fn init(io: std.Io, alloc: Allocator) Allocator.Error!Viewer { // Create our initial command queue var command_queue: CommandQueue = try .init(alloc, COMMAND_QUEUE_INITIAL); errdefer command_queue.deinit(alloc); return .{ + .io = io, .alloc = alloc, .state = .startup_block, // The default value here is meaningless. We don't get started @@ -639,6 +643,7 @@ pub const Viewer = struct { panes.deinit(self.alloc); } for (windows) |window| try initLayout( + self.io, self.alloc, &self.panes, &panes, @@ -723,7 +728,7 @@ pub const Viewer = struct { session_id: usize, ) (Allocator.Error || std.Io.Writer.Error)!void { // Build up a new viewer. Its the easiest way to reset ourselves. - var replacement: Viewer = try .init(self.alloc); + var replacement: Viewer = try .init(self.io, self.alloc); errdefer replacement.deinit(); // Our actions must start out empty so we don't mix arenas @@ -1115,6 +1120,7 @@ pub const Viewer = struct { } fn initLayout( + io: std.Io, gpa_alloc: Allocator, panes_old: *const PanesMap, panes_new: *PanesMap, @@ -1125,6 +1131,7 @@ pub const Viewer = struct { .horizontal, .vertical => |layouts| { for (layouts) |l| { try initLayout( + io, gpa_alloc, panes_old, panes_new, @@ -1149,7 +1156,7 @@ pub const Viewer = struct { // TODO: We need to gracefully handle overflow of our // max cols/width here. In practice we shouldn't hit this // so we cast but its not safe. - var t: Terminal = try .init(gpa_alloc, .{ + var t: Terminal = try .init(io, gpa_alloc, .{ .cols = @intCast(layout.width), .rows = @intCast(layout.height), }); @@ -1494,7 +1501,7 @@ fn testViewer(viewer: *Viewer, steps: []const TestStep) !void { } test "immediate exit" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ @@ -1514,7 +1521,7 @@ test "immediate exit" { } test "session changed resets state" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ @@ -1605,7 +1612,7 @@ test "session changed resets state" { } test "initial flow" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ @@ -1786,7 +1793,7 @@ test "initial flow" { } test "layout change" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ @@ -1857,7 +1864,7 @@ test "layout change" { } test "layout_change does not return command when queue not empty" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ @@ -1918,7 +1925,7 @@ test "layout_change does not return command when queue not empty" { } test "layout_change returns command when queue was empty" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ @@ -1985,7 +1992,7 @@ test "layout_change returns command when queue was empty" { } test "window_add queues list_windows when queue empty" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ @@ -2046,7 +2053,7 @@ test "window_add queues list_windows when queue empty" { } test "window_add queues list_windows when queue not empty" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ @@ -2102,7 +2109,7 @@ test "window_add queues list_windows when queue not empty" { } test "two pane flow with pane state" { - var viewer = try Viewer.init(testing.allocator); + var viewer = try Viewer.init(testing.io, testing.allocator); defer viewer.deinit(); try testViewer(&viewer, &.{ -- cgit v1.2.3