Skip to content

Commit

Permalink
Unify colorize methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 14, 2017
1 parent bffbc8e commit 2006c84
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
27 changes: 11 additions & 16 deletions lib/Default.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ms = require 'pretty-ms'

{getColor} = require './Util'
{getColor, colorize} = require './Util'
CONST = require './Constants'
format = require './Format'

Expand Down Expand Up @@ -72,20 +72,20 @@ module.exports = () ->
diff = " +0ms"

messageType = @outputType type
messageType = @colorize colorType, messageType
messageType = colorize colorType, messageType

separator = @outputSeparator(type)

messageCounter = @outputCounter()
messageCounter = @colorize CONST.LINE_COLOR, messageCounter
messageCounter = colorize CONST.LINE_COLOR, messageCounter

messageContext = @outputContext()
messageContext = @colorize CONST.LINE_COLOR, messageContext
messageContext = colorize CONST.LINE_COLOR, messageContext

align = @outputAlign()

output = "#{separator}#{messageType}#{messageCounter}#{messageContext}#{align}#{message}"
output += @colorize colorType, diff if diff
output += colorize colorType, diff if diff
output

generateTypeMessage: (type) ->
Expand All @@ -99,24 +99,19 @@ module.exports = () ->
colorizeMessage: (type, message) ->
return message unless @color
lineColor = CONST.LINE_COLOR
return @colorize lineColor, message if message.indexOf '=' is -1
return colorize lineColor, message if message.indexOf '=' is -1
typeColor = @types[type].color

message.toString().split(' ').map((msg) =>
message.toString().split(' ').map(msg ->
msg = msg.split '='
if msg.length > 1
msg[0] = @colorize typeColor, msg[0]
msg[1] = @colorize lineColor, msg[1]
msg.join @colorize lineColor, '='
msg[0] = colorize typeColor, msg[0]
msg[1] = colorize lineColor, msg[1]
msg.join colorize lineColor, '='
else
@colorize lineColor, msg
colorize lineColor, msg
).join(' ')

colorize: (colors, message) ->
return message unless @color
(stylize = getColor color) for color in colors
stylize message

isPrintable: (type) ->
return true if @level is CONST.UNMUTED
return false if @level is CONST.MUTED
Expand Down
2 changes: 1 addition & 1 deletion lib/Format.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ serialize = (obj, color, key) ->
else if isDate value
msg += key + '=' + value
else
msg += serialize(value, color, colorize(key, color))
msg += serialize(value, color, colorize(color, key))
if i < length - 1
msg += ' '
i++
Expand Down
12 changes: 8 additions & 4 deletions lib/Util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ chalk = require 'chalk'

isHexColor = (str) -> str.charAt(0) is '#'

module.exports =
getColor: (color) ->
if isHexColor(color) then chalk.hex(color) else chalk[color]
getColor = (color) ->
if isHexColor(color) then chalk.hex(color) else chalk[color]

colorize: (value, color) => @getColor(color)(value)
colorize = (colors, value) ->
stylize = getColor
(stylize = getColor color) for color in colors
stylize value

module.exports = {getColor, colorize}

0 comments on commit 2006c84

Please sign in to comment.