mirror of
https://github.com/vlang/v.git
synced 2025-09-14 18:06:48 -04:00
term.ui: fix exception thrown in GetConsoleMode on windows (stop abusing consts as globals) (#19529)
This commit is contained in:
parent
2ce209dac5
commit
a1c16a76be
@ -1,6 +1,7 @@
|
|||||||
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
[has_globals]
|
||||||
module ui
|
module ui
|
||||||
|
|
||||||
struct ExtraContext {
|
struct ExtraContext {
|
||||||
@ -11,7 +12,7 @@ mut:
|
|||||||
read_all_bytes bool = true
|
read_all_bytes bool = true
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctx_ptr = &Context(unsafe { nil })
|
__global ctx_ptr = &Context(unsafe { nil })
|
||||||
|
|
||||||
// init initializes the terminal console with Config `cfg`.
|
// init initializes the terminal console with Config `cfg`.
|
||||||
pub fn init(cfg Config) &Context {
|
pub fn init(cfg Config) &Context {
|
||||||
@ -20,12 +21,7 @@ pub fn init(cfg Config) &Context {
|
|||||||
}
|
}
|
||||||
ctx.read_buf = []u8{cap: cfg.buffer_size}
|
ctx.read_buf = []u8{cap: cfg.buffer_size}
|
||||||
|
|
||||||
// lmao
|
ctx_ptr = ctx
|
||||||
unsafe {
|
|
||||||
x := &ui.ctx_ptr
|
|
||||||
*x = ctx
|
|
||||||
_ = x
|
|
||||||
}
|
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license
|
||||||
// that can be found in the LICENSE file.
|
// that can be found in the LICENSE file.
|
||||||
|
[has_globals]
|
||||||
module ui
|
module ui
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@ -8,9 +9,9 @@ import time
|
|||||||
|
|
||||||
const buf_size = 64
|
const buf_size = 64
|
||||||
|
|
||||||
const ctx_ptr = &Context(unsafe { nil })
|
__global ctx_ptr = &Context(unsafe { nil })
|
||||||
|
|
||||||
const stdin_at_startup = u32(0)
|
__global stdin_at_startup = u32(0)
|
||||||
|
|
||||||
struct ExtraContext {
|
struct ExtraContext {
|
||||||
mut:
|
mut:
|
||||||
@ -21,14 +22,14 @@ mut:
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn restore_terminal_state() {
|
fn restore_terminal_state() {
|
||||||
if unsafe { ui.ctx_ptr != 0 } {
|
if unsafe { ctx_ptr != 0 } {
|
||||||
if ui.ctx_ptr.cfg.use_alternate_buffer {
|
if ctx_ptr.cfg.use_alternate_buffer {
|
||||||
// clear the terminal and set the cursor to the origin
|
// clear the terminal and set the cursor to the origin
|
||||||
print('\x1b[2J\x1b[3J')
|
print('\x1b[2J\x1b[3J')
|
||||||
print('\x1b[?1049l')
|
print('\x1b[?1049l')
|
||||||
flush_stdout()
|
flush_stdout()
|
||||||
}
|
}
|
||||||
C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup)
|
C.SetConsoleMode(ctx_ptr.stdin_handle, stdin_at_startup)
|
||||||
}
|
}
|
||||||
load_title()
|
load_title()
|
||||||
os.flush()
|
os.flush()
|
||||||
@ -46,7 +47,7 @@ pub fn init(cfg Config) &Context {
|
|||||||
panic('could not get stdin handle')
|
panic('could not get stdin handle')
|
||||||
}
|
}
|
||||||
// save the current input mode, to be restored on exit
|
// save the current input mode, to be restored on exit
|
||||||
if !C.GetConsoleMode(stdin_handle, &ui.stdin_at_startup) {
|
if !C.GetConsoleMode(stdin_handle, &stdin_at_startup) {
|
||||||
panic('could not get stdin console mode')
|
panic('could not get stdin console mode')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,14 +81,11 @@ pub fn init(cfg Config) &Context {
|
|||||||
flush_stdout()
|
flush_stdout()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe {
|
ctx_ptr = ctx
|
||||||
x := &ui.ctx_ptr
|
|
||||||
*x = ctx
|
|
||||||
}
|
|
||||||
C.atexit(restore_terminal_state)
|
C.atexit(restore_terminal_state)
|
||||||
for code in ctx.cfg.reset {
|
for code in ctx.cfg.reset {
|
||||||
os.signal_opt(code, fn (_ os.Signal) {
|
os.signal_opt(code, fn (_ os.Signal) {
|
||||||
mut c := unsafe { ui.ctx_ptr }
|
mut c := ctx_ptr
|
||||||
if unsafe { c != 0 } {
|
if unsafe { c != 0 } {
|
||||||
c.cleanup()
|
c.cleanup()
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ fn restore_terminal_state_signal(_ os.Signal) {
|
|||||||
|
|
||||||
fn restore_terminal_state() {
|
fn restore_terminal_state() {
|
||||||
termios_reset()
|
termios_reset()
|
||||||
mut c := unsafe { ctx_ptr }
|
mut c := ctx_ptr
|
||||||
if unsafe { c != 0 } {
|
if unsafe { c != 0 } {
|
||||||
c.paused = true
|
c.paused = true
|
||||||
load_title()
|
load_title()
|
||||||
@ -118,7 +118,7 @@ fn (mut ctx Context) termios_setup() ! {
|
|||||||
C.atexit(restore_terminal_state)
|
C.atexit(restore_terminal_state)
|
||||||
os.signal_opt(.tstp, restore_terminal_state_signal) or {}
|
os.signal_opt(.tstp, restore_terminal_state_signal) or {}
|
||||||
os.signal_opt(.cont, fn (_ os.Signal) {
|
os.signal_opt(.cont, fn (_ os.Signal) {
|
||||||
mut c := unsafe { ctx_ptr }
|
mut c := ctx_ptr
|
||||||
if unsafe { c != 0 } {
|
if unsafe { c != 0 } {
|
||||||
c.termios_setup() or { panic(err) }
|
c.termios_setup() or { panic(err) }
|
||||||
c.window_height, c.window_width = get_terminal_size()
|
c.window_height, c.window_width = get_terminal_size()
|
||||||
@ -133,7 +133,7 @@ fn (mut ctx Context) termios_setup() ! {
|
|||||||
}) or {}
|
}) or {}
|
||||||
for code in ctx.cfg.reset {
|
for code in ctx.cfg.reset {
|
||||||
os.signal_opt(code, fn (_ os.Signal) {
|
os.signal_opt(code, fn (_ os.Signal) {
|
||||||
mut c := unsafe { ctx_ptr }
|
mut c := ctx_ptr
|
||||||
if unsafe { c != 0 } {
|
if unsafe { c != 0 } {
|
||||||
c.cleanup()
|
c.cleanup()
|
||||||
}
|
}
|
||||||
@ -142,7 +142,7 @@ fn (mut ctx Context) termios_setup() ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
os.signal_opt(.winch, fn (_ os.Signal) {
|
os.signal_opt(.winch, fn (_ os.Signal) {
|
||||||
mut c := unsafe { ctx_ptr }
|
mut c := ctx_ptr
|
||||||
if unsafe { c != 0 } {
|
if unsafe { c != 0 } {
|
||||||
c.window_height, c.window_width = get_terminal_size()
|
c.window_height, c.window_width = get_terminal_size()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user