Class: ColoredString

Attributes

Instance Attributes

colors [RW] public

Returns the value of attribute colors.

Constants

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, }

Constructor Summary

public initialize(string = "")
[View source]


59
60
61
62
# File 'lib/rango/ext/colored_string.rb', line 59

def initialize(string = "")
  @string = string
  @colors = Array.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

public method_missing(method, *args, &block)
[View source]


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rango/ext/colored_string.rb', line 92

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

Public Visibility

Public Class Method Summary

template(string)

Public Instance Method Summary

#inspect
#raw
#to_s

Public Instance Methods Inherited from Object

define_instance_method, not_nil?, try

Public Class Method Details

template

public template(string)

Title

  • u, i/em, b/strong
[View source]


53
54
55
# File 'lib/rango/ext/colored_string.rb', line 53

def template(string)
  return string # TODO
end

Public Instance Method Details

inspect

public inspect
[View source]


84
85
86
# File 'lib/rango/ext/colored_string.rb', line 84

def inspect
  "#<ColoredString:#@string>"
end

raw

public raw
[View source]


88
89
90
# File 'lib/rango/ext/colored_string.rb', line 88

def raw
  @string
end

to_s

public to_s
[View source]


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rango/ext/colored_string.rb', line 71

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