Permalink
Browse files

Beautify styles

  • Loading branch information...
1 parent 33d5c0f commit c99bf9ef3f0780d0c592c40e77fc9e5e275fdbd4 @wdavidw committed Nov 29, 2011
Showing with 34 additions and 35 deletions.
  1. +34 −35 lib/Styles.coffee
View
@@ -20,78 +20,77 @@ bgcolors =
white: 47
module.exports = Styles = (settings = {}) ->
- if this not instanceof Styles
+ if @ not instanceof Styles
return new Styles settings
- this.settings = settings
- this.settings.stdout = settings.stdout ? process.stdout
+ @settings = settings
+ @settings.stdout = settings.stdout ? process.stdout
# Current state
- this.current =
+ @current =
weight: 'regular'
# Export colors
- this.colors = colors
- this.bgcolors = bgcolors
- this
+ @colors = colors
+ @bgcolors = bgcolors
+ @
# Color
Styles.prototype.color = (color, text) ->
- this.print text, {color: color}
- if not text
- # Save state if no text
- this.current.color = color
- this
+ @print text, {color: color}
+ # Save state if no text
+ @current.color = color unless text
+ @
for color, code of colors
do (color) ->
Styles.prototype[color] = (text) ->
- this.color color, text
+ @color color, text
Styles.prototype.nocolor = (text) ->
- this.color null, text
+ @color null, text
# bgcolor
Styles.prototype.bgcolor = (bgcolor) ->
bgcolor ?= 0
- this.print '\x1B[' + bgcolor + ';m39'
- this
+ @print '\x1B[' + bgcolor + ';m39'
+ @
# Font weight
Styles.prototype.weight = (weight, text) ->
- this.print text, {weight: weight}
+ @print text, {weight: weight}
if not text
# Save state if no text
- this.current.weight = weight
- this
+ @current.weight = weight
+ @
Styles.prototype.bold = (text) ->
- this.weight 'bold', text
+ @weight 'bold', text
Styles.prototype.regular = (text) ->
- this.weight 'regular', text
+ @weight 'regular', text
# Print
Styles.prototype.print = (text, settings) ->
- this.settings.stdout.write this.raw(text, settings)
- this
+ @settings.stdout.write @raw(text, settings)
+ @
Styles.prototype.println = (text) ->
- this.settings.stdout.write text + '\n'
- this
+ @settings.stdout.write text + '\n'
+ @
Styles.prototype.ln = ->
- this.settings.stdout.write '\n'
- this
+ @settings.stdout.write '\n'
+ @
# Others
Styles.prototype.raw = (text, settings) ->
raw = '';
settings ?= {}
- if settings.color isnt null and ( settings.color or this.current.color )
- raw += '\x1b[' + this.colors[settings.color or this.current.color] + 'm'
+ if settings.color isnt null and ( settings.color or @current.color )
+ raw += '\x1b[' + @colors[settings.color or @current.color] + 'm'
else
raw += '\x1b[39m'
- switch settings.weight or this.current.weight
+ switch settings.weight or @current.weight
when 'bold'
raw += '\x1b[1m'
when 'regular'
@@ -102,13 +101,13 @@ Styles.prototype.raw = (text, settings) ->
# Print text if any
raw += text
# Restore state if any
- if this.current.color and this.current.color isnt settings.color
- raw += this.raw null, this.current.color
- if this.current.weight and this.current.weight isnt settings.weight
- raw += this.raw null, this.current.weight
+ if @current.color and @current.color isnt settings.color
+ raw += @raw null, @current.color
+ if @current.weight and @current.weight isnt settings.weight
+ raw += @raw null, @current.weight
raw
Styles.prototype.reset = (text) ->
- this.print null,
+ @print null,
color: null
weight: 'regular'

0 comments on commit c99bf9e

Please sign in to comment.