Module: Vedeu::Terminal
- Extended by:
- Terminal
- Includes:
- Mode
- Included in:
- Terminal
- Defined in:
- lib/vedeu/terminal/all.rb,
lib/vedeu/terminal/mode.rb,
lib/vedeu/terminal/buffer.rb,
lib/vedeu/terminal/terminal.rb
Overview
This module is the direct interface between Vedeu and your terminal/ console, via Ruby’s IO core library.
Defined Under Namespace
Class Method Summary collapse
-
.centre ⇒ Array
Returns a coordinate tuple of the format [y, x], where ‘y` is the row/line and `x` is the column/character.
-
.centre_x ⇒ Fixnum
Returns the ‘x` (column/character) component of the coodinate tuple provided by centre.
-
.centre_y ⇒ Fixnum
Returns the ‘y` (row/line) component of the coordinate tuple provided by centre.
-
.clear ⇒ String
Clears the entire terminal space.
-
.console ⇒ File
Provides our gateway into the wonderful rainbow-filled world of IO.
-
.cooked_mode! ⇒ Symbol
extended
from Mode
Sets the terminal in to ‘cooked` mode.
-
.cooked_mode? ⇒ Boolean
extended
from Mode
Returns a boolean indicating whether the terminal is currently in ‘cooked` mode.
-
.fake_mode! ⇒ Symbol
extended
from Mode
Sets the terminal in to ‘fake` mode.
-
.fake_mode? ⇒ Boolean
extended
from Mode
Returns a boolean indicating whether the terminal is currently in ‘fake` mode.
-
.height ⇒ Fixnum
(also: #yn, #tyn)
Returns the total height (number of rows/lines) of the current terminal.
- .initialize_screen(mode) ⇒ void
-
.input ⇒ String
(also: #read)
Takes input from the user via the keyboard.
-
.mode ⇒ Symbol
extended
from Mode
Returns the mode of the terminal, either ‘:cooked`, `:fake` or `:raw`.
-
.open ⇒ Array
Opens a terminal screen in either ‘raw` or `cooked` mode.
-
.origin ⇒ Fixnum
(also: #x, #y, #tx, #ty)
Returns 1.
-
.output(*streams) ⇒ Array
(also: #write)
Prints the streams to the screen and returns the streams.
-
.raw_mode! ⇒ Symbol
extended
from Mode
Sets the terminal in to ‘raw` mode.
-
.raw_mode? ⇒ Boolean
extended
from Mode
Returns a boolean indicating whether the terminal is currently in ‘raw` mode.
-
.resize ⇒ TrueClass
When the terminal emit the ‘SIGWINCH’ signal, Vedeu can intercept this and attempt to redraw the current interface with varying degrees of success.
-
.restore_screen ⇒ String
Attempts to tidy up the screen just before the application terminates.
-
.set_cursor_mode ⇒ String
Sets the cursor to be visible unless in raw mode, whereby it will be left hidden.
-
.size ⇒ Array
Returns a tuple containing the height and width of the current terminal.
-
.switch_mode! ⇒ Symbol
extended
from Mode
Toggles the terminal’s mode between ‘cooked`, `fake` and `raw`, depending on its current mode.
-
.width ⇒ Fixnum
(also: #xn, #txn)
Returns the total width (number of columns/characters) of the current terminal.
Instance Method Summary collapse
-
#centre ⇒ Array
Returns a coordinate tuple of the format [y, x], where ‘y` is the row/line and `x` is the column/character.
-
#centre_x ⇒ Fixnum
Returns the ‘x` (column/character) component of the coodinate tuple provided by centre.
-
#centre_y ⇒ Fixnum
Returns the ‘y` (row/line) component of the coordinate tuple provided by centre.
-
#clear ⇒ String
Clears the entire terminal space.
-
#console ⇒ File
Provides our gateway into the wonderful rainbow-filled world of IO.
-
#cooked_mode! ⇒ Symbol
included
from Mode
Sets the terminal in to ‘cooked` mode.
-
#cooked_mode? ⇒ Boolean
included
from Mode
Returns a boolean indicating whether the terminal is currently in ‘cooked` mode.
-
#fake_mode! ⇒ Symbol
included
from Mode
Sets the terminal in to ‘fake` mode.
-
#fake_mode? ⇒ Boolean
included
from Mode
Returns a boolean indicating whether the terminal is currently in ‘fake` mode.
-
#height ⇒ Fixnum
(also: #yn, #tyn)
Returns the total height (number of rows/lines) of the current terminal.
- #initialize_screen(mode) ⇒ void
-
#input ⇒ String
(also: #read)
Takes input from the user via the keyboard.
-
#mode ⇒ Symbol
included
from Mode
Returns the mode of the terminal, either ‘:cooked`, `:fake` or `:raw`.
-
#open ⇒ Array
Opens a terminal screen in either ‘raw` or `cooked` mode.
-
#origin ⇒ Fixnum
(also: #x, #y, #tx, #ty)
Returns 1.
-
#output(*streams) ⇒ Array
(also: #write)
Prints the streams to the screen and returns the streams.
-
#raw_mode! ⇒ Symbol
included
from Mode
Sets the terminal in to ‘raw` mode.
-
#raw_mode? ⇒ Boolean
included
from Mode
Returns a boolean indicating whether the terminal is currently in ‘raw` mode.
-
#resize ⇒ TrueClass
When the terminal emit the ‘SIGWINCH’ signal, Vedeu can intercept this and attempt to redraw the current interface with varying degrees of success.
-
#restore_screen ⇒ String
Attempts to tidy up the screen just before the application terminates.
-
#set_cursor_mode ⇒ String
Sets the cursor to be visible unless in raw mode, whereby it will be left hidden.
-
#size ⇒ Array
Returns a tuple containing the height and width of the current terminal.
-
#switch_mode! ⇒ Symbol
included
from Mode
Toggles the terminal’s mode between ‘cooked`, `fake` and `raw`, depending on its current mode.
-
#width ⇒ Fixnum
(also: #xn, #txn)
Returns the total width (number of columns/characters) of the current terminal.
Class Method Details
.centre ⇒ Array
Returns a coordinate tuple of the format [y, x], where ‘y` is the row/line and `x` is the column/character.
117 118 119 |
# File 'lib/vedeu/terminal/terminal.rb', line 117 def centre [(height / 2), (width / 2)] end |
.centre_x ⇒ Fixnum
Returns the ‘x` (column/character) component of the coodinate tuple provided by centre
133 134 135 |
# File 'lib/vedeu/terminal/terminal.rb', line 133 def centre_x centre[-1] end |
.centre_y ⇒ Fixnum
Returns the ‘y` (row/line) component of the coordinate tuple provided by centre
125 126 127 |
# File 'lib/vedeu/terminal/terminal.rb', line 125 def centre_y centre[0] end |
.clear ⇒ String
Clears the entire terminal space.
91 92 93 |
# File 'lib/vedeu/terminal/terminal.rb', line 91 def clear output(Vedeu::EscapeSequences::Esc.string('clear')) end |
.console ⇒ File
Provides our gateway into the wonderful rainbow-filled world of IO.
213 214 215 |
# File 'lib/vedeu/terminal/terminal.rb', line 213 def console IO.console end |
.cooked_mode! ⇒ Symbol Originally defined in module Mode
Sets the terminal in to ‘cooked` mode.
.cooked_mode? ⇒ Boolean Originally defined in module Mode
Returns a boolean indicating whether the terminal is currently in ‘cooked` mode.
.fake_mode! ⇒ Symbol Originally defined in module Mode
Sets the terminal in to ‘fake` mode.
.fake_mode? ⇒ Boolean Originally defined in module Mode
Returns a boolean indicating whether the terminal is currently in ‘fake` mode.
.height ⇒ Fixnum Also known as: yn, tyn
Returns the total height (number of rows/lines) of the current terminal.
172 173 174 175 176 177 |
# File 'lib/vedeu/terminal/terminal.rb', line 172 def height return Vedeu::Configuration.drb_height if Vedeu::Configuration.drb? return Vedeu::Configuration.height if Vedeu::Configuration.height size[0] end |
.initialize_screen(mode) ⇒ void
This method returns an undefined value.
77 78 79 80 81 82 83 |
# File 'lib/vedeu/terminal/terminal.rb', line 77 def initialize_screen(mode) Vedeu.log(message: "Terminal entering '#{mode}' mode".freeze) output(Vedeu::EscapeSequences::Esc.string('screen_init')) yield if block_given? end |
.input ⇒ String Also known as: read
Takes input from the user via the keyboard. Accepts special keys like the F-Keys etc, by capturing the entire sequence.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vedeu/terminal/terminal.rb', line 36 def input Vedeu.log(type: :input, message: 'Waiting for user input...'.freeze) if raw_mode? || fake_mode? Vedeu::Editor::Capture.read(console) else console.gets.chomp end end |
.mode ⇒ Symbol Originally defined in module Mode
Returns the mode of the terminal, either ‘:cooked`, `:fake` or `:raw`. Can change throughout the lifespan of the client application.
.open ⇒ Array
Opens a terminal screen in either ‘raw` or `cooked` mode. On exit, attempts to restore the screen. See #restore_screen.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/vedeu/terminal/terminal.rb', line 18 def open fail Vedeu::Error::RequiresBlock unless block_given? if raw_mode? || fake_mode? console.raw { initialize_screen(mode) { yield } } else console.cooked { initialize_screen(mode) { yield } } end ensure restore_screen end |
.origin ⇒ Fixnum Also known as: x, y, tx, ty
Returns 1. This 1 is either the top-most or left-most coordinate of the terminal.
141 142 143 |
# File 'lib/vedeu/terminal/terminal.rb', line 141 def origin 1 end |
.output(*streams) ⇒ Array Also known as: write
Prints the streams to the screen and returns the streams.
53 54 55 |
# File 'lib/vedeu/terminal/terminal.rb', line 53 def output(*streams) streams.each { |stream| console.print(stream) } end |
.raw_mode! ⇒ Symbol Originally defined in module Mode
Sets the terminal in to ‘raw` mode.
.raw_mode? ⇒ Boolean Originally defined in module Mode
Returns a boolean indicating whether the terminal is currently in ‘raw` mode.
.resize ⇒ TrueClass
When the terminal emit the ‘SIGWINCH’ signal, Vedeu can intercept this and attempt to redraw the current interface with varying degrees of success. Can also be used to simulate a terminal resize.
67 68 69 70 71 72 73 |
# File 'lib/vedeu/terminal/terminal.rb', line 67 def resize Vedeu.trigger(:_clear_) Vedeu.trigger(:_refresh_) true end |
.restore_screen ⇒ String
Attempts to tidy up the screen just before the application terminates. The cursor is shown, colours are reset to terminal defaults, the terminal is told to reset, and finally we clear the last line ready for the prompt.
101 102 103 |
# File 'lib/vedeu/terminal/terminal.rb', line 101 def restore_screen output(Vedeu::EscapeSequences::Esc.string('screen_exit')) end |
.set_cursor_mode ⇒ String
Sets the cursor to be visible unless in raw mode, whereby it will be left hidden.
109 110 111 |
# File 'lib/vedeu/terminal/terminal.rb', line 109 def set_cursor_mode output(Vedeu::EscapeSequences::Esc.string('show_cursor')) unless raw_mode? end |
.size ⇒ Array
If the terminal is a odd number of characters in height or width, then 1 is deducted from the dimension to make it even. For example; the actual terminal is height: 37, width: 145, then the reported size will be 36, 144 respectively.
This is done to make it easier for client applications to divide the terminal space up when defining interfaces or views, leading to more consistent rendering.
If the client application is using the Geometry::Grid#rows or Geometry::Grid#columns helpers, the dimensions are made more consistent using this approach.
Returns a tuple containing the height and width of the current terminal.
200 201 202 203 204 205 206 207 |
# File 'lib/vedeu/terminal/terminal.rb', line 200 def size h, w = console.winsize h = (h.even? ? h : h - 1) w = (w.even? ? w : w - 1) [h, w] end |
.switch_mode! ⇒ Symbol Originally defined in module Mode
Toggles the terminal’s mode between ‘cooked`, `fake` and `raw`, depending on its current mode.
.width ⇒ Fixnum Also known as: xn, txn
Returns the total width (number of columns/characters) of the current terminal.
156 157 158 159 160 161 |
# File 'lib/vedeu/terminal/terminal.rb', line 156 def width return Vedeu::Configuration.drb_width if Vedeu::Configuration.drb? return Vedeu::Configuration.width if Vedeu::Configuration.width size[-1] end |
Instance Method Details
#centre ⇒ Array
Returns a coordinate tuple of the format [y, x], where ‘y` is the row/line and `x` is the column/character.
117 118 119 |
# File 'lib/vedeu/terminal/terminal.rb', line 117 def centre [(height / 2), (width / 2)] end |
#centre_x ⇒ Fixnum
Returns the ‘x` (column/character) component of the coodinate tuple provided by centre
133 134 135 |
# File 'lib/vedeu/terminal/terminal.rb', line 133 def centre_x centre[-1] end |
#centre_y ⇒ Fixnum
Returns the ‘y` (row/line) component of the coordinate tuple provided by centre
125 126 127 |
# File 'lib/vedeu/terminal/terminal.rb', line 125 def centre_y centre[0] end |
#clear ⇒ String
Clears the entire terminal space.
91 92 93 |
# File 'lib/vedeu/terminal/terminal.rb', line 91 def clear output(Vedeu::EscapeSequences::Esc.string('clear')) end |
#console ⇒ File
Provides our gateway into the wonderful rainbow-filled world of IO.
213 214 215 |
# File 'lib/vedeu/terminal/terminal.rb', line 213 def console IO.console end |
#cooked_mode! ⇒ Symbol Originally defined in module Mode
Sets the terminal in to ‘cooked` mode.
#cooked_mode? ⇒ Boolean Originally defined in module Mode
Returns a boolean indicating whether the terminal is currently in ‘cooked` mode.
#fake_mode! ⇒ Symbol Originally defined in module Mode
Sets the terminal in to ‘fake` mode.
#fake_mode? ⇒ Boolean Originally defined in module Mode
Returns a boolean indicating whether the terminal is currently in ‘fake` mode.
#height ⇒ Fixnum Also known as: yn, tyn
Returns the total height (number of rows/lines) of the current terminal.
172 173 174 175 176 177 |
# File 'lib/vedeu/terminal/terminal.rb', line 172 def height return Vedeu::Configuration.drb_height if Vedeu::Configuration.drb? return Vedeu::Configuration.height if Vedeu::Configuration.height size[0] end |
#initialize_screen(mode) ⇒ void
This method returns an undefined value.
77 78 79 80 81 82 83 |
# File 'lib/vedeu/terminal/terminal.rb', line 77 def initialize_screen(mode) Vedeu.log(message: "Terminal entering '#{mode}' mode".freeze) output(Vedeu::EscapeSequences::Esc.string('screen_init')) yield if block_given? end |
#input ⇒ String Also known as: read
Takes input from the user via the keyboard. Accepts special keys like the F-Keys etc, by capturing the entire sequence.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/vedeu/terminal/terminal.rb', line 36 def input Vedeu.log(type: :input, message: 'Waiting for user input...'.freeze) if raw_mode? || fake_mode? Vedeu::Editor::Capture.read(console) else console.gets.chomp end end |
#mode ⇒ Symbol Originally defined in module Mode
Returns the mode of the terminal, either ‘:cooked`, `:fake` or `:raw`. Can change throughout the lifespan of the client application.
#open ⇒ Array
Opens a terminal screen in either ‘raw` or `cooked` mode. On exit, attempts to restore the screen. See #restore_screen.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/vedeu/terminal/terminal.rb', line 18 def open fail Vedeu::Error::RequiresBlock unless block_given? if raw_mode? || fake_mode? console.raw { initialize_screen(mode) { yield } } else console.cooked { initialize_screen(mode) { yield } } end ensure restore_screen end |
#origin ⇒ Fixnum Also known as: x, y, tx, ty
Returns 1. This 1 is either the top-most or left-most coordinate of the terminal.
141 142 143 |
# File 'lib/vedeu/terminal/terminal.rb', line 141 def origin 1 end |
#output(*streams) ⇒ Array Also known as: write
Prints the streams to the screen and returns the streams.
53 54 55 |
# File 'lib/vedeu/terminal/terminal.rb', line 53 def output(*streams) streams.each { |stream| console.print(stream) } end |
#raw_mode! ⇒ Symbol Originally defined in module Mode
Sets the terminal in to ‘raw` mode.
#raw_mode? ⇒ Boolean Originally defined in module Mode
Returns a boolean indicating whether the terminal is currently in ‘raw` mode.
#resize ⇒ TrueClass
When the terminal emit the ‘SIGWINCH’ signal, Vedeu can intercept this and attempt to redraw the current interface with varying degrees of success. Can also be used to simulate a terminal resize.
67 68 69 70 71 72 73 |
# File 'lib/vedeu/terminal/terminal.rb', line 67 def resize Vedeu.trigger(:_clear_) Vedeu.trigger(:_refresh_) true end |
#restore_screen ⇒ String
Attempts to tidy up the screen just before the application terminates. The cursor is shown, colours are reset to terminal defaults, the terminal is told to reset, and finally we clear the last line ready for the prompt.
101 102 103 |
# File 'lib/vedeu/terminal/terminal.rb', line 101 def restore_screen output(Vedeu::EscapeSequences::Esc.string('screen_exit')) end |
#set_cursor_mode ⇒ String
Sets the cursor to be visible unless in raw mode, whereby it will be left hidden.
109 110 111 |
# File 'lib/vedeu/terminal/terminal.rb', line 109 def set_cursor_mode output(Vedeu::EscapeSequences::Esc.string('show_cursor')) unless raw_mode? end |
#size ⇒ Array
If the terminal is a odd number of characters in height or width, then 1 is deducted from the dimension to make it even. For example; the actual terminal is height: 37, width: 145, then the reported size will be 36, 144 respectively.
This is done to make it easier for client applications to divide the terminal space up when defining interfaces or views, leading to more consistent rendering.
If the client application is using the Geometry::Grid#rows or Geometry::Grid#columns helpers, the dimensions are made more consistent using this approach.
Returns a tuple containing the height and width of the current terminal.
200 201 202 203 204 205 206 207 |
# File 'lib/vedeu/terminal/terminal.rb', line 200 def size h, w = console.winsize h = (h.even? ? h : h - 1) w = (w.even? ? w : w - 1) [h, w] end |
#switch_mode! ⇒ Symbol Originally defined in module Mode
Toggles the terminal’s mode between ‘cooked`, `fake` and `raw`, depending on its current mode.
#width ⇒ Fixnum Also known as: xn, txn
Returns the total width (number of columns/characters) of the current terminal.
156 157 158 159 160 161 |
# File 'lib/vedeu/terminal/terminal.rb', line 156 def width return Vedeu::Configuration.drb_width if Vedeu::Configuration.drb? return Vedeu::Configuration.width if Vedeu::Configuration.width size[-1] end |