feat(io): introduce std.Io parameter for App.init
Some checks failed
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 1m22s

The provided `std.Io` is used for running the input reading and the
rendering asynch. The user can provide their desired `std.Io`
implementation as they wish to use. The examples use `std.Io.Threaded`
as a simple threaded solution.
This commit is contained in:
2025-11-10 16:44:02 +01:00
parent accbb4c97d
commit 79a0d17a66
17 changed files with 354 additions and 277 deletions

View File

@@ -133,7 +133,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -31,8 +31,10 @@ pub fn main() !void {
defer if (gpa.deinit() == .leak) log.err("memory leak", .{}); defer if (gpa.deinit() == .leak) log.err("memory leak", .{});
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(.{}); var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -32,7 +32,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -82,7 +82,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -65,7 +65,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -32,7 +32,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -32,7 +32,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -65,7 +65,10 @@ pub fn main() !void {
} }
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -32,7 +32,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -97,7 +97,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -35,7 +35,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -35,7 +35,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -35,7 +35,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -35,7 +35,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -32,7 +32,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -178,7 +178,10 @@ pub fn main() !void {
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var app: App = .init(.{}); var threaded_io: std.Io.Threaded = .init(allocator);
defer threaded_io.deinit();
var app: App = .init(threaded_io.ioBasic(), .{});
var renderer = zterm.Renderer.Buffered.init(allocator); var renderer = zterm.Renderer.Buffered.init(allocator);
defer renderer.deinit(); defer renderer.deinit();

View File

@@ -16,7 +16,7 @@
/// union(enum) {}, // no additional user event's /// union(enum) {}, // no additional user event's
/// ); /// );
/// // later on create an `App` instance and start the event loop /// // later on create an `App` instance and start the event loop
/// var app: App = .init(.{}); // provide instance of the model that shall be used /// var app: App = .init(io, .{}); // provide instance of the `std.Io` and `App` model that shall be used
/// try app.start(); /// try app.start();
/// defer app.stop() catch unreachable; // does not clean-up the resources used in the model /// defer app.stop() catch unreachable; // does not clean-up the resources used in the model
/// ``` /// ```
@@ -24,10 +24,10 @@ pub fn App(comptime M: type, comptime E: type) type {
if (!isStruct(M)) @compileError("Provided model `M` for `App(comptime M: type, comptime E: type)` is not of type `struct`"); if (!isStruct(M)) @compileError("Provided model `M` for `App(comptime M: type, comptime E: type)` is not of type `struct`");
if (!isTaggedUnion(E)) @compileError("Provided user event `E` for `App(comptime M: type, comptime E: type)` is not of type `union(enum)`."); if (!isTaggedUnion(E)) @compileError("Provided user event `E` for `App(comptime M: type, comptime E: type)` is not of type `union(enum)`.");
return struct { return struct {
io: std.Io,
model: Model, model: Model,
queue: Queue, queue: Queue,
thread: ?Thread = null, future: ?std.Io.Future(@typeInfo(@typeInfo(@TypeOf(run)).@"fn".return_type.?).error_union.error_set!void) = null,
quit_event: Thread.ResetEvent,
termios: ?posix.termios = null, termios: ?posix.termios = null,
winch_registered: bool = false, winch_registered: bool = false,
@@ -41,16 +41,16 @@ pub fn App(comptime M: type, comptime E: type) type {
this.postEvent(.resize); this.postEvent(.resize);
} }
pub fn init(model: Model) @This() { pub fn init(io: std.Io, model: Model) @This() {
return .{ return .{
.io = io,
.model = model, .model = model,
.queue = .{}, .queue = .{},
.quit_event = .unset,
}; };
} }
pub fn start(this: *@This()) !void { pub fn start(this: *@This()) !void {
if (this.thread) |_| return; if (this.future) |_| return;
// post init event (as the very first element to be in the queue - event loop) // post init event (as the very first element to be in the queue - event loop)
this.postEvent(.init); this.postEvent(.init);
@@ -66,8 +66,7 @@ pub fn App(comptime M: type, comptime E: type) type {
this.winch_registered = true; this.winch_registered = true;
} }
this.quit_event.reset(); this.future = this.io.async(run, .{this});
this.thread = try Thread.spawn(.{}, @This().run, .{this});
var termios: posix.termios = undefined; var termios: posix.termios = undefined;
try terminal.enableRawMode(&termios); try terminal.enableRawMode(&termios);
@@ -80,13 +79,12 @@ pub fn App(comptime M: type, comptime E: type) type {
} }
pub fn interrupt(this: *@This()) !void { pub fn interrupt(this: *@This()) !void {
this.quit_event.set();
try terminal.disableMouseSupport(); try terminal.disableMouseSupport();
try terminal.restoreScreen(); try terminal.restoreScreen();
try terminal.exitAltScreen(); try terminal.exitAltScreen();
if (this.thread) |thread| { if (this.future) |*future| {
thread.join(); future.cancel(this.io) catch {};
this.thread = null; this.future = null;
} }
} }
@@ -98,14 +96,17 @@ pub fn App(comptime M: type, comptime E: type) type {
try terminal.restoreScreen(); try terminal.restoreScreen();
try terminal.disableRawMode(&termios); try terminal.disableRawMode(&termios);
try terminal.exitAltScreen(); try terminal.exitAltScreen();
}
this.termios = null; this.termios = null;
} }
}
/// Quit the application loop. /// Quit the application loop.
/// This will stop the internal input thread and post a **.quit** `Event`. /// This will cancel the internal input thread and post a **.quit** `Event`.
pub fn quit(this: *@This()) void { pub fn quit(this: *@This()) void {
this.quit_event.set(); if (this.future) |*future| {
future.cancel(this.io) catch {};
this.future = null;
}
this.postEvent(.quit); this.postEvent(.quit);
} }
@@ -122,12 +123,43 @@ pub fn App(comptime M: type, comptime E: type) type {
fn run(this: *@This()) !void { fn run(this: *@This()) !void {
// thread to read user inputs // thread to read user inputs
var buf: [256]u8 = undefined; var buf: [256]u8 = undefined;
// NOTE set the `NONBLOCK` option for the stdin file, such that reading is not blocking!
{
// TODO is there a better way to do this through the `std.Io` interface?
var fl_flags = posix.fcntl(posix.STDIN_FILENO, posix.F.GETFL, 0) catch |err| switch (err) {
error.FileBusy => unreachable,
error.Locked => unreachable,
error.PermissionDenied => unreachable,
error.DeadLock => unreachable,
error.LockedRegionLimitExceeded => unreachable,
else => |e| return e,
};
fl_flags |= 1 << @bitOffsetOf(posix.system.O, "NONBLOCK");
_ = posix.fcntl(posix.STDIN_FILENO, posix.F.SETFL, fl_flags) catch |err| switch (err) {
error.FileBusy => unreachable,
error.Locked => unreachable,
error.PermissionDenied => unreachable,
error.DeadLock => unreachable,
error.LockedRegionLimitExceeded => unreachable,
else => |e| return e,
};
}
while (true) { while (true) {
// FIX I still think that there is a race condition (I'm just waiting 'long' enough) if (this.io.cancelRequested()) break;
this.quit_event.timedWait(20 * std.time.ns_per_ms) catch {
// FIX in case the queue is full -> the next user input should panic and quit the application? because something seems to clock up the event queue // non-blocking read
const read_bytes = try terminal.read(buf[0..]); const read_bytes = terminal.read(&buf) catch |err| switch (err) {
// TODO `break` should not terminate the reading of the user inputs, but instead only the received faulty input! error.WouldBlock => {
// wait a bit
this.io.sleep(.fromMilliseconds(20), .awake) catch |e| switch (e) {
error.Canceled => break,
else => return e,
};
continue;
},
else => return err,
};
// 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]) {
@@ -154,9 +186,9 @@ pub fn App(comptime M: type, comptime E: type) type {
if (read_bytes < 3) continue; if (read_bytes < 3) continue;
// We start iterating at index 2 to get past the '[' // We start iterating at index 2 to get past the '['
const sequence = for (buf[2..], 2..) |b, i| { const sequence: []u8 = blk: for (buf[2..], 2..) |b, i| {
switch (b) { switch (b) {
0x40...0xFF => break buf[0 .. i + 1], 0x40...0xFF => break :blk buf[0 .. i + 1],
else => continue, else => continue,
} }
} else continue; } else continue;
@@ -287,7 +319,8 @@ pub fn App(comptime M: type, comptime E: type) type {
// const alt = button_mask & mouse_bits.alt > 0; // const alt = button_mask & mouse_bits.alt > 0;
// const ctrl = button_mask & mouse_bits.ctrl > 0; // const ctrl = button_mask & mouse_bits.ctrl > 0;
const mouse: Mouse = .{ this.postEvent(.{
.mouse = .{
.button = button, .button = button,
.x = px -| 1, .x = px -| 1,
.y = py -| 1, .y = py -| 1,
@@ -297,8 +330,8 @@ pub fn App(comptime M: type, comptime E: type) type {
if (sequence[sequence.len - 1] == 'm') break :blk .release; if (sequence[sequence.len - 1] == 'm') break :blk .release;
break :blk .press; break :blk .press;
}, },
}; },
this.postEvent(.{ .mouse = mouse }); });
}, },
'c' => { 'c' => {
// Primary DA (CSI ? Pm c) // Primary DA (CSI ? Pm c)
@@ -387,9 +420,6 @@ pub fn App(comptime M: type, comptime E: type) type {
}; };
this.postEvent(.{ .key = key }); this.postEvent(.{ .key = key });
} }
continue;
};
break;
} }
} }