fix: calculate box width with the title width (#362)

This commit is contained in:
kricsleo
2025-03-18 18:16:17 +08:00
committed by GitHub
parent 7b82f84bfa
commit 71df5634ab
2 changed files with 9 additions and 2 deletions

View File

@@ -8,6 +8,11 @@ function main() {
`${colors.red("I")} ${colors.yellowBright("am")} ${colors.yellow("the")} ${colors.green("rainbow")} ${colors.blue("banner")}`,
);
consola.box({
title: "longer title",
message: "short msg",
});
consola.box({
title: "Box with options",
message: `I am a banner with different options`,

View File

@@ -256,8 +256,10 @@ export function box(text: string, _opts: BoxOpts = {}) {
opts.style.padding % 2 === 0 ? opts.style.padding : opts.style.padding + 1;
const height = textLines.length + paddingOffset;
const width =
Math.max(...textLines.map((line) => stripAnsi(line).length)) +
paddingOffset;
Math.max(
...textLines.map((line) => stripAnsi(line).length),
opts.title ? stripAnsi(opts.title).length : 0,
) + paddingOffset;
const widthOffset = width + paddingOffset;
const leftSpace =