From 37b0d5c69df7873bfd39ab25e64a1c07179b296c Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Tue, 11 Jun 2024 13:59:07 -0500 Subject: [PATCH] widgets(terminal): read full codepoints Only read full codepoints. Some programs will split codepoints between reads, which can mess with our parser (looking at you, btop) --- src/widgets/terminal/Parser.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/widgets/terminal/Parser.zig b/src/widgets/terminal/Parser.zig index cccdde6..77564a5 100644 --- a/src/widgets/terminal/Parser.zig +++ b/src/widgets/terminal/Parser.zig @@ -66,6 +66,17 @@ pub fn parseReader(self: *Parser, buffered: *BufferedReader) !Event { inline fn parseGround(self: *Parser, reader: *BufferedReader) !Event { var buf: [1]u8 = undefined; + { + std.debug.assert(self.buf.items.len > 0); + // Handle first byte + const len = try std.unicode.utf8ByteSequenceLength(self.buf.items[0]); + var i: usize = 1; + while (i < len) : (i += 1) { + const read = try reader.read(&buf); + if (read == 0) return error.EOF; + try self.buf.append(buf[0]); + } + } while (true) { if (reader.start == reader.end) return .{ .print = self.buf.items }; const n = try reader.read(&buf); @@ -78,6 +89,14 @@ inline fn parseGround(self: *Parser, reader: *BufferedReader) !Event { }, else => { try self.buf.append(b); + const len = try std.unicode.utf8ByteSequenceLength(b); + var i: usize = 1; + while (i < len) : (i += 1) { + const read = try reader.read(&buf); + if (read == 0) return error.EOF; + + try self.buf.append(buf[0]); + } }, } }