* 格式化代码块
(code: string, language?: string)
| 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 |