Class: Rainbow::Presenter

Inherits:
String
  • Object
show all
Defined in:
lib/rainbow/presenter.rb

Constant Summary collapse

TERM_EFFECTS =
{
  reset:     0,
  bright:    1,
  faint:     2,
  italic:    3,
  underline: 4,
  blink:     5,
  inverse:   7,
  hide:      8
}.freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

We take care of X11 color method call here. Such as #aqua, #ghostwhite.



117
118
119
120
121
122
123
# File 'lib/rainbow/presenter.rb', line 117

def method_missing(method_name, *args)
  if Color::X11Named.color_names.include?(method_name) && args.empty?
    color(method_name)
  else
    super
  end
end

Instance Method Details

#background(*values) ⇒ Object Also known as: bg

Sets background color of this text.



27
28
29
# File 'lib/rainbow/presenter.rb', line 27

def background(*values)
  wrap_with_sgr(Color.build(:background, values).codes)
end

#blackObject



83
84
85
# File 'lib/rainbow/presenter.rb', line 83

def black
  color(:black)
end

Turns on blinking attribute for this text (not well supported by terminal emulators).



69
70
71
# File 'lib/rainbow/presenter.rb', line 69

def blink
  wrap_with_sgr(TERM_EFFECTS[:blink])
end

#blueObject



99
100
101
# File 'lib/rainbow/presenter.rb', line 99

def blue
  color(:blue)
end

#brightObject Also known as: bold

Turns on bright/bold for this text.



42
43
44
# File 'lib/rainbow/presenter.rb', line 42

def bright
  wrap_with_sgr(TERM_EFFECTS[:bright])
end

#color(*values) ⇒ Object Also known as: foreground, fg

Sets color of this text.



19
20
21
# File 'lib/rainbow/presenter.rb', line 19

def color(*values)
  wrap_with_sgr(Color.build(:foreground, values).codes)
end

#cyanObject



107
108
109
# File 'lib/rainbow/presenter.rb', line 107

def cyan
  color(:cyan)
end

#faintObject Also known as: dark

Turns on faint/dark for this text (not well supported by terminal emulators).



50
51
52
# File 'lib/rainbow/presenter.rb', line 50

def faint
  wrap_with_sgr(TERM_EFFECTS[:faint])
end

#greenObject



91
92
93
# File 'lib/rainbow/presenter.rb', line 91

def green
  color(:green)
end

#hideObject

Hides this text (set its color to the same as background).



79
80
81
# File 'lib/rainbow/presenter.rb', line 79

def hide
  wrap_with_sgr(TERM_EFFECTS[:hide])
end

#inverseObject

Inverses current foreground/background colors.



74
75
76
# File 'lib/rainbow/presenter.rb', line 74

def inverse
  wrap_with_sgr(TERM_EFFECTS[:inverse])
end

#italicObject

Turns on italic style for this text (not well supported by terminal emulators).



58
59
60
# File 'lib/rainbow/presenter.rb', line 58

def italic
  wrap_with_sgr(TERM_EFFECTS[:italic])
end

#magentaObject



103
104
105
# File 'lib/rainbow/presenter.rb', line 103

def magenta
  color(:magenta)
end

#redObject



87
88
89
# File 'lib/rainbow/presenter.rb', line 87

def red
  color(:red)
end

#resetObject

Resets terminal to default colors/backgrounds.

It shouldn’t be needed to use this method because all methods append terminal reset code to end of string.



37
38
39
# File 'lib/rainbow/presenter.rb', line 37

def reset
  wrap_with_sgr(TERM_EFFECTS[:reset])
end

#respond_to_missing?(method_name, *args) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/rainbow/presenter.rb', line 125

def respond_to_missing?(method_name, *args)
  Color::X11Named.color_names.include?(method_name) && args.empty? || super
end

#underlineObject

Turns on underline decoration for this text.



63
64
65
# File 'lib/rainbow/presenter.rb', line 63

def underline
  wrap_with_sgr(TERM_EFFECTS[:underline])
end

#whiteObject



111
112
113
# File 'lib/rainbow/presenter.rb', line 111

def white
  color(:white)
end

#yellowObject



95
96
97
# File 'lib/rainbow/presenter.rb', line 95

def yellow
  color(:yellow)
end