From 48e5369f564db06ff2eebd6bbf5ceaf8dc55373a Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Tue, 8 Jul 2025 16:32:20 +0200 Subject: [PATCH] Fix character width calculation Signed-off-by: AnErrupTion --- src/tui/TerminalBuffer.zig | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/tui/TerminalBuffer.zig b/src/tui/TerminalBuffer.zig index ca072f6..7d608c1 100644 --- a/src/tui/TerminalBuffer.zig +++ b/src/tui/TerminalBuffer.zig @@ -207,9 +207,9 @@ pub fn drawColorLabel(text: []const u8, x: usize, y: usize, fg: u32, bg: u32) vo const utf8view = std.unicode.Utf8View.init(text) catch return; var utf8 = utf8view.iterator(); - var i = x; - while (utf8.nextCodepoint()) |codepoint| : (i += 1) { - _ = termbox.tb_set_cell(@intCast(i), yc, codepoint, fg, bg); + var i: c_int = @intCast(x); + while (utf8.nextCodepoint()) |codepoint| : (i += termbox.tb_wcwidth(codepoint)) { + _ = termbox.tb_set_cell(i, yc, codepoint, fg, bg); } } @@ -218,10 +218,10 @@ pub fn drawConfinedLabel(self: TerminalBuffer, text: []const u8, x: usize, y: us const utf8view = std.unicode.Utf8View.init(text) catch return; var utf8 = utf8view.iterator(); - var i: usize = 0; - while (utf8.nextCodepoint()) |codepoint| : (i += 1) { + var i: c_int = @intCast(x); + while (utf8.nextCodepoint()) |codepoint| : (i += termbox.tb_wcwidth(codepoint)) { if (i >= max_length) break; - _ = termbox.tb_set_cell(@intCast(i + x), yc, codepoint, self.fg, self.bg); + _ = termbox.tb_set_cell(i, yc, codepoint, self.fg, self.bg); } } @@ -235,7 +235,8 @@ pub fn drawCharMultiple(self: TerminalBuffer, char: u32, x: usize, y: usize, len pub fn strWidth(str: []const u8) !u8 { const utf8view = try std.unicode.Utf8View.init(str); var utf8 = utf8view.iterator(); - var i: u8 = 0; - while (utf8.nextCodepoint()) |_| i += 1; - return i; + var i: c_int = 0; + while (utf8.nextCodepoint()) |codepoint| i += termbox.tb_wcwidth(codepoint); + + return @intCast(i); }