Skip to content

Commit 1790daa

Browse files
committed
Show a spinner until the first Inc()
1 parent 4c8c040 commit 1790daa

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/meter/meter.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import (
1010

1111
// Progress is an interface for a simple progress meter. Call
1212
// `Start()` to begin reporting. `format` should include some kind of
13-
// '%d' field, into which will be written the current count. The
14-
// format should be constructed so that the new output covers up the
15-
// old output (e.g., it should be fixed length or include some
16-
// trailing spaces). A CR character will be added automatically.
13+
// '%d' field, into which will be written the current count. A spinner
14+
// and a CR character will be added automatically.
1715
//
1816
// Call `Inc()` every time the quantity of interest increases. Call
1917
// `Stop()` to stop reporting. After an instance's `Stop()` method has
@@ -26,13 +24,16 @@ type Progress interface {
2624
Done()
2725
}
2826

27+
var Spinners = []string{"|", "(", "<", "-", "<", "(", "|", ")", ">", "-", ">", ")"}
28+
2929
// progressMeter is a `Progress` that reports the current state every
3030
// `period`.
3131
type progressMeter struct {
3232
lock sync.Mutex
3333
format string
3434
period time.Duration
3535
lastShownCount int64
36+
spinnerIndex int
3637
// When `ticker` is changed, that tells the old goroutine that
3738
// it's time to shut down.
3839
ticker *time.Ticker
@@ -50,9 +51,10 @@ func NewProgressMeter(period time.Duration) Progress {
5051
func (p *progressMeter) Start(format string) {
5152
p.lock.Lock()
5253
defer p.lock.Unlock()
53-
p.format = "\r" + format
54+
p.format = format + " %s %s"
5455
atomic.StoreInt64(&p.count, 0)
5556
p.lastShownCount = -1
57+
p.spinnerIndex = 0
5658
ticker := time.NewTicker(p.period)
5759
p.ticker = ticker
5860
go func() {
@@ -66,10 +68,14 @@ func (p *progressMeter) Start(format string) {
6668
return
6769
}
6870
c := atomic.LoadInt64(&p.count)
69-
if c != p.lastShownCount {
70-
fmt.Fprintf(os.Stderr, p.format, c)
71-
p.lastShownCount = c
71+
var s string
72+
if c == 0 {
73+
p.spinnerIndex = (p.spinnerIndex + 1) % len(Spinners)
74+
s = Spinners[p.spinnerIndex]
75+
} else {
76+
s = ""
7277
}
78+
fmt.Fprintf(os.Stderr, p.format, c, s, "\r")
7379
p.lock.Unlock()
7480
}
7581
}()
@@ -88,7 +94,7 @@ func (p *progressMeter) Done() {
8894
defer p.lock.Unlock()
8995
p.ticker = nil
9096
c := atomic.LoadInt64(&p.count)
91-
fmt.Fprintf(os.Stderr, p.format+"\n", c)
97+
fmt.Fprintf(os.Stderr, p.format, c, " ", "\n")
9298
}
9399

94100
// NoProgressMeter is a `Progress` that doesn't actually report

src/sizes/graph.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func ScanRepositoryUsingGraph(
124124
var trees, tags []ObjectHeader
125125
var commits []CommitHeader
126126

127-
progressMeter.Start("Processing blobs: %-20d")
127+
progressMeter.Start("Processing blobs: %d")
128128
for {
129129
oid, objectType, objectSize, err := iter.Next()
130130
if err != nil {
@@ -208,7 +208,7 @@ func ScanRepositoryUsingGraph(
208208
errChan <- nil
209209
}()
210210

211-
progressMeter.Start("Processing trees: %-20d")
211+
progressMeter.Start("Processing trees: %d")
212212
for _ = range trees {
213213
oid, objectType, _, data, err := objectIter.Next()
214214
if err != nil {
@@ -235,7 +235,7 @@ func ScanRepositoryUsingGraph(
235235
// Process the commits in (roughly) chronological order, to
236236
// minimize the number of commits that are pending at any one
237237
// time:
238-
progressMeter.Start("Processing commits: %-20d")
238+
progressMeter.Start("Processing commits: %d")
239239
for i := len(commits); i > 0; i-- {
240240
oid, objectType, _, data, err := objectIter.Next()
241241
if err != nil {
@@ -263,15 +263,15 @@ func ScanRepositoryUsingGraph(
263263
// Tell PathResolver about the commits in (roughly) reverse
264264
// chronological order, to favor new ones in the paths of trees:
265265
if nameStyle != NameStyleNone {
266-
progressMeter.Start("Matching commits to trees: %-20d")
266+
progressMeter.Start("Matching commits to trees: %d")
267267
for _, commit := range commits {
268268
progressMeter.Inc()
269269
graph.pathResolver.RecordCommit(commit.oid, commit.tree)
270270
}
271271
progressMeter.Done()
272272
}
273273

274-
progressMeter.Start("Processing annotated tags: %-20d")
274+
progressMeter.Start("Processing annotated tags: %d")
275275
for range tags {
276276
oid, objectType, _, data, err := objectIter.Next()
277277
if err != nil {
@@ -297,7 +297,7 @@ func ScanRepositoryUsingGraph(
297297
return HistorySize{}, err
298298
}
299299

300-
progressMeter.Start("Processing references: %-20d")
300+
progressMeter.Start("Processing references: %d")
301301
for _, ref := range refs {
302302
progressMeter.Inc()
303303
graph.RegisterReference(ref)

0 commit comments

Comments
 (0)