Skip to content

Commit

Permalink
feat: add symbols to browser
Browse files Browse the repository at this point in the history
  • Loading branch information
pimlie committed Oct 11, 2018
1 parent 39584d2 commit 30cd4f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Open developer tools to see the magic!
<script type="module">
import consola from '../src/browser.js'
consola.level = 5

for (let type of Object.keys(consola.types).sort()) {
consola[type](`A message with consola.${type}()`)
Expand Down
39 changes: 35 additions & 4 deletions src/reporters/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,43 @@ export default class BrowserReporter {
}

log (logObj) {
/*
* Symbol width is dependent on user's system font
*
* In Chrome:
* - console.error, warn and trace have collapsable output
* - trace is expanded by default
*
* In Firefox:
* - console.error has collapsable output
* - console.warn has not
* - console.trace is always expanded and has no style support
*
* In IE:
* - ?
*/
const replaceColors = {
undefined: 'grey',
yellow: 'goldenrod',
white: 'grey'
}

//
const symbols = {
start: '\u25cf',
info: '\u2139',
success: '\u2713',
debug: '\u2026',
log: '\u276f',
ready: '\u2764'
// warn: '\u00a0' // non-breaking space to help alignment in FF
}
const color = replaceColors[logObj.color] ? replaceColors[logObj.color] : logObj.color

const styleDefault = 'color: silver; background-color: inherit;'
const styleDefault = 'color: ' + color + '; background-color: inherit;'
const styleDiscrete = 'color: silver; background-color: inherit;'
const styleAdditional = 'color: ' + (logObj.additionalColor ? logObj.additionalColor : 'black') + '; background-color: inherit;'
const badgeStyle = 'color: white; background-color: ' + color + ';'
const style = 'color: ' + color + '; background-color: inherit;'

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

/* eslint-disable-next-line no-console */
console[type](args.join(' '), badgeStyle, styleDefault, style, styleAdditional)
if (symbols[logObj.type]) {
args.unshift('%c' + symbols[logObj.type])
console[type](args.join(' '), styleDefault, badgeStyle, styleDiscrete, styleDefault, styleAdditional)
} else {
/* eslint-disable-next-line no-console */
console[type](args.join(' '), badgeStyle, styleDiscrete, styleDefault, styleAdditional)
}
}
}

0 comments on commit 30cd4f0

Please sign in to comment.