Synopsis | Examples | Install | Usage | Styles | License
Clor is a 30 some LOC original alternative to colors.js and Chalk.
In Chalk you concatenate strings to compose your output:
console.log(
chalk.red.bold("fee") + "\n" + chalk.inverse("fi") + "\n" + chalk.underline("fo")
)
In Clor you can concatenate and use any style property as a function
console.log(String(
clor.red.bold("fee").line.inverse("fi").line.underline("fo")
))
You can get the string and styles using string
console.log(
clor.red.bold("fee").line.inverse("fi").line.underline("fo").string
)
or use the log()
wrapper to console.log()
clor.red.bold("fee").line.inverse("fi").line.underline("fo").log(/* args */)
You can also provide your own logger function
clor.red.bold("hi").log(function (args) {
process.stdout.write("[" + timestamp() + "] " + this)
}/*, ... */)
Inside the logger wrapper
this
is bound to the string.
In just a few lines of code Clor packs a lot of interesting functionality.
npm install clor
var $ = require("clor")
$.red("hey")
//→ \u001b[31mhey\u001b[0m
$.underline.bold.bgBlue.yellow("howdy!")
//→ \u001b[4m\u001b[1m\u001b[44m\u001b[33mhowdy!\u001b[0m
// You can add new lines with `line` or \n
$("1st line").line("2nd line").red.line("3rd line")
//→ 1st line\u001b[0m\n2nd line\u001b[0m\u001b[31m\n3rd line\u001b[0m
console.log("npm install %s", $.blue("clor"))
// No need to cast to String explicitly when concatenating
console.log(
$.red("a red line") + $.line.blue("and a blue one")
)
// Using Clor
var $ = require("clor")
console.log($
.blue("A blue line\n")
.green("I am a green line ")
.blue.underline.bold("with a blue substring")
.green(" that becomes green again!") + "Au revoir!"
)
// Using Chalk
var $ = require("chalk")
console.log(
$.blue("A blue line.\n") +
$.green(
"I am a green line " +
$.blue.underline.bold("with a blue substring") +
" that becomes green again!"
) + "Hasta la vista!"
)
// Using Colors.js
console.log(
"A blue line.\n".blue +
"I am a green line ".green +
"with a blue substring".blue.underline.bold +
" that becomes green again!".green +
" Saraba!"
)
Notes: To be fair, colors is probably the most readable, but it extends the String Prototype which is generally a bad practice.
Modifiers | Colors | Background Colors |
---|---|---|
reset |
black |
bgBlack |
bold |
red |
bgRed |
dim |
green |
bgGreen |
italic |
yellow |
bgYellow |
underline |
blue |
bgBlue |
inverse |
magenta |
bgMagenta |
hidden |
cyan |
bgCyan |
strikethrough |
white |
bgWhite |
line |
gray |
|
_ or space |
||
tab |
Create custom styles easily:
const Style = {
head: $.bold.underline.green,
file: $.blue.bold("\"%s\""),
date: $.yellow("{{").gray(datefmt(new Date(), "HH:MM:ss")).yellow("}}")
}
console.log(Style.head("Starting app..."))
console.log("Loading file " + Style.file + " on " + Style.date, file)