( str: string, maxWidth: number, singleLine: boolean = false, )
| 132 | * @returns The truncated string with ellipsis if needed |
| 133 | */ |
| 134 | export function truncate( |
| 135 | str: string, |
| 136 | maxWidth: number, |
| 137 | singleLine: boolean = false, |
| 138 | ): string { |
| 139 | let result = str |
| 140 | |
| 141 | // If singleLine is true, truncate at first newline |
| 142 | if (singleLine) { |
| 143 | const firstNewline = str.indexOf('\n') |
| 144 | if (firstNewline !== -1) { |
| 145 | result = str.substring(0, firstNewline) |
| 146 | // Ensure total width including ellipsis doesn't exceed maxWidth |
| 147 | if (stringWidth(result) + 1 > maxWidth) { |
| 148 | return truncateToWidth(result, maxWidth) |
| 149 | } |
| 150 | return `${result}…` |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (stringWidth(result) <= maxWidth) { |
| 155 | return result |
| 156 | } |
| 157 | return truncateToWidth(result, maxWidth) |
| 158 | } |
| 159 | |
| 160 | export function wrapText(text: string, width: number): string[] { |
| 161 | const lines: string[] = [] |
no test coverage detected