Fix clock string length issues (fixes #716)

Co-authored-by: Plash <plash@noreply.codeberg.org>
Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2025-08-02 13:08:07 +02:00
parent c05c32c5be
commit b35c055e7b
22 changed files with 99 additions and 138 deletions

View File

@@ -106,6 +106,7 @@ pub fn main() !void {
var save: Save = undefined;
var config_load_failed = false;
var can_get_lock_state = true;
var can_draw_clock = true;
if (res.args.help != 0) {
try clap.help(stderr, clap.Help, &params, .{});
@@ -465,15 +466,13 @@ pub fn main() !void {
length += ly_top_str.len + 1;
}
if (config.bigclock != .none and buffer.box_height + (bigclock.HEIGHT + 2) * 2 < buffer.height) draw_big_clock: {
if (config.bigclock != .none and buffer.box_height + (bigclock.HEIGHT + 2) * 2 < buffer.height) {
const format = "%H:%M";
const xo = buffer.width / 2 - @min(buffer.width, (format.len * (bigclock.WIDTH + 1))) / 2;
const yo = (buffer.height - buffer.box_height) / 2 - bigclock.HEIGHT - 2;
var clock_buf: [format.len + 1:0]u8 = undefined;
const clock_str = interop.timeAsString(&clock_buf, format) catch {
break :draw_big_clock;
};
const clock_str = interop.timeAsString(&clock_buf, format);
for (clock_str, 0..) |c, i| {
const clock_cell = bigclock.clockCell(animate, c, buffer.fg, buffer.bg, config.bigclock);
@@ -503,12 +502,16 @@ pub fn main() !void {
}
if (config.clock) |clock| draw_clock: {
var clock_buf: [32:0]u8 = undefined;
const clock_str = interop.timeAsString(&clock_buf, clock) catch {
break :draw_clock;
};
if (!can_draw_clock) break :draw_clock;
if (clock_str.len == 0) return error.FormattedTimeEmpty;
var clock_buf: [64:0]u8 = undefined;
const clock_str = interop.timeAsString(&clock_buf, clock);
if (clock_str.len == 0) {
try info_line.addMessage(lang.err_clock_too_long, config.error_bg, config.error_fg);
can_draw_clock = false;
break :draw_clock;
}
buffer.drawLabel(clock_str, buffer.width - @min(buffer.width, clock_str.len), 0);
}