0.2.0 • Published 5 years ago

@fav/cli.text-style v0.2.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

@fav/cli.text-style NPM MIT License Build Status Build Status Coverage status

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:

ParameterTypeDescription
textstringA text to be decorated.
colorDepthnumberA color depth to be specified.
envobjectAn environment object to be used instead of process.env.

Returns:

TypeDescription
stringA decorated string.

Styles:

Modifiers
NameEffectANSI ESC code
resetReset all styles0
boldIncrese intensity1
faintDecrease intensity2
dimAlias of faint (follow chalk)2
italicItalic style3
underlineDraw underline4
slowBlinkSlow blink5
blinkAlias of slowBlink (follow chalk)5
reverseReverse foreground color and background color7
inverseAlias of reverse (follow chalk)7
concealHide text8
hiddenAlias of conceal (follow chalk)8
crossedOutDraw strikethrough9
strikethroughAlias of crossedOut (follow chalk)9
doublyUnderlineDraw doubly underline21
Foreground colors
NameEffectANSI ESC code
blackMake foreground black30
redMake foreground red31
greenMake foreground green32
yellowMake foreground yellow33
blueMake foreground blue34
magentaMake foreground magenta35
cyanMake foreground cyan36
whiteMake foreground white37
brightBlackMake foreground bright black90
blackBrightAlias of brightBlack (follow chalk)90
grayAlias of brightBlack (follow chalk)90
greyAlias of brightBlack (follow chalk)90
brightRedMake foreground bright red91
redBrightAlias of brightRed (follow chalk)91
brightGreenMake foreground bright green92
greenBrightAlias of brightGreen (follow chalk)92
brightYellowMake foreground bright yellow93
yellowBrightAlias of brightYellow (follow chalk)93
brightBlueMake foreground bright blue94
blueBrightAlias of brightBlue (follow chalk)94
brightMagentaMake foreground bright magenta95
magentaBrightAlias of brightBlue (follow chalk)95
brightCyanMake foreground bright cyan96
cyanBrightAlias of brightCyan (follow chalk)96
brightWhiteMake foreground bright white97
whiteBrightAlias 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
NameEffectANSI ESC code
bgBlackMake background black40
bgRedMake background red41
bgGreenMake background green42
bgYellowMake background yellow43
bgBlueMake background blue44
bgMagentaMake background magenta45
bgCyanMake background cyan46
bgWhiteMake background white47
bgBrightBlackMake background bright black100
bgBlackBrightAlias of bgBrightBlack (follow chalk)100
bgGrayAlias of bgBrightBlack (follow chalk)100
bgGreyAlias of bgBrightBlack (follow chalk)100
bgBrightRedMake background bright red101
bgRedBrightAlias of bgBrightRed (follow chalk)101
bgBrightGreenMake background bright green102
bgGreenBrightAlias of bgBrightGreen (follow chalk)102
bgBrightYellowMake background bright yellow103
bgYellowBrightAlias of bgBrightYellow (follow chalk)103
bgBrightBlueMake background bright blue104
bgBlueBrightAlias of bgBrightBlue (follow chalk)104
bgBrightMagentaMake background bright magenta105
bgMagentaBrightAlias of bgBrightBlue (follow chalk)105
bgBrightCyanMake background bright cyan106
bgCyanBrightAlias of bgBrightCyan (follow chalk)106
bgBrightWhiteMake background bright white107
bgWhiteBrightAlias 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)

Platform1112
macOS
Windows10
Linux

Node.js (4〜10)

Platform45678911
macOS
Windows10
Linux

io.js (1〜3)

Platform123
macOS
Windows10
Linux

Node.js (〜0.12)

Platform0.80.90.100.110.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.