aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/frontend/ncurses.zig26
-rw-r--r--src/main.zig20
2 files changed, 32 insertions, 14 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,
diff --git a/src/main.zig b/src/main.zig
index 5a6dbb0..6816231 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -3,25 +3,35 @@ const IO = @import("frontend/ncurses.zig").IO;
const Action = @import("actions.zig").Action;
pub fn main() u8 {
- var io = IO.init() catch |err| {
+ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
+ const allocator = gpa.allocator();
+ var io = IO.init(allocator) catch |err| {
std.log.err("{}", .{err});
return 1;
};
defer io.deinit();
- io.displayMessage("Initialized", .{});
+ io.displayMessage("Initialized");
var action = Action.illegal;
var tick_count: usize = 0;
while (action != Action.exit) {
- io.displayMessage("{}", .{tick_count});
+ var str = std.fmt.allocPrintZ(allocator, "{}", .{tick_count}) catch {
+ return 1;
+ };
+ io.displayMessage(str);
+ allocator.free(str);
action = io.waitForTick();
switch (action) {
Action.tick => {
- io.displayMessage("{}", .{tick_count});
+ str = std.fmt.allocPrintZ(allocator, "{}", .{tick_count}) catch {
+ return 1;
+ };
+ io.displayMessage(str);
+ allocator.free(str);
tick_count += 1;
},
- else => io.displayMessage("{}", .{tick_count}),
+ else => {},
}
}