-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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
console: add color support #19372
console: add color support #19372
Changes from all commits
75cc568
565d8c3
ec7741f
313f29c
0e52d4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,23 +173,28 @@ function tryStringify(arg) { | |
} | ||
} | ||
|
||
function format(f) { | ||
const emptyOptions = {}; | ||
function format(...args) { | ||
return formatWithOptions(emptyOptions, ...args); | ||
} | ||
|
||
function formatWithOptions(inspectOptions, f) { | ||
let i, tempStr; | ||
if (typeof f !== 'string') { | ||
if (arguments.length === 0) return ''; | ||
if (arguments.length === 1) return ''; | ||
let res = ''; | ||
for (i = 0; i < arguments.length - 1; i++) { | ||
res += inspect(arguments[i]); | ||
for (i = 1; i < arguments.length - 1; i++) { | ||
res += inspect(arguments[i], inspectOptions); | ||
res += ' '; | ||
} | ||
res += inspect(arguments[i]); | ||
res += inspect(arguments[i], inspectOptions); | ||
return res; | ||
} | ||
|
||
if (arguments.length === 1) return f; | ||
if (arguments.length === 2) return f; | ||
|
||
let str = ''; | ||
let a = 1; | ||
let a = 2; | ||
let lastPos = 0; | ||
for (i = 0; i < f.length - 1; i++) { | ||
if (f.charCodeAt(i) === 37) { // '%' | ||
|
@@ -206,12 +211,17 @@ function format(f) { | |
tempStr = `${Number(arguments[a++])}`; | ||
break; | ||
case 79: // 'O' | ||
tempStr = inspect(arguments[a++]); | ||
tempStr = inspect(arguments[a++], inspectOptions); | ||
break; | ||
case 111: // 'o' | ||
tempStr = inspect(arguments[a++], | ||
{ showHidden: true, showProxy: true }); | ||
{ | ||
const opts = Object.assign({}, inspectOptions, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: how about const opts = Object.assign({
showHidden: true,
showProxy: true
}, inspectOptions); Feel free to ignore. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lpinca This is kind of intentional … we document that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In what case would it be overridden? It's still a new copy per call no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @lpinca I meant, if If you do think that that is the right thing to do, then I’m okay with that; I’d prefer to keep this as it is currently documented, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, ignore my comment. |
||
showHidden: true, | ||
showProxy: true | ||
}); | ||
tempStr = inspect(arguments[a++], opts); | ||
break; | ||
} | ||
case 105: // 'i' | ||
tempStr = `${parseInt(arguments[a++])}`; | ||
break; | ||
|
@@ -244,7 +254,7 @@ function format(f) { | |
if ((typeof x !== 'object' && typeof x !== 'symbol') || x === null) { | ||
str += ` ${x}`; | ||
} else { | ||
str += ` ${inspect(x)}`; | ||
str += ` ${inspect(x, inspectOptions)}`; | ||
} | ||
} | ||
return str; | ||
|
@@ -1206,6 +1216,7 @@ module.exports = exports = { | |
debuglog, | ||
deprecate, | ||
format, | ||
formatWithOptions, | ||
getSystemErrorName, | ||
inherits, | ||
inspect, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const util = require('util'); | ||
const { Writable } = require('stream'); | ||
const { Console } = require('console'); | ||
|
||
function check(isTTY, colorMode, expectedColorMode) { | ||
const items = [ | ||
1, | ||
{ a: 2 }, | ||
[ 'foo' ], | ||
{ '\\a': '\\bar' } | ||
]; | ||
|
||
let i = 0; | ||
const stream = new Writable({ | ||
write: common.mustCall((chunk, enc, cb) => { | ||
assert.strictEqual(chunk.trim(), | ||
util.inspect(items[i++], { | ||
colors: expectedColorMode | ||
})); | ||
cb(); | ||
}, items.length), | ||
decodeStrings: false | ||
}); | ||
stream.isTTY = isTTY; | ||
|
||
// Set ignoreErrors to `false` here so that we see assertion failures | ||
// from the `write()` call happen. | ||
const testConsole = new Console({ | ||
stdout: stream, | ||
ignoreErrors: false, | ||
colorMode | ||
}); | ||
for (const item of items) { | ||
testConsole.log(item); | ||
} | ||
} | ||
|
||
check(true, 'auto', true); | ||
check(false, 'auto', false); | ||
check(true, true, true); | ||
check(false, true, true); | ||
check(true, false, false); | ||
check(false, false, false); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
require('../common'); | ||
// Make this test OS-independent by overriding stdio getColorDepth(). | ||
process.stdout.getColorDepth = () => 8; | ||
process.stderr.getColorDepth = () => 8; | ||
|
||
console.log({ foo: 'bar' }); | ||
console.log('%s q', 'string'); | ||
console.log('%o with object format param', { foo: 'bar' }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ foo: *[32m'bar'*[39m } | ||
string q | ||
{ foo: *[32m'bar'*[39m } with object format param |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is worth to just mention the parameter types: