From 54def846bf84627833bede0d0b2c0793b5f9fe61 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Tue, 4 Jun 2024 21:27:50 +0900 Subject: [PATCH] TextView: add Error to buffer operations Typed error sets are nice to have. --- src/widgets/TextView.zig | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/widgets/TextView.zig b/src/widgets/TextView.zig index 6f63979..9a8310d 100644 --- a/src/widgets/TextView.zig +++ b/src/widgets/TextView.zig @@ -43,6 +43,8 @@ pub const Buffer = struct { style: vaxis.Style, }; + pub const Error = error{OutOfMemory}; + grapheme: std.MultiArrayList(grapheme.Grapheme) = .{}, content: std.ArrayListUnmanaged(u8) = .{}, style_list: StyleList = .{}, @@ -67,14 +69,14 @@ pub const Buffer = struct { } /// Replaces contents of the buffer, all previous buffer data is lost. - pub fn update(self: *@This(), allocator: std.mem.Allocator, content: Content) !void { + pub fn update(self: *@This(), allocator: std.mem.Allocator, content: Content) Error!void { self.clear(allocator); errdefer self.clear(allocator); try self.append(allocator, content); } /// 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) Error!void { var cols: usize = self.last_cols; var iter = grapheme.Iterator.init(content.bytes, content.gd); const dw: DisplayWidth = .{ .data = content.wd }; @@ -104,7 +106,7 @@ pub const Buffer = struct { } /// Update style for range of the buffer contents. - pub fn updateStyle(self: *@This(), allocator: std.mem.Allocator, style: Style) !void { + pub fn updateStyle(self: *@This(), allocator: std.mem.Allocator, style: Style) Error!void { const style_index = blk: { for (self.style_list.items, 0..) |s, i| { if (std.meta.eql(s, style.style)) {