* Returns the cached ANSI escape sequences for a hex color. * Computes and caches on first use to avoid repeated Buffer allocations. * @param {string} hex A valid hex color string (#RGB or #RRGGBB) * @returns {{openSeq: string, closeSeq: string}}
(hex)
| 161 | * @returns {{openSeq: string, closeSeq: string}} |
| 162 | */ |
| 163 | function getHexStyle(hex) { |
| 164 | const cache = getHexStyleCache(); |
| 165 | const cached = cache.get(hex); |
| 166 | if (cached !== undefined) return cached; |
| 167 | const { 0: r, 1: g, 2: b } = hexToRgb(hex); |
| 168 | const style = { |
| 169 | __proto__: null, |
| 170 | openSeq: kEscape + rgbToAnsi24Bit(r, g, b) + kEscapeEnd, |
| 171 | closeSeq: kHexCloseSeq, |
| 172 | }; |
| 173 | if (cache.size >= kHexStyleCacheMax) |
| 174 | cache.delete(cache.keys().next().value); |
| 175 | cache.set(hex, style); |
| 176 | return style; |
| 177 | } |
| 178 | |
| 179 | function replaceCloseCode(str, closeSeq, openSeq, keepClose) { |
| 180 | const closeLen = closeSeq.length; |
no test coverage detected
searching dependent graphs…