vaxis: send winsize at run, initialize screen to default cells

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
Tim Culverhouse 2024-01-19 11:30:00 -06:00
parent 08d2aae5fe
commit bef5276340
7 changed files with 34 additions and 14 deletions

View file

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const vaxis = @import("vaxis"); const vaxis = @import("vaxis");
const Cell = vaxis.Cell;
const log = std.log.scoped(.main); const log = std.log.scoped(.main);
pub fn main() !void { pub fn main() !void {
@ -21,6 +22,7 @@ pub fn main() !void {
try vx.enterAltScreen(); try vx.enterAltScreen();
const msg = "Hello, world!";
outer: while (true) { outer: while (true) {
const event = vx.nextEvent(); const event = vx.nextEvent();
log.debug("event: {}\r\n", .{event}); log.debug("event: {}\r\n", .{event});
@ -37,12 +39,17 @@ pub fn main() !void {
} }
const win = vx.window(); const win = vx.window();
_ = win; const child = win.initChild(20, 20, .expand, .expand);
for (msg, 0..) |_, i| {
const cell: Cell = .{ .char = .{ .grapheme = msg[i .. i + 1] } };
child.writeCell(cell, i, 0);
}
try vx.render();
} }
} }
const Event = union(enum) { const Event = union(enum) {
key_press: vaxis.Key, key_press: vaxis.Key,
winsize: vaxis.Winsize, winsize: vaxis.Winsize,
mouse: u8, foo: u8,
}; };

View file

@ -24,6 +24,9 @@ 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 {
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| {
self.buf[i] = .{};
}
self.width = w; self.width = w;
self.height = h; self.height = h;
} }

View file

@ -60,7 +60,7 @@ pub fn run(
// get our initial winsize // get our initial winsize
const winsize = try getWinsize(self.fd); const winsize = try getWinsize(self.fd);
log.debug("{}", .{winsize}); vx.postEvent(.{ .winsize = winsize });
// assign the write end of the pipe to our quit_fd // assign the write end of the pipe to our quit_fd
self.quit_fd = pipe[1]; self.quit_fd = pipe[1];

View file

@ -25,7 +25,7 @@ screen: *Screen,
/// parent's size. Windows do not retain a reference to their parent and are /// parent's size. Windows do not retain a reference to their parent and are
/// unaware of resizes. /// unaware of resizes.
pub fn initChild( pub fn initChild(
self: *Window, self: Window,
x_off: usize, x_off: usize,
y_off: usize, y_off: usize,
width: Size, width: Size,
@ -60,7 +60,7 @@ 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, cell: Cell, row: usize, col: usize) void {
if (self.h < row or self.w < 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(cell, row + self.y_off, col + self.x_off);
} }

View file

@ -9,6 +9,7 @@ pub const csi_u_push = "\x1b[>{d}u";
pub const csi_u_pop = "\x1b[<u"; pub const csi_u_pop = "\x1b[<u";
// Cursor // Cursor
pub const home = "\x1b[H";
pub const cup = "\x1b[{d};{d}H"; pub const cup = "\x1b[{d};{d}H";
// alt screen // alt screen

View file

@ -1,6 +1,9 @@
pub const Vaxis = @import("vaxis.zig").Vaxis; pub const Vaxis = @import("vaxis.zig").Vaxis;
pub const Options = @import("Options.zig"); pub const Options = @import("Options.zig");
const cell = @import("cell.zig");
pub const Cell = cell.Cell;
pub const Key = @import("Key.zig"); pub const Key = @import("Key.zig");
pub const Winsize = @import("Tty.zig").Winsize; pub const Winsize = @import("Tty.zig").Winsize;

View file

@ -110,21 +110,27 @@ pub fn Vaxis(comptime T: type) type {
/// be exited if calling deinit while in the alt screen /// be exited if calling deinit while in the alt screen
pub fn enterAltScreen(self: *Self) !void { pub fn enterAltScreen(self: *Self) !void {
if (self.alt_screen) return; if (self.alt_screen) return;
if (self.tty) |_| { var tty = self.tty orelse return;
var tty = &self.tty.?;
_ = try tty.write(ctlseqs.smcup); _ = try tty.write(ctlseqs.smcup);
self.alt_screen = true; self.alt_screen = true;
} }
}
/// exit the alternate screen /// exit the alternate screen
pub fn exitaltScreen(self: *Self) !void { pub fn exitaltScreen(self: *Self) !void {
if (!self.alt_screen) return; if (!self.alt_screen) return;
if (self.tty) |_| { var tty = self.tty orelse return;
var tty = &self.tty.?;
_ = try tty.write(ctlseqs.rmcup); _ = try tty.write(ctlseqs.rmcup);
self.alt_screen = false; self.alt_screen = false;
} }
pub fn render(self: *Self) !void {
log.debug("HERE", .{});
var tty = self.tty orelse return;
_ = try tty.write(ctlseqs.home);
for (self.screen.buf) |cell| {
_ = try tty.write(cell.char.grapheme);
}
} }
}; };
} }