@visulima/colorize
Version:
Terminal and Console string styling done right.
57 lines (54 loc) • 2.13 kB
JavaScript
import { C as Colorize } from './packem_shared/colorize.server-DSyvWqmN.mjs';
import { GradientBuilder } from './packem_shared/GradientBuilder-Dy53F9zB.mjs';
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
const colorize = new Colorize();
const forbiddenChars = /\s/g;
const gradient = /* @__PURE__ */ __name((stops, options) => {
const { hsvSpin = "short", interpolation = "rgb" } = options ?? {};
let builder = new 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(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");
export { GradientBuilder, gradient, multilineGradient };