Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable color output in pipes #33

Merged
merged 11 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));