mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-05 04:58:33 +02:00
More granular progress report for JSPF and scrobblerlog
This commit is contained in:
parent
83eac8c801
commit
c7af90b585
4 changed files with 55 additions and 19 deletions
|
@ -22,6 +22,7 @@ THE SOFTWARE.
|
|||
package models
|
||||
|
||||
import (
|
||||
"iter"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -244,3 +245,38 @@ func (p *Progress) Complete() {
|
|||
func (p *Progress) Abort() {
|
||||
p.Aborted = true
|
||||
}
|
||||
|
||||
func IterExportProgress[T any](
|
||||
items []T, t *TransferProgress, c chan TransferProgress,
|
||||
) iter.Seq2[int, T] {
|
||||
return iterProgress(items, t, t.Export, c, true)
|
||||
}
|
||||
|
||||
func IterImportProgress[T any](
|
||||
items []T, t *TransferProgress, c chan TransferProgress,
|
||||
) iter.Seq2[int, T] {
|
||||
return iterProgress(items, t, t.Import, c, false)
|
||||
}
|
||||
|
||||
func iterProgress[T any](
|
||||
items []T, t *TransferProgress,
|
||||
p *Progress, c chan TransferProgress,
|
||||
autocomplete bool,
|
||||
) iter.Seq2[int, T] {
|
||||
// Report progress in 1% steps
|
||||
steps := len(items) / 100
|
||||
return func(yield func(int, T) bool) {
|
||||
for i, item := range items {
|
||||
yield(i, item)
|
||||
p.Elapsed++
|
||||
if i%steps == 0 {
|
||||
c <- *t
|
||||
}
|
||||
}
|
||||
|
||||
if autocomplete {
|
||||
p.Complete()
|
||||
c <- *t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue