Skip to content

Commit a418e0e

Browse files
committed
fixup! util: respect nested formats in styleText
1 parent 7bce05f commit a418e0e

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

lib/util.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const {
3636
ObjectSetPrototypeOf,
3737
ObjectValues,
3838
ReflectApply,
39+
StringPrototypeReplace,
3940
StringPrototypeToWellFormed,
4041
} = primordials;
4142

@@ -165,31 +166,32 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
165166
}
166167

167168
// Process the text to handle nested styles
168-
const processedText = text.replace(fontColorEndRegex, (match, offset) => {
169-
// Check if there's more content after this reset
170-
if (offset + match.length < text.length) {
171-
for (let i = 0; i < codes.length; i++) {
172-
const open = codes[i][0];
173-
// Check if this is a foreground color (30-37, 90-97)
174-
if ((open >= 30 && open <= 37) || (open >= 90 && open <= 97)) {
175-
return escapeStyleCode(open);
169+
const processedText = StringPrototypeReplace(
170+
StringPrototypeReplace(text, fontColorEndRegex, (match, offset) => {
171+
// Check if there's more content after this reset
172+
if (offset + match.length < text.length) {
173+
for (let i = 0; i < codes.length; i++) {
174+
const open = codes[i][0];
175+
// Check if this is a foreground color (30-37, 90-97)
176+
if ((open >= 30 && open <= 37) || (open >= 90 && open <= 97)) {
177+
return escapeStyleCode(open);
178+
}
176179
}
177180
}
178-
}
179-
return match;
180-
}).replace(bgColorEndRegex, (match, offset) => {
181-
// Check if there's more content after this reset
182-
if (offset + match.length < text.length) {
183-
for (let i = 0; i < codes.length; i++) {
184-
const open = codes[i][0];
185-
// Check if this is a background color (40-47, 100-107)
186-
if ((open >= 40 && open <= 47) || (open >= 100 && open <= 107)) {
187-
return escapeStyleCode(open);
181+
return match;
182+
}), bgColorEndRegex, (match, offset) => {
183+
// Check if there's more content after this reset
184+
if (offset + match.length < text.length) {
185+
for (let i = 0; i < codes.length; i++) {
186+
const open = codes[i][0];
187+
// Check if this is a background color (40-47, 100-107)
188+
if ((open >= 40 && open <= 47) || (open >= 100 && open <= 107)) {
189+
return escapeStyleCode(open);
190+
}
188191
}
189192
}
190-
}
191-
return match;
192-
});
193+
return match;
194+
});
193195

194196
// Build closing codes in reverse order
195197
let closeCodes = '';

0 commit comments

Comments
 (0)