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

term.ui: render to the alternate buffer (#6832)

This commit is contained in:
spaceface777
2020-11-14 20:05:22 +01:00
committed by GitHub
parent b96a0246b5
commit 1ead130eed
6 changed files with 102 additions and 83 deletions

View File

@ -167,8 +167,7 @@ mut:
termios C.termios
read_buf []byte
print_buf []byte
// init_called bool
// quit_ordered bool
paused bool
pub mut:
frame_count u64
window_width int
@ -190,6 +189,7 @@ pub struct Config {
window_title string
hide_cursor bool
capture_events bool
use_alternate_buffer bool = true
// All kill signals
reset []int = [1, 2, 3, 4, 6, 7, 8, 9, 11, 13, 14, 15, 19]
}

View File

@ -45,9 +45,10 @@ fn restore_terminal_state() {
termios_reset()
mut c := ctx_ptr
if c != 0 {
c.paused = true
c.load_title()
}
println('')
os.flush()
}
fn (mut ctx Context) termios_setup() {
@ -79,9 +80,10 @@ fn (mut ctx Context) termios_setup() {
termios.c_cc[C.VMIN] = 0
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH, &termios)
print('\x1b[?1003h\x1b[?1006h')
if ctx.cfg.use_alternate_buffer {
print('\x1b[?1049h')
}
ctx.termios = termios
ctx.window_height, ctx.window_width = get_terminal_size()
// Reset console on exit
@ -97,6 +99,7 @@ fn (mut ctx Context) termios_setup() {
width: c.window_width
height: c.window_height
}
c.paused = false
c.event(event)
}
})
@ -123,11 +126,17 @@ fn (mut ctx Context) termios_setup() {
c.event(event)
}
})
os.flush()
}
fn termios_reset() {
C.tcsetattr(C.STDIN_FILENO, C.TCSAFLUSH /* C.TCSANOW ?? */, &termios_at_startup)
print('\x1b[?1003l\x1b[?1015l\x1b[?1006l\x1b[0J\x1b[?25h')
print('\x1b[?1003l\x1b[?1006l\x1b[?25h')
c := ctx_ptr
if c != 0 && c.cfg.use_alternate_buffer {
print('\x1b[?1049l')
}
os.flush()
}
///////////////////////////////////////////
@ -147,20 +156,22 @@ fn (mut ctx Context) termios_loop() {
if sleep_len > 0 {
time.usleep(sleep_len)
}
sw.restart()
if ctx.cfg.event_fn != voidptr(0) {
len := C.read(C.STDIN_FILENO, ctx.read_buf.data, ctx.read_buf.cap - ctx.read_buf.len)
if len > 0 {
ctx.resize_arr(len)
ctx.parse_events()
if !ctx.paused {
sw.restart()
if ctx.cfg.event_fn != voidptr(0) {
len := C.read(C.STDIN_FILENO, ctx.read_buf.data, ctx.read_buf.cap - ctx.read_buf.len)
if len > 0 {
ctx.resize_arr(len)
ctx.parse_events()
}
}
}
ctx.frame()
sw.pause()
e := sw.elapsed().microseconds()
sleep_len = frame_time - int(e)
ctx.frame()
sw.pause()
e := sw.elapsed().microseconds()
sleep_len = frame_time - int(e)
ctx.frame_count++
ctx.frame_count++
}
}
}