vaxis: add panic handler function

Applications may use this to handle panics gracefully
This commit is contained in:
Tim Culverhouse 2024-05-30 09:34:12 -05:00
parent ba4caa0767
commit 04f1586e8f
2 changed files with 30 additions and 1 deletions

View file

@ -1,7 +1,10 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin");
pub const Vaxis = @import("Vaxis.zig"); pub const Vaxis = @import("Vaxis.zig");
pub const Loop = @import("Loop.zig").Loop; pub const Loop = @import("Loop.zig").Loop;
pub const xev = @import("xev.zig");
pub const Queue = @import("queue.zig").Queue; pub const Queue = @import("queue.zig").Queue;
pub const Key = @import("Key.zig"); pub const Key = @import("Key.zig");
@ -22,12 +25,29 @@ pub const Winsize = tty.Winsize;
pub const widgets = @import("widgets.zig"); pub const widgets = @import("widgets.zig");
pub const gwidth = @import("gwidth.zig"); pub const gwidth = @import("gwidth.zig");
pub const ctlseqs = @import("ctlseqs.zig");
/// Initialize a Vaxis application. /// Initialize a Vaxis application.
pub fn init(alloc: std.mem.Allocator, opts: Vaxis.Options) !Vaxis { pub fn init(alloc: std.mem.Allocator, opts: Vaxis.Options) !Vaxis {
return Vaxis.init(alloc, opts); return Vaxis.init(alloc, opts);
} }
/// Resets terminal state on a panic, then calls the default zig panic handler
pub fn panic_handler(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn {
if (Tty.global_tty) |gty| {
const reset: []const u8 = ctlseqs.csi_u_pop ++
ctlseqs.mouse_reset ++
ctlseqs.bp_reset ++
ctlseqs.rmcup;
gty.anyWriter().writeAll(reset) catch {};
gty.deinit();
}
std.builtin.default_panic(msg, error_return_trace, ret_addr);
}
/// the vaxis logo. In PixelCode /// the vaxis logo. In PixelCode
pub const logo = pub const logo =
\\▄ ▄ ▄▄▄ ▄ ▄ ▄▄▄ ▄▄▄ \\▄ ▄ ▄▄▄ ▄ ▄ ▄▄▄ ▄▄▄

View file

@ -35,6 +35,11 @@ pub const PosixTty = struct {
var handler_mutex: std.Thread.Mutex = .{}; var handler_mutex: std.Thread.Mutex = .{};
var handler_idx: usize = 0; var handler_idx: usize = 0;
/// global tty instance, used in case of a panic. Not guaranteed to work if
/// for some reason there are multiple TTYs open under a single vaxis
/// compilation unit - but this is better than nothing
pub var global_tty: ?PosixTty = null;
/// initializes a Tty instance by opening /dev/tty and "making it raw". A /// initializes a Tty instance by opening /dev/tty and "making it raw". A
/// signal handler is installed for SIGWINCH. No callbacks are installed, be /// signal handler is installed for SIGWINCH. No callbacks are installed, be
/// sure to register a callback when initializing the event loop /// sure to register a callback when initializing the event loop
@ -56,10 +61,14 @@ pub const PosixTty = struct {
}; };
try posix.sigaction(posix.SIG.WINCH, &act, null); try posix.sigaction(posix.SIG.WINCH, &act, null);
return .{ const self: PosixTty = .{
.fd = fd, .fd = fd,
.termios = termios, .termios = termios,
}; };
global_tty = self;
return self;
} }
/// release resources associated with the Tty return it to its original state /// release resources associated with the Tty return it to its original state