From 71b8ecc7c24ead9ec60929a9e13eb599fdca9998 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Tue, 4 Jun 2024 14:13:48 +0900 Subject: [PATCH] TextView: fix cols when appending to a buffer If appending to a buffer through writer for example, the cols may not be correct if the writes don't end up in a newline (\n). Fix this by keeping track of the cols of previous append. --- src/widgets/TextView.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/widgets/TextView.zig b/src/widgets/TextView.zig index 97f2943..6f63979 100644 --- a/src/widgets/TextView.zig +++ b/src/widgets/TextView.zig @@ -49,6 +49,8 @@ pub const Buffer = struct { style_map: StyleMap = .{}, rows: usize = 0, cols: usize = 0, + // used when appending to a buffer + last_cols: usize = 0, pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void { self.style_map.deinit(allocator); @@ -73,7 +75,7 @@ pub const Buffer = struct { /// Appends content to the buffer. pub fn append(self: *@This(), allocator: std.mem.Allocator, content: Content) !void { - var cols: usize = 0; + var cols: usize = self.last_cols; var iter = grapheme.Iterator.init(content.bytes, content.gd); const dw: DisplayWidth = .{ .data = content.wd }; while (iter.next()) |g| { @@ -90,6 +92,7 @@ pub const Buffer = struct { cols +|= dw.strWidth(cluster); } try self.content.appendSlice(allocator, content.bytes); + self.last_cols = cols; self.cols = @max(self.cols, cols); self.rows +|= std.mem.count(u8, content.bytes, "\n"); }