UNPKG

mlld

Version:

mlld: llm scripting language

84 lines (82 loc) 3.32 kB
import { __name, __publicField } from './chunk-NJQT543K.mjs'; // core/utils/DirectiveTraceFormatter.ts var _DirectiveTraceFormatter = class _DirectiveTraceFormatter { constructor() { __publicField(this, "LINE_WIDTH", 80); // ANSI color codes __publicField(this, "colors", { dim: "\x1B[90m", directive: "\x1B[36m", variable: "\x1B[33m", file: "\x1B[90m", blue: "\x1B[34m", red: "\x1B[31m", reset: "\x1B[0m" }); } /** * Format a directive trace for display */ format(trace, useColors = true, errorMessage) { if (trace.length === 0) return ""; const c = useColors ? this.colors : { dim: "", directive: "", variable: "", file: "", blue: "", red: "", reset: "" }; const lines = []; const headerText = " mlld error "; const headerPadding = Math.floor((this.LINE_WIDTH - headerText.length) / 2); const leftPad = "\u2501".repeat(headerPadding); const rightPad = "\u2501".repeat(this.LINE_WIDTH - headerPadding - headerText.length); lines.push(c.dim + leftPad + c.reset + c.blue + headerText + c.reset + c.dim + rightPad + c.reset); if (errorMessage && !trace.some((t) => t.failed)) { lines.push(""); lines.push(c.red + "\u2718" + c.reset + " " + c.red + "Error: " + c.reset + errorMessage); lines.push(""); } trace.forEach((entry, i) => { const indent = " ".repeat(entry.depth); const prefix = i === 0 ? "" : "\u2514\u2500\u2500 "; const directive = c.directive + entry.directive + c.reset; const variable = entry.varName ? " " + c.variable + entry.varName + c.reset : ""; const location = c.file + entry.location + c.reset; const leftPart = indent + prefix + directive + variable + " "; const rightPart = " " + location; const leftLength = this.stripAnsi(leftPart).length; const rightLength = this.stripAnsi(rightPart).length; const dotsNeeded = this.LINE_WIDTH - leftLength - rightLength; const dots = c.dim + ".".repeat(Math.max(dotsNeeded, 3)) + c.reset; lines.push(leftPart + dots + rightPart); if (entry.failed && entry.errorMessage) { const errorIndent = entry.depth === 0 ? "" : " ".repeat(entry.depth); const errorPrefix = "\u2514\u2500\u2500 " + c.red + "\u2718" + c.reset + " "; const match = entry.errorMessage.match(/^(.+?\.mld failed to parse at line \d+): (.+)$/); if (match) { const [_, fileInfo, errorDetails] = match; lines.push(errorIndent + errorPrefix + c.red + fileInfo + ":" + c.reset); lines.push(errorIndent + " " + c.reset + errorDetails); } else { lines.push(errorIndent + errorPrefix + c.red + entry.errorMessage + c.reset); } } }); lines.push(c.dim + "\u2501".repeat(this.LINE_WIDTH) + c.reset); return lines.join("\n"); } /** * Strip ANSI escape codes from a string */ stripAnsi(str) { return str.replace(/\x1b\[[0-9;]*m/g, ""); } }; __name(_DirectiveTraceFormatter, "DirectiveTraceFormatter"); var DirectiveTraceFormatter = _DirectiveTraceFormatter; export { DirectiveTraceFormatter }; //# sourceMappingURL=DirectiveTraceFormatter-RJ3SLLGB.mjs.map //# sourceMappingURL=DirectiveTraceFormatter-RJ3SLLGB.mjs.map