Properly calculate string lengths

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-02-08 22:04:09 +01:00
parent e9e2d51261
commit 941b7e0dae
8 changed files with 28 additions and 24 deletions

View File

@@ -251,13 +251,16 @@ pub fn drawCharMultiple(
// Every codepoint is assumed to have a width of 1.
// Since Ly is normally running in a TTY, this should be fine.
pub fn strWidth(str: []const u8) !u8 {
const utf8view = try std.unicode.Utf8View.init(str);
pub fn strWidth(str: []const u8) usize {
const utf8view = std.unicode.Utf8View.init(str) catch return str.len;
var utf8 = utf8view.iterator();
var i: c_int = 0;
while (utf8.nextCodepoint()) |codepoint| i += termbox.tb_wcwidth(codepoint);
var length: c_int = 0;
return @intCast(i);
while (utf8.nextCodepoint()) |codepoint| {
length += termbox.tb_wcwidth(codepoint);
}
return @intCast(length);
}
fn clearBackBuffer() !void {

View File

@@ -9,7 +9,7 @@ const MessageLabel = generic.CyclableLabel(Message, Message);
const InfoLine = @This();
const Message = struct {
width: u8,
width: usize,
text: []const u8,
bg: u32,
fg: u32,
@@ -47,7 +47,7 @@ pub fn addMessage(self: *InfoLine, text: []const u8, bg: u32, fg: u32) !void {
if (text.len == 0) return;
try self.label.addItem(.{
.width = try TerminalBuffer.strWidth(text),
.width = TerminalBuffer.strWidth(text),
.text = text,
.bg = bg,
.fg = fg,

View File

@@ -76,7 +76,7 @@ fn sessionChanged(env: Env, maybe_user_list: ?*UserList) void {
fn drawItem(label: *EnvironmentLabel, env: Env, x: usize, y: usize, width: usize) void {
if (width < 3) return;
const length = @min(env.environment.name.len, width - 3);
const length = @min(TerminalBuffer.strWidth(env.environment.name), width - 3);
if (length == 0) return;
const x_offset = if (label.text_in_center and width >= length) (width - length) / 2 else 0;

View File

@@ -123,7 +123,7 @@ pub fn draw(self: Text) void {
if (self.maybe_mask) |mask| {
if (self.width < 1) return;
const length = @min(self.text.items.len, self.width - 1);
const length = @min(TerminalBuffer.strWidth(self.text.items), self.width - 1);
if (length == 0) return;
TerminalBuffer.drawCharMultiple(
@@ -138,11 +138,12 @@ pub fn draw(self: Text) void {
return;
}
const length = @min(self.text.items.len, self.width);
const str_length = TerminalBuffer.strWidth(self.text.items);
const length = @min(str_length, self.width);
if (length == 0) return;
const visible_slice = vs: {
if (self.text.items.len > self.width and self.cursor < self.text.items.len) {
if (str_length > self.width and self.cursor < str_length) {
break :vs self.text.items[self.visible_start..(self.width + self.visible_start)];
} else {
break :vs self.text.items[self.visible_start..];

View File

@@ -98,7 +98,7 @@ fn usernameChanged(user: User, maybe_session: ?*Session) void {
fn drawItem(label: *UserLabel, user: User, x: usize, y: usize, width: usize) void {
if (width < 3) return;
const length = @min(user.name.len, width - 3);
const length = @min(TerminalBuffer.strWidth(user.name), width - 3);
if (length == 0) return;
const x_offset = if (label.text_in_center and width >= length) (width - length) / 2 else 0;

View File

@@ -113,7 +113,7 @@ pub fn BigLabel(comptime ContextType: type) type {
pub fn positionX(self: *Self, original_pos: Position) void {
self.component_pos = original_pos;
self.children_pos = original_pos.addX(self.text.len * CHAR_WIDTH);
self.children_pos = original_pos.addX(TerminalBuffer.strWidth(self.text) * CHAR_WIDTH);
}
pub fn positionY(self: *Self, original_pos: Position) void {
@@ -124,7 +124,7 @@ pub fn BigLabel(comptime ContextType: type) type {
pub fn positionXY(self: *Self, original_pos: Position) void {
self.component_pos = original_pos;
self.children_pos = Position.init(
self.text.len * CHAR_WIDTH,
TerminalBuffer.strWidth(self.text) * CHAR_WIDTH,
CHAR_HEIGHT,
).add(original_pos);
}

View File

@@ -71,7 +71,7 @@ pub fn Label(comptime ContextType: type) type {
pub fn positionX(self: *Self, original_pos: Position) void {
self.component_pos = original_pos;
self.children_pos = original_pos.addX(self.text.len);
self.children_pos = original_pos.addX(TerminalBuffer.strWidth(self.text));
}
pub fn positionY(self: *Self, original_pos: Position) void {
@@ -82,7 +82,7 @@ pub fn Label(comptime ContextType: type) type {
pub fn positionXY(self: *Self, original_pos: Position) void {
self.component_pos = original_pos;
self.children_pos = Position.init(
self.text.len,
TerminalBuffer.strWidth(self.text),
1,
).add(original_pos);
}