this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

vaxis: add panic handler function

Applications may use this to handle panics gracefully

+30 -1
+20
src/main.zig
··· 1 1 const std = @import("std"); 2 + const builtin = @import("builtin"); 2 3 3 4 pub const Vaxis = @import("Vaxis.zig"); 5 + 4 6 pub const Loop = @import("Loop.zig").Loop; 7 + pub const xev = @import("xev.zig"); 5 8 6 9 pub const Queue = @import("queue.zig").Queue; 7 10 pub const Key = @import("Key.zig"); ··· 22 25 23 26 pub const widgets = @import("widgets.zig"); 24 27 pub const gwidth = @import("gwidth.zig"); 28 + pub const ctlseqs = @import("ctlseqs.zig"); 25 29 26 30 /// Initialize a Vaxis application. 27 31 pub fn init(alloc: std.mem.Allocator, opts: Vaxis.Options) !Vaxis { 28 32 return Vaxis.init(alloc, opts); 33 + } 34 + 35 + /// Resets terminal state on a panic, then calls the default zig panic handler 36 + pub fn panic_handler(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn { 37 + if (Tty.global_tty) |gty| { 38 + const reset: []const u8 = ctlseqs.csi_u_pop ++ 39 + ctlseqs.mouse_reset ++ 40 + ctlseqs.bp_reset ++ 41 + ctlseqs.rmcup; 42 + 43 + gty.anyWriter().writeAll(reset) catch {}; 44 + 45 + gty.deinit(); 46 + } 47 + 48 + std.builtin.default_panic(msg, error_return_trace, ret_addr); 29 49 } 30 50 31 51 /// the vaxis logo. In PixelCode
+10 -1
src/tty.zig
··· 35 35 var handler_mutex: std.Thread.Mutex = .{}; 36 36 var handler_idx: usize = 0; 37 37 38 + /// global tty instance, used in case of a panic. Not guaranteed to work if 39 + /// for some reason there are multiple TTYs open under a single vaxis 40 + /// compilation unit - but this is better than nothing 41 + pub var global_tty: ?PosixTty = null; 42 + 38 43 /// initializes a Tty instance by opening /dev/tty and "making it raw". A 39 44 /// signal handler is installed for SIGWINCH. No callbacks are installed, be 40 45 /// sure to register a callback when initializing the event loop ··· 56 61 }; 57 62 try posix.sigaction(posix.SIG.WINCH, &act, null); 58 63 59 - return .{ 64 + const self: PosixTty = .{ 60 65 .fd = fd, 61 66 .termios = termios, 62 67 }; 68 + 69 + global_tty = self; 70 + 71 + return self; 63 72 } 64 73 65 74 /// release resources associated with the Tty return it to its original state