summaryrefslogtreecommitdiff
path: root/lex-fetch/progress/progress.go
blob: 430d36eacf36de267f1eaa0154063369a2b06fa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package progress

import (
	"fmt"
	"io"
	"sync"
)

type Reporter struct {
	Total  int
	cur    int
	mu     sync.Mutex
	writer io.Writer
}

func NewReporter(writer io.Writer) Reporter {
	return Reporter{writer: writer}
}

func (r *Reporter) ReportProgress(num int) {
	r.mu.Lock()
	if num > r.cur {
		fmt.Fprintf(r.writer, "%d/%d\n", num, r.Total)
		r.cur = num
	}
	r.mu.Unlock()
}