Module: Vedeu::EscapeSequences::Colours

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

Overview

Provides colour related escape sequences.

Instance Method Summary collapse

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_codesHash<Symbol => Fixnum>

Produces the background named colour escape sequence hash from the foreground escape sequence hash.

Returns:

  • (Hash<Symbol => Fixnum>)


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

Parameters:

  • named_colour (Symbol)
  • block (Proc)

Returns:

  • (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

Parameters:

  • named_colour (Symbol)
  • block (Proc)

Returns:

  • (String)


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

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

Returns:

  • (Hash<Symbol => Fixnum>)


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_codesArray<Symbol>

Returns:

  • (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.

Parameters:

  • named_colour (Symbol)

Returns:



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