Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/utils/box.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import stringWidth from "string-width";
import { getColor } from "./color";
import { stripAnsi } from "./string";

Expand Down Expand Up @@ -257,8 +258,8 @@ export function box(text: string, _opts: BoxOpts = {}) {
const height = textLines.length + paddingOffset;
const width =
Math.max(
...textLines.map((line) => stripAnsi(line).length),
opts.title ? stripAnsi(opts.title).length : 0,
...textLines.map((line) => stringWidth(line)),
opts.title ? stringWidth(opts.title) : 0,
) + paddingOffset;
const widthOffset = width + paddingOffset;

Expand All @@ -273,12 +274,12 @@ export function box(text: string, _opts: BoxOpts = {}) {
if (opts.title) {
const title = _color ? _color(opts.title) : opts.title;
const left = borderStyle.h.repeat(
Math.floor((width - stripAnsi(opts.title).length) / 2),
Math.floor((width - stringWidth(opts.title)) / 2),
);
const right = borderStyle.h.repeat(
width -
stripAnsi(opts.title).length -
stripAnsi(left).length +
stringWidth(opts.title) -
stringWidth(left) +
paddingOffset,
);
boxLines.push(
Expand Down Expand Up @@ -312,7 +313,7 @@ export function box(text: string, _opts: BoxOpts = {}) {
// Text line
const line = textLines[i - valignOffset];
const left = " ".repeat(paddingOffset);
const right = " ".repeat(width - stripAnsi(line).length);
const right = " ".repeat(width - stringWidth(line));
boxLines.push(
`${leftSpace}${borderStyle.v}${left}${line}${right}${borderStyle.v}`,
);
Expand Down