(input: string)
| 79 | } |
| 80 | |
| 81 | function parseAnsi(input: string): Segment[] { |
| 82 | const segments: Segment[] = []; |
| 83 | let current: AnsiStyle = {}; |
| 84 | let pos = 0; |
| 85 | let textStart = 0; |
| 86 | |
| 87 | const pushSegment = (end: number) => { |
| 88 | const text = input.slice(textStart, end); |
| 89 | if (text) { |
| 90 | segments.push({ text, style: { ...current } }); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | while (pos < input.length) { |
| 95 | const esc = input.indexOf("\x1b[", pos); |
| 96 | if (esc === -1) break; |
| 97 | |
| 98 | pushSegment(esc); |
| 99 | |
| 100 | // Find the end of the escape sequence (letter terminator) |
| 101 | let seqEnd = esc + 2; |
| 102 | while (seqEnd < input.length && !/[A-Za-z]/.test(input[seqEnd])) { |
| 103 | seqEnd++; |
| 104 | } |
| 105 | const terminator = input[seqEnd]; |
| 106 | const params = input.slice(esc + 2, seqEnd).split(";").map(Number); |
| 107 | |
| 108 | if (terminator === "m") { |
| 109 | // SGR sequence |
| 110 | let i = 0; |
| 111 | while (i < params.length) { |
| 112 | const p = params[i]; |
| 113 | if (p === 0 || isNaN(p)) { |
| 114 | current = {}; |
| 115 | } else if (p === 1) { |
| 116 | current.bold = true; |
| 117 | } else if (p === 2) { |
| 118 | current.dim = true; |
| 119 | } else if (p === 3) { |
| 120 | current.italic = true; |
| 121 | } else if (p === 4) { |
| 122 | current.underline = true; |
| 123 | } else if (p === 9) { |
| 124 | current.strikethrough = true; |
| 125 | } else if (p === 22) { |
| 126 | current.bold = false; |
| 127 | current.dim = false; |
| 128 | } else if (p === 23) { |
| 129 | current.italic = false; |
| 130 | } else if (p === 24) { |
| 131 | current.underline = false; |
| 132 | } else if (p === 29) { |
| 133 | current.strikethrough = false; |
| 134 | } else if (p >= 30 && p <= 37) { |
| 135 | current.color = FG_COLORS[p]; |
| 136 | } else if (p === 38) { |
| 137 | if (params[i + 1] === 5 && params[i + 2] !== undefined) { |
| 138 | current.color = get256Color(params[i + 2]); |
no test coverage detected