Skip to content

Commit

Permalink
term.ui: use os.signal_opt instead of os.signal
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed May 18, 2021
1 parent 21b34b3 commit 453fb1b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
66 changes: 36 additions & 30 deletions vlib/os/signal.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions vlib/term/ui/consoleapi_windows.c.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module ui

import os

union C.Event {
KeyEvent C.KEY_EVENT_RECORD
MouseEvent C.MOUSE_EVENT_RECORD
Expand Down
6 changes: 4 additions & 2 deletions vlib/term/ui/input.v
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// that can be found in the LICENSE file.
module ui

import os

pub enum KeyCode {
null = 0
tab = 9
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions vlib/term/ui/input_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions vlib/term/ui/termios_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
Expand All @@ -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()
Expand All @@ -160,7 +164,7 @@ fn (mut ctx Context) termios_setup() ? {
}
c.event(event)
}
})
}) or {}

os.flush()
}
Expand Down

0 comments on commit 453fb1b

Please sign in to comment.