MCPcopy Create free account
hub / github.com/anus-dev/ANUS / colorizeCode

Function colorizeCode

packages/cli/src/ui/utils/CodeColorizer.tsx:128–218  ·  view source on GitHub ↗
(
  code: string,
  language: string | null,
  availableHeight?: number,
  maxWidth?: number,
  theme?: Theme,
  settings?: LoadedSettings,
)

Source from the content-addressed store, hash-verified

126 * @returns A React.ReactNode containing Ink <Text> elements for the highlighted code.
127 */
128export function colorizeCode(
129 code: string,
130 language: string | null,
131 availableHeight?: number,
132 maxWidth?: number,
133 theme?: Theme,
134 settings?: LoadedSettings,
135): React.ReactNode {
136 const codeToHighlight = code.replace(/\n$/, '');
137 const activeTheme = theme || themeManager.getActiveTheme();
138 const showLineNumbers = settings?.merged.showLineNumbers ?? true;
139
140 try {
141 // Render the HAST tree using the adapted theme
142 // Apply the theme's default foreground color to the top-level Text element
143 let lines = codeToHighlight.split('\n');
144 const padWidth = String(lines.length).length; // Calculate padding width based on number of lines
145
146 let hiddenLinesCount = 0;
147
148 // Optimization to avoid highlighting lines that cannot possibly be displayed.
149 if (availableHeight !== undefined) {
150 availableHeight = Math.max(availableHeight, MINIMUM_MAX_HEIGHT);
151 if (lines.length > availableHeight) {
152 const sliceIndex = lines.length - availableHeight;
153 hiddenLinesCount = sliceIndex;
154 lines = lines.slice(sliceIndex);
155 }
156 }
157
158 return (
159 <MaxSizedBox
160 maxHeight={availableHeight}
161 maxWidth={maxWidth}
162 additionalHiddenLinesCount={hiddenLinesCount}
163 overflowDirection="top"
164 >
165 {lines.map((line, index) => {
166 const contentToRender = highlightAndRenderLine(
167 line,
168 language,
169 activeTheme,
170 );
171
172 return (
173 <Box key={index}>
174 {showLineNumbers && (
175 <Text color={activeTheme.colors.Gray}>
176 {`${String(index + 1 + hiddenLinesCount).padStart(
177 padWidth,
178 ' ',
179 )} `}
180 </Text>
181 )}
182 <Text color={activeTheme.defaultColor} wrap="wrap">
183 {contentToRender}
184 </Text>
185 </Box>

Callers 3

ThemeDialogFunction · 0.85
DiffRendererFunction · 0.85
RenderCodeBlockInternalFunction · 0.85

Calls 2

highlightAndRenderLineFunction · 0.85
getActiveThemeMethod · 0.80

Tested by

no test coverage detected