use standard line clearing in printProgress

This commit is contained in:
Michael Eischer
2025-09-07 14:47:31 +02:00
parent 529baf50f8
commit e7890d7b81
2 changed files with 7 additions and 27 deletions

View File

@@ -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...)

View File

@@ -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"))
}