cliui

中文文档
9.0.1发布于 2025/03/17ISCES Module近 30 天下载 10.3亿

easily create complex multi-column command-line-interfaces

原始使用文档
文档目录9 节
  1. Example
  2. Deno/ESM Support
  3. Layout DSL
  4. Methods
  5. cliui({width: integer})
  6. cliui({wrap: boolean})
  7. cliui.div(column, column, column)
  8. cliui.span(column, column, column)
  9. cliui.resetOutput()

cliui

ci NPM version Conventional Commits nycrc config on GitHub

easily create complex multi-column command-line-interfaces.

Example

npm i cliui@latest chalk@latest
const ui = require('cliui')()
const {Chalk} = require('chalk');
const chalk = new Chalk();

ui.div('Usage: $0 [command] [options]')

ui.div({
  text: 'Options:',
  padding: [2, 0, 1, 0]
})

ui.div(
  {
    text: "-f, --file",
    width: 20,
    padding: [0, 4, 0, 4]
  },
  {
    text: "the file to load." +
      chalk.green("(if this description is long it wraps).")
    ,
    width: 20
  },
  {
    text: chalk.red("[required]"),
    align: 'right'
  }
)

console.log(ui.toString())

Deno/ESM Support

As of v7 cliui supports Deno and ESM:

import cliui from "cliui";
import chalk from "chalk";
// Deno: import cliui from "https://deno.land/x/cliui/deno.ts";

const ui = cliui({})

ui.div('Usage: $0 [command] [options]')

ui.div({
  text: 'Options:',
  padding: [2, 0, 1, 0]
})

ui.div(
  {
    text: "-f, --file",
    width: 20,
    padding: [0, 4, 0, 4]
  },
  {
    text: "the file to load." +
      chalk.green("(if this description is long it wraps).")
    ,
    width: 20
  },
  {
    text: chalk.red("[required]"),
    align: 'right'
  }
)

console.log(ui.toString())

Layout DSL

cliui exposes a simple layout DSL:

If you create a single ui.div, passing a string rather than an object:

  • \n: characters will be interpreted as new rows.
  • \t: characters will be interpreted as new columns.
  • \s: characters will be interpreted as padding.

as an example...

var ui = require('./')({
  width: 60
})

ui.div(
  'Usage: node ./bin/foo.js\n' +
  '  <regex>\t  provide a regex\n' +
  '  <glob>\t  provide a glob\t [required]'
)

console.log(ui.toString())

will output:

Usage: node ./bin/foo.js
  <regex>  provide a regex
  <glob>   provide a glob          [required]

Methods

cliui = require('cliui')

cliui({width: integer})

Specify the maximum width of the UI being generated. If no width is provided, cliui will try to get the current window's width and use it, and if that doesn't work, width will be set to 80.

cliui({wrap: boolean})

Enable or disable the wrapping of text in a column.

cliui.div(column, column, column)

Create a row with any number of columns, a column can either be a string, or an object with the following options:

  • text: some text to place in the column.
  • width: the width of a column.
  • align: alignment, right or center.
  • padding: [top, right, bottom, left].
  • border: should a border be placed around the div?

cliui.span(column, column, column)

Similar to div, except the next row will be appended without a new line being created.

cliui.resetOutput()

Resets the UI elements of the current cliui instance, maintaining the values set for width and wrap.

版本与发布记录

cliui 共发布 27 个版本,首次发布于 2015/04/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
9.0.1latest2025/03/171.6 年前
9.0.02025/03/161.6 年前
8.0.12022/10/014 年前
8.0.02022/10/014 年前
7.0.42020/11/095 年前
7.0.32020/10/166 年前

关于 cliui 的常见问题

cliui 是什么?

easily create complex multi-column command-line-interfaces

如何安装 cliui?

在项目目录运行 npm install cliui,或使用 pnpm add / yarn add / bun add。

cliui 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cliui 是否存在。

cliui 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

cliui 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

cliui 可以商用吗?

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

cliui 的下载量有多少?

根据 npm 官方下载统计,cliui 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,033,751,747 次,日均约 34,458,392 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

cliui 还在维护吗?

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