Module: Vedeu::Terminal

Extended by:
Terminal
Included in:
Terminal
Defined in:
lib/vedeu/support/terminal.rb

Instance Method Summary collapse

Instance Method Details

#centreArray

Returns a coordinate tuple of the format [y, x], where ‘y` is the row/line and `x` is the column/character.

Returns:

  • (Array)


118
119
120
# File 'lib/vedeu/support/terminal.rb', line 118

def centre
  [(height / 2), (width / 2)]
end

#centre_xFixnum

Returns the ‘x` (column/character) component of the coodinate tuple provided by #centre

Returns:



134
135
136
# File 'lib/vedeu/support/terminal.rb', line 134

def centre_x
  centre.last
end

#centre_yFixnum

Returns the ‘y` (row/line) component of the coordinate tuple provided by #centre

Returns:



126
127
128
# File 'lib/vedeu/support/terminal.rb', line 126

def centre_y
  centre.first
end

#clear_last_lineString

Returns:

  • (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_screenString

Returns:

  • (String)


56
57
58
# File 'lib/vedeu/support/terminal.rb', line 56

def clear_screen
  output Esc.string 'clear'
end

#consoleFile

Returns:

  • (File)


161
162
163
# File 'lib/vedeu/support/terminal.rb', line 161

def console
  IO.console
end

#cooked_mode!Symbol

Returns:

  • (Symbol)


77
78
79
# File 'lib/vedeu/support/terminal.rb', line 77

def cooked_mode!
  @_mode = :cooked
end

#cooked_mode?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/vedeu/support/terminal.rb', line 72

def cooked_mode?
  mode == :cooked
end

#heightFixnum

Returns the total height (number of rows/lines) of the current terminal.

Returns:



149
150
151
# File 'lib/vedeu/support/terminal.rb', line 149

def height
  size.first
end

#initialize_screen(&block) ⇒ Object

Returns [].

Parameters:

  • block (Proc)

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

#inputString

Returns:

  • (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

#modeSymbol

Returns the mode of the terminal, either ‘:raw` or `:cooked`

Returns:

  • (Symbol)


110
111
112
# File 'lib/vedeu/support/terminal.rb', line 110

def mode
  @_mode ||= Configuration.terminal_mode
end

#open(&block) ⇒ Object

Returns [].

Parameters:

  • block (Proc)

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

Parameters:

  • stream (String) (defaults to: '')

Returns:

  • (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

Returns:

  • (Symbol)


87
88
89
# File 'lib/vedeu/support/terminal.rb', line 87

def raw_mode!
  @_mode = :raw
end

#raw_mode?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/vedeu/support/terminal.rb', line 82

def raw_mode?
  mode == :raw
end

#restore_screenString

Returns:

  • (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_modeString

Returns:

  • (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

#sizeArray

Returns a tuple containing the height and width of the current terminal.

Returns:

  • (Array)


156
157
158
# File 'lib/vedeu/support/terminal.rb', line 156

def size
  console.winsize
end

#switch_mode!Symbol

Returns:

  • (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

#widthFixnum

Returns the total width (number of columns/characters) of the current terminal.

Returns:



142
143
144
# File 'lib/vedeu/support/terminal.rb', line 142

def width
  size.last
end