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 when not in TTY #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
32 changes: 26 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var name_maxlen = 0
, 'slow': 31
, 'green': 32
}
, isTTY = typeof process !== 'undefined' && process.stdout && process.stdout.isTTY

console.log('')

Expand All @@ -34,8 +35,14 @@ function add(bench) {
ops_arr.push(ops)
ops_top = Math.max.apply(Math, ops_arr);

exports.numCompleted++;

if (!isTTY) {
return
}

process.stdout.write(' '
+ color('pending', (++exports.numCompleted))
+ color('pending', exports.numCompleted)
+ ' test'
+ (exports.numCompleted > 1 ? 's' : '')
+ ' completed.\u000D')
Expand All @@ -58,6 +65,14 @@ function log(options) {
var reset = options.reset || true
, tolerances = options.tolerances || { pass: .95, mid: .80 }

if (!isTTY) {
process.stdout.write(' '
+ color('pending', exports.numCompleted)
+ ' test'
+ (exports.numCompleted > 1 ? 's' : '')
+ ' completed.')
}

console.log('\n')

for (var i = 0; i < exports.store.length; i++) {
Expand All @@ -66,7 +81,9 @@ function log(options) {

if (options.reset === undefined || options.reset === true) exports.reset()

console.log('')
if (isTTY) {
console.log('')
}
}

function reset() {
Expand Down Expand Up @@ -108,11 +125,10 @@ function logBench(bench, tolerances) {
: deviation > 2 ? 'medium'
: 'green', deviation)
+ color('pending', '% ')
+ '\u001b[' + colors['pass'] + 'm('
+ color('pass', '('
+ size
+ ' run' + (size == 1 ? '' : 's')
+ ' sampled)'
+ '\u001b[0m'
+ ' sampled)')
}
console.log(' ' + result)
}
Expand All @@ -124,7 +140,11 @@ function makeSpace(len) {
}

function color(type, str) {
return '\u001b[' + colors[type] + 'm' + str + '\u001b[0m'
if (isTTY) {
return '\u001b[' + colors[type] + 'm' + str + '\u001b[0m'
}

return str
}

function formatNumber(number) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Beautify Benchmark.js's output into readable form.",
"main": "index.js",
"scripts": {
"test": "node test.js"
"test": "node test.js && node testNonTTY.js > /dev/null"
},
"repository": {
"type": "git",
Expand Down
12 changes: 12 additions & 0 deletions testNonTTY.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var assert = require('assert')

assert.strictEqual(Boolean(process.stdout.isTTY), false, 'This test should be run with its output piped')

var _log = console.log

console.log = function() {
_log.apply(console, arguments)
assert.strictEqual(arguments[0].indexOf('\u001b[0m'), -1, 'There should be no colors when piping output')
}

require('./test.js')