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.
- .define_backgrounds! ⇒ void
-
.define_foregrounds! ⇒ void
Dynamically creates methods for each terminal named colour.
-
.foreground_codes ⇒ Hash<Symbol => Fixnum>
(also: #codes)
Produces the foreground named colour escape sequence hash.
- .setup! ⇒ void
Instance Method Summary collapse
-
#background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
- #define_backgrounds! ⇒ void
-
#define_foregrounds! ⇒ void
Dynamically creates methods for each terminal named colour.
-
#foreground_codes ⇒ Hash<Symbol => Fixnum>
(also: #codes)
Produces the foreground named colour escape sequence hash.
- #setup! ⇒ void
Class Method Details
.background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
15 16 17 18 19 20 |
# File 'lib/vedeu/esc/colours.rb', line 15 def background_codes foreground_codes.each_with_object({}) do |(k, v), h| h[k] = v + 10 h end.freeze end |
.define_backgrounds! ⇒ void
This method returns an undefined value.
78 79 80 81 82 83 84 85 |
# File 'lib/vedeu/esc/colours.rb', line 78 def define_backgrounds! background_codes.each do |key, code| define_method(:"on_#{key}") do |&blk| "\e[#{code}m".freeze + (blk ? blk.call + "\e[49m".freeze : ''.freeze) end end end |
.define_foregrounds! ⇒ void
This method returns an undefined value.
Dynamically creates methods for each terminal named colour. When a block is given, then the colour is reset to ‘default’ once the block is called.
92 93 94 95 96 97 98 99 |
# File 'lib/vedeu/esc/colours.rb', line 92 def define_foregrounds! foreground_codes.each do |key, code| define_method(key) do |&blk| "\e[#{code}m".freeze + (blk ? blk.call + "\e[39m".freeze : ''.freeze) end end end |
.foreground_codes ⇒ Hash<Symbol => Fixnum> Also known as: codes
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:
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vedeu/esc/colours.rb', line 48 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, }.freeze end |
.setup! ⇒ void
This method returns an undefined value.
72 73 74 75 |
# File 'lib/vedeu/esc/colours.rb', line 72 def setup! define_backgrounds! define_foregrounds! end |
Instance Method Details
#background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
15 16 17 18 19 20 |
# File 'lib/vedeu/esc/colours.rb', line 15 def background_codes foreground_codes.each_with_object({}) do |(k, v), h| h[k] = v + 10 h end.freeze end |
#define_backgrounds! ⇒ void
This method returns an undefined value.
78 79 80 81 82 83 84 85 |
# File 'lib/vedeu/esc/colours.rb', line 78 def define_backgrounds! background_codes.each do |key, code| define_method(:"on_#{key}") do |&blk| "\e[#{code}m".freeze + (blk ? blk.call + "\e[49m".freeze : ''.freeze) end end end |
#define_foregrounds! ⇒ void
This method returns an undefined value.
Dynamically creates methods for each terminal named colour. When a block is given, then the colour is reset to ‘default’ once the block is called.
92 93 94 95 96 97 98 99 |
# File 'lib/vedeu/esc/colours.rb', line 92 def define_foregrounds! foreground_codes.each do |key, code| define_method(key) do |&blk| "\e[#{code}m".freeze + (blk ? blk.call + "\e[39m".freeze : ''.freeze) end end end |
#foreground_codes ⇒ Hash<Symbol => Fixnum> Also known as: codes
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:
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/vedeu/esc/colours.rb', line 48 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, }.freeze end |
#setup! ⇒ void
This method returns an undefined value.
72 73 74 75 |
# File 'lib/vedeu/esc/colours.rb', line 72 def setup! define_backgrounds! define_foregrounds! end |