Skip to content

Commit

Permalink
Add colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 18, 2019
1 parent 1d27c07 commit 6a3944b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
4 changes: 1 addition & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
},
"dependencies": {
"bytes": "^3.1.0",
"chalk": "^2.4.2",
"cli-cursor": "^3.1.0",
"core-js": "^3.2.1",
"fast-glob": "^3.0.4",
"figures": "^3.0.0",
"find-up": "^4.1.0",
"get-stream": "^5.1.0",
"is-interactive": "^1.0.0",
Expand Down
8 changes: 8 additions & 0 deletions src/report/colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { constructor as Chalk } from 'chalk'

// Pass `chalk` to reporters. Is noop if `colors` option is `false`.
// We also pass `colors` in case reporters cannot use Chalk (e.g. not CLI).
export const getChalk = function({ colors, ...reportOpt }) {
const chalk = new Chalk({ enabled: colors })
return { ...reportOpt, colors, chalk }
}
7 changes: 5 additions & 2 deletions src/report/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { addPrintedInfo } from '../print/main.js'

import { getChalk } from './colors.js'
import { handleContent } from './content.js'

// Report benchmark results
Expand Down Expand Up @@ -63,9 +64,11 @@ const useReporter = async function({

const reportOptB = convertBooleans(reportOptA)

const content = await reportFunc(benchmark, reportOptB)
const reportOptC = getChalk(reportOptB)

await handleContent(content, reportOptB)
const content = await reportFunc(benchmark, reportOptC)

await handleContent(content, reportOptC)
}

// --report.REPORTER.* options are dynamic, i.e. are not normalized by our
Expand Down
42 changes: 29 additions & 13 deletions src/report/reporters/debug.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import { pointer } from 'figures'

// Debugging reporter only meant for development purpose
const report = function(
{ timestamp, iterations, systemPretty },
{ link, system, show },
{ link, system, show, chalk },
) {
const content = iterations.map(serializeIteration).join('\n')
const content = iterations
.map(iteration => serializeIteration({ iteration, chalk }))
.join('\n')
const contentA = addSystem(content, system, systemPretty)
const contentB = addLink(contentA, link)
const contentB = addLink({ content: contentA, link, chalk })
const contentC = addTimestamp(contentB, timestamp, show)
return contentC
}

const serializeIteration = function({ name, stats, fastest }) {
const fastestMark = fastest ? '*' : ' '
const statsStr = serializeStats(stats)
return `${fastestMark} ${name} | ${statsStr}`
const serializeIteration = function({
iteration: { name, stats, fastest },
chalk,
chalk: { cyan },
}) {
const fastestMark = fastest ? cyan.bold('*') : ' '
const statsStr = serializeStats({ stats, chalk })
const nameA = name
.split('|')
.map(part => cyan.bold(part))
.join(cyan.dim(pointer))
return ` ${fastestMark} ${nameA} ${cyan.dim('|')} ${statsStr}`
}

export const serializeStats = function(stats) {
return STATS.map(statName => serializeStat(stats, statName)).join(' | ')
export const serializeStats = function({ stats, chalk, chalk: { dim } }) {
return STATS.map(statName => serializeStat(stats, statName, chalk)).join(
dim(' | '),
)
}

const STATS = [
Expand All @@ -34,9 +48,9 @@ const STATS = [
'processes',
]

const serializeStat = function(stats, statName) {
const serializeStat = function(stats, statName, { yellow }) {
const stat = stats[`${statName}Pretty`]
return `${statName} ${stat}`
return `${statName} ${yellow(stat)}`
}

const addSystem = function(content, system, systemPretty) {
Expand All @@ -47,12 +61,14 @@ const addSystem = function(content, system, systemPretty) {
return `${content}\n\n${systemPretty}`
}

const addLink = function(content, link) {
const addLink = function({ content, link, chalk: { dim, underline } }) {
if (!link) {
return content
}

return `${content}\n\nBenchmarked with spyd (https://github.com/ehmicky/spyd)`
return `${content}\n\n${dim(
`Benchmarked with spyd ${underline('(https://github.com/ehmicky/spyd)')}`,
)}`
}

const addTimestamp = function(content, timestamp, show) {
Expand Down

0 comments on commit 6a3944b

Please sign in to comment.