Skip to content

Commit

Permalink
text: color: fix race condition while generating escape sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Aug 23, 2018
1 parent 2da1889 commit 76d5c8b
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions text/color.go
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 76d5c8b

Please sign in to comment.