包详细信息

@visulima/boxen

visulima107kMIT1.0.30

Util.format-like string formatting utility.

anolilab, visulima, ansi, cli

自述文件

Visulima boxen

Boxen Examples

Create boxes in the terminal, built on top of cli-boxes, string-width, terminal-size and wrap-ansi


typescript-image npm-image license-image

Daniel Bannert's open source work is supported by the community on GitHub Sponsors


Install

npm install @visulima/boxen
yarn add @visulima/boxen
pnpm add @visulima/boxen

Usage

import { boxen } from "@visulima/boxen";

console.log(boxen("unicorn", { padding: 1 }));
/*
┌─────────────┐
│             │
│   unicorn   │
│             │
└─────────────┘
*/

console.log(boxen("unicorn", { padding: 1, margin: 1, borderStyle: "double" }));
/*
   ╔═════════════╗
   ║             ║
   ║   unicorn   ║
   ║             ║
   ╚═════════════╝

*/

console.log(
    boxen("unicorns love rainbows", {
        headerText: "magical",
        headerAlignment: "center",
    }),
);
/*
┌────── magical ───────┐
│unicorns love rainbows│
└──────────────────────┘
*/

console.log(
    boxen("unicorns love rainbows", {
        headerText: "magical",
        headerAlignment: "center",
        footerText: "magical",
        footerAlignment: "center",
    }),
);
/*
┌────── magical ───────┐
│unicorns love rainbows│
└────── magical ───────┘
*/

Check more examples in the examples/boxen folder.

API

boxen(text, options?)

text

Type: string

Text inside the box.

options

Type: object

borderColor

Type: (border: string, position: BorderPosition, length: number) => string\

Color of the box border.

import { boxen } from "@visulima/boxen";
import { red, green, yellow, blue } from "@visulima/colorize";

console.log(
    boxen("Hello, world!", {
        borderColor: (border, position) => {
            if (["top", "topLeft", "topRight"].includes(position)) {
                return red(border);
            }

            if (position === "left") {
                return yellow(border);
            }

            if (position === "right") {
                return green(border);
            }

            if (["bottom", "bottomLeft", "bottomRight"].includes(position)) {
                return blue(border);
            }
        },
    }),
);
borderStyle

Type: string | object\ Default: 'single'\ Values:

  • 'single'
┌───┐
│foo│
└───┘
  • 'double'
╔═══╗
║foo║
╚═══╝
  • 'round' ('single' sides with round corners)
╭───╮
│foo│
╰───╯
  • 'bold'
┏━━━┓
┃foo┃
┗━━━┛
  • 'singleDouble' ('single' on top and bottom, 'double' on right and left)
╓───╖
║foo║
╙───╜
  • 'doubleSingle' ('double' on top and bottom, 'single' on right and left)
╒═══╕
│foo│
╘═══╛
  • 'classic'
+---+
|foo|
+---+
  • 'arrow'
↘↓↓↓↙
→foo←
↗↑↑↑↖
  • 'none'
foo

Style of the box border.

Can be any of the above predefined styles or an object with the following keys:

{
    topLeft: '+',
    topRight: '+',
    bottomLeft: '+',
    bottomRight: '+',
    top: '-',
    bottom: '-',
    left: '|',
    right: '|'
}
headerText

Type: string

Display a title at the top of the box. If needed, the box will horizontally expand to fit the text.

Example:

import { boxen } from "@visulima/boxen";

console.log(boxen("foo bar", { headerText: "example" }));

/*
┌ example ┐
│foo bar  │
└─────────┘
*/
headerColor

Type: (text: string) => string

import { red } from "@visulima/colorize";
import { boxen } from "@visulima/boxen";

console.log(
    boxen("foo bar", {
        headerText: "example",
        headerColor: (text) => red(text),
    }),
);
headerAlignment

Type: string\ Default: 'left'

Align the text in the top bar.

Values:

  • 'left'
┌ example ──────┐
│foo bar foo bar│
└───────────────┘
  • 'center'
┌─── example ───┐
│foo bar foo bar│
└───────────────┘
  • 'right'
┌────── example ┐
│foo bar foo bar│
└───────────────┘
footerText

Type: string

Display a text at the bottom of the box. If needed, the box will horizontally expand to fit the text.

Example:

import { boxen } from "@visulima/boxen";

console.log(boxen("foo bar", { footerText: "example" }));

/*
┌─────────┐
│foo bar  │
└ example ┘
*/
footerColor

Type: (text: string) => string

import { red } from "@visulima/colorize";
import { boxen } from "@visulima/boxen";

console.log(
    boxen("foo bar", {
        footerText: "example",
        footerColor: (text) => red(text),
    }),
);
footerAlignment

Type: string\ Default: 'left'

Align the footer text.

Values:

  • 'left'
┌───────────────┐
│foo bar foo bar│
└ Footer Text ──┘
  • 'center'
┌───────────────┐
│foo bar foo bar│
└─── example ───┘
  • 'right'
┌───────────────┐
│foo bar foo bar│
└────── example ┘
width

Type: number

Set a fixed width for the box.

Note: This disables terminal overflow handling and may cause the box to look broken if the user's terminal is not wide enough.

import { boxen } from "@visulima/boxen";

console.log(boxen("foo bar", { width: 15 }));
// ┌─────────────┐
// │foo bar      │
// └─────────────┘
height

Type: number

Set a fixed height for the box.

Note: This option will crop overflowing content.

import { boxen } from "@visulima/boxen";

console.log(boxen("foo bar", { height: 5 }));
// ┌───────┐
// │foo bar│
// │       │
// │       │
// └───────┘
fullscreen

Type: boolean | (width: number, height: number) => [width: number, height: number]

Whether or not to fit all available space within the terminal.

Pass a callback function to control box dimensions:

import { boxen } from "@visulima/boxen";

console.log(
    boxen("foo bar", {
        fullscreen: (width, height) => [width, height - 1],
    }),
);
padding

Type: number | object\ Default: 0

Space between the text and box border.

Accepts a number or an object with any of the top, right, bottom, left properties. When a number is specified, the left/right padding is 3 times the top/bottom to make it look nice.

margin

Type: number | object\ Default: 0

Space around the box.

Accepts a number or an object with any of the top, right, bottom, left properties. When a number is specified, the left/right margin is 3 times the top/bottom to make it look nice.

float

Type: string\ Default: 'left'\ Values: 'right' 'center' 'left'

Float the box on the available terminal screen space.

textColor

Type: (text: string) => string\

import { bgRed } from "@visulima/colorize";
import { boxen } from "@visulima/boxen";

console.log(
    boxen("foo bar", {
        textColor: (text) => bgRed(text),
    }),
);
textAlignment

Type: string\ Default: 'left'\ Values: 'left' 'center' 'right'

Align the text in the box based on the widest line.

transformTabToSpace

Type: false | number\ Default: '4'

Transform tab characters to spaces. Set to false to disable.

Supported Node.js Versions

26.3 15:00 Libraries in this ecosystem make the best effort to track Node.js’ release schedule. Here’s a post on why we think this is important.

Contributing

If you would like to help take a look at the list of issues and check our Contributing guidelines.

Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Credits

License

The visulima boxen is open-sourced software licensed under the MIT

更新日志

@visulima/boxen 1.0.30 (2025-03-07)

Bug Fixes

  • updated @visulima/packem and other dev deps, for better bundling size (e940581)

Miscellaneous Chores

  • updated dev dependencies (487a976)

Dependencies

  • @visulima/colorize: upgraded to 1.4.21
  • @visulima/path: upgraded to 1.3.5

@visulima/boxen 1.0.29 (2025-01-25)

Bug Fixes

  • fixed wrong node version range in package.json (4ae2929)

Miscellaneous Chores

  • fixed typescript url (fe65a8c)
  • updated all dev dependencies (37fb298)

Dependencies

  • @visulima/colorize: upgraded to 1.4.20
  • @visulima/path: upgraded to 1.3.4

@visulima/boxen 1.0.28 (2025-01-22)

Styles

Miscellaneous Chores

  • updated all dev dependencies and all dependencies in the app folder (87f4ccb)

Dependencies

  • @visulima/colorize: upgraded to 1.4.19

@visulima/boxen 1.0.27 (2025-01-13)

Dependencies

  • @visulima/colorize: upgraded to 1.4.18
  • @visulima/path: upgraded to 1.3.3

@visulima/boxen 1.0.26 (2025-01-12)

Bug Fixes

  • updated @visulima/packem, and all other dev dependencies (7797a1c)

Dependencies

  • @visulima/colorize: upgraded to 1.4.17
  • @visulima/path: upgraded to 1.3.2

@visulima/boxen 1.0.25 (2025-01-08)

Dependencies

  • @visulima/colorize: upgraded to 1.4.16
  • @visulima/path: upgraded to 1.3.1

@visulima/boxen 1.0.24 (2025-01-08)

Dependencies

  • @visulima/colorize: upgraded to 1.4.15
  • @visulima/path: upgraded to 1.3.0

@visulima/boxen 1.0.23 (2024-12-31)

Miscellaneous Chores

  • updated dev dependencies (9de2eab)

Dependencies

  • @visulima/colorize: upgraded to 1.4.14
  • @visulima/path: upgraded to 1.2.0

@visulima/boxen 1.0.22 (2024-12-12)

Bug Fixes

  • added missing placeholder variables into LICENSE.md file (cef32c6)
  • allow node v23 (8ca929a)
  • allowed node 23, updated dev dependencies (f99d34e)
  • boxen: fixed wrong error message for textColor (7d489b3)
  • updated packem to v1.8.2 (23f869b)
  • updated packem to v1.9.2 (47bdc2d)

Styles

Miscellaneous Chores

Dependencies

  • @visulima/colorize: upgraded to 1.4.13
  • @visulima/path: upgraded to 1.1.2

@visulima/boxen 1.0.21 (2024-10-05)

Dependencies

  • @visulima/colorize: upgraded to 1.4.12
  • @visulima/path: upgraded to 1.1.1

@visulima/boxen 1.0.20 (2024-10-05)

Bug Fixes

  • updated dev dependencies, updated packem to v1.0.7, fixed naming of some lint config files (c071a9c)

Dependencies

  • @visulima/colorize: upgraded to 1.4.11
  • @visulima/path: upgraded to 1.1.0

@visulima/boxen 1.0.19 (2024-09-24)

Bug Fixes

  • update packem to v1 (05f3bc9)
  • updated esbuild from v0.23 to v0.24 (3793010)

Miscellaneous Chores

  • updated dev dependencies (05edb67)

Dependencies

  • @visulima/colorize: upgraded to 1.4.10
  • @visulima/path: upgraded to 1.0.9

@visulima/boxen 1.0.18 (2024-09-12)

Bug Fixes

  • boxen: moved from tsup to packem (ef8c01a)

Styles

@visulima/boxen 1.0.17 (2024-09-11)

Dependencies

  • @visulima/colorize: upgraded to 1.4.9
  • @visulima/path: upgraded to 1.0.8

@visulima/boxen 1.0.16 (2024-09-07)

Dependencies

  • @visulima/colorize: upgraded to 1.4.8
  • @visulima/path: upgraded to 1.0.7

@visulima/boxen 1.0.15 (2024-09-07)

Dependencies

  • @visulima/colorize: upgraded to 1.4.7
  • @visulima/path: upgraded to 1.0.6

@visulima/boxen 1.0.14 (2024-08-30)

Miscellaneous Chores

  • updated dev dependencies (45c2a76)

Dependencies

  • @visulima/colorize: upgraded to 1.4.6
  • @visulima/path: upgraded to 1.0.5

@visulima/boxen 1.0.13 (2024-08-04)

Dependencies

  • @visulima/colorize: upgraded to 1.4.5
  • @visulima/path: upgraded to 1.0.4

@visulima/boxen 1.0.12 (2024-08-01)

Styles

Miscellaneous Chores

  • added private true into fixture package.json files (4a9494c)
  • changed typescript version back to 5.4.5 (55d28bb)
  • updated dev dependencies (ac67ec1)
  • updated dev dependencies and sorted the package.json (9571572)

Dependencies

  • @visulima/colorize: upgraded to 1.4.4
  • @visulima/path: upgraded to 1.0.3

@visulima/boxen 1.0.11 (2024-07-01)

Bug Fixes

  • update dev-dependency string-width to 7.2.0 (0cdc4e9)

@visulima/boxen 1.0.10 (2024-06-13)

Miscellaneous Chores

Build System

  • fixed found audit error, updated all dev package deps, updated deps in apps and examples (4c51950)

Dependencies

  • @visulima/colorize: upgraded to 1.4.3

@visulima/boxen 1.0.9 (2024-06-06)

Bug Fixes

Miscellaneous Chores

  • updated dev dependencies (a2e0504)

Dependencies

  • @visulima/colorize: upgraded to 1.4.2
  • @visulima/path: upgraded to 1.0.2

@visulima/boxen 1.0.8 (2024-05-24)

Bug Fixes

Miscellaneous Chores

  • changed semantic-release-npm to pnpm (b6d100a)
  • fixed wrong named folders to integration, added TEST_PROD_BUILD (1b826f5)
  • updated dev dependencies (abd319c)
  • updated dev dependencies (0767afe)

Dependencies

  • @visulima/colorize: upgraded to 1.4.1
  • @visulima/path: upgraded to 1.0.1

@visulima/boxen 1.0.7 (2024-05-03)

Bug Fixes

  • boxen: moved cli-boxes into boxen (25f2e83)

Miscellaneous Chores

@visulima/boxen 1.0.6 (2024-04-10)

Dependencies

  • @visulima/colorize: upgraded to 1.4.0

@visulima/boxen 1.0.5 (2024-04-09)

Dependencies

  • @visulima/colorize: upgraded to 1.3.3

@visulima/boxen 1.0.4 (2024-04-09)

Styles

  • disabled noPropertyAccessFromIndexSignature (#386) (2250c02)

Miscellaneous Chores

  • downgrade eslint-plugin-vitest (0162771)
  • updated dev dependencies (87dee15)
  • updated dev dependencies (bf2c635)
  • updated dev dependencies (f67c7f1)

Dependencies

  • @visulima/colorize: upgraded to 1.3.2

@visulima/boxen 1.0.3 (2024-03-27)

Dependencies

  • @visulima/colorize: upgraded to 1.3.1

@visulima/boxen 1.0.2 (2024-03-26)

Bug Fixes

  • boxen: adding cjs and esm package test (#382) (ac08502)

Miscellaneous Chores

  • better code-coverage (51d8efb)
  • updated all devDependencies (133b1ca)
  • updated dev dependencies (5f0bcd6)
  • updated dev dependencies (130b82c)
  • updated dev dependencies (956b7b3)

Continuous Integration

@visulima/boxen 1.0.1 (2024-03-09)

Bug Fixes

  • added missing type module to the package.json (510c5b7)

@visulima/boxen 1.0.0 (2024-03-06)

Features