Module: Vedeu::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_last_line ⇒ String
- #clear_screen ⇒ String
- #console ⇒ File
- #cooked_mode! ⇒ Symbol
- #cooked_mode? ⇒ Boolean
-
#height ⇒ Fixnum
Returns the total height (number of rows/lines) of the current terminal.
-
#initialize_screen(&block) ⇒ Object
[].
- #input ⇒ String
-
#mode ⇒ Symbol
Returns the mode of the terminal, either ‘:raw` or `:cooked`.
-
#open(&block) ⇒ Object
[].
- #output(stream = '') ⇒ String
- #raw_mode! ⇒ Symbol
- #raw_mode? ⇒ Boolean
- #restore_screen ⇒ String
- #set_cursor_mode ⇒ String
-
#size ⇒ Array
Returns a tuple containing the height and width of the current terminal.
- #switch_mode! ⇒ Symbol
-
#width ⇒ Fixnum
Returns the total width (number of columns/characters) of the current terminal.
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.
118 119 120 |
# File 'lib/vedeu/support/terminal.rb', line 118 def centre [(height / 2), (width / 2)] end |
#centre_x ⇒ Fixnum
Returns the ‘x` (column/character) component of the coodinate tuple provided by #centre
134 135 136 |
# File 'lib/vedeu/support/terminal.rb', line 134 def centre_x centre.last end |
#centre_y ⇒ Fixnum
Returns the ‘y` (row/line) component of the coordinate tuple provided by #centre
126 127 128 |
# File 'lib/vedeu/support/terminal.rb', line 126 def centre_y centre.first end |
#clear_last_line ⇒ String
103 104 105 |
# File 'lib/vedeu/support/terminal.rb', line 103 def clear_last_line Esc.set_position((height - 1), 1) + Esc.string('clear_line') end |
#clear_screen ⇒ String
56 57 58 |
# File 'lib/vedeu/support/terminal.rb', line 56 def clear_screen output Esc.string 'clear' end |
#console ⇒ File
161 162 163 |
# File 'lib/vedeu/support/terminal.rb', line 161 def console IO.console end |
#cooked_mode! ⇒ Symbol
77 78 79 |
# File 'lib/vedeu/support/terminal.rb', line 77 def cooked_mode! @_mode = :cooked end |
#cooked_mode? ⇒ Boolean
72 73 74 |
# File 'lib/vedeu/support/terminal.rb', line 72 def cooked_mode? mode == :cooked end |
#height ⇒ Fixnum
Returns the total height (number of rows/lines) of the current terminal.
149 150 151 |
# File 'lib/vedeu/support/terminal.rb', line 149 def height size.first end |
#initialize_screen(&block) ⇒ Object
Returns [].
49 50 51 52 53 |
# File 'lib/vedeu/support/terminal.rb', line 49 def initialize_screen(&block) output Esc.string 'screen_init' yield end |
#input ⇒ String
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/vedeu/support/terminal.rb', line 24 def input if raw_mode? keys = console.getch if keys.ord == 27 keys << console.read_nonblock(3) rescue nil keys << console.read_nonblock(2) rescue nil end keys else console.gets.chomp end end |
#mode ⇒ Symbol
Returns the mode of the terminal, either ‘:raw` or `:cooked`
110 111 112 |
# File 'lib/vedeu/support/terminal.rb', line 110 def mode @_mode ||= Configuration.terminal_mode end |
#open(&block) ⇒ Object
Returns [].
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/vedeu/support/terminal.rb', line 8 def open(&block) fail InvalidSyntax, '`open` requires a block.' unless block_given? if raw_mode? console.raw { initialize_screen { yield } } else console.cooked { initialize_screen { yield } } end ensure restore_screen end |
#output(stream = '') ⇒ String
41 42 43 44 45 |
# File 'lib/vedeu/support/terminal.rb', line 41 def output(stream = '') console.print(stream) stream end |
#raw_mode! ⇒ Symbol
87 88 89 |
# File 'lib/vedeu/support/terminal.rb', line 87 def raw_mode! @_mode = :raw end |
#raw_mode? ⇒ Boolean
82 83 84 |
# File 'lib/vedeu/support/terminal.rb', line 82 def raw_mode? mode == :raw end |
#restore_screen ⇒ String
61 62 63 64 |
# File 'lib/vedeu/support/terminal.rb', line 61 def restore_screen output Esc.string 'screen_exit' output clear_last_line end |
#set_cursor_mode ⇒ String
67 68 69 |
# File 'lib/vedeu/support/terminal.rb', line 67 def set_cursor_mode output Esc.string 'show_cursor' unless raw_mode? end |
#size ⇒ Array
Returns a tuple containing the height and width of the current terminal.
156 157 158 |
# File 'lib/vedeu/support/terminal.rb', line 156 def size console.winsize end |
#switch_mode! ⇒ Symbol
92 93 94 95 96 97 98 99 100 |
# File 'lib/vedeu/support/terminal.rb', line 92 def switch_mode! if raw_mode? cooked_mode! else raw_mode! end end |
#width ⇒ Fixnum
Returns the total width (number of columns/characters) of the current terminal.
142 143 144 |
# File 'lib/vedeu/support/terminal.rb', line 142 def width size.last end |