Module: Vedeu::Esc
Overview
Provides escape sequence strings for setting the cursor position and various display related functions.
Class Method Summary collapse
- .actions ⇒ Hash<Symbol => String>
-
.background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
-
.border ⇒ String
Return the escape sequence to render a border character.
- .clear ⇒ String private
- .clear_last_line ⇒ String private
- .clear_line ⇒ String private
-
.codes ⇒ Hash<Symbol => Fixnum>
(also: #foreground_codes)
Produces the foreground named colour escape sequence hash.
- .colour_reset ⇒ String private
-
.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.
-
.key ⇒ String
Dynamically creates methods for each terminal named colour.
- .normal ⇒ String private
- .screen_exit ⇒ String private
- .screen_init ⇒ String private
-
.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.
Instance Method Summary collapse
- #actions ⇒ Hash<Symbol => String>
-
#background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
-
#border ⇒ String
Return the escape sequence to render a border character.
- #clear ⇒ String private
- #clear_last_line ⇒ String private
- #clear_line ⇒ String private
-
#codes ⇒ Hash<Symbol => Fixnum>
(also: #foreground_codes)
Produces the foreground named colour escape sequence hash.
- #colour_reset ⇒ String private
-
#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.
-
#key ⇒ String
Dynamically creates methods for each terminal named colour.
- #normal ⇒ String private
- #screen_exit ⇒ String private
- #screen_init ⇒ String private
-
#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.
Class Method Details
.actions ⇒ Hash<Symbol => String>
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/vedeu/output/esc.rb', line 78 def actions { hide_cursor: "\e[?25l", show_cursor: "\e[?25h", bg_reset: "\e[49m", blink: "\e[5m", blink_off: "\e[25m", bold: "\e[1m", bold_off: "\e[22m", border_on: "\e(0", border_off: "\e(B", dim: "\e[2m", fg_reset: "\e[39m", negative: "\e[7m", positive: "\e[27m", reset: "\e[0m", underline: "\e[4m", underline_off: "\e[24m", } end |
.background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
57 58 59 |
# File 'lib/vedeu/output/esc.rb', line 57 def background_codes Vedeu::Esc.codes.inject({}) { |h, (k, v)| h.merge!(k => v + 10) } end |
.border ⇒ String
Return the escape sequence to render a border character.
137 138 139 140 141 |
# File 'lib/vedeu/output/esc.rb', line 137 def border return '' unless block_given? [border_on, yield, border_off].join end |
.clear ⇒ String (private)
146 147 148 |
# File 'lib/vedeu/output/esc.rb', line 146 def clear [colour_reset, "\e[2J"].join end |
.clear_last_line ⇒ String (private)
156 157 158 |
# File 'lib/vedeu/output/esc.rb', line 156 def clear_last_line Vedeu::Position.new((Vedeu::Terminal.height - 1), 1).to_s { clear_line } end |
.clear_line ⇒ String (private)
151 152 153 |
# File 'lib/vedeu/output/esc.rb', line 151 def clear_line [colour_reset, "\e[2K"].join end |
.codes ⇒ Hash<Symbol => Fixnum> 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:
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/vedeu/output/esc.rb', line 30 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_reset ⇒ String (private)
161 162 163 |
# File 'lib/vedeu/output/esc.rb', line 161 def colour_reset [fg_reset, bg_reset].join 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.
110 111 112 113 114 |
# File 'lib/vedeu/output/esc.rb', line 110 def escape(stream = '') return stream if stream.nil? || stream.empty? stream.gsub(/\e/, '\\e') end |
.key ⇒ String
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.
65 66 67 68 69 |
# File 'lib/vedeu/output/esc.rb', line 65 foreground_codes.each do |key, code| define_method(key) do |&blk| "\e[#{code}m" + (blk ? blk.call + "\e[39m" : '') end end |
.normal ⇒ String (private)
166 167 168 |
# File 'lib/vedeu/output/esc.rb', line 166 def normal [underline_off, bold_off, positive].join end |
.screen_exit ⇒ String (private)
176 177 178 |
# File 'lib/vedeu/output/esc.rb', line 176 def screen_exit [show_cursor, colour_reset, reset].join end |
.screen_init ⇒ String (private)
171 172 173 |
# File 'lib/vedeu/output/esc.rb', line 171 def screen_init [reset, clear, hide_cursor].join 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.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/vedeu/output/esc.rb', line 121 def string(value = '') name = value.to_sym return '' if name.empty? send(name) rescue NoMethodError '' end |
Instance Method Details
#actions ⇒ Hash<Symbol => String>
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/vedeu/output/esc.rb', line 78 def actions { hide_cursor: "\e[?25l", show_cursor: "\e[?25h", bg_reset: "\e[49m", blink: "\e[5m", blink_off: "\e[25m", bold: "\e[1m", bold_off: "\e[22m", border_on: "\e(0", border_off: "\e(B", dim: "\e[2m", fg_reset: "\e[39m", negative: "\e[7m", positive: "\e[27m", reset: "\e[0m", underline: "\e[4m", underline_off: "\e[24m", } end |
#background_codes ⇒ Hash<Symbol => Fixnum>
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
57 58 59 |
# File 'lib/vedeu/output/esc.rb', line 57 def background_codes Vedeu::Esc.codes.inject({}) { |h, (k, v)| h.merge!(k => v + 10) } end |
#border ⇒ String
Return the escape sequence to render a border character.
137 138 139 140 141 |
# File 'lib/vedeu/output/esc.rb', line 137 def border return '' unless block_given? [border_on, yield, border_off].join end |
#clear ⇒ String (private)
146 147 148 |
# File 'lib/vedeu/output/esc.rb', line 146 def clear [colour_reset, "\e[2J"].join end |
#clear_last_line ⇒ String (private)
156 157 158 |
# File 'lib/vedeu/output/esc.rb', line 156 def clear_last_line Vedeu::Position.new((Vedeu::Terminal.height - 1), 1).to_s { clear_line } end |
#clear_line ⇒ String (private)
151 152 153 |
# File 'lib/vedeu/output/esc.rb', line 151 def clear_line [colour_reset, "\e[2K"].join end |
#codes ⇒ Hash<Symbol => Fixnum> 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:
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/vedeu/output/esc.rb', line 30 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_reset ⇒ String (private)
161 162 163 |
# File 'lib/vedeu/output/esc.rb', line 161 def colour_reset [fg_reset, bg_reset].join 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.
110 111 112 113 114 |
# File 'lib/vedeu/output/esc.rb', line 110 def escape(stream = '') return stream if stream.nil? || stream.empty? stream.gsub(/\e/, '\\e') end |
#key ⇒ String
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.
65 66 67 68 69 |
# File 'lib/vedeu/output/esc.rb', line 65 foreground_codes.each do |key, code| define_method(key) do |&blk| "\e[#{code}m" + (blk ? blk.call + "\e[39m" : '') end end |
#normal ⇒ String (private)
166 167 168 |
# File 'lib/vedeu/output/esc.rb', line 166 def normal [underline_off, bold_off, positive].join end |
#screen_exit ⇒ String (private)
176 177 178 |
# File 'lib/vedeu/output/esc.rb', line 176 def screen_exit [show_cursor, colour_reset, reset].join end |
#screen_init ⇒ String (private)
171 172 173 |
# File 'lib/vedeu/output/esc.rb', line 171 def screen_init [reset, clear, hide_cursor].join 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.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/vedeu/output/esc.rb', line 121 def string(value = '') name = value.to_sym return '' if name.empty? send(name) rescue NoMethodError '' end |