mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
Revert "Revert "term: obtain the cursor position via termios.h (#11372)""
This reverts commit 0376cbf6bd
.
This commit is contained in:
10
vlib/term/declarations_default.c.v
Normal file
10
vlib/term/declarations_default.c.v
Normal file
@@ -0,0 +1,10 @@
|
||||
module term
|
||||
|
||||
pub struct C.termios {
|
||||
mut:
|
||||
c_iflag int
|
||||
c_oflag int
|
||||
c_cflag int
|
||||
c_lflag int
|
||||
c_cc [10]int
|
||||
}
|
11
vlib/term/declarations_linux.c.v
Normal file
11
vlib/term/declarations_linux.c.v
Normal file
@@ -0,0 +1,11 @@
|
||||
module term
|
||||
|
||||
pub struct C.termios {
|
||||
mut:
|
||||
c_iflag int
|
||||
c_oflag int
|
||||
c_cflag int
|
||||
c_lflag int
|
||||
c_line byte
|
||||
c_cc [10]int
|
||||
}
|
@@ -13,6 +13,10 @@ pub:
|
||||
ws_ypixel u16
|
||||
}
|
||||
|
||||
fn C.tcgetattr(fd int, ptr &C.termios) int
|
||||
|
||||
fn C.tcsetattr(fd int, action int, const_ptr &C.termios)
|
||||
|
||||
fn C.ioctl(fd int, request u64, arg voidptr) int
|
||||
|
||||
// get_terminal_size returns a number of colums and rows of terminal window.
|
||||
@@ -21,70 +25,58 @@ pub fn get_terminal_size() (int, int) {
|
||||
return default_columns_size, default_rows_size
|
||||
}
|
||||
w := C.winsize{}
|
||||
C.ioctl(1, u64(C.TIOCGWINSZ), &w)
|
||||
unsafe { C.ioctl(1, u64(C.TIOCGWINSZ), &w) }
|
||||
return int(w.ws_col), int(w.ws_row)
|
||||
}
|
||||
|
||||
// get_cursor_position returns a Coord containing the current cursor position
|
||||
pub fn get_cursor_position() Coord {
|
||||
pub fn get_cursor_position() ?Coord {
|
||||
if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
|
||||
return Coord{
|
||||
x: 0
|
||||
y: 0
|
||||
}
|
||||
return Coord{0, 0}
|
||||
}
|
||||
// TODO: use termios.h, C.tcgetattr & C.tcsetattr directly,
|
||||
// instead of using `stty`
|
||||
mut oldsettings := os.execute('stty -g')
|
||||
if oldsettings.exit_code < 0 {
|
||||
oldsettings = os.Result{}
|
||||
|
||||
old_state := C.termios{}
|
||||
if unsafe { C.tcgetattr(0, &old_state) } != 0 {
|
||||
return os.last_error()
|
||||
}
|
||||
os.system('stty -echo -icanon time 0')
|
||||
print('\033[6n')
|
||||
mut ch := int(0)
|
||||
mut i := 0
|
||||
// ESC [ YYY `;` XXX `R`
|
||||
mut reading_x := false
|
||||
mut reading_y := false
|
||||
defer {
|
||||
// restore the old terminal state:
|
||||
unsafe { C.tcsetattr(0, C.TCSANOW, &old_state) }
|
||||
}
|
||||
|
||||
mut state := C.termios{}
|
||||
if unsafe { C.tcgetattr(0, &state) } != 0 {
|
||||
return os.last_error()
|
||||
}
|
||||
state.c_lflag &= int(~(u32(C.ICANON) | u32(C.ECHO)))
|
||||
unsafe { C.tcsetattr(0, C.TCSANOW, &state) }
|
||||
|
||||
print('\e[6n')
|
||||
|
||||
mut x := 0
|
||||
mut y := 0
|
||||
mut stage := byte(0)
|
||||
|
||||
// ESC [ YYY `;` XXX `R`
|
||||
|
||||
for {
|
||||
ch = C.getchar()
|
||||
b := byte(ch)
|
||||
i++
|
||||
if i >= 15 {
|
||||
panic('C.getchar() called too many times')
|
||||
}
|
||||
// state management:
|
||||
if b == `R` {
|
||||
w := unsafe { C.getchar() }
|
||||
if w < 0 {
|
||||
return error_with_code('Failed to read from stdin', 888)
|
||||
} else if w == `[` || w == `;` {
|
||||
stage++
|
||||
} else if `0` <= w && w <= `9` {
|
||||
match stage {
|
||||
// converting string values to int:
|
||||
1 { y = y * 10 + int(w - `0`) }
|
||||
2 { x = x * 10 + int(w - `0`) }
|
||||
else {}
|
||||
}
|
||||
} else if w == `R` {
|
||||
break
|
||||
}
|
||||
if b == `[` {
|
||||
reading_y = true
|
||||
reading_x = false
|
||||
continue
|
||||
}
|
||||
if b == `;` {
|
||||
reading_y = false
|
||||
reading_x = true
|
||||
continue
|
||||
}
|
||||
// converting string vals to ints:
|
||||
if reading_x {
|
||||
x *= 10
|
||||
x += (b - byte(`0`))
|
||||
}
|
||||
if reading_y {
|
||||
y *= 10
|
||||
y += (b - byte(`0`))
|
||||
}
|
||||
}
|
||||
// restore the old terminal settings:
|
||||
os.system('stty $oldsettings.output')
|
||||
return Coord{
|
||||
x: x
|
||||
y: y
|
||||
}
|
||||
return Coord{x, y}
|
||||
}
|
||||
|
||||
// set_terminal_title change the terminal title
|
||||
|
@@ -57,9 +57,9 @@ fn test_header() {
|
||||
assert term_width == term.header('1234', '_-/\\/\\').len
|
||||
}
|
||||
|
||||
fn test_get_cursor_position() {
|
||||
original_position := term.get_cursor_position()
|
||||
cursor_position_1 := term.get_cursor_position()
|
||||
fn test_get_cursor_position() ? {
|
||||
original_position := term.get_cursor_position() ?
|
||||
cursor_position_1 := term.get_cursor_position() ?
|
||||
assert original_position.x == cursor_position_1.x
|
||||
assert original_position.y == cursor_position_1.y
|
||||
//
|
||||
@@ -67,13 +67,13 @@ fn test_get_cursor_position() {
|
||||
x: 10
|
||||
y: 11
|
||||
)
|
||||
cursor_position_2 := term.get_cursor_position()
|
||||
cursor_position_2 := term.get_cursor_position() ?
|
||||
//
|
||||
term.set_cursor_position(
|
||||
x: 5
|
||||
y: 6
|
||||
)
|
||||
cursor_position_3 := term.get_cursor_position()
|
||||
cursor_position_3 := term.get_cursor_position() ?
|
||||
//
|
||||
term.set_cursor_position(original_position)
|
||||
eprintln('original_position: $original_position')
|
||||
|
@@ -69,13 +69,15 @@ pub fn get_terminal_size() (int, int) {
|
||||
}
|
||||
|
||||
// get_cursor_position returns a Coord containing the current cursor position
|
||||
pub fn get_cursor_position() Coord {
|
||||
pub fn get_cursor_position() ?Coord {
|
||||
mut res := Coord{}
|
||||
if os.is_atty(1) > 0 && os.getenv('TERM') != 'dumb' {
|
||||
info := C.CONSOLE_SCREEN_BUFFER_INFO{}
|
||||
if C.GetConsoleScreenBufferInfo(C.GetStdHandle(C.STD_OUTPUT_HANDLE), &info) {
|
||||
res.x = info.dwCursorPosition.X
|
||||
res.y = info.dwCursorPosition.Y
|
||||
} else {
|
||||
return os.last_error()
|
||||
}
|
||||
}
|
||||
return res
|
||||
|
@@ -4,10 +4,9 @@
|
||||
|
||||
module ui
|
||||
|
||||
const (
|
||||
value_range = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]!
|
||||
color_table = init_color_table()
|
||||
)
|
||||
const value_range = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff]!
|
||||
|
||||
pub const color_table = init_color_table()
|
||||
|
||||
[direct_array_access]
|
||||
fn init_color_table() []int {
|
||||
|
10
vlib/term/ui/declarations_default.c.v
Normal file
10
vlib/term/ui/declarations_default.c.v
Normal file
@@ -0,0 +1,10 @@
|
||||
module ui
|
||||
|
||||
struct C.termios {
|
||||
mut:
|
||||
c_iflag int
|
||||
c_oflag int
|
||||
c_cflag int
|
||||
c_lflag int
|
||||
c_cc [10]int
|
||||
}
|
11
vlib/term/ui/declarations_linux.c.v
Normal file
11
vlib/term/ui/declarations_linux.c.v
Normal file
@@ -0,0 +1,11 @@
|
||||
module ui
|
||||
|
||||
struct C.termios {
|
||||
mut:
|
||||
c_iflag int
|
||||
c_oflag int
|
||||
c_cflag int
|
||||
c_lflag int
|
||||
c_line byte
|
||||
c_cc [10]int
|
||||
}
|
@@ -8,9 +8,7 @@ mut:
|
||||
read_buf []byte
|
||||
}
|
||||
|
||||
const (
|
||||
ctx_ptr = &Context(0)
|
||||
)
|
||||
const ctx_ptr = &Context(0)
|
||||
|
||||
pub fn init(cfg Config) &Context {
|
||||
mut ctx := &Context{
|
||||
|
@@ -6,11 +6,11 @@ module ui
|
||||
import os
|
||||
import time
|
||||
|
||||
const (
|
||||
buf_size = 64
|
||||
ctx_ptr = &Context(0)
|
||||
stdin_at_startup = u32(0)
|
||||
)
|
||||
const buf_size = 64
|
||||
|
||||
const ctx_ptr = &Context(0)
|
||||
|
||||
const stdin_at_startup = u32(0)
|
||||
|
||||
struct ExtraContext {
|
||||
mut:
|
||||
|
@@ -10,27 +10,18 @@ import time
|
||||
#include <sys/ioctl.h>
|
||||
#include <signal.h>
|
||||
|
||||
fn C.tcgetattr(fd int, termios_p &C.termios) int
|
||||
|
||||
fn C.tcsetattr(fd int, optional_actions int, termios_p &C.termios) int
|
||||
|
||||
fn C.ioctl(fd int, request u64, arg voidptr) int
|
||||
|
||||
struct C.termios {
|
||||
mut:
|
||||
c_iflag u32
|
||||
c_lflag u32
|
||||
c_cc [32]byte
|
||||
}
|
||||
|
||||
struct C.winsize {
|
||||
ws_row u16
|
||||
ws_col u16
|
||||
}
|
||||
|
||||
const (
|
||||
termios_at_startup = get_termios()
|
||||
)
|
||||
fn C.tcgetattr(fd int, termios_p &C.termios) int
|
||||
|
||||
fn C.tcsetattr(fd int, optional_actions int, const_termios_p &C.termios) int
|
||||
|
||||
fn C.ioctl(fd int, request u64, arg voidptr) int
|
||||
|
||||
const termios_at_startup = get_termios()
|
||||
|
||||
[inline]
|
||||
fn get_termios() C.termios {
|
||||
@@ -74,11 +65,11 @@ fn (mut ctx Context) termios_setup() ? {
|
||||
if ctx.cfg.capture_events {
|
||||
// Set raw input mode by unsetting ICANON and ECHO,
|
||||
// as well as disable e.g. ctrl+c and ctrl.z
|
||||
termios.c_iflag &= ~u32(C.IGNBRK | C.BRKINT | C.PARMRK | C.IXON)
|
||||
termios.c_lflag &= ~u32(C.ICANON | C.ISIG | C.ECHO | C.IEXTEN | C.TOSTOP)
|
||||
termios.c_iflag &= ~(C.IGNBRK | C.BRKINT | C.PARMRK | C.IXON)
|
||||
termios.c_lflag &= ~(C.ICANON | C.ISIG | C.ECHO | C.IEXTEN | C.TOSTOP)
|
||||
} else {
|
||||
// Set raw input mode by unsetting ICANON and ECHO
|
||||
termios.c_lflag &= ~u32(C.ICANON | C.ECHO)
|
||||
termios.c_lflag &= ~(C.ICANON | C.ECHO)
|
||||
}
|
||||
|
||||
if ctx.cfg.hide_cursor {
|
||||
|
@@ -18,10 +18,9 @@ pub fn (c Color) hex() string {
|
||||
|
||||
// Synchronized Updates spec, designed to avoid tearing during renders
|
||||
// https://gitlab.com/gnachman/iterm2/-/wikis/synchronized-updates-spec
|
||||
const (
|
||||
bsu = '\x1bP=1s\x1b\\'
|
||||
esu = '\x1bP=2s\x1b\\'
|
||||
)
|
||||
const bsu = '\x1bP=1s\x1b\\'
|
||||
|
||||
const esu = '\x1bP=2s\x1b\\'
|
||||
|
||||
// write puts the string `s` into the print buffer.
|
||||
[inline]
|
||||
|
8
vlib/term/ui/ui_test.v
Normal file
8
vlib/term/ui/ui_test.v
Normal file
@@ -0,0 +1,8 @@
|
||||
import term.ui
|
||||
|
||||
// This test just ensures that programs importing term.ui can compile
|
||||
|
||||
fn test_a_simple_term_ui_program_can_be_compiled() {
|
||||
println(ui.color_table)
|
||||
assert true
|
||||
}
|
Reference in New Issue
Block a user