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 private
-
.define_foregrounds! ⇒ void
private
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 private
-
#define_foregrounds! ⇒ void
private
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.
17 18 19 20 21 22 |
# File 'lib/vedeu/esc/colours.rb', line 17 def background_codes hash = {} foreground_codes.inject(hash) do |h, (k, v)| h.merge!(k => v + 10) end end |
.define_backgrounds! ⇒ void (private)
This method returns an undefined value.
78 79 80 81 82 83 84 |
# File 'lib/vedeu/esc/colours.rb', line 78 def define_backgrounds! background_codes.each do |key, code| define_method('on_' + key.to_s) do |&blk| "\e[#{code}m" + (blk ? blk.call + "\e[49m" : '') end end end |
.define_foregrounds! ⇒ void (private)
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.
91 92 93 94 95 96 97 |
# File 'lib/vedeu/esc/colours.rb', line 91 def define_foregrounds! foreground_codes.each do |key, code| define_method(key) do |&blk| "\e[#{code}m" + (blk ? blk.call + "\e[39m" : '') 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:
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/vedeu/esc/colours.rb', line 46 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 |
.setup! ⇒ void
This method returns an undefined value.
70 71 72 73 |
# File 'lib/vedeu/esc/colours.rb', line 70 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.
17 18 19 20 21 22 |
# File 'lib/vedeu/esc/colours.rb', line 17 def background_codes hash = {} foreground_codes.inject(hash) do |h, (k, v)| h.merge!(k => v + 10) end end |
#define_backgrounds! ⇒ void (private)
This method returns an undefined value.
78 79 80 81 82 83 84 |
# File 'lib/vedeu/esc/colours.rb', line 78 def define_backgrounds! background_codes.each do |key, code| define_method('on_' + key.to_s) do |&blk| "\e[#{code}m" + (blk ? blk.call + "\e[49m" : '') end end end |
#define_foregrounds! ⇒ void (private)
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.
91 92 93 94 95 96 97 |
# File 'lib/vedeu/esc/colours.rb', line 91 def define_foregrounds! foreground_codes.each do |key, code| define_method(key) do |&blk| "\e[#{code}m" + (blk ? blk.call + "\e[39m" : '') 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:
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/vedeu/esc/colours.rb', line 46 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 |
#setup! ⇒ void
This method returns an undefined value.
70 71 72 73 |
# File 'lib/vedeu/esc/colours.rb', line 70 def setup! define_backgrounds! define_foregrounds! end |