Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Beautify styles

  • Loading branch information...
commit c99bf9ef3f0780d0c592c40e77fc9e5e275fdbd4 1 parent 33d5c0f
Worms David authored

Showing 1 changed file with 34 additions and 35 deletions. Show diff stats Hide diff stats

  1. +34 35 lib/Styles.coffee
69 lib/Styles.coffee
@@ -20,78 +20,77 @@ bgcolors =
20 20 white: 47
21 21
22 22 module.exports = Styles = (settings = {}) ->
23   - if this not instanceof Styles
  23 + if @ not instanceof Styles
24 24 return new Styles settings
25   - this.settings = settings
26   - this.settings.stdout = settings.stdout ? process.stdout
  25 + @settings = settings
  26 + @settings.stdout = settings.stdout ? process.stdout
27 27 # Current state
28   - this.current =
  28 + @current =
29 29 weight: 'regular'
30 30 # Export colors
31   - this.colors = colors
32   - this.bgcolors = bgcolors
33   - this
  31 + @colors = colors
  32 + @bgcolors = bgcolors
  33 + @
34 34
35 35 # Color
36 36 Styles.prototype.color = (color, text) ->
37   - this.print text, {color: color}
38   - if not text
39   - # Save state if no text
40   - this.current.color = color
41   - this
  37 + @print text, {color: color}
  38 + # Save state if no text
  39 + @current.color = color unless text
  40 + @
42 41
43 42 for color, code of colors
44 43 do (color) ->
45 44 Styles.prototype[color] = (text) ->
46   - this.color color, text
  45 + @color color, text
47 46
48 47 Styles.prototype.nocolor = (text) ->
49   - this.color null, text
  48 + @color null, text
50 49
51 50 # bgcolor
52 51 Styles.prototype.bgcolor = (bgcolor) ->
53 52 bgcolor ?= 0
54   - this.print '\x1B[' + bgcolor + ';m39'
55   - this
  53 + @print '\x1B[' + bgcolor + ';m39'
  54 + @
56 55
57 56 # Font weight
58 57 Styles.prototype.weight = (weight, text) ->
59   - this.print text, {weight: weight}
  58 + @print text, {weight: weight}
60 59 if not text
61 60 # Save state if no text
62   - this.current.weight = weight
63   - this
  61 + @current.weight = weight
  62 + @
64 63
65 64 Styles.prototype.bold = (text) ->
66   - this.weight 'bold', text
  65 + @weight 'bold', text
67 66
68 67 Styles.prototype.regular = (text) ->
69   - this.weight 'regular', text
  68 + @weight 'regular', text
70 69
71 70 # Print
72 71
73 72 Styles.prototype.print = (text, settings) ->
74   - this.settings.stdout.write this.raw(text, settings)
75   - this
  73 + @settings.stdout.write @raw(text, settings)
  74 + @
76 75
77 76 Styles.prototype.println = (text) ->
78   - this.settings.stdout.write text + '\n'
79   - this
  77 + @settings.stdout.write text + '\n'
  78 + @
80 79
81 80 Styles.prototype.ln = ->
82   - this.settings.stdout.write '\n'
83   - this
  81 + @settings.stdout.write '\n'
  82 + @
84 83
85 84 # Others
86 85
87 86 Styles.prototype.raw = (text, settings) ->
88 87 raw = '';
89 88 settings ?= {}
90   - if settings.color isnt null and ( settings.color or this.current.color )
91   - raw += '\x1b[' + this.colors[settings.color or this.current.color] + 'm'
  89 + if settings.color isnt null and ( settings.color or @current.color )
  90 + raw += '\x1b[' + @colors[settings.color or @current.color] + 'm'
92 91 else
93 92 raw += '\x1b[39m'
94   - switch settings.weight or this.current.weight
  93 + switch settings.weight or @current.weight
95 94 when 'bold'
96 95 raw += '\x1b[1m'
97 96 when 'regular'
@@ -102,13 +101,13 @@ Styles.prototype.raw = (text, settings) ->
102 101 # Print text if any
103 102 raw += text
104 103 # Restore state if any
105   - if this.current.color and this.current.color isnt settings.color
106   - raw += this.raw null, this.current.color
107   - if this.current.weight and this.current.weight isnt settings.weight
108   - raw += this.raw null, this.current.weight
  104 + if @current.color and @current.color isnt settings.color
  105 + raw += @raw null, @current.color
  106 + if @current.weight and @current.weight isnt settings.weight
  107 + raw += @raw null, @current.weight
109 108 raw
110 109
111 110 Styles.prototype.reset = (text) ->
112   - this.print null,
  111 + @print null,
113 112 color: null
114 113 weight: 'regular'

0 comments on commit c99bf9e

Please sign in to comment.
Something went wrong with that request. Please try again.