rclone/cmd/progress_windows.go

53 lines
1.1 KiB
Go
Raw Normal View History

2018-08-17 17:59:59 +02:00
//+build windows
package cmd
import (
"fmt"
"os"
"syscall"
2018-08-17 17:59:59 +02:00
ansiterm "github.com/Azure/go-ansiterm"
"github.com/Azure/go-ansiterm/winterm"
"github.com/pkg/errors"
2018-08-17 17:59:59 +02:00
)
var (
ansiParser *ansiterm.AnsiParser
2018-08-17 17:59:59 +02:00
)
func init() {
// Default terminal is Windows console for Windows
initTerminal = initTerminalWindows
writeToTerminal = writeToTerminalWindows
}
func initTerminalWindows() error {
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
}
func writeToTerminalWindows(b []byte) {
// 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)
}
}