aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/ncurses.zig
diff options
context:
space:
mode:
authorjjanzen <jjanzen@jjanzen.ca>2025-01-22 10:04:04 -0600
committerjjanzen <jjanzen@jjanzen.ca>2025-01-22 10:04:04 -0600
commit4438558931d634704b1f8d4af698afca3b5c4a1b (patch)
tree19d35be819c463041a8de2c961ec3106fce187f7 /src/frontend/ncurses.zig
parentb7d602f885f992d00ad1ca7d9040782920931293 (diff)
handle errors
Diffstat (limited to 'src/frontend/ncurses.zig')
-rw-r--r--src/frontend/ncurses.zig52
1 files changed, 27 insertions, 25 deletions
diff --git a/src/frontend/ncurses.zig b/src/frontend/ncurses.zig
index bf1447a..9b626b3 100644
--- a/src/frontend/ncurses.zig
+++ b/src/frontend/ncurses.zig
@@ -57,7 +57,7 @@ pub const IO = struct {
return local_win;
}
- fn formatInstruction(self: *IO, pos: i32, key: i32, description: [:0]const u8) void {
+ fn formatInstruction(self: *IO, pos: i32, key: i32, description: [:0]const u8) !void {
const c_description: ?[*:0]const u8 = description.ptr;
if (ncurses.KEY_F0 <= key and key <= ncurses.KEY_F(64)) {
@@ -65,22 +65,22 @@ pub const IO = struct {
} else if (key < 128) {
_ = ncurses.mvwprintw(self.inst, pos, 2, "%c - %s", key, c_description);
} else {
- const str = std.fmt.allocPrintZ(self.allocator, "Invalid key name: {d}", .{key}) catch {
- return;
+ const str = std.fmt.allocPrint(self.allocator, "Invalid key name: {d}", .{key}) catch {
+ return error.OutOfMemory;
};
- self.displayMessage(str);
- self.allocator.free(str);
+ defer self.allocator.free(str);
+ try self.displayMessage(str);
}
}
- fn createPanels(self: *IO) void {
- self.createInstructionPanel();
+ fn createPanels(self: *IO) !void {
+ try self.createInstructionPanel();
self.createMessagePanel();
self.createStatisticsPanel();
self.createMainPanel();
}
- fn handleResize(self: *IO) Action {
+ fn handleResize(self: *IO) !Action {
const lines = @as(usize, @intCast(ncurses.LINES));
const cols = @as(usize, @intCast(ncurses.COLS));
for (0..lines) |i| {
@@ -97,7 +97,7 @@ pub const IO = struct {
if (!self.validTermSize()) {
_ = ncurses.mvprintw(0, 0, "Terminal must be at least %dx%d", @as(i32, @intCast(MIN_WIDTH)), @as(i32, @intCast(MIN_HEIGHT)));
} else {
- self.createPanels();
+ try self.createPanels();
}
_ = ncurses.refresh();
@@ -105,21 +105,21 @@ pub const IO = struct {
return Action.illegal;
}
- fn displayInstructions(self: *IO) void {
- self.formatInstruction(1, KEY_MOVE_LEFT, "move left");
- self.formatInstruction(2, KEY_MOVE_DOWN, "move down");
- self.formatInstruction(3, KEY_MOVE_UP, "move up");
- self.formatInstruction(4, KEY_MOVE_RIGHT, "move right");
- self.formatInstruction(5, KEY_STAIR_DOWN, "move down staircase");
- self.formatInstruction(6, KEY_STAIR_UP, "move up staircase");
- self.formatInstruction(7, KEY_QUIT, "quit");
+ fn displayInstructions(self: *IO) !void {
+ try self.formatInstruction(1, KEY_MOVE_LEFT, "move left");
+ try self.formatInstruction(2, KEY_MOVE_DOWN, "move down");
+ try self.formatInstruction(3, KEY_MOVE_UP, "move up");
+ try self.formatInstruction(4, KEY_MOVE_RIGHT, "move right");
+ try self.formatInstruction(5, KEY_STAIR_DOWN, "move down staircase");
+ try self.formatInstruction(6, KEY_STAIR_UP, "move up staircase");
+ try self.formatInstruction(7, KEY_QUIT, "quit");
_ = ncurses.wrefresh(self.inst);
}
/// Display a message in the message box.
/// 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, str: [:0]const u8) void {
+ pub fn displayMessage(self: *IO, str: []const u8) !void {
if (self.msgs == null) return;
for (1..MESSAGE_PANEL_WIDTH - 1) |i| {
@@ -128,7 +128,7 @@ pub const IO = struct {
}
if (str.len > MESSAGE_PANEL_WIDTH - 2) {
- self.displayMessage("Message too long");
+ try self.displayMessage("Message too long");
return;
}
@@ -160,11 +160,13 @@ pub const IO = struct {
/// An interface for user input and time processing.
/// Waits for the end of a tick and returns a tick action.
/// If input is given before the end of the tick, return that instead.
- pub fn waitForTick(self: *IO) Action {
+ pub fn waitForTick(self: *IO) !Action {
var new = std.time.milliTimestamp();
while (new - self.prev_tick_time <= TICK_MS) {
- const action = self.processInput();
+ const action = self.processInput() catch |err| {
+ return err;
+ };
if (action != Action.illegal) return action;
new = std.time.milliTimestamp();
@@ -174,7 +176,7 @@ pub const IO = struct {
return Action.tick;
}
- fn processInput(self: *IO) Action {
+ fn processInput(self: *IO) !Action {
const ch = ncurses.getch();
if (!self.validTermSize()) {
@@ -194,7 +196,7 @@ pub const IO = struct {
};
}
- fn createInstructionPanel(self: *IO) void {
+ fn createInstructionPanel(self: *IO) !void {
self.inst = self.createNewWin(
INSTRUCTION_PANEL_HEIGHT,
INSTRUCTION_PANEL_WIDTH,
@@ -202,7 +204,7 @@ pub const IO = struct {
@divTrunc(ncurses.COLS - MAIN_PANEL_WIDTH - INSTRUCTION_PANEL_WIDTH, 2) + MAIN_PANEL_WIDTH - 1,
);
if (self.inst != null) {
- self.displayInstructions();
+ try self.displayInstructions();
}
}
@@ -269,7 +271,7 @@ pub const IO = struct {
if (!io.validTermSize()) {
_ = ncurses.mvprintw(0, 0, "Terminal must be at least %dx%d", @as(i32, @intCast(MIN_WIDTH)), @as(i32, @intCast(MIN_HEIGHT)));
} else {
- io.createPanels();
+ try io.createPanels();
}
return io;