1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
// Copyright 2013 Apcera Inc. All rights reserved.
// +build windows
package term
// Used when we have no other source for getting platform-specific information
// about the terminal sizes available.
import (
"os"
"syscall"
"unsafe"
)
// Based on source from from golang.org/x/crypto/ssh/terminal/util_windows.go
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
)
type (
short int16
word uint16
coord struct {
x short
y short
}
smallRect struct {
left short
top short
right short
bottom short
}
consoleScreenBufferInfo struct {
size coord
cursorPosition coord
attributes word
window smallRect
maximumWindowSize coord
}
)
// GetTerminalWindowSize returns the width and height of a terminal in Windows.
func GetTerminalWindowSize(file *os.File) (*Size, error) {
var info consoleScreenBufferInfo
_, _, e := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, file.Fd(), uintptr(unsafe.Pointer(&info)), 0)
if e != 0 {
return nil, error(e)
}
return &Size{
Lines: int(info.size.y),
Columns: int(info.size.x),
}, nil
}
|