* Parse an ANSI string into spans using the termio parser.
(input: string)
| 116 | * Parse an ANSI string into spans using the termio parser. |
| 117 | */ |
| 118 | function parseToSpans(input: string): Span[] { |
| 119 | const parser = new Parser(); |
| 120 | const actions = parser.feed(input); |
| 121 | const spans: Span[] = []; |
| 122 | let currentHyperlink: string | undefined; |
| 123 | for (const action of actions) { |
| 124 | if (action.type === 'link') { |
| 125 | if (action.action.type === 'start') { |
| 126 | currentHyperlink = action.action.url; |
| 127 | } else { |
| 128 | currentHyperlink = undefined; |
| 129 | } |
| 130 | continue; |
| 131 | } |
| 132 | if (action.type === 'text') { |
| 133 | const text = action.graphemes.map(g => g.value).join(''); |
| 134 | if (!text) continue; |
| 135 | const props = textStyleToSpanProps(action.style); |
| 136 | if (currentHyperlink) { |
| 137 | props.hyperlink = currentHyperlink; |
| 138 | } |
| 139 | |
| 140 | // Try to merge with previous span if props match |
| 141 | const lastSpan = spans[spans.length - 1]; |
| 142 | if (lastSpan && propsEqual(lastSpan.props, props)) { |
| 143 | lastSpan.text += text; |
| 144 | } else { |
| 145 | spans.push({ |
| 146 | text, |
| 147 | props |
| 148 | }); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return spans; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Convert termio's TextStyle to SpanProps. |
no test coverage detected