Skip to content

Commit 30cd4f0

Browse files
committed
feat: add symbols to browser
1 parent 39584d2 commit 30cd4f0

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

demo/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Open developer tools to see the magic!
88
<script type="module">
99
import consola from '../src/browser.js'
10+
consola.level = 5
1011

1112
for (let type of Object.keys(consola.types).sort()) {
1213
consola[type](`A message with consola.${type}()`)

src/reporters/browser.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,43 @@ export default class BrowserReporter {
44
}
55

66
log (logObj) {
7+
/*
8+
* Symbol width is dependent on user's system font
9+
*
10+
* In Chrome:
11+
* - console.error, warn and trace have collapsable output
12+
* - trace is expanded by default
13+
*
14+
* In Firefox:
15+
* - console.error has collapsable output
16+
* - console.warn has not
17+
* - console.trace is always expanded and has no style support
18+
*
19+
* In IE:
20+
* - ?
21+
*/
722
const replaceColors = {
823
undefined: 'grey',
924
yellow: 'goldenrod',
1025
white: 'grey'
1126
}
27+
28+
//
29+
const symbols = {
30+
start: '\u25cf',
31+
info: '\u2139',
32+
success: '\u2713',
33+
debug: '\u2026',
34+
log: '\u276f',
35+
ready: '\u2764'
36+
// warn: '\u00a0' // non-breaking space to help alignment in FF
37+
}
1238
const color = replaceColors[logObj.color] ? replaceColors[logObj.color] : logObj.color
1339

14-
const styleDefault = 'color: silver; background-color: inherit;'
40+
const styleDefault = 'color: ' + color + '; background-color: inherit;'
41+
const styleDiscrete = 'color: silver; background-color: inherit;'
1542
const styleAdditional = 'color: ' + (logObj.additionalColor ? logObj.additionalColor : 'black') + '; background-color: inherit;'
1643
const badgeStyle = 'color: white; background-color: ' + color + ';'
17-
const style = 'color: ' + color + '; background-color: inherit;'
1844

1945
let type = logObj.type
2046
if (!console[type]) { // eslint-disable-line no-console
@@ -45,7 +71,12 @@ export default class BrowserReporter {
4571
'%c' + (logObj.args.length ? '\n' + logObj.args.join() : '')
4672
]
4773

48-
/* eslint-disable-next-line no-console */
49-
console[type](args.join(' '), badgeStyle, styleDefault, style, styleAdditional)
74+
if (symbols[logObj.type]) {
75+
args.unshift('%c' + symbols[logObj.type])
76+
console[type](args.join(' '), styleDefault, badgeStyle, styleDiscrete, styleDefault, styleAdditional)
77+
} else {
78+
/* eslint-disable-next-line no-console */
79+
console[type](args.join(' '), badgeStyle, styleDiscrete, styleDefault, styleAdditional)
80+
}
5081
}
5182
}

0 commit comments

Comments
 (0)