Skip to content

Commit

Permalink
Add keys color based on the level
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Apr 22, 2016
1 parent 8e9eeee commit d120614
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lib/Default.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ module.exports =

generateTypeMessage: (type) ->
(message...) ->
message = @format message
color = @types[type].color
message = @format message, color
message = @generateMessage type, message
@transport message if message
this
Expand All @@ -90,7 +91,9 @@ module.exports =
return false if @level is CONST.MUTED
@types[type].level <= @types[@level].level

format: (messages) -> formatUtil.apply null, messages
format: (messages, color) ->
messages.push color
formatUtil.apply null, messages

keyword: null
diff: false
Expand Down
16 changes: 10 additions & 6 deletions lib/Format.coffee
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict'

chalk = require 'chalk'

REGEX =
escape: /%{2,2}/g
type: /(%?)(%([jds]))/g

hasWhiteSpace = (s) ->
s.indexOf(' ') isnt -1

serialize = (obj, key) ->
serialize = (color, obj, key) ->
# symbols cannot be directly casted to strings
if typeof key is 'symbol'
key = key.toString()
Expand All @@ -34,22 +36,24 @@ serialize = (obj, key) ->
j = 0
l = obj[keys[i]].length
while j < l
msg += serialize(obj[keys[i]][j])
msg += serialize(color, obj[keys[i]][j])
if j < l - 1
msg += ' '
j++
msg += ']'
else if obj[keys[i]] instanceof Date
msg += keys[i] + '=' + obj[keys[i]]
else
msg += serialize(obj[keys[i]], keys[i])
msg += serialize(color, obj[keys[i]], chalk[color](keys[i]))
if i < length - 1
msg += ' '
i++
msg

format = (fmt) ->
args = Array::slice.call(arguments, 1)
color = args.pop()

if args.length
fmt = fmt.replace(REGEX.type, (match, escaped, ptn, flag) ->
arg = args.shift()
Expand All @@ -59,14 +63,14 @@ format = (fmt) ->
when 'd'
arg = Number(arg)
when 'j'
arg = serialize arg
arg = serialize color, arg
return arg if !escaped
args.unshift arg
match
)

fmt += ' ' + serialize arg for arg in args if args.length
fmt += ' ' + serialize color, arg for arg in args if args.length
fmt = fmt.replace(REGEX.escape, '%') if fmt.replace?
serialize fmt
serialize color, fmt

module.exports = format

0 comments on commit d120614

Please sign in to comment.