-
Notifications
You must be signed in to change notification settings - Fork 175
/
fancy.js
75 lines (56 loc) · 1.97 KB
/
fancy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import stringWidth from 'string-width'
import figures from 'figures'
import BasicReporter from './basic'
import { parseStack } from '../utils/error'
import { chalkColor, chalkBgColor } from '../utils/chalk'
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from '../utils/fancy'
const DEFAULTS = {
secondaryColor: 'grey'
}
const TYPE_ICONS = {
info: figures('ℹ'),
success: figures('✔'),
error: figures('✖'),
fatal: figures('✖'),
warn: figures('⚠'),
debug: figures('→'),
trace: figures('→'),
log: figures('›')
}
export default class FancyReporter extends BasicReporter {
constructor (options) {
super(Object.assign({}, DEFAULTS, options))
}
formatStack (stack) {
return ' at ' + parseStack(stack).join(' ↲\n at ')
}
typeColor (type, level) {
return chalkColor()
}
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 isBadge = logObj.badge || logObj.level < 2
const secondaryColor = chalkColor(this.options.secondaryColor)
const date = secondaryColor(this.formatDate(logObj.date))
const type = this.formatType(logObj, isBadge)
const tag = logObj.tag ? secondaryColor(logObj.tag) : ''
let left = this.filterAndJoin([type, message])
let right = this.filterAndJoin([tag, date])
const space = width - stringWidth(left) - stringWidth(right) - 2
let line = space > 0 ? (left + ' '.repeat(space) + right) : left
line += additional
? secondaryColor('\n' + additional)
: ''
return isBadge ? '\n' + line + '\n' : line
}
}