From c67285a76cbabe9354b919f2cc423341539632ab Mon Sep 17 00:00:00 2001 From: Vilsol Date: Fri, 29 Dec 2023 13:18:01 -0800 Subject: [PATCH] feat: show progress in bytes (#55) * feat: show progress in bytes * fix: never progress backwards --- go.mod | 1 + go.sum | 2 ++ tea/scenes/apply.go | 9 +++++++++ utils/io.go | 20 ++++++++------------ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index da52b14..c6d2409 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ require ( github.com/charmbracelet/glamour v0.6.0 github.com/charmbracelet/lipgloss v0.9.1 github.com/charmbracelet/x/exp/teatest v0.0.0-20231215171016-7ba2b450712d + github.com/dustin/go-humanize v1.0.1 github.com/jackc/puddle/v2 v2.2.1 github.com/jlaffaye/ftp v0.2.0 github.com/lmittmann/tint v1.0.3 diff --git a/go.sum b/go.sum index d3fa484..22f1f92 100644 --- a/go.sum +++ b/go.sum @@ -78,6 +78,8 @@ github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55k github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0= github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= diff --git a/tea/scenes/apply.go b/tea/scenes/apply.go index 00eb50b..e26dd8d 100644 --- a/tea/scenes/apply.go +++ b/tea/scenes/apply.go @@ -7,6 +7,7 @@ import ( "github.com/charmbracelet/bubbles/progress" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/dustin/go-humanize" "github.com/muesli/reflow/wrap" "github.com/satisfactorymodding/ficsit-cli/cli" @@ -272,6 +273,10 @@ func (m apply) View() string { lipgloss.Left, m.sub.ViewAs(p.downloadProgress.Percentage()), " - ", + humanize.Bytes(uint64(p.downloadProgress.Completed)), + "/", + humanize.Bytes(uint64(p.downloadProgress.Total)), + " - ", lipgloss.NewStyle().Render(modReference+" (Downloading)"), ))) } else { @@ -279,6 +284,10 @@ func (m apply) View() string { lipgloss.Left, m.sub.ViewAs(p.extractProgress.Percentage()), " - ", + humanize.Bytes(uint64(p.extractProgress.Completed)), + "/", + humanize.Bytes(uint64(p.extractProgress.Total)), + " - ", lipgloss.NewStyle().Render(modReference+" (Extracting)"), ))) } diff --git a/utils/io.go b/utils/io.go index 31a142f..bda2467 100644 --- a/utils/io.go +++ b/utils/io.go @@ -9,7 +9,6 @@ import ( "os" "path/filepath" "sync" - "sync/atomic" "github.com/satisfactorymodding/ficsit-cli/cli/disk" ) @@ -69,15 +68,6 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates } totalExtracted := int64(0) - totalExtractedPtr := &totalExtracted - - channelUsers := sync.WaitGroup{} - - if updates != nil { - defer func() { - channelUsers.Wait() - }() - } for _, file := range reader.File { if !file.FileInfo().IsDir() { @@ -87,15 +77,18 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates return fmt.Errorf("failed to create mod directory: %s: %w", location, err) } + channelUsers := sync.WaitGroup{} + var fileUpdates chan GenericProgress if updates != nil { fileUpdates = make(chan GenericProgress) channelUsers.Add(1) + beforeProgress := totalExtracted go func() { defer channelUsers.Done() for fileUpdate := range fileUpdates { updates <- GenericProgress{ - Completed: atomic.LoadInt64(totalExtractedPtr) + fileUpdate.Completed, + Completed: beforeProgress + fileUpdate.Completed, Total: totalSize, } } @@ -103,10 +96,13 @@ func ExtractMod(f io.ReaderAt, size int64, location string, hash string, updates } if err := writeZipFile(outFileLocation, file, d, fileUpdates); err != nil { + channelUsers.Wait() return err } - atomic.AddInt64(totalExtractedPtr, int64(file.UncompressedSize64)) + channelUsers.Wait() + + totalExtracted += int64(file.UncompressedSize64) } }