Skip to content

Commit b01b5b9

Browse files
committed
Better terminal styles.
0 parents  commit b01b5b9

File tree

7 files changed

+315
-0
lines changed

7 files changed

+315
-0
lines changed

.jshintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"asi" : true,
3+
"node": true
4+
}

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: node_js
2+
node_js:
3+
- iojs
4+
- "0.12"
5+
env:
6+
- NODE_ENV=development
7+
script: "npm test"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015, Jorge Bucaran <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
[![Build Status][TravisLogo]][Travis]
2+
![](https://img.shields.io/badge/License-MIT-303030.svg?style=flat-square) ![](https://img.shields.io/badge/Color-Ful-0080ff.svg?style=flat-square)
3+
![](https://img.shields.io/badge/Clor-JS-f00033.svg?style=flat-square)
4+
[![NPM Downloads](http://img.shields.io/npm/dm/clor.svg?style=flat-square)](https://www.npmjs.org/package/clor)
5+
6+
7+
<a name="clor"></a>
8+
9+
<p align="center">
10+
11+
<b><a href="#synopsis">Synopsis</a></b>
12+
|
13+
<b><a href="#examples">Examples</a></b>
14+
|
15+
<b><a href="#install">Install</a></b>
16+
|
17+
<b><a href="#usage">Usage</a></b>
18+
|
19+
<b><a href="#styles">Styles</a></b>
20+
|
21+
<b><a href="#license">License</a></b>
22+
23+
</p>
24+
25+
<p align="center">
26+
<a href="https://github.com/bucaran/clor/blob/master/clor">
27+
<img width=44% src="https://cloud.githubusercontent.com/assets/8317250/7343629/9bfe2a46-ecfe-11e4-8878-fcb9bac8b9f9.png">
28+
</a>
29+
</p>
30+
31+
32+
# Synopsis
33+
34+
_Clor_ is a __30__ some LOC _original_ alternative to [colors.js](https://github.com/Marak/colors.js) and [Chalk](https://github.com/sindresorhus/chalk).
35+
36+
37+
<p align="center">
38+
<a href="https://github.com/bucaran/clor/blob/master/clor">
39+
<img src="https://cloud.githubusercontent.com/assets/8317250/7427256/5470b678-f012-11e4-9c46-dbfa0c958fe7.png">
40+
</a>
41+
</p>
42+
43+
## Compare
44+
45+
In [Chalk](https://github.com/sindresorhus/chalk) you concatenate strings to compose your output:
46+
47+
```js
48+
console.log(
49+
chalk.red.bold("fee") + "\n" + chalk.inverse("fi") + "\n" + chalk.underline("fo")
50+
)
51+
```
52+
53+
In _Clor_ you concatenate or use each style property as a function:
54+
55+
```js
56+
console.log(String(
57+
clor.red.bold("fee").line.inverse("fi").line.underline("fo")
58+
))
59+
```
60+
61+
You can get the composed string using `string`
62+
63+
```js
64+
console.log(
65+
clor.red.bold("fee").line.inverse("fi").line.underline("fo").string
66+
)
67+
```
68+
69+
Or provide your own logger function:
70+
71+
```js
72+
clor.red.bold("hi").log(function (args) {
73+
process.stdout.write("[" + this + "]")
74+
}/*, ... */)
75+
```
76+
77+
Where `args` is optional and `this` is bound to the string.
78+
79+
In just a few lines of code _Clor_ packs a lot of interesting functionality.
80+
81+
# Install
82+
83+
```sh
84+
npm install clor
85+
```
86+
87+
# Usage
88+
89+
```js
90+
var $ = require("clor")
91+
92+
$.red("hey")
93+
//→ \u001b[31mhey\u001b[0m
94+
95+
$.underline.bold.bgBlue.yellow("howdy!")
96+
//→ \u001b[4m\u001b[1m\u001b[44m\u001b[33mhowdy!\u001b[0m
97+
98+
// You can add new lines with `line` or \n
99+
$("1st line").line("2nd line").red.line("3rd line")
100+
//→ 1st line\u001b[0m\n2nd line\u001b[0m\u001b[31m\n3rd line\u001b[0m
101+
```
102+
103+
## Examples
104+
105+
```js
106+
console.log("npm install %s", $.blue("clor"))
107+
108+
// No need to cast to String explicitly when concatenating
109+
console.log(
110+
$.red("a red line") + $.line.blue("and a blue one")
111+
)
112+
113+
// Using Clor
114+
var $ = require("clor")
115+
console.log($
116+
.blue("A blue line\n")
117+
.green("I am a green line ")
118+
.blue.underline.bold("with a blue substring")
119+
.green(" that becomes green again!") + "Au revoir!"
120+
)
121+
122+
// Using Chalk
123+
var $ = require("chalk")
124+
console.log(
125+
$.blue("A blue line.\n") +
126+
$.green(
127+
"I am a green line " +
128+
$.blue.underline.bold("with a blue substring") +
129+
" that becomes green again!"
130+
) + "Hasta la vista!"
131+
)
132+
133+
// Using Colors.js
134+
console.log(
135+
"A blue line.\n".blue +
136+
"I am a green line ".green +
137+
"with a blue substring".blue.underline.bold +
138+
" that becomes green again!".green +
139+
" Saraba!"
140+
)
141+
```
142+
143+
> 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/).
144+
145+
146+
147+
# Styles
148+
149+
| Modifiers | Colors | Background Colors |
150+
|:---------------:|:---------:|:-----------------:|
151+
| `reset` | `black` | bgBlack |
152+
| `bold` | `red` | `bgRed` |
153+
| `dim` | `green` | `bgGreen` |
154+
| `italic` | `yellow` | `bgYellow` |
155+
| `underline` | `blue` | `bgBlue` |
156+
| `inverse` | `magenta` | `bgMagenta` |
157+
| `hidden` | `cyan` | `bgCyan` |
158+
| `strikethrough` | `white` | `bgWhite` |
159+
| `line` | `gray` | |
160+
| `_` or `space` | | |
161+
| `tab` | | |
162+
163+
## Custom Styles
164+
165+
Create custom styles easily:
166+
167+
```js
168+
let Style {
169+
title: $.bold.underline,
170+
quote: $.inverse,
171+
notes: $.bold.green.italic
172+
}
173+
174+
console.log("[" + Style.title(title) + "]")
175+
176+
```
177+
178+
# License
179+
180+
[MIT](http://opensource.org/licenses/MIT)
181+
182+
[TravisLogo]: http://img.shields.io/travis/bucaran/clor.svg?style=flat-square
183+
[Travis]: https://travis-ci.org/bucaran/clor

index.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*ಠ_ಠ*/"use strict"
2+
var ansi = require("ansi-styles")
3+
ansi.line = { open: "\n", close: "" }
4+
ansi.tab = { open: "\t", close: "" }
5+
6+
module.exports = (function clor (s) {
7+
function $ (str) {
8+
return (str === undefined) ?
9+
$.toString() : clor(s + str + ansi.reset.close)
10+
}
11+
12+
$.toString = function () { return s + ansi.reset.close }
13+
14+
$.log = function (logger) {
15+
var args = [].slice.call(arguments, 0)
16+
if (typeof logger === "function") {
17+
logger.apply(s, args.slice(1))
18+
} else {
19+
console.log.apply(console, [s].concat(args))
20+
}
21+
}
22+
23+
Object.defineProperty($, "string", {
24+
get: function () { return $.toString() }
25+
})
26+
27+
Object.keys(ansi).forEach(function (style) {
28+
Object.defineProperty($, style, {
29+
get: function () {
30+
return clor(s + ansi[style].open)
31+
}
32+
})
33+
})
34+
35+
return $
36+
}(""))

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "clor",
3+
"version": "1.0.0",
4+
"description": "Better terminal styles.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "npm run tap",
8+
"tap": "NODE_ENV=development tap test.js"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/bucaran/clor"
13+
},
14+
"keywords": [
15+
"color",
16+
"terminal",
17+
"cli"
18+
],
19+
"author": "Jorge Bucaran",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/bucaran/clor/issues"
23+
},
24+
"homepage": "https://github.com/bucaran/clor",
25+
"dependencies": {
26+
"ansi-styles": "^2.0.1"
27+
},
28+
"devDependencies": {
29+
"tap": "^0.7.1"
30+
}
31+
}

test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env node
2+
3+
// var test = require("tap").test
4+
var $ = require("./")
5+
6+
console.log("npm install %s", $.blue("clor"))
7+
8+
console.log(String($
9+
("Roses are ").red("red")(", violets are ")
10+
.blue("blue")(", I'm ")
11+
.dim("schizophrenic")(", and so am I")
12+
))
13+
14+
console.log($
15+
.red("red").blue("blue").green("green")()
16+
)
17+
18+
console.log(String($
19+
.red("some red").tab("interlude")
20+
.line.bgRed.yellow("a new line, yellow on red")
21+
.line.bgYellow.red("and red on a yellow line!")
22+
))
23+
24+
// No need to cast to String explicitly when concatenating
25+
console.log(
26+
$.red("a red line") +
27+
$.line.blue("and a blue one")
28+
)
29+
30+
// test("test", function (t) {
31+
// // Do some more meaningful tests here!!
32+
// t.end()
33+
// })

0 commit comments

Comments
 (0)