From 145406d9b609c1247cbd2c70b388955985090fce Mon Sep 17 00:00:00 2001 From: Vasily Litvinov Date: Thu, 7 Nov 2019 14:42:31 +0300 Subject: [PATCH 1/2] Fix getTermSize() for Windows --- terminal_size_windows.go | 54 ++++++---------------------------------- 1 file changed, 7 insertions(+), 47 deletions(-) diff --git a/terminal_size_windows.go b/terminal_size_windows.go index 71b8bec..390a525 100644 --- a/terminal_size_windows.go +++ b/terminal_size_windows.go @@ -3,61 +3,21 @@ package uilive import ( - "math" - "syscall" + "os" "unsafe" ) -type consoleFontInfo struct { - font uint32 - fontSize coord -} - -const ( - SmCxMin = 28 - SmCyMin = 29 -) - -var ( - tmpConsoleFontInfo consoleFontInfo - moduleUser32 = syscall.NewLazyDLL("user32.dll") - procGetCurrentConsoleFont = kernel32.NewProc("GetCurrentConsoleFont") - getSystemMetrics = moduleUser32.NewProc("GetSystemMetrics") -) - -func getCurrentConsoleFont(h syscall.Handle, info *consoleFontInfo) (err error) { - r0, _, e1 := syscall.Syscall( - procGetCurrentConsoleFont.Addr(), 3, uintptr(h), 0, uintptr(unsafe.Pointer(info)), - ) - if int(r0) == 0 { - if e1 != 0 { - err = error(e1) - } else { - err = syscall.EINVAL - } - } - return -} - func getTermSize() (int, int) { - out, err := syscall.Open("CONOUT$", syscall.O_RDWR, 0) + out, err := os.Open("CONOUT$") if err != nil { return 0, 0 } - x, _, err := getSystemMetrics.Call(SmCxMin) - y, _, err := getSystemMetrics.Call(SmCyMin) - - if x == 0 || y == 0 { - if err != nil { - panic(err) - } - } - - err = getCurrentConsoleFont(out, &tmpConsoleFontInfo) - if err != nil { - panic(err) + var csbi consoleScreenBufferInfo + ret, _, _ := procGetConsoleScreenBufferInfo.Call(out.Fd(), uintptr(unsafe.Pointer(&csbi))) + if ret == 0 { + return 0, 0 } - return int(math.Ceil(float64(x) / float64(tmpConsoleFontInfo.fontSize.x))), int(math.Ceil(float64(y) / float64(tmpConsoleFontInfo.fontSize.y))) + return int(csbi.window.right - csbi.window.left + 1), int(csbi.window.bottom - csbi.window.top + 1) } From eafd45c022863b0ad01a61a000d0b19ea60d80a6 Mon Sep 17 00:00:00 2001 From: Vasily Litvinov Date: Thu, 7 Nov 2019 14:48:30 +0300 Subject: [PATCH 2/2] Add forgotten Close() for opened console output --- terminal_size_windows.go | 1 + 1 file changed, 1 insertion(+) diff --git a/terminal_size_windows.go b/terminal_size_windows.go index 390a525..94b7ed0 100644 --- a/terminal_size_windows.go +++ b/terminal_size_windows.go @@ -12,6 +12,7 @@ func getTermSize() (int, int) { if err != nil { return 0, 0 } + defer out.Close() var csbi consoleScreenBufferInfo ret, _, _ := procGetConsoleScreenBufferInfo.Call(out.Fd(), uintptr(unsafe.Pointer(&csbi)))