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:
73
src/app.zig
73
src/app.zig
@@ -5,6 +5,7 @@ const terminal = @import("terminal.zig");
|
||||
const mergeTaggedUnions = @import("event.zig").mergeTaggedUnions;
|
||||
const isTaggedUnion = @import("event.zig").isTaggedUnion;
|
||||
|
||||
const Key = @import("key.zig");
|
||||
const SystemEvent = @import("event.zig").SystemEvent;
|
||||
const Queue = @import("queue.zig").Queue;
|
||||
|
||||
@@ -24,53 +25,64 @@ pub fn App(comptime E: type) type {
|
||||
|
||||
queue: Queue(Event, 256) = .{},
|
||||
thread: ?std.Thread = null,
|
||||
quit: std.Thread.ResetEvent = .{},
|
||||
quit_event: std.Thread.ResetEvent = .{},
|
||||
termios: ?std.posix.termios = null,
|
||||
attached_handler: bool = false,
|
||||
|
||||
pub const SignalHandler = struct {
|
||||
context: *anyopaque,
|
||||
callback: *const fn (context: *anyopaque) void,
|
||||
};
|
||||
|
||||
pub fn init() @This() {
|
||||
var winch_act = std.posix.Sigaction{
|
||||
.handler = .{ .handler = @This().handleWinch },
|
||||
.mask = std.posix.empty_sigset,
|
||||
.flags = 0,
|
||||
};
|
||||
std.posix.sigaction(std.posix.SIG.WINCH, &winch_act, null) catch @panic("could not attach signal WINCH");
|
||||
|
||||
return .{};
|
||||
}
|
||||
|
||||
pub fn start(this: *@This()) !void {
|
||||
if (this.thread) |_| return;
|
||||
|
||||
try registerWinch(.{
|
||||
.context = this,
|
||||
.callback = @This().winsizeCallback,
|
||||
});
|
||||
if (!this.attached_handler) {
|
||||
var winch_act = std.posix.Sigaction{
|
||||
.handler = .{ .handler = @This().handleWinch },
|
||||
.mask = std.posix.empty_sigset,
|
||||
.flags = 0,
|
||||
};
|
||||
std.posix.sigaction(std.posix.SIG.WINCH, &winch_act, null) catch @panic("could not attach signal WINCH");
|
||||
|
||||
this.quit.reset();
|
||||
try registerWinch(.{
|
||||
.context = this,
|
||||
.callback = @This().winsizeCallback,
|
||||
});
|
||||
this.attached_handler = true;
|
||||
}
|
||||
|
||||
this.quit_event.reset();
|
||||
this.thread = try std.Thread.spawn(.{}, @This().run, .{this});
|
||||
if (this.termios) |_| return;
|
||||
|
||||
var termios: std.posix.termios = undefined;
|
||||
try terminal.enableRawMode(&termios);
|
||||
this.termios = termios;
|
||||
try terminal.saveScreen();
|
||||
}
|
||||
|
||||
pub fn stop(this: *@This()) !void {
|
||||
if (this.termios) |*termios| {
|
||||
try terminal.disableRawMode(termios);
|
||||
try terminal.restoreScreen();
|
||||
}
|
||||
this.quit.set();
|
||||
pub fn interrupt(this: *@This()) !void {
|
||||
this.quit_event.set();
|
||||
if (this.thread) |thread| {
|
||||
thread.join();
|
||||
this.thread = null;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(this: *@This()) !void {
|
||||
try this.interrupt();
|
||||
if (this.termios) |*termios| {
|
||||
try terminal.disableRawMode(termios);
|
||||
try terminal.restoreScreen();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn quit(this: *@This()) void {
|
||||
this.quit_event.set();
|
||||
this.postEvent(.quit);
|
||||
}
|
||||
|
||||
/// Returns the next available event, blocking until one is available.
|
||||
pub fn nextEvent(this: *@This()) Event {
|
||||
return this.queue.pop();
|
||||
@@ -111,7 +123,7 @@ pub fn App(comptime E: type) type {
|
||||
var buf: [256]u8 = undefined;
|
||||
while (true) {
|
||||
// FIX: I still think that there is a race condition (I'm just waiting 'long' enough)
|
||||
this.quit.timedWait(20 * std.time.ns_per_ms) catch {
|
||||
this.quit_event.timedWait(20 * std.time.ns_per_ms) catch {
|
||||
const read_bytes = try terminal.read(buf[0..]);
|
||||
// escape key presses
|
||||
if (buf[0] == 0x1b and read_bytes > 1) {
|
||||
@@ -121,17 +133,17 @@ pub fn App(comptime E: type) type {
|
||||
}
|
||||
} else {
|
||||
const b = buf[0];
|
||||
const key: terminal.Key = switch (b) {
|
||||
const key: Key = switch (b) {
|
||||
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
|
||||
0x08 => .{ .cp = terminal.Key.backspace },
|
||||
0x09 => .{ .cp = terminal.Key.tab },
|
||||
0x0a, 0x0d => .{ .cp = terminal.Key.enter },
|
||||
0x08 => .{ .cp = Key.backspace },
|
||||
0x09 => .{ .cp = Key.tab },
|
||||
0x0a, 0x0d => .{ .cp = Key.enter },
|
||||
0x01...0x07, 0x0b...0x0c, 0x0e...0x1a => .{ .cp = b + 0x60, .mod = .{ .ctrl = true } },
|
||||
0x1b => escape: {
|
||||
std.debug.assert(read_bytes == 1);
|
||||
break :escape .{ .cp = terminal.Key.escape };
|
||||
break :escape .{ .cp = Key.escape };
|
||||
},
|
||||
0x7f => .{ .cp = terminal.Key.backspace },
|
||||
0x7f => .{ .cp = Key.backspace },
|
||||
else => {
|
||||
var iter = terminal.code_point.Iterator{ .bytes = buf[0..read_bytes] };
|
||||
while (iter.next()) |cp| {
|
||||
@@ -146,7 +158,6 @@ pub fn App(comptime E: type) type {
|
||||
};
|
||||
break;
|
||||
}
|
||||
this.postEvent(.quit);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user