1.0.2 • Published 5 years ago

short-ansi v1.0.2

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

short-ansi

This package allows you to easily mix and match ANSI terminal color/formatting to make it easier to style your output logs.

To use short-ansi, start by creating a session:

let a = require('short-ansi')();

From there you can immediately generate formatting codes by accessing properties of your short-ansi session:

console.log(a.ut + "This text is underlined and bold" + a.e + " and this is not");

The name of the property you access gets parsed by the session (a JavaScript proxy) to create the formatting codes dynamically, and each character of that name represents a command or parameter.

There is also another, nicer way to use short-ansi, using JavaScript template strings. A short-ansi session, along with being a proxy, is also a function that can parse template strings and format them automatically:

console.log(a`\{ut This text is underlined and bold\} and this is not`);

Notice how our session a is prepended to the template string. This tells JavaScript to use our session as a parser for the template string.

When short-ansi parses your template strings, it looks for matching escaped brackets and applies styles within them. Normally, escaped brackets would just be converted into normal brackets, but short-ansi actually parses the raw text inside the string. The first non-space characters inside a set of brackets determine the formatting to apply to the text inside the brackets (ut in this example), which is then followed by a space and the text you want to format. Then after the closing bracket of the set, the formatting of the string is returned to its previous state. This means that you can actually nest formatting inside template strings:

console.log(a`\{u This text is underlined \{t and this is also bold\} and this is just underlined again\}`);
console.log(a`\{rt This is red and bold \{e this is normal\} and this is red and bold again\}`);

Because we are using template strings, we can still use string interpolation too:

let someText = "This will be printed in red";
console.log(a`Value of 'someText': \{r ${someText}\}`);

short-ansi has one last feature: presets. You can define and use a preset just like so:

a.p1 = a.utr;
console.log(a`\{p1 This is underlined, bold, and red\}`);

Presets are numbered and prefixed with a p, and are unique to each session.

Format

If you have any confusion about what the color commands do, Wikipedia has some good documentation.

CommandTypeDescriptionParameters
,no-opused for readability(none)
eresetreset all formatting (end)(none)
ustyleunderline(none)
tstylebold (thick)(none)
sstylefaint (slim)(none)
istyleitalic(none)
f[n]stylefontf: font number
dbasic colorblack (dark)(none)
rbasic colorred(none)
gbasic colorgreen(none)
ybasic coloryellow(none)
bbasic colorblue(none)
mbasic colormagenta(none)
cbasic colorcyan(none)
wbasic colorwhite(none)
q[n]color8-bit color cube (qube)n: color cube number
a[r]_[g]_[b]color24-bit rgb (all colors)r: red, g: green, b: blue
l[c]color modifierbrighten (lighten)c: basic color
h[c]color modifierbackground (highlight)c: any color or brightened color
n[c]modifiernotc: any of utsidrgybmcwqah
p[n]presetpresetn: preset number

Note: the "not" (n) command takes a single character without any extra parameters. For example, nq30 is not valid because q is used as a raw parameter instead of as a command, and 30 isn't a known command. Conversely, q30 is valid, because 30 is used as q's parameter. Also, when paired with a style, the "not" command will reset that style specifically, but when paired with h or any color command, it will reset the background color or foreground color, respectively.