第一版

This commit is contained in:
yinsx
2025-12-16 16:21:26 +08:00
parent 0c73ef2547
commit 8ba7e635f5
15 changed files with 1006 additions and 30 deletions

46
lib/bigtext.js Normal file
View File

@ -0,0 +1,46 @@
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;
}