diff --git a/cli/installations.go b/cli/installations.go index 8abdece..43f54ae 100644 --- a/cli/installations.go +++ b/cli/installations.go @@ -7,6 +7,7 @@ import ( "path/filepath" "regexp" "strings" + "sync" "github.com/satisfactorymodding/ficsit-cli/utils" @@ -341,43 +342,64 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e } } + downloading := true completed := 0 + + var genericUpdates chan utils.GenericUpdate + if updates != nil { + var wg sync.WaitGroup + wg.Add(1) + defer wg.Wait() + + genericUpdates = make(chan utils.GenericUpdate) + defer close(genericUpdates) + + go func() { + defer wg.Done() + + update := InstallUpdate{ + OverallProgress: float64(completed) / float64(len(lockfile)), + DownloadProgress: 0, + ExtractProgress: 0, + } + + select { + case updates <- update: + default: + } + + for up := range genericUpdates { + if downloading { + update.DownloadProgress = up.Progress + } else { + update.DownloadProgress = 1 + update.ExtractProgress = up.Progress + } + + if up.ModReference != nil { + update.ModName = *up.ModReference + } + + update.OverallProgress = float64(completed) / float64(len(lockfile)) + + select { + case updates <- update: + default: + } + } + }() + } + for modReference, version := range lockfile { // Only install if a link is provided, otherwise assume mod is already installed if version.Link != "" { - downloading := true + downloading = true - var genericUpdates chan utils.GenericUpdate - if updates != nil { - genericUpdates = make(chan utils.GenericUpdate) - - go func() { - update := InstallUpdate{ - ModName: modReference, - OverallProgress: float64(completed) / float64(len(lockfile)), - DownloadProgress: 0, - ExtractProgress: 0, - } - - select { - case updates <- update: - default: - } - - for up := range genericUpdates { - if downloading { - update.DownloadProgress = up.Progress - } else { - update.DownloadProgress = 1 - update.ExtractProgress = up.Progress - } - - select { - case updates <- update: - default: - } - } - }() + if genericUpdates != nil { + select { + case genericUpdates <- utils.GenericUpdate{ModReference: &modReference}: + default: + } } log.Info().Str("mod_reference", modReference).Str("version", version.Version).Str("link", version.Link).Msg("downloading mod") @@ -392,10 +414,6 @@ func (i *Installation) Install(ctx *GlobalContext, updates chan InstallUpdate) e if err := utils.ExtractMod(reader, size, filepath.Join(modsDirectory, modReference), version.Hash, genericUpdates); err != nil { return errors.Wrap(err, "could not extract "+modReference) } - - if genericUpdates != nil { - close(genericUpdates) - } } completed++ diff --git a/tea/scenes/apply.go b/tea/scenes/apply.go index 04ac16a..dc6f8d1 100644 --- a/tea/scenes/apply.go +++ b/tea/scenes/apply.go @@ -4,6 +4,7 @@ import ( "github.com/charmbracelet/bubbles/progress" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/satisfactorymodding/ficsit-cli/cli" "github.com/satisfactorymodding/ficsit-cli/tea/components" "github.com/satisfactorymodding/ficsit-cli/tea/utils" @@ -194,7 +195,7 @@ func (m apply) View() string { } if m.status.done { - strs = append(strs, utils.LabelStyle.Padding(0).Margin(1).Render("Done! Press Enter to return")) + strs = append(strs, utils.LabelStyle.Copy().Padding(0).Margin(1).Render("Done! Press Enter to return")) } result := lipgloss.NewStyle().MarginLeft(1).Render(lipgloss.JoinVertical(lipgloss.Left, strs...)) diff --git a/utils/io.go b/utils/io.go index c768159..6272416 100644 --- a/utils/io.go +++ b/utils/io.go @@ -42,7 +42,8 @@ func (pt *Progresser) Read(p []byte) (int, error) { } type GenericUpdate struct { - Progress float64 + Progress float64 + ModReference *string } func DownloadOrCache(cacheKey string, hash string, url string, updates chan GenericUpdate) (r io.ReaderAt, size int64, err error) {