Module: Vedeu::Terminal

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

Instance Method Summary collapse

Instance Method Details

#centreObject



69
70
71
# File 'lib/vedeu/support/terminal.rb', line 69

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

#clear_last_lineObject



65
66
67
# File 'lib/vedeu/support/terminal.rb', line 65

def clear_last_line
  Esc.set_position((height - 1), 1) + Esc.string('clear_line')
end

#clear_screenObject



48
49
50
# File 'lib/vedeu/support/terminal.rb', line 48

def clear_screen
  output Esc.string 'clear'
end

#consoleObject



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

def console
  IO.console
end

#heightFixnum

Returns The total height of the current terminal.

Returns:

  • (Fixnum)

    The total height of the current terminal.



79
80
81
# File 'lib/vedeu/support/terminal.rb', line 79

def height
  size.first
end

#initialize_screen(&block) ⇒ Object



42
43
44
45
46
# File 'lib/vedeu/support/terminal.rb', line 42

def initialize_screen(&block)
  output Esc.string 'screen_init'

  yield
end

#inputObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vedeu/support/terminal.rb', line 21

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

#open(mode, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/vedeu/support/terminal.rb', line 5

def open(mode, &block)
  @mode = mode

  if block_given?
    if raw_mode?
      console.raw    { initialize_screen { yield } }

    else
      console.cooked { initialize_screen { yield } }

    end
  end
ensure
  restore_screen
end

#output(stream = '') ⇒ Object



36
37
38
39
40
# File 'lib/vedeu/support/terminal.rb', line 36

def output(stream = '')
  console.print(stream)

  stream
end

#raw_mode?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/vedeu/support/terminal.rb', line 61

def raw_mode?
  @mode == :raw
end

#restore_screenObject



52
53
54
55
# File 'lib/vedeu/support/terminal.rb', line 52

def restore_screen
  output Esc.string 'screen_exit'
  output clear_last_line
end

#set_cursor_modeObject



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

def set_cursor_mode
  output Esc.string 'show_cursor' unless raw_mode?
end

#sizeObject



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

def size
  console.winsize
end

#widthFixnum

Returns The total width of the current terminal.

Returns:

  • (Fixnum)

    The total width of the current terminal.



74
75
76
# File 'lib/vedeu/support/terminal.rb', line 74

def width
  size.last
end