2018-04-22 09:57:11 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package termstatus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2020-02-29 14:56:26 +00:00
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2020-02-29 16:22:38 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2018-04-22 09:57:11 +00:00
|
|
|
)
|
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
// clearCurrentLine removes all characters from the current line and resets the
|
|
|
|
// cursor position to the first column.
|
|
|
|
func clearCurrentLine(wr io.Writer, fd uintptr) func(io.Writer, uintptr) {
|
2018-04-22 09:57:11 +00:00
|
|
|
// easy case, the terminal is cmd or psh, without redirection
|
|
|
|
if isWindowsTerminal(fd) {
|
2018-05-07 19:40:07 +00:00
|
|
|
return windowsClearCurrentLine
|
|
|
|
}
|
|
|
|
|
|
|
|
// assume we're running in mintty/cygwin
|
|
|
|
return posixClearCurrentLine
|
|
|
|
}
|
|
|
|
|
|
|
|
// moveCursorUp moves the cursor to the line n lines above the current one.
|
|
|
|
func moveCursorUp(wr io.Writer, fd uintptr) func(io.Writer, uintptr, int) {
|
|
|
|
// easy case, the terminal is cmd or psh, without redirection
|
|
|
|
if isWindowsTerminal(fd) {
|
|
|
|
return windowsMoveCursorUp
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assume we're running in mintty/cygwin
|
2018-05-07 19:40:07 +00:00
|
|
|
return posixMoveCursorUp
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var kernel32 = syscall.NewLazyDLL("kernel32.dll")
|
|
|
|
|
|
|
|
var (
|
|
|
|
procSetConsoleCursorPosition = kernel32.NewProc("SetConsoleCursorPosition")
|
|
|
|
procFillConsoleOutputCharacter = kernel32.NewProc("FillConsoleOutputCharacterW")
|
|
|
|
procFillConsoleOutputAttribute = kernel32.NewProc("FillConsoleOutputAttribute")
|
|
|
|
)
|
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
// windowsClearCurrentLine removes all characters from the current line and
|
|
|
|
// resets the cursor position to the first column.
|
|
|
|
func windowsClearCurrentLine(wr io.Writer, fd uintptr) {
|
2020-02-29 16:22:38 +00:00
|
|
|
var info windows.ConsoleScreenBufferInfo
|
|
|
|
windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info)
|
2018-04-22 09:57:11 +00:00
|
|
|
|
2018-05-07 19:40:07 +00:00
|
|
|
// clear the line
|
2020-02-29 16:22:38 +00:00
|
|
|
cursor := windows.Coord{
|
|
|
|
X: info.Window.Left,
|
|
|
|
Y: info.CursorPosition.Y,
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
2020-02-29 16:22:38 +00:00
|
|
|
var count, w uint32
|
|
|
|
count = uint32(info.Size.X)
|
|
|
|
procFillConsoleOutputAttribute.Call(fd, uintptr(info.Attributes), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&w)))
|
2018-05-07 19:40:07 +00:00
|
|
|
procFillConsoleOutputCharacter.Call(fd, uintptr(' '), uintptr(count), *(*uintptr)(unsafe.Pointer(&cursor)), uintptr(unsafe.Pointer(&w)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// windowsMoveCursorUp moves the cursor to the line n lines above the current one.
|
|
|
|
func windowsMoveCursorUp(wr io.Writer, fd uintptr, n int) {
|
2020-02-29 16:22:38 +00:00
|
|
|
var info windows.ConsoleScreenBufferInfo
|
|
|
|
windows.GetConsoleScreenBufferInfo(windows.Handle(fd), &info)
|
2018-04-22 09:57:11 +00:00
|
|
|
|
|
|
|
// move cursor up by n lines and to the first column
|
2020-02-29 16:22:38 +00:00
|
|
|
info.CursorPosition.Y -= int16(n)
|
|
|
|
info.CursorPosition.X = 0
|
|
|
|
procSetConsoleCursorPosition.Call(fd, uintptr(*(*int32)(unsafe.Pointer(&info.CursorPosition))))
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isWindowsTerminal return true if the file descriptor is a windows terminal (cmd, psh).
|
|
|
|
func isWindowsTerminal(fd uintptr) bool {
|
2020-02-29 14:56:26 +00:00
|
|
|
return terminal.IsTerminal(int(fd))
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 16:22:38 +00:00
|
|
|
func isPipe(fd uintptr) bool {
|
|
|
|
typ, err := windows.GetFileType(windows.Handle(fd))
|
|
|
|
return err == nil && typ == windows.FILE_TYPE_PIPE
|
2018-04-22 09:57:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// canUpdateStatus returns true if status lines can be printed, the process
|
|
|
|
// output is not redirected to a file or pipe.
|
|
|
|
func canUpdateStatus(fd uintptr) bool {
|
|
|
|
// easy case, the terminal is cmd or psh, without redirection
|
|
|
|
if isWindowsTerminal(fd) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the output file type is a pipe (0x0003)
|
2020-02-29 16:22:38 +00:00
|
|
|
if isPipe(fd) {
|
2018-04-22 09:57:11 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// assume we're running in mintty/cygwin
|
|
|
|
return true
|
|
|
|
}
|