Module: Vedeu::EscapeSequences::Esc

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

Overview

Provides escape sequence strings for setting the cursor position and various display related functions.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.absent?(variable) ⇒ Boolean Originally defined in module Common

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 a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

.background_codesHash<Symbol => Fixnum> Originally defined in module Colours

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

Returns:

  • (Hash<Symbol => Fixnum>)

.borderString

Return the escape sequence to render a border character.

Yield Returns:

  • (void)

    The border character to wrap with border on and off escape sequences.

Returns:

  • (String)


54
55
56
57
58
# File 'lib/vedeu/esc/esc.rb', line 54

def border
  return '' unless block_given?

  "#{border_on}#{yield}#{border_off}".freeze
end

.border_offString Originally defined in module Borders

Returns:

  • (String)

.border_onString Originally defined in module Borders

Returns:

  • (String)

.charactersHash<Symbol => String> Originally defined in module Borders

Provides all the semigraphic characters.

# 0 1 2 3 4 5 6 7 8 9 A B C D E F 6 ┘ ┐ ┌ └ ┼7 ─ ├ ┤ ┴ ┬ │

Returns:

  • (Hash<Symbol => String>)

.clearString (private)

Returns:

  • (String)


63
64
65
# File 'lib/vedeu/esc/esc.rb', line 63

def clear
  "#{colour_reset}\e[2J".freeze
end

.clear_lineString (private)

Returns:

  • (String)


68
69
70
# File 'lib/vedeu/esc/esc.rb', line 68

def clear_line
  "#{colour_reset}\e[2K".freeze
end

.colour_resetString (private)

Returns:

  • (String)


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

def colour_reset
  Vedeu::Configuration.colour.to_s
end

.define_backgrounds!void Originally defined in module Colours

This method returns an undefined value.

.define_foregrounds!void Originally defined in module Colours

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.

.demodulize(klass) ⇒ String Originally defined in module Common

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.

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

.disable_mouseString (private)

Returns:

  • (String)


78
79
80
81
82
83
84
85
86
# File 'lib/vedeu/esc/esc.rb', line 78

def disable_mouse
  if Vedeu::Configuration.mouse == true
    "#{mouse_x10_off}".freeze

  else
    ''.freeze

  end
end

.enable_mouseString (private)

Returns:

  • (String)


89
90
91
92
93
94
95
96
97
# File 'lib/vedeu/esc/esc.rb', line 89

def enable_mouse
  if Vedeu::Configuration.mouse == true
    "#{mouse_x10_on}".freeze

  else
    ''.freeze

  end
end

.escape(stream = '') ⇒ String

Return the stream with the escape sequences escaped so that they can be printed to the terminal instead of being interpreted by the terminal which will render them. This way we can see what escape sequences are being sent along with the content.

Parameters:

  • stream (String) (defaults to: '')

Returns:

  • (String)


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

def escape(stream = '')
  return stream if absent?(stream)

  stream.gsub(/\e/, '\\e')
end

.foreground_codesHash<Symbol => Fixnum> Also known as: codes Originally defined in module Colours

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

.last_character_positionString (private)

Returns:

  • (String)


121
122
123
# File 'lib/vedeu/esc/esc.rb', line 121

def last_character_position
  Vedeu::Geometry::Position[Vedeu.height, Vedeu.width].to_s
end

.mouse_codesHash<Symbol => String> Originally defined in module Mouse

Returns:

  • (Hash<Symbol => String>)

.normalString (private)

Returns:

  • (String)


100
101
102
# File 'lib/vedeu/esc/esc.rb', line 100

def normal
  "#{underline_off}#{bold_off}#{positive}".freeze
end

.present?(variable) ⇒ Boolean Originally defined in module Common

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 a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

.screen_colour_resetString (private)

Returns:

  • (String)


110
111
112
# File 'lib/vedeu/esc/esc.rb', line 110

def screen_colour_reset
  "#{fg_reset}#{bg_reset}".freeze
end

.screen_exitString (private)

Returns:

  • (String)


115
116
117
118
# File 'lib/vedeu/esc/esc.rb', line 115

def screen_exit
  "#{disable_mouse}#{show_cursor}#{screen_colour_reset}#{reset}" \
  "#{last_character_position}\n".freeze
end

.screen_initString (private)

Returns:

  • (String)


105
106
107
# File 'lib/vedeu/esc/esc.rb', line 105

def screen_init
  "#{reset}#{clear}#{hide_cursor}#{enable_mouse}".freeze
end

.setup!void Originally defined in module Mouse

This method returns an undefined value.

.snake_case(name) ⇒ String Originally defined in module Common

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.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

.string(value = '') ⇒ String

Return the escape sequence string from the list of recognised sequence ‘commands’, or an empty string when the ‘command’ cannot be found.

Parameters:

  • value (String|Symbol) (defaults to: '')

Returns:

  • (String)


41
42
43
44
45
46
47
# File 'lib/vedeu/esc/esc.rb', line 41

def string(value = '')
  return ''.freeze if value.empty?

  send(value)
rescue NoMethodError
  ''.freeze
end

Instance Method Details

#absent?(variable) ⇒ Boolean Originally defined in module Common

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 a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#background_codesHash<Symbol => Fixnum> Originally defined in module Colours

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

Returns:

  • (Hash<Symbol => Fixnum>)

#borderString

Return the escape sequence to render a border character.

Yield Returns:

  • (void)

    The border character to wrap with border on and off escape sequences.

Returns:

  • (String)


54
55
56
57
58
# File 'lib/vedeu/esc/esc.rb', line 54

def border
  return '' unless block_given?

  "#{border_on}#{yield}#{border_off}".freeze
end

#border_offString Originally defined in module Borders

Returns:

  • (String)

#border_onString Originally defined in module Borders

Returns:

  • (String)

#charactersHash<Symbol => String> Originally defined in module Borders

Provides all the semigraphic characters.

# 0 1 2 3 4 5 6 7 8 9 A B C D E F 6 ┘ ┐ ┌ └ ┼7 ─ ├ ┤ ┴ ┬ │

Returns:

  • (Hash<Symbol => String>)

#clearString (private)

Returns:

  • (String)


63
64
65
# File 'lib/vedeu/esc/esc.rb', line 63

def clear
  "#{colour_reset}\e[2J".freeze
end

#clear_lineString (private)

Returns:

  • (String)


68
69
70
# File 'lib/vedeu/esc/esc.rb', line 68

def clear_line
  "#{colour_reset}\e[2K".freeze
end

#colour_resetString (private)

Returns:

  • (String)


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

def colour_reset
  Vedeu::Configuration.colour.to_s
end

#define_backgrounds!void Originally defined in module Colours

This method returns an undefined value.

#define_foregrounds!void Originally defined in module Colours

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.

#demodulize(klass) ⇒ String Originally defined in module Common

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.

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#disable_mouseString (private)

Returns:

  • (String)


78
79
80
81
82
83
84
85
86
# File 'lib/vedeu/esc/esc.rb', line 78

def disable_mouse
  if Vedeu::Configuration.mouse == true
    "#{mouse_x10_off}".freeze

  else
    ''.freeze

  end
end

#enable_mouseString (private)

Returns:

  • (String)


89
90
91
92
93
94
95
96
97
# File 'lib/vedeu/esc/esc.rb', line 89

def enable_mouse
  if Vedeu::Configuration.mouse == true
    "#{mouse_x10_on}".freeze

  else
    ''.freeze

  end
end

#escape(stream = '') ⇒ String

Return the stream with the escape sequences escaped so that they can be printed to the terminal instead of being interpreted by the terminal which will render them. This way we can see what escape sequences are being sent along with the content.

Parameters:

  • stream (String) (defaults to: '')

Returns:

  • (String)


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

def escape(stream = '')
  return stream if absent?(stream)

  stream.gsub(/\e/, '\\e')
end

#foreground_codesHash<Symbol => Fixnum> Also known as: codes Originally defined in module Colours

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

#last_character_positionString (private)

Returns:

  • (String)


121
122
123
# File 'lib/vedeu/esc/esc.rb', line 121

def last_character_position
  Vedeu::Geometry::Position[Vedeu.height, Vedeu.width].to_s
end

#mouse_codesHash<Symbol => String> Originally defined in module Mouse

Returns:

  • (Hash<Symbol => String>)

#normalString (private)

Returns:

  • (String)


100
101
102
# File 'lib/vedeu/esc/esc.rb', line 100

def normal
  "#{underline_off}#{bold_off}#{positive}".freeze
end

#present?(variable) ⇒ Boolean Originally defined in module Common

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 a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#screen_colour_resetString (private)

Returns:

  • (String)


110
111
112
# File 'lib/vedeu/esc/esc.rb', line 110

def screen_colour_reset
  "#{fg_reset}#{bg_reset}".freeze
end

#screen_exitString (private)

Returns:

  • (String)


115
116
117
118
# File 'lib/vedeu/esc/esc.rb', line 115

def screen_exit
  "#{disable_mouse}#{show_cursor}#{screen_colour_reset}#{reset}" \
  "#{last_character_position}\n".freeze
end

#screen_initString (private)

Returns:

  • (String)


105
106
107
# File 'lib/vedeu/esc/esc.rb', line 105

def screen_init
  "#{reset}#{clear}#{hide_cursor}#{enable_mouse}".freeze
end

#setup!void Originally defined in module Mouse

This method returns an undefined value.

#snake_case(name) ⇒ String Originally defined in module Common

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.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

#string(value = '') ⇒ String

Return the escape sequence string from the list of recognised sequence ‘commands’, or an empty string when the ‘command’ cannot be found.

Parameters:

  • value (String|Symbol) (defaults to: '')

Returns:

  • (String)


41
42
43
44
45
46
47
# File 'lib/vedeu/esc/esc.rb', line 41

def string(value = '')
  return ''.freeze if value.empty?

  send(value)
rescue NoMethodError
  ''.freeze
end