47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
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;
|
||
}
|