Module: Vedeu::Esc
Overview
Provides escape sequence strings for setting the cursor position and various display related functions.
Class Method Summary collapse
-
.background_codes ⇒ Hash
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
- .bg_reset ⇒ String private
- .blink ⇒ String private
- .blink_off ⇒ String private
- .bold ⇒ String private
- .bold_off ⇒ String private
-
.border(&block) ⇒ String
Return the escape sequence to render a border character.
-
.border_off ⇒ String
private
Returns the escape sequence to end a border.
-
.border_on ⇒ String
private
Returns the escape sequence to start a border.
- .clear ⇒ String private
- .clear_last_line ⇒ String private
- .clear_line ⇒ String private
-
.codes ⇒ Hash
(also: #foreground_codes)
Produces the foreground named colour escape sequence hash.
- .colour_reset ⇒ String private
- .dim ⇒ 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.
- .fg_reset ⇒ String private
- .hide_cursor ⇒ String private
-
.key ⇒ String
Dynamically creates methods for each terminal named colour.
- .negative ⇒ String private
- .normal ⇒ String private
- .positive ⇒ String private
- .reset ⇒ String private
- .screen_exit ⇒ String private
- .screen_init ⇒ String private
- .show_cursor ⇒ 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.
- .underline ⇒ String private
- .underline_off ⇒ String private
Instance Method Summary collapse
-
#background_codes ⇒ Hash
Produces the background named colour escape sequence hash from the foreground escape sequence hash.
- #bg_reset ⇒ String private
- #blink ⇒ String private
- #blink_off ⇒ String private
- #bold ⇒ String private
- #bold_off ⇒ String private
-
#border(&block) ⇒ String
Return the escape sequence to render a border character.
-
#border_off ⇒ String
private
Returns the escape sequence to end a border.
-
#border_on ⇒ String
private
Returns the escape sequence to start a border.
- #clear ⇒ String private
- #clear_last_line ⇒ String private
- #clear_line ⇒ String private
-
#codes ⇒ Hash
(also: #foreground_codes)
Produces the foreground named colour escape sequence hash.
- #colour_reset ⇒ String private
- #dim ⇒ 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.
- #fg_reset ⇒ String private
- #hide_cursor ⇒ String private
-
#key ⇒ String
Dynamically creates methods for each terminal named colour.
- #negative ⇒ String private
- #normal ⇒ String private
- #positive ⇒ String private
- #reset ⇒ String private
- #screen_exit ⇒ String private
- #screen_init ⇒ String private
- #show_cursor ⇒ 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.
- #underline ⇒ String private
- #underline_off ⇒ String private
Class Method Details
.background_codes ⇒ Hash
Produces the background named colour escape sequence hash from the foreground escape sequence 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_reset ⇒ String (private)
130 131 132 |
# File 'lib/vedeu/output/esc.rb', line 130 def bg_reset "\e[49m" end |
.blink ⇒ String (private)
135 136 137 |
# File 'lib/vedeu/output/esc.rb', line 135 def blink "\e[5m" end |
.blink_off ⇒ String (private)
140 141 142 |
# File 'lib/vedeu/output/esc.rb', line 140 def blink_off "\e[25m" end |
.bold ⇒ String (private)
145 146 147 |
# File 'lib/vedeu/output/esc.rb', line 145 def bold "\e[1m" end |
.bold_off ⇒ String (private)
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.
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_off ⇒ String (private)
Returns the escape sequence to end a border.
164 165 166 |
# File 'lib/vedeu/output/esc.rb', line 164 def border_off "\e(B" end |
.border_on ⇒ String (private)
Returns the escape sequence to start a border.
157 158 159 |
# File 'lib/vedeu/output/esc.rb', line 157 def border_on "\e(0" end |
.clear ⇒ String (private)
169 170 171 172 173 174 |
# File 'lib/vedeu/output/esc.rb', line 169 def clear [ colour_reset, "\e[2J" ].join end |
.clear_last_line ⇒ String (private)
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_line ⇒ String (private)
177 178 179 180 181 182 |
# File 'lib/vedeu/output/esc.rb', line 177 def clear_line [ colour_reset, "\e[2K" ].join end |
.codes ⇒ Hash 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:
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_reset ⇒ String (private)
190 191 192 |
# File 'lib/vedeu/output/esc.rb', line 190 def colour_reset [fg_reset, bg_reset].join end |
.dim ⇒ String (private)
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.
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_reset ⇒ String (private)
200 201 202 |
# File 'lib/vedeu/output/esc.rb', line 200 def fg_reset "\e[39m" end |
.hide_cursor ⇒ String (private)
205 206 207 |
# File 'lib/vedeu/output/esc.rb', line 205 def hide_cursor "\e[?25l" 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.
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 |
.negative ⇒ String (private)
210 211 212 |
# File 'lib/vedeu/output/esc.rb', line 210 def negative "\e[7m" end |
.normal ⇒ String (private)
215 216 217 |
# File 'lib/vedeu/output/esc.rb', line 215 def normal [underline_off, bold_off, positive].join end |
.positive ⇒ String (private)
220 221 222 |
# File 'lib/vedeu/output/esc.rb', line 220 def positive "\e[27m" end |
.reset ⇒ String (private)
225 226 227 |
# File 'lib/vedeu/output/esc.rb', line 225 def reset "\e[0m" end |
.screen_exit ⇒ String (private)
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_init ⇒ String (private)
230 231 232 |
# File 'lib/vedeu/output/esc.rb', line 230 def screen_init [reset, clear, hide_cursor].join end |
.show_cursor ⇒ String (private)
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.
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 |
.underline ⇒ String (private)
249 250 251 |
# File 'lib/vedeu/output/esc.rb', line 249 def underline "\e[4m" end |
.underline_off ⇒ String (private)
254 255 256 |
# File 'lib/vedeu/output/esc.rb', line 254 def underline_off "\e[24m" end |
Instance Method Details
#background_codes ⇒ Hash
Produces the background named colour escape sequence hash from the foreground escape sequence 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_reset ⇒ String (private)
130 131 132 |
# File 'lib/vedeu/output/esc.rb', line 130 def bg_reset "\e[49m" end |
#blink ⇒ String (private)
135 136 137 |
# File 'lib/vedeu/output/esc.rb', line 135 def blink "\e[5m" end |
#blink_off ⇒ String (private)
140 141 142 |
# File 'lib/vedeu/output/esc.rb', line 140 def blink_off "\e[25m" end |
#bold ⇒ String (private)
145 146 147 |
# File 'lib/vedeu/output/esc.rb', line 145 def bold "\e[1m" end |
#bold_off ⇒ String (private)
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.
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_off ⇒ String (private)
Returns the escape sequence to end a border.
164 165 166 |
# File 'lib/vedeu/output/esc.rb', line 164 def border_off "\e(B" end |
#border_on ⇒ String (private)
Returns the escape sequence to start a border.
157 158 159 |
# File 'lib/vedeu/output/esc.rb', line 157 def border_on "\e(0" end |
#clear ⇒ String (private)
169 170 171 172 173 174 |
# File 'lib/vedeu/output/esc.rb', line 169 def clear [ colour_reset, "\e[2J" ].join end |
#clear_last_line ⇒ String (private)
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_line ⇒ String (private)
177 178 179 180 181 182 |
# File 'lib/vedeu/output/esc.rb', line 177 def clear_line [ colour_reset, "\e[2K" ].join end |
#codes ⇒ Hash 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:
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_reset ⇒ String (private)
190 191 192 |
# File 'lib/vedeu/output/esc.rb', line 190 def colour_reset [fg_reset, bg_reset].join end |
#dim ⇒ String (private)
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.
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_reset ⇒ String (private)
200 201 202 |
# File 'lib/vedeu/output/esc.rb', line 200 def fg_reset "\e[39m" end |
#hide_cursor ⇒ String (private)
205 206 207 |
# File 'lib/vedeu/output/esc.rb', line 205 def hide_cursor "\e[?25l" 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.
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 |
#negative ⇒ String (private)
210 211 212 |
# File 'lib/vedeu/output/esc.rb', line 210 def negative "\e[7m" end |
#normal ⇒ String (private)
215 216 217 |
# File 'lib/vedeu/output/esc.rb', line 215 def normal [underline_off, bold_off, positive].join end |
#positive ⇒ String (private)
220 221 222 |
# File 'lib/vedeu/output/esc.rb', line 220 def positive "\e[27m" end |
#reset ⇒ String (private)
225 226 227 |
# File 'lib/vedeu/output/esc.rb', line 225 def reset "\e[0m" end |
#screen_exit ⇒ String (private)
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_init ⇒ String (private)
230 231 232 |
# File 'lib/vedeu/output/esc.rb', line 230 def screen_init [reset, clear, hide_cursor].join end |
#show_cursor ⇒ String (private)
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.
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 |
#underline ⇒ String (private)
249 250 251 |
# File 'lib/vedeu/output/esc.rb', line 249 def underline "\e[4m" end |
#underline_off ⇒ String (private)
254 255 256 |
# File 'lib/vedeu/output/esc.rb', line 254 def underline_off "\e[24m" end |