mirror of
https://git.sr.ht/~phw/scotty
synced 2025-04-25 22:07:56 +02:00
Transfer command refactoring
Make transfer command processing even more modular
This commit is contained in:
parent
7c85ba05ab
commit
a9e07915ce
7 changed files with 236 additions and 179 deletions
79
internal/cli/progress.go
Normal file
79
internal/cli/progress.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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/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, "exporting")
|
||||
importBar := setupProgressBar(p, "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}),
|
||||
"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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue