From b39d4326be5d2bd119716b0f5b33f5c784863b6f Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Fri, 13 Feb 2026 13:27:58 +0100 Subject: [PATCH] mod(app): remove `interupt` method, merged with `stop` --- examples/demo.zig | 5 ++--- examples/direct.zig | 17 ++++++----------- src/app.zig | 22 ++++++++++------------ src/element.zig | 3 --- 4 files changed, 18 insertions(+), 29 deletions(-) diff --git a/examples/demo.zig b/examples/demo.zig index f904ac6..bf64460 100644 --- a/examples/demo.zig +++ b/examples/demo.zig @@ -147,8 +147,8 @@ pub fn main() !void { if (key.eql(.{ .cp = 'c', .mod = .{ .ctrl = true } })) app.quit(); if (key.eql(.{ .cp = 'n', .mod = .{ .ctrl = true } })) { - try app.interrupt(); - renderer.size = .{}; // reset size, such that next resize will cause a full re-draw! + try app.stop(); + defer renderer.clear() catch @panic("could not clear the screen"); defer app.start(.full) catch @panic("could not start app event loop"); var child = std.process.Child.init(&.{"vim"}, allocator); _ = child.spawnAndWait() catch |err| app.postEvent(.{ @@ -157,7 +157,6 @@ pub fn main() !void { .msg = "Spawning $EDITOR failed", }, }); - continue; } }, // NOTE errors could be displayed in another container in case one was received, etc. to provide the user with feedback diff --git a/examples/direct.zig b/examples/direct.zig index a33485c..56b529f 100644 --- a/examples/direct.zig +++ b/examples/direct.zig @@ -1,5 +1,5 @@ const QuitText = struct { - const text = "Press ctrl+c to quit."; + const text = "Press ctrl+c or ctrl+d to quit."; pub fn element(this: *@This()) App.Element { return .{ @@ -142,13 +142,16 @@ pub fn main() !void { // handle events for (0..len) |_| { - const event = app.queue.pop(); + const event = app.queue.drain().?; log.debug("received event: {s}", .{@tagName(event)}); // pre event handling switch (event) { // NOTE draw the character with the ctrl indication and a newline to make sure the rendering stays consistent - .cancel => try renderer.writeCtrlDWithNewline(), + .cancel => { + app.quit(); + break :event; + }, .line => |line| { defer gpa.free(line); log.debug("{s}", .{line}); @@ -165,15 +168,7 @@ pub fn main() !void { .msg = "Container Event handling failed", }, }); - - // post event handling - switch (event) { - .quit => break :event, - else => {}, - } } - // if there are more events to process continue handling them otherwise I can render the next frame - if (app.queue.len() > 0) continue :event; container.resize(&app.model, try renderer.resize()); container.reposition(&app.model, .{}); diff --git a/src/app.zig b/src/app.zig index 0a24060..b493f83 100644 --- a/src/app.zig +++ b/src/app.zig @@ -91,12 +91,11 @@ pub fn App(comptime M: type, comptime E: type) type { pub fn start(this: *@This(), config: TerminalConfiguration) !void { this.config = config; - if (this.thread) |_| return; - - // post init event (as the very first element to be in the queue - event loop) - this.postEvent(.init); if (!this.handler_registered) { + // post init event (as the very first element to be in the queue - event loop) + this.postEvent(.init); + handler_ctx = this; if (config.altScreen) { posix.sigaction(posix.SIG.WINCH, &.{ @@ -105,11 +104,13 @@ pub fn App(comptime M: type, comptime E: type) type { .flags = 0, }, null); } + // only applies to non-raw mode applications posix.sigaction(posix.SIG.CONT, &.{ .handler = .{ .handler = handleCont }, .mask = posix.sigemptyset(), .flags = 0, }, null); + // only applies to non-raw mode applications posix.sigaction(posix.SIG.INT, &.{ .handler = .{ .handler = handleInt }, .mask = posix.sigemptyset(), @@ -131,25 +132,22 @@ pub fn App(comptime M: type, comptime E: type) type { if (this.config.altScreen and this.config.rawMode) try terminal.enableMouseSupport(); } - pub fn interrupt(this: *@This()) !void { + pub fn stop(this: *@This()) !void { this.quit_event.set(); if (this.config.altScreen and this.config.rawMode) try terminal.disableMouseSupport(); if (this.config.saveScreen) try terminal.restoreScreen(); if (this.config.altScreen) try terminal.exitAltScreen(); - if (this.thread) |*thread| { - thread.join(); - this.thread = null; - } - } - pub fn stop(this: *@This()) !void { - try this.interrupt(); if (this.config.hideCursor) try terminal.showCursor(); if (this.config.saveScreen) try terminal.resetCursor(); if (this.termios) |termios| { if (this.config.rawMode) try terminal.disableRawMode(&termios); this.termios = null; } + if (this.thread) |*thread| { + thread.join(); + this.thread = null; + } } /// Quit the application loop. diff --git a/src/element.zig b/src/element.zig index fcda7ee..2db0039 100644 --- a/src/element.zig +++ b/src/element.zig @@ -724,9 +724,6 @@ pub fn TextField(Model: type, Event: type) type { }; } // TextField(Model: type, Event: type) -// TODO features -// - clear input (with and without retaining of capacity) through an public api -// - make handle / content functions public pub fn Input(Model: type, Event: type, Queue: type) fn (meta.FieldEnum(Event)) type { // NOTE the struct is necessary, as otherwise I cannot point to the function I want to return const input_struct = struct {