From 2e28bb90a00ad415d453a2ec868faa78679602f0 Mon Sep 17 00:00:00 2001 From: Miguel Solorio Date: Wed, 23 Jul 2025 15:39:22 -0700 Subject: Update diff colors (#4747) Co-authored-by: Jacob Richman --- packages/cli/src/ui/utils/CodeColorizer.tsx | 45 ++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-) (limited to 'packages/cli/src/ui/utils') diff --git a/packages/cli/src/ui/utils/CodeColorizer.tsx b/packages/cli/src/ui/utils/CodeColorizer.tsx index 38dc49d4..58b32c7e 100644 --- a/packages/cli/src/ui/utils/CodeColorizer.tsx +++ b/packages/cli/src/ui/utils/CodeColorizer.tsx @@ -88,6 +88,34 @@ function renderHastNode( return null; } +function highlightAndRenderLine( + line: string, + language: string | null, + theme: Theme, +): React.ReactNode { + try { + const getHighlightedLine = () => + !language || !lowlight.registered(language) + ? lowlight.highlightAuto(line) + : lowlight.highlight(language, line); + + const renderedNode = renderHastNode(getHighlightedLine(), theme, undefined); + + return renderedNode !== null ? renderedNode : line; + } catch (_error) { + return line; + } +} + +export function colorizeLine( + line: string, + language: string | null, + theme?: Theme, +): React.ReactNode { + const activeTheme = theme || themeManager.getActiveTheme(); + return highlightAndRenderLine(line, language, activeTheme); +} + /** * Renders syntax-highlighted code for Ink applications using a selected theme. * @@ -123,11 +151,6 @@ export function colorizeCode( } } - const getHighlightedLines = (line: string) => - !language || !lowlight.registered(language) - ? lowlight.highlightAuto(line) - : lowlight.highlight(language, line); - return ( {lines.map((line, index) => { - const renderedNode = renderHastNode( - getHighlightedLines(line), + const contentToRender = highlightAndRenderLine( + line, + language, activeTheme, - undefined, ); - const contentToRender = renderedNode !== null ? renderedNode : line; return ( - {`${String(index + 1 + hiddenLinesCount).padStart(padWidth, ' ')} `} + {`${String(index + 1 + hiddenLinesCount).padStart( + padWidth, + ' ', + )} `} {contentToRender} -- cgit v1.2.3