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

Add magenta as the color for printed object properties. Fixes suport for --no-colorizeObjects #553

Merged
merged 2 commits into from
Jan 14, 2025
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
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ node app.js | pino-pretty
### CLI Arguments

- `--colorize` (`-c`): Adds terminal color escape sequences to the output.
- `--colorizeObjects` (`-C`): Allows suppressing colorization of objects when set to `false`. In combination with `--singleLine`, this ensures that the end of each line is parsable JSON.
- `--no-colorizeObjects`: Suppress colorization of objects.
- `--crlf` (`-f`): Appends carriage return and line feed, instead of just a line
feed, to the formatted log line.
- `--errorProps` (`-e`): When formatting an error object, display this list
Expand Down
1 change: 0 additions & 1 deletion bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ if (cmd.h || cmd.help) {
let opts = minimist(process.argv, {
alias: {
colorize: 'c',
colorizeObjects: 'C',
crlf: 'f',
errorProps: 'e',
levelFirst: 'l',
Expand Down
10 changes: 7 additions & 3 deletions lib/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ const plain = {
20: nocolor,
10: nocolor,
message: nocolor,
greyMessage: nocolor
greyMessage: nocolor,
property: nocolor
}

const { createColors } = require('colorette')
const getLevelLabelData = require('./utils/get-level-label-data')
const availableColors = createColors({ useColor: true })
const { white, bgRed, red, yellow, green, blue, gray, cyan } = availableColors
const { white, bgRed, red, yellow, green, blue, gray, cyan, magenta } = availableColors

const colored = {
default: white,
Expand All @@ -27,7 +28,8 @@ const colored = {
20: blue,
10: gray,
message: cyan,
greyMessage: gray
greyMessage: gray,
property: magenta
}

function resolveCustomColoredColorizer (customColors) {
Expand Down Expand Up @@ -56,6 +58,7 @@ function plainColorizer (useOnlyCustomProps) {
}
customColoredColorizer.message = plain.message
customColoredColorizer.greyMessage = plain.greyMessage
customColoredColorizer.property = plain.property
customColoredColorizer.colors = createColors({ useColor: false })
return customColoredColorizer
}
Expand All @@ -66,6 +69,7 @@ function coloredColorizer (useOnlyCustomProps) {
return newColoredColorizer(level, colored, opts)
}
customColoredColorizer.message = colored.message
customColoredColorizer.property = colored.property
customColoredColorizer.greyMessage = colored.greyMessage
customColoredColorizer.colors = availableColors
return customColoredColorizer
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/prettify-error-log.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const tap = require('tap')
const prettifyErrorLog = require('./prettify-error-log')
const colors = require('../colors')
const {
ERROR_LIKE_KEYS,
MESSAGE_KEY
Expand All @@ -13,7 +14,8 @@ const context = {
customPrettifiers: {},
errorLikeObjectKeys: ERROR_LIKE_KEYS,
errorProps: [],
messageKey: MESSAGE_KEY
messageKey: MESSAGE_KEY,
objectColorizer: colors()
}

tap.test('returns string with default settings', async t => {
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/prettify-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function prettifyObject ({
lines = lines.replace(/\\\\/gi, '\\')

const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol })
result += `${ident}${keyName}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}`
result += `${ident}${objectColorizer.property(keyName)}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}`
})
}

Expand Down
16 changes: 14 additions & 2 deletions lib/utils/prettify-object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const prettifyObject = require('./prettify-object')
const {
ERROR_LIKE_KEYS
} = require('../constants')
const getColorizer = require('../colors')

const context = {
EOL: '\n',
Expand All @@ -15,7 +14,7 @@ const context = {
errorLikeObjectKeys: ERROR_LIKE_KEYS,
objectColorizer: colors(),
singleLine: false,
colorizer: getColorizer()
colorizer: colors()
}

tap.test('returns empty string if no properties present', async t => {
Expand Down Expand Up @@ -152,3 +151,16 @@ tap.test('errors skips prettifying if no lines are present', async t => {
})
t.equal(str, '')
})

tap.test('works with single level properties', async t => {
const colorizer = colors(true)
const str = prettifyObject({
log: { foo: 'bar' },
context: {
...context,
objectColorizer: colorizer,
colorizer
}
})
t.equal(str, ` ${colorizer.colors.magenta('foo')}: "bar"\n`)
})
Loading