This commit is contained in:
Jesse Duffield 2021-04-03 15:56:11 +11:00
parent fafd5234bd
commit 0898a7bb57
36 changed files with 215 additions and 171 deletions

View file

@ -5,6 +5,8 @@
package gocui
import (
"sync"
"github.com/gdamore/tcell/v2"
)
@ -21,6 +23,7 @@ type oldStyle struct {
// we're using this cache to speed up rendering, because obtaining the tcell style
// from the old style is deterministic
var cellStyleCache map[oldStyle]tcell.Style = map[oldStyle]tcell.Style{}
var cacheMutex = sync.RWMutex{}
// tcellInit initializes tcell screen for use.
func tcellInit() error {
@ -43,7 +46,10 @@ func tcellSetCell(x, y int, ch rune, fg, bg Attribute, outputMode OutputMode) {
// getTcellStyle creates tcell.Style from Attributes
func getTcellStyle(input oldStyle) tcell.Style {
if cachedResult, ok := cellStyleCache[input]; ok {
cacheMutex.RLock()
cachedResult, ok := cellStyleCache[input]
cacheMutex.RUnlock()
if ok {
return cachedResult
}
@ -59,7 +65,9 @@ func getTcellStyle(input oldStyle) tcell.Style {
st = setTcellFontEffectStyle(st, input.bg)
}
cacheMutex.Lock()
cellStyleCache[input] = st
cacheMutex.Unlock()
return st
}