Skip to content

Commit 5a92830

Browse files
fix: ANSI parser now correctly renders green colors in SVG
The parser split ANSI codes like into individual elements but compared each element against the full string , which never matched. No ANSI colors were ever rendered - all text was white. Now uses index-based parsing: detects → reads next two elements (, ) to extract the 256-color number. Changes visible in demo SVG: - , , , , , , → green - Unselected items, prompts → light green - Selected items, headings → bold white - Commands → white with green prefix
1 parent c4d8633 commit 5a92830

2 files changed

Lines changed: 43 additions & 37 deletions

File tree

public/demo-terminal.svg

Lines changed: 28 additions & 28 deletions
Loading

scripts/generate-demo-svg.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
("", " \x1b[38;5;33m?\x1b[0m \x1b[1mRun this install plan now?\x1b[0m \x1b[38;5;47m(Y/n)\x1b[0m"),
5656
]
5757

58+
COLOR_MAP = {
59+
47: '#4ade80',
60+
33: '#4ade80',
61+
245: '#86efac',
62+
240: '#ffffff',
63+
}
64+
5865
def ansi_to_html(s):
5966
import re
6067
parts = re.split(r'(\x1b\[[0-9;]*m)', s)
@@ -64,19 +71,18 @@ def ansi_to_html(s):
6471
for part in parts:
6572
if part.startswith('\x1b['):
6673
codes = part[2:-1].split(';')
67-
for c in codes:
74+
i = 0
75+
while i < len(codes):
76+
c = codes[i]
6877
if c == '0':
6978
fg = None; bold = False
7079
elif c == '1':
7180
bold = True
72-
elif c == '38;5;47':
73-
fg = '#4ade80'
74-
elif c == '38;5;33':
75-
fg = '#ffffff'
76-
elif c == '38;5;245':
77-
fg = '#86efac'
78-
elif c == '38;5;240':
79-
fg = '#ffffff'
81+
elif c == '38' and i + 2 < len(codes) and codes[i+1] == '5':
82+
color_num = int(codes[i+2])
83+
fg = COLOR_MAP.get(color_num)
84+
i += 2
85+
i += 1
8086
else:
8187
style = []
8288
if fg: style.append(f'fill:{fg}')

0 commit comments

Comments
 (0)