aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/ncurses.zig
diff options
context:
space:
mode:
authorjjanzen <jjanzen@jjanzen.ca>2025-01-22 09:49:08 -0600
committerjjanzen <jjanzen@jjanzen.ca>2025-01-22 09:49:08 -0600
commitb7d602f885f992d00ad1ca7d9040782920931293 (patch)
tree09ffb317cb4c47aa5a8cc0498edf892573ffc2bc /src/frontend/ncurses.zig
parent900c8756edc3bc547dfd2cd47921ecd453bf703b (diff)
remove generics from io interface
Diffstat (limited to 'src/frontend/ncurses.zig')
-rw-r--r--src/frontend/ncurses.zig26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/frontend/ncurses.zig b/src/frontend/ncurses.zig
index 3d4fbb7..bf1447a 100644
--- a/src/frontend/ncurses.zig
+++ b/src/frontend/ncurses.zig
@@ -31,6 +31,7 @@ const KEY_QUIT: i32 = ncurses.KEY_F(1);
/// Provide an IO struct that manages the state of the display and user input
pub const IO = struct {
+ allocator: std.mem.Allocator,
main: ?*ncurses.WINDOW,
inst: ?*ncurses.WINDOW,
msgs: ?*ncurses.WINDOW,
@@ -64,7 +65,11 @@ pub const IO = struct {
} else if (key < 128) {
_ = ncurses.mvwprintw(self.inst, pos, 2, "%c - %s", key, c_description);
} else {
- self.displayMessage("Invalid key name: {d}", .{key});
+ const str = std.fmt.allocPrintZ(self.allocator, "Invalid key name: {d}", .{key}) catch {
+ return;
+ };
+ self.displayMessage(str);
+ self.allocator.free(str);
}
}
@@ -112,9 +117,9 @@ pub const IO = struct {
}
/// Display a message in the message box.
- /// Takes a format string and arguments.
+ /// Takes a pre-formatted null-terminated string
/// If the message is too wide for the box, display "Message too long" instead.
- pub fn displayMessage(self: *IO, comptime fmt: []const u8, args: anytype) void {
+ pub fn displayMessage(self: *IO, str: [:0]const u8) void {
if (self.msgs == null) return;
for (1..MESSAGE_PANEL_WIDTH - 1) |i| {
@@ -122,12 +127,14 @@ pub const IO = struct {
_ = ncurses.mvwaddch(self.msgs, 1, i_32, ' ');
}
- var buf: [MESSAGE_PANEL_WIDTH:0]u8 = undefined;
- const data = std.fmt.bufPrint(&buf, fmt, args) catch {
- self.displayMessage("Message too long", .{});
+ if (str.len > MESSAGE_PANEL_WIDTH - 2) {
+ self.displayMessage("Message too long");
return;
- };
- buf[data.len] = 0;
+ }
+
+ var buf: [MESSAGE_PANEL_WIDTH:0]u8 = undefined;
+ std.mem.copyForwards(u8, &buf, str);
+ buf[str.len] = 0;
const msg: [:0]u8 = &buf;
@@ -226,10 +233,11 @@ pub const IO = struct {
);
}
- pub fn init() !IO {
+ pub fn init(allocator: std.mem.Allocator) !IO {
_ = locale.setlocale(locale.LC_ALL, "");
var io = IO{
+ .allocator = allocator,
.inst = null,
.msgs = null,
.stat = null,