Module: Vedeu::EscapeSequences::Colours
Overview
Provides colour related escape sequences.
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.
Methods included from Common
#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?
Instance Method Details
#background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
18 19 20 21 22 23 |
# File 'lib/vedeu/esc/colours.rb', line 18 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
28 29 30 31 32 |
# File 'lib/vedeu/esc/colours.rb', line 28 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
37 38 39 40 41 |
# File 'lib/vedeu/esc/colours.rb', line 37 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:
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/vedeu/esc/colours.rb', line 70 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>
93 94 95 96 97 |
# File 'lib/vedeu/esc/colours.rb', line 93 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.
104 105 106 107 108 |
# File 'lib/vedeu/esc/colours.rb', line 104 def valid_name?(named_colour) return false unless symbol?(named_colour) valid_codes.include?(named_colour) end |