Class: Vedeu::Editor::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/editor/capture.rb

Overview

Capture input from the terminal via ‘getch’.

Constant Summary collapse

ESCAPE_KEY_CODE =

e

27

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(console) ⇒ Vedeu::Editor::Capture

Returns a new instance of Vedeu::Editor::Capture.

Parameters:

  • console (IO)


21
22
23
# File 'lib/vedeu/editor/capture.rb', line 21

def initialize(console)
  @console = console
end

Class Method Details

.read(console) ⇒ String|Symbol

Parameters:

  • console (IO)

Returns:

  • (String|Symbol)


13
14
15
# File 'lib/vedeu/editor/capture.rb', line 13

def self.read(console)
  new(console).read
end

Instance Method Details

#consoleIO (private)

Returns:

  • (IO)


59
60
61
# File 'lib/vedeu/editor/capture.rb', line 59

def console
  @console || Vedeu::Terminal.console
end

#keysString (private)

Returns:

  • (String)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vedeu/editor/capture.rb', line 33

def keys
  keys = console.getch

  if keys.ord == ESCAPE_KEY_CODE
    @chars = 3

    begin
      keys << console.read_nonblock(@chars)

    rescue IO::WaitReadable
      IO.select([console])
      @chars -= 1
      retry

    rescue IO::WaitWritable
      IO.select(nil, [console])
      @chars -= 1
      retry

    end
  end

  keys
end

#readString|Symbol

Returns:

  • (String|Symbol)


26
27
28
# File 'lib/vedeu/editor/capture.rb', line 26

def read
  Vedeu::Input::Translator.translate(keys)
end