mod(app): SIG.INT handler for Ctrl-C which issues a .cancel event
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 1m28s

This commit is contained in:
2026-02-02 21:28:21 +01:00
parent 6781c43fcc
commit 0bb91cab0d

View File

@@ -71,6 +71,12 @@ pub fn App(comptime M: type, comptime E: type) type {
// -> the signal might not work correctly when hosting the application over ssh! // -> the signal might not work correctly when hosting the application over ssh!
this.postEvent(.resize); this.postEvent(.resize);
} }
/// registered INT handler to catch ctrl-c to send a `.cancel`
fn handleInt(_: i32) callconv(.c) void {
const this: *@This() = @ptrCast(@alignCast(handler_ctx));
// NOTE do not quit the application and instead issue a `.cancel` event
this.postEvent(.cancel);
}
pub fn init(gpa: Allocator, io: std.Io, model: Model) @This() { pub fn init(gpa: Allocator, io: std.Io, model: Model) @This() {
return .{ return .{
@@ -92,16 +98,23 @@ pub fn App(comptime M: type, comptime E: type) type {
if (!this.handler_registered) { if (!this.handler_registered) {
handler_ctx = this; handler_ctx = this;
if (config.altScreen) {
posix.sigaction(posix.SIG.WINCH, &.{ posix.sigaction(posix.SIG.WINCH, &.{
.handler = .{ .handler = handleWinch }, .handler = .{ .handler = handleWinch },
.mask = posix.sigemptyset(), .mask = posix.sigemptyset(),
.flags = 0, .flags = 0,
}, null); }, null);
}
posix.sigaction(posix.SIG.CONT, &.{ posix.sigaction(posix.SIG.CONT, &.{
.handler = .{ .handler = handleCont }, .handler = .{ .handler = handleCont },
.mask = posix.sigemptyset(), .mask = posix.sigemptyset(),
.flags = 0, .flags = 0,
}, null); }, null);
posix.sigaction(posix.SIG.INT, &.{
.handler = .{ .handler = handleInt },
.mask = posix.sigemptyset(),
.flags = 0,
}, null);
this.handler_registered = true; this.handler_registered = true;
} }