add/mod the following features

- split structure for better inclusions
- create PlainRenderer to render contents to the terminal
- simplify events
- clearify what structs are created on the heap and which are on the stack
- quit event is now emitted from the main event loop and not the input loop (see helper function `App.quit`)
- rename several variables and/or functions for easier understanding
- introduce `App.interrupt` to stop the input thread and start a new sub TUI which takes over the entire screen (i.e. 'hx', 'nvim', etc.)
This commit is contained in:
2024-11-06 15:20:34 +01:00
parent 9ddbb19336
commit 9b165e8f81
10 changed files with 320 additions and 186 deletions

View File

@@ -3,6 +3,8 @@ const terminal = @import("terminal.zig");
const zlog = @import("zlog");
const App = @import("app.zig").App(union(enum) {});
const Renderer = @import("render.zig").PlainRenderer();
const Key = @import("key.zig");
pub const std_options = zlog.std_options;
const log = std.log.scoped(.default);
@@ -20,7 +22,8 @@ pub fn main() !void {
}
const allocator = gpa.allocator();
var app = App.init();
var app: App = .{};
var renderer: Renderer = .{};
var rawText = App.Widget.RawText.init(allocator);
const widget = App.Widget.createFrom(&rawText);
@@ -40,29 +43,36 @@ pub fn main() !void {
switch (event) {
.quit => break,
.resize => |size| {
// NOTE: draw actions should not happen here (still here for testing)
// NOTE: clearing the screen and positioning the cursor is only necessary for full screen applications
// - in-line applications should use relative movements instead and should only clear lines (which they draw)
// - in-line applications should not enter the alt screen
try terminal.clearScreen();
try terminal.setCursorPositionHome();
log.debug("Size := [x: {d}, y: {d}]", .{ size.cols, size.rows });
},
.key => |key| {
log.debug("received key: {any}", .{key});
// ctrl+c to quit
if (terminal.Key.matches(key, .{ .cp = 'c', .mod = .{ .ctrl = true } })) {
app.quit.set();
if (Key.matches(key, .{ .cp = 'c', .mod = .{ .ctrl = true } })) {
app.quit();
}
if (Key.matches(key, .{ .cp = 'n', .mod = .{ .ctrl = true } })) {
try app.interrupt();
defer app.start() catch @panic("could not start app event loop");
var child = std.process.Child.init(&.{"hx"}, allocator);
_ = child.spawnAndWait() catch |err| {
app.postEvent(.{
.err = .{
.err = err,
.msg = "Spawning Helix failed",
},
});
};
}
},
.err => |err| {
log.err("Received {any} with message: {s}", .{ err.err, err.msg });
},
else => {},
}
// NOTE: this currently re-renders the screen for every key-press -> which might be a bit of an overkill
const events = try layout.handle(event);
for (events.items) |e| {
app.postEvent(e);
}
log.debug("Layout result: {s}", .{(try layout.content()).items});
try renderer.render(try layout.content());
}
// TODO: I could use the ascii codes in vaxis
// - see https://gist.github.com/ConnerWill/d4b6c776b509add763e17f9f113fd25b