Skip to content

Commit

Permalink
fix: calculate box width without escape sequence chars (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
yongqi14 authored Jan 13, 2025
1 parent 0e13792 commit c5c1534
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/box.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { consola } from "./utils";
import { colors } from "../src/utils";

function main() {
consola.box(`I am the default banner`);

consola.box(
`${colors.red("I")} ${colors.yellowBright("am")} ${colors.yellow("the")} ${colors.green("rainbow")} ${colors.blue("banner")}`,
);

consola.box({
title: "Box with options",
message: `I am a banner with different options`,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ 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) => line.length)) + paddingOffset;
Math.max(...textLines.map((line) => stripAnsi(line).length)) +
paddingOffset;
const widthOffset = width + paddingOffset;

const leftSpace =
Expand Down

0 comments on commit c5c1534

Please sign in to comment.