Skip to content

Commit

Permalink
minor fixes to progress and text (#63)
Browse files Browse the repository at this point in the history
* text: color: fix race condition while generating escape sequences
* progress: Tracker.ETA() should return remaining time (not total)
  • Loading branch information
jedib0t authored Aug 23, 2018
1 parent 5dd3188 commit a516594
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion progress/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (t *Tracker) ETA() time.Duration {
}

timeTaken := time.Since(t.timeStart)
eta := time.Duration((int64(timeTaken) / int64(percDone)) * 100)
eta := time.Duration((int64(timeTaken) / int64(percDone)) * int64(100-percDone))
return eta
}

Expand Down
4 changes: 2 additions & 2 deletions progress/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ func TestTracker_ETA(t *testing.T) {

tracker.timeStart = time.Now()
time.Sleep(time.Millisecond * 100)
tracker.value = 10
tracker.value = 50
eta := tracker.ETA()
assert.NotEqual(t, time.Duration(0), eta)
assert.True(t, eta > time.Second)
assert.True(t, eta < time.Second)
}

func TestTracker_Increment(t *testing.T) {
Expand Down
15 changes: 5 additions & 10 deletions text/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ type Colors []Color

var (
// colorsSeqMap caches the escape sequence for a set of colors
colorsSeqMap = make(map[string]string)

// colorsSeqMapMutex should be used to lock writes to the map
colorsSeqMapMutex = sync.Mutex{}
colorsSeqMap = sync.Map{}
)

// GetEscapeSeq returns the ANSI escape sequence for the colors set.
Expand All @@ -129,18 +126,16 @@ func (c Colors) GetEscapeSeq() string {
return ""
}
colorsKey := fmt.Sprintf("%#v", c)
escapeSeq := colorsSeqMap[colorsKey]
if escapeSeq == "" {
escapeSeq, ok := colorsSeqMap.Load(colorsKey)
if !ok || escapeSeq == "" {
colorNums := make([]string, len(c))
for idx, c := range c {
colorNums[idx] = strconv.Itoa(int(c))
}
escapeSeq = EscapeStart + strings.Join(colorNums, ";") + EscapeStop
colorsSeqMapMutex.Lock()
colorsSeqMap[colorsKey] = escapeSeq
colorsSeqMapMutex.Unlock()
colorsSeqMap.Store(colorsKey, escapeSeq)
}
return escapeSeq
return escapeSeq.(string)
}

// Sprint colorizes and prints the given string(s).
Expand Down

0 comments on commit a516594

Please sign in to comment.