mlld
Version:
mlld: llm scripting language
1 lines • 8.28 kB
Source Map (JSON)
{"version":3,"sources":["../core/utils/DirectiveTraceFormatter.ts"],"names":["DirectiveTraceFormatter","LINE_WIDTH","colors","dim","directive","variable","file","blue","red","reset","format","trace","useColors","errorMessage","length","c","lines","headerText","headerPadding","Math","floor","leftPad","repeat","rightPad","push","some","t","failed","forEach","entry","i","indent","depth","prefix","varName","location","leftPart","rightPart","leftLength","stripAnsi","rightLength","dotsNeeded","dots","max","errorIndent","errorPrefix","match","_","fileInfo","errorDetails","join","str","replace"],"mappings":";;;AAKO,IAAMA,wBAAAA,GAAN,MAAMA,wBAAAA,CAAAA;AAAN,EAAA,WAAA,GAAA;AACYC,IAAa,aAAA,CAAA,IAAA,EAAA,YAAA,EAAA,EAAA,CAAA;AAGbC;AAAS,IAAA,aAAA,CAAA,IAAA,EAAA,QAAA,EAAA;MACxBC,GAAK,EAAA,UAAA;MACLC,SAAW,EAAA,UAAA;MACXC,QAAU,EAAA,UAAA;MACVC,IAAM,EAAA,UAAA;MACNC,IAAM,EAAA,UAAA;MACNC,GAAK,EAAA,UAAA;MACLC,KAAO,EAAA;AACT,KAAA,CAAA;;;;;EAKAC,MAAOC,CAAAA,KAAAA,EAAyBC,SAAY,GAAA,IAAA,EAAMC,YAA+B,EAAA;AAC/E,IAAIF,IAAAA,KAAAA,CAAMG,MAAW,KAAA,CAAA,EAAU,OAAA,EAAA;AAE/B,IAAMC,MAAAA,CAAAA,GAAIH,SAAY,GAAA,IAAA,CAAKV,MAAS,GAAA;MAClCC,GAAK,EAAA,EAAA;MAAIC,SAAW,EAAA,EAAA;MAAIC,QAAU,EAAA,EAAA;MAAIC,IAAM,EAAA,EAAA;MAAIC,IAAM,EAAA,EAAA;MAAIC,GAAK,EAAA,EAAA;MAAIC,KAAO,EAAA;AAC5E,KAAA;AAEA,IAAA,MAAMO,QAAkB,EAAA;AAGxB,IAAA,MAAMC,UAAa,GAAA,cAAA;AACnB,IAAA,MAAMC,gBAAg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ace } from '@core/types/trace';\n\n/**\n * Formats directive traces for display in error messages\n */\nexport class DirectiveTraceFormatter {\n private readonly LINE_WIDTH = 80;\n \n // ANSI color codes\n private readonly colors = {\n dim: '\\x1b[90m', // gray for dots/lines\n directive: '\\x1b[36m', // cyan for @directives \n variable: '\\x1b[33m', // yellow for variable names\n file: '\\x1b[90m', // gray for files\n blue: '\\x1b[34m', // blue for header\n red: '\\x1b[31m', // red for errors\n reset: '\\x1b[0m'\n };\n\n /**\n * Format a directive trace for display\n */\n format(trace: DirectiveTrace[], useColors = true, errorMessage?: string): string {\n if (trace.length === 0) return '';\n \n const c = useColors ? this.colors : {\n dim: '', directive: '', variable: '', file: '', blue: '', red: '', reset: ''\n };\n \n const lines: string[] = [];\n \n // Header with centered \"mlld error\" text\n const headerText = ' mlld error ';\n const headerPadding = Math.floor((this.LINE_WIDTH - headerText.length) / 2);\n const leftPad = '━'.repeat(headerPadding);\n const rightPad = '━'.repeat(this.LINE_WIDTH - headerPadding - headerText.length);\n \n lines.push(c.dim + leftPad + c.reset + c.blue + headerText + c.reset + c.dim + rightPad + c.reset);\n \n // If we have an error message and no failed entries, show it at the top\n if (errorMessage && !trace.some(t => t.failed)) {\n lines.push('');\n lines.push(c.red + '✘' + c.reset + ' ' + c.red + 'Error: ' + c.reset + errorMessage);\n lines.push('');\n }\n \n // Format each entry\n trace.forEach((entry, i) => {\n const indent = ' '.repeat(entry.depth);\n const prefix = i === 0 ? '' : '└── ';\n \n // Build colored parts\n const directive = c.directive + entry.directive + c.reset;\n const variable = entry.varName ? \n ' ' + c.variable + entry.varName + c.reset : '';\n const location = c.file + entry.location + c.reset;\n \n // Calculate dots needed\n const leftPart = indent + prefix + directive + variable + ' ';\n const rightPart = ' ' + location;\n \n // Strip ANSI codes for length calculation\n const leftLength = this.stripAnsi(leftPart).length;\n const rightLength = this.stripAnsi(rightPart).length;\n \n const dotsNeeded = this.LINE_WIDTH - leftLength - rightLength;\n const dots = c.dim + '.'.repeat(Math.max(dotsNeeded, 3)) + c.reset;\n \n lines.push(leftPart + dots + rightPart);\n \n // If this entry failed, show the error\n if (entry.failed && entry.errorMessage) {\n const errorIndent = entry.depth === 0 ? '' : ' '.repeat(entry.depth);\n const errorPrefix = '└── ' + c.red + '✘' + c.reset + ' ';\n \n // Split the error message to show file info and error details separately\n const match = entry.errorMessage.match(/^(.+?\\.mld failed to parse at line \\d+): (.+)$/);\n if (match) {\n const [_, fileInfo, errorDetails] = match;\n lines.push(errorIndent + errorPrefix + c.red + fileInfo + ':' + c.reset);\n lines.push(errorIndent + ' ' + c.reset + errorDetails);\n } else {\n // Fallback for other error formats\n lines.push(errorIndent + errorPrefix + c.red + entry.errorMessage + c.reset);\n }\n }\n });\n \n // Footer\n lines.push(c.dim + '━'.repeat(this.LINE_WIDTH) + c.reset);\n \n return lines.join('\\n');\n }\n \n /**\n * Strip ANSI escape codes from a string\n */\n private stripAnsi(str: string): string {\n return str.replace(/\\x1b\\[[0-9;]*m/g, '');\n }\n}"]}