UNPKG

@visulima/colorize

Version:

Terminal and Console string styling done right.

63 lines (58 loc) 2.38 kB
'use strict'; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } }); const colorize_server = require('./packem_shared/colorize.server-0LnwnixR.cjs'); const GradientBuilder = require('./packem_shared/GradientBuilder-Cn2sQ1aK.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); const colorize = new colorize_server.Colorize(); const forbiddenChars = /\s/g; const gradient = /* @__PURE__ */ __name((stops, options) => { const { hsvSpin = "short", interpolation = "rgb" } = options ?? {}; let builder = new GradientBuilder.GradientBuilder(colorize, stops); if (options?.loop) { builder = builder.loop(); } else if (options?.reverse) { builder = builder.reverse(); } return (string_) => { const colorsCount = Math.max(string_.replaceAll(forbiddenChars, "").length, builder.stops.length); const colors = interpolation === "rgb" ? builder.rgb(colorsCount) : builder.hsv(colorsCount, hsvSpin); let result = ""; for (const s of string_) { if (forbiddenChars.test(s)) { result += s; } else { const color = colors.shift(); result += color(s); } } return result; }; }, "gradient"); const multilineGradient = /* @__PURE__ */ __name((stops, options) => { const { hsvSpin = "short", interpolation = "rgb" } = options ?? {}; let builder = new GradientBuilder.GradientBuilder(colorize, stops); if (options?.loop) { builder = builder.loop(); } else if (options?.reverse) { builder = builder.reverse(); } return (string_) => { const lines = string_.split("\n"); const colorsCount = Reflect.apply(Math.max, null, [...lines.map((l) => l.length), builder.stops.length]); const colors = interpolation === "rgb" ? builder.rgb(colorsCount) : builder.hsv(colorsCount, hsvSpin); const results = []; for (const line of lines) { const lineColors = [...colors]; let lineResult = ""; for (const l of line) { lineResult += forbiddenChars.test(l) ? l : lineColors.shift()(l); } results.push(lineResult); } return results.join("\n"); }; }, "multilineGradient"); exports.GradientBuilder = GradientBuilder.GradientBuilder; exports.gradient = gradient; exports.multilineGradient = multilineGradient;