| 1 |
package twwidth |
| 2 |
|
| 3 |
import "github.com/olekukonko/tablewriter/pkg/twcache" |
| 4 |
|
| 5 |
// widthCache stores memoized results of Width calculations to improve performance. |
| 6 |
var widthCache *twcache.LRU[cacheKey, int] |
| 7 |
|
| 8 |
type cacheKey struct { |
| 9 |
eastAsian bool |
| 10 |
str string |
| 11 |
} |
| 12 |
|
| 13 |
// SetCacheCapacity changes the cache size dynamically |
| 14 |
// If capacity <= 0, disables caching entirely |
| 15 |
func SetCacheCapacity(capacity int) { |
| 16 |
mu.Lock() |
| 17 |
defer mu.Unlock() |
| 18 |
|
| 19 |
if capacity <= 0 { |
| 20 |
widthCache = nil // nil = fully disabled |
| 21 |
return |
| 22 |
} |
| 23 |
|
| 24 |
newCache := twcache.NewLRU[cacheKey, int](capacity) |
| 25 |
widthCache = newCache |
| 26 |
} |