Update to Zig 0.16.0 (#962)

Signed-off-by: AnErrupTion <anerruption@disroot.org>

## What are the changes about?

Ports the code base to Zig 0.16.0.

## What existing issue does this resolve?

N/A

## Pre-requisites

- [x] I have tested & confirmed the changes work locally
- [x] I have run `zig fmt` throughout my changes

Reviewed-on: https://codeberg.org/fairyglade/ly/pulls/962
This commit is contained in:
AnErrupTion
2026-04-25 17:37:34 +02:00
committed by AnErrupTion
parent eec83179b9
commit 5edf5251f6
26 changed files with 751 additions and 587 deletions

View File

@@ -1,4 +1,5 @@
const std = @import("std");
const Translator = @import("translate_c").Translator;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
@@ -17,15 +18,30 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
const translate_c = b.addTranslateC(.{
.root_source_file = termbox_dep.path("termbox2.h"),
const translate_c_dep = b.dependency("translate_c", .{
.target = target,
.optimize = optimize,
});
translate_c.defineCMacroRaw("TB_IMPL");
translate_c.defineCMacro("TB_OPT_ATTR_W", "32"); // Enable 24-bit color support + styling (32-bit)
const termbox2 = translate_c.addModule("termbox2");
mod.addImport("termbox2", termbox2);
const termbox2: Translator = .init(translate_c_dep, .{
.c_source_file = termbox_dep.path("termbox2.h"),
.target = target,
.optimize = optimize,
});
termbox2.defineCMacro("TB_IMPL", null);
// TODO 0.16.0: Workaround until Aro gets better...
// https://codeberg.org/ziglang/translate-c/issues/319
termbox2.defineCMacro("_XOPEN_SOURCE", "700");
termbox2.defineCMacro("TB_OPT_ATTR_W", "32"); // Enable 24-bit color support + styling (32-bit)
// TODO 0.16.0: Including <fcntl.h> with -OReleaseSafe causes
// __attribute__(__error__()) to be called. Below
// is the workaround.
termbox2.defineCMacro("_FORTIFY_SOURCE", "0");
// TODO 0.16.0: Needed for now
if (target.result.os.tag == .freebsd) {
termbox2.defineCMacro("__BSD_VISIBLE", "1");
}
mod.addImport("termbox2", termbox2.mod);
const mod_tests = b.addTest(.{
.root_module = mod,

View File

@@ -2,14 +2,18 @@
.name = .ly_ui,
.version = "1.0.0",
.fingerprint = 0x8d11bf85a74ec803,
.minimum_zig_version = "0.15.0",
.minimum_zig_version = "0.16.0",
.dependencies = .{
.ly_core = .{
.path = "../ly-core",
},
.termbox2 = .{
.url = "git+https://github.com/AnErrupTion/termbox2?ref=master#496730697c662893eec43192f48ff616c2539da6",
.hash = "N-V-__8AAOEWBQDt5tNdIzIFY6n8DdZsCP-6MyLoNS20wgpA",
.url = "git+https://github.com/AnErrupTion/termbox2?ref=master#c7f241e8888ce243e1748b05c26a42fcfaaad936",
.hash = "N-V-__8AAAUXBQD6Fwpi9m0MBqWXFFaqW5l1lVrJC2Ynj7a-",
},
.translate_c = .{
.url = "git+https://codeberg.org/ziglang/translate-c#7a1a9fdc4ab00835748a6657ecbb835e3d5d45f7",
.hash = "translate_c-0.0.0-Q_BUWvP1BgCjAk6PWv5286tOlvzD9-X-NkuTzh0KxY0Q",
},
},
.paths = .{

View File

@@ -97,6 +97,7 @@ active_widget_index: usize,
pub fn init(
allocator: Allocator,
io: std.Io,
options: InitOptions,
log_file: *LogFile,
random: Random,
@@ -106,9 +107,9 @@ pub fn init(
if (options.full_color) {
_ = termbox.tb_set_output_mode(termbox.TB_OUTPUT_TRUECOLOR);
try log_file.info("tui", "termbox2 set to 24-bit color output mode", .{});
try log_file.info(io, "tui", "termbox2 set to 24-bit color output mode", .{});
} else {
try log_file.info("tui", "termbox2 set to eight-color output mode", .{});
try log_file.info(io, "tui", "termbox2 set to eight-color output mode", .{});
}
_ = termbox.tb_clear();
@@ -119,7 +120,7 @@ pub fn init(
const width: usize = @intCast(termbox.tb_width());
const height: usize = @intCast(termbox.tb_height());
try log_file.info("tui", "screen resolution is {d}x{d}", .{ width, height });
try log_file.info(io, "tui", "screen resolution is {d}x{d}", .{ width, height });
return .{
.log_file = log_file,
@@ -168,6 +169,7 @@ pub fn deinit(self: *TerminalBuffer) void {
pub fn runEventLoop(
self: *TerminalBuffer,
allocator: Allocator,
io: std.Io,
shared_error: SharedError,
layers: [][]*Widget,
active_widget: *Widget,
@@ -176,14 +178,14 @@ pub fn runEventLoop(
inactivity_event_fn: ?*const fn (*anyopaque) anyerror!void,
context: *anyopaque,
) !void {
try self.registerGlobalKeybind("Ctrl+K", &moveCursorUp, self);
try self.registerGlobalKeybind("Up", &moveCursorUp, self);
try self.registerGlobalKeybind(io, "Ctrl+K", &moveCursorUp, self);
try self.registerGlobalKeybind(io, "Up", &moveCursorUp, self);
try self.registerGlobalKeybind("Ctrl+J", &moveCursorDown, self);
try self.registerGlobalKeybind("Down", &moveCursorDown, self);
try self.registerGlobalKeybind(io, "Ctrl+J", &moveCursorDown, self);
try self.registerGlobalKeybind(io, "Down", &moveCursorDown, self);
try self.registerGlobalKeybind("Tab", &wrapCursor, self);
try self.registerGlobalKeybind("Shift+Tab", &wrapCursorReverse, self);
try self.registerGlobalKeybind(io, "Tab", &wrapCursor, self);
try self.registerGlobalKeybind(io, "Shift+Tab", &wrapCursorReverse, self);
defer self.handlable_widgets.deinit(allocator);
@@ -218,6 +220,7 @@ pub fn runEventLoop(
current_widget.handle(null) catch |err| {
shared_error.writeError(error.SetCursorFailed);
try self.log_file.err(
io,
"tui",
"failed to set cursor in active widget '{s}': {s}",
.{ current_widget.display_name, @errorName(err) },
@@ -261,6 +264,7 @@ pub fn runEventLoop(
self.height = TerminalBuffer.getHeight();
try self.log_file.info(
io,
"tui",
"screen resolution updated to {d}x{d}",
.{ self.width, self.height },
@@ -271,6 +275,7 @@ pub fn runEventLoop(
widget.realloc() catch |err| {
shared_error.writeError(error.WidgetReallocationFailed);
try self.log_file.err(
io,
"tui",
"failed to reallocate widget '{s}': {s}",
.{ widget.display_name, @errorName(err) },
@@ -294,6 +299,7 @@ pub fn runEventLoop(
current_widget.handle(key) catch |err| {
shared_error.writeError(error.CurrentWidgetHandlingFailed);
try self.log_file.err(
io,
"tui",
"failed to handle active widget '{s}': {s}",
.{ current_widget.display_name, @errorName(err) },
@@ -390,18 +396,20 @@ pub fn reclaim(self: TerminalBuffer) !void {
pub fn registerKeybind(
self: *TerminalBuffer,
io: std.Io,
keybinds: *KeybindMap,
keybind: []const u8,
callback: KeybindCallbackFn,
context: *anyopaque,
) !void {
const key = try self.parseKeybind(keybind);
const key = try self.parseKeybind(io, keybind);
keybinds.put(key, .{
.callback = callback,
.context = context,
}) catch |err| {
try self.log_file.err(
io,
"tui",
"failed to register keybind {s}: {s}",
.{ keybind, @errorName(err) },
@@ -411,15 +419,16 @@ pub fn registerKeybind(
pub fn registerGlobalKeybind(
self: *TerminalBuffer,
io: std.Io,
keybind: []const u8,
callback: KeybindCallbackFn,
context: *anyopaque,
) !void {
try self.registerKeybind(&self.keybinds, keybind, callback, context);
try self.registerKeybind(io, &self.keybinds, keybind, callback, context);
}
pub fn simulateKeybind(self: *TerminalBuffer, keybind: []const u8) !bool {
const key = try self.parseKeybind(keybind);
pub fn simulateKeybind(self: *TerminalBuffer, io: std.Io, keybind: []const u8) !bool {
const key = try self.parseKeybind(io, keybind);
if (self.keybinds.get(key)) |binding| {
return try @call(
@@ -509,10 +518,13 @@ fn clearBackBuffer() !void {
// Clear the TTY because termbox2 doesn't seem to do it properly
const capability = termbox.global.caps[termbox.TB_CAP_CLEAR_SCREEN];
const capability_slice = std.mem.span(capability);
_ = try std.posix.write(termbox.global.ttyfd, capability_slice);
const result = std.posix.system.write(termbox.global.ttyfd, capability_slice.ptr, capability_slice.len);
if (result != capability_slice.len) return error.PartialClearBackBuffer;
if (result < 0) return error.ClearBackBufferFailed;
}
fn parseKeybind(self: *TerminalBuffer, keybind: []const u8) !keyboard.Key {
fn parseKeybind(self: *TerminalBuffer, io: std.Io, keybind: []const u8) !keyboard.Key {
var key = std.mem.zeroes(keyboard.Key);
var iterator = std.mem.splitScalar(u8, keybind, '+');
@@ -529,6 +541,7 @@ fn parseKeybind(self: *TerminalBuffer, keybind: []const u8) !keyboard.Key {
if (!found) {
try self.log_file.err(
io,
"tui",
"failed to parse key {s} of keybind {s}",
.{ item, keybind },

View File

@@ -29,6 +29,7 @@ keybinds: TerminalBuffer.KeybindMap,
pub fn init(
allocator: Allocator,
io: std.Io,
buffer: *TerminalBuffer,
should_insert: bool,
masked: bool,
@@ -57,11 +58,11 @@ pub fn init(
.keybinds = .init(allocator),
};
try buffer.registerKeybind(&self.keybinds, "Left", &goLeft, self);
try buffer.registerKeybind(&self.keybinds, "Right", &goRight, self);
try buffer.registerKeybind(&self.keybinds, "Delete", &delete, self);
try buffer.registerKeybind(&self.keybinds, "Backspace", &backspace, self);
try buffer.registerKeybind(&self.keybinds, "Ctrl+U", &clearTextEntry, self);
try buffer.registerKeybind(io, &self.keybinds, "Left", &goLeft, self);
try buffer.registerKeybind(io, &self.keybinds, "Right", &goRight, self);
try buffer.registerKeybind(io, &self.keybinds, "Delete", &delete, self);
try buffer.registerKeybind(io, &self.keybinds, "Backspace", &backspace, self);
try buffer.registerKeybind(io, &self.keybinds, "Ctrl+U", &clearTextEntry, self);
return self;
}

View File

@@ -32,6 +32,7 @@ pub fn CyclableLabel(comptime ItemType: type, comptime ChangeItemType: type) typ
pub fn init(
allocator: Allocator,
io: std.Io,
buffer: *TerminalBuffer,
draw_item_fn: DrawItemFn,
change_item_fn: ?ChangeItemFn,
@@ -60,10 +61,10 @@ pub fn CyclableLabel(comptime ItemType: type, comptime ChangeItemType: type) typ
.keybinds = .init(allocator),
};
try buffer.registerKeybind(&self.keybinds, "Left", &goLeft, self);
try buffer.registerKeybind(&self.keybinds, "Ctrl+H", &goLeft, self);
try buffer.registerKeybind(&self.keybinds, "Right", &goRight, self);
try buffer.registerKeybind(&self.keybinds, "Ctrl+L", &goRight, self);
try buffer.registerKeybind(io, &self.keybinds, "Left", &goLeft, self);
try buffer.registerKeybind(io, &self.keybinds, "Ctrl+H", &goLeft, self);
try buffer.registerKeybind(io, &self.keybinds, "Right", &goRight, self);
try buffer.registerKeybind(io, &self.keybinds, "Ctrl+L", &goRight, self);
return self;
}