Module: Vedeu::EscapeSequences::Esc

Extended by:
Esc
Includes:
Common, Actions, Borders, Colours
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

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)


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

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)


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

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

.clear_lineString (private)

Returns:

  • (String)


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

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

.colour_resetString (private)

Returns:

  • (String)


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

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

.define_actions!void (private) Originally defined in module Actions

This method returns an undefined value.

.define_backgrounds!void (private) Originally defined in module Colours

This method returns an undefined value.

.define_borders!void (private) Originally defined in module Borders

This method returns an undefined value.

.define_foregrounds!void (private) 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

Removes the module part from the expression in the string.

Examples:

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

Parameters:

  • klass (Class|String)

Returns:

  • (String)

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


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

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)


93
94
95
# File 'lib/vedeu/esc/esc.rb', line 93

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

.normalString (private)

Returns:

  • (String)


77
78
79
# File 'lib/vedeu/esc/esc.rb', line 77

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

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

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

.screen_exitString (private)

Returns:

  • (String)


87
88
89
90
# File 'lib/vedeu/esc/esc.rb', line 87

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

.screen_initString (private)

Returns:

  • (String)


82
83
84
# File 'lib/vedeu/esc/esc.rb', line 82

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

.setup!void Originally defined in module Colours

This method returns an undefined value.

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

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)


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

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

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)


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

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)


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

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

#clear_lineString (private)

Returns:

  • (String)


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

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

#colour_resetString (private)

Returns:

  • (String)


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

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

#define_actions!void (private) Originally defined in module Actions

This method returns an undefined value.

#define_backgrounds!void (private) Originally defined in module Colours

This method returns an undefined value.

#define_borders!void (private) Originally defined in module Borders

This method returns an undefined value.

#define_foregrounds!void (private) 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

Removes the module part from the expression in the string.

Examples:

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

Parameters:

  • klass (Class|String)

Returns:

  • (String)

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


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

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)


93
94
95
# File 'lib/vedeu/esc/esc.rb', line 93

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

#normalString (private)

Returns:

  • (String)


77
78
79
# File 'lib/vedeu/esc/esc.rb', line 77

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

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

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#screen_exitString (private)

Returns:

  • (String)


87
88
89
90
# File 'lib/vedeu/esc/esc.rb', line 87

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

#screen_initString (private)

Returns:

  • (String)


82
83
84
# File 'lib/vedeu/esc/esc.rb', line 82

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

#setup!void Originally defined in module Colours

This method returns an undefined value.

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

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)


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

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

  send(value)
rescue NoMethodError
  ''.freeze
end