tty: enable writing to tty and add smcup in example
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
parent
76b2bf7bbc
commit
8c8caf4769
3 changed files with 37 additions and 2 deletions
|
@ -19,6 +19,8 @@ pub fn main() !void {
|
||||||
try vx.start();
|
try vx.start();
|
||||||
defer vx.stop();
|
defer vx.stop();
|
||||||
|
|
||||||
|
try vx.enterAltScreen();
|
||||||
|
|
||||||
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});
|
||||||
|
|
|
@ -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.
|
/// makeRaw enters the raw state for the terminal.
|
||||||
pub fn makeRaw(fd: os.fd_t) !os.termios {
|
pub fn makeRaw(fd: os.fd_t) !os.termios {
|
||||||
const state = try os.tcgetattr(fd);
|
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));
|
return (inout | ((len & IOCPARM_MASK) << 16) | ((group) << 8) | (num));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The size of the terminal screen
|
||||||
pub const Winsize = struct {
|
pub const Winsize = struct {
|
||||||
rows: usize,
|
rows: usize,
|
||||||
cols: usize,
|
cols: usize,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
const std = @import("std");
|
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 Tty = @import("Tty.zig");
|
||||||
const Key = @import("Key.zig");
|
const Key = @import("Key.zig");
|
||||||
const Screen = @import("Screen.zig");
|
const Screen = @import("Screen.zig");
|
||||||
|
@ -27,18 +28,21 @@ pub fn Vaxis(comptime T: type) type {
|
||||||
/// the event queue for Vaxis
|
/// the event queue for Vaxis
|
||||||
//
|
//
|
||||||
// TODO: is 512 ok?
|
// TODO: is 512 ok?
|
||||||
queue: queue.Queue(T, 512),
|
queue: Queue(T, 512),
|
||||||
|
|
||||||
tty: ?Tty,
|
tty: ?Tty,
|
||||||
|
|
||||||
screen: Screen,
|
screen: Screen,
|
||||||
|
|
||||||
|
alt_screen: bool,
|
||||||
|
|
||||||
/// Initialize Vaxis with runtime options
|
/// Initialize Vaxis with runtime options
|
||||||
pub fn init(_: Options) !Self {
|
pub fn init(_: Options) !Self {
|
||||||
return Self{
|
return Self{
|
||||||
.queue = .{},
|
.queue = .{},
|
||||||
.tty = null,
|
.tty = null,
|
||||||
.screen = Screen.init(),
|
.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 {
|
pub fn deinit(self: *Self, alloc: ?std.mem.Allocator) void {
|
||||||
if (self.tty) |_| {
|
if (self.tty) |_| {
|
||||||
var tty = &self.tty.?;
|
var tty = &self.tty.?;
|
||||||
|
if (self.alt_screen) {
|
||||||
|
_ = tty.write(ctlseqs.rmcup) catch {};
|
||||||
|
}
|
||||||
tty.deinit();
|
tty.deinit();
|
||||||
}
|
}
|
||||||
if (alloc) |a| self.screen.deinit(a);
|
if (alloc) |a| self.screen.deinit(a);
|
||||||
|
@ -98,6 +105,24 @@ pub fn Vaxis(comptime T: type) type {
|
||||||
.screen = &self.screen,
|
.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue