Skip to content

Commit

Permalink
feat: show progress in bytes (#55)
Browse files Browse the repository at this point in the history
* feat: show progress in bytes

* fix: never progress backwards
  • Loading branch information
Vilsol authored Dec 29, 2023
1 parent 5d48312 commit c67285a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 9 additions & 0 deletions tea/scenes/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -272,13 +273,21 @@ 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 {
strs = append(strs, lipgloss.NewStyle().MarginLeft(1).Render(lipgloss.JoinHorizontal(
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)"),
)))
}
Expand Down
20 changes: 8 additions & 12 deletions utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"sync"
"sync/atomic"

"github.com/satisfactorymodding/ficsit-cli/cli/disk"
)
Expand Down Expand Up @@ -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() {
Expand All @@ -87,26 +77,32 @@ 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,
}
}
}()
}

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)
}
}

Expand Down

0 comments on commit c67285a

Please sign in to comment.