screen: refactor arg order and fix some math

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
Tim Culverhouse 2024-01-19 12:21:14 -06:00
parent bef5276340
commit 811fbdd2cb
4 changed files with 16 additions and 10 deletions

View file

@ -33,16 +33,16 @@ pub fn main() !void {
} }
}, },
.winsize => |ws| { .winsize => |ws| {
try vx.resize(alloc, ws.rows, ws.cols); try vx.resize(alloc, ws);
}, },
else => {}, else => {},
} }
const win = vx.window(); const win = vx.window();
const child = win.initChild(20, 20, .expand, .expand); const child = win.initChild(win.width / 2 - msg.len / 2, win.height / 2, .expand, .expand);
for (msg, 0..) |_, i| { for (msg, 0..) |_, i| {
const cell: Cell = .{ .char = .{ .grapheme = msg[i .. i + 1] } }; const cell: Cell = .{ .char = .{ .grapheme = msg[i .. i + 1] } };
child.writeCell(cell, i, 0); child.writeCell(i, 0, cell);
} }
try vx.render(); try vx.render();
} }

View file

@ -3,6 +3,8 @@ const assert = std.debug.assert;
const Cell = @import("cell.zig").Cell; const Cell = @import("cell.zig").Cell;
const log = std.log.scoped(.screen);
const Screen = @This(); const Screen = @This();
width: usize, width: usize,
@ -22,6 +24,7 @@ pub fn deinit(self: *Screen, alloc: std.mem.Allocator) void {
} }
pub fn resize(self: *Screen, alloc: std.mem.Allocator, w: usize, h: usize) !void { pub fn resize(self: *Screen, alloc: std.mem.Allocator, w: usize, h: usize) !void {
log.debug("resizing screen: width={d} height={d}", .{ w, h });
alloc.free(self.buf); alloc.free(self.buf);
self.buf = try alloc.alloc(Cell, w * h); self.buf = try alloc.alloc(Cell, w * h);
for (self.buf, 0..) |_, i| { for (self.buf, 0..) |_, i| {
@ -32,7 +35,7 @@ pub fn resize(self: *Screen, alloc: std.mem.Allocator, w: usize, h: usize) !void
} }
/// writes a cell to a location. 0 indexed /// writes a cell to a location. 0 indexed
pub fn writeCell(self: *Screen, cell: Cell, row: usize, col: usize) void { pub fn writeCell(self: *Screen, col: usize, row: usize, cell: Cell) void {
if (self.width < col) { if (self.width < col) {
// column out of bounds // column out of bounds
return; return;
@ -41,7 +44,7 @@ pub fn writeCell(self: *Screen, cell: Cell, row: usize, col: usize) void {
// height out of bounds // height out of bounds
return; return;
} }
const i = (col * self.width) + row; const i = (row * self.width) + col;
assert(i < self.buf.len); assert(i < self.buf.len);
self.buf[i] = cell; self.buf[i] = cell;
} }

View file

@ -3,6 +3,8 @@ const std = @import("std");
const Screen = @import("Screen.zig"); const Screen = @import("Screen.zig");
const Cell = @import("cell.zig").Cell; const Cell = @import("cell.zig").Cell;
const log = std.log.scoped(.window);
const Window = @This(); const Window = @This();
pub const Size = union(enum) { pub const Size = union(enum) {
@ -59,9 +61,10 @@ pub fn initChild(
} }
/// writes a cell to the location in the window /// writes a cell to the location in the window
pub fn writeCell(self: Window, cell: Cell, row: usize, col: usize) void { pub fn writeCell(self: Window, col: usize, row: usize, cell: Cell) void {
if (self.height == 0 or self.width == 0) return;
if (self.height < row or self.width < col) return; if (self.height < row or self.width < col) return;
self.screen.writeCell(cell, row + self.y_off, col + self.x_off); self.screen.writeCell(col + self.x_off, row + self.y_off, cell);
} }
test "Window size set" { test "Window size set" {

View file

@ -3,6 +3,7 @@ const std = @import("std");
const Queue = @import("queue.zig").Queue; const Queue = @import("queue.zig").Queue;
const ctlseqs = @import("ctlseqs.zig"); const ctlseqs = @import("ctlseqs.zig");
const Tty = @import("Tty.zig"); const Tty = @import("Tty.zig");
const Winsize = Tty.Winsize;
const Key = @import("Key.zig"); const Key = @import("Key.zig");
const Screen = @import("Screen.zig"); const Screen = @import("Screen.zig");
const Window = @import("Window.zig"); const Window = @import("Window.zig");
@ -91,8 +92,8 @@ pub fn Vaxis(comptime T: type) type {
/// resize allocates a slice of cellsequal to the number of cells /// resize allocates a slice of cellsequal to the number of cells
/// required to display the screen (ie width x height). Any previous screen is /// required to display the screen (ie width x height). Any previous screen is
/// freed when resizing /// freed when resizing
pub fn resize(self: *Self, alloc: std.mem.Allocator, w: usize, h: usize) !void { pub fn resize(self: *Self, alloc: std.mem.Allocator, winsize: Winsize) !void {
try self.screen.resize(alloc, w, h); try self.screen.resize(alloc, winsize.cols, winsize.rows);
} }
/// returns a Window comprising of the entire terminal screen /// returns a Window comprising of the entire terminal screen
@ -124,7 +125,6 @@ pub fn Vaxis(comptime T: type) type {
} }
pub fn render(self: *Self) !void { pub fn render(self: *Self) !void {
log.debug("HERE", .{});
var tty = self.tty orelse return; var tty = self.tty orelse return;
_ = try tty.write(ctlseqs.home); _ = try tty.write(ctlseqs.home);