From 5c00851b4437295c15ddb24422b6b5d62818a11c Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 29 Jan 2020 01:44:57 +0300 Subject: [PATCH] term: implement `get_terminal_size` for Windows --- vlib/term/misc_windows.v | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/vlib/term/misc_windows.v b/vlib/term/misc_windows.v index e3c67ea906..573fa9b9bf 100644 --- a/vlib/term/misc_windows.v +++ b/vlib/term/misc_windows.v @@ -1,6 +1,35 @@ module term -pub fn get_terminal_size() (int, int) { - // TODO: find a way to get proper width&height of the terminal on windows, probably using Winapis - return 80, 25 +import os + +struct C.SMALL_RECT { + Left i16 + Top i16 + Right i16 + Bottom i16 +} + +struct C.CONSOLE_SCREEN_BUFFER_INFO { + dwSize C.COORD + dwCursorPosition C.COORD + wAttributes C.COORD + srWindow C.SMALL_RECT + dwMaximumWindowSize C.COORD +} + +fn C.GetConsoleScreenBufferInfo(handle C.HANDLE, info &CONSOLE_SCREEN_BUFFER_INFO) bool + +// 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' { + info := CONSOLE_SCREEN_BUFFER_INFO{} + + if C.GetConsoleScreenBufferInfo(C.GetStdHandle(C.STD_OUTPUT_HANDLE), &info) { + columns := (info.srWindow.Right - info.srWindow.Left + 1) as int + rows := (info.srWindow.Bottom - info.srWindow.Top + 1) as int + return columns, rows + } + } + + return 80, 25 }