Class: ColoredString
Constant Summary collapse
- ATTRIBUTES =
{ clear: 0, reset: 0, # synonym for :clear bold: 1, dark: 2, italic: 3, # not widely implemented underline: 4, underscore: 4, # synonym for :underline blink: 5, rapid_blink: 6, # not widely implemented negative: 7, # no reverse because of String#reverse concealed: 8, strikethrough: 9, # not widely implemented black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, white: 37, on_black: 40, on_red: 41, on_green: 42, on_yellow: 43, on_blue: 44, on_magenta: 45, on_cyan: 46, on_white: 47, }
Instance Attribute Summary collapse
-
#colors ⇒ Object
Returns the value of attribute colors.
Class Method Summary collapse
-
.template(string) ⇒ Object
<red.bold>Title</red.bold> - u, i/em, b/strong.
Instance Method Summary collapse
-
#initialize(string = "") ⇒ ColoredString
constructor
A new instance of ColoredString.
- #inspect ⇒ Object
- #method_missing(method, *args, &block) ⇒ Object
- #raw ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(string = "") ⇒ ColoredString
Returns a new instance of ColoredString.
57 58 59 60 |
# File 'lib/rubyexts/colored_string.rb', line 57 def initialize(string = "") @string = string @colors = Array.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/rubyexts/colored_string.rb', line 90 def method_missing(method, *args, &block) returned = @string.send(method, *args, &block) # Bang methods like upcase! if returned.is_a?(String) and @string.equal?(returned) # same object @string.replace(returned) return self # upcase elsif returned.is_a?(String) and not @string.equal?(returned) # different object return self.class.new(returned) else # not string, String#split etc return returned end end |
Instance Attribute Details
#colors ⇒ Object
Returns the value of attribute colors.
56 57 58 |
# File 'lib/rubyexts/colored_string.rb', line 56 def colors @colors end |
Class Method Details
.template(string) ⇒ Object
<red.bold>Title</red.bold>
-
u, i/em, b/strong
51 52 53 |
# File 'lib/rubyexts/colored_string.rb', line 51 def template(string) return string # TODO end |
Instance Method Details
#inspect ⇒ Object
82 83 84 |
# File 'lib/rubyexts/colored_string.rb', line 82 def inspect "#<ColoredString:#@string>" end |
#raw ⇒ Object
86 87 88 |
# File 'lib/rubyexts/colored_string.rb', line 86 def raw @string end |
#to_s ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rubyexts/colored_string.rb', line 69 def to_s if ! @colors.empty? && self.class.coloring? sequences = "" @colors.each do |name| code = ATTRIBUTES[name] sequences << "\e[#{code}m" end return "#{sequences}#{@string}\e[0m" else @string end end |