Skip to content

Commit 38600fe

Browse files
committed
feat: support badge with fancy
1 parent 66eba0f commit 38600fe

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/reporters/fancy.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import stringWidth from 'string-width'
22
import figures from 'figures'
33
import BasicReporter from './basic'
44
import { parseStack } from '../utils/error'
5-
import { chalkColor } from '../utils/chalk'
5+
import { chalkColor, chalkBgColor } from '../utils/chalk'
66
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from '../utils/fancy'
77

88
const DEFAULTS = {
@@ -30,22 +30,33 @@ export default class FancyReporter extends BasicReporter {
3030
}
3131

3232
typeColor (type, level) {
33-
return chalkColor(TYPE_COLOR_MAP[type] || LEVEL_COLOR_MAP[level] || this.options.secondaryColor)
33+
return chalkColor()
3434
}
3535

36-
formatType (type, typeColor) {
37-
const _type = typeof TYPE_ICONS[type] === 'string' ? TYPE_ICONS[type] : type
38-
return _type ? typeColor(_type) : ''
36+
formatType (logObj, isBadge) {
37+
const typeColor = TYPE_COLOR_MAP[logObj.type] ||
38+
LEVEL_COLOR_MAP[logObj.level] ||
39+
this.options.secondaryColor
40+
41+
if (isBadge) {
42+
return chalkBgColor(typeColor).black(` ${logObj.type.toUpperCase()} `)
43+
}
44+
45+
const _type = typeof TYPE_ICONS[logObj.type] === 'string' ? TYPE_ICONS[logObj.type] : logObj.type
46+
return _type ? chalkColor(typeColor)(_type) : ''
3947
}
4048

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

44-
const typeColor = this.typeColor(logObj.type, logObj.level)
52+
const isBadge = logObj.badge || logObj.level < 2
53+
4554
const secondaryColor = chalkColor(this.options.secondaryColor)
4655

4756
const date = secondaryColor(this.formatDate(logObj.date))
48-
const type = this.formatType(logObj.type, typeColor)
57+
58+
const type = this.formatType(logObj, isBadge)
59+
4960
const tag = logObj.tag ? secondaryColor(logObj.tag) : ''
5061

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

62-
return line
73+
return isBadge ? '\n' + line + '\n' : line
6374
}
6475
}

0 commit comments

Comments
 (0)