Further fracturing of termios to try and handle partial coverage for windows

This commit is contained in:
sloum 2022-04-21 22:35:51 -07:00
parent 3a61428145
commit b9c0a034b7
2 changed files with 94 additions and 1 deletions

View File

@ -1,4 +1,4 @@
// +build !linux
// +build !linux,!windows
package termios

View File

@ -0,0 +1,93 @@
// +build windows
package termios
import (
"fmt"
"syscall"
"unsafe"
)
type (
short int16
word uint16
smallRect struct {
Left short
Top short
Right short
Bottom short
}
coord struct {
X short
Y short
}
consoleScreenBufferInfo struct {
Size coord
CursorPosition coord
Attributes word
Window smallRect
MaximumWindowSize coord
}
)
var kernel32DLL = syscall.NewLazyDLL("kernel32.dll")
var getConsoleScreenBufferInfoProc = kernel32DLL.NewProc("GetConsoleScreenBufferInfo")
// GetConsoleSize returns the current number of columns and rows in the active console window.
// The return value of this function is in the order of cols, rows.
func GetWindowSize() (int, int) {
stdoutHandle := getStdHandle(syscall.STD_OUTPUT_HANDLE)
var info, err = getConsoleScreenBufferInfo(stdoutHandle)
if err != nil {
return 0, 0
}
return int(info.Window.Right - info.Window.Left + 1), int(info.Window.Bottom - info.Window.Top + 1)
}
func getError(r1, r2 uintptr, lastErr error) error {
// If the function fails, the return value is zero.
if r1 == 0 {
if lastErr != nil {
return lastErr
}
return syscall.EINVAL
}
return nil
}
func getStdHandle(stdhandle int) uintptr {
handle, err := syscall.GetStdHandle(stdhandle)
if err != nil {
panic(fmt.Errorf("could not get standard io handle %d", stdhandle))
}
return uintptr(handle)
}
func getConsoleScreenBufferInfo(handle uintptr) (*consoleScreenBufferInfo, error) {
var info consoleScreenBufferInfo
if err := getError(getConsoleScreenBufferInfoProc.Call(handle, uintptr(unsafe.Pointer(&info)), 0)); err != nil {
return nil, err
}
return &info, nil
}
// The following are all stubs to allow for
// some level of windows usage until windows
// solutions can be worked out and verified
// by someone that uses windows (ie. not me)
func SetCharMode() {
}
func SetRawMode() {
}
func SetSaneMode() {
}
func SetCookedMode() {
}
func Restore() {
}