Class: Pow::Puts

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Puts

Returns a new instance of Puts.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pow.rb', line 55

def initialize(*args)
  options = args[0].is_a?(Hash) ? args[0] : {:text => args[0].to_s}.merge(args[1] || {})
  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[:strikethrough]  ||= options[:misc][:strikethrough]
  end
  @writer = options[:writer] || STDOUT
  @formatted_text = format_string(options)
  out!(@formatted_text)
end

Instance Attribute Details

#writerObject

Returns the value of attribute writer.



54
55
56
# File 'lib/pow.rb', line 54

def writer
  @writer
end

Instance Method Details

#inspectObject



96
97
98
# File 'lib/pow.rb', line 96

def inspect
  nil
end

#out!(string = nil) ⇒ Object

Write the coloured text to IO



79
80
81
82
83
# File 'lib/pow.rb', line 79

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

#painbow(*args) ⇒ Object

Feel the painbow



91
92
93
94
# File 'lib/pow.rb', line 91

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



85
86
87
88
# File 'lib/pow.rb', line 85

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