term.ui: use os.signal_opt instead of os.signal

pull/10134/head
Delyan Angelov 2021-05-18 11:59:57 +03:00
parent 21b34b3a0b
commit 453fb1b08b
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
5 changed files with 53 additions and 43 deletions

View File

@ -2,38 +2,44 @@ module os
#include <signal.h> #include <signal.h>
// os.Signal - enumerate possible POSIX signals and
// their integer codes.
// NB: the integer codes are given here explicitly,
// to make it easier to lookup, without needing to
// consult man pages / signal.h .
pub enum Signal { pub enum Signal {
hup = 1 hup = 1
int int = 2
quit quit = 3
ill ill = 4
trap trap = 5
abrt abrt = 6
bus bus = 7
fpe fpe = 8
kill kill = 9
usr1 usr1 = 10
segv segv = 11
usr2 usr2 = 12
pipe pipe = 13
alrm alrm = 14
term term = 15
stkflt stkflt = 16
chld chld = 17
cont cont = 18
stop stop = 19
tstp tstp = 20
ttin ttin = 21
ttou ttou = 22
urg urg = 23
xcpu xcpu = 24
xfsz xfsz = 25
vtalrm vtalrm = 26
prof prof = 27
winch winch = 28
poll poll = 29
pwr pwr = 30
sys sys = 31
} }
type SignalHandler = fn (Signal) type SignalHandler = fn (Signal)

View File

@ -1,7 +1,5 @@
module ui module ui
import os
union C.Event { union C.Event {
KeyEvent C.KEY_EVENT_RECORD KeyEvent C.KEY_EVENT_RECORD
MouseEvent C.MOUSE_EVENT_RECORD MouseEvent C.MOUSE_EVENT_RECORD

View File

@ -3,6 +3,8 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module ui module ui
import os
pub enum KeyCode { pub enum KeyCode {
null = 0 null = 0
tab = 9 tab = 9
@ -208,8 +210,8 @@ pub struct Config {
capture_events bool capture_events bool
use_alternate_buffer bool = true use_alternate_buffer bool = true
skip_init_checks bool skip_init_checks bool
// All kill signals to set up exit listeners on // All kill signals to set up exit listeners on:
reset []int = [1, 2, 3, 4, 6, 7, 8, 9, 11, 13, 14, 15, 19] reset []os.Signal = [.hup, .int, .quit, .ill, .abrt, .bus, .fpe, .kill, .segv, .pipe, .alrm, .term, .stop]
} }
[inline] [inline]

View File

@ -82,13 +82,13 @@ pub fn init(cfg Config) &Context {
} }
C.atexit(restore_terminal_state) C.atexit(restore_terminal_state)
for code in ctx.cfg.reset { for code in ctx.cfg.reset {
os.signal(code, fn () { os.signal_opt(code, fn (_ os.Signal) {
mut c := ui.ctx_ptr mut c := ui.ctx_ptr
if c != 0 { if c != 0 {
c.cleanup() c.cleanup()
} }
exit(0) exit(0)
}) }) or {}
} }
ctx.stdin_handle = stdin_handle ctx.stdin_handle = stdin_handle

View File

@ -46,6 +46,10 @@ fn get_terminal_size() (u16, u16) {
return winsz.ws_row, winsz.ws_col return winsz.ws_row, winsz.ws_col
} }
fn restore_terminal_state_signal(_ os.Signal) {
restore_terminal_state()
}
fn restore_terminal_state() { fn restore_terminal_state() {
termios_reset() termios_reset()
mut c := ctx_ptr mut c := ctx_ptr
@ -123,8 +127,8 @@ fn (mut ctx Context) termios_setup() ? {
// Reset console on exit // Reset console on exit
C.atexit(restore_terminal_state) C.atexit(restore_terminal_state)
os.signal(C.SIGTSTP, restore_terminal_state) os.signal_opt(.tstp, restore_terminal_state_signal) or {}
os.signal(C.SIGCONT, fn () { os.signal_opt(.cont, fn (_ os.Signal) {
mut c := ctx_ptr mut c := ctx_ptr
if c != 0 { if c != 0 {
c.termios_setup() or { panic(err) } c.termios_setup() or { panic(err) }
@ -137,18 +141,18 @@ fn (mut ctx Context) termios_setup() ? {
c.paused = false c.paused = false
c.event(event) c.event(event)
} }
}) }) or {}
for code in ctx.cfg.reset { for code in ctx.cfg.reset {
os.signal(code, fn () { os.signal_opt(code, fn (_ os.Signal) {
mut c := ctx_ptr mut c := ctx_ptr
if c != 0 { if c != 0 {
c.cleanup() c.cleanup()
} }
exit(0) exit(0)
}) }) or {}
} }
os.signal(C.SIGWINCH, fn () { os.signal_opt(.winch, fn (_ os.Signal) {
mut c := ctx_ptr mut c := ctx_ptr
if c != 0 { if c != 0 {
c.window_height, c.window_width = get_terminal_size() c.window_height, c.window_width = get_terminal_size()
@ -160,7 +164,7 @@ fn (mut ctx Context) termios_setup() ? {
} }
c.event(event) c.event(event)
} }
}) }) or {}
os.flush() os.flush()
} }