This module provides utilities for styling and manipulating strings in TypeScript.
Members
# static constant AnsiReset
This constant holds the ANSI escape sequence used to reset all text formatting to default.
ANSI escape code for resetting text formatting.
# static constant StandardForegroundColors
This object maps color names to their corresponding ANSI color codes for standard foreground colors.
Standard foreground color codes for ANSI text formatting.
Properties:
Name | Type | Description |
---|---|---|
black |
number
|
ANSI code for black text (30). |
red |
number
|
ANSI code for red text (31). |
green |
number
|
ANSI code for green text (32). |
yellow |
number
|
ANSI code for yellow text (33). |
blue |
number
|
ANSI code for blue text (34). |
magenta |
number
|
ANSI code for magenta text (35). |
cyan |
number
|
ANSI code for cyan text (36). |
white |
number
|
ANSI code for white text (37). |
Methods
# static applyStyle(text, n) → {string}
This function takes a string and a style code (either a number or a key from the styles object) and returns the text wrapped in the appropriate ANSI escape codes for that style.
Applies an ANSI style code to text.
Parameters:
Name | Type | Description |
---|---|---|
text |
string
|
The text to be styled. |
n |
number
|
string
|
The style code or style name. |
The text wrapped in ANSI style codes.
string
# static clear(text) → {string}
This function takes a string that may contain ANSI escape codes for formatting and returns a new string with all such codes removed, leaving only the plain text content. It uses a regular expression to match and remove ANSI escape sequences.
Removes all ANSI formatting codes from text.
Parameters:
Name | Type | Description |
---|---|---|
text |
string
|
The text potentially containing ANSI formatting codes. |
The input text with all ANSI formatting codes removed.
string
# static colorizeRGB(text, r, g, b, bgopt) → {string}
This function takes a string and RGB color values (0-255 for each component) and returns the text wrapped in ANSI escape codes for either foreground or background coloring.
Applies an RGB color ANSI code to text.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
text |
string
|
The text to be colored. |
||
r |
number
|
The red component of the color (0-255). |
||
g |
number
|
The green component of the color (0-255). |
||
b |
number
|
The blue component of the color (0-255). |
||
bg |
boolean
|
<optional> |
false | If true, applies the color to the background instead of the foreground. |
The text wrapped in ANSI color codes.
string
# static raw(text, raw) → {string}
This function takes a string and a raw ANSI escape code, and returns the text wrapped in the provided raw ANSI code and the reset code. This allows for applying custom or complex ANSI formatting that may not be covered by other utility functions.
Applies raw ANSI escape codes to text.
Parameters:
Name | Type | Description |
---|---|---|
text |
string
|
The text to be formatted. |
raw |
string
|
The raw ANSI escape code to be applied. |
The text wrapped in the raw ANSI code and the reset code.
string
Type Definitions
object
# ColorizeOptions
This type defines the structure of the object returned by the colorize function. It includes methods for applying various color and style options to text using ANSI escape codes.
Options for text colorization using ANSI codes.
Properties:
Name | Type | Description |
---|---|---|
StandardForegroundColors |
StyledString
|
Getter for each standard foreground color. |
BrightForegroundColors |
StyledString
|
Getter for each bright foreground color. |
StandardBackgroundColors |
StyledString
|
Getter for each standard background color. |
BrightBackgroundColors |
StyledString
|
Getter for each bright background color. |
styles |
StyledString
|
Getter for each text style. |
clear |
function
|
Removes all styling from the text. |
raw |
function
|
Applies raw ANSI codes to the text. |
foreground |
function
|
Applies a foreground color using ANSI codes. |
background |
function
|
Applies a background color using ANSI codes. |
style |
function
|
Applies a text style using ANSI codes. |
color256 |
function
|
Applies a 256-color foreground color. |
bgColor256 |
function
|
Applies a 256-color background color. |
rgb |
function
|
Applies an RGB foreground color. |
bgRgb |
function
|
Applies an RGB background color. |
text |
string
|
The underlying text content. |
clear |
function
|
|
raw |
function
|
|
foreground |
function
|
|
background |
function
|
|
style |
function
|
|
color256 |
function
|
|
bgColor256 |
function
|
|
rgb |
function
|
|
bgRgb |
function
|
|
text |
string
|