2020-01-28 07:18:19 +03:00
|
|
|
module term
|
|
|
|
|
2020-01-29 01:44:57 +03:00
|
|
|
import os
|
|
|
|
|
2021-01-27 15:52:39 +03:00
|
|
|
[typedef]
|
2022-06-19 17:42:22 +03:00
|
|
|
pub struct C.COORD {
|
2021-02-09 15:25:31 +03:00
|
|
|
mut:
|
2021-01-27 15:52:39 +03:00
|
|
|
X i16
|
|
|
|
Y i16
|
2020-04-04 12:56:43 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 15:52:39 +03:00
|
|
|
[typedef]
|
2022-06-19 17:42:22 +03:00
|
|
|
pub struct C.SMALL_RECT {
|
2021-02-09 15:25:31 +03:00
|
|
|
mut:
|
2021-01-27 15:52:39 +03:00
|
|
|
Left u16
|
|
|
|
Top u16
|
|
|
|
Right u16
|
|
|
|
Bottom u16
|
2020-01-29 01:44:57 +03:00
|
|
|
}
|
|
|
|
|
2020-09-08 22:00:10 +03:00
|
|
|
// win: CONSOLE_SCREEN_BUFFER_INFO
|
|
|
|
// https://docs.microsoft.com/en-us/windows/console/console-screen-buffer-info-str
|
2021-01-27 15:52:39 +03:00
|
|
|
[typedef]
|
2022-06-19 17:42:22 +03:00
|
|
|
pub struct C.CONSOLE_SCREEN_BUFFER_INFO {
|
2021-02-09 15:25:31 +03:00
|
|
|
mut:
|
2021-01-27 15:52:39 +03:00
|
|
|
dwSize C.COORD
|
|
|
|
dwCursorPosition C.COORD
|
|
|
|
wAttributes u16
|
|
|
|
srWindow C.SMALL_RECT
|
|
|
|
dwMaximumWindowSize C.COORD
|
2020-01-29 01:44:57 +03:00
|
|
|
}
|
|
|
|
|
2022-06-19 17:42:22 +03:00
|
|
|
pub union C.uChar {
|
2021-02-09 15:25:31 +03:00
|
|
|
mut:
|
|
|
|
UnicodeChar rune
|
2022-04-15 18:25:45 +03:00
|
|
|
AsciiChar u8
|
2021-02-09 15:25:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[typedef]
|
2022-06-19 17:42:22 +03:00
|
|
|
pub struct C.CHAR_INFO {
|
2021-02-09 15:25:31 +03:00
|
|
|
mut:
|
2021-05-08 13:32:29 +03:00
|
|
|
Char C.uChar
|
2021-02-09 15:25:31 +03:00
|
|
|
Attributes u16
|
|
|
|
}
|
|
|
|
|
2020-09-08 22:00:10 +03:00
|
|
|
// ref - https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.GetConsoleScreenBufferInfo(handle C.HANDLE, info &C.CONSOLE_SCREEN_BUFFER_INFO) bool
|
2020-01-29 01:44:57 +03:00
|
|
|
|
2020-09-08 22:00:10 +03:00
|
|
|
// ref - https://docs.microsoft.com/en-us/windows/console/setconsoletitle
|
|
|
|
fn C.SetConsoleTitle(title &u16) bool
|
|
|
|
|
2021-02-09 15:25:31 +03:00
|
|
|
// ref - https://docs.microsoft.com/en-us/windows/console/setconsolecursorposition
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.SetConsoleCursorPosition(handle C.HANDLE, coord C.COORD) bool
|
2021-02-09 15:25:31 +03:00
|
|
|
|
|
|
|
// ref - https://docs.microsoft.com/en-us/windows/console/scrollconsolescreenbuffer
|
2021-03-05 17:41:11 +03:00
|
|
|
fn C.ScrollConsoleScreenBuffer(output C.HANDLE, scroll_rect &C.SMALL_RECT, clip_rect &C.SMALL_RECT, des C.COORD, fill &C.CHAR_INFO) bool
|
2021-02-09 15:25:31 +03:00
|
|
|
|
2020-01-29 01:44:57 +03:00
|
|
|
// get_terminal_size returns a number of colums and rows of terminal window.
|
|
|
|
pub fn get_terminal_size() (int, int) {
|
2021-04-14 08:50:50 +03:00
|
|
|
if os.is_atty(1) > 0 && os.getenv('TERM') != 'dumb' {
|
2021-01-27 15:52:39 +03:00
|
|
|
info := C.CONSOLE_SCREEN_BUFFER_INFO{}
|
2020-01-29 01:44:57 +03:00
|
|
|
if C.GetConsoleScreenBufferInfo(C.GetStdHandle(C.STD_OUTPUT_HANDLE), &info) {
|
2021-01-27 15:52:39 +03:00
|
|
|
columns := int(info.srWindow.Right - info.srWindow.Left + 1)
|
|
|
|
rows := int(info.srWindow.Bottom - info.srWindow.Top + 1)
|
2020-01-29 01:44:57 +03:00
|
|
|
return columns, rows
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 07:12:12 +03:00
|
|
|
return default_columns_size, default_rows_size
|
2020-01-28 07:18:19 +03:00
|
|
|
}
|
2020-09-08 22:00:10 +03:00
|
|
|
|
|
|
|
// get_cursor_position returns a Coord containing the current cursor position
|
2022-10-26 11:26:28 +03:00
|
|
|
pub fn get_cursor_position() !Coord {
|
2020-09-08 22:00:10 +03:00
|
|
|
mut res := Coord{}
|
2021-04-14 08:50:50 +03:00
|
|
|
if os.is_atty(1) > 0 && os.getenv('TERM') != 'dumb' {
|
2021-01-27 15:52:39 +03:00
|
|
|
info := C.CONSOLE_SCREEN_BUFFER_INFO{}
|
2020-09-08 22:00:10 +03:00
|
|
|
if C.GetConsoleScreenBufferInfo(C.GetStdHandle(C.STD_OUTPUT_HANDLE), &info) {
|
2021-01-27 15:52:39 +03:00
|
|
|
res.x = info.dwCursorPosition.X
|
|
|
|
res.y = info.dwCursorPosition.Y
|
2021-09-06 19:04:14 +03:00
|
|
|
} else {
|
|
|
|
return os.last_error()
|
2020-09-08 22:00:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-01-14 16:10:07 +03:00
|
|
|
// set_terminal_title changes the terminal title
|
2020-09-08 22:00:10 +03:00
|
|
|
pub fn set_terminal_title(title string) bool {
|
2023-01-14 16:10:07 +03:00
|
|
|
wide_title := title.to_wide()
|
|
|
|
return C.SetConsoleTitle(wide_title)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_tab_title changes the terminal *tab title*, for terminal emulators that do support several tabs
|
|
|
|
pub fn set_tab_title(title string) bool {
|
|
|
|
// TODO: investigate, whether there is an API for changing just the tab title on windows yet.
|
|
|
|
return set_terminal_title(title)
|
2020-09-08 22:00:10 +03:00
|
|
|
}
|
2021-02-09 15:25:31 +03:00
|
|
|
|
|
|
|
// clear clears current terminal screen.
|
|
|
|
// Implementation taken from https://docs.microsoft.com/en-us/windows/console/clearing-the-screen#example-2.
|
|
|
|
pub fn clear() {
|
|
|
|
hconsole := C.GetStdHandle(C.STD_OUTPUT_HANDLE)
|
|
|
|
mut csbi := C.CONSOLE_SCREEN_BUFFER_INFO{}
|
|
|
|
mut scrollrect := C.SMALL_RECT{}
|
|
|
|
mut scrolltarget := C.COORD{}
|
|
|
|
mut fill := C.CHAR_INFO{}
|
|
|
|
|
|
|
|
// Get the number of character cells in the current buffer.
|
|
|
|
if !C.GetConsoleScreenBufferInfo(hconsole, &csbi) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Scroll the rectangle of the entire buffer.
|
|
|
|
scrollrect.Left = 0
|
|
|
|
scrollrect.Top = 0
|
|
|
|
scrollrect.Right = u16(csbi.dwSize.X)
|
|
|
|
scrollrect.Bottom = u16(csbi.dwSize.Y)
|
|
|
|
|
|
|
|
// Scroll it upwards off the top of the buffer with a magnitude of the entire height.
|
|
|
|
scrolltarget.X = 0
|
|
|
|
scrolltarget.Y = (0 - csbi.dwSize.Y)
|
|
|
|
|
|
|
|
// Fill with empty spaces with the buffer's default text attribute.
|
|
|
|
fill.Char.UnicodeChar = rune(` `)
|
|
|
|
fill.Attributes = csbi.wAttributes
|
|
|
|
|
|
|
|
// Do the scroll
|
|
|
|
C.ScrollConsoleScreenBuffer(hconsole, &scrollrect, C.NULL, scrolltarget, &fill)
|
|
|
|
|
|
|
|
// Move the cursor to the top left corner too.
|
|
|
|
csbi.dwCursorPosition.X = 0
|
|
|
|
csbi.dwCursorPosition.Y = 0
|
|
|
|
|
|
|
|
C.SetConsoleCursorPosition(hconsole, csbi.dwCursorPosition)
|
|
|
|
}
|