Created
September 27, 2021 18:24
-
-
Save crazy-max/9b76c1b168cb21015d3d971fab2f1553 to your computer and use it in GitHub Desktop.
term_windows.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//go:build windows | |
// +build windows | |
package term | |
import ( | |
"fmt" | |
"unsafe" | |
"golang.org/x/sys/windows" | |
) | |
const ( | |
foregroundBlue = uint16(0x0001) | |
foregroundGreen = uint16(0x0002) | |
foregroundRed = uint16(0x0004) | |
foregroundIntensity = uint16(0x0008) | |
backgroundBlue = uint16(0x0010) | |
backgroundGreen = uint16(0x0020) | |
backgroundRed = uint16(0x0040) | |
backgroundIntensity = uint16(0x0080) | |
underscore = uint16(0x8000) | |
foregroundMask = foregroundBlue | foregroundGreen | foregroundRed | foregroundIntensity | |
backgroundMask = backgroundBlue | backgroundGreen | backgroundRed | backgroundIntensity | |
) | |
// textAttributes defines attributes of the characters written to a screen buffer. | |
// https://docs.microsoft.com/en-us/windows/console/console-screen-buffer-info-str | |
type textAttributes struct { | |
foregroundColor uint16 | |
backgroundColor uint16 | |
foregroundIntensity uint16 | |
backgroundIntensity uint16 | |
underscore uint16 | |
otherAttributes uint16 | |
} | |
// coord defines the coordinates of a character cell in a console screen buffer. | |
// https://docs.microsoft.com/en-us/windows/console/coord-str | |
type coord struct { | |
X, Y int16 | |
} | |
// smallRect defines the coordinates of the upper left and lower right corners of a rectangle. | |
// https://docs.microsoft.com/en-us/windows/console/small-rect-str | |
type smallRect struct { | |
Left, Top, Right, Bottom int16 | |
} | |
// consoleScreenBufferInfo contains information about a console screen buffer. | |
// https://docs.microsoft.com/en-us/windows/console/console-screen-buffer-info-str | |
type consoleScreenBufferInfo struct { | |
DwSize coord | |
DwCursorPosition coord | |
WAttributes uint16 | |
SrWindow smallRect | |
DwMaximumWindowSize coord | |
} | |
var ( | |
kernel32 = windows.NewLazyDLL("kernel32.dll") | |
defaultAttr *textAttributes | |
) | |
func init() { | |
screenInfo := getConsoleScreenBufferInfo(uintptr(windows.Stdout)) | |
if screenInfo != nil { | |
defaultAttr = &textAttributes{ | |
foregroundColor: screenInfo.WAttributes & (foregroundRed | foregroundGreen | foregroundBlue), | |
backgroundColor: screenInfo.WAttributes & (backgroundRed | backgroundGreen | backgroundBlue), | |
foregroundIntensity: screenInfo.WAttributes & foregroundIntensity, | |
backgroundIntensity: screenInfo.WAttributes & backgroundIntensity, | |
underscore: screenInfo.WAttributes & underscore, | |
otherAttributes: screenInfo.WAttributes &^ (foregroundMask | backgroundMask | underscore), | |
} | |
} | |
} | |
// getConsoleScreenBufferInfo retrieves information about the specified console screen buffer. | |
// https://docs.microsoft.com/en-us/windows/console/getconsolescreenbufferinfo | |
func getConsoleScreenBufferInfo(hConsoleOutput uintptr) *consoleScreenBufferInfo { | |
var csbi consoleScreenBufferInfo | |
ret, _, _ := kernel32.NewProc("GetConsoleScreenBufferInfo").Call( | |
hConsoleOutput, | |
uintptr(unsafe.Pointer(&csbi))) | |
if ret == 0 { | |
return nil | |
} | |
return &csbi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment