Module: Vedeu::EscapeSequences::Colours Private

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

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (Hash<Symbol => Fixnum>)


20
21
22
23
24
25
# File 'lib/vedeu/esc/colours.rb', line 20

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • named_colour (Symbol)
  • block (Proc)

Returns:

  • (String)


30
31
32
33
34
# File 'lib/vedeu/esc/colours.rb', line 30

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • named_colour (Symbol)
  • block (Proc)

Returns:

  • (String)


39
40
41
42
43
# File 'lib/vedeu/esc/colours.rb', line 39

def colour(named_colour, &block)
  return '' unless valid_name?(named_colour)

  public_send(named_colour, &block)
end

#foreground_codesHash<Symbol => Fixnum>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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>)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vedeu/esc/colours.rb', line 72

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>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array<Symbol>)


95
96
97
98
99
# File 'lib/vedeu/esc/colours.rb', line 95

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether the colour provided is a valid named colour.

Parameters:

  • named_colour (Symbol)

Returns:



106
107
108
109
110
# File 'lib/vedeu/esc/colours.rb', line 106

def valid_name?(named_colour)
  return false unless symbol?(named_colour)

  valid_codes.include?(named_colour)
end