Module: Vedeu::EscapeSequences::Colours
Overview
Provides colour related escape sequences.
Class Method Summary collapse
-
.background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
- .background_colour(named_colour, &block) ⇒ String
- .colour(named_colour, &block) ⇒ String (also: #foreground_colour)
-
.foreground_codes ⇒ Hash<Symbol => Fixnum>
Produces the foreground named colour escape sequence hash.
- .valid_codes ⇒ Array<Symbol>
-
.valid_name?(named_colour) ⇒ Boolean
Returns a boolean indicating whether the colour provided is a valid named colour.
Instance Method Summary collapse
-
#background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
- #background_colour(named_colour, &block) ⇒ String
- #colour(named_colour, &block) ⇒ String (also: #foreground_colour)
-
#foreground_codes ⇒ Hash<Symbol => Fixnum>
Produces the foreground named colour escape sequence hash.
- #valid_codes ⇒ Array<Symbol>
-
#valid_name?(named_colour) ⇒ Boolean
Returns a boolean indicating whether the colour provided is a valid named colour.
Class Method Details
.background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
17 18 19 20 21 22 |
# File 'lib/vedeu/esc/colours.rb', line 17 def background_codes foreground_codes.each_with_object({}) do |(k, v), h| h[k] = v + 10 h end end |
.background_colour(named_colour, &block) ⇒ String
75 76 77 78 79 |
# File 'lib/vedeu/esc/colours.rb', line 75 def background_colour(named_colour, &block) return '' unless valid_name?(named_colour) colour(named_colour.to_s.prepend('on_').to_sym, &block) end |
.colour(named_colour, &block) ⇒ String Also known as: foreground_colour
84 85 86 87 88 |
# File 'lib/vedeu/esc/colours.rb', line 84 def colour(named_colour, &block) return '' unless valid_name?(named_colour) public_send(named_colour, &block) end |
.foreground_codes ⇒ Hash<Symbol => Fixnum>
Produces the foreground named colour escape sequence hash. The background escape sequences are also generated from this by adding 10 to the values. This hash gives rise to methods you can call directly on ‘Esc` to produce the desired colours:
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vedeu/esc/colours.rb', line 50 def foreground_codes { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, light_grey: 37, default: 39, dark_grey: 90, light_red: 91, light_green: 92, light_yellow: 93, light_blue: 94, light_magenta: 95, light_cyan: 96, white: 97, } end |
.valid_codes ⇒ Array<Symbol>
92 93 94 95 96 |
# File 'lib/vedeu/esc/colours.rb', line 92 def valid_codes @_valid_codes ||= foreground_codes.keys.map do |name| name.to_s.prepend('on_').to_sym end + foreground_codes.keys end |
.valid_name?(named_colour) ⇒ Boolean
Returns a boolean indicating whether the colour provided is a valid named colour.
103 104 105 106 107 |
# File 'lib/vedeu/esc/colours.rb', line 103 def valid_name?(named_colour) return false unless named_colour.is_a?(Symbol) valid_codes.include?(named_colour) end |
Instance Method Details
#background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
17 18 19 20 21 22 |
# File 'lib/vedeu/esc/colours.rb', line 17 def background_codes foreground_codes.each_with_object({}) do |(k, v), h| h[k] = v + 10 h end end |
#background_colour(named_colour, &block) ⇒ String
75 76 77 78 79 |
# File 'lib/vedeu/esc/colours.rb', line 75 def background_colour(named_colour, &block) return '' unless valid_name?(named_colour) colour(named_colour.to_s.prepend('on_').to_sym, &block) end |
#colour(named_colour, &block) ⇒ String Also known as: foreground_colour
84 85 86 87 88 |
# File 'lib/vedeu/esc/colours.rb', line 84 def colour(named_colour, &block) return '' unless valid_name?(named_colour) public_send(named_colour, &block) end |
#foreground_codes ⇒ Hash<Symbol => Fixnum>
Produces the foreground named colour escape sequence hash. The background escape sequences are also generated from this by adding 10 to the values. This hash gives rise to methods you can call directly on ‘Esc` to produce the desired colours:
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/vedeu/esc/colours.rb', line 50 def foreground_codes { black: 30, red: 31, green: 32, yellow: 33, blue: 34, magenta: 35, cyan: 36, light_grey: 37, default: 39, dark_grey: 90, light_red: 91, light_green: 92, light_yellow: 93, light_blue: 94, light_magenta: 95, light_cyan: 96, white: 97, } end |
#valid_codes ⇒ Array<Symbol>
92 93 94 95 96 |
# File 'lib/vedeu/esc/colours.rb', line 92 def valid_codes @_valid_codes ||= foreground_codes.keys.map do |name| name.to_s.prepend('on_').to_sym end + foreground_codes.keys end |
#valid_name?(named_colour) ⇒ Boolean
Returns a boolean indicating whether the colour provided is a valid named colour.
103 104 105 106 107 |
# File 'lib/vedeu/esc/colours.rb', line 103 def valid_name?(named_colour) return false unless named_colour.is_a?(Symbol) valid_codes.include?(named_colour) end |