Skip to content

Commit

Permalink
added support for styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 15, 2015
1 parent 2a5d5f7 commit 775e84a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": false,
"eqeqeq": false,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 2,
"latedef": true,
Expand Down
27 changes: 12 additions & 15 deletions lib/Acho.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict'

chalk = require 'chalk'
DEFAULT = require './default'
chalk = DEFAULT.chalk()

module.exports = class Acho
constructor: (options = {}) ->
@color = if options.color? then options.color else DEFAULT.COLOR
@level = if options.level? then options.level else DEFAULT.LEVEL
@types = if options.types? then options.types else DEFAULT.TYPES
@print = if options.print? then options.print else DEFAULT.PRINT
@muted = if options.muted? then options.muted else DEFAULT.MUTED
if options.messages?
@messages = options.messages
else
Expand Down Expand Up @@ -58,25 +59,21 @@ module.exports = class Acho
this

isPrintable: (type) ->
return false if @level is 'silent'
return false if @level is @muted
@types[type].level <= @types[@level].level

colorize: (color, message) ->
colorize: (colors, message) ->
return message unless @color
return chalk[color](message) unless color is 'rainbow'
# rainbow is the core of this library
rainbowColors = ['red', 'yellow', 'green', 'blue', 'magenta']
message = message.split('')
for char, position in message
color = rainbowColors[position % rainbowColors.length]
message[position] = chalk[color](char)
message.join('')
colors = colors.split ' '
stylize = chalk
stylize = stylize[color] for color in colors when color
stylize message

printLine: (type, message) ->
return unless @isPrintable type
colorType = @types[type].color
colorType = @types[type].color
messageType = @outputType type
messageType = @colorize colorType, messageType
message = @outputMessage message
message = @colorize 'gray', message
console.log messageType + message
message = @outputMessage message
message = @colorize @types.line.color, message
console.log "#{messageType} #{message}"
14 changes: 14 additions & 0 deletions lib/default.coffee
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
'use strict'

module.exports =
chalk: ->
chalk = require 'chalk'
chalk.rainbow = (text) ->
text = text.split('')
rainbowColors = ['red', 'yellow', 'green', 'blue', 'magenta']
colorize = ''
for letter, position in text
color = rainbowColors[position % rainbowColors.length]
colorize += chalk.styles[color].open + letter + chalk.styles[color].close
colorize
chalk
OUTPUT_TYPE: (type) -> "#{type}\t: "
OUTPUT_MESSAGE: (message) -> message
PRINT: ->
for type of @types
@printLine(type, message) for message in @messages[type]
LEVEL: 'info'
COLOR: false
MUTED: 'silent'
TYPES:
line:
color: 'gray'
error:
level : 0
color : 'red'
Expand Down
4 changes: 4 additions & 0 deletions test/test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ describe 'Acho ::', ->
it 'print a normal message', ->
@acho.warning 'warning message'

it 'change the color behavior', ->
@acho.types.error.color = 'red bold'
@acho.print()

it 'print the messages', ->
@acho.print()

Expand Down

0 comments on commit 775e84a

Please sign in to comment.