(text: string, maxWidth: number)
| 59 | } |
| 60 | |
| 61 | function truncateFragmentToWidth(text: string, maxWidth: number): { text: string; width: number } { |
| 62 | if (maxWidth <= 0 || text.length === 0) { |
| 63 | return { text: "", width: 0 }; |
| 64 | } |
| 65 | |
| 66 | if (isPrintableAscii(text)) { |
| 67 | const clipped = text.slice(0, maxWidth); |
| 68 | return { text: clipped, width: clipped.length }; |
| 69 | } |
| 70 | |
| 71 | const hasAnsi = text.includes("\x1b"); |
| 72 | const hasTabs = text.includes("\t"); |
| 73 | if (!hasAnsi && !hasTabs) { |
| 74 | let result = ""; |
| 75 | let width = 0; |
| 76 | for (const { segment } of graphemeSegmenter.segment(text)) { |
| 77 | const w = graphemeWidth(segment); |
| 78 | if (width + w > maxWidth) { |
| 79 | break; |
| 80 | } |
| 81 | result += segment; |
| 82 | width += w; |
| 83 | } |
| 84 | return { text: result, width }; |
| 85 | } |
| 86 | |
| 87 | let result = ""; |
| 88 | let width = 0; |
| 89 | let i = 0; |
| 90 | let pendingAnsi = ""; |
| 91 | |
| 92 | while (i < text.length) { |
| 93 | const ansi = extractAnsiCode(text, i); |
| 94 | if (ansi) { |
| 95 | pendingAnsi += ansi.code; |
| 96 | i += ansi.length; |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | if (text[i] === "\t") { |
| 101 | if (width + 3 > maxWidth) { |
| 102 | break; |
| 103 | } |
| 104 | if (pendingAnsi) { |
| 105 | result += pendingAnsi; |
| 106 | pendingAnsi = ""; |
| 107 | } |
| 108 | result += "\t"; |
| 109 | width += 3; |
| 110 | i++; |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | let end = i; |
| 115 | while (end < text.length && text[end] !== "\t") { |
| 116 | const nextAnsi = extractAnsiCode(text, end); |
| 117 | if (nextAnsi) { |
| 118 | break; |
no test coverage detected