mod: use unbuffered streaming writer for TUIs
Zig Project Action / Lint, Spell-check and test zig project (push) Failing after 15m36s

This commit is contained in:
2026-06-03 17:39:11 +02:00
parent 24a08e0e62
commit cb262aa51f
18 changed files with 131 additions and 116 deletions
+15 -14
View File
@@ -83,14 +83,14 @@ const InputField = struct {
};
}
fn handle(ctx: *anyopaque, _: *App.Model, event: App.Event) !void {
fn handle(ctx: *anyopaque, io: std.Io, _: *App.Model, event: App.Event) !void {
const this: *@This() = @ptrCast(@alignCast(ctx));
switch (event) {
.key => |key| {
if (key.isUnicode()) try this.input.append(this.allocator, key.cp);
if (key.eql(.{ .cp = zterm.input.Enter }) or key.eql(.{ .cp = zterm.input.KpEnter }))
this.queue.push(.{ .accept = try this.input.toOwnedSlice(this.allocator) });
try this.queue.push(io, .{ .accept = try this.input.toOwnedSlice(this.allocator) });
if (key.eql(.{ .cp = zterm.input.Backspace }))
_ = this.input.pop();
@@ -132,8 +132,9 @@ pub fn main(init: std.process.Init) !void {
var app: App = .init(allocator, io, .{});
var stdout = std.Io.File.stdout();
var stdout_buffer: [4096]u8 = undefined;
var writer = stdout.writerStreaming(io, &stdout_buffer);
// var stdout_buffer: [4096]u8 = undefined;
// var writer = stdout.writerStreaming(io, &stdout_buffer);
var writer = stdout.writerStreaming(io, &.{});
const w = &writer.interface;
defer w.flush() catch |err| log.err("Could not flush: {any}", .{err});
@@ -168,8 +169,8 @@ pub fn main(init: std.process.Init) !void {
}, spinner.element());
try container.append(nested_container);
try app.start(.full);
defer app.stop() catch |err| log.err("Failed to stop application: {any}", .{err});
try app.start(.full, w);
defer app.stop(w) catch |err| log.err("Failed to stop application: {any}", .{err});
var framerate: u64 = 60;
var tick_ms: u64 = @divFloor(time.ms_per_s, framerate);
@@ -177,17 +178,17 @@ pub fn main(init: std.process.Init) !void {
// draw loop
draw: while (true) {
const now_ms: u64 = @intCast(time.milliTimestamp());
const now_ms: u64 = @intCast(std.Io.Timestamp.now(io, .real).toMilliseconds());
if (now_ms >= next_frame_ms) {
next_frame_ms = now_ms + tick_ms;
} else {
std.Thread.sleep((next_frame_ms - now_ms) * time.ns_per_ms);
try io.sleep(.{ .nanoseconds = (next_frame_ms - now_ms) * time.ns_per_ms }, .real);
next_frame_ms += tick_ms;
}
const len = blk: {
app.queue.lock();
defer app.queue.unlock();
try app.queue.lock(io);
defer app.queue.unlock(io);
break :blk app.queue.len();
};
@@ -199,7 +200,7 @@ pub fn main(init: std.process.Init) !void {
switch (event) {
.key => |key| {
log.debug("key {any}", .{key});
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit();
if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) try app.quit();
},
.accept => |input| {
defer allocator.free(input);
@@ -217,7 +218,7 @@ pub fn main(init: std.process.Init) !void {
else => {},
}
container.handle(&app.model, event) catch |err| app.postEvent(.{
container.handle(io, &app.model, event) catch |err| try app.postEvent(.{
.err = .{
.err = err,
.msg = "Container Event handling failed",
@@ -231,10 +232,10 @@ pub fn main(init: std.process.Init) !void {
}
}
container.resize(&app.model, try renderer.resize());
container.resize(&app.model, try renderer.resize(w));
container.reposition(&app.model, .{});
try renderer.render(@TypeOf(container), &container, App.Model, &app.model);
try renderer.flush();
try renderer.flush(w);
}
}