Skip to content

Commit c66f604

Browse files
BenBen
authored andcommitted
fix(userMessageDisplay): paint wrapped lines with the configured bg by finalizing boxAttrs after the bg block
Reported: with custom User Message Display settings, a message long enough to wrap in the terminal had its highlight only on line 1 — line 2+ rendered against the terminal default bg. Two bugs collided to cause this: 1. Latent ordering bug: `boxAttrsObjStr` was computed right after the padding/border block, before the foreground/background block ran. Every `boxAttrs.push('backgroundColor:...')` afterwards — including the default- branch push added in 3114c5b — was silently discarded. So even with `backgroundColor: 'default'` the Box had no bg; only the inner Text's theme token was in play, and Ink's Text bg applies to the text cells but doesn't fill the padded Box width on wrapped lines. 2. For a custom bg (`rgb(r,g,b)`) the patch was only adding `.bgRgb()` to the chalk chain. That wraps the text content in ANSI bg escapes, but when Ink word-wraps the message the escape on line 1 doesn't reliably re-open on line 2, leaving wrapped lines bare. Fixes: - Move `boxAttrsObjStr` finalization to after the bg block so every push (default-theme token and custom rgb) reaches the emitted element attrs. - For custom bg, also push `backgroundColor:"rgb(r,g,b)"` onto boxAttrs and textAttrs alongside the existing chalk.bgRgb chain. Box-level bg is what actually fills the padded width across every wrapped line; Text- level bg covers the text cells where chalk's state may have been reset. Verified against CC 2.1.112 cli.js — emitted shapes: default bg: Box{paddingRight:1,backgroundColor:"userMessageBackground"} Text{color:"text",backgroundColor:"userMessageBackground"} custom bg: Box{paddingRight:1,backgroundColor:"rgb(200,80,40)"} Text{color:"text",backgroundColor:"rgb(200,80,40)"} chalk.bgRgb(200,80,40)(...) null bg: Box{paddingRight:1} Text{color:"text"} (unchanged) Ink's `colorize.js` accepts `"rgb(r,g,b)"` (no spaces) which is exactly what the `\d+` regex + join produces, even if the input has spaces. All 228 tests still pass.
1 parent 5f4c442 commit c66f604

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/patches/userMessageDisplay.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,9 @@ export const writeUserMessageDisplay = (
222222
boxAttrs.push(`alignSelf:"flex-start"`);
223223
}
224224

225-
const boxAttrsObjStr =
226-
boxAttrs.length > 0 ? `{${boxAttrs.join(',')}}` : 'null';
225+
// Note: boxAttrsObjStr is finalized further below, after the bg block. The
226+
// bg handling pushes onto boxAttrs so the Box fills wrapped lines across
227+
// the full width — finalizing here would silently drop those pushes.
227228

228229
// Build chalk chain for custom colors and styling, plus any Text-level
229230
// theme tokens for 'default' fg/bg (ink Text props apply theme colors
@@ -248,6 +249,17 @@ export const writeUserMessageDisplay = (
248249
const bgMatch = config.backgroundColor.match(/\d+/g);
249250
if (bgMatch) {
250251
chalkChain += `.bgRgb(${bgMatch.join(',')})`;
252+
// Also apply the bg at the Ink level. chalk's ANSI bg codes live inside
253+
// the text content, and when Ink word-wraps the message the escape on
254+
// line 1 doesn't reliably re-open on line 2 — so a message that wraps
255+
// renders with a highlighted first line and a bare (terminal-default)
256+
// second line. Pushing the color onto the Box (fills the padded width
257+
// across every wrapped line) and onto the inner Text (covers the text
258+
// cells themselves where chalk's state may have been reset) keeps the
259+
// highlight consistent across the whole wrapped block.
260+
const inkBg = `"rgb(${bgMatch.join(',')})"`;
261+
boxAttrs.push(`backgroundColor:${inkBg}`);
262+
textAttrs.push(`backgroundColor:${inkBg}`);
251263
}
252264
} else if (config.backgroundColor === 'default') {
253265
// Stock CC 2.1.79+ renders user messages inside a Box with
@@ -297,6 +309,12 @@ export const writeUserMessageDisplay = (
297309

298310
const chalkFormattedString = `${chalkChain}(${formattedMessage})`;
299311

312+
// Finalize the Box/Text attrs AFTER the bg block above has had a chance
313+
// to push onto them. Doing this earlier (e.g. right after the padding
314+
// block) would silently drop every bg push — which is what caused
315+
// wrapped user messages to lose their highlight on line 2+.
316+
const boxAttrsObjStr =
317+
boxAttrs.length > 0 ? `{${boxAttrs.join(',')}}` : 'null';
300318
const textAttrsObjStr =
301319
textAttrs.length > 0 ? `{${textAttrs.join(',')}}` : 'null';
302320

0 commit comments

Comments
 (0)