Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@alcalzone/ansi-tokenize
Advanced tools
Readme
Efficiently modify strings containing ANSI escape codes
If you find yourself modifying styled strings repeatedly, alternatives like slice-ansi
may end up doing a lot of unnecessary work by re-parsing the string each time. This module provides a way to parse the string into an array of tokens (characters or ANSI codes), which can then be modified and re-serialized into a styled string.
$ npm install @alcalzone/ansi-tokenize
import { tokenize } from "@alcalzone/ansi-tokenize";
// red "foo", followed by unstyled "bar"
const str = "\x1B[31mfoo\x1B[39mbar";
const tokens = tokenize(str);
// tokens will now look like this:
[
{
type: "ansi",
code: "\x1B[31m",
endCode: "\x1B[39m",
},
{
type: "char",
value: "f",
fullWidth: false,
},
{
type: "char",
value: "o",
fullWidth: false,
},
{
type: "char",
value: "o",
fullWidth: false,
},
{
type: "ansi",
code: "\x1B[39m",
endCode: "\x1B[39m",
},
{
type: "char",
value: "b",
fullWidth: false,
},
{
type: "char",
value: "a",
fullWidth: false,
},
{
type: "char",
value: "r",
fullWidth: false,
},
];
Each token is either a character
export interface Char {
type: "char";
value: string;
fullWidth: boolean;
}
where
value
is the string representation of the characterfullWidth
is true
if the character is full width (takes up 2 characters in monospace, like CJK characters)or an ANSI code
export interface AnsiCode {
type: "ansi";
code: string;
endCode: string;
}
where
code
is the ANSI code that starts the styleendCode
is the corresponding ANSI code that ends the style.An AnsiCode
can also be an end code, in which case code
and endCode
will be the same.
This representation is a 1:1 mapping of the original string, but not very useful for modifying the string. The styledCharsFromTokens
function converts a token array to an array of characters, where each character has an all currently active styles associated with it:
export interface StyledChar {
type: "char";
value: string;
fullWidth: boolean;
styles: AnsiCode[];
}
Using the above example:
import { tokenize, styledCharsFromTokens } from "@alcalzone/ansi-tokenize";
// red "foo", followed by unstyled "bar"
const str = "\x1B[31mfoo\x1B[39mbar";
const tokens = tokenize(str);
const styledChars = styledCharsFromTokens(tokens);
// styledChars will contain the following:
[
{
type: "char",
value: "f",
fullWidth: false,
styles: [
{
type: "ansi",
code: "\x1B[31m",
endCode: "\x1B[39m",
},
],
},
{
type: "char",
value: "o",
fullWidth: false,
styles: [
{
type: "ansi",
code: "\x1B[31m",
endCode: "\x1B[39m",
},
],
},
{
type: "char",
value: "o",
fullWidth: false,
styles: [
{
type: "ansi",
code: "\x1B[31m",
endCode: "\x1B[39m",
},
],
},
{
type: "char",
value: "b",
fullWidth: false,
styles: [],
},
{
type: "char",
value: "a",
fullWidth: false,
styles: [],
},
{
type: "char",
value: "r",
fullWidth: false,
styles: [],
},
];
For modification simply edit the items in the array as necessary, as long as the following rules are followed:
code
and endCode
properties must match. You can use the ansi-styles
module to do this.fullWidth
property must be correct. You can use the is-fullwidth-code-point
module to do this, or if working with multiple strings, turn those into styled char arrays first.E.g. to make the first o
blue and bold:
import ansiStyles from "ansi-styles";
// ... include the above code
styledChars[1].styles = [
{
type: "ansi",
code: ansiStyles.blue.open,
endCode: ansiStyles.blue.close,
},
{
type: "ansi",
code: ansiStyles.bold.open,
endCode: ansiStyles.bold.close,
},
];
The styledCharsToString
function converts a styled character array back to a string:
import { styledCharsToString } from "@alcalzone/ansi-tokenize";
// ... include the above code
const strOut = styledCharsToString(styledChars);
// str will now be '\x1B[31mf\x1B[34m\x1B[1mo\x1B[22m\x1B[31mo\x1B[39mbar'
This automatically figures out the least amount of escape codes necessary to achieve the desired result, as long as the styles
arrays contain no unnecessary styles, e.g. blue + red foreground.
14.13.1
Initial release
FAQs
Efficiently modify strings containing ANSI escape codes
The npm package @alcalzone/ansi-tokenize receives a total of 204,151 weekly downloads. As such, @alcalzone/ansi-tokenize popularity was classified as popular.
We found that @alcalzone/ansi-tokenize demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.