mod: non blocking read of stdin regardless of raw mode option
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 11m42s

This commit is contained in:
2026-06-03 22:50:24 +02:00
parent cb262aa51f
commit 5831a8e2d2
2 changed files with 10 additions and 7 deletions
+7 -3
View File
@@ -76,8 +76,9 @@ pub fn main(init: std.process.Init) !void {
var app: App = .init(gpa, io, .{}); var app: App = .init(gpa, io, .{});
var stdout = std.Io.File.stdout(); var stdout = std.Io.File.stdout();
var buffer: [4096]u8 = undefined; // var buffer: [4096]u8 = undefined;
var writer = stdout.writerStreaming(io, &buffer); // var writer = stdout.writerStreaming(io, &buffer);
var writer = stdout.writerStreaming(io, &.{});
const w = &writer.interface; const w = &writer.interface;
var renderer = zterm.Renderer.Direct.init(gpa); var renderer = zterm.Renderer.Direct.init(gpa);
@@ -158,7 +159,10 @@ pub fn main(init: std.process.Init) !void {
defer gpa.free(line); defer gpa.free(line);
log.debug("{s}", .{line}); log.debug("{s}", .{line});
if (std.mem.eql(u8, line, "q\n")) try app.quit(); if (std.mem.eql(u8, line, "q\n")) {
try app.quit();
break :event;
}
}, },
// NOTE errors could be displayed in another container in case one was received, etc. to provide the user with feedback // NOTE errors could be displayed in another container in case one was received, etc. to provide the user with feedback
.err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }), .err => |err| log.err("Received {s} with message: {s}", .{ @errorName(err.err), err.msg }),
+3 -4
View File
@@ -28,8 +28,6 @@ pub fn App(comptime M: type, comptime E: type) type {
io: std.Io, io: std.Io,
model: Model, model: Model,
queue: Queue, queue: Queue,
// thread: ?Thread = null,
// quit_event: Thread.ResetEvent,
future: ?std.Io.Future(@typeInfo(@typeInfo(@TypeOf(run)).@"fn".return_type.?).error_union.error_set!void) = null, future: ?std.Io.Future(@typeInfo(@typeInfo(@TypeOf(run)).@"fn".return_type.?).error_union.error_set!void) = null,
termios: ?posix.termios = null, termios: ?posix.termios = null,
handler_registered: bool = false, handler_registered: bool = false,
@@ -152,11 +150,11 @@ pub fn App(comptime M: type, comptime E: type) type {
/// Quit the application loop. /// Quit the application loop.
/// This will cancel 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 {
try this.postEvent(.quit);
if (this.future) |*future| { if (this.future) |*future| {
future.cancel(this.io) catch {}; future.cancel(this.io) catch {};
this.future = null; this.future = null;
} }
try this.postEvent(.quit);
} }
/// Returns the next available event, blocking until one is available. /// Returns the next available event, blocking until one is available.
@@ -173,7 +171,8 @@ pub fn App(comptime M: type, comptime E: type) type {
// thread to read user inputs // thread to read user inputs
var buf: [512]u8 = undefined; var buf: [512]u8 = undefined;
// NOTE set the `NONBLOCK` option for the stdin file, such that reading is not blocking! // NOTE set the `NONBLOCK` option for the stdin file, such that reading is not blocking!
if (this.config.rawMode) { // if (this.config.rawMode) {
{
// TODO is there a better way to do this through the `std.Io` interface? // TODO is there a better way to do this through the `std.Io` interface?
var fl_flags = posix.system.fcntl(posix.STDIN_FILENO, posix.F.GETFL, 0); var fl_flags = posix.system.fcntl(posix.STDIN_FILENO, posix.F.GETFL, 0);
fl_flags |= 1 << @bitOffsetOf(posix.system.O, "NONBLOCK"); fl_flags |= 1 << @bitOffsetOf(posix.system.O, "NONBLOCK");