Class: Vedeu::Input::Capture

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

Overview

Captures input from the user via Terminal#input and translates special characters into symbols.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader) ⇒ Vedeu::Input::Input

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

Parameters:

  • reader (IO)

    An object that responds to ‘#read`. Typically, this is Vedeu::Terminal.



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

def initialize(reader)
  @reader = reader
end

Instance Attribute Details

#readerIO (readonly, protected)

Returns:

  • (IO)


76
77
78
# File 'lib/vedeu/input/capture.rb', line 76

def reader
  @reader
end

Class Method Details

.read(reader) ⇒ Array|String|Symbol

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

Parameters:

  • reader (IO)

    An object that responds to ‘#read`. Typically, this is Vedeu::Terminal.

Returns:

  • (Array|String|Symbol)


14
15
16
# File 'lib/vedeu/input/capture.rb', line 14

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

Instance Method Details

#click?(key) ⇒ Boolean (private)

Returns a boolean indicating whether a mouse click was received.

Parameters:

  • key (String)

Returns:

  • (Boolean)


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

def click?(key)
  return false if key.is_a?(Symbol)

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

#inputString (private) Also known as: command

Returns the input from the terminal.

Returns:

  • (String)


103
104
105
# File 'lib/vedeu/input/capture.rb', line 103

def input
  @input ||= reader.read
end

#keypressString|Symbol (private)

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

Returns:

  • (String|Symbol)


94
95
96
97
98
# File 'lib/vedeu/input/capture.rb', line 94

def keypress
  key = input

  @keypress ||= Vedeu::Input::Translator.translate(key)
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)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vedeu/input/capture.rb', line 44

def read
  if click?(keypress)
    Vedeu.trigger(:_mouse_event_, keypress)

  elsif reader.raw_mode?
    Vedeu.trigger(:_keypress_, keypress)

  elsif reader.fake_mode?
    name      = Vedeu.focus
    interface = Vedeu.interfaces.by_name(name)
    key       = keypress

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

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

    else
      Vedeu.trigger(:key, key)

    end
  else
    Vedeu.trigger(:_command_, command)

  end
end