Replace vaxis with zterm (#1)
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 32s

Vaxis sadly cannot be used for applications which want to serve their contents via ssh to the (remote) user.

Instead I wrote my own tui library [zterm](https://gitea.yves-biener.de/yves-biener/zterm), which this tui application now uses. This will serve two purposes:
1. implement the tui application and be servable through ssh (see [wish-serve](https://gitea.yves-biener.de/yves-biener/wish-serve))
2. serve as documentation and showcase of an example application for **zterm**

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2024-11-13 19:52:55 +01:00
parent c62fc6fb43
commit 9515def4fb
9 changed files with 91 additions and 817 deletions

View File

@@ -1,142 +1,98 @@
const std = @import("std");
const vaxis = @import("vaxis");
const zlog = @import("zlog");
const zterm = @import("zterm");
const widget = @import("widget.zig");
const TextInput = vaxis.widgets.TextInput;
const Event = widget.Event;
const App = zterm.App(
union(enum) {},
zterm.Renderer.Direct,
true,
);
const Key = zterm.Key;
const Layout = App.Layout;
const Widget = App.Widget;
pub const std_options = zlog.std_options;
const log = std.log.scoped(.default);
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
errdefer |err| log.err("Application Error: {any}", .{err});
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .{};
defer {
const deinit_status = gpa.deinit();
//fail test; can't try in defer as defer is executed after we return
// fail test; can't try in defer as defer is executed after we return
if (deinit_status == .leak) {
std.log.err("memory leak", .{});
log.err("memory leak", .{});
}
}
const alloc = gpa.allocator();
const allocator = gpa.allocator();
// Initialize a tty
var tty = try vaxis.Tty.init();
defer tty.deinit();
var app: App = .{};
var renderer: App.Renderer = .{};
// Initialize Vaxis
var vx = try vaxis.init(alloc, .{});
// deinit takes an optional allocator. If your program is exiting, you can
// choose to pass a null allocator to save some exit time.
defer vx.deinit(alloc, tty.anyWriter());
var layout = Layout.createFrom(vstack: {
var vstack = Layout.VStack.init(allocator, .{
Layout.createFrom(framing: {
var framing = Layout.Framing.init(allocator, .{
.title = .{
.str = "Welcome to my terminal website",
.style = .{
.ul = .{ .index = 6 },
.ul_style = .single,
},
},
}, .{
.widget = Widget.createFrom(header: {
const doc = try std.fs.cwd().openFile("./doc/home.md", .{});
defer doc.close();
var header = Widget.RawText.init(allocator, doc);
break :header &header;
}),
});
break :framing &framing;
}),
Layout.createFrom(margin: {
var margin = Layout.Margin.init(allocator, .{ .left = 15, .right = 15 }, .{
.widget = Widget.createFrom(body: {
const doc = try std.fs.cwd().openFile("./doc/test.md", .{});
defer doc.close();
var body = Widget.RawText.init(allocator, doc);
break :body &body;
}),
});
break :margin &margin;
}),
});
break :vstack &vstack;
});
defer layout.deinit();
// The event loop requires an intrusive init. We create an instance with
// stable pointers to Vaxis and our TTY, then init the instance. Doing so
// installs a signal handler for SIGWINCH on posix TTYs
//
// This event loop is thread safe. It reads the tty in a separate thread
var loop: vaxis.Loop(Event) = .{
.tty = &tty,
.vaxis = &vx,
};
try loop.init();
// Start the read loop. This puts the terminal in raw mode and begins
// reading user input
try loop.start();
defer loop.stop();
// Optionally enter the alternate screen
try vx.enterAltScreen(tty.anyWriter());
var header = try widget.Header.init(alloc, &vx.unicode);
defer header.deinit();
var view_port = widget.ViewPort.init(alloc, &vx.unicode);
defer view_port.deinit();
var active_menu = false;
var menu = widget.PopupMenu.init(alloc, &vx.unicode);
defer menu.deinit();
// Sends queries to terminal to detect certain features. This should always
// be called after entering the alt screen, if you are using the alt screen
try vx.queryTerminal(tty.anyWriter(), 1 * std.time.ns_per_s);
loop.postEvent(.{ .path = "./doc/home.md" });
try app.start();
defer app.stop() catch unreachable;
// App.Event loop
while (true) {
const event = loop.nextEvent();
// update widgets
header.update(event);
view_port.update(event);
if (active_menu) {
if (menu.update(event)) |e| {
_ = loop.tryPostEvent(e);
active_menu = false;
}
}
const event = app.nextEvent();
switch (event) {
.key_press => |key| {
if (active_menu) {
if (key.matches(vaxis.Key.escape, .{})) {
active_menu = false;
}
}
if (key.matches('c', .{ .ctrl = true })) {
break;
} else if (key.matches(vaxis.Key.space, .{})) {
active_menu = true;
} else if (key.matches('l', .{ .ctrl = true })) {
vx.queueRefresh();
.quit => break,
.key => |key| {
// ctrl+c to quit
if (Key.matches(key, .{ .cp = 'c', .mod = .{ .ctrl = true } })) {
app.quit();
break; // no need to render this frame anyway
}
},
.winsize => |ws| try vx.resize(alloc, tty.anyWriter(), ws),
.err => |err| {
log.err("Received {any} with message: {s}", .{ err.err, err.msg });
},
else => {},
}
var root_window = vx.window();
root_window.clear();
// FIXME: this should not be necessary to clear the contents
try vx.render(tty.anyWriter()); // re-draw after clear!
header.draw(root_window.child(.{
.x_off = 0,
.y_off = 0,
.height = .{ .limit = 3 },
.border = .{ .where = .all },
}));
// should be 120 characters wide and centered horizontally
var view_port_x_off: usize = undefined;
var limit: usize = 120;
if (root_window.width / 2 -| 60 > 0) {
view_port_x_off = root_window.width / 2 -| 60;
} else {
view_port_x_off = 1;
limit = root_window.width - 1;
const events = try layout.handle(event);
for (events.items) |e| {
app.postEvent(e);
}
view_port.draw(root_window.child(.{
.x_off = view_port_x_off,
.y_off = 3,
.width = .{ .limit = limit },
}));
if (active_menu) {
menu.draw(root_window.child(.{
.x_off = root_window.width / 2 -| 25,
.y_off = root_window.height / 2 -| 10,
.width = .{ .limit = 50 },
.height = .{ .limit = 20 },
.border = .{ .where = .all },
}));
}
// Render the screen. Using a buffered writer will offer much better
// performance, but is not required
try vx.render(tty.anyWriter());
try layout.render(&renderer);
}
}