terminal: dumpString options

This commit is contained in:
Mitchell Hashimoto
2024-03-13 09:26:24 -07:00
parent 1cdeacea34
commit 5813304358
2 changed files with 24 additions and 5 deletions

View File

@ -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.?;

View File

@ -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();
}