mirror of
https://github.com/vlang/v.git
synced 2025-09-14 09:56:16 -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.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
[has_globals]
|
||||
module ui
|
||||
|
||||
struct ExtraContext {
|
||||
@ -11,7 +12,7 @@ mut:
|
||||
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`.
|
||||
pub fn init(cfg Config) &Context {
|
||||
@ -20,12 +21,7 @@ pub fn init(cfg Config) &Context {
|
||||
}
|
||||
ctx.read_buf = []u8{cap: cfg.buffer_size}
|
||||
|
||||
// lmao
|
||||
unsafe {
|
||||
x := &ui.ctx_ptr
|
||||
*x = ctx
|
||||
_ = x
|
||||
}
|
||||
ctx_ptr = ctx
|
||||
return ctx
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
[has_globals]
|
||||
module ui
|
||||
|
||||
import os
|
||||
@ -8,9 +9,9 @@ import time
|
||||
|
||||
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 {
|
||||
mut:
|
||||
@ -21,14 +22,14 @@ mut:
|
||||
}
|
||||
|
||||
fn restore_terminal_state() {
|
||||
if unsafe { ui.ctx_ptr != 0 } {
|
||||
if ui.ctx_ptr.cfg.use_alternate_buffer {
|
||||
if unsafe { ctx_ptr != 0 } {
|
||||
if ctx_ptr.cfg.use_alternate_buffer {
|
||||
// clear the terminal and set the cursor to the origin
|
||||
print('\x1b[2J\x1b[3J')
|
||||
print('\x1b[?1049l')
|
||||
flush_stdout()
|
||||
}
|
||||
C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup)
|
||||
C.SetConsoleMode(ctx_ptr.stdin_handle, stdin_at_startup)
|
||||
}
|
||||
load_title()
|
||||
os.flush()
|
||||
@ -46,7 +47,7 @@ pub fn init(cfg Config) &Context {
|
||||
panic('could not get stdin handle')
|
||||
}
|
||||
// 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')
|
||||
}
|
||||
|
||||
@ -80,14 +81,11 @@ pub fn init(cfg Config) &Context {
|
||||
flush_stdout()
|
||||
}
|
||||
|
||||
unsafe {
|
||||
x := &ui.ctx_ptr
|
||||
*x = ctx
|
||||
}
|
||||
ctx_ptr = ctx
|
||||
C.atexit(restore_terminal_state)
|
||||
for code in ctx.cfg.reset {
|
||||
os.signal_opt(code, fn (_ os.Signal) {
|
||||
mut c := unsafe { ui.ctx_ptr }
|
||||
mut c := ctx_ptr
|
||||
if unsafe { c != 0 } {
|
||||
c.cleanup()
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ fn restore_terminal_state_signal(_ os.Signal) {
|
||||
|
||||
fn restore_terminal_state() {
|
||||
termios_reset()
|
||||
mut c := unsafe { ctx_ptr }
|
||||
mut c := ctx_ptr
|
||||
if unsafe { c != 0 } {
|
||||
c.paused = true
|
||||
load_title()
|
||||
@ -118,7 +118,7 @@ fn (mut ctx Context) termios_setup() ! {
|
||||
C.atexit(restore_terminal_state)
|
||||
os.signal_opt(.tstp, restore_terminal_state_signal) or {}
|
||||
os.signal_opt(.cont, fn (_ os.Signal) {
|
||||
mut c := unsafe { ctx_ptr }
|
||||
mut c := ctx_ptr
|
||||
if unsafe { c != 0 } {
|
||||
c.termios_setup() or { panic(err) }
|
||||
c.window_height, c.window_width = get_terminal_size()
|
||||
@ -133,7 +133,7 @@ fn (mut ctx Context) termios_setup() ! {
|
||||
}) or {}
|
||||
for code in ctx.cfg.reset {
|
||||
os.signal_opt(code, fn (_ os.Signal) {
|
||||
mut c := unsafe { ctx_ptr }
|
||||
mut c := ctx_ptr
|
||||
if unsafe { c != 0 } {
|
||||
c.cleanup()
|
||||
}
|
||||
@ -142,7 +142,7 @@ fn (mut ctx Context) termios_setup() ! {
|
||||
}
|
||||
|
||||
os.signal_opt(.winch, fn (_ os.Signal) {
|
||||
mut c := unsafe { ctx_ptr }
|
||||
mut c := ctx_ptr
|
||||
if unsafe { c != 0 } {
|
||||
c.window_height, c.window_width = get_terminal_size()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user