Skip to content

Commit

Permalink
feat: gray out meta data object when using --singleLine=true (#161)
Browse files Browse the repository at this point in the history
* test: gray out meta data when using `--singleLine=true`

* fix: gray out meta data when using `--singleLine=true`

* WIP colorizer.greyMessage

* remove chalk from tests, add unit test for `colorize.greyMessage`

* pass through `colorizer` instance into `prettifyObject()`  call
  • Loading branch information
gr2m committed Mar 5, 2021
1 parent 2e68a53 commit 20e0c7d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ module.exports = function prettyFactory (options) {
errorLikeKeys: errorLikeObjectKeys,
eol: EOL,
ident: IDENT,
singleLine
singleLine,
colorizer
})
line += prettifiedObject
}
Expand Down
8 changes: 6 additions & 2 deletions lib/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const plain = {
30: nocolor,
20: nocolor,
10: nocolor,
message: nocolor
message: nocolor,
greyMessage: nocolor
}

const chalk = require('chalk')
Expand All @@ -24,7 +25,8 @@ const colored = {
30: ctx.green,
20: ctx.blue,
10: ctx.grey,
message: ctx.cyan
message: ctx.cyan,
greyMessage: ctx.grey
}

function colorizeLevel (level, colorizer) {
Expand All @@ -41,11 +43,13 @@ function plainColorizer (level) {
return colorizeLevel(level, plain)
}
plainColorizer.message = plain.message
plainColorizer.greyMessage = plain.greyMessage

function coloredColorizer (level) {
return colorizeLevel(level, colored)
}
coloredColorizer.message = colored.message
coloredColorizer.greyMessage = colored.greyMessage

/**
* Factory function get a function to colorized levels. The returned function
Expand Down
5 changes: 3 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ function prettifyObject ({
customPrettifiers = {},
errorLikeKeys = ERROR_LIKE_KEYS,
excludeLoggerKeys = true,
singleLine = false
singleLine = false,
colorizer = defaultColorizer
}) {
const keysToIgnore = [].concat(skipKeys)

Expand All @@ -334,7 +335,7 @@ function prettifyObject ({
if (singleLine) {
// Stringify the entire object as a single JSON line
if (Object.keys(plain).length > 0) {
result += stringifySafe(plain)
result += colorizer.greyMessage(stringifySafe(plain))
}
result += eol
} else {
Expand Down
6 changes: 6 additions & 0 deletions test/lib/colors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ test('returns default colorizer', async t => {

colorized = colorizer.message('foo')
t.is(colorized, 'foo')

colorized = colorizer.greyMessage('foo')
t.is(colorized, 'foo')
})

test('returns colorizing colorizer', async t => {
Expand Down Expand Up @@ -67,4 +70,7 @@ test('returns colorizing colorizer', async t => {

colorized = colorizer.message('foo')
t.is(colorized, '\u001B[36mfoo\u001B[39m')

colorized = colorizer.greyMessage('foo')
t.is(colorized, '\u001B[90mfoo\u001B[39m')
})

0 comments on commit 20e0c7d

Please sign in to comment.