2023-05-01 09:19:09 +00:00
|
|
|
package restore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/ui"
|
|
|
|
)
|
|
|
|
|
|
|
|
type textPrinter struct {
|
2024-07-27 23:06:26 +00:00
|
|
|
*ui.Message
|
|
|
|
|
|
|
|
terminal ui.Terminal
|
2023-05-01 09:19:09 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
func NewTextProgress(terminal ui.Terminal, verbosity uint) ProgressPrinter {
|
2023-05-01 09:19:09 +00:00
|
|
|
return &textPrinter{
|
2024-07-27 23:06:26 +00:00
|
|
|
Message: ui.NewMessage(terminal, verbosity),
|
|
|
|
terminal: terminal,
|
2023-05-01 09:19:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
func (t *textPrinter) Update(p State, duration time.Duration) {
|
2023-05-01 09:19:09 +00:00
|
|
|
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)
|
2024-05-31 12:12:06 +00:00
|
|
|
if p.FilesSkipped > 0 {
|
|
|
|
progress += fmt.Sprintf(", skipped %v files/dirs %v", p.FilesSkipped, ui.FormatBytes(p.AllBytesSkipped))
|
|
|
|
}
|
2023-05-01 09:19:09 +00:00
|
|
|
|
|
|
|
t.terminal.SetStatus([]string{progress})
|
|
|
|
}
|
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
func (t *textPrinter) Error(item string, err error) error {
|
|
|
|
t.E("ignoring error for %s: %s\n", item, err)
|
|
|
|
return nil
|
|
|
|
}
|
2024-05-31 18:38:51 +00:00
|
|
|
|
2024-07-27 23:06:26 +00:00
|
|
|
func (t *textPrinter) CompleteItem(messageType ItemAction, item string, size uint64) {
|
2024-05-31 18:38:51 +00:00
|
|
|
var action string
|
|
|
|
switch messageType {
|
|
|
|
case ActionDirRestored:
|
|
|
|
action = "restored"
|
|
|
|
case ActionFileRestored:
|
|
|
|
action = "restored"
|
2024-07-20 10:46:28 +00:00
|
|
|
case ActionOtherRestored:
|
|
|
|
action = "restored"
|
2024-05-31 18:38:51 +00:00
|
|
|
case ActionFileUpdated:
|
|
|
|
action = "updated"
|
|
|
|
case ActionFileUnchanged:
|
|
|
|
action = "unchanged"
|
2024-06-29 19:29:42 +00:00
|
|
|
case ActionDeleted:
|
|
|
|
action = "deleted"
|
2024-05-31 18:38:51 +00:00
|
|
|
default:
|
|
|
|
panic("unknown message type")
|
|
|
|
}
|
|
|
|
|
2024-07-20 10:46:28 +00:00
|
|
|
if messageType == ActionDirRestored || messageType == ActionOtherRestored || messageType == ActionDeleted {
|
2024-07-27 23:06:26 +00:00
|
|
|
t.VV("%-9v %v", action, item)
|
2024-05-31 18:38:51 +00:00
|
|
|
} else {
|
2024-07-27 23:06:26 +00:00
|
|
|
t.VV("%-9v %v with size %v", action, item, ui.FormatBytes(size))
|
2024-05-31 18:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 11:43:57 +00:00
|
|
|
func (t *textPrinter) Finish(p State, duration time.Duration) {
|
2024-07-06 09:27:35 +00:00
|
|
|
t.terminal.SetStatus(nil)
|
2023-05-01 09:19:09 +00:00
|
|
|
|
|
|
|
timeLeft := ui.FormatDuration(duration)
|
2024-05-31 11:43:57 +00:00
|
|
|
formattedAllBytesTotal := ui.FormatBytes(p.AllBytesTotal)
|
2023-05-01 09:19:09 +00:00
|
|
|
|
|
|
|
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)
|
2023-05-01 09:19:09 +00:00
|
|
|
} 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)
|
2023-05-01 09:19:09 +00:00
|
|
|
}
|
2024-05-31 12:12:06 +00:00
|
|
|
if p.FilesSkipped > 0 {
|
|
|
|
summary += fmt.Sprintf(", skipped %v files/dirs %v", p.FilesSkipped, ui.FormatBytes(p.AllBytesSkipped))
|
|
|
|
}
|
2023-05-01 09:19:09 +00:00
|
|
|
|
|
|
|
t.terminal.Print(summary)
|
|
|
|
}
|