From 243c0c1e52c2bf2ec6faa78a470a453c1c0d3c1e Mon Sep 17 00:00:00 2001 From: Titanium Brain Date: Sat, 9 May 2026 19:54:11 +0100 Subject: [PATCH] refactor: address review comments Removes unused code related to calculating frame size. Moves all bounds checks to `TerminalBuffer.setCellBoundsChecked`. --- ly-ui/src/TerminalBuffer.zig | 9 +++------ src/animations/DurFile.zig | 30 ++++-------------------------- 2 files changed, 7 insertions(+), 32 deletions(-) diff --git a/ly-ui/src/TerminalBuffer.zig b/ly-ui/src/TerminalBuffer.zig index 19805ec..1fd9999 100644 --- a/ly-ui/src/TerminalBuffer.zig +++ b/ly-ui/src/TerminalBuffer.zig @@ -381,12 +381,9 @@ pub fn setCell(x: usize, y: usize, cell: Cell) void { ); } -pub fn setCellBoundsChecked(self: *TerminalBuffer, x: usize, y: usize, cell: Cell) void { - const width = self.width; - const height = self.height; - - if (0 <= x and x < width and 0 <= y and y < height) { - cell.put(x, y); +pub fn setCellBoundsChecked(self: *TerminalBuffer, x: isize, y: isize, cell: Cell) void { + if (0 <= x and x < self.width and 0 <= y and y < self.height) { + cell.put(@intCast(x), @intCast(y)); } } diff --git a/src/animations/DurFile.zig b/src/animations/DurFile.zig index e53b049..969157b 100644 --- a/src/animations/DurFile.zig +++ b/src/animations/DurFile.zig @@ -337,7 +337,6 @@ io: std.Io, terminal_buffer: *TerminalBuffer, dur_movie: DurFormat, frames: usize, -frame_size: UVec2, start_pos: IVec2, full_color: bool, animate: *bool, @@ -376,20 +375,6 @@ fn calc_start_position(terminal_buffer: *TerminalBuffer, dur_movie: *DurFormat, return start_pos + offset; } -fn calc_frame_size(terminal_buffer: *TerminalBuffer, dur_movie: *DurFormat) UVec2 { - const buf_width: u32 = @intCast(terminal_buffer.width); - const buf_height: u32 = @intCast(terminal_buffer.height); - - const movie_width: u32 = dur_movie.columns; - const movie_height: u32 = dur_movie.lines; - - // Draw only the needed amount if movie smaller than screen. If movie is bigger, we will just draw entire screen - const frame_width = if (movie_width < buf_width) movie_width else buf_width; - const frame_height = if (movie_height < buf_height) movie_height else buf_height; - - return .{ frame_width, frame_height }; -} - pub fn init( allocator: Allocator, io: std.Io, @@ -480,7 +465,6 @@ pub fn init( const offset: IVec2 = .{ x_offset, y_offset }; const start_pos = calc_start_position(terminal_buffer, &dur_movie, offset_alignment, offset); - const frame_size = calc_frame_size(terminal_buffer, &dur_movie); // Convert dur fps to frames per ms const frame_time: u32 = @trunc(1000 / dur_movie.framerate); @@ -493,7 +477,6 @@ pub fn init( .terminal_buffer = terminal_buffer, .frames = 0, .time_previous = std.Io.Timestamp.now(io, .real).toMilliseconds(), - .frame_size = frame_size, .start_pos = start_pos, .full_color = full_color, .animate = animate, @@ -528,9 +511,8 @@ fn deinit(self: *DurFile) 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 + // when terminal size changes, we need to recalculate the start_pos 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); } fn draw(self: *DurFile) void { @@ -540,17 +522,13 @@ fn draw(self: *DurFile) void { // y is used as an iterator in the durformat, while cell_y gives us the correct placement for the cell (same for x) for (0..@intCast(self.dur_movie.lines)) |y| { - const y_offset_i = @as(i32, @intCast(y)) + self.start_pos[VEC_Y]; - const cell_y: u32 = if (y_offset_i >= 0) @intCast(y_offset_i) else continue; + const cell_y = @as(i32, @intCast(y)) + self.start_pos[VEC_Y]; var iter = std.unicode.Utf8View.initUnchecked(current_frame.contents[y]).iterator(); for (0..@intCast(self.dur_movie.columns)) |x| { - const x_offset_i = @as(i32, @intCast(x)) + self.start_pos[VEC_X]; - const cell_x: u32 = if (x_offset_i >= 0) @intCast(x_offset_i) else { - _ = iter.nextCodepoint().?; - continue; - }; + const cell_x = @as(i32, @intCast(x)) + self.start_pos[VEC_X]; + const codepoint: u21 = iter.nextCodepoint().?; const color_map = current_frame.colorMap[x][y];