@davidwells/box-logger

中文文档
2.0.8发布于 2026/01/01MITTypeScriptCommonJS近 30 天下载 2167

A simple utility for creating boxes around text in the console

原始使用文档
文档目录21 节
  1. Installation
  2. Usage
  3. Box
  4. Stacked Boxes
  5. Horizontal Boxes
  6. Header
  7. Line
  8. Typed Variants
  9. Error Handling
  10. Examples
  11. Basic Box
  12. Rounded Box with Title
  13. Bold Box
  14. Header (No Left Border)
  15. Line with Text
  16. Line with Left and Right
  17. Utilities
  18. terminalSize
  19. getInnerWidth
  20. License
  21. Other rendering libs

box-logger

A simple utility for creating formatted text boxes, headers, and lines in the terminal.

Installation

npm install @davidwells/box-logger

Usage

Box

Create a box around text with customizable borders, padding, and styling.

const { makeBox, logBox } = require('@davidwells/box-logger')

// Simple usage with a string
console.log(makeBox('Hello World'))

// With options
console.log(makeBox({
  content: 'Hello World',
  textAlign: 'center',
  paddingLeft: 4,
  paddingRight: 4,
  borderColor: 'green',
  borderStyle: 'bold'
}))

// With title and content
console.log(makeBox({
  title: 'My Title',
  content: 'Box content here',
  borderStyle: 'rounded'
}))

// Hex colors are supported
console.log(makeBox({
  content: 'Custom colors',
  borderColor: '#4287f5',
  backgroundColor: '#1a1a2e'
}))

// Dynamic content - function receives { innerWidth } for sizing
console.log(makeBox({
  title: 'Dynamic Width',
  content: ({ innerWidth }) => generateContent({ width: innerWidth }),
  paddingLeft: 1,
  paddingRight: 0
}))

// Async content - use makeBoxAsync for async functions
const box = await makeBoxAsync({
  title: 'Async Content',
  content: async ({ innerWidth }) => await fetchData({ width: innerWidth })
})
console.log(box)

// Call logger directly
logBox('Log the box directly')

Box Options

OptionTypeDefaultDescription
contentstring | Section | function-Content (or function receiving { innerWidth })
titlestring | Section-Title section at top of box
fontStyle'normal' | 'bold''normal'Font style
textAlign'left' | 'center' | 'right''left'Text alignment
colorstring-Text color (chalk color or hex like '#ff0000')
backgroundColorstring-Background color (chalk color or hex)
paddingTopnumber0Newlines before content
paddingBottomnumber0Newlines after content
paddingLeftnumber2Spaces for left padding
paddingRightnumber2Spaces for right padding
borderColorstring-Border color (chalk color or hex)
borderStylestring'normal'Border style (see below)
borderTopbooleantrueShow top border
borderBottombooleantrueShow bottom border
borderLeftbooleantrueShow left border
borderRightbooleantrueShow right border
borderTextstring-Text embedded in top border
borderTextColorstring-Color for border text
borderTextAlign'left' | 'center' | 'right''left'Border text alignment
widthnumber | string-Fixed width
minWidthnumber | string-Minimum width ('100%' for full width)
maxWidthnumber | stringterminal widthMaximum width
marginTopnumber0Newlines before box
marginBottomnumber0Newlines after box
marginLeftnumber0Spaces before each line
wrapTextbooleantrueWrap text to fit inside box
typestring-Box type: 'success', 'error', 'warning', 'info'
iconstring-Custom icon
headerbooleanfalseHeader mode (no left border)
edgesobject-Custom border symbols

Border Styles

  • normal - Standard box drawing characters (─│┌┐└┘)
  • rounded - Rounded corners (╭╮╰╯)
  • bold - Heavy weight borders (━┃┏┓┗┛)
  • double - Double line borders (═║╔╗╚╝)
  • dashed - Dashed horizontal lines (╌)
  • dotted - Dotted horizontal lines (┈)
  • thick - Block characters (█▀▄)
  • half-thick - Half block borders (▐▌▔▂)
  • ultra-thick - Full block borders (█)
  • ascii - ASCII fallback (-|+)
  • dots - Bullet dots (•)
  • circles - Hollow circles (○)
  • stars - Sparkle stars (✨)
  • triple - Triple lines (≡)
  • transparent - Invisible borders (spaces)
  • none - No borders at all
  • filled - For use with backgroundColor

Section Object

Title and content can be strings or Section objects for more control:

makeBox({
  title: {
    left: 'Title',
    right: 'v1.0.0',
    fontStyle: 'bold',
    color: 'cyan'
  },
  content: {
    left: 'Description',
    right: 'Status: OK',
    color: 'green'
  }
})

Section properties: left, right, center, color, fontStyle, paddingLeft, paddingRight, paddingTop, paddingBottom, truncate

Stacked Boxes

Stack multiple boxes vertically with shared borders:

const { makeStackedBoxes } = require('@davidwells/box-logger')

console.log(makeStackedBoxes([
  { title: 'Box 1', content: 'First box content' },
  { title: 'Box 2', content: 'Second box content' },
  { title: 'Box 3', content: 'Third box content' }
], {
  borderStyle: 'rounded',
  borderColor: 'cyan'
}))

Horizontal Boxes

Render boxes side by side:

const { makeHorizontalBoxes } = require('@davidwells/box-logger')

console.log(makeHorizontalBoxes([
  { content: 'Left box' },
  { content: 'Middle box' },
  { content: 'Right box' }
], {
  gap: 2,
  mergeBoxes: true  // Merge adjacent borders
}))

Options: gap, marginLeft, connectRows, justifyContent ('flex-start', 'flex-end', 'space-between'), wrap, mergeBoxes

Header

Create a header (box with no left border):

const { makeHeader, logHeader } = require('@davidwells/box-logger')

// Simple usage
console.log(makeHeader('Hello World'))

// With options
console.log(makeHeader({
  content: 'Hello World',
  borderColor: 'blue'
}))

// Typed variants
logHeader.success('Operation completed')
logHeader.error('An error occurred')
logHeader.warning('Please be careful')
logHeader.info('Information')

Header uses the same options as Box (it's just header: true under the hood).

Line

Create horizontal lines with optional text:

const { makeLine, logLine } = require('@davidwells/box-logger')

// Simple line
console.log(makeLine())

// Line with centered text
console.log(makeLine('Section Title'))

// Line with options
console.log(makeLine({
  text: 'Styled text',
  fontStyle: 'bold',
  borderColor: 'green'
}))

// Left and right text
console.log(makeLine({
  left: 'Section',
  right: 'v1.2.3',
  borderColor: 'cyan'
}))

// All three positions
console.log(makeLine({
  left: 'Left',
  center: 'Center',
  right: 'Right'
}))

Line Options

OptionTypeDefaultDescription
textstring-Text to display (use with textAlign)
leftstring-Left-aligned text
rightstring-Right-aligned text
centerstring-Center-aligned text
colorstring-Text color
backgroundColorstring-Background color for text
lineBackgroundColorstring-Background for entire line
fontStyle'normal' | 'bold''normal'Font style
borderColorstring-Line color
borderStylestring'normal'Line style
textAlign'left' | 'center' | 'right''center'Text alignment
paddingLeftnumber4Left padding
paddingRightnumber4Right padding
minWidthnumber | string-Minimum width
maxWidthnumber | stringterminal widthMaximum width
linesnumber-Generate multiple blank lines

Typed Variants

All log functions have typed variants with automatic icons and colors:

const { logBox, logHeader, logLine } = require('@davidwells/box-logger')

// Box variants
logBox.success('Operation completed successfully')
logBox.error('An error occurred')
logBox.warning('Please be careful')
logBox.info('Here is some information')

// Header variants
logHeader.success('Success!')
logHeader.error('Error!')

// Line variants
logLine.success('Success line')
logLine.error('Error line')

Error Handling

Pass an Error object to logBox for formatted error display:

try {
  throw new Error('Something went wrong')
} catch (err) {
  logBox.error(err)  // Shows error name, message, and cleaned stack trace
}

Examples

Basic Box

┌─────────────┐
│  Hello World  │
└─────────────┘

Rounded Box with Title

╭──────────────────────╮
│  My Title            │
├──────────────────────┤
│  Content goes here   │
╰──────────────────────╯

Bold Box

┏━━━━━━━━━━━━━━━━━━━━━━┓
┃  Hello World         ┃
┗━━━━━━━━━━━━━━━━━━━━━━┛

Header (No Left Border)

──────────────────────────────┐
  Hello World                 │
──────────────────────────────┘

Line with Text

────────── Section Title ──────────

Line with Left and Right

──── Section ──────────── v1.2.3 ────

Utilities

terminalSize

Get the current terminal dimensions:

const { terminalSize } = require('@davidwells/box-logger')

const { columns, rows } = terminalSize()
console.log(`Terminal is ${columns}x${rows}`)

getInnerWidth

Calculate the inner content width for a box configuration:

const { getInnerWidth } = require('@davidwells/box-logger')

const innerWidth = getInnerWidth({
  paddingLeft: 1,
  paddingRight: 0,
  marginLeft: 5
})
// Use innerWidth to pre-size content before calling makeBox

License

MIT

Other rendering libs

版本与发布记录

@davidwells/box-logger 共发布 31 个版本,首次发布于 2025/04/05;过去 12 个月发布了 10 个版本。

版本发布日期距今
2.0.8latest2026/01/0110 个月前
2.0.72026/01/0110 个月前
2.0.62025/12/0511 个月前
2.0.52025/12/0511 个月前
2.0.42025/12/0511 个月前
2.0.32025/11/2811 个月前

关于 @davidwells/box-logger 的常见问题

@davidwells/box-logger 是什么?

A simple utility for creating boxes around text in the console

如何安装 @davidwells/box-logger?

在项目目录运行 npm install @davidwells/box-logger,或使用 pnpm add / yarn add / bun add。

@davidwells/box-logger 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@davidwells/box-logger 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@davidwells/box-logger 可以商用吗?

@davidwells/box-logger 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@davidwells/box-logger 的下载量有多少?

根据 npm 官方下载统计,@davidwells/box-logger 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,167 次,日均约 72 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@davidwells/box-logger 还在维护吗?

最新版本 2.0.8 发布于 2026/01/01(10 个月前);至今共发布 31 个版本,其中过去 12 个月发布了 10 个;首次发布于 2025/04/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。