Class: String
- Inherits:
-
Object
show all
- Defined in:
- lib/colorize.rb,
lib/hash_delegator.rb,
lib/object_present.rb,
lib/object_present.rb
Overview
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ String
Handles dynamic method calls to create RGB colors.
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/colorize.rb', line 14
def method_missing(method_name, *args, &block)
case method_name.to_s
when /^fg_rgb_/
fg_rgb_color($'.gsub('_', ';'))
when /^fg_rgbh_/
hex_to_rgb($')
else
super
end
end
|
Instance Method Details
#ansi_control_sequence ⇒ String
Generates an ANSI control sequence for the string.
28
29
30
|
# File 'lib/colorize.rb', line 28
def ansi_control_sequence
"\033[#{self}\033[0m"
end
|
64
|
# File 'lib/colorize.rb', line 64
def bgreen; "1;32m#{self}".ansi_control_sequence; end
|
A collection of methods for applying named colors.
For example, #black applies a black foreground color to the string. These are provided for convenience and easy readability.
62
|
# File 'lib/colorize.rb', line 62
def black; "30m#{self}".ansi_control_sequence; end
|
#blank? ⇒ Boolean
20
21
22
|
# File 'lib/object_present.rb', line 20
def blank?
empty? || /\A[[:space:]]*\z/.freeze.match?(self)
end
|
More named colors using RGB hex values
72
|
# File 'lib/colorize.rb', line 72
def blue; fg_rgbh_00_00_FF; end
|
63
|
# File 'lib/colorize.rb', line 63
def bred; "1;31m#{self}".ansi_control_sequence; end
|
69
|
# File 'lib/colorize.rb', line 69
def bwhite; "1;37m#{self}".ansi_control_sequence; end
|
65
|
# File 'lib/colorize.rb', line 65
def byellow; "1;33m#{self}".ansi_control_sequence; end
|
67
|
# File 'lib/colorize.rb', line 67
def cyan; "36m#{self}".ansi_control_sequence; end
|
#fg_rgb_color(rgb) ⇒ String
Applies a 24-bit RGB foreground color to the string.
36
37
38
|
# File 'lib/colorize.rb', line 36
def fg_rgb_color(rgb)
"38;2;#{rgb}m#{self}".ansi_control_sequence
end
|
73
|
# File 'lib/colorize.rb', line 73
def green; fg_rgbh_00_FF_00; end
|
#hex_to_rgb(hex_str) ⇒ String
Converts hex color codes to RGB and applies them to the string.
44
45
46
47
48
|
# File 'lib/colorize.rb', line 44
def hex_to_rgb(hex_str)
fg_rgb_color(
hex_str.split('_').map { |hex| hex.to_i(16).to_s }.join(';')
)
end
|
74
|
# File 'lib/colorize.rb', line 74
def indigo; fg_rgbh_4B_00_82; end
|
66
|
# File 'lib/colorize.rb', line 66
def magenta; "35m#{self}".ansi_control_sequence; end
|
#non_empty? ⇒ Boolean
Checks if the string is not empty.
40
41
42
|
# File 'lib/hash_delegator.rb', line 40
def non_empty?
!empty?
end
|
75
|
# File 'lib/colorize.rb', line 75
def orange; fg_rgbh_FF_7F_00; end
|
Provides a plain, unmodified version of the string.
53
54
55
|
# File 'lib/colorize.rb', line 53
def plain
self
end
|
#present? ⇒ Boolean
30
31
32
|
# File 'lib/object_present.rb', line 30
def present?
!empty?
end
|
76
|
# File 'lib/colorize.rb', line 76
def red; fg_rgbh_FF_00_00; end
|
77
|
# File 'lib/colorize.rb', line 77
def violet; fg_rgbh_94_00_D3; end
|
68
|
# File 'lib/colorize.rb', line 68
def white; "37m#{self}".ansi_control_sequence; end
|
78
|
# File 'lib/colorize.rb', line 78
def yellow; fg_rgbh_FF_FF_00; end
|