From b62806360b6a566a1791023fc50e51043b097219 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 24 Jun 2024 20:58:11 -0700 Subject: [PATCH] terminal: add test for pagelist to clear styles --- src/terminal/PageList.zig | 50 ++++++++++++++++++++++++++++++++ src/terminal/ref_counted_set.zig | 5 +--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/terminal/PageList.zig b/src/terminal/PageList.zig index 2d2973a03..f5a2817c6 100644 --- a/src/terminal/PageList.zig +++ b/src/terminal/PageList.zig @@ -4765,6 +4765,56 @@ test "PageList clone partial trimmed left" { try testing.expectEqual(@as(usize, 40), s2.totalRows()); } +test "PageList clone partial trimmed left reclaims styles" { + const testing = std.testing; + const alloc = testing.allocator; + + var s = try init(alloc, 80, 20, null); + defer s.deinit(); + try testing.expectEqual(@as(usize, s.rows), s.totalRows()); + try s.growRows(30); + + // Style the rows we're trimming + { + try testing.expect(s.pages.first == s.pages.last); + const page = &s.pages.first.?.data; + + const style: stylepkg.Style = .{ .flags = .{ .bold = true } }; + const style_id = try page.styles.add(page.memory, style); + + var it = s.rowIterator(.left_up, .{ .screen = .{} }, .{ .screen = .{ .y = 9 } }); + while (it.next()) |p| { + const rac = p.rowAndCell(); + rac.row.styled = true; + rac.cell.* = .{ + .content_tag = .codepoint, + .content = .{ .codepoint = 'A' }, + .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); + + // Expect to have one style + try testing.expectEqual(1, page.styles.count()); + } + + var s2 = try s.clone(.{ + .top = .{ .screen = .{ .y = 10 } }, + .memory = .{ .alloc = alloc }, + }); + defer s2.deinit(); + try testing.expectEqual(@as(usize, 40), s2.totalRows()); + + { + try testing.expect(s2.pages.first == s2.pages.last); + const page = &s2.pages.first.?.data; + try testing.expectEqual(0, page.styles.count()); + } +} + test "PageList clone partial trimmed both" { const testing = std.testing; const alloc = testing.allocator; diff --git a/src/terminal/ref_counted_set.zig b/src/terminal/ref_counted_set.zig index ae434a149..c6cf12db5 100644 --- a/src/terminal/ref_counted_set.zig +++ b/src/terminal/ref_counted_set.zig @@ -339,10 +339,7 @@ pub fn RefCountedSet( assert(item.meta.ref > 0); item.meta.ref -= 1; - - if (item.meta.ref == 0) { - self.living -= 1; - } + if (item.meta.ref == 0) self.living -= 1; } /// Release a specified number of references to an item by its ID.