Module: Vedeu::Esc

Extended by:
Esc
Included in:
Esc
Defined in:
lib/vedeu/output/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

.background_codesHash

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

Returns:

  • (Hash)


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

def background_codes
  Esc.codes.inject({}) { |h, (k, v)| h.merge!(k => v + 10) }
end

.bg_resetString (private)

Returns:

  • (String)


130
131
132
# File 'lib/vedeu/output/esc.rb', line 130

def bg_reset
  "\e[49m"
end

Returns:

  • (String)


135
136
137
# File 'lib/vedeu/output/esc.rb', line 135

def blink
  "\e[5m"
end

Returns:

  • (String)


140
141
142
# File 'lib/vedeu/output/esc.rb', line 140

def blink_off
  "\e[25m"
end

.boldString (private)

Returns:

  • (String)


145
146
147
# File 'lib/vedeu/output/esc.rb', line 145

def bold
  "\e[1m"
end

.bold_offString (private)

Returns:

  • (String)


150
151
152
# File 'lib/vedeu/output/esc.rb', line 150

def bold_off
  "\e[22m"
end

.border(&block) ⇒ String

Return the escape sequence to render a border character.

Parameters:

  • block (Proc)

Returns:

  • (String)


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

def border(&block)
  return '' unless block_given?

  [ border_on, yield, border_off ].join
end

.border_offString (private)

Returns the escape sequence to end a border.

Returns:

  • (String)


164
165
166
# File 'lib/vedeu/output/esc.rb', line 164

def border_off
  "\e(B"
end

.border_onString (private)

Returns the escape sequence to start a border.

Returns:

  • (String)


157
158
159
# File 'lib/vedeu/output/esc.rb', line 157

def border_on
  "\e(0"
end

.clearString (private)

Returns:

  • (String)


169
170
171
172
173
174
# File 'lib/vedeu/output/esc.rb', line 169

def clear
  [
    colour_reset,
    "\e[2J"
  ].join
end

.clear_last_lineString (private)

Returns:

  • (String)


185
186
187
# File 'lib/vedeu/output/esc.rb', line 185

def clear_last_line
  Vedeu::Position.new((Terminal.height - 1), 1).to_s { clear_line }
end

.clear_lineString (private)

Returns:

  • (String)


177
178
179
180
181
182
# File 'lib/vedeu/output/esc.rb', line 177

def clear_line
  [
    colour_reset,
    "\e[2K"
  ].join
end

.codesHash Also known as: foreground_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:

Esc.red                     # => "\e[31m"

Esc.red { 'some text' }     # => "\e[31msome text\e[39m"

Esc.on_blue                 # => "\e[44m"

Esc.on_blue { 'some text' } # => "\e[44msome text\e[49m"

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


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vedeu/output/esc.rb', line 44

def 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

.colour_resetString (private)

Returns:

  • (String)


190
191
192
# File 'lib/vedeu/output/esc.rb', line 190

def colour_reset
  [fg_reset, bg_reset].join
end

.dimString (private)

Returns:

  • (String)


195
196
197
# File 'lib/vedeu/output/esc.rb', line 195

def dim
  "\e[2m"
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)


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

def escape(stream = '')
  return stream if stream.nil? || stream.empty?

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

.fg_resetString (private)

Returns:

  • (String)


200
201
202
# File 'lib/vedeu/output/esc.rb', line 200

def fg_reset
  "\e[39m"
end

.hide_cursorString (private)

Returns:

  • (String)


205
206
207
# File 'lib/vedeu/output/esc.rb', line 205

def hide_cursor
  "\e[?25l"
end

.keyString

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.

Returns:

  • (String)


79
80
81
82
83
# File 'lib/vedeu/output/esc.rb', line 79

foreground_codes.each do |key, code|
  define_method(key) do |&blk|
    "\e[#{code}m" + (blk ? blk.call + "\e[39m" : '')
  end
end

.negativeString (private)

Returns:

  • (String)


210
211
212
# File 'lib/vedeu/output/esc.rb', line 210

def negative
  "\e[7m"
end

.normalString (private)

Returns:

  • (String)


215
216
217
# File 'lib/vedeu/output/esc.rb', line 215

def normal
  [underline_off, bold_off, positive].join
end

.positiveString (private)

Returns:

  • (String)


220
221
222
# File 'lib/vedeu/output/esc.rb', line 220

def positive
  "\e[27m"
end

.resetString (private)

Returns:

  • (String)


225
226
227
# File 'lib/vedeu/output/esc.rb', line 225

def reset
  "\e[0m"
end

.screen_exitString (private)

Returns:

  • (String)


235
236
237
238
239
240
241
# File 'lib/vedeu/output/esc.rb', line 235

def screen_exit
  [
    show_cursor,
    colour_reset,
    reset
  ].join
end

.screen_initString (private)

Returns:

  • (String)


230
231
232
# File 'lib/vedeu/output/esc.rb', line 230

def screen_init
  [reset, clear, hide_cursor].join
end

.show_cursorString (private)

Returns:

  • (String)


244
245
246
# File 'lib/vedeu/output/esc.rb', line 244

def show_cursor
  "\e[?25h"
end

.string(value = '') ⇒ String

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

Parameters:

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

Returns:

  • (String)


109
110
111
112
113
114
115
# File 'lib/vedeu/output/esc.rb', line 109

def string(value = '')
  name = value.to_sym

  return '' if name.empty?

  send(name) rescue ''
end

.underlineString (private)

Returns:

  • (String)


249
250
251
# File 'lib/vedeu/output/esc.rb', line 249

def underline
  "\e[4m"
end

.underline_offString (private)

Returns:

  • (String)


254
255
256
# File 'lib/vedeu/output/esc.rb', line 254

def underline_off
  "\e[24m"
end

Instance Method Details

#background_codesHash

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

Returns:

  • (Hash)


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

def background_codes
  Esc.codes.inject({}) { |h, (k, v)| h.merge!(k => v + 10) }
end

#bg_resetString (private)

Returns:

  • (String)


130
131
132
# File 'lib/vedeu/output/esc.rb', line 130

def bg_reset
  "\e[49m"
end

Returns:

  • (String)


135
136
137
# File 'lib/vedeu/output/esc.rb', line 135

def blink
  "\e[5m"
end

Returns:

  • (String)


140
141
142
# File 'lib/vedeu/output/esc.rb', line 140

def blink_off
  "\e[25m"
end

#boldString (private)

Returns:

  • (String)


145
146
147
# File 'lib/vedeu/output/esc.rb', line 145

def bold
  "\e[1m"
end

#bold_offString (private)

Returns:

  • (String)


150
151
152
# File 'lib/vedeu/output/esc.rb', line 150

def bold_off
  "\e[22m"
end

#border(&block) ⇒ String

Return the escape sequence to render a border character.

Parameters:

  • block (Proc)

Returns:

  • (String)


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

def border(&block)
  return '' unless block_given?

  [ border_on, yield, border_off ].join
end

#border_offString (private)

Returns the escape sequence to end a border.

Returns:

  • (String)


164
165
166
# File 'lib/vedeu/output/esc.rb', line 164

def border_off
  "\e(B"
end

#border_onString (private)

Returns the escape sequence to start a border.

Returns:

  • (String)


157
158
159
# File 'lib/vedeu/output/esc.rb', line 157

def border_on
  "\e(0"
end

#clearString (private)

Returns:

  • (String)


169
170
171
172
173
174
# File 'lib/vedeu/output/esc.rb', line 169

def clear
  [
    colour_reset,
    "\e[2J"
  ].join
end

#clear_last_lineString (private)

Returns:

  • (String)


185
186
187
# File 'lib/vedeu/output/esc.rb', line 185

def clear_last_line
  Vedeu::Position.new((Terminal.height - 1), 1).to_s { clear_line }
end

#clear_lineString (private)

Returns:

  • (String)


177
178
179
180
181
182
# File 'lib/vedeu/output/esc.rb', line 177

def clear_line
  [
    colour_reset,
    "\e[2K"
  ].join
end

#codesHash Also known as: foreground_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:

Esc.red                     # => "\e[31m"

Esc.red { 'some text' }     # => "\e[31msome text\e[39m"

Esc.on_blue                 # => "\e[44m"

Esc.on_blue { 'some text' } # => "\e[44msome text\e[49m"

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


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vedeu/output/esc.rb', line 44

def 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

#colour_resetString (private)

Returns:

  • (String)


190
191
192
# File 'lib/vedeu/output/esc.rb', line 190

def colour_reset
  [fg_reset, bg_reset].join
end

#dimString (private)

Returns:

  • (String)


195
196
197
# File 'lib/vedeu/output/esc.rb', line 195

def dim
  "\e[2m"
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)


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

def escape(stream = '')
  return stream if stream.nil? || stream.empty?

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

#fg_resetString (private)

Returns:

  • (String)


200
201
202
# File 'lib/vedeu/output/esc.rb', line 200

def fg_reset
  "\e[39m"
end

#hide_cursorString (private)

Returns:

  • (String)


205
206
207
# File 'lib/vedeu/output/esc.rb', line 205

def hide_cursor
  "\e[?25l"
end

#keyString

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.

Returns:

  • (String)


79
80
81
82
83
# File 'lib/vedeu/output/esc.rb', line 79

foreground_codes.each do |key, code|
  define_method(key) do |&blk|
    "\e[#{code}m" + (blk ? blk.call + "\e[39m" : '')
  end
end

#negativeString (private)

Returns:

  • (String)


210
211
212
# File 'lib/vedeu/output/esc.rb', line 210

def negative
  "\e[7m"
end

#normalString (private)

Returns:

  • (String)


215
216
217
# File 'lib/vedeu/output/esc.rb', line 215

def normal
  [underline_off, bold_off, positive].join
end

#positiveString (private)

Returns:

  • (String)


220
221
222
# File 'lib/vedeu/output/esc.rb', line 220

def positive
  "\e[27m"
end

#resetString (private)

Returns:

  • (String)


225
226
227
# File 'lib/vedeu/output/esc.rb', line 225

def reset
  "\e[0m"
end

#screen_exitString (private)

Returns:

  • (String)


235
236
237
238
239
240
241
# File 'lib/vedeu/output/esc.rb', line 235

def screen_exit
  [
    show_cursor,
    colour_reset,
    reset
  ].join
end

#screen_initString (private)

Returns:

  • (String)


230
231
232
# File 'lib/vedeu/output/esc.rb', line 230

def screen_init
  [reset, clear, hide_cursor].join
end

#show_cursorString (private)

Returns:

  • (String)


244
245
246
# File 'lib/vedeu/output/esc.rb', line 244

def show_cursor
  "\e[?25h"
end

#string(value = '') ⇒ String

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

Parameters:

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

Returns:

  • (String)


109
110
111
112
113
114
115
# File 'lib/vedeu/output/esc.rb', line 109

def string(value = '')
  name = value.to_sym

  return '' if name.empty?

  send(name) rescue ''
end

#underlineString (private)

Returns:

  • (String)


249
250
251
# File 'lib/vedeu/output/esc.rb', line 249

def underline
  "\e[4m"
end

#underline_offString (private)

Returns:

  • (String)


254
255
256
# File 'lib/vedeu/output/esc.rb', line 254

def underline_off
  "\e[24m"
end