feat: show progress in bytes (#55)

* feat: show progress in bytes

* fix: never progress backwards
This commit is contained in:
Vilsol 2023-12-29 13:18:01 -08:00 committed by GitHub
parent 5d48312060
commit c67285a76c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 12 deletions

1
go.mod
View file

@ -15,6 +15,7 @@ require (
github.com/charmbracelet/glamour v0.6.0 github.com/charmbracelet/glamour v0.6.0
github.com/charmbracelet/lipgloss v0.9.1 github.com/charmbracelet/lipgloss v0.9.1
github.com/charmbracelet/x/exp/teatest v0.0.0-20231215171016-7ba2b450712d 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/jackc/puddle/v2 v2.2.1
github.com/jlaffaye/ftp v0.2.0 github.com/jlaffaye/ftp v0.2.0
github.com/lmittmann/tint v1.0.3 github.com/lmittmann/tint v1.0.3

2
go.sum
View file

@ -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 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= 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.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 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=

View file

@ -7,6 +7,7 @@ import (
"github.com/charmbracelet/bubbles/progress" "github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/dustin/go-humanize"
"github.com/muesli/reflow/wrap" "github.com/muesli/reflow/wrap"
"github.com/satisfactorymodding/ficsit-cli/cli" "github.com/satisfactorymodding/ficsit-cli/cli"
@ -272,6 +273,10 @@ func (m apply) View() string {
lipgloss.Left, lipgloss.Left,
m.sub.ViewAs(p.downloadProgress.Percentage()), m.sub.ViewAs(p.downloadProgress.Percentage()),
" - ", " - ",
humanize.Bytes(uint64(p.downloadProgress.Completed)),
"/",
humanize.Bytes(uint64(p.downloadProgress.Total)),
" - ",
lipgloss.NewStyle().Render(modReference+" (Downloading)"), lipgloss.NewStyle().Render(modReference+" (Downloading)"),
))) )))
} else { } else {
@ -279,6 +284,10 @@ func (m apply) View() string {
lipgloss.Left, lipgloss.Left,
m.sub.ViewAs(p.extractProgress.Percentage()), m.sub.ViewAs(p.extractProgress.Percentage()),
" - ", " - ",
humanize.Bytes(uint64(p.extractProgress.Completed)),
"/",
humanize.Bytes(uint64(p.extractProgress.Total)),
" - ",
lipgloss.NewStyle().Render(modReference+" (Extracting)"), lipgloss.NewStyle().Render(modReference+" (Extracting)"),
))) )))
} }

View file

@ -9,7 +9,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"sync/atomic"
"github.com/satisfactorymodding/ficsit-cli/cli/disk" "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) totalExtracted := int64(0)
totalExtractedPtr := &totalExtracted
channelUsers := sync.WaitGroup{}
if updates != nil {
defer func() {
channelUsers.Wait()
}()
}
for _, file := range reader.File { for _, file := range reader.File {
if !file.FileInfo().IsDir() { 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) return fmt.Errorf("failed to create mod directory: %s: %w", location, err)
} }
channelUsers := sync.WaitGroup{}
var fileUpdates chan GenericProgress var fileUpdates chan GenericProgress
if updates != nil { if updates != nil {
fileUpdates = make(chan GenericProgress) fileUpdates = make(chan GenericProgress)
channelUsers.Add(1) channelUsers.Add(1)
beforeProgress := totalExtracted
go func() { go func() {
defer channelUsers.Done() defer channelUsers.Done()
for fileUpdate := range fileUpdates { for fileUpdate := range fileUpdates {
updates <- GenericProgress{ updates <- GenericProgress{
Completed: atomic.LoadInt64(totalExtractedPtr) + fileUpdate.Completed, Completed: beforeProgress + fileUpdate.Completed,
Total: totalSize, 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 { if err := writeZipFile(outFileLocation, file, d, fileUpdates); err != nil {
channelUsers.Wait()
return err return err
} }
atomic.AddInt64(totalExtractedPtr, int64(file.UncompressedSize64)) channelUsers.Wait()
totalExtracted += int64(file.UncompressedSize64)
} }
} }