update min zig version, readme
This commit is contained in:
parent
612e298d72
commit
e37790904f
3 changed files with 45 additions and 34 deletions
31
README.md
31
README.md
|
@ -13,6 +13,10 @@ features is detected through terminal queries.
|
||||||
|
|
||||||
Contributions are welcome.
|
Contributions are welcome.
|
||||||
|
|
||||||
|
Vaxis tracks zig `master`. Vaxis is currently compatible with zig version
|
||||||
|
`0.12.0-dev.3397+43edd53c3`. When `0.12.0` is released, a release will be tagged
|
||||||
|
against it.
|
||||||
|
|
||||||
## Feature comparison
|
## Feature comparison
|
||||||
|
|
||||||
| Feature | Vaxis | libvaxis | notcurses |
|
| Feature | Vaxis | libvaxis | notcurses |
|
||||||
|
@ -51,8 +55,6 @@ const Cell = vaxis.Cell;
|
||||||
const TextInput = vaxis.widgets.TextInput;
|
const TextInput = vaxis.widgets.TextInput;
|
||||||
const border = vaxis.widgets.border;
|
const border = vaxis.widgets.border;
|
||||||
|
|
||||||
const log = std.log.scoped(.main);
|
|
||||||
|
|
||||||
// This can contain internal events as well as Vaxis events.
|
// This can contain internal events as well as Vaxis events.
|
||||||
// Internal events can be posted into the same queue as vaxis events to allow
|
// Internal events can be posted into the same queue as vaxis events to allow
|
||||||
// for a single event loop with exhaustive switching. Booya
|
// for a single event loop with exhaustive switching. Booya
|
||||||
|
@ -151,17 +153,26 @@ pub fn main() !void {
|
||||||
// vaxis double buffers the screen. This new frame will be compared to
|
// vaxis double buffers the screen. This new frame will be compared to
|
||||||
// the old and only updated cells will be drawn
|
// the old and only updated cells will be drawn
|
||||||
win.clear();
|
win.clear();
|
||||||
const child = win.initChild(
|
|
||||||
win.width / 2 - 20,
|
// Create a style
|
||||||
win.height / 2 - 3,
|
|
||||||
.{ .limit = 40 },
|
|
||||||
.{ .limit = 3 },
|
|
||||||
);
|
|
||||||
// draw the text_input using a bordered window
|
|
||||||
const style: vaxis.Style = .{
|
const style: vaxis.Style = .{
|
||||||
.fg = .{ .index = color_idx },
|
.fg = .{ .index = color_idx },
|
||||||
};
|
};
|
||||||
text_input.draw(border.all(child, style));
|
|
||||||
|
// Create a bordered child window
|
||||||
|
const child = win.child(.{
|
||||||
|
.x_off = win.width / 2 - 20,
|
||||||
|
.y_off = win.height / 2 - 3,
|
||||||
|
.width = .{ .limit = 40 },
|
||||||
|
.height = .{ .limit = 3 },
|
||||||
|
.border = .{
|
||||||
|
.where = .all,
|
||||||
|
.style = .{ .fg = .index = color_idx },
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// Draw the text_input in the child window
|
||||||
|
text_input.draw(child);
|
||||||
|
|
||||||
// Render the screen
|
// Render the screen
|
||||||
try vx.render();
|
try vx.render();
|
||||||
|
|
46
src/Tty.zig
46
src/Tty.zig
|
@ -1,6 +1,6 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const builtin = @import("builtin");
|
const builtin = @import("builtin");
|
||||||
const os = std.os;
|
const posix = std.posix;
|
||||||
const Vaxis = @import("vaxis.zig").Vaxis;
|
const Vaxis = @import("vaxis.zig").Vaxis;
|
||||||
const Parser = @import("Parser.zig");
|
const Parser = @import("Parser.zig");
|
||||||
const GraphemeCache = @import("GraphemeCache.zig");
|
const GraphemeCache = @import("GraphemeCache.zig");
|
||||||
|
@ -10,15 +10,15 @@ const log = std.log.scoped(.tty);
|
||||||
|
|
||||||
const Tty = @This();
|
const Tty = @This();
|
||||||
|
|
||||||
const Writer = std.io.Writer(os.fd_t, os.WriteError, os.write);
|
const Writer = std.io.Writer(posix.fd_t, posix.WriteError, posix.write);
|
||||||
|
|
||||||
const BufferedWriter = std.io.BufferedWriter(4096, Writer);
|
const BufferedWriter = std.io.BufferedWriter(4096, Writer);
|
||||||
|
|
||||||
/// the original state of the terminal, prior to calling makeRaw
|
/// the original state of the terminal, prior to calling makeRaw
|
||||||
termios: os.termios,
|
termios: posix.termios,
|
||||||
|
|
||||||
/// The file descriptor we are using for I/O
|
/// The file descriptor we are using for I/O
|
||||||
fd: os.fd_t,
|
fd: posix.fd_t,
|
||||||
|
|
||||||
should_quit: bool = false,
|
should_quit: bool = false,
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ buffered_writer: BufferedWriter,
|
||||||
/// initializes a Tty instance by opening /dev/tty and "making it raw"
|
/// initializes a Tty instance by opening /dev/tty and "making it raw"
|
||||||
pub fn init() !Tty {
|
pub fn init() !Tty {
|
||||||
// Open our tty
|
// Open our tty
|
||||||
const fd = try os.open("/dev/tty", .{ .ACCMODE = .RDWR }, 0);
|
const fd = try posix.open("/dev/tty", .{ .ACCMODE = .RDWR }, 0);
|
||||||
|
|
||||||
// Set the termios of the tty
|
// Set the termios of the tty
|
||||||
const termios = try makeRaw(fd);
|
const termios = try makeRaw(fd);
|
||||||
|
@ -41,16 +41,16 @@ pub fn init() !Tty {
|
||||||
|
|
||||||
/// release resources associated with the Tty return it to its original state
|
/// release resources associated with the Tty return it to its original state
|
||||||
pub fn deinit(self: *Tty) void {
|
pub fn deinit(self: *Tty) void {
|
||||||
os.tcsetattr(self.fd, .FLUSH, self.termios) catch |err| {
|
posix.tcsetattr(self.fd, .FLUSH, self.termios) catch |err| {
|
||||||
log.err("couldn't restore terminal: {}", .{err});
|
log.err("couldn't restore terminal: {}", .{err});
|
||||||
};
|
};
|
||||||
os.close(self.fd);
|
posix.close(self.fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// stops the run loop
|
/// stops the run loop
|
||||||
pub fn stop(self: *Tty) void {
|
pub fn stop(self: *Tty) void {
|
||||||
self.should_quit = true;
|
self.should_quit = true;
|
||||||
_ = std.os.write(self.fd, ctlseqs.device_status_report) catch {};
|
_ = posix.write(self.fd, ctlseqs.device_status_report) catch {};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// read input from the tty
|
/// read input from the tty
|
||||||
|
@ -73,22 +73,22 @@ pub fn run(
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
var vx_winch: *Vaxis(Event) = undefined;
|
var vx_winch: *Vaxis(Event) = undefined;
|
||||||
var fd: os.fd_t = undefined;
|
var fd: posix.fd_t = undefined;
|
||||||
|
|
||||||
fn init(vx_arg: *Vaxis(Event), fd_arg: os.fd_t) !void {
|
fn init(vx_arg: *Vaxis(Event), fd_arg: posix.fd_t) !void {
|
||||||
vx_winch = vx_arg;
|
vx_winch = vx_arg;
|
||||||
fd = fd_arg;
|
fd = fd_arg;
|
||||||
var act = os.Sigaction{
|
var act = posix.Sigaction{
|
||||||
.handler = .{ .handler = Self.handleWinch },
|
.handler = .{ .handler = Self.handleWinch },
|
||||||
.mask = switch (builtin.os.tag) {
|
.mask = switch (builtin.os.tag) {
|
||||||
.macos => 0,
|
.macos => 0,
|
||||||
.linux => std.os.empty_sigset,
|
.linux => posix.empty_sigset,
|
||||||
else => @compileError("os not supported"),
|
else => @compileError("os not supported"),
|
||||||
},
|
},
|
||||||
.flags = 0,
|
.flags = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
try os.sigaction(os.SIG.WINCH, &act, null);
|
try posix.sigaction(posix.SIG.WINCH, &act, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handleWinch(_: c_int) callconv(.C) void {
|
fn handleWinch(_: c_int) callconv(.C) void {
|
||||||
|
@ -111,7 +111,7 @@ pub fn run(
|
||||||
var buf: [1024]u8 = undefined;
|
var buf: [1024]u8 = undefined;
|
||||||
// read loop
|
// read loop
|
||||||
while (!self.should_quit) {
|
while (!self.should_quit) {
|
||||||
const n = try os.read(self.fd, &buf);
|
const n = try posix.read(self.fd, &buf);
|
||||||
var start: usize = 0;
|
var start: usize = 0;
|
||||||
while (start < n) {
|
while (start < n) {
|
||||||
const result = try parser.parse(buf[start..n]);
|
const result = try parser.parse(buf[start..n]);
|
||||||
|
@ -198,8 +198,8 @@ pub fn flush(self: *Tty) !void {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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: posix.fd_t) !posix.termios {
|
||||||
const state = try os.tcgetattr(fd);
|
const state = try posix.tcgetattr(fd);
|
||||||
var raw = state;
|
var raw = state;
|
||||||
// see termios(3)
|
// see termios(3)
|
||||||
raw.iflag.IGNBRK = false;
|
raw.iflag.IGNBRK = false;
|
||||||
|
@ -222,9 +222,9 @@ pub fn makeRaw(fd: os.fd_t) !os.termios {
|
||||||
raw.cflag.CSIZE = .CS8;
|
raw.cflag.CSIZE = .CS8;
|
||||||
raw.cflag.PARENB = false;
|
raw.cflag.PARENB = false;
|
||||||
|
|
||||||
raw.cc[@intFromEnum(std.posix.V.MIN)] = 1;
|
raw.cc[@intFromEnum(posix.V.MIN)] = 1;
|
||||||
raw.cc[@intFromEnum(std.posix.V.TIME)] = 0;
|
raw.cc[@intFromEnum(posix.V.TIME)] = 0;
|
||||||
try os.tcsetattr(fd, .FLUSH, raw);
|
try posix.tcsetattr(fd, .FLUSH, raw);
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,16 +236,16 @@ pub const Winsize = struct {
|
||||||
y_pixel: usize,
|
y_pixel: usize,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn getWinsize(fd: os.fd_t) !Winsize {
|
fn getWinsize(fd: posix.fd_t) !Winsize {
|
||||||
var winsize = os.winsize{
|
var winsize = posix.winsize{
|
||||||
.ws_row = 0,
|
.ws_row = 0,
|
||||||
.ws_col = 0,
|
.ws_col = 0,
|
||||||
.ws_xpixel = 0,
|
.ws_xpixel = 0,
|
||||||
.ws_ypixel = 0,
|
.ws_ypixel = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
const err = os.system.ioctl(fd, os.T.IOCGWINSZ, @intFromPtr(&winsize));
|
const err = posix.system.ioctl(fd, posix.T.IOCGWINSZ, @intFromPtr(&winsize));
|
||||||
if (os.errno(err) == .SUCCESS)
|
if (posix.errno(err) == .SUCCESS)
|
||||||
return Winsize{
|
return Winsize{
|
||||||
.rows = winsize.ws_row,
|
.rows = winsize.ws_row,
|
||||||
.cols = winsize.ws_col,
|
.cols = winsize.ws_col,
|
||||||
|
|
|
@ -225,7 +225,7 @@ pub fn Vaxis(comptime T: type) type {
|
||||||
pub fn queryTerminal(self: *Self) !void {
|
pub fn queryTerminal(self: *Self) !void {
|
||||||
var tty = self.tty orelse return;
|
var tty = self.tty orelse return;
|
||||||
|
|
||||||
const colorterm = std.os.getenv("COLORTERM") orelse "";
|
const colorterm = std.posix.getenv("COLORTERM") orelse "";
|
||||||
if (std.mem.eql(u8, colorterm, "truecolor") or
|
if (std.mem.eql(u8, colorterm, "truecolor") or
|
||||||
std.mem.eql(u8, colorterm, "24bit"))
|
std.mem.eql(u8, colorterm, "24bit"))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue