mirror of
https://github.com/celogeek/piwigo-cli.git
synced 2025-05-24 17:52:36 +02:00
change upload stats report
This commit is contained in:
parent
2df5388b4c
commit
7969ec305f
@ -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
|
||||
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()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user