mirror of
https://github.com/octoleo/restic.git
synced 2025-01-10 18:04:38 +00:00
Merge pull request #3501 from greatroar/printprogress
Streamline progress printing in cmd/restic
This commit is contained in:
commit
a29777f467
@ -58,7 +58,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("signal %v received, cleaning up", s)
|
debug.Log("signal %v received, cleaning up", s)
|
||||||
Warnf("%ssignal %v received, cleaning up\n", ClearLine(), s)
|
Warnf("%ssignal %v received, cleaning up\n", clearLine(0), s)
|
||||||
|
|
||||||
code := 0
|
code := 0
|
||||||
|
|
||||||
|
@ -119,18 +119,6 @@ func verifyPruneOptions(opts *PruneOptions) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func shortenStatus(maxLength int, s string) string {
|
|
||||||
if len(s) <= maxLength {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
if maxLength < 3 {
|
|
||||||
return s[:maxLength]
|
|
||||||
}
|
|
||||||
|
|
||||||
return s[:maxLength-3] + "..."
|
|
||||||
}
|
|
||||||
|
|
||||||
func runPrune(opts PruneOptions, gopts GlobalOptions) error {
|
func runPrune(opts PruneOptions, gopts GlobalOptions) error {
|
||||||
err := verifyPruneOptions(&opts)
|
err := verifyPruneOptions(&opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -197,16 +197,21 @@ func restoreTerminal() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ClearLine creates a platform dependent string to clear the current
|
// ClearLine creates a platform dependent string to clear the current
|
||||||
// line, so it can be overwritten. ANSI sequences are not supported on
|
// line, so it can be overwritten.
|
||||||
// current windows cmd shell.
|
//
|
||||||
func ClearLine() string {
|
// w should be the terminal width, or 0 to let clearLine figure it out.
|
||||||
if runtime.GOOS == "windows" {
|
func clearLine(w int) string {
|
||||||
if w := stdoutTerminalWidth(); w > 0 {
|
if runtime.GOOS != "windows" {
|
||||||
return strings.Repeat(" ", w-1) + "\r"
|
return "\x1b[2K"
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
return "\x1b[2K"
|
|
||||||
|
// ANSI sequences are not supported on Windows cmd shell.
|
||||||
|
if w <= 0 {
|
||||||
|
if w = stdoutTerminalWidth(); w <= 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strings.Repeat(" ", w-1) + "\r"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Printf writes the message to the configured stdout stream.
|
// Printf writes the message to the configured stdout stream.
|
||||||
@ -247,31 +252,6 @@ func Verboseff(format string, args ...interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintProgress wraps fmt.Printf to handle the difference in writing progress
|
|
||||||
// information to terminals and non-terminal stdout
|
|
||||||
func PrintProgress(format string, args ...interface{}) {
|
|
||||||
var (
|
|
||||||
message string
|
|
||||||
carriageControl string
|
|
||||||
)
|
|
||||||
message = fmt.Sprintf(format, args...)
|
|
||||||
|
|
||||||
if !(strings.HasSuffix(message, "\r") || strings.HasSuffix(message, "\n")) {
|
|
||||||
if stdoutCanUpdateStatus() {
|
|
||||||
carriageControl = "\r"
|
|
||||||
} else {
|
|
||||||
carriageControl = "\n"
|
|
||||||
}
|
|
||||||
message = fmt.Sprintf("%s%s", message, carriageControl)
|
|
||||||
}
|
|
||||||
|
|
||||||
if stdoutCanUpdateStatus() {
|
|
||||||
message = fmt.Sprintf("%s%s", ClearLine(), message)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Warnf writes the message to the configured stderr stream.
|
// Warnf writes the message to the configured stderr stream.
|
||||||
func Warnf(format string, args ...interface{}) {
|
func Warnf(format string, args ...interface{}) {
|
||||||
_, err := fmt.Fprintf(globalOptions.stderr, format, args...)
|
_, err := fmt.Fprintf(globalOptions.stderr, format, args...)
|
||||||
|
@ -4,9 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/ui/progress"
|
"github.com/restic/restic/internal/ui/progress"
|
||||||
|
"github.com/restic/restic/internal/ui/termstatus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// calculateProgressInterval returns the interval configured via RESTIC_PROGRESS_FPS
|
// calculateProgressInterval returns the interval configured via RESTIC_PROGRESS_FPS
|
||||||
@ -32,6 +34,7 @@ func newProgressMax(show bool, max uint64, description string) *progress.Counter
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
interval := calculateProgressInterval(show)
|
interval := calculateProgressInterval(show)
|
||||||
|
canUpdateStatus := stdoutCanUpdateStatus()
|
||||||
|
|
||||||
return progress.New(interval, max, func(v uint64, max uint64, d time.Duration, final bool) {
|
return progress.New(interval, max, func(v uint64, max uint64, d time.Duration, final bool) {
|
||||||
var status string
|
var status string
|
||||||
@ -42,13 +45,36 @@ func newProgressMax(show bool, max uint64, description string) *progress.Counter
|
|||||||
formatDuration(d), formatPercent(v, max), v, max, description)
|
formatDuration(d), formatPercent(v, max), v, max, description)
|
||||||
}
|
}
|
||||||
|
|
||||||
if w := stdoutTerminalWidth(); w > 0 {
|
printProgress(status, canUpdateStatus)
|
||||||
status = shortenStatus(w, status)
|
|
||||||
}
|
|
||||||
|
|
||||||
PrintProgress("%s", status)
|
|
||||||
if final {
|
if final {
|
||||||
fmt.Print("\n")
|
fmt.Print("\n")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func printProgress(status string, canUpdateStatus bool) {
|
||||||
|
w := stdoutTerminalWidth()
|
||||||
|
if w > 0 {
|
||||||
|
if w < 3 {
|
||||||
|
status = termstatus.Truncate(status, w)
|
||||||
|
} else {
|
||||||
|
status = termstatus.Truncate(status, w-3) + "..."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var carriageControl, clear string
|
||||||
|
|
||||||
|
if canUpdateStatus {
|
||||||
|
clear = clearLine(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !(strings.HasSuffix(status, "\r") || strings.HasSuffix(status, "\n")) {
|
||||||
|
if canUpdateStatus {
|
||||||
|
carriageControl = "\r"
|
||||||
|
} else {
|
||||||
|
carriageControl = "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _ = os.Stdout.Write([]byte(clear + status + carriageControl))
|
||||||
|
}
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"golang.org/x/crypto/ssh/terminal"
|
"golang.org/x/crypto/ssh/terminal"
|
||||||
"golang.org/x/text/width"
|
"golang.org/x/text/width"
|
||||||
@ -280,7 +281,7 @@ func (t *Terminal) Errorf(msg string, args ...interface{}) {
|
|||||||
|
|
||||||
// Truncate s to fit in width (number of terminal cells) w.
|
// Truncate s to fit in width (number of terminal cells) w.
|
||||||
// If w is negative, returns the empty string.
|
// If w is negative, returns the empty string.
|
||||||
func truncate(s string, w int) string {
|
func Truncate(s string, w int) string {
|
||||||
if len(s) < w {
|
if len(s) < w {
|
||||||
// Since the display width of a character is at most 2
|
// Since the display width of a character is at most 2
|
||||||
// and all of ASCII (single byte per rune) has width 1,
|
// and all of ASCII (single byte per rune) has width 1,
|
||||||
@ -289,16 +290,11 @@ func truncate(s string, w int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, r := range s {
|
for i, r := range s {
|
||||||
// Determine width of the rune. This cannot be determined without
|
w--
|
||||||
// knowing the terminal font, so let's just be careful and treat
|
if r > unicode.MaxASCII && wideRune(r) {
|
||||||
// all ambigous characters as full-width, i.e., two cells.
|
w--
|
||||||
wr := 2
|
|
||||||
switch width.LookupRune(r).Kind() {
|
|
||||||
case width.Neutral, width.EastAsianNarrow:
|
|
||||||
wr = 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
w -= wr
|
|
||||||
if w < 0 {
|
if w < 0 {
|
||||||
return s[:i]
|
return s[:i]
|
||||||
}
|
}
|
||||||
@ -307,6 +303,14 @@ func truncate(s string, w int) string {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Guess whether r would occupy two terminal cells instead of one.
|
||||||
|
// This cannot be determined exactly without knowing the terminal font,
|
||||||
|
// so we treat all ambigous runes as full-width, i.e., two cells.
|
||||||
|
func wideRune(r rune) bool {
|
||||||
|
kind := width.LookupRune(r).Kind()
|
||||||
|
return kind != width.Neutral && kind != width.EastAsianNarrow
|
||||||
|
}
|
||||||
|
|
||||||
// SetStatus updates the status lines.
|
// SetStatus updates the status lines.
|
||||||
func (t *Terminal) SetStatus(lines []string) {
|
func (t *Terminal) SetStatus(lines []string) {
|
||||||
if len(lines) == 0 {
|
if len(lines) == 0 {
|
||||||
@ -328,7 +332,7 @@ func (t *Terminal) SetStatus(lines []string) {
|
|||||||
for i, line := range lines {
|
for i, line := range lines {
|
||||||
line = strings.TrimRight(line, "\n")
|
line = strings.TrimRight(line, "\n")
|
||||||
if width > 0 {
|
if width > 0 {
|
||||||
line = truncate(line, width-2)
|
line = Truncate(line, width-2)
|
||||||
}
|
}
|
||||||
lines[i] = line + "\n"
|
lines[i] = line + "\n"
|
||||||
}
|
}
|
||||||
|
@ -19,13 +19,14 @@ func TestTruncate(t *testing.T) {
|
|||||||
{"foo", 0, ""},
|
{"foo", 0, ""},
|
||||||
{"foo", -1, ""},
|
{"foo", -1, ""},
|
||||||
{"Löwen", 4, "Löwe"},
|
{"Löwen", 4, "Löwe"},
|
||||||
{"あああああああああ/data", 10, "あああああ"},
|
{"あああああ/data", 7, "あああ"},
|
||||||
{"あああああああああ/data", 11, "あああああ"},
|
{"あああああ/data", 10, "あああああ"},
|
||||||
|
{"あああああ/data", 11, "あああああ/"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run("", func(t *testing.T) {
|
t.Run("", func(t *testing.T) {
|
||||||
out := truncate(test.input, test.width)
|
out := Truncate(test.input, test.width)
|
||||||
if out != test.output {
|
if out != test.output {
|
||||||
t.Fatalf("wrong output for input %v, width %d: want %q, got %q",
|
t.Fatalf("wrong output for input %v, width %d: want %q, got %q",
|
||||||
test.input, test.width, test.output, out)
|
test.input, test.width, test.output, out)
|
||||||
@ -33,3 +34,26 @@ func TestTruncate(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func benchmarkTruncate(b *testing.B, s string, w int) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Truncate(s, w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkTruncateASCII(b *testing.B) {
|
||||||
|
s := "This is an ASCII-only status message...\r\n"
|
||||||
|
benchmarkTruncate(b, s, len(s)-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkTruncateUnicode(b *testing.B) {
|
||||||
|
s := "Hello World or Καλημέρα κόσμε or こんにちは 世界"
|
||||||
|
w := 0
|
||||||
|
for _, r := range s {
|
||||||
|
w++
|
||||||
|
if wideRune(r) {
|
||||||
|
w++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
benchmarkTruncate(b, s, w-1)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user