Files
yinx-cli/lib/bigtext.js
2025-12-16 16:21:26 +08:00

47 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import color from "picocolors";
// 计算字符串显示宽度中文2英文1
function strWidth(str) {
let width = 0;
for (const char of str) {
width += char.charCodeAt(0) > 127 ? 2 : 1;
}
return width;
}
// 大字效果框
export function bigText(text, options = {}) {
const {
style = "double",
textColor = "yellow",
padding = 2,
} = options;
const chars = {
block: { tl: "█", tr: "█", bl: "█", br: "█", h: "█", v: "█" },
double: { tl: "╔", tr: "╗", bl: "╚", br: "╝", h: "═", v: "║" },
simple: { tl: "┌", tr: "┐", bl: "└", br: "┘", h: "─", v: "│" },
};
const c = chars[style] || chars.double;
const lines = text.split("\n");
const maxWidth = Math.max(...lines.map(strWidth));
const boxWidth = maxWidth + padding * 2;
const result = [];
result.push(c.tl + c.h.repeat(boxWidth) + c.tr);
result.push(c.v + " ".repeat(boxWidth) + c.v);
for (const line of lines) {
const lineWidth = strWidth(line);
const rightPad = maxWidth - lineWidth;
result.push(c.v + " ".repeat(padding) + line + " ".repeat(rightPad + padding) + c.v);
}
result.push(c.v + " ".repeat(boxWidth) + c.v);
result.push(c.bl + c.h.repeat(boxWidth) + c.br);
const box = result.join("\n");
return color[textColor] ? color[textColor](box) : box;
}