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>
// 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 {
hup = 1
int
quit
ill
trap
abrt
bus
fpe
kill
usr1
segv
usr2
pipe
alrm
term
stkflt
chld
cont
stop
tstp
ttin
ttou
urg
xcpu
xfsz
vtalrm
prof
winch
poll
pwr
sys
int = 2
quit = 3
ill = 4
trap = 5
abrt = 6
bus = 7
fpe = 8
kill = 9
usr1 = 10
segv = 11
usr2 = 12
pipe = 13
alrm = 14
term = 15
stkflt = 16
chld = 17
cont = 18
stop = 19
tstp = 20
ttin = 21
ttou = 22
urg = 23
xcpu = 24
xfsz = 25
vtalrm = 26
prof = 27
winch = 28
poll = 29
pwr = 30
sys = 31
}
type SignalHandler = fn (Signal)

View File

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

View File

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

View File

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

View File

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