Module: Vedeu::EscapeSequences::Colours

Extended by:
Colours
Included in:
Colours, Esc
Defined in:
lib/vedeu/esc/colours.rb

Overview

Provides colour related escape sequences.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.background_codesHash<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



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_codesHash<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:

Examples:

# "\e[31m"
Vedeu::EscapeSequences::Esc.red

# "\e[31msome text\e[39m"
Vedeu::EscapeSequences::Esc.red { 'some text' }

# "\e[44m"
Vedeu::EscapeSequences::Esc.on_blue

# "\e[44msome text\e[49m"
Vedeu::EscapeSequences::Esc.on_blue { 'some text' }

# Valid names:
:black, :red, :green, :yellow, :blue, :magenta, :cyan,
:light_grey, :default, :dark_grey, :light_red, :light_green,
:light_yellow, :light_blue, :light_magenta, :light_cyan,
:white


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



72
73
74
75
# File 'lib/vedeu/esc/colours.rb', line 72

def setup!
  define_backgrounds!
  define_foregrounds!
end

Instance Method Details

#background_codesHash<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



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_codesHash<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:

Examples:

# "\e[31m"
Vedeu::EscapeSequences::Esc.red

# "\e[31msome text\e[39m"
Vedeu::EscapeSequences::Esc.red { 'some text' }

# "\e[44m"
Vedeu::EscapeSequences::Esc.on_blue

# "\e[44msome text\e[49m"
Vedeu::EscapeSequences::Esc.on_blue { 'some text' }

# Valid names:
:black, :red, :green, :yellow, :blue, :magenta, :cyan,
:light_grey, :default, :dark_grey, :light_red, :light_green,
:light_yellow, :light_blue, :light_magenta, :light_cyan,
:white


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



72
73
74
75
# File 'lib/vedeu/esc/colours.rb', line 72

def setup!
  define_backgrounds!
  define_foregrounds!
end