1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
Delyan Angelov
2021-05-18 11:59:57 +03:00
parent 21b34b3a0b
commit 453fb1b08b
5 changed files with 53 additions and 43 deletions

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()
}