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.
This commit is contained in:
parent
ef028bfe72
commit
71b8ecc7c2
1 changed files with 4 additions and 1 deletions
|
@ -49,6 +49,8 @@ pub const Buffer = struct {
|
||||||
style_map: StyleMap = .{},
|
style_map: StyleMap = .{},
|
||||||
rows: usize = 0,
|
rows: usize = 0,
|
||||||
cols: 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 {
|
pub fn deinit(self: *@This(), allocator: std.mem.Allocator) void {
|
||||||
self.style_map.deinit(allocator);
|
self.style_map.deinit(allocator);
|
||||||
|
@ -73,7 +75,7 @@ pub const Buffer = struct {
|
||||||
|
|
||||||
/// Appends content to the buffer.
|
/// Appends content to the buffer.
|
||||||
pub fn append(self: *@This(), allocator: std.mem.Allocator, content: Content) !void {
|
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);
|
var iter = grapheme.Iterator.init(content.bytes, content.gd);
|
||||||
const dw: DisplayWidth = .{ .data = content.wd };
|
const dw: DisplayWidth = .{ .data = content.wd };
|
||||||
while (iter.next()) |g| {
|
while (iter.next()) |g| {
|
||||||
|
@ -90,6 +92,7 @@ pub const Buffer = struct {
|
||||||
cols +|= dw.strWidth(cluster);
|
cols +|= dw.strWidth(cluster);
|
||||||
}
|
}
|
||||||
try self.content.appendSlice(allocator, content.bytes);
|
try self.content.appendSlice(allocator, content.bytes);
|
||||||
|
self.last_cols = cols;
|
||||||
self.cols = @max(self.cols, cols);
|
self.cols = @max(self.cols, cols);
|
||||||
self.rows +|= std.mem.count(u8, content.bytes, "\n");
|
self.rows +|= std.mem.count(u8, content.bytes, "\n");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue