Skip to content

sttk/fav-cli.text-style

Repository files navigation

Decorate terminal text with ANSI escape code.

"fav" is an abbreviation of "favorite" and also the acronym of "for all versions". This package is intended to support all Node.js versions as possible. At least, this package supports Node.js >= v0.10.

Install

To install from npm:

$ npm install --save @fav/cli.text-style

NOTE: npm < 2.7.0 does not support scoped package, but old version Node.js supports it. So when you use such older npm, you should download this package from github.com, and move it in node_modules/@fav/cli.text-style/ directory manually.

Usage

var textStyle = require('@fav/cli.text-style');

textStyle.underline('Hello!'); // => '\u001b[4mHello!\u001b[24m'
textStyle.red('Hello!'); // => '\u001b[31mHello!\u001b[39m'
textStyle.color(222)('Hello!'); // => '\u001b[38;222mHello!\u001b[39m' (256 colors)
textStyle.rgb(255,128,96)('Hello!'); // => '\u001b[38;2;255;128;96mHello!\u001b[39m' (16m colors)

API

textStyle.style(text) => string

Decorates text with the ANSI escape code corresponding to style. This automatically detects the color depth of the terminal and disables decorations which requires more color depth.

textStyle(colorDepth).style(text) => string

Decorates text with the ANSI escape code corresponding to style. This disables decorations which requires more color depth than the specified colorDepth.

textStyle(env).style(text) => string

Decorates text with the ANSI escape code corresponding to style. This determines the color depth with require('@fav/cli.get-color-depth')(env), and disables decorations which requires more color depth than the gotten colorDepth.

NOTE: On Windows, the combination of .bold and .faint/.dim makes disable to be off bold. Since .faint/.dim looks ineffective, this package adds no decoration for .faint/.dim on Windows.

Parameters:

Parameter Type Description
text string A text to be decorated.
colorDepth number A color depth to be specified.
env object An environment object to be used instead of process.env.

Returns:

Type Description
string A decorated string.

Styles:

Modifiers
Name Effect ANSI ESC code
reset Reset all styles 0
bold Increse intensity 1
faint Decrease intensity 2
dim Alias of faint (follow chalk) 2
italic Italic style 3
underline Draw underline 4
slowBlink Slow blink 5
blink Alias of slowBlink (follow chalk) 5
reverse Reverse foreground color and background color 7
inverse Alias of reverse (follow chalk) 7
conceal Hide text 8
hidden Alias of conceal (follow chalk) 8
crossedOut Draw strikethrough 9
strikethrough Alias of crossedOut (follow chalk) 9
doublyUnderline Draw doubly underline 21
Foreground colors
Name Effect ANSI ESC code
black Make foreground black 30
red Make foreground red 31
green Make foreground green 32
yellow Make foreground yellow 33
blue Make foreground blue 34
magenta Make foreground magenta 35
cyan Make foreground cyan 36
white Make foreground white 37
brightBlack Make foreground bright black 90
blackBright Alias of brightBlack (follow chalk) 90
gray Alias of brightBlack (follow chalk) 90
grey Alias of brightBlack (follow chalk) 90
brightRed Make foreground bright red 91
redBright Alias of brightRed (follow chalk) 91
brightGreen Make foreground bright green 92
greenBright Alias of brightGreen (follow chalk) 92
brightYellow Make foreground bright yellow 93
yellowBright Alias of brightYellow (follow chalk) 93
brightBlue Make foreground bright blue 94
blueBright Alias of brightBlue (follow chalk) 94
brightMagenta Make foreground bright magenta 95
magentaBright Alias of brightBlue (follow chalk) 95
brightCyan Make foreground bright cyan 96
cyanBright Alias of brightCyan (follow chalk) 96
brightWhite Make foreground bright white 97
whiteBright Alias of brightWhite (follow chalk) 97
color(n) Make foreground one of 256 colors (n = color code) 38;n
rgb(r,g,b) Make foregournd one of 16m colors (r,g,b = intensity of red, green, and blue) 38;2;r;g;b
Background colors
Name Effect ANSI ESC code
bgBlack Make background black 40
bgRed Make background red 41
bgGreen Make background green 42
bgYellow Make background yellow 43
bgBlue Make background blue 44
bgMagenta Make background magenta 45
bgCyan Make background cyan 46
bgWhite Make background white 47
bgBrightBlack Make background bright black 100
bgBlackBright Alias of bgBrightBlack (follow chalk) 100
bgGray Alias of bgBrightBlack (follow chalk) 100
bgGrey Alias of bgBrightBlack (follow chalk) 100
bgBrightRed Make background bright red 101
bgRedBright Alias of bgBrightRed (follow chalk) 101
bgBrightGreen Make background bright green 102
bgGreenBright Alias of bgBrightGreen (follow chalk) 102
bgBrightYellow Make background bright yellow 103
bgYellowBright Alias of bgBrightYellow (follow chalk) 103
bgBrightBlue Make background bright blue 104
bgBlueBright Alias of bgBrightBlue (follow chalk) 104
bgBrightMagenta Make background bright magenta 105
bgMagentaBright Alias of bgBrightBlue (follow chalk) 105
bgBrightCyan Make background bright cyan 106
bgCyanBright Alias of bgBrightCyan (follow chalk) 106
bgBrightWhite Make background bright white 107
bgWhiteBright Alias of bgBrightWhite (follow chalk) 107
bgColor(n) Make background one of 256 colors (n = color code) 48;n
bgRgb(r,g,b) Make background one of 16m colors (r,g,b = intensity of red, green, and blue) 48;2;r;g;b

References

Checked

Node.js (11〜12)

Platform 11 12
macOS
Windows10
Linux

Node.js (4〜10)

Platform 4 5 6 7 8 9 11
macOS
Windows10
Linux

io.js (1〜3)

Platform 1 2 3
macOS
Windows10
Linux

Node.js (〜0.12)

Platform 0.8 0.9 0.10 0.11 0.12
macOS
Windows10
Linux

License

Copyright (C) 2019 Takayuki Sato

This program is free software under MIT License. See the file LICENSE in this distribution for more details.

About

Decorate terminal text with ANSI escape code

Resources

License

Stars

Watchers

Forks

Packages

No packages published