@cjser/slice-ansi

中文文档
10.0.0-cjser.2发布于 2026/11/01MITTypeScriptESM + CJS

Slice a string with ANSI escape codes

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. API
  4. sliceAnsi(string, startSlice, endSlice?)
  5. Related
  6. Maintainers
  7. cjser

slice-ansi XO: Linted

Slice a string with ANSI escape codes

Install

npm install slice-ansi

Usage

import chalk from 'chalk';
import sliceAnsi from 'slice-ansi';

const string = 'The quick brown ' + chalk.red('fox jumped over ') +
    'the lazy ' + chalk.green('dog and then ran away with the unicorn.');

console.log(sliceAnsi(string, 20, 30));

API

sliceAnsi(string, startSlice, endSlice?)

Columns are counted the same way as string-width.

[!NOTE] Tabs and other control characters take no columns. Replace tabs with spaces first if you need tab stops.

string

Type: string

String with ANSI escape codes. Like one styled by chalk.

startSlice

Type: number

Zero-based visible-column index at which to start the slice. Grapheme clusters (for example, emoji sequences and combining marks) are kept intact.

endSlice

Type: number

Zero-based visible-column index at which to end the slice. If a full grapheme cluster would cross endSlice, it is excluded.

  • wrap-ansi - Wordwrap a string with ANSI escape codes
  • cli-truncate - Truncate a string to a specific width in the terminal
  • chalk - Terminal string styling done right

Maintainers

cjser

This package is a CommonJS-compatible build generated by cjser for projects that still need require() support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build. Original repository: https://github.com/chalk/slice-ansi

版本与发布记录

@cjser/slice-ansi 共发布 3 个版本,首次发布于 2026/05/07;过去 12 个月发布了 3 个版本。

版本发布日期距今
10.0.0-cjser.2latest2026/11/012 天前
9.0.1-cjser.22026/09/201 个月前
9.0.0-cjser.22026/05/075 个月前

关于 @cjser/slice-ansi 的常见问题

@cjser/slice-ansi 是什么?

使用ANSI转义码对字符串进行切片

如何安装 @cjser/slice-ansi?

在项目目录运行 npm install @cjser/slice-ansi,或使用 pnpm add / yarn add / bun add。

@cjser/slice-ansi 支持 TypeScript 吗?

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

@cjser/slice-ansi 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cjser/slice-ansi 需要什么版本的 Node.js?

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

@cjser/slice-ansi 可以商用吗?

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

@cjser/slice-ansi 还在维护吗?

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