UNPKG

@visulima/colorize

Version:

Terminal and Console string styling done right.

159 lines (151 loc) 6.07 kB
'use strict'; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } }); const colorize_server = require('./packem_shared/colorize.server-0LnwnixR.cjs'); const convertHexToRgb = require('./packem_shared/convertHexToRgb-DDy3Eccg.cjs'); var __defProp$3 = Object.defineProperty; var __name$3 = (target, value) => __defProp$3(target, "name", { value, configurable: true }); const makeColorizeTemplate = /* @__PURE__ */ __name$3((template) => ( // eslint-disable-next-line @typescript-eslint/no-explicit-any (firstString, ...arguments_) => { if (!Array.isArray(firstString) || !Array.isArray(firstString.raw)) { throw new TypeError("A tagged template literal must be provided"); } const parts = [firstString.raw[0]]; for (let index = 1; index < firstString.raw.length; index++) { parts.push(String(arguments_[index - 1]).replaceAll(/[{}\\]/g, "\\$&"), String(firstString.raw[index])); } return template(parts.join("")); } ), "makeColorizeTemplate"); var __defProp$2 = Object.defineProperty; var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true }); const ESCAPES = /* @__PURE__ */ new Map([ ["\\", "\\"], ["0", "\0"], ["a", "\x07"], ["b", "\b"], ["e", "\x1B"], ["f", "\f"], ["n", "\n"], ["r", "\r"], ["t", " "], ["v", "\v"] ]); const unescape = /* @__PURE__ */ __name$2((c) => { const u = c.startsWith("u"); const bracket = c[1] === "{"; if (u && !bracket && c.length === 5 || c.startsWith("x") && c.length === 3) { return String.fromCodePoint(Number.parseInt(c.slice(1), 16)); } if (u && bracket) { return String.fromCodePoint(Number.parseInt(c.slice(2, -1), 16)); } return ESCAPES.get(c) ?? c; }, "unescape"); var __defProp$1 = Object.defineProperty; var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true }); const TEMPLATE_REGEX = ( // eslint-disable-next-line security/detect-unsafe-regex,regexp/no-lazy-ends,regexp/no-dupe-disjunctions /\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.)|\{(~)?(#?[\w:]+(?:\([^)]*\))?(?:\.#?[\w:]+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n))|(\})|((?:.|[\r\n\f])+?)/gi ); const STYLE_REGEX = /(?:^|\.)(?:(\w+)(?:\(([^)]*)\))?|#(?=[:a-f\d]{2,})([a-f\d]{6})?(?::([a-f\d]{6}))?)/gi; const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/; const ESCAPE_REGEX = /\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.)|([^\\])/gi; const parseArguments = /* @__PURE__ */ __name$1((name, value) => { const results = []; const chunks = value.trim().split(/\s*,\s*/); let matches; for (const chunk of chunks) { const number = Number(chunk); if (!Number.isNaN(number)) { results.push(number); } else if (matches = STRING_REGEX.exec(chunk)) { results.push(matches[2].replace(ESCAPE_REGEX, (_, escape, character) => escape ? unescape(escape) : character)); } else { throw new Error(`Invalid template style argument: ${chunk} (in style '${name}')`); } } return results; }, "parseArguments"); const parseStyle = /* @__PURE__ */ __name$1((style) => { STYLE_REGEX.lastIndex = 0; const results = []; let matches; while ((matches = STYLE_REGEX.exec(style)) !== null) { const name = matches[1]; if (matches[2]) { results.push([name, ...parseArguments(name, matches[2])]); } else if (matches[3] || matches[4]) { if (matches[3]) { results.push(["rgb", ...convertHexToRgb.convertHexToRgb(matches[3])]); } if (matches[4]) { results.push(["bgRgb", ...convertHexToRgb.convertHexToRgb(matches[4])]); } } else { results.push([name]); } } return results; }, "parseStyle"); const buildStyle = /* @__PURE__ */ __name$1((colorize, styles) => { const enabled = {}; for (const layer of styles) { for (const style of layer.styles) { enabled[style[0]] = layer.inverse ? null : style.slice(1); } } let current = colorize; for (const [styleName, enabledStyles] of Object.entries(enabled)) { if (!Array.isArray(enabledStyles)) { continue; } if (!(styleName in current)) { throw new Error(`Unknown style: ${styleName}`); } current = enabledStyles.length > 0 ? current[styleName](...enabledStyles) : current[styleName]; } return current; }, "buildStyle"); const makeTemplate = /* @__PURE__ */ __name$1((colorize) => (string) => { const styles = []; const chunks = []; let chunk = []; string.replaceAll( TEMPLATE_REGEX, // @ts-expect-error - TS doesn't understand that the regex args are defined (_, escapeCharacter, inverse, style, close, character) => { if (escapeCharacter) { chunk.push(unescape(escapeCharacter)); } else if (style) { const joinedChunk = chunk.join(""); chunk = []; chunks.push(styles.length === 0 ? joinedChunk : buildStyle(colorize, styles)(joinedChunk)); styles.push({ inverse, styles: parseStyle(style) }); } else if (close) { if (styles.length === 0) { throw new Error("Found extraneous } in template literal"); } chunks.push(buildStyle(colorize, styles)(chunk.join(""))); chunk = []; styles.pop(); } else { chunk.push(character); } } ); chunks.push(chunk.join("")); if (styles.length > 0) { throw new Error(`template literal is missing ${styles.length} closing bracket${styles.length === 1 ? "" : "s"} (\`}\`)`); } return chunks.join(""); }, "makeTemplate"); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const colorize = new colorize_server.Colorize(); const makeTaggedTemplate = /* @__PURE__ */ __name((instance) => makeColorizeTemplate(makeTemplate(instance)), "makeTaggedTemplate"); const template = makeTemplate(colorize); const template$1 = makeColorizeTemplate(template); module.exports = template$1; module.exports.makeTaggedTemplate = makeTaggedTemplate; module.exports.template = template;