Class: Vedeu::Input::Capture

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vedeu/input/capture.rb

Overview

Captures input from the user terminal via ‘getch’ and translates special characters into symbols.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVedeu::Input::Input

Returns a new instance of Vedeu::Input::Input.



31
# File 'lib/vedeu/input/capture.rb', line 31

def initialize; end

Class Method Details

.readArray|String|Symbol

Instantiate Vedeu::Input::Input and capture keypress(es).

Returns:

  • (Array|String|Symbol)


24
25
26
# File 'lib/vedeu/input/capture.rb', line 24

def self.read
  new.read
end

Instance Method Details

#click?(keys) ⇒ Boolean (private)

Returns a boolean indicating whether a mouse click was received.

Parameters:

Returns:



117
118
119
120
121
# File 'lib/vedeu/input/capture.rb', line 117

def click?(keys)
  return false if keys.nil? || keys.is_a?(Symbol)

  keys.is_a?(Vedeu::Cursors::Cursor) || keys.start_with?("\e[M")
end

#commandString (private)

Takes input from the user via the keyboard. Accepts special keys like the F-Keys etc, by capturing the entire sequence.

Returns:

  • (String)


132
133
134
# File 'lib/vedeu/input/capture.rb', line 132

def command
  console.gets.chomp
end

#consoleIO (private)

Returns:

  • (IO)


124
125
126
# File 'lib/vedeu/input/capture.rb', line 124

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

#inputString (private)

Takes input from the user via the keyboard. Accepts special keys like the F-Keys etc, by capturing the entire sequence.

Returns:

  • (String)


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/vedeu/input/capture.rb', line 95

def input
  keys = Vedeu::Input::Raw.read

  if click?(keys)
    Vedeu::Input::Mouse.click(keys)

  else
    keys

  end
end

#interfaceVedeu::Interfaces::Interface (private)



108
109
110
# File 'lib/vedeu/input/capture.rb', line 108

def interface
  Vedeu.interfaces.by_name(name)
end

#keypressString|Symbol (private)

Returns the translated (when possible) keypress(es).

Returns:

  • (String|Symbol)


87
88
89
# File 'lib/vedeu/input/capture.rb', line 87

def keypress
  Vedeu::Input::Translator.translate(input)
end

#nameString|Symbol (private)

Returns:

  • (String|Symbol)


137
138
139
# File 'lib/vedeu/input/capture.rb', line 137

def name
  Vedeu.focus
end

#readArray|String|Symbol

Triggers various events dependent on the terminal mode.

  • When in raw mode, the :keypress event is triggered with the key(s) pressed.

  • When in fake mode, the keypress will be checked against the keymap relating to the interface/view currently in focus.

    • If registered, the :keypress event is triggered with the key(s) pressed.

    • If the keypress is not registered, we check whether the interface in focus is editable, if so, the :editor event is triggered.

    • Otherwise, the :key event is triggered for the client application to handle.

  • When in cooked mode, the :command event is triggered with the input given.

Returns:

  • (Array|String|Symbol)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vedeu/input/capture.rb', line 50

def read
  Vedeu.log(type: :input, message: 'Waiting for user input...')

  if raw_mode?
    Vedeu.trigger(:_keypress_, keypress)

  elsif fake_mode?
    @key ||= keypress

    if @key.nil?
      nil

    elsif click?(@key)
      Vedeu.trigger(:_mouse_event_, @key)

    elsif Vedeu::Input::Mapper.registered?(@key, name)
      Vedeu.trigger(:_keypress_, @key, name)

    elsif interface.editable?
      Vedeu.trigger(:_editor_, @key)

    else
      Vedeu.trigger(:key, @key)

    end

  elsif cooked_mode?
    Vedeu.trigger(:_command_, command)

  end
end