2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-23 11:28:54 +00:00

Merge pull request #583 from stuertz/windowsoutput

Fix progress output on Windows
This commit is contained in:
Alexander Neumann 2016-08-23 21:18:09 +02:00
commit 9fd941f6fc
5 changed files with 24 additions and 9 deletions

View File

@ -57,7 +57,7 @@ func RunCleanupHandlers() {
func CleanupHandler(c <-chan os.Signal) { func CleanupHandler(c <-chan os.Signal) {
for s := range c { for s := range c {
debug.Log("CleanupHandler", "signal %v received, cleaning up", s) debug.Log("CleanupHandler", "signal %v received, cleaning up", s)
fmt.Println("\x1b[2KInterrupt received, cleaning up") fmt.Printf("%sInterrupt received, cleaning up\n", ClearLine())
RunCleanupHandlers() RunCleanupHandlers()
fmt.Println("exiting") fmt.Println("exiting")
os.Exit(0) os.Exit(0)

View File

@ -114,10 +114,10 @@ func (cmd CmdBackup) newScanProgress() *restic.Progress {
p := restic.NewProgress(time.Second) p := restic.NewProgress(time.Second)
p.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) { p.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
fmt.Printf("\x1b[2K[%s] %d directories, %d files, %s\r", formatDuration(d), s.Dirs, s.Files, formatBytes(s.Bytes)) fmt.Printf("%s[%s] %d directories, %d files, %s\r", ClearLine(), formatDuration(d), s.Dirs, s.Files, formatBytes(s.Bytes))
} }
p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) { p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
fmt.Printf("\x1b[2Kscanned %d directories, %d files in %s\n", s.Dirs, s.Files, formatDuration(d)) fmt.Printf("%sscanned %d directories, %d files in %s\n", ClearLine(), s.Dirs, s.Files, formatDuration(d))
} }
return p return p
@ -157,7 +157,7 @@ func (cmd CmdBackup) newArchiveProgress(todo restic.Stat) *restic.Progress {
w, _, err := terminal.GetSize(int(os.Stdout.Fd())) w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err == nil { if err == nil {
maxlen := w - len(status2) maxlen := w - len(status2) - 1
if maxlen < 4 { if maxlen < 4 {
status1 = "" status1 = ""
@ -167,7 +167,7 @@ func (cmd CmdBackup) newArchiveProgress(todo restic.Stat) *restic.Progress {
} }
} }
fmt.Printf("\x1b[2K%s%s\r", status1, status2) fmt.Printf("%s%s%s\r", ClearLine(), status1, status2)
} }
archiveProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) { archiveProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
@ -208,7 +208,7 @@ func (cmd CmdBackup) newArchiveStdinProgress() *restic.Progress {
} }
} }
fmt.Printf("\x1b[2K%s\r", status1) fmt.Printf("%s%s\r", ClearLine(), status1)
} }
archiveProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) { archiveProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
@ -375,7 +375,7 @@ func (cmd CmdBackup) Execute(args []string) error {
arch.Error = func(dir string, fi os.FileInfo, err error) error { arch.Error = func(dir string, fi os.FileInfo, err error) error {
// TODO: make ignoring errors configurable // TODO: make ignoring errors configurable
cmd.global.Warnf("\x1b[2K\rerror for %s: %v\n", dir, err) cmd.global.Warnf("%s\rerror for %s: %v\n", ClearLine(), dir, err)
return nil return nil
} }

View File

@ -54,7 +54,7 @@ func (cmd CmdCheck) newReadProgress(todo restic.Stat) *restic.Progress {
} }
} }
fmt.Printf("\x1b[2K%s\r", status) fmt.Printf("%s%s\r", ClearLine(), status)
} }
readProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) { readProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {

View File

@ -55,7 +55,7 @@ func newProgressMax(show bool, max uint64, description string) *restic.Progress
} }
} }
fmt.Printf("\x1b[2K%s\r", status) fmt.Printf("%s%s\r", ClearLine(), status)
} }
p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) { p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"runtime"
"strings" "strings"
"syscall" "syscall"
@ -82,6 +83,20 @@ func restoreTerminal() {
var globalOpts = GlobalOptions{stdout: os.Stdout, stderr: os.Stderr} var globalOpts = GlobalOptions{stdout: os.Stdout, stderr: os.Stderr}
var parser = flags.NewParser(&globalOpts, flags.HelpFlag|flags.PassDoubleDash) var parser = flags.NewParser(&globalOpts, flags.HelpFlag|flags.PassDoubleDash)
// ClearLine creates a platform dependent string to clear the current
// line, so it can be overwritten. ANSI sequences are not supported on
// current windows cmd shell.
func ClearLine() string {
if runtime.GOOS == "windows" {
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err == nil {
return strings.Repeat(" ", w-1) + "\r"
}
return ""
}
return "\x1b[2K"
}
// Printf writes the message to the configured stdout stream. // Printf writes the message to the configured stdout stream.
func (o GlobalOptions) Printf(format string, args ...interface{}) { func (o GlobalOptions) Printf(format string, args ...interface{}) {
_, err := fmt.Fprintf(o.stdout, format, args...) _, err := fmt.Fprintf(o.stdout, format, args...)