Add central Widget struct + clean up code

In particular, move all termbox2 usage to TerminalBuffer.zig &
keyboard.zig

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2026-02-10 00:22:27 +01:00
parent 99dba44e46
commit 207b352888
23 changed files with 1456 additions and 1029 deletions

View File

@@ -1,9 +1,9 @@
const std = @import("std");
const math = std.math;
const Animation = @import("../tui/Animation.zig");
const Cell = @import("../tui/Cell.zig");
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
const Widget = @import("../tui/Widget.zig");
const ColorMix = @This();
@@ -45,14 +45,17 @@ pub fn init(terminal_buffer: *TerminalBuffer, col1: u32, col2: u32, col3: u32) C
};
}
pub fn animation(self: *ColorMix) Animation {
return Animation.init(self, deinit, realloc, draw);
pub fn widget(self: *ColorMix) Widget {
return Widget.init(
self,
null,
null,
draw,
null,
null,
);
}
fn deinit(_: *ColorMix) void {}
fn realloc(_: *ColorMix) anyerror!void {}
fn draw(self: *ColorMix) void {
self.frames +%= 1;
const time: f32 = @as(f32, @floatFromInt(self.frames)) * time_scale;

View File

@@ -1,9 +1,9 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const Animation = @import("../tui/Animation.zig");
const Cell = @import("../tui/Cell.zig");
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
const Widget = @import("../tui/Widget.zig");
const Doom = @This();
@@ -49,15 +49,22 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, top_color: u
};
}
pub fn animation(self: *Doom) Animation {
return Animation.init(self, deinit, realloc, draw);
pub fn widget(self: *Doom) Widget {
return Widget.init(
self,
deinit,
realloc,
draw,
null,
null,
);
}
fn deinit(self: *Doom) void {
self.allocator.free(self.buffer);
}
fn realloc(self: *Doom) anyerror!void {
fn realloc(self: *Doom) !void {
const buffer = try self.allocator.realloc(self.buffer, self.terminal_buffer.width * self.terminal_buffer.height);
initBuffer(buffer, self.terminal_buffer.width);
self.buffer = buffer;

View File

@@ -9,11 +9,11 @@ const LogFile = ly_core.LogFile;
const enums = @import("../enums.zig");
const DurOffsetAlignment = enums.DurOffsetAlignment;
const Animation = @import("../tui/Animation.zig");
const Cell = @import("../tui/Cell.zig");
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
const Color = TerminalBuffer.Color;
const Styling = TerminalBuffer.Styling;
const Widget = @import("../tui/Widget.zig");
fn read_decompress_file(allocator: Allocator, file_path: []const u8) ![]u8 {
const file_buffer = std.fs.cwd().openFile(file_path, .{}) catch {
@@ -403,15 +403,22 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, log_file: *L
};
}
pub fn animation(self: *DurFile) Animation {
return Animation.init(self, deinit, realloc, draw);
pub fn widget(self: *DurFile) Widget {
return Widget.init(
self,
deinit,
realloc,
draw,
null,
null,
);
}
fn deinit(self: *DurFile) void {
self.dur_movie.deinit();
}
fn realloc(self: *DurFile) anyerror!void {
fn realloc(self: *DurFile) !void {
// when terminal size changes, we need to recalculate the start_pos and frame_size based on the new size
self.start_pos = calc_start_position(self.terminal_buffer, &self.dur_movie, self.offset_alignment, self.offset);
self.frame_size = calc_frame_size(self.terminal_buffer, &self.dur_movie);

View File

@@ -1,9 +1,9 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const Animation = @import("../tui/Animation.zig");
const Cell = @import("../tui/Cell.zig");
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
const Widget = @import("../tui/Widget.zig");
const GameOfLife = @This();
@@ -60,8 +60,15 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, fg_color: u3
return game;
}
pub fn animation(self: *GameOfLife) Animation {
return Animation.init(self, deinit, realloc, draw);
pub fn widget(self: *GameOfLife) Widget {
return Widget.init(
self,
deinit,
realloc,
draw,
null,
null,
);
}
fn deinit(self: *GameOfLife) void {
@@ -69,7 +76,7 @@ fn deinit(self: *GameOfLife) void {
self.allocator.free(self.next_grid);
}
fn realloc(self: *GameOfLife) anyerror!void {
fn realloc(self: *GameOfLife) !void {
const new_width = self.terminal_buffer.width;
const new_height = self.terminal_buffer.height;
const new_size = new_width * new_height;

View File

@@ -2,9 +2,9 @@ const std = @import("std");
const Allocator = std.mem.Allocator;
const Random = std.Random;
const Animation = @import("../tui/Animation.zig");
const Cell = @import("../tui/Cell.zig");
const TerminalBuffer = @import("../tui/TerminalBuffer.zig");
const Widget = @import("../tui/Widget.zig");
pub const FRAME_DELAY: usize = 8;
@@ -57,8 +57,15 @@ pub fn init(allocator: Allocator, terminal_buffer: *TerminalBuffer, fg: u32, hea
};
}
pub fn animation(self: *Matrix) Animation {
return Animation.init(self, deinit, realloc, draw);
pub fn widget(self: *Matrix) Widget {
return Widget.init(
self,
deinit,
realloc,
draw,
null,
null,
);
}
fn deinit(self: *Matrix) void {
@@ -66,7 +73,7 @@ fn deinit(self: *Matrix) void {
self.allocator.free(self.lines);
}
fn realloc(self: *Matrix) anyerror!void {
fn realloc(self: *Matrix) !void {
const dots = try self.allocator.realloc(self.dots, self.terminal_buffer.width * (self.terminal_buffer.height + 1));
const lines = try self.allocator.realloc(self.lines, self.terminal_buffer.width);