MCPcopy Create free account
hub / github.com/echoVic/blade-code / codeBlock

Method codeBlock

src/ui/utils/formatter.ts:190–215  ·  view source on GitHub ↗

* 格式化代码块

(code: string, language?: string)

Source from the content-addressed store, hash-verified

188 * 格式化代码块
189 */
190 public static codeBlock(code: string, language?: string): string {
191 const lines = code.split('\n');
192 const maxLineNumber = lines.length;
193 const lineNumberWidth = maxLineNumber.toString().length;
194
195 const formattedLines = lines.map((line, index) => {
196 const lineNumber = (index + 1).toString().padStart(lineNumberWidth);
197 const numberedLine = UIStyles.status.muted(`${lineNumber} │ `) + line;
198 return numberedLine;
199 });
200
201 const header = language ? UIStyles.status.info(`[${language}]`) : '';
202
203 return [
204 header,
205 UIStyles.border.line(
206 Math.max(60, Math.max(...lines.map(l => l.length)) + lineNumberWidth + 3)
207 ),
208 ...formattedLines,
209 UIStyles.border.line(
210 Math.max(60, Math.max(...lines.map(l => l.length)) + lineNumberWidth + 3)
211 ),
212 ]
213 .filter(Boolean)
214 .join('\n');
215 }
216
217 /**
218 * 格式化JSON

Callers

nothing calls this directly

Calls 3

mutedMethod · 0.80
infoMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected