From 58133043588bd15b832571fdf4b5bc05c7c5df75 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 13 Mar 2024 09:26:24 -0700 Subject: [PATCH] terminal: dumpString options --- src/terminal/PageList.zig | 4 ++-- src/terminal/Screen.zig | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/terminal/PageList.zig b/src/terminal/PageList.zig index fbf4b11c1..96a12513c 100644 --- a/src/terminal/PageList.zig +++ b/src/terminal/PageList.zig @@ -2309,7 +2309,7 @@ pub fn pageIterator( } /// Get the top-left of the screen for the given tag. -fn getTopLeft(self: *const PageList, tag: point.Tag) Pin { +pub fn getTopLeft(self: *const PageList, tag: point.Tag) Pin { return switch (tag) { // The full screen or history is always just the first page. .screen, .history => .{ .page = self.pages.first.? }, @@ -2343,7 +2343,7 @@ fn getTopLeft(self: *const PageList, tag: point.Tag) Pin { /// Returns the bottom right of the screen for the given tag. This can /// return null because it is possible that a tag is not in the screen /// (e.g. history does not yet exist). -fn getBottomRight(self: *const PageList, tag: point.Tag) ?Pin { +pub fn getBottomRight(self: *const PageList, tag: point.Tag) ?Pin { return switch (tag) { .screen, .active => last: { const page = self.pages.last.?; diff --git a/src/terminal/Screen.zig b/src/terminal/Screen.zig index 414051fcf..846d312da 100644 --- a/src/terminal/Screen.zig +++ b/src/terminal/Screen.zig @@ -1667,17 +1667,28 @@ pub fn promptPath( return .{ .x = to_x - from_x, .y = to_y - from_y }; } +pub const DumpString = struct { + /// The start and end points of the dump, both inclusive. The x will + /// be ignored and the full row will always be dumped. + tl: Pin, + br: ?Pin = null, + + /// If true, this will unwrap soft-wrapped lines. If false, this will + /// dump the screen as it is visually seen in a rendered window. + unwrap: bool = true, +}; + /// Dump the screen to a string. The writer given should be buffered; /// this function does not attempt to efficiently write and generally writes /// one byte at a time. pub fn dumpString( self: *const Screen, writer: anytype, - tl: point.Point, + opts: DumpString, ) !void { var blank_rows: usize = 0; - var iter = self.pages.rowIterator(.right_down, tl, null); + var iter = opts.tl.rowIterator(.right_down, opts.br); while (iter.next()) |row_offset| { const rac = row_offset.rowAndCell(); const cells = cells: { @@ -1736,6 +1747,8 @@ pub fn dumpString( } } +/// You should use dumpString, this is a restricted version mostly for +/// legacy and convenience reasons for unit tests. pub fn dumpStringAlloc( self: *const Screen, alloc: Allocator, @@ -1743,7 +1756,13 @@ pub fn dumpStringAlloc( ) ![]const u8 { var builder = std.ArrayList(u8).init(alloc); defer builder.deinit(); - try self.dumpString(builder.writer(), tl); + + try self.dumpString(builder.writer(), .{ + .tl = self.pages.getTopLeft(tl), + .br = self.pages.getBottomRight(tl) orelse return error.UnknownPoint, + .unwrap = false, + }); + return try builder.toOwnedSlice(); }