Replace vaxis with zterm #1
123
src/app.zig
123
src/app.zig
@@ -1,11 +1,11 @@
|
|||||||
//! Application type for TUI-applications
|
//! Application type for TUI-applications
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const terminal = @import("terminal.zig");
|
const terminal = @import("terminal.zig");
|
||||||
const code_point = terminal.code_point;
|
|
||||||
const mergeTaggedUnions = @import("event.zig").mergeTaggedUnions;
|
const mergeTaggedUnions = @import("event.zig").mergeTaggedUnions;
|
||||||
const isTaggedUnion = @import("event.zig").isTaggedUnion;
|
const isTaggedUnion = @import("event.zig").isTaggedUnion;
|
||||||
|
|
||||||
const BuiltinEvent = @import("event.zig").BuiltinEvent;
|
const SystemEvent = @import("event.zig").SystemEvent;
|
||||||
const Queue = @import("queue.zig").Queue;
|
const Queue = @import("queue.zig").Queue;
|
||||||
|
|
||||||
const log = std.log.scoped(.app);
|
const log = std.log.scoped(.app);
|
||||||
@@ -18,25 +18,40 @@ pub fn App(comptime E: type) type {
|
|||||||
@compileError("Provided user event `E` for `App(comptime E: type)` is not of type `union(enum)`.");
|
@compileError("Provided user event `E` for `App(comptime E: type)` is not of type `union(enum)`.");
|
||||||
}
|
}
|
||||||
return struct {
|
return struct {
|
||||||
pub const Event = mergeTaggedUnions(BuiltinEvent, E);
|
pub const Event = mergeTaggedUnions(SystemEvent, E);
|
||||||
pub const Layout = @import("layout.zig").Layout(Event);
|
pub const Layout = @import("layout.zig").Layout(Event);
|
||||||
pub const Widget = @import("widget.zig").Widget(Event);
|
pub const Widget = @import("widget.zig").Widget(Event);
|
||||||
|
|
||||||
queue: Queue(Event, 256) = .{},
|
queue: Queue(Event, 256) = .{},
|
||||||
thread: ?std.Thread = null,
|
thread: ?std.Thread = null,
|
||||||
quit: bool = false,
|
quit: std.Thread.ResetEvent = .{},
|
||||||
termios: ?std.posix.termios = null,
|
termios: ?std.posix.termios = null,
|
||||||
|
|
||||||
// TODO: event loop function?
|
pub const SignalHandler = struct {
|
||||||
// layout handling?
|
context: *anyopaque,
|
||||||
|
callback: *const fn (context: *anyopaque) void,
|
||||||
|
};
|
||||||
|
|
||||||
pub fn init() @This() {
|
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 .{};
|
return .{};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(this: *@This()) !void {
|
pub fn start(this: *@This()) !void {
|
||||||
if (this.thread) |_| return;
|
if (this.thread) |_| return;
|
||||||
|
|
||||||
|
try registerWinch(.{
|
||||||
|
.context = this,
|
||||||
|
.callback = @This().winsizeCallback,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.quit.reset();
|
||||||
this.thread = try std.Thread.spawn(.{}, @This().run, .{this});
|
this.thread = try std.Thread.spawn(.{}, @This().run, .{this});
|
||||||
var termios: std.posix.termios = undefined;
|
var termios: std.posix.termios = undefined;
|
||||||
try terminal.enableRawMode(&termios);
|
try terminal.enableRawMode(&termios);
|
||||||
@@ -49,7 +64,7 @@ pub fn App(comptime E: type) type {
|
|||||||
try terminal.disableRawMode(termios);
|
try terminal.disableRawMode(termios);
|
||||||
try terminal.restoreScreen();
|
try terminal.restoreScreen();
|
||||||
}
|
}
|
||||||
this.quit = true;
|
this.quit.set();
|
||||||
if (this.thread) |thread| {
|
if (this.thread) |thread| {
|
||||||
thread.join();
|
thread.join();
|
||||||
this.thread = null;
|
this.thread = null;
|
||||||
@@ -66,46 +81,70 @@ pub fn App(comptime E: type) type {
|
|||||||
this.queue.push(event);
|
this.queue.push(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn winsizeCallback(ptr: *anyopaque) void {
|
||||||
|
const this: *@This() = @ptrCast(@alignCast(ptr));
|
||||||
|
this.postEvent(.{ .resize = terminal.getTerminalSize() });
|
||||||
|
}
|
||||||
|
|
||||||
|
var winch_handler: ?SignalHandler = null;
|
||||||
|
|
||||||
|
fn registerWinch(handler: SignalHandler) !void {
|
||||||
|
if (winch_handler) |_| {
|
||||||
|
@panic("Cannot register another WINCH handler.");
|
||||||
|
}
|
||||||
|
winch_handler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handleWinch(_: c_int) callconv(.C) void {
|
||||||
|
if (winch_handler) |handler| {
|
||||||
|
handler.callback(handler.context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn run(this: *@This()) !void {
|
fn run(this: *@This()) !void {
|
||||||
// thread to read user inputs
|
// send initial terminal size
|
||||||
|
// changes are handled by the winch signal handler (see `init` and `registerWinch`)
|
||||||
const size = terminal.getTerminalSize();
|
const size = terminal.getTerminalSize();
|
||||||
this.postEvent(.{ .resize = size });
|
this.postEvent(.{ .resize = size });
|
||||||
// read input in loop
|
|
||||||
|
// thread to read user inputs
|
||||||
var buf: [256]u8 = undefined;
|
var buf: [256]u8 = undefined;
|
||||||
while (!this.quit) {
|
while (true) {
|
||||||
// FIXME: here is a race-condition -> i.e. there could be events
|
// FIX: I still think that there is a race condition (I'm just waiting 'long' enough)
|
||||||
// in the queue, but they will not be executed because the main
|
this.quit.timedWait(20 * std.time.ns_per_ms) catch {
|
||||||
// loop will close!
|
const read_bytes = try terminal.read(buf[0..]);
|
||||||
const read_bytes = try terminal.read(buf[0..]);
|
// escape key presses
|
||||||
// escape key presses
|
if (buf[0] == 0x1b and read_bytes > 1) {
|
||||||
if (buf[0] == 0x1b and read_bytes > 1) {
|
switch (buf[1]) {
|
||||||
switch (buf[1]) {
|
// TODO: parse corresponding codes
|
||||||
// TODO: parse corresponding codes
|
else => {},
|
||||||
else => {},
|
}
|
||||||
|
} else {
|
||||||
|
const b = buf[0];
|
||||||
|
const key: terminal.Key = switch (b) {
|
||||||
|
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
|
||||||
|
0x08 => .{ .cp = terminal.Key.backspace },
|
||||||
|
0x09 => .{ .cp = terminal.Key.tab },
|
||||||
|
0x0a, 0x0d => .{ .cp = terminal.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 };
|
||||||
|
},
|
||||||
|
0x7f => .{ .cp = terminal.Key.backspace },
|
||||||
|
else => {
|
||||||
|
var iter = terminal.code_point.Iterator{ .bytes = buf[0..read_bytes] };
|
||||||
|
while (iter.next()) |cp| {
|
||||||
|
this.postEvent(.{ .key = .{ .cp = cp.code } });
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
this.postEvent(.{ .key = key });
|
||||||
}
|
}
|
||||||
} else {
|
continue;
|
||||||
const b = buf[0];
|
};
|
||||||
const key: terminal.Key = switch (b) {
|
break;
|
||||||
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
|
|
||||||
0x08 => .{ .cp = terminal.Key.backspace },
|
|
||||||
0x09 => .{ .cp = terminal.Key.tab },
|
|
||||||
0x0a, 0x0d => .{ .cp = terminal.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 };
|
|
||||||
},
|
|
||||||
0x7f => .{ .cp = terminal.Key.backspace },
|
|
||||||
else => {
|
|
||||||
var iter = code_point.Iterator{ .bytes = buf[0..read_bytes] };
|
|
||||||
while (iter.next()) |cp| {
|
|
||||||
this.postEvent(.{ .key = .{ .cp = cp.code } });
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
this.postEvent(.{ .key = key });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this.postEvent(.quit);
|
this.postEvent(.quit);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,27 +3,19 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const terminal = @import("terminal.zig");
|
const terminal = @import("terminal.zig");
|
||||||
|
|
||||||
// Application events which contain information about default application
|
pub const Error = struct {
|
||||||
// parameter. Either `none` or `err`, where `none` represents no event with no
|
err: anyerror,
|
||||||
// message, while `err` represents an error which is propagated. `Widget`s or
|
msg: []const u8,
|
||||||
// `Layout`s may react to the event but should continue throwing the message up
|
|
||||||
// to the application event loop.
|
|
||||||
const ApplicationEvent = union(enum) {
|
|
||||||
none,
|
|
||||||
quit,
|
|
||||||
err: []const u8,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// System events which contain information about events triggered from outside
|
// System events available to every application.
|
||||||
// of the application which impact the application. E.g. the terminal window
|
pub const SystemEvent = union(enum) {
|
||||||
// size has changed, etc.
|
quit,
|
||||||
const SystemEvent = union(enum) {
|
err: Error,
|
||||||
resize: terminal.Size,
|
resize: terminal.Size,
|
||||||
key: terminal.Key,
|
key: terminal.Key,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const BuiltinEvent = mergeTaggedUnions(SystemEvent, ApplicationEvent);
|
|
||||||
|
|
||||||
pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
|
pub fn mergeTaggedUnions(comptime A: type, comptime B: type) type {
|
||||||
if (!isTaggedUnion(A) or !isTaggedUnion(B)) {
|
if (!isTaggedUnion(A) or !isTaggedUnion(B)) {
|
||||||
@compileError("Both types for merging tagged unions need to be of type `union(enum)`.");
|
@compileError("Both types for merging tagged unions need to be of type `union(enum)`.");
|
||||||
|
|||||||
@@ -12,10 +12,10 @@
|
|||||||
//! widgets when deallocated. This means that `deinit()` will also deallocate
|
//! widgets when deallocated. This means that `deinit()` will also deallocate
|
||||||
//! every used widget too.
|
//! every used widget too.
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const lib_event = @import("event.zig");
|
const isTaggedUnion = @import("event.zig").isTaggedUnion;
|
||||||
|
|
||||||
pub fn Layout(comptime Event: type) type {
|
pub fn Layout(comptime Event: type) type {
|
||||||
if (!lib_event.isTaggedUnion(Event)) {
|
if (!isTaggedUnion(Event)) {
|
||||||
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
||||||
}
|
}
|
||||||
return struct {
|
return struct {
|
||||||
@@ -31,12 +31,12 @@ pub fn Layout(comptime Event: type) type {
|
|||||||
object: Ptr = undefined,
|
object: Ptr = undefined,
|
||||||
vtable: *const VTable = undefined,
|
vtable: *const VTable = undefined,
|
||||||
|
|
||||||
// Handle the provided `Event` for this `Widget`.
|
// Handle the provided `Event` for this `Layout`.
|
||||||
pub fn handle(this: *LayoutType, event: Event) !*std.ArrayList(Event) {
|
pub fn handle(this: *LayoutType, event: Event) !*std.ArrayList(Event) {
|
||||||
return try this.vtable.handle(this, event);
|
return try this.vtable.handle(this, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the entire content of this `Widget`.
|
// Return the entire content of this `Layout`.
|
||||||
pub fn content(this: *LayoutType) !*std.ArrayList(u8) {
|
pub fn content(this: *LayoutType) !*std.ArrayList(u8) {
|
||||||
return try this.vtable.content(this);
|
return try this.vtable.content(this);
|
||||||
}
|
}
|
||||||
@@ -51,14 +51,14 @@ pub fn Layout(comptime Event: type) type {
|
|||||||
.object = @intFromPtr(object),
|
.object = @intFromPtr(object),
|
||||||
.vtable = &.{
|
.vtable = &.{
|
||||||
.handle = struct {
|
.handle = struct {
|
||||||
// Handle the provided `Event` for this `Widget`.
|
// Handle the provided `Event` for this `Layout`.
|
||||||
fn handle(this: *LayoutType, event: Event) !*std.ArrayList(Event) {
|
fn handle(this: *LayoutType, event: Event) !*std.ArrayList(Event) {
|
||||||
const layout: @TypeOf(object) = @ptrFromInt(this.object);
|
const layout: @TypeOf(object) = @ptrFromInt(this.object);
|
||||||
return try layout.handle(event);
|
return try layout.handle(event);
|
||||||
}
|
}
|
||||||
}.handle,
|
}.handle,
|
||||||
.content = struct {
|
.content = struct {
|
||||||
// Return the entire content of this `Widget`.
|
// Return the entire content of this `Layout`.
|
||||||
fn content(this: *LayoutType) !*std.ArrayList(u8) {
|
fn content(this: *LayoutType) !*std.ArrayList(u8) {
|
||||||
const layout: @TypeOf(object) = @ptrFromInt(this.object);
|
const layout: @TypeOf(object) = @ptrFromInt(this.object);
|
||||||
return try layout.content();
|
return try layout.content();
|
||||||
|
|||||||
@@ -1,26 +1,27 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const lib_event = @import("../event.zig");
|
const isTaggedUnion = @import("../event.zig").isTaggedUnion;
|
||||||
const widget = @import("../widget.zig");
|
const Error = @import("../event.zig").Error;
|
||||||
|
|
||||||
pub fn Layout(comptime Event: type) type {
|
pub fn Layout(comptime Event: type) type {
|
||||||
if (!lib_event.isTaggedUnion(Event)) {
|
if (!isTaggedUnion(Event)) {
|
||||||
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
||||||
}
|
}
|
||||||
|
const Widget = @import("../widget.zig").Widget(Event);
|
||||||
return struct {
|
return struct {
|
||||||
w: widget.Widget(Event) = undefined,
|
widget: Widget = undefined,
|
||||||
events: std.ArrayList(Event) = undefined,
|
events: std.ArrayList(Event) = undefined,
|
||||||
c: std.ArrayList(u8) = undefined,
|
c: std.ArrayList(u8) = undefined,
|
||||||
|
|
||||||
pub fn init(allocator: std.mem.Allocator, w: widget.Widget(Event)) @This() {
|
pub fn init(allocator: std.mem.Allocator, widget: Widget) @This() {
|
||||||
return .{
|
return .{
|
||||||
.w = w,
|
.widget = widget,
|
||||||
.events = std.ArrayList(Event).init(allocator),
|
.events = std.ArrayList(Event).init(allocator),
|
||||||
.c = std.ArrayList(u8).init(allocator),
|
.c = std.ArrayList(u8).init(allocator),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(this: *@This()) void {
|
pub fn deinit(this: *@This()) void {
|
||||||
this.w.deinit();
|
this.widget.deinit();
|
||||||
this.events.deinit();
|
this.events.deinit();
|
||||||
this.c.deinit();
|
this.c.deinit();
|
||||||
this.* = undefined;
|
this.* = undefined;
|
||||||
@@ -28,14 +29,14 @@ pub fn Layout(comptime Event: type) type {
|
|||||||
|
|
||||||
pub fn handle(this: *@This(), event: Event) !*std.ArrayList(Event) {
|
pub fn handle(this: *@This(), event: Event) !*std.ArrayList(Event) {
|
||||||
this.events.clearRetainingCapacity();
|
this.events.clearRetainingCapacity();
|
||||||
if (this.w.handle(event)) |e| {
|
if (this.widget.handle(event)) |e| {
|
||||||
try this.events.append(e);
|
try this.events.append(e);
|
||||||
}
|
}
|
||||||
return &this.events;
|
return &this.events;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn content(this: *@This()) !*std.ArrayList(u8) {
|
pub fn content(this: *@This()) !*std.ArrayList(u8) {
|
||||||
const widget_content = try this.w.content();
|
const widget_content = try this.widget.content();
|
||||||
this.c.clearRetainingCapacity();
|
this.c.clearRetainingCapacity();
|
||||||
try this.c.appendSlice(widget_content.items);
|
try this.c.appendSlice(widget_content.items);
|
||||||
return &this.c;
|
return &this.c;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const zlog = @import("zlog");
|
|||||||
const App = @import("app.zig").App(union(enum) {});
|
const App = @import("app.zig").App(union(enum) {});
|
||||||
|
|
||||||
pub const std_options = zlog.std_options;
|
pub const std_options = zlog.std_options;
|
||||||
const log = std.log.scoped(.main);
|
const log = std.log.scoped(.default);
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
errdefer |err| log.err("Application Error: {any}", .{err});
|
errdefer |err| log.err("Application Error: {any}", .{err});
|
||||||
@@ -39,7 +39,6 @@ pub fn main() !void {
|
|||||||
const event = app.nextEvent();
|
const event = app.nextEvent();
|
||||||
|
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.none => continue,
|
|
||||||
.quit => break,
|
.quit => break,
|
||||||
.resize => |size| {
|
.resize => |size| {
|
||||||
// NOTE: draw actions should not happen here (still here for testing)
|
// NOTE: draw actions should not happen here (still here for testing)
|
||||||
@@ -52,8 +51,9 @@ pub fn main() !void {
|
|||||||
},
|
},
|
||||||
.key => |key| {
|
.key => |key| {
|
||||||
log.debug("received key: {any}", .{key});
|
log.debug("received key: {any}", .{key});
|
||||||
if (terminal.Key.matches(key, .{ .cp = 'q' })) {
|
// ctrl+c to quit
|
||||||
app.quit = true; // TODO: who should emit the .quit event?
|
if (terminal.Key.matches(key, .{ .cp = 'c', .mod = .{ .ctrl = true } })) {
|
||||||
|
app.quit.set();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => {},
|
else => {},
|
||||||
|
|||||||
@@ -11,10 +11,10 @@
|
|||||||
//! Each `Widget` may cache its content and should if the contents will not
|
//! Each `Widget` may cache its content and should if the contents will not
|
||||||
//! change for a long time.
|
//! change for a long time.
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const lib_event = @import("event.zig");
|
const isTaggedUnion = @import("event.zig").isTaggedUnion;
|
||||||
|
|
||||||
pub fn Widget(comptime Event: type) type {
|
pub fn Widget(comptime Event: type) type {
|
||||||
if (!lib_event.isTaggedUnion(Event)) {
|
if (!isTaggedUnion(Event)) {
|
||||||
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
||||||
}
|
}
|
||||||
return struct {
|
return struct {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const lib_event = @import("../event.zig");
|
const isTaggedUnion = @import("../event.zig").isTaggedUnion;
|
||||||
|
const Error = @import("../event.zig").Error;
|
||||||
|
|
||||||
pub fn Widget(comptime Event: type) type {
|
pub fn Widget(comptime Event: type) type {
|
||||||
if (!lib_event.isTaggedUnion(Event)) {
|
if (!isTaggedUnion(Event)) {
|
||||||
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
@compileError("Provided user event `Event` for `Layout(comptime Event: type)` is not of type `union(enum)`.");
|
||||||
}
|
}
|
||||||
return struct {
|
return struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user