| 1 |
//go:build windows |
| 2 |
// +build windows |
| 3 |
|
| 4 |
package termutil |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"os/exec" |
| 10 |
"strconv" |
| 11 |
"syscall" |
| 12 |
"unsafe" |
| 13 |
) |
| 14 |
|
| 15 |
var ( |
| 16 |
tty = os.Stdin |
| 17 |
|
| 18 |
unlockSignals = []os.Signal{ |
| 19 |
os.Interrupt, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL, |
| 20 |
} |
| 21 |
) |
| 22 |
|
| 23 |
var ( |
| 24 |
kernel32 = syscall.NewLazyDLL("kernel32.dll") |
| 25 |
|
| 26 |
// GetConsoleScreenBufferInfo retrieves information about the |
| 27 |
// specified console screen buffer. |
| 28 |
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx |
| 29 |
procGetConsoleScreenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo") |
| 30 |
|
| 31 |
// GetConsoleMode retrieves the current input mode of a console's |
| 32 |
// input buffer or the current output mode of a console screen buffer. |
| 33 |
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683167(v=vs.85).aspx |
| 34 |
getConsoleMode = kernel32.NewProc("GetConsoleMode") |
| 35 |
|
| 36 |
// SetConsoleMode sets the input mode of a console's input buffer |
| 37 |
// or the output mode of a console screen buffer. |
| 38 |
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx |
| 39 |
setConsoleMode = kernel32.NewProc("SetConsoleMode") |
| 40 |
|
| 41 |
// SetConsoleCursorPosition sets the cursor position in the |
| 42 |
// specified console screen buffer. |
| 43 |
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx |
| 44 |
setConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition") |
| 45 |
|
| 46 |
mingw = isMingw() |
| 47 |
) |
| 48 |
|
| 49 |
type ( |
| 50 |
// Defines the coordinates of the upper left and lower right corners |
| 51 |
// of a rectangle. |
| 52 |
// See |
| 53 |
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms686311(v=vs.85).aspx |
| 54 |
smallRect struct { |
| 55 |
Left, Top, Right, Bottom int16 |
| 56 |
} |
| 57 |
|
| 58 |
// Defines the coordinates of a character cell in a console screen |
| 59 |
// buffer. The origin of the coordinate system (0,0) is at the top, left cell |
| 60 |
// of the buffer. |
| 61 |
// See |
| 62 |
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119(v=vs.85).aspx |
| 63 |
coordinates struct { |
| 64 |
X, Y int16 |
| 65 |
} |
| 66 |
|
| 67 |
word int16 |
| 68 |
|
| 69 |
// Contains information about a console screen buffer. |
| 70 |
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682093(v=vs.85).aspx |
| 71 |
consoleScreenBufferInfo struct { |
| 72 |
dwSize coordinates |
| 73 |
dwCursorPosition coordinates |
| 74 |
wAttributes word |
| 75 |
srWindow smallRect |
| 76 |
dwMaximumWindowSize coordinates |
| 77 |
} |
| 78 |
) |
| 79 |
|
| 80 |
// TerminalWidth returns width of the terminal. |
| 81 |
func TerminalWidth() (width int, err error) { |
| 82 |
if mingw { |
| 83 |
return termWidthTPut() |
| 84 |
} |
| 85 |
return termWidthCmd() |
| 86 |
} |
| 87 |
|
| 88 |
func termWidthCmd() (width int, err error) { |
| 89 |
var info consoleScreenBufferInfo |
| 90 |
_, _, e := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, uintptr(syscall.Stdout), uintptr(unsafe.Pointer(&info)), 0) |
| 91 |
if e != 0 { |
| 92 |
return 0, error(e) |
| 93 |
} |
| 94 |
return int(info.dwSize.X) - 1, nil |
| 95 |
} |
| 96 |
|
| 97 |
func isMingw() bool { |
| 98 |
return os.Getenv("MINGW_PREFIX") != "" || os.Getenv("MSYSTEM") == "MINGW64" |
| 99 |
} |
| 100 |
|
| 101 |
func termWidthTPut() (width int, err error) { |
| 102 |
// TODO: maybe anybody knows a better way to get it on mintty... |
| 103 |
var res []byte |
| 104 |
cmd := exec.Command("tput", "cols") |
| 105 |
cmd.Stdin = os.Stdin |
| 106 |
if res, err = cmd.CombinedOutput(); err != nil { |
| 107 |
return 0, fmt.Errorf("%s: %v", string(res), err) |
| 108 |
} |
| 109 |
if len(res) > 1 { |
| 110 |
res = res[:len(res)-1] |
| 111 |
} |
| 112 |
return strconv.Atoi(string(res)) |
| 113 |
} |
| 114 |
|
| 115 |
func GetCursorPos() (pos coordinates, err error) { |
| 116 |
var info consoleScreenBufferInfo |
| 117 |
_, _, e := syscall.Syscall(procGetConsoleScreenBufferInfo.Addr(), 2, uintptr(syscall.Stdout), uintptr(unsafe.Pointer(&info)), 0) |
| 118 |
if e != 0 { |
| 119 |
return info.dwCursorPosition, error(e) |
| 120 |
} |
| 121 |
return info.dwCursorPosition, nil |
| 122 |
} |
| 123 |
|
| 124 |
func SetCursorPos(pos coordinates) error { |
| 125 |
_, _, e := syscall.Syscall(setConsoleCursorPosition.Addr(), 2, uintptr(syscall.Stdout), uintptr(uint32(uint16(pos.Y))<<16|uint32(uint16(pos.X))), 0) |
| 126 |
if e != 0 { |
| 127 |
return error(e) |
| 128 |
} |
| 129 |
return nil |
| 130 |
} |
| 131 |
|
| 132 |
var oldState word |
| 133 |
|
| 134 |
func lockEcho() (err error) { |
| 135 |
if _, _, e := syscall.Syscall(getConsoleMode.Addr(), 2, uintptr(syscall.Stdout), uintptr(unsafe.Pointer(&oldState)), 0); e != 0 { |
| 136 |
err = fmt.Errorf("Can't get terminal settings: %v", e) |
| 137 |
return |
| 138 |
} |
| 139 |
|
| 140 |
newState := oldState |
| 141 |
const ENABLE_LINE_INPUT = 0x0002 |
| 142 |
newState = newState & (^ENABLE_LINE_INPUT) |
| 143 |
if _, _, e := syscall.Syscall(setConsoleMode.Addr(), 2, uintptr(syscall.Stdout), uintptr(newState), 0); e != 0 { |
| 144 |
err = fmt.Errorf("Can't set terminal settings: %v", e) |
| 145 |
return |
| 146 |
} |
| 147 |
return |
| 148 |
} |
| 149 |
|
| 150 |
func unlockEcho() (err error) { |
| 151 |
if _, _, e := syscall.Syscall(setConsoleMode.Addr(), 2, uintptr(syscall.Stdout), uintptr(oldState), 0); e != 0 { |
| 152 |
err = fmt.Errorf("Can't set terminal settings") |
| 153 |
} |
| 154 |
return |
| 155 |
} |