tty: enable writing to tty and add smcup in example

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
Tim Culverhouse 2024-01-19 10:58:14 -06:00
parent 76b2bf7bbc
commit 8c8caf4769
3 changed files with 37 additions and 2 deletions

View file

@ -19,6 +19,8 @@ pub fn main() !void {
try vx.start();
defer vx.stop();
try vx.enterAltScreen();
outer: while (true) {
const event = vx.nextEvent();
log.debug("event: {}\r\n", .{event});

View file

@ -167,6 +167,13 @@ pub fn run(
}
}
/// write to the tty
//
// TODO: buffer the writes
pub fn write(self: *Tty, bytes: []const u8) !usize {
return os.write(self.fd, bytes);
}
/// makeRaw enters the raw state for the terminal.
pub fn makeRaw(fd: os.fd_t) !os.termios {
const state = try os.tcgetattr(fd);
@ -218,6 +225,7 @@ fn ior(inout: u32, group: usize, num: usize, len: usize) usize {
return (inout | ((len & IOCPARM_MASK) << 16) | ((group) << 8) | (num));
}
/// The size of the terminal screen
pub const Winsize = struct {
rows: usize,
cols: usize,

View file

@ -1,6 +1,7 @@
const std = @import("std");
const queue = @import("queue.zig");
const Queue = @import("queue.zig").Queue;
const ctlseqs = @import("ctlseqs.zig");
const Tty = @import("Tty.zig");
const Key = @import("Key.zig");
const Screen = @import("Screen.zig");
@ -27,18 +28,21 @@ pub fn Vaxis(comptime T: type) type {
/// the event queue for Vaxis
//
// TODO: is 512 ok?
queue: queue.Queue(T, 512),
queue: Queue(T, 512),
tty: ?Tty,
screen: Screen,
alt_screen: bool,
/// Initialize Vaxis with runtime options
pub fn init(_: Options) !Self {
return Self{
.queue = .{},
.tty = null,
.screen = Screen.init(),
.alt_screen = false,
};
}
@ -49,6 +53,9 @@ pub fn Vaxis(comptime T: type) type {
pub fn deinit(self: *Self, alloc: ?std.mem.Allocator) void {
if (self.tty) |_| {
var tty = &self.tty.?;
if (self.alt_screen) {
_ = tty.write(ctlseqs.rmcup) catch {};
}
tty.deinit();
}
if (alloc) |a| self.screen.deinit(a);
@ -98,6 +105,24 @@ pub fn Vaxis(comptime T: type) type {
.screen = &self.screen,
};
}
pub fn enterAltScreen(self: *Self) !void {
if (self.tty) |_| {
var tty = &self.tty.?;
_ = try tty.write(ctlseqs.smcup);
self.alt_screen = true;
}
}
pub fn exitaltScreen(self: *Self) !void {
if (self.tty) |_| {
if (!self.alt_screen) return;
var tty = &self.tty.?;
_ = try tty.write(ctlseqs.rmcup);
self.alt_screen = false;
}
}
};
}