Giter Club home page Giter Club logo

clor's Introduction

NPM Downloads

Synopsis | Examples | Install | Usage | Styles | License

Synopsis Build Status

Clor is a 30 some LOC original alternative to colors.js and Chalk.

Compare

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.

Install

npm install clor

Usage

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

Examples

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.

Styles

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

Custom Styles

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)

License

MIT © Jorge Bucaran

clor's People

Watchers

Mārtiņš avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.