2018-08-17 17:59:59 +02:00
|
|
|
//+build windows
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-12-19 13:36:29 +01:00
|
|
|
"syscall"
|
2018-08-17 17:59:59 +02:00
|
|
|
|
|
|
|
ansiterm "github.com/Azure/go-ansiterm"
|
|
|
|
"github.com/Azure/go-ansiterm/winterm"
|
2018-12-19 13:36:29 +01:00
|
|
|
"github.com/pkg/errors"
|
2018-08-17 17:59:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-19 13:36:29 +01:00
|
|
|
ansiParser *ansiterm.AnsiParser
|
2018-08-17 17:59:59 +02:00
|
|
|
)
|
|
|
|
|
2019-09-13 16:24:47 +02:00
|
|
|
func init() {
|
|
|
|
// Default terminal is Windows console for Windows
|
|
|
|
initTerminal = initTerminalWindows
|
|
|
|
writeToTerminal = writeToTerminalWindows
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTerminalWindows() error {
|
2018-12-19 13:36:29 +01:00
|
|
|
winEventHandler := winterm.CreateWinEventHandler(os.Stdout.Fd(), os.Stdout)
|
|
|
|
if winEventHandler == nil {
|
|
|
|
err := syscall.GetLastError()
|
|
|
|
if err == nil {
|
|
|
|
err = errors.New("initialization failed")
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "windows terminal")
|
|
|
|
}
|
|
|
|
ansiParser = ansiterm.CreateParser("Ground", winEventHandler)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-13 16:24:47 +02:00
|
|
|
func writeToTerminalWindows(b []byte) {
|
2018-09-14 13:01:52 +02:00
|
|
|
// Remove all non-ASCII characters until this is fixed
|
|
|
|
// https://github.com/Azure/go-ansiterm/issues/26
|
|
|
|
r := []rune(string(b))
|
|
|
|
for i := range r {
|
|
|
|
if r[i] >= 127 {
|
|
|
|
r[i] = '.'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b = []byte(string(r))
|
2018-08-17 17:59:59 +02:00
|
|
|
_, err := ansiParser.Parse(b)
|
|
|
|
if err != nil {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "\n*** Error from ANSI parser: %v\n", err)
|
|
|
|
}
|
|
|
|
}
|