rclone/cmd/progress_windows.go

47 lines
951 B
Go
Raw Normal View History

2018-08-17 23:59:59 +08:00
//+build windows
package cmd
import (
"fmt"
"os"
"syscall"
2018-08-17 23:59:59 +08:00
ansiterm "github.com/Azure/go-ansiterm"
"github.com/Azure/go-ansiterm/winterm"
"github.com/pkg/errors"
2018-08-17 23:59:59 +08:00
)
var (
ansiParser *ansiterm.AnsiParser
2018-08-17 23:59:59 +08:00
)
func initTerminal() 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
}
2018-08-17 23:59:59 +08:00
func writeToTerminal(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 23:59:59 +08:00
_, err := ansiParser.Parse(b)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "\n*** Error from ANSI parser: %v\n", err)
}
}