From b03f80caddc0652bb90f7900cf4682c399da9bc0 Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Thu, 4 Apr 2024 17:44:06 -0400 Subject: [PATCH 1/2] terminal/Screen: improve selectionString page test More readable (smaller) output when failing, catches more edge cases. At the time of this commit, this test is failing. Changed primarily to address the edge case that was missed before. --- src/terminal/Screen.zig | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/terminal/Screen.zig b/src/terminal/Screen.zig index 9b7472531..c9e468bed 100644 --- a/src/terminal/Screen.zig +++ b/src/terminal/Screen.zig @@ -7079,29 +7079,30 @@ test "Screen: selectionString multi-page" { const testing = std.testing; const alloc = testing.allocator; - var s = try init(alloc, 130, 40, 512); + var s = try init(alloc, 1, 3, 2048); defer s.deinit(); - // 512 * "y\n" - var str: [1024]u8 = undefined; - var i: usize = 0; - while (i < str.len) : (i += 2) { - str[i] = 'y'; - str[i + 1] = '\n'; + + const first_page_size = s.pages.pages.first.?.data.capacity.rows; + + // Lazy way to seek to the first page boundary. + for (0..first_page_size - 1) |_| { + try s.testWriteString("\n"); } - try s.testWriteString(&str); + + try s.testWriteString("y\ny\ny"); { const sel = Selection.init( - s.pages.pin(.{ .screen = .{ .x = 0, .y = 0 } }).?, - s.pages.pin(.{ .active = .{ .x = 1, .y = 39 } }).?, + s.pages.pin(.{ .active = .{ .x = 0, .y = 0 } }).?, + s.pages.pin(.{ .active = .{ .x = 0, .y = 2 } }).?, false, ); const contents = try s.selectionString(alloc, .{ .sel = sel, - .trim = true, + .trim = false, }); defer alloc.free(contents); - const expected = str[0..1023]; + const expected = "y\ny\ny"; try testing.expectEqualStrings(expected, contents); } } From be07856647d2b53132211adc82b359cff2b3b054 Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Thu, 4 Apr 2024 17:59:42 -0400 Subject: [PATCH 2/2] fix(terminal/Screen): selectionString multi-page Corrected logic for detecting if the current row is the end of the selection. Previous logic was faulty because when I calculated the current page y incorrectly, not realizing that it was already available as `y`. --- src/terminal/Screen.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/terminal/Screen.zig b/src/terminal/Screen.zig index c9e468bed..b51f10764 100644 --- a/src/terminal/Screen.zig +++ b/src/terminal/Screen.zig @@ -1433,7 +1433,7 @@ pub fn selectionString(self: *Screen, alloc: Allocator, opts: SelectionString) ! } } - const is_final_row = sel_end.page == chunk.page and sel_end.y == chunk.start + row_count; + const is_final_row = chunk.page == sel_end.page and y == sel_end.y; if (!is_final_row and (!row.wrap or sel_ordered.rectangle))