(text: string, maxWidth: number)
| 80 | * Width-aware and grapheme-safe. |
| 81 | */ |
| 82 | export function truncateStartToWidth(text: string, maxWidth: number): string { |
| 83 | if (stringWidth(text) <= maxWidth) return text |
| 84 | if (maxWidth <= 1) return '…' |
| 85 | const segments = [...getGraphemeSegmenter().segment(text)] |
| 86 | let width = 0 |
| 87 | let startIdx = segments.length |
| 88 | for (let i = segments.length - 1; i >= 0; i--) { |
| 89 | const segWidth = stringWidth(segments[i]!.segment) |
| 90 | if (width + segWidth > maxWidth - 1) break // -1 for '…' |
| 91 | width += segWidth |
| 92 | startIdx = i |
| 93 | } |
| 94 | return ( |
| 95 | '…' + |
| 96 | segments |
| 97 | .slice(startIdx) |
| 98 | .map(s => s.segment) |
| 99 | .join('') |
| 100 | ) |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Truncates a string to fit within a maximum display width, without appending an ellipsis. |
no test coverage detected