Class: Vedeu::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/input/input.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) ⇒ Input

Returns a new instance of Vedeu::Input.

Parameters:

  • reader (IO)

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



20
21
22
# File 'lib/vedeu/input/input.rb', line 20

def initialize(reader)
  @reader = reader
end

Instance Attribute Details

#readerIO (readonly, protected)

Returns:

  • (IO)


42
43
44
# File 'lib/vedeu/input/input.rb', line 42

def reader
  @reader
end

Class Method Details

.capture(reader) ⇒ String|Symbol

Instantiate Input and capture keypress(es).

Parameters:

  • reader (IO)

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

Returns:

  • (String|Symbol)


11
12
13
# File 'lib/vedeu/input/input.rb', line 11

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

Instance Method Details

#captureArray|String|Symbol

Triggers either a ‘:command’ event with the command if the reader is in cooked mode, or if raw mode, the keypress event with the key(s) pressed.

Returns:

  • (Array|String|Symbol)


28
29
30
31
32
33
34
35
36
# File 'lib/vedeu/input/input.rb', line 28

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

  else
    Vedeu.trigger(:_command_, command)

  end
end

#inputString (private) Also known as: command

Returns the input from the terminal.

Returns:

  • (String)


49
50
51
# File 'lib/vedeu/input/input.rb', line 49

def input
  @input ||= reader.read
end

#keypressString|Symbol (private)

Returns the translated (if possible) keypress(es) as either a String or a Symbol.

Returns:

  • (String|Symbol)


58
59
60
61
62
# File 'lib/vedeu/input/input.rb', line 58

def keypress
  key = input

  specials.fetch(key, key)
end

#specialsHash (private)

Translates (if possible) entered escape sequences into symbols representing the key which was pressed.

Returns:

  • (Hash)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/vedeu/input/input.rb', line 68

def specials
  {
    "\r"      => :enter,
    "\t"      => :tab,
    "\e"      => :escape,
    "\e[A"    => :up,
    "\e[B"    => :down,
    "\e[C"    => :right,
    "\e[D"    => :left,
    "\e[5~"   => :page_up,
    "\e[6~"   => :page_down,
    "\e[H"    => :home,
    "\e[3~"   => :delete,
    "\e[F"    => :end,
    "\e[Z"    => :shift_tab,
    "\eOP"    => :f1,
    "\eOQ"    => :f2,
    "\eOR"    => :f3,
    "\eOS"    => :f4,
    "\e[15~"  => :f5,
    "\e[17~"  => :f6,
    "\e[18~"  => :f7,
    "\e[19~"  => :f8,
    "\e[20~"  => :f9,
    "\e[21~"  => :f10,
    "\e[23~"  => :f11,
    "\e[24~"  => :f12,
    "\e[1;2P" => :print_screen,
    "\e[1;2Q" => :scroll_lock,
    "\e[1;2R" => :pause_break,
    "\u007F"  => :backspace,
    "\u0001"  => :ctrl_a,
    "\u0002"  => :ctrl_b,
    "\u0003"  => :ctrl_c,
    "\u0004"  => :ctrl_d,
    "\u0005"  => :ctrl_e,
    "\u0006"  => :ctrl_f,
    "\u0007"  => :ctrl_g,
    "\u0008"  => :ctrl_h,
    # "\u0009"  => :ctrl_i, # duplicates tab
    "\u0010"  => :ctrl_j,
    "\u0011"  => :ctrl_k,
    "\u0012"  => :ctrl_l,
    "\u0013"  => :ctrl_m,
    "\u0014"  => :ctrl_n,
    "\u0015"  => :ctrl_o,
    "\u0016"  => :ctrl_p,
    "\u0017"  => :ctrl_q,
    "\u0018"  => :ctrl_r,
    "\u0019"  => :ctrl_s,
    # "\u0020"  => :ctrl_t, # duplicates spacebar
    "\u0021"  => :ctrl_u,
    "\u0022"  => :ctrl_v,
    "\u0023"  => :ctrl_w,
    "\u0024"  => :ctrl_x,
    "\u0025"  => :ctrl_y,
    "\u0026"  => :ctrl_z,
  }
end