2020-01-28 05:18:19 +01:00
|
|
|
module term
|
|
|
|
|
2020-01-28 23:44:57 +01:00
|
|
|
import os
|
|
|
|
|
2021-01-27 13:52:39 +01:00
|
|
|
[typedef]
|
|
|
|
struct C.COORD {
|
|
|
|
X i16
|
|
|
|
Y i16
|
2020-04-04 11:56:43 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 13:52:39 +01:00
|
|
|
[typedef]
|
|
|
|
struct C.SMALL_RECT {
|
|
|
|
Left u16
|
|
|
|
Top u16
|
|
|
|
Right u16
|
|
|
|
Bottom u16
|
2020-01-28 23:44:57 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 21:00:10 +02:00
|
|
|
// win: CONSOLE_SCREEN_BUFFER_INFO
|
|
|
|
// https://docs.microsoft.com/en-us/windows/console/console-screen-buffer-info-str
|
2021-01-27 13:52:39 +01:00
|
|
|
[typedef]
|
|
|
|
struct C.CONSOLE_SCREEN_BUFFER_INFO {
|
|
|
|
dwSize C.COORD
|
|
|
|
dwCursorPosition C.COORD
|
|
|
|
wAttributes u16
|
|
|
|
srWindow C.SMALL_RECT
|
|
|
|
dwMaximumWindowSize C.COORD
|
2020-01-28 23:44:57 +01:00
|
|
|
}
|
|
|
|
|
2020-09-08 21:00:10 +02:00
|
|
|
// ref - https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo
|
2021-01-27 13:52:39 +01:00
|
|
|
fn C.GetConsoleScreenBufferInfo(handle os.HANDLE, info &C.CONSOLE_SCREEN_BUFFER_INFO) bool
|
2020-01-28 23:44:57 +01:00
|
|
|
|
2020-09-08 21:00:10 +02:00
|
|
|
// ref - https://docs.microsoft.com/en-us/windows/console/setconsoletitle
|
|
|
|
fn C.SetConsoleTitle(title &u16) bool
|
|
|
|
|
2020-01-28 23:44:57 +01:00
|
|
|
// get_terminal_size returns a number of colums and rows of terminal window.
|
|
|
|
pub fn get_terminal_size() (int, int) {
|
|
|
|
if is_atty(1) > 0 && os.getenv('TERM') != 'dumb' {
|
2021-01-27 13:52:39 +01:00
|
|
|
info := C.CONSOLE_SCREEN_BUFFER_INFO{}
|
2020-01-28 23:44:57 +01:00
|
|
|
if C.GetConsoleScreenBufferInfo(C.GetStdHandle(C.STD_OUTPUT_HANDLE), &info) {
|
2021-01-27 13:52:39 +01:00
|
|
|
columns := int(info.srWindow.Right - info.srWindow.Left + 1)
|
|
|
|
rows := int(info.srWindow.Bottom - info.srWindow.Top + 1)
|
2020-01-28 23:44:57 +01:00
|
|
|
return columns, rows
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 05:12:12 +01:00
|
|
|
return default_columns_size, default_rows_size
|
2020-01-28 05:18:19 +01:00
|
|
|
}
|
2020-09-08 21:00:10 +02:00
|
|
|
|
|
|
|
// get_cursor_position returns a Coord containing the current cursor position
|
|
|
|
pub fn get_cursor_position() Coord {
|
|
|
|
mut res := Coord{}
|
|
|
|
if is_atty(1) > 0 && os.getenv('TERM') != 'dumb' {
|
2021-01-27 13:52:39 +01:00
|
|
|
info := C.CONSOLE_SCREEN_BUFFER_INFO{}
|
2020-09-08 21:00:10 +02:00
|
|
|
if C.GetConsoleScreenBufferInfo(C.GetStdHandle(C.STD_OUTPUT_HANDLE), &info) {
|
2021-01-27 13:52:39 +01:00
|
|
|
res.x = info.dwCursorPosition.X
|
|
|
|
res.y = info.dwCursorPosition.Y
|
2020-09-08 21:00:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_terminal_title change the terminal title
|
|
|
|
pub fn set_terminal_title(title string) bool {
|
|
|
|
title_change := C.SetConsoleTitle(title.to_wide())
|
|
|
|
return title_change
|
|
|
|
}
|