0.15.0 ā€¢ Published 1 month ago

@opentf/cli-styles v0.15.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Ā OPEN TECH FOUNDATION

CLI Styles

Build

Style your CLI text using ANSI escape sequences.

Try it online at https://node-repl.pages.dev


šŸš€ @opentf/std - An Extensive JavaScript Standard Library. Please review and give feedback.


Features

  • 24-Bit colors only (True Color)

  • Nested styles

  • Auto detects CLI color support

  • Supports RGB & Hex color modes

  • Respects NO_COLOR & FORCE_COLOR

Supported Terminals (Popular)

āœ”ļø GNOME Terminal

āœ”ļø Konsole

āœ”ļø xfce4-terminal

āœ”ļø MATE Terminal

āœ”ļø iTerm2

āœ”ļø VS Code

āœ”ļø Tabby

āœ”ļø Hyper

āœ”ļø Alacritty

āœ”ļø Contour

āœ”ļø kitty

āœ”ļø WezTerm

āœ”ļø Windows Terminal

āŒ Terminal.app

*Missing your favorite terminal emulators here? Please give a PR or file an issue in Github.

Installation

npm install @opentf/cli-styles
yarn add @opentf/cli-styles
pnpm add @opentf/cli-styles
bun add @opentf/cli-styles

Syntax

style(str: string, options?: { color: boolean }): string;

Usage

import { style } from '@opentf/cli-styles';

const out = style('$key[.key...]{Text}');
console.log(out);

Examples

Using foreground color

style(
  'šŸŠ - An $o{orange} is a fruit of various citrus species in the family Rutaceae.'
);

npm.io

Using multiple colors

style(
  '$bgy.bl{The $r.bol{R}$g.bol{G}$b.bol{B} color model is an additive color model in which the $r.bol{red}, $g.bol{green} and $b.bol{blue} primary colors of light are added together in various ways to reproduce a broad array of colors.}'
);

npm.io

Nested colors

style(
  "$bgbl.b{THE QUICK $g{BROWN $r.bol{CAT} JUMPED} OVER THE LAZY $r.bol{DOG}'S BACK}"
);

npm.io

Composing different styles

style('$bol.w.bgg{ PASS }');

npm.io

Inverse colors

style('$inv.r.bgw.bol{ FAILED }');

npm.io

Normal vs Bold vs Dim text

style('Normal text | $bol{Bold text} | $dim{Dimmed text}');

npm.io

Italic fonts

style(
  '$ita.fuchsia.bol.bgw{"This poem is endless,\n the odds against us are endless,\n our chances of being alive together statistically nonexistent;\n still we have made it"}'
);

npm.io

Underlined texts

style('$und{Straight underline}');
style('$und{Straight $nou{(No underline here)}  underline}');
style('$dbu{Double underline}');
style('$cru{Curly underline}');
style('$dou{Dotted underline}');
style('$dau{Dashed underline}');
style('$und.ug{Straight underline colored}');
style('$dbu.uy{Double underline colored}');
style('$cru.ur{Curly underline colored}');
style('$dou.ub{Dotted underline colored}');
style('$dau.uo{Dashed underline colored}');
style('$und.urgb(100,200,255){Straight underline RGB colored}');
style('$und.ub{Straight $ruc{underline reset} colored}');

Underline Demo

*Output from VS Code.

Strikethrough text

style('Price: $str.r{\\$75.00} $g{\\$50.00}');

npm.io

Code Highlighting:

import { style } from '@opentf/cli-styles';
import hljs from 'highlight.js';
import { decode } from 'html-entities';

function highlight(code) {
  let html = hljs.highlight(code, {
    language: 'js',
  }).value;
  html = html.replaceAll('<span class="hljs-keyword">', '$fuchsia{');
  html = html.replaceAll('<span class="hljs-variable language_">', '$b{');
  html = html.replaceAll('<span class="hljs-title function_">', '$lime{');
  html = html.replaceAll('<span class="hljs-string">', '$y{');
  html = html.replaceAll('<span class="hljs-params"></span>', '');
  html = html.replaceAll('<span class="hljs-comment">', '$gr.dim{');
  html = html.replaceAll('</span>', '}');
  html = decode(html);
  return style(html);
}

const code = `
// Comment
function greet() {
  console.log('Hello World!');
}
`;

console.log(highlight(code));

npm.io

Using Template Literals:

const cpu = 90;
const ram = 40;
const disk = 70;

const getColor = (n) => (n <= 50 ? 'g' : n > 50 && n <= 70 ? 'y' : 'r');

style(`
 CPU: $${getColor(cpu)}{${cpu}%}
 RAM: $${getColor(ram)}{${ram}%}
DISK: $${getColor(disk)}{${disk}%}
`);

npm.io

Escape characters:

Use double back slashes to escape a character in the string.

style(
  `<$hex(#39CCCC){input} name=$y{"price"} value=$y{"\\$\\{ Cost + Tax \\}.00"} />`
);

npm.io

Blinking Text:

style('$g.bol{SALE! -} $blk.r.bol{50% OFFER}');

npm.io

Color Keys

KeyDescription
rRed - rgb(255,65,54)
gGreen - rgb(46,204,64)
bBlue - rgb(0,116,217)
oOrange - rgb(255,133,27)
yYellow - rgb(255,220,0)
wWhite - rgb(255,255,255)
mMagenta - rgb(255,105,193)
cCyan - rgb(154, 236, 254)
nNavy - rgb(0,31,63)
aAqua - rgb(127,219,255)
tTeal - rgb(57,204,204)
pPurple - rgb(177,13,201)
fFuchsia - rgb(240,18,190)
sSilver - rgb(221,221,221)
maMaroon - rgb(133,20,75)
olOlive - rgb(61,153,112)
liLime - rgb(1,255,112)
blBlack - rgb(17,17,17)
grGrey - rgb(170,170,170)
piPink - rgb(255, 191, 203)
rgb(red, green, blue)The RGB foreground colors, Eg: rgb(255,0,0) for red color.
urgb(red, green, blue)The RGB underlined colors, Eg: urgb(255, 255, 0) for yellow underlined color.
hex(#------)The 6-digit Hex colors, Eg: hex(#00ff00) for green color.
bg*The background colors can be applied with prefix bg to any color keys. Eg: bgw for white bgbgrgb(0,0,0) for black bgbghex(#0000FF) for blue bg
u*The underline colors.

Modifier Keys

KeyDescription
resReset all attributes to normal.
norNormal intensity Neither bold nor dim.
bolBold or increased intensity text.
dimDimmed or decreased intensity text.
itaItalic text.
invSwap foreground and background colors.
noiNot inversed.
strStrikethrough text.
nosNot Strikethrough text.
hidHidden text.
visVisible text.
undStraight underline text.
nouNo Underlined text.
dbuDoubly Underlined text.
cruCurly Underlined text.
douDotted Underlined text.
dauDashed Underlined text.
rucReset Underlined text color.
ovlOverlined text.
nooNot Overlined text.
blkBlinking text.
nobNo Blinking text.

Color Overrides

You can disable colors/styles by the following methods:

  • Pass color option false to the style function.

  • Pass --no-color or --color=false to the process arguments.

  • Set FORCE_COLOR=0 in enviroment variables.

You can force enable colors/styles by setting FORCE_COLOR=3 in enviroment variables.

Modifier Keys Supported Terminals

Supported Terminalsresnorboldimitainvstrhidovlblkunddbu
Gnome Terminalāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…
Konsoleāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āŒ
xfce4-terminalāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…
MATE Terminalāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…
iTerm2āœ…āœ…āœ…āœ…āœ…āœ…āœ…āŒāŒāŒāœ…āŒ
VS Codeāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āŒāœ…āœ…
Tabbyāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āŒāŒāœ…āœ…
Hyperāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āŒāŒāœ…āŒ
Alacrittyāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āŒāŒāœ…āŒ
Contourāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…
kittyāœ…āœ…āœ…āœ…āŒāœ…āœ…āŒāŒāŒāœ…āœ…
WezTermāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…
Windows Terminalāœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…āœ…

References

https://en.wikipedia.org/wiki/ANSI_escape_code

https://github.com/termstandard/colors

https://no-color.org/

https://nodejs.org/api/cli.html#force_color1-2-3

https://clrs.cc/

https://developer.chrome.com/docs/devtools/console/format-style/

Related

License

Copyright (c) 2022, Thanga Ganapathy (MIT License).

0.13.0

1 month ago

0.14.0

1 month ago

0.15.0

1 month ago

0.12.0

1 month ago

0.11.0

1 month ago

0.11.1

1 month ago

0.11.2

1 month ago

0.10.3

2 months ago

0.10.2

2 months ago

0.10.1

2 months ago

0.10.0

3 months ago

0.9.2

10 months ago

0.9.1

11 months ago

0.9.0

11 months ago

0.8.0

11 months ago

0.7.0

11 months ago

0.6.0

11 months ago

0.5.0

11 months ago