// src/colors.ts var namedColors = [ "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "brightBlack", "brightRed", "brightGreen", "brightYellow", "brightBlue", "brightMagenta", "brightCyan", "brightWhite" ]; // src/decorations.ts var decorations = { 1: "bold", 2: "dim", 3: "italic", 4: "underline", 7: "reverse", 9: "strikethrough" }; // src/parser.ts function findSequence(value, position) { const nextEscape = value.indexOf("\x1B", position); if (nextEscape !== -1) { if (value[nextEscape + 1] === "[") { const nextClose = value.indexOf("m", nextEscape); return { sequence: value.substring(nextEscape + 2, nextClose).split(";"), startPosition: nextEscape, position: nextClose + 1 }; } } return { position: value.length }; } function parseColor(sequence) { const colorMode = sequence.shift(); if (colorMode === "2") { const rgb = sequence.splice(0, 3).map((x) => Number.parseInt(x)); if (rgb.length !== 3 || rgb.some((x) => Number.isNaN(x))) return; return { type: "rgb", rgb }; } else if (colorMode === "5") { const index = sequence.shift(); if (index) { return { type: "table", index: Number(index) }; } } } function parseSequence(sequence) { const commands = []; while (sequence.length > 0) { const code = sequence.shift(); if (!code) continue; const codeInt = Number.parseInt(code); if (Number.isNaN(codeInt)) continue; if (codeInt === 0) { commands.push({ type: "resetAll" }); } else if (codeInt <= 9) { const decoration = decorations[codeInt]; if (decoration) { commands.push({ type: "setDecoration", value: decorations[codeInt] }); } } else if (codeInt <= 29) { const decoration = decorations[codeInt - 20]; if (decoration) { commands.push({ type: "resetDecoration", value: decoration }); } } else if (codeInt <= 37) { commands.push({ type: "setForegroundColor", value: { type: "named", name: namedColors[codeInt - 30] } }); } else if (codeInt === 38) { const color = parseColor(sequence); if (color) { commands.push({ type: "setForegroundColor", value: color }); } } else if (codeInt === 39) { commands.push({ type: "resetForegroundColor" }); } else if (codeInt <= 47) { commands.push({ type: "setBackgroundColor", value: { type: "named", name: namedColors[codeInt - 40] } }); } else if (codeInt === 48) { const color = parseColor(sequence); if (color) { commands.push({ type: "setBackgroundColor", value: color }); } } else if (codeInt === 49) { commands.push({ type: "resetBackgroundColor" }); } else if (codeInt >= 90 && codeInt <= 97) { commands.push({ type: "setForegroundColor", value: { type: "named", name: namedColors[codeInt - 90 + 8] } }); } else if (codeInt >= 100 && codeInt <= 107) { commands.push({ type: "setBackgroundColor", value: { type: "named", name: namedColors[codeInt - 100 + 8] } }); } } return commands; } function createAnsiSequenceParser() { let foreground = null; let background = null; let decorations2 = /* @__PURE__ */ new Set(); return { parse(value) { const tokens = []; let position = 0; do { const findResult = findSequence(value, position); const text = findResult.sequence ? value.substring(position, findResult.startPosition) : value.substring(position); if (text.length > 0) { tokens.push({ value: text, foreground, background, decorations: new Set(decorations2) }); } if (findResult.sequence) { const commands = parseSequence(findResult.sequence); for (const styleToken of commands) { if (styleToken.type === "resetAll") { foreground = null; background = null; decorations2.clear(); } else if (styleToken.type === "resetForegroundColor") { foreground = null; } else if (styleToken.type === "resetBackgroundColor") { background = null; } else if (styleToken.type === "resetDecoration") { decorations2.delete(styleToken.value); } } for (const styleToken of commands) { if (styleToken.type === "setForegroundColor") { foreground = styleToken.value; } else if (styleToken.type === "setBackgroundColor") { background = styleToken.value; } else if (styleToken.type === "setDecoration") { decorations2.add(styleToken.value); } } } position = findResult.position; } while (position < value.length); return tokens; } }; } function parseAnsiSequences(value) { return createAnsiSequenceParser().parse(value); } // src/palette.ts var defaultNamedColorsMap = { black: "#000000", red: "#bb0000", green: "#00bb00", yellow: "#bbbb00", blue: "#0000bb", magenta: "#ff00ff", cyan: "#00bbbb", white: "#eeeeee", brightBlack: "#555555", brightRed: "#ff5555", brightGreen: "#00ff00", brightYellow: "#ffff55", brightBlue: "#5555ff", brightMagenta: "#ff55ff", brightCyan: "#55ffff", brightWhite: "#ffffff" }; function createColorPalette(namedColorsMap = defaultNamedColorsMap) { function namedColor(name) { return namedColorsMap[name]; } function rgbColor(rgb) { return `#${rgb.map((x) => Math.max(0, Math.min(x, 255)).toString(16).padStart(2, "0")).join("")}`; } let colorTable; function getColorTable() { if (colorTable) { return colorTable; } colorTable = []; for (let i = 0; i < namedColors.length; i++) { colorTable.push(namedColor(namedColors[i])); } let levels = [0, 95, 135, 175, 215, 255]; for (let r = 0; r < 6; r++) { for (let g = 0; g < 6; g++) { for (let b = 0; b < 6; b++) { colorTable.push(rgbColor([levels[r], levels[g], levels[b]])); } } } let level = 8; for (let i = 0; i < 24; i++, level += 10) { colorTable.push(rgbColor([level, level, level])); } return colorTable; } function tableColor(index) { return getColorTable()[index]; } function value(color) { switch (color.type) { case "named": return namedColor(color.name); case "rgb": return rgbColor(color.rgb); case "table": return tableColor(color.index); } } return { value }; } export { createAnsiSequenceParser, createColorPalette, decorations, defaultNamedColorsMap, namedColors, parseAnsiSequences };