1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-23 00:07:25 +00:00
restic/internal/ui/restore/text.go

88 lines
2.5 KiB
Go
Raw Normal View History

package restore
import (
"fmt"
"time"
"github.com/restic/restic/internal/ui"
)
type textPrinter struct {
*ui.Message
terminal ui.Terminal
}
func NewTextProgress(terminal ui.Terminal, verbosity uint) ProgressPrinter {
return &textPrinter{
Message: ui.NewMessage(terminal, verbosity),
terminal: terminal,
}
}
2024-05-31 11:43:57 +00:00
func (t *textPrinter) Update(p State, duration time.Duration) {
timeLeft := ui.FormatDuration(duration)
2024-05-31 11:43:57 +00:00
formattedAllBytesWritten := ui.FormatBytes(p.AllBytesWritten)
formattedAllBytesTotal := ui.FormatBytes(p.AllBytesTotal)
allPercent := ui.FormatPercent(p.AllBytesWritten, p.AllBytesTotal)
2023-09-20 08:58:11 +00:00
progress := fmt.Sprintf("[%s] %s %v files/dirs %s, total %v files/dirs %v",
2024-05-31 11:43:57 +00:00
timeLeft, allPercent, p.FilesFinished, formattedAllBytesWritten, p.FilesTotal, formattedAllBytesTotal)
if p.FilesSkipped > 0 {
progress += fmt.Sprintf(", skipped %v files/dirs %v", p.FilesSkipped, ui.FormatBytes(p.AllBytesSkipped))
}
t.terminal.SetStatus([]string{progress})
}
func (t *textPrinter) Error(item string, err error) error {
t.E("ignoring error for %s: %s\n", item, err)
return nil
}
func (t *textPrinter) CompleteItem(messageType ItemAction, item string, size uint64) {
var action string
switch messageType {
case ActionDirRestored:
action = "restored"
case ActionFileRestored:
action = "restored"
case ActionOtherRestored:
action = "restored"
case ActionFileUpdated:
action = "updated"
case ActionFileUnchanged:
action = "unchanged"
case ActionDeleted:
action = "deleted"
default:
panic("unknown message type")
}
if messageType == ActionDirRestored || messageType == ActionOtherRestored || messageType == ActionDeleted {
t.VV("%-9v %v", action, item)
} else {
t.VV("%-9v %v with size %v", action, item, ui.FormatBytes(size))
}
}
2024-05-31 11:43:57 +00:00
func (t *textPrinter) Finish(p State, duration time.Duration) {
t.terminal.SetStatus(nil)
timeLeft := ui.FormatDuration(duration)
2024-05-31 11:43:57 +00:00
formattedAllBytesTotal := ui.FormatBytes(p.AllBytesTotal)
var summary string
2024-05-31 11:43:57 +00:00
if p.FilesFinished == p.FilesTotal && p.AllBytesWritten == p.AllBytesTotal {
summary = fmt.Sprintf("Summary: Restored %d files/dirs (%s) in %s", p.FilesTotal, formattedAllBytesTotal, timeLeft)
} else {
2024-05-31 11:43:57 +00:00
formattedAllBytesWritten := ui.FormatBytes(p.AllBytesWritten)
2023-09-20 08:58:11 +00:00
summary = fmt.Sprintf("Summary: Restored %d / %d files/dirs (%s / %s) in %s",
2024-05-31 11:43:57 +00:00
p.FilesFinished, p.FilesTotal, formattedAllBytesWritten, formattedAllBytesTotal, timeLeft)
}
if p.FilesSkipped > 0 {
summary += fmt.Sprintf(", skipped %v files/dirs %v", p.FilesSkipped, ui.FormatBytes(p.AllBytesSkipped))
}
t.terminal.Print(summary)
}