ansi_escape_codes 1.4.0 copy "ansi_escape_codes: ^1.4.0" to clipboard
ansi_escape_codes: ^1.4.0 copied to clipboard

Yet another package for ANSI escape sequences. It differs from the others only in that it focuses on using constants rather than functions or methods.

ansi_escape_codes #

This is yet another package of many for ANSI escape codes. It differs from the others only in that it focuses on using constants rather than functions or methods.

Since version 1.4.0, analysis has also been added.

Usage #

const text =
    // 4-bit colors.
    '${ansi.fgBrightGreen}Lorem'
    ' ${ansi.fgGreen}ipsum'
    ' ${ansi.bgBrightBlack}${ansi.fgWhite}dolor${ansi.bgDefault}'
    ' ${ansi.bgBrightWhite}${ansi.fgBlack}sit${ansi.bgDefault}'
    // 8-bit colors.
    ' ${ansi.fg256HighRed}amet,'
    ' ${ansi.fg256Red}consectetur${ansi.fgDefault}'
    // 24-bit colors.
    ' ${ansi.bgRgbOpen}249;105;14${ansi.bgRgbClose}'
    '${ansi.fgRgbOpen}64;48;32${ansi.fgRgbClose}'
    'adipiscing'
    // Inverted colors.
    ' ${ansi.invert} elit,${ansi.notInverted}'
    '${ansi.fgDefault}${ansi.bgDefault}'
    // Italic.
    ' ${ansi.italic}sed${ansi.notItalic}'
    ' do'
    // Bold and faint.
    ' ${ansi.bold}eiusmod${ansi.notBoldNotFaint}'
    ' ${ansi.faint}tempor${ansi.notBoldNotFaint}'
    '${ansi.fgCyan}'
    ' incididunt'
    ' ${ansi.increasedIntensity}ut${ansi.normalIntensity}'
    ' ${ansi.decreasedIntensity}labore${ansi.normalIntensity}'
    '${ansi.fgDefault}'
    // Etc.
    ' ${ansi.underline}et${ansi.notUnderlined}'
    ' ${ansi.strike}dolore${ansi.notStriked}'
    ' ${ansi.hide}magna${ansi.notHidden}'
    ' ${ansi.blink}aliqua${ansi.notBlinking}.';

print(text);

Colors #

4-bit colors

https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit

Foreground Background
fgBlack bgBlack
fgRed bgRed
fgGreen bgGreen
fgYellow bgYellow
fgBlue bgBlue
fgMagenta bgMagenta
fgCyan bgCyan
fgWhite bgWhite
fgBrightBlack bgBrightBlack
fgBrightRed bgBrightRed
fgBrightGreen bgBrightGreen
fgBrightYellow bgBrightYellow
fgBrightBlue bgBrightBlue
fgBrightMagenta bgBrightMagenta
fgBrightCyan bgBrightCyan
fgBrightWhite bgBrightWhite

Example:

print('$fgBrightYellow$bgGreen Yellow text on green field $reset');

8-bit colors (256-color table)

https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit

Templates for using a color from a 256-color table:

  • Foreground:

    '${fg256Open}${colorIndex}${fg256Close}'
    
  • Background:

    '${bg256Open}${colorIndex}${bg256Close}'
    
  • Underline:

    '${underline256Open}${colorIndex}${underline256Close}'
    

Where colorIndex is the color index from the 256-color table. You can use predefined values from the following table:

Index Name Comment
0 black
1 red
2 green
3 yellow
4 blue
5 magenta
6 cyan
7 white
8 highBlack
9 highRed
10 highGreen
11 highYellow
12 highBlue
13 highMagenta
14 highCyan
15 highWhite
16-231 rgbNNN N are numbers from 0 to 5 (6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b)
232-255 grayN N is a number from 0 to 23 (grayscale from dark to light in 24 steps)

Example:

print('$fg256Open$highYellow$fg256Close$bg256Open$green$bg256Close Yellow text on green field $fgDefault');

You don't have to use templates and take the predefined values from the following table:

Index Name Comment
0 (fg/bg/underline)256Black
1 (fg/bg/underline)256Red
2 (fg/bg/underline)256Green
3 (fg/bg/underline)256Yellow
4 (fg/bg/underline)256Blue
5 (fg/bg/underline)256Magenta
6 (fg/bg/underline)256Cyan
7 (fg/bg/underline)256White
8 (fg/bg/underline)256HighBlack
9 (fg/bg/underline)256HighRed
10 (fg/bg/underline)256HighGreen
11 (fg/bg/underline)256HighYellow
12 (fg/bg/underline)256HighBlue
13 (fg/bg/underline)256HighMagenta
14 (fg/bg/underline)256HighCyan
15 (fg/bg/underline)256HighWhite
16..231 (fg/bg/underline)256RgbNNN N are numbers from 0 to 5 (6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b)
232..255 (fg/bg/underline)256GrayN N is a number from 0 to 23 (grayscale from dark to light in 24 steps)

Example:

print('$fg256HighYellow$bg256Green Yellow text on green field $reset');
print('$fg256Rgb550$bg256Rgb240 Yellow text on green field $reset');

You can use the following functions:

  • to get color indexes:

    int rgb(int r, int g, int b);
    int gray(int level);
    

    Where r, g, b are values from 0 to 5. And where level is a value from 0 to 23.

    Example:

    print('$fg256Open${rgb(5, 5, 0)}$fg256Close$bg256Open${rgb(2, 4, 0)}$bg256Close Yellow text on green field $reset');
    print('$fg256Open${gray(16)}$fg256Close$bg256Open${gray(8)}$bg256Close Gray text on gray field $reset');
    
  • to set the color:

    String fg256(int colorIndex);
    String bg256(int colorIndex);
    String underline256(int colorIndex);
    

    Example:

    print('${fg256(highYellow)}${bg256(green)} Yellow text on green field $reset');
    

24-bit colors (RGB)

https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit

Templates for using a RGB color:

  • Foreground:

    '${fgRgbOpen}${r};${g};${b}${fgRgbClose}'
    
  • Background:

    '${bgRgbOpen}${r};${g};${b}${bgRgbClose}`'
    
  • Underline:

    '${underlineRgbOpen}${r};${g};${b}${underlineRgbClose}'
    

Where r, g, b are values from 0 to 255.

You can use the following functions to dynamically generate a value:

String fgRgb(int r, int g, int b);
String bgRgb(int r, int g, int b);
String underlineRgb(int r, int g, int b);

Example:

print('${fgRgb(255, 255, 0)}${bgRgb(128, 192, 0)} Yellow text on green field $reset');

Reset color

You can reset the set color with fgDefault, bgDefault and underlineColorDefault. Or with reset, which will turn off all attributes.

Control sequences #

Default constant Template Function Description
cursorUp ${cursorUpOpen}[${n}]${cursorUpClose} cursorUpN(n) Moves the cursor up n (default 1) lines.
cursorDown ${cursorDownOpen}[${n}]${cursorDownClose} cursorDownN(n) Moves the cursor down n (default 1) lines.
cursorForward ${cursorForwardOpen}[${n}]${cursorForwardClose} cursorForwardN(n) Moves the cursor forward n (default 1) cells.
cursorBack ${cursorBackOpen}[${n}]${cursorBackClose} cursorBackN(n) Moves the cursor back n (default 1) cells.
cursorNextLine ${cursorNextLineOpen}[${n}]${cursorNextLineClose} cursorNextLineN(n) Moves cursor to beginning of the line n (default 1) lines down.
cursorPrevLine ${cursorPrevLineOpen}[${n}]${cursorPrevLineClose} cursorPrevLineN(n) Moves cursor to beginning of the line n (default 1) lines up.
cursorHPos ${cursorHPosOpen}[${n}]${cursorHPosClose} cursorHPosN(n) Moves the cursor to column n (default 1).
cursorPos ${cursorPosOpen}[${row};${col}]${cursorPosClose} cursorPosTo(row, col) Moves the cursor to row and col. The values are 1-based, and default to 1 (top left corner) if omitted.
cursorHVPos ${cursorHVPosOpen}[${row};${col}]${cursorHVPosClose} cursorHVPosTo(row, col) Same as cursorPos, but counts as a format effector function (like cr or lf) rather than an editor function (like cursorUp or cursorNextLine). This can lead to different handling in certain terminal modes.
clearScreen… ${clearScreenOpen}[${n}]${clearScreenClose} Clears part of the screen. If n is 0 (or missing), clear from cursor to end of screen (clearScreenToEnd). If n is 1, clear from cursor to beginning of the screen (clearScreenToBegin). If n is 2, clear entire screen and moves cursor to upper left (clearScreen). If n is 3, clear entire screen and delete all lines saved in the scrollback buffer (clearScreenWithBuf).
eraseLine… ${eraseLineOpen}[${n}]${eraseLineClose} Erases part of the line. If n is 0 (or missing), clear from cursor to the end of the line (eraseLineToEnd). If n is 1, clear from cursor to beginning of the line (eraseLineToBegin). If n is 2, clear entire line (eraseLine). Cursor position does not change.
scrollUp ${scrollUpOpen}[${n}]${scrollUpClose} scrollUpN(n) Scroll whole page up by n (default 1) lines. New lines are added at the bottom.
scrollDown ${scrollDownOpen}[${n}]${scrollDownClose} scrollDownN(n) Scroll whole page down by n (default 1) lines. New lines are added at the top.
hideCursor Shows the cursor.
showCursor Hides the cursor.
saveCursor Saves the cursor position, encoding shift state and formatting attributes.
restoreCursor Restores the cursor position, encoding shift state and formatting attributes from the previous saveCursor if any, otherwise resets these all to their defaults.

Analysis #

You can find out if a string contains escape sequences by using the has method hasEscapeSequences and others.

With the removeEscapeSequences method and others, you can remove all escape sequences from a string, converting it to plain text.

Find all escape sequences in a string using the allEscapeSequences method and others.

Finally, you can convert the escape sequence into a readable form using the showEscapeSequences method:

const text =
    '${ansi.fgBrightGreen}Lorem ${ansi.fgGreen}ipsum...';
print(ansi.showEscapeSequences(text));
[CSI 92 SGR]Lorem [CSI 32 SGR]ipsum...

Escape sequences can also be recognized:

print(ansi.showEscapeSequences(text, recognizeSequences: true));
[fgBrightGreen]Lorem [fgGreen]ipsum...
0
likes
0
points
41
downloads

Publisher

verified publisheryet-another.dev

Weekly Downloads

Yet another package for ANSI escape sequences. It differs from the others only in that it focuses on using constants rather than functions or methods.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta

More

Packages that depend on ansi_escape_codes