Skip to content

Commit

Permalink
Add --color and --no-color support (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgebucaran committed Oct 1, 2021
1 parent 0865cfc commit 6aead77
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as tty from "tty"

const env = process.env || {}
const argv = process.argv || []

const isDisabled = "NO_COLOR" in env
const isForced = "FORCE_COLOR" in env
const isDisabled = "NO_COLOR" in env || argv.includes("--no-color")
const isForced = "FORCE_COLOR" in env || argv.includes("--color")
const isWindows = process.platform === "win32"

const isCompatibleTerminal =
Expand Down
3 changes: 3 additions & 0 deletions tests/scripts/--color.NO_COLOR.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh

[ `NO_COLOR= node --eval 'import("./index.js").then(({ blue }) => console.log(blue("foobar")))' -- --color` = foobar ]
5 changes: 5 additions & 0 deletions tests/scripts/--color.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

[ `node --eval 'import("./index.js").then(({ blue }) => console.log(blue("foobar")))' -- --color` = `printf '\e[34mfoobar\e[39m'` ]


4 changes: 4 additions & 0 deletions tests/scripts/--no-color.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

[ `node --eval 'import("./index.js").then(({ blue }) => console.log(blue("foobar")))' -- --no-color` = 'foobar' ]

1 change: 1 addition & 0 deletions tests/scripts/NO_COLOR.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
console.log(createColors().blue("foobar")))
'` = "foobar" ]

9 changes: 9 additions & 0 deletions tests/variables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ export default [
t("`NO_COLOR` disables color", [
sh(`${SCRIPTS_PATH}/NO_COLOR.sh`, exitStatusSuccess),
]),
t("`--no-color` disables color", [
sh(`${SCRIPTS_PATH}/--no-color.sh`, exitStatusSuccess),
]),
t("`--color` enables color", [
sh(`${SCRIPTS_PATH}/--color.sh`, exitStatusSuccess),
]),
t("`NO_COLOR beats --color` enables color", [
sh(`${SCRIPTS_PATH}/--color.NO_COLOR.sh`, exitStatusSuccess),
]),
]),
]

0 comments on commit 6aead77

Please sign in to comment.