2020-01-28 05:18:19 +01:00
|
|
|
module term
|
|
|
|
|
2020-01-28 20:25:17 +01:00
|
|
|
import os
|
2020-01-28 05:18:19 +01:00
|
|
|
|
2020-01-28 20:25:17 +01:00
|
|
|
#include <sys/ioctl.h>
|
2020-03-31 12:46:46 +02:00
|
|
|
#include <termios.h> // TIOCGWINSZ
|
2021-03-05 15:41:11 +01:00
|
|
|
|
2020-01-28 20:25:17 +01:00
|
|
|
pub struct C.winsize {
|
|
|
|
pub:
|
|
|
|
ws_row u16
|
|
|
|
ws_col u16
|
|
|
|
ws_xpixel u16
|
|
|
|
ws_ypixel u16
|
2020-01-28 05:18:19 +01:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:04:14 +02:00
|
|
|
fn C.tcgetattr(fd int, ptr &C.termios) int
|
|
|
|
|
|
|
|
fn C.tcsetattr(fd int, action int, const_ptr &C.termios)
|
|
|
|
|
2020-01-28 20:25:17 +01:00
|
|
|
fn C.ioctl(fd int, request u64, arg voidptr) int
|
2020-01-28 05:18:19 +01:00
|
|
|
|
2020-01-29 05:12:12 +01:00
|
|
|
// get_terminal_size returns a number of colums and rows of terminal window.
|
2020-09-08 21:00:10 +02:00
|
|
|
pub fn get_terminal_size() (int, int) {
|
2021-04-14 07:50:50 +02:00
|
|
|
if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
|
2020-01-29 05:12:12 +01:00
|
|
|
return default_columns_size, default_rows_size
|
2020-01-28 20:25:17 +01:00
|
|
|
}
|
|
|
|
w := C.winsize{}
|
2021-09-06 18:04:14 +02:00
|
|
|
unsafe { C.ioctl(1, u64(C.TIOCGWINSZ), &w) }
|
2020-09-08 21:00:10 +02:00
|
|
|
return int(w.ws_col), int(w.ws_row)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get_cursor_position returns a Coord containing the current cursor position
|
2021-09-06 18:04:14 +02:00
|
|
|
pub fn get_cursor_position() ?Coord {
|
2021-04-14 07:50:50 +02:00
|
|
|
if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
|
2021-09-06 18:04:14 +02:00
|
|
|
return Coord{0, 0}
|
2021-09-06 16:24:39 +02:00
|
|
|
}
|
2021-09-06 18:04:14 +02:00
|
|
|
|
|
|
|
old_state := C.termios{}
|
|
|
|
if unsafe { C.tcgetattr(0, &old_state) } != 0 {
|
|
|
|
return os.last_error()
|
2021-09-06 16:24:39 +02:00
|
|
|
}
|
2021-09-06 18:04:14 +02:00
|
|
|
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')
|
2022-05-30 18:15:05 +02:00
|
|
|
flush_stdout()
|
2021-09-06 18:04:14 +02:00
|
|
|
|
2020-09-08 21:00:10 +02:00
|
|
|
mut x := 0
|
|
|
|
mut y := 0
|
2022-04-15 13:58:56 +02:00
|
|
|
mut stage := u8(0)
|
2021-09-06 18:04:14 +02:00
|
|
|
|
|
|
|
// ESC [ YYY `;` XXX `R`
|
|
|
|
|
2020-09-08 21:00:10 +02:00
|
|
|
for {
|
2021-09-06 18:04:14 +02:00
|
|
|
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` {
|
2020-09-08 21:00:10 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 18:04:14 +02:00
|
|
|
return Coord{x, y}
|
2020-09-08 21:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set_terminal_title change the terminal title
|
|
|
|
pub fn set_terminal_title(title string) bool {
|
2021-04-14 07:50:50 +02:00
|
|
|
if os.is_atty(1) <= 0 || os.getenv('TERM') == 'dumb' {
|
2020-09-08 21:00:10 +02:00
|
|
|
return true
|
|
|
|
}
|
2021-03-08 19:52:13 +01:00
|
|
|
print('\033]0')
|
|
|
|
print(title)
|
|
|
|
print('\007')
|
2022-05-30 18:15:05 +02:00
|
|
|
flush_stdout()
|
2020-09-08 21:00:10 +02:00
|
|
|
return true
|
2020-01-28 05:18:19 +01:00
|
|
|
}
|
2021-02-09 13:25:31 +01:00
|
|
|
|
|
|
|
// clear clears current terminal screen.
|
|
|
|
pub fn clear() {
|
|
|
|
print('\x1b[2J')
|
|
|
|
print('\x1b[H')
|
2022-05-30 18:15:05 +02:00
|
|
|
flush_stdout()
|
2021-02-09 13:25:31 +01:00
|
|
|
}
|