(text: string, maxWidth: number)
| 61 | * Appends '…' when truncation occurs. |
| 62 | */ |
| 63 | export function truncateToWidth(text: string, maxWidth: number): string { |
| 64 | if (stringWidth(text) <= maxWidth) return text |
| 65 | if (maxWidth <= 1) return '…' |
| 66 | let width = 0 |
| 67 | let result = '' |
| 68 | for (const { segment } of getGraphemeSegmenter().segment(text)) { |
| 69 | const segWidth = stringWidth(segment) |
| 70 | if (width + segWidth > maxWidth - 1) break |
| 71 | result += segment |
| 72 | width += segWidth |
| 73 | } |
| 74 | return result + '…' |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Truncates from the start of a string, keeping the tail end. |
no test coverage detected