Skip to content

Commit

Permalink
feat: support badge with fancy
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 2, 2018
1 parent 66eba0f commit 38600fe
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/reporters/fancy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import stringWidth from 'string-width'
import figures from 'figures'
import BasicReporter from './basic'
import { parseStack } from '../utils/error'
import { chalkColor } from '../utils/chalk'
import { chalkColor, chalkBgColor } from '../utils/chalk'
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from '../utils/fancy'

const DEFAULTS = {
Expand Down Expand Up @@ -30,22 +30,33 @@ export default class FancyReporter extends BasicReporter {
}

typeColor (type, level) {
return chalkColor(TYPE_COLOR_MAP[type] || LEVEL_COLOR_MAP[level] || this.options.secondaryColor)
return chalkColor()
}

formatType (type, typeColor) {
const _type = typeof TYPE_ICONS[type] === 'string' ? TYPE_ICONS[type] : type
return _type ? typeColor(_type) : ''
formatType (logObj, isBadge) {
const typeColor = TYPE_COLOR_MAP[logObj.type] ||
LEVEL_COLOR_MAP[logObj.level] ||
this.options.secondaryColor

if (isBadge) {
return chalkBgColor(typeColor).black(` ${logObj.type.toUpperCase()} `)
}

const _type = typeof TYPE_ICONS[logObj.type] === 'string' ? TYPE_ICONS[logObj.type] : logObj.type
return _type ? chalkColor(typeColor)(_type) : ''
}

formatLogObj (logObj, { width }) {
const { message, additional } = this.formatArgs(logObj.args)

const typeColor = this.typeColor(logObj.type, logObj.level)
const isBadge = logObj.badge || logObj.level < 2

const secondaryColor = chalkColor(this.options.secondaryColor)

const date = secondaryColor(this.formatDate(logObj.date))
const type = this.formatType(logObj.type, typeColor)

const type = this.formatType(logObj, isBadge)

const tag = logObj.tag ? secondaryColor(logObj.tag) : ''

let left = this.filterAndJoin([type, message])
Expand All @@ -59,6 +70,6 @@ export default class FancyReporter extends BasicReporter {
? secondaryColor('\n' + additional)
: ''

return line
return isBadge ? '\n' + line + '\n' : line
}
}

0 comments on commit 38600fe

Please sign in to comment.