From 7969ec305f47540e34b1ac6b65e10585956f4a09 Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Sun, 3 Mar 2024 17:15:38 +0100 Subject: [PATCH] change upload stats report --- .../piwigo/piwigotools/file_to_upload_stat.go | 122 +++++++++++------- 1 file changed, 78 insertions(+), 44 deletions(-) diff --git a/internal/piwigo/piwigotools/file_to_upload_stat.go b/internal/piwigo/piwigotools/file_to_upload_stat.go index ff74b0d..4068336 100644 --- a/internal/piwigo/piwigotools/file_to_upload_stat.go +++ b/internal/piwigo/piwigotools/file_to_upload_stat.go @@ -2,43 +2,73 @@ package piwigotools import ( "fmt" + "github.com/apoorvam/goterminal" + ct "github.com/daviddengcn/go-colortext" "os" + "strings" "sync" + "time" +) - "github.com/schollz/progressbar/v3" +const ( + megabytes = 1 << 20 + kilobytes = 1 << 10 ) type FileToUploadStat struct { - Checked int64 - Total int64 - TotalBytes int64 - Uploaded int64 + Checked uint32 + Total uint32 + Uploaded uint32 + Skipped uint32 + Failed uint32 + UploadedBytes int64 - Skipped int64 - Failed int64 - Progress *progressbar.ProgressBar - mu sync.Mutex + TotalBytes int64 + + progress *goterminal.Writer + mu sync.Mutex + lastRefresh time.Time } func NewFileToUploadStat() *FileToUploadStat { - bar := progressbar.DefaultBytes(1, "...") - progressbar.OptionOnCompletion(func() { _, _ = os.Stderr.WriteString("\n") })(bar) return &FileToUploadStat{ - Progress: bar, + progress: goterminal.New(os.Stdout), } } -func (s *FileToUploadStat) Refresh() { - s.Progress.Describe(fmt.Sprintf( - "%d / %d - check:%d, upload:%d, skip:%d, fail:%d", - s.Uploaded+s.Skipped+s.Failed, - s.Total, - s.Checked, - s.Uploaded, - s.Skipped, - s.Failed, - ), +func (s *FileToUploadStat) formatBytes(b int64) string { + if b > megabytes { + return fmt.Sprintf("%.2fMB", float64(b)/(1<<20)) + } else if b > kilobytes { + return fmt.Sprintf("%.2fKB", float64(b)/(1<<10)) + } else { + return fmt.Sprintf("%dB", b) + } +} + +func (s *FileToUploadStat) refreshIfNeeded() { + if time.Since(s.lastRefresh) > 200*time.Millisecond { + s.refresh() + } + +} + +func (s *FileToUploadStat) refresh() { + s.progress.Clear() + _, _ = s.progress.Write([]byte("Statistics:\n")) + _, _ = fmt.Fprintf( + s.progress, + strings.Repeat("%20s: %d\n", 5)+strings.Repeat("%20s: %s\n", 2), + "Checked", s.Checked, + "Uploaded", s.Uploaded, + "Skipped", s.Skipped, + "Failed", s.Failed, + "Total", s.Total, + "Uploaded Bytes", s.formatBytes(s.UploadedBytes), + "Total Bytes", s.formatBytes(s.TotalBytes), ) + _ = s.progress.Print() + s.lastRefresh = time.Now() } func (s *FileToUploadStat) Check() { @@ -46,16 +76,7 @@ func (s *FileToUploadStat) Check() { defer s.mu.Unlock() s.Checked++ - s.Refresh() -} - -func (s *FileToUploadStat) AddBytes(filesize int64) { - s.mu.Lock() - defer s.mu.Unlock() - - s.TotalBytes += filesize - s.Progress.ChangeMax64(s.TotalBytes + 1) - s.Refresh() + s.refreshIfNeeded() } func (s *FileToUploadStat) Add() { @@ -63,7 +84,15 @@ func (s *FileToUploadStat) Add() { defer s.mu.Unlock() s.Total++ - s.Refresh() + s.refreshIfNeeded() +} + +func (s *FileToUploadStat) AddBytes(filesize int64) { + s.mu.Lock() + defer s.mu.Unlock() + + s.TotalBytes += filesize + s.refreshIfNeeded() } func (s *FileToUploadStat) Commit(fileread int64) { @@ -71,7 +100,7 @@ func (s *FileToUploadStat) Commit(fileread int64) { defer s.mu.Unlock() s.UploadedBytes += fileread - _ = s.Progress.Set64(s.UploadedBytes) + s.refreshIfNeeded() } func (s *FileToUploadStat) Done() { @@ -79,11 +108,7 @@ func (s *FileToUploadStat) Done() { defer s.mu.Unlock() s.Uploaded++ - s.Refresh() -} - -func (s *FileToUploadStat) Close() { - _ = s.Progress.Close() + s.refreshIfNeeded() } func (s *FileToUploadStat) Fail() { @@ -91,7 +116,7 @@ func (s *FileToUploadStat) Fail() { defer s.mu.Unlock() s.Failed++ - s.Refresh() + s.refreshIfNeeded() } func (s *FileToUploadStat) Skip() { @@ -99,14 +124,23 @@ func (s *FileToUploadStat) Skip() { defer s.mu.Unlock() s.Skipped++ - s.Refresh() + s.refreshIfNeeded() } func (s *FileToUploadStat) Error(origin string, filename string, err error) { s.mu.Lock() defer s.mu.Unlock() - _ = s.Progress.Clear() - fmt.Printf("[%s] %s: %s\n", origin, filename, err) - _ = s.Progress.RenderBlank() + s.progress.Clear() + ct.Foreground(ct.Red, false) + _, _ = fmt.Fprintf(s.progress, "[%s] %s: %s\n", origin, filename, err) + _ = s.progress.Print() + ct.ResetColor() + s.progress.Reset() + + s.refreshIfNeeded() +} + +func (s *FileToUploadStat) Close() { + s.refresh() }