@skyra/start-banner
Version:
A banner generator utility for your CLI
57 lines (54 loc) • 2.38 kB
JavaScript
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
// src/lib/utils.ts
var ansiRegExp = new RegExp(
[
String.raw`[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*`,
String.raw`[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?\u0007)`,
String.raw`(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))`
].join("|"),
"g"
);
function escapedLength(line) {
return line.replaceAll(ansiRegExp, "").length;
}
__name(escapedLength, "escapedLength");
function generateLineData(lines) {
return lines.map((line) => ({ line, length: escapedLength(line) }));
}
__name(generateLineData, "generateLineData");
function generateFrameData(lines) {
if (!lines?.length) return { length: 0, lines: [] };
const entries = generateLineData(lines);
const length = entries.reduce((prev, entry) => Math.max(prev, entry.length), 0);
return {
length,
lines: entries.map((entry) => `${entry.line}${" ".repeat(length - entry.length)}`)
};
}
__name(generateFrameData, "generateFrameData");
// src/lib/creator.ts
function createBanner(options) {
const logoHeight = options.logo?.length ?? 0;
const nameHeight = options.name?.length ?? 0;
const extraHeight = options.extra?.length ?? 0;
const detailsHeight = nameHeight + extraHeight;
const fullHeight = Math.max(logoHeight, detailsHeight);
if (fullHeight === 0) throw new Error("Expected any of the options to be passed");
if (logoHeight === 0) return [...options.name ?? [], ...options.extra ?? []].join("\n");
if (detailsHeight === 0) return options.logo.join("\n");
const logoFrame = generateFrameData(options.logo);
if (logoFrame.length === 0) return [...options.name ?? [], ...options.extra ?? []].join("\n");
const logoPadding = " ".repeat(logoFrame.length);
const lines = [];
for (let i = 0, nl = 0, el = 0; i < fullHeight; ++i) {
const left = i < logoHeight ? logoFrame.lines[i] : logoPadding;
const right = nl < nameHeight ? options.name[nl++] : el < extraHeight ? options.extra[el++] : "";
lines.push(right.length === 0 ? left.trimEnd() : `${left} ${right}`);
}
return lines.join("\n");
}
__name(createBanner, "createBanner");
export { ansiRegExp, createBanner, escapedLength, generateFrameData, generateLineData };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map