Skip to content

Commit

Permalink
Better terminal styles.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebucaran committed May 1, 2015
0 parents commit b01b5b9
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"asi" : true,
"node": true
}
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- iojs
- "0.12"
env:
- NODE_ENV=development
script: "npm test"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015, Jorge Bucaran <jbucaran@me.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
183 changes: 183 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
[![Build Status][TravisLogo]][Travis]
![](https://img.shields.io/badge/License-MIT-303030.svg?style=flat-square) ![](https://img.shields.io/badge/Color-Ful-0080ff.svg?style=flat-square)
![](https://img.shields.io/badge/Clor-JS-f00033.svg?style=flat-square)
[![NPM Downloads](http://img.shields.io/npm/dm/clor.svg?style=flat-square)](https://www.npmjs.org/package/clor)


<a name="clor"></a>

<p align="center">

<b><a href="#synopsis">Synopsis</a></b>
|
<b><a href="#examples">Examples</a></b>
|
<b><a href="#install">Install</a></b>
|
<b><a href="#usage">Usage</a></b>
|
<b><a href="#styles">Styles</a></b>
|
<b><a href="#license">License</a></b>

</p>

<p align="center">
<a href="https://github.com/bucaran/clor/blob/master/clor">
<img width=44% src="https://cloud.githubusercontent.com/assets/8317250/7343629/9bfe2a46-ecfe-11e4-8878-fcb9bac8b9f9.png">
</a>
</p>


# Synopsis

_Clor_ is a __30__ some LOC _original_ alternative to [colors.js](https://github.com/Marak/colors.js) and [Chalk](https://github.com/sindresorhus/chalk).


<p align="center">
<a href="https://github.com/bucaran/clor/blob/master/clor">
<img src="https://cloud.githubusercontent.com/assets/8317250/7427256/5470b678-f012-11e4-9c46-dbfa0c958fe7.png">
</a>
</p>

## Compare

In [Chalk](https://github.com/sindresorhus/chalk) you concatenate strings to compose your output:

```js
console.log(
chalk.red.bold("fee") + "\n" + chalk.inverse("fi") + "\n" + chalk.underline("fo")
)
```

In _Clor_ you concatenate or use each style property as a function:

```js
console.log(String(
clor.red.bold("fee").line.inverse("fi").line.underline("fo")
))
```

You can get the composed string using `string`

```js
console.log(
clor.red.bold("fee").line.inverse("fi").line.underline("fo").string
)
```

Or provide your own logger function:

```js
clor.red.bold("hi").log(function (args) {
process.stdout.write("[" + this + "]")
}/*, ... */)
```

Where `args` is optional and `this` is bound to the string.

In just a few lines of code _Clor_ packs a lot of interesting functionality.

# Install

```sh
npm install clor
```

# Usage

```js
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

```js
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_](https://github.com/marak/colors.js/) is probably the most readable, but it extends the [String Prototype](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/prototype) which is generally a [bad practice](http://perfectionkills.com/whats-wrong-with-extending-the-dom/).


# 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:

```js
let Style {
title: $.bold.underline,
quote: $.inverse,
notes: $.bold.green.italic
}

console.log("[" + Style.title(title) + "]")

```

# License

[MIT](http://opensource.org/licenses/MIT)

[TravisLogo]: http://img.shields.io/travis/bucaran/clor.svg?style=flat-square
[Travis]: https://travis-ci.org/bucaran/clor
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*ಠ_ಠ*/"use strict"
var ansi = require("ansi-styles")
ansi.line = { open: "\n", close: "" }
ansi.tab = { open: "\t", close: "" }

module.exports = (function clor (s) {
function $ (str) {
return (str === undefined) ?
$.toString() : clor(s + str + ansi.reset.close)
}

$.toString = function () { return s + ansi.reset.close }

$.log = function (logger) {
var args = [].slice.call(arguments, 0)
if (typeof logger === "function") {
logger.apply(s, args.slice(1))
} else {
console.log.apply(console, [s].concat(args))
}
}

Object.defineProperty($, "string", {
get: function () { return $.toString() }
})

Object.keys(ansi).forEach(function (style) {
Object.defineProperty($, style, {
get: function () {
return clor(s + ansi[style].open)
}
})
})

return $
}(""))
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "clor",
"version": "1.0.0",
"description": "Better terminal styles.",
"main": "index.js",
"scripts": {
"test": "npm run tap",
"tap": "NODE_ENV=development tap test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/bucaran/clor"
},
"keywords": [
"color",
"terminal",
"cli"
],
"author": "Jorge Bucaran",
"license": "MIT",
"bugs": {
"url": "https://github.com/bucaran/clor/issues"
},
"homepage": "https://github.com/bucaran/clor",
"dependencies": {
"ansi-styles": "^2.0.1"
},
"devDependencies": {
"tap": "^0.7.1"
}
}
33 changes: 33 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env node

// var test = require("tap").test
var $ = require("./")

console.log("npm install %s", $.blue("clor"))

console.log(String($
("Roses are ").red("red")(", violets are ")
.blue("blue")(", I'm ")
.dim("schizophrenic")(", and so am I")
))

console.log($
.red("red").blue("blue").green("green")()
)

console.log(String($
.red("some red").tab("interlude")
.line.bgRed.yellow("a new line, yellow on red")
.line.bgYellow.red("and red on a yellow line!")
))

// No need to cast to String explicitly when concatenating
console.log(
$.red("a red line") +
$.line.blue("and a blue one")
)

// test("test", function (t) {
// // Do some more meaningful tests here!!
// t.end()
// })

0 comments on commit b01b5b9

Please sign in to comment.