Skip to content

Commit

Permalink
fix: disable if no TTY session (eg; command pipe) (#33)
Browse files Browse the repository at this point in the history
* Disable color output in pipes

* chore: set `isTTY` for CI

* fix: allow `FORCE_COLOR` override

* chore: run CI w/ `FORCE_COLOR` override

* fix: respond to "FORCE_COLOR=0" & GITHUB_ACTIONS

* fix: revert GITHUB_ACTIONS read

* chore: include bash tests for ENV detect

* sigh

* debug: print all

* fix(bash): remove faulty TTY assumption

* chore: addl combos; ignore trailing space

Co-authored-by: Luke Edwards <luke.edwards05@gmail.com>
  • Loading branch information
ai and lukeed authored Jun 24, 2020
1 parent 4f2e8aa commit 5c7353f
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jobs:
- name: Test w/ Coverage
run: c8 npm test
env:
FORCE_COLOR: 1

- name: Test w/ Environment
run: npm run build && ./test/env.sh

- name: Report
if: matrix.nodejs >= 14
Expand Down
4 changes: 3 additions & 1 deletion colors.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { FORCE_COLOR, NODE_DISABLE_COLORS, TERM } = process.env;

export const $ = {
enabled: !NODE_DISABLE_COLORS && TERM !== 'dumb' && FORCE_COLOR !== '0'
enabled: !NODE_DISABLE_COLORS && TERM !== 'dumb' && (
FORCE_COLOR != null && FORCE_COLOR !== '0' || process.stdout.isTTY
)
}

function init(x, y) {
Expand Down
4 changes: 3 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const { FORCE_COLOR, NODE_DISABLE_COLORS, TERM } = process.env;

const $ = {
enabled: !NODE_DISABLE_COLORS && TERM !== 'dumb' && FORCE_COLOR !== '0',
enabled: !NODE_DISABLE_COLORS && TERM !== 'dumb' && (
FORCE_COLOR != null && FORCE_COLOR !== '0' || process.stdout.isTTY
),

// modifiers
reset: init(0, 0),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"scripts": {
"build": "node build",
"test": "uvu -r esm -i utils"
"test": "uvu -r esm -i utils -i xyz"
},
"engines": {
"node": ">=6"
Expand Down
47 changes: 47 additions & 0 deletions test/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

code=0
fail() {
printf "\x1b[31m[FAIL]\x1b[39m %s\n" "$@" >&2
code=1
}

colors() {
printf "\x1b[33m[DEBUG]\x1b[39m \"%s\" :: %s \n" "$2" "$1"
[ "$1" != "foo" ] || fail "$2"
}

nocolor() {
printf "\x1b[33m[DEBUG]\x1b[39m \"%s\" :: %s \n" "$2" "$1"
[[ "$1" =~ foo* ]] || fail "$2"
}

faketty() {
script -qfc "$(printf "%q " "$@")" /dev/null
}

# process.stdout.isTTY = undefined;
printf "\nprocess.stdout.isTTY = %s;\n" `node -p "process.stdout.isTTY"`
nocolor `node -p "require('.').red('foo')"` "FORCE_COLOR=?"
nocolor `FORCE_COLOR=0 node -p "require('.').red('foo')"` "FORCE_COLOR=0"
nocolor `NODE_DISABLE_COLORS=1 node -p "require('.').red('foo')"` "NODE_DISABLE_COLORS=1"
nocolor `NODE_DISABLE_COLORS=1 FORCE_COLOR=1 node -p "require('.').red('foo')"` "NODE_DISABLE_COLORS=1; FORCE_COLOR=1"
colors `FORCE_COLOR=1 node -p "require('.').red('foo')"` "FORCE_COLOR=1"
nocolor `TERM=dumb FORCE_COLOR=1 node -p "require('.').red('foo')"` "TERM=dumb; FORCE_COLOR=1"
nocolor `TERM=dumb node -p "require('.').red('foo')"` "TERM=dumb"

# process.stdout.isTTY = true;
printf "\n(faketty) process.stdout.isTTY = %s;\n" `faketty node -p "process.stdout.isTTY"`
colors `faketty node -p "require('.').red('foo')"` "FORCE_COLOR=?"
colors `FORCE_COLOR=0 faketty node -p "require('.').red('foo')"` "FORCE_COLOR=0"
nocolor `NODE_DISABLE_COLORS=1 faketty node -p "require('.').red('foo')"` "NODE_DISABLE_COLORS=1"
nocolor `NODE_DISABLE_COLORS=1 FORCE_COLOR=1 faketty node -p "require('.').red('foo')"` "NODE_DISABLE_COLORS=1; FORCE_COLOR=1"
nocolor `TERM=dumb FORCE_COLOR=1 faketty node -p "require('.').red('foo')"` "TERM=dumb; FORCE_COLOR=1"
colors `FORCE_COLOR=1 faketty node -p "require('.').red('foo')"` "FORCE_COLOR=1"
nocolor `TERM=dumb node -r esm test/xyz.js` "TERM=dumb"

if [ "$code" == "0" ]; then
printf "\x1b[32m[PASS]\x1b[39m $.enabled updates correctly \n"
fi

exit $code
2 changes: 2 additions & 0 deletions test/xyz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import kleur from '../index.mjs';
console.log(kleur.red('foo'));

0 comments on commit 5c7353f

Please sign in to comment.