@ansi-tools/parser

中文文档
1.1.0发布于 2026/09/12ISCTypeScriptES Module零依赖近 30 天下载 1.8万

ANSI escape code parser and tokenizer. Parse terminal control sequences (CSI, SGR, OSC, DCS) from strings. Zero dependencies.

原始使用文档
文档目录11 节
  1. Features
  2. Installation
  3. Usage
  4. Examples
  5. Default (raw/unescaped)
  6. Escaped
  7. Tokenizer & generators
  8. tokenize
  9. Generators
  10. Type Definitions
  11. License

@ansi-tools/parser

Parser for ANSI escape sequences.

Features

  • ✅ Supports CSI, OSC, DCS, ESC, APC, SOS, PM, etc.
  • ✅ Handles 7-bit (\x1b or \u001b) and 8-bit (\u009b) introducers
  • ✅ Handles octal (\033) and shorthand \e introducers (only escaped)
  • ✅ Multiple string terminators (\x1b\\, \x07)
  • ✅ Zero dependencies
  • ✅ Separate optimized modules for raw and escaped input

Used by ansi.tools.

Installation

npm install @ansi-tools/parser

Usage

import { parse } from "@ansi-tools/parser";

const input = "\x1b[31mHello\x1b[0m World";

for (const code of parse(input)) {
  console.log(code);
}

There is a difference between escaped and unescaped input. Only with an escaped input string the raw input and the positions can be preserved in the tokens and control codes. See the example below for the default and the /escaped import.

The default and unescaped tokenization is roughly ~30% faster. Use this if you just need the control codes.

Examples

Default (raw/unescaped)

import { parse } from "@ansi-tools/parser";

parse(`\x1b[31mHello\x1b[0m`);

// result:
[
  {
    type: "CSI",
    pos: 0,
    raw: "\u001b[31m",
    command: "m",
    params: ["31"],
  },
  {
    type: "TEXT",
    pos: 5,
    raw: "Hello",
  },
  {
    type: "CSI",
    pos: 10,
    raw: "\u001b[0m",
    command: "m",
    params: ["0"],
  },
];

Escaped

import { parse } from "@ansi-tools/parser/escaped";

parse(String.raw`\x1b[31mHello\x1b[0m`);

// result:
[
  {
    type: "CSI",
    pos: 0,
    raw: "\\x1b[31m",
    command: "m",
    params: ["31"],
  },
  {
    type: "TEXT",
    pos: 8,
    raw: "Hello",
  },
  {
    type: "CSI",
    pos: 13,
    raw: "\\x1b[0m",
    command: "m",
    params: ["0"],
  },
];

Tokenizer & generators

The tokenizer and generators are also available, for both the default and the /escaped versions.

tokenize

import { tokenize } from "@ansi-tools/parser";

const input = "\x1b[31m";

for (const token of tokenize(input)) {
  console.log(token);
}

Generators

import { tokenizer, parser } from "@ansi-tools/parser";

const input = "\x1b[31mHello\x1b[0m";

const tokens = tokenizer(input);

const codes = parser(tokens);

for (const code of codes) {
  console.log(code);
}

Type Definitions

function tokenize(input: string): TOKEN[];
function parse(input: string): CODE[];

function* tokenizer(input: string): Generator<TOKEN>;
function* parser(tokens: Generator<TOKEN>): Generator<CODE>;

type TOKEN = {
  type: "INTRODUCER" | "DATA" | "FINAL" | "TEXT";
  pos: number;
  raw: string;
  code?: string;
  intermediate?: string;
};

type CONTROL_CODE = {
  type: "CSI" | "DCS" | "DEC" | "ESC" | "OSC" | "SGR" | "STRING" | "PRIVATE";
  command: string;
  raw: string;
  params: string[];
  parameterGroups?: string[][];
  pos: number;
};

type TEXT = {
  type: "TEXT";
  raw: string;
  pos: number;
};

type CODE = CONTROL_CODE | TEXT;

License

ISC

版本与发布记录

@ansi-tools/parser 共发布 22 个版本,首次发布于 2025/07/14;过去 12 个月发布了 6 个版本。

版本发布日期距今
1.1.0latest2026/09/121 个月前
1.0.172026/05/175 个月前
1.0.162026/05/175 个月前
1.0.152026/03/137 个月前
1.0.142026/03/137 个月前
1.0.132026/03/137 个月前

关于 @ansi-tools/parser 的常见问题

@ansi-tools/parser 是什么?

ANSI escape code parser and tokenizer. Parse terminal control sequences (CSI, SGR, OSC, DCS) from strings. Zero dependencies.

如何安装 @ansi-tools/parser?

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

@ansi-tools/parser 支持 TypeScript 吗?

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

@ansi-tools/parser 支持 ESM 还是 CommonJS?

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

@ansi-tools/parser 可以商用吗?

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

@ansi-tools/parser 的下载量有多少?

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

@ansi-tools/parser 还在维护吗?

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