Class: Pow::Puts

Inherits:
Object
  • Object
show all
Defined in:
lib/pow.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Puts

Returns a new instance of Puts.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pow.rb', line 101

def initialize(*args)
  options = args[0].is_a?(Hash) ? args[0] : {:text => args[0].to_s}.merge(args[1] || {}) rescue {:text => args[0]}
  @@match_color = :red
  CODES.keys.each do |key|
    # Color
    self.class.send(:define_method, key.to_sym)       { |*args| Puts.new({:color => key.to_sym, :text => args[0], :misc => args[1]}).out! }
    # Bold
    self.class.send(:define_method, "#{key}!".to_sym) { |*args| Puts.new({:color => key.to_sym, :text => args[0], :bold => true, :misc => args[1]}).out! }
    # Underline
    self.class.send(:define_method, "#{key}_".to_sym) { |*args| Puts.new({:color => key.to_sym, :text => args[0], :underline => true, :misc => args[1]}).out! }
  end     
  # FIXME make this part a method or less fug
  if options[:misc] && options[:misc].is_a?(Hash)
    options[:bold]       ||= options[:misc][:bold]
    options[:negative]   ||= options[:misc][:negative]
    options[:underline]  ||= options[:misc][:underline]
    options[:background] ||= (options[:misc][:background] || options[:misc][:on])
    options[:on]         ||= options[:background]
    options[:strikethrough]  ||= options[:misc][:strikethrough]
    options[:match]       ||= options[:misc][:match]
    options[:match_color] ||= options[:misc][:match_color]
  end
  @@color    = options[:color]  || :white
  @writer    = options[:writer] || STDOUT
  @formatted_text = Pow.enabled? ? format_string(options) : options[:text] + "\n"
  out!(@formatted_text)
end

Instance Attribute Details

#writerObject

Returns the value of attribute writer.



100
101
102
# File 'lib/pow.rb', line 100

def writer
  @writer
end

Class Method Details

.colorObject



130
131
132
# File 'lib/pow.rb', line 130

def color
  @@color
end

.match_colorObject



138
139
140
# File 'lib/pow.rb', line 138

def match_color
  @@match_color
end

.match_color=(val) ⇒ Object



134
135
136
# File 'lib/pow.rb', line 134

def match_color=(val)
  @@match_color = val
end

Instance Method Details

#inspectObject



161
162
163
# File 'lib/pow.rb', line 161

def inspect
  @formatted_text.inspect
end

#out!(string = nil) ⇒ Object

Write the coloured text to IO



144
145
146
147
148
# File 'lib/pow.rb', line 144

def out!(string=nil)
  if string && string != ""
    printf(writer, string)
  end
end

#painbow(*args) ⇒ Object

Feel the painbow



156
157
158
159
# File 'lib/pow.rb', line 156

def painbow(*args)
  text = args[0]
  out!(text.to_s.split("").inject(""){|m, word| m+= format_string(:text => word, :bold => true, :newline => "", :color => painbow_keys.sort_by{|k| rand}[0]) + " "  } + "\n")
end

#rainbow(*args) ⇒ Object



150
151
152
153
# File 'lib/pow.rb', line 150

def rainbow(*args)
  text = args[0]
  out!(text.to_s.split("").inject(""){|m, word| m+= format_string(:text => word, :bold => true, :newline => "", :color => rainbow_keys.sort_by{|k| rand}[0]) + " "  } + "\n")
end