@je-es/format
Version:
A function to format text with ANSI styles, padding, and borders
207 lines (203 loc) • 8.92 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/code/main.ts
var main_exports = {};
__export(main_exports, {
apply: () => apply,
combine: () => combine,
style: () => style2,
t_style: () => import_ansi.t_style
});
module.exports = __toCommonJS(main_exports);
// src/code/modules/types.ts
var import_ansi = require("@je-es/ansi");
// src/code/modules/format.ts
var ansi = __toESM(require("@je-es/ansi"));
var style2 = (input, options = {}) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
if (Object.keys(options).length === 0)
return input;
let res = "";
let originalLength = input.length + (((_b = (_a = options.prefix) == null ? void 0 : _a.value) == null ? void 0 : _b.length) || 0) + (((_d = (_c = options.suffix) == null ? void 0 : _c.value) == null ? void 0 : _d.length) || 0);
if (options.prefix) {
if (((_e = options.ansi) == null ? void 0 : _e.bg) && !((_g = (_f = options.prefix) == null ? void 0 : _f.style) == null ? void 0 : _g.bg))
options.prefix.style.bg = (_h = options.ansi) == null ? void 0 : _h.bg;
if (((_i = options.ansi) == null ? void 0 : _i.fg) && !((_k = (_j = options.prefix) == null ? void 0 : _j.style) == null ? void 0 : _k.fg))
options.prefix.style.fg = (_l = options.ansi) == null ? void 0 : _l.fg;
if (((_m = options.ansi) == null ? void 0 : _m.attr) && !((_o = (_n = options.prefix) == null ? void 0 : _n.style) == null ? void 0 : _o.attr))
options.prefix.style.attr = (_p = options.ansi) == null ? void 0 : _p.attr;
res += Helpers.ApplyPrefix(options.prefix);
}
if (options.ansi)
res += Helpers.ApplyStyle(input, options);
if (options.suffix) {
if (((_q = options.ansi) == null ? void 0 : _q.bg) && !((_s = (_r = options.suffix) == null ? void 0 : _r.style) == null ? void 0 : _s.bg))
options.suffix.style.bg = (_t = options.ansi) == null ? void 0 : _t.bg;
if (((_u = options.ansi) == null ? void 0 : _u.fg) && !((_w = (_v = options.suffix) == null ? void 0 : _v.style) == null ? void 0 : _w.fg))
options.suffix.style.fg = (_x = options.ansi) == null ? void 0 : _x.fg;
if (((_y = options.ansi) == null ? void 0 : _y.attr) && !((_A = (_z = options.suffix) == null ? void 0 : _z.style) == null ? void 0 : _A.attr))
options.suffix.style.attr = (_B = options.ansi) == null ? void 0 : _B.attr;
res += Helpers.ApplySuffix(options.suffix);
}
if (options.width && options.width > originalLength) {
{
if (options.align === "center") {
const left = Math.floor((options.width - originalLength) / 2);
const right = options.width - originalLength - left;
res = " ".repeat(left) + res + " ".repeat(right);
} else if (options.align === "right") {
const left = options.width - originalLength;
res = " ".repeat(left) + res;
} else {
const right = options.width - originalLength;
res += " ".repeat(right);
}
}
originalLength = options.width;
}
if (options.padding)
res = Helpers.ApplyPadding(res, options);
if (options.border)
res = Helpers.ApplyBorder(res, options, originalLength);
return res;
};
var Helpers = {
ApplyPrefix: (options) => {
const { value, style: style3 } = options;
return ansi.style(value, style3);
},
ApplySuffix: (options) => {
const { value, style: style3 } = options;
return ansi.style(value, style3);
},
ApplyStyle: (input, options) => {
const { fg, bg, attr } = options.ansi || {};
return ansi.style(input, { fg, bg, attr });
},
ApplyPadding: (input, options) => {
var _a, _b, _c, _d;
if ((_a = options.padding) == null ? void 0 : _a.left) {
input = " ".repeat(options.padding.left) + input;
}
if ((_b = options.padding) == null ? void 0 : _b.right) {
input = input + " ".repeat(options.padding.right);
}
if ((_c = options.padding) == null ? void 0 : _c.top) {
input = "\n".repeat(options.padding.top) + input;
}
if ((_d = options.padding) == null ? void 0 : _d.bottom) {
input = input + "\n".repeat(options.padding.bottom);
}
return input;
},
ApplyBorder: (input, options, originalLength) => {
var _a, _b;
const Bwidth = ((_a = options.border) == null ? void 0 : _a.width) || 1;
const color = ((_b = options.border) == null ? void 0 : _b.color) || "white";
const _paddingWidth = Helpers.More.__GetPaddingWidth(options);
const width = Math.max(originalLength + _paddingWidth + 2, 0);
let chars = { topBeg: "", topEnd: "", botBeg: "", botEnd: "", char: "", side: "" };
switch (Bwidth) {
case 1:
chars = { topBeg: "\u250C", topEnd: "\u2510", botBeg: "\u2514", botEnd: "\u2518", char: "\u2500", side: "\u2502" };
break;
case 2:
chars = { topBeg: "\u2554", topEnd: "\u2557", botBeg: "\u255A", botEnd: "\u255D", char: "\u2550", side: "\u2551" };
break;
case 3:
chars = { topBeg: "\u250F", topEnd: "\u2513", botBeg: "\u2517", botEnd: "\u251B", char: "\u2501", side: "\u2503" };
break;
case 4:
chars = { topBeg: "\u256D", topEnd: "\u256E", botBeg: "\u2570", botEnd: "\u256F", char: "\u2500", side: "\u2502" };
break;
case 5:
chars = { topBeg: "\u256D", topEnd: "\u256E", botBeg: "\u2570", botEnd: "\u256F", char: "\u2550", side: "\u2551" };
break;
default:
chars = { topBeg: "\u250C", topEnd: "\u2510", botBeg: "\u2514", botEnd: "\u2518", char: "\u2500", side: "\u2502" };
break;
}
const totalWidth = originalLength + _paddingWidth + 2;
const paddedWidth = Math.max(totalWidth, 0);
input = input.split("\n").map((line) => {
const lineWidth = line.length;
if (lineWidth) {
const n = paddedWidth - lineWidth - 2 + 1;
const x = n && n > 0 ? n : 1;
return ansi.style(chars.side, { fg: color }) + " " + line + " ".repeat(x) + ansi.style(chars.side, { fg: color });
} else {
return ansi.style(chars.side, { fg: color }) + " ".repeat(paddedWidth) + ansi.style(chars.side, { fg: color });
}
}).join("\n");
const firstLine = ansi.style(chars.topBeg + chars.char.repeat(paddedWidth) + chars.topEnd, { fg: color });
input = firstLine + "\n" + input;
const lastLine = ansi.style(chars.botBeg + chars.char.repeat(paddedWidth) + chars.botEnd, { fg: color });
input = input + "\n" + lastLine;
return input;
},
More: {
__GetPaddingWidth: (options) => {
var _a, _b;
return (((_a = options.padding) == null ? void 0 : _a.left) || 0) + (((_b = options.padding) == null ? void 0 : _b.right) || 0);
}
}
};
// src/code/modules/pattern.ts
var apply = (msg, pattern, values) => {
let result = "";
for (let x = 0; x < pattern.length; x++) {
const pat = pattern[x];
for (let i = 0; i < pat.length; i++) {
if (values && pat[i].gvalue) {
result += style2(values[pat[i].gvalue ? pat[i].gvalue : msg], pat[i].options);
} else
result += style2(pat[i].value || msg, pat[i].options);
}
if (x < pattern.length - 1)
result += "\n";
}
return result;
};
var combine = (opt) => {
let res = "";
for (let i = 0; i < opt.length; i++) {
res += apply(opt[i].msg, opt[i].pattern, opt[i].values);
if (i < opt[i].pattern.length - 1)
res += "\n";
}
return res;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
apply,
combine,
style,
t_style
});
//# sourceMappingURL=main.js.map