mirror of
https://github.com/restic/restic.git
synced 2025-12-11 18:21:54 +00:00
use standard line clearing in printProgress
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -197,24 +196,6 @@ func collectBackends() *location.Registry {
|
||||
return backends
|
||||
}
|
||||
|
||||
// ClearLine creates a platform dependent string to clear the current
|
||||
// line, so it can be overwritten.
|
||||
//
|
||||
// w should be the terminal width, or 0 to let clearLine figure it out.
|
||||
func clearLine(w int) string {
|
||||
if runtime.GOOS != "windows" {
|
||||
return "\x1b[2K"
|
||||
}
|
||||
|
||||
// ANSI sequences are not supported on Windows cmd shell.
|
||||
if w <= 0 {
|
||||
if w = terminal.StdoutWidth(); w <= 0 {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
return strings.Repeat(" ", w-1) + "\r"
|
||||
}
|
||||
|
||||
// Printf writes the message to the configured stdout stream.
|
||||
func Printf(format string, args ...interface{}) {
|
||||
_, err := fmt.Fprintf(globalOptions.stdout, format, args...)
|
||||
|
||||
@@ -68,7 +68,6 @@ func newProgressMax(show bool, max uint64, description string) *progress.Counter
|
||||
}
|
||||
|
||||
func printProgress(status string, final bool) {
|
||||
|
||||
canUpdateStatus := terminal.StdoutCanUpdateStatus()
|
||||
|
||||
w := terminal.StdoutWidth()
|
||||
@@ -83,12 +82,7 @@ func printProgress(status string, final bool) {
|
||||
}
|
||||
}
|
||||
|
||||
var carriageControl, cl string
|
||||
|
||||
if canUpdateStatus {
|
||||
cl = clearLine(w)
|
||||
}
|
||||
|
||||
var carriageControl string
|
||||
if !(strings.HasSuffix(status, "\r") || strings.HasSuffix(status, "\n")) {
|
||||
if canUpdateStatus {
|
||||
carriageControl = "\r"
|
||||
@@ -97,7 +91,12 @@ func printProgress(status string, final bool) {
|
||||
}
|
||||
}
|
||||
|
||||
_, _ = os.Stdout.Write([]byte(cl + status + carriageControl))
|
||||
if canUpdateStatus {
|
||||
clearCurrentLine := terminal.ClearCurrentLine(os.Stdout.Fd())
|
||||
clearCurrentLine(os.Stdout, os.Stdout.Fd())
|
||||
}
|
||||
|
||||
_, _ = os.Stdout.Write([]byte(status + carriageControl))
|
||||
if final {
|
||||
_, _ = os.Stdout.Write([]byte("\n"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user