Skip to content

Commit

Permalink
tweak: make enabled true if term supports color out-of-the-box
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebucaran committed Jul 16, 2018
1 parent 3da1c1d commit d363f6e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 50 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ Turbocolor is a Node.js library for colorizing text using [ANSI escape sequences

## Features

- No dependencies
- [Toggle color support](#color-support) off as needed
- Use it as a drop-in replacement for [chalk](https://github.com/chalk/chalk), [ansi-colors](https://github.com/doowb/ansi-colors), [kleur](https://github.com/lukeed/kleur), etc
- Need for speed? Turbocolor is the [fastest](/bench) terminal colorizer for Node.js
- No dependencies!
- [Toggle color support](#color-support) on/off as needed.
- Use it as a drop-in replacement for [chalk](https://github.com/chalk/chalk), [ansi-colors](https://github.com/doowb/ansi-colors), [kleur](https://github.com/lukeed/kleur), etc.
- Need for speed? Turbocolor is the [fastest](/bench) terminal colorizer for Node.js.

## Installation

Expand All @@ -25,7 +25,7 @@ npm i <a href="https://www.npmjs.com/package/turbocolor">turbocolor</a>
const tc = require("turbocolor")
```

Using color.
Writing with color.

```jsx
console.log(tc.red("Bonjour!"))
Expand Down Expand Up @@ -77,17 +77,19 @@ Every style function can be chained or nested with one another and will return a

## Color Support

Turbocolor color support is enabled by default, but you can toggle it off as needed. For example, to disable color when the terminal does not support it use `supportsColor` like so.
Color support is automatically enabled if your terminal supports it, but you can toggle it on/off as needed.

```js
const tc = require("turbocolor")

tc.enabled = tc.supportsColor
tc.enabled = false

console.log(tc.red("No color!"))
```

## Escape Codes

Turbocolor exports ANSI escape codes which can be used for manually styling console output. Each style has an `open`, `close` and `strip` property. The `strip` property is a regular expression that matches all `close` substrings of that style and can be useful for creating nested expressions.
Turbocolor exports ANSI escape codes for each [available style](#styles). Each has an `open` and `close` property which can be used for manually styling console output.

```jsx
const { Styles } = require("turbocolor")
Expand Down
29 changes: 14 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,41 @@ const Styles = {

const turbocolor = {
Styles,
enabled: true,
supportsColor:
enabled:
process.env.FORCE_COLOR ||
process.platform === "win32" ||
(process.stdout.isTTY && process.env.TERM && process.env.TERM !== "dumb")
}

const color = function (out) {
const color = function(out) {
if (!turbocolor.enabled) return out

let i, style
const styles = this.styles
const length = styles.length
const names = this.names
const length = names.length

for (i = 0, out = out + ""; i < length; i++) {
style = Styles[styles[i]]
style = Styles[names[i]]
out = `${style.open}${out.replace(style.strip, style.open)}${style.close}`
}

return out
}

for (const style in Styles) {
defineProperty(turbocolor, style, {
for (const name in Styles) {
defineProperty(turbocolor, name, {
get() {
if (this.styles === undefined) {
const o = {}
const f = color.bind(o)
if (this.names === undefined) {
const chain = {}
const style = color.bind(chain)

f.__proto__ = turbocolor
f.styles = o.styles = [style]
style.__proto__ = turbocolor
style.names = chain.names = [name]

return f
return style
}

this.styles.push(style)
this.names.push(name)
return this
}
})
Expand Down
49 changes: 22 additions & 27 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
const color = require("..")
const tc = require("..")
const test = require("tape")

test("styles", t => {
Object.keys(color.Styles).map(k => {
const actual = color[k](k)
const expected = `${color.Styles[k].open}${k}${color.Styles[k].close}`
Object.keys(tc.Styles).map(k => {
const actual = tc[k](k)
const expected = `${tc.Styles[k].open}${k}${tc.Styles[k].close}`

t.is(actual, expected, actual)
})
t.end()
})

test("numbers", t => {
const actual = color.inverse.bgRed(1985)
const expected = `${color.Styles.bgRed.open}${color.Styles.inverse.open}1985${
color.Styles.inverse.close
}${color.Styles.bgRed.close}`
const actual = tc.inverse.bgRed(1985)
const expected = `${tc.Styles.bgRed.open}${tc.Styles.inverse.open}1985${
tc.Styles.inverse.close
}${tc.Styles.bgRed.close}`

t.is(actual, expected, actual)

t.end()
})

test("chains", t => {
const red = color.Styles.red
const bold = color.Styles.bold
const underline = color.Styles.underline
const italic = color.Styles.italic
const red = tc.Styles.red
const bold = tc.Styles.bold
const underline = tc.Styles.underline
const italic = tc.Styles.italic
const fixture = "Red, bold, underline and italic."
const actual = color.red.bold.underline.italic(fixture)
const actual = tc.red.bold.underline.italic(fixture)
const expected = `${italic.open}${underline.open}${bold.open}${
red.open
}${fixture}${red.close}${bold.close}${underline.close}${italic.close}`
Expand All @@ -38,13 +38,13 @@ test("chains", t => {
})

test("nested", t => {
const red = color.Styles.red
const blue = color.Styles.blue
const black = color.Styles.black
const bold = color.Styles.bold
const inverse = color.Styles.inverse
const actual = color.red(
`Red ${color.blue.bold("Bold Blue")} Red ${color.black.inverse(
const red = tc.Styles.red
const blue = tc.Styles.blue
const black = tc.Styles.black
const bold = tc.Styles.bold
const inverse = tc.Styles.inverse
const actual = tc.red(
`Red ${tc.blue.bold("Bold Blue")} Red ${tc.black.inverse(
"Inverse Black"
)} Red`
)
Expand All @@ -58,13 +58,8 @@ test("nested", t => {
t.end()
})

test("supports color", t => {
t.is(typeof color.supportsColor, "boolean")
t.end()
})

test("toggle", t => {
color.enabled = false
t.is(color.red.bold.inverse.dim.underline("Ok"), "Ok")
tc.enabled = false
t.is(tc.red.bold.inverse.dim.underline("Ok"), "Ok")
t.end()
})

0 comments on commit d363f6e

Please sign in to comment.