/* Copyright © 2023 Philipp Wolfer This file is part of Scotty. Scotty is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Scotty is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scotty. If not, see . */ package cli import ( "sync" "time" "github.com/fatih/color" "github.com/vbauerster/mpb/v8" "github.com/vbauerster/mpb/v8/decor" "go.uploadedlobster.com/scotty/internal/i18n" "go.uploadedlobster.com/scotty/internal/models" ) func progressBar(wg *sync.WaitGroup, exportProgress chan models.Progress, importProgress chan models.Progress) *mpb.Progress { p := mpb.New( mpb.WithWaitGroup(wg), mpb.WithOutput(color.Output), // mpb.WithWidth(64), mpb.WithAutoRefresh(), ) exportBar := setupProgressBar(p, i18n.Tr("exporting")) importBar := setupProgressBar(p, i18n.Tr("importing")) go updateProgressBar(exportBar, wg, exportProgress) go updateProgressBar(importBar, wg, importProgress) return p } func setupProgressBar(p *mpb.Progress, name string) *mpb.Bar { green := color.New(color.FgGreen).SprintFunc() return p.New(0, mpb.BarStyle(), mpb.PrependDecorators( decor.Name(" "), decor.OnComplete( decor.Spinner(nil, decor.WC{W: 2, C: decor.DidentRight}), green("✓ "), ), decor.Name(name, decor.WCSyncWidthR), ), mpb.AppendDecorators( decor.OnComplete( decor.EwmaETA(decor.ET_STYLE_GO, 0, decor.WC{C: decor.DSyncWidth}), i18n.Tr("done"), ), // decor.OnComplete(decor.Percentage(decor.WC{W: 5, C: decor.DSyncWidthR}), "done"), decor.Name(" "), ), ) } func updateProgressBar(bar *mpb.Bar, wg *sync.WaitGroup, progressChan chan models.Progress) { wg.Add(1) defer wg.Done() lastIterTime := time.Now() for progress := range progressChan { oldIterTime := lastIterTime lastIterTime = time.Now() bar.EwmaSetCurrent(progress.Elapsed, lastIterTime.Sub(oldIterTime)) bar.SetTotal(progress.Total, progress.Completed) } }