Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/easy-color.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/easy-color.rb', line 78

def method_missing(m, *args, &block)
  if $color.keys.include?(m)
    fg(m)
  elsif $attrs.keys.include?(m)
    attr(m)
  else
    raise NoMethodError, "Undefined method '#{m.inspect}'."
  end
end

Instance Method Details

#attr(attr) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/easy-color.rb', line 70

def attr(attr)
  if $stdout.isatty
    "\033[#{($attrs[attr])}m#{self}\033[0m" if valid(:attr => attr)
  else
    "#{self}"
  end
end

#bg(color) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/easy-color.rb', line 62

def bg(color)
  if $stdout.isatty
    "\033[#{($color[color]+10)}m#{self}\033[0m" if valid(:color => color)
  else
    "#{self}"
  end
end

#fg(color) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/easy-color.rb', line 54

def fg(color)
  if $stdout.isatty
    "\033[#{$color[color]}m#{self}\033[0m" if valid(:color => color)
  else
    "#{self}"
  end
end

#valid(args) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/easy-color.rb', line 44

def valid(args)
  if args.keys.include? :color and $color.keys.include? args[:color]
    true
  elsif args.keys.include? :attr and $attrs.keys.include? args[:attr]
    true
  else
    false
  end
end