tty-reader 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1b83301002055869c17bb4ecb0571be89b2d8e0
4
- data.tar.gz: 39bd25b1b47c2542c000a407a2839bd4cba12c31
3
+ metadata.gz: c03f74e7241085592fa20639a1e611fcc67236e0
4
+ data.tar.gz: 4e86e58a736ac624efcbb11bd4d4d09a7e80e7ed
5
5
  SHA512:
6
- metadata.gz: 08c4198829dcc5a4f95c865c4dfcb2c4be9711885a3d071c6f1d9f917e8dc20083d558dde98ffc38af0d7e6ce6de9f4fbfeecb1158109f2905adb047aa6970f1
7
- data.tar.gz: 3899ba4f5461099024379df0058deb177dbf1a45d6fb250970f3a639adf792f9563773adeae1b240f2feb41241d6ec45b4a012c15f4b1c234ad6bf582f7c0ac4
6
+ metadata.gz: ce215884478b45022b5e44a0f464906880dedcfb059f8f265ac6a016a41a622385ef2c899bb370c8f63d3417fac4ebcfb9ac284d819e7e97220c3ad0d52ee8bf
7
+ data.tar.gz: 7bf7f7e083acfefef98c5806f54c4e14398d286cb7e926f79a41ef7b4216f77d3d312074d1703a306bd4b280bccccc8e17ceb648d37e1419fada48aac10ab868
@@ -11,6 +11,7 @@ rvm:
11
11
  - 2.2.9
12
12
  - 2.3.6
13
13
  - 2.4.3
14
+ - 2.5.0
14
15
  - ruby-head
15
16
  - jruby-9.1.1.0
16
17
  - jruby-head
@@ -1,5 +1,14 @@
1
1
  # Change log
2
2
 
3
+ ## [v0.3.0] - 2018-04-29
4
+
5
+ ### Added
6
+ * Add Reader#unsubscribe to allow stop listening to local key events
7
+
8
+ ### Changed
9
+ * Change Reader#subscribe to allow to listening for key events only inside a block
10
+ * Change to group xterm keys for navigation
11
+
3
12
  ## [v0.2.0] - 2018-01-01
4
13
 
5
14
  ### Added
@@ -24,5 +33,6 @@
24
33
 
25
34
  * Initial implementation and release
26
35
 
36
+ [v0.3.0]: https://github.com/piotrmurach/tty-reader/compare/v0.2.0...v0.3.0
27
37
  [v0.2.0]: https://github.com/piotrmurach/tty-reader/compare/v0.1.0...v0.2.0
28
38
  [v0.1.0]: https://github.com/piotrmurach/tty-reader/compare/v0.1.0
data/README.md CHANGED
@@ -60,8 +60,9 @@ Or install it yourself as:
60
60
  * [2.3 read_multiline](#23-read_multiline)
61
61
  * [2.4 on](#24-on)
62
62
  * [2.5 subscribe](#25-subscribe)
63
- * [2.6 trigger](#26-trigger)
64
- * [2.7 supported events](#27-supported-events)
63
+ * [2.6 unsubscribe](#26-unsubscribe)
64
+ * [2.7 trigger](#27-trigger)
65
+ * [2.8 supported events](#28-supported-events)
65
66
  * [3. Configuration](#3-configuration)
66
67
  * [3.1 :interrupt](#31-interrupt)
67
68
  * [3.2 :track_history](#32-track_history)
@@ -178,10 +179,10 @@ prompt.on(:keypress) { |key| ... }
178
179
 
179
180
  You can subscribe any object to listen for the emitted [key events](#27-supported-events) using the `subscribe` message. The listener would need to implement a method for every event it wishes to receive.
180
181
 
181
- For example, if a `Context` class wishes to only listen for `keypress` event:
182
+ For example, if a `MyListener` class wishes to only listen for `keypress` event:
182
183
 
183
184
  ```ruby
184
- class Context
185
+ class MyListener
185
186
  def keypress(event)
186
187
  ...
187
188
  end
@@ -191,11 +192,26 @@ end
191
192
  Then subcribing is done:
192
193
 
193
194
  ```ruby
194
- context = Context.new
195
- reader.subscribe(context)
195
+ reader.subscribe(MyListener.new)
196
196
  ```
197
197
 
198
- ### 2.6 trigger
198
+ Alternatively, `subscribe` allows you to listen to events only for the dueration of block execution like so:
199
+
200
+ ```ruby
201
+ reader.subscribe(MyListener) do
202
+ ...
203
+ end
204
+ ```
205
+
206
+ ### 2.6 unsubscribe
207
+
208
+ You can unsubscribe any object from listening to the key events using the `unsubscribe` message:
209
+
210
+ ```ruby
211
+ reader.unsubscribe(my_listener)
212
+ ```
213
+
214
+ ### 2.7 trigger
199
215
 
200
216
  The signature for triggering key events is `trigger(event, args...)`. The first argument is a [key event name](#27-supported-events) followed by any number of actual values related to the event being triggered.
201
217
 
@@ -213,12 +229,12 @@ reader.on(:keypress) do |event|
213
229
  reader.trigger(:keydown)
214
230
  end
215
231
  if evevnt.value == 'k'
216
- reader.trigger(:keup)
232
+ reader.trigger(:keyup)
217
233
  end
218
234
  end
219
235
  ```
220
236
 
221
- ### 2.7 supported events
237
+ ### 2.8 supported events
222
238
 
223
239
  The available key events for character input are:
224
240
 
@@ -19,3 +19,5 @@ environment:
19
19
  - ruby_version: "23-x64"
20
20
  - ruby_version: "24"
21
21
  - ruby_version: "24-x64"
22
+ - ruby_version: "25"
23
+ - ruby_version: "25-x64"
@@ -91,6 +91,43 @@ module TTY
91
91
  subscribe(self)
92
92
  end
93
93
 
94
+ alias old_subcribe subscribe
95
+
96
+ # Subscribe to receive key events
97
+ #
98
+ # @example
99
+ # reader.subscribe(MyListener.new)
100
+ #
101
+ # @return [self|yield]
102
+ #
103
+ # @api public
104
+ def subscribe(listener, options = {})
105
+ old_subcribe(listener, options)
106
+ object = self
107
+ if block_given?
108
+ object = yield
109
+ unsubscribe(listener)
110
+ end
111
+ object
112
+ end
113
+
114
+ # Unsubscribe from receiving key events
115
+ #
116
+ # @example
117
+ # reader.unsubscribe(my_listener)
118
+ #
119
+ # @return [void]
120
+ #
121
+ # @api public
122
+ def unsubscribe(listener)
123
+ registry = send(:local_registrations)
124
+ registry.each do |object|
125
+ if object.listener.equal?(listener)
126
+ registry.delete(object)
127
+ end
128
+ end
129
+ end
130
+
94
131
  # Select appropriate console
95
132
  #
96
133
  # @api private
@@ -69,15 +69,22 @@ module TTY
69
69
  "\e[D" => :left,
70
70
  "\e[E" => :clear,
71
71
  "\e[H" => :home,
72
- "\eOH" => :home,
73
72
  "\e[F" => :end,
74
- "\eOF" => :end,
75
73
  "\e[Z" => :back_tab, # shift + tab
76
74
 
77
- "\eOP" => :f1,
78
- "\eOQ" => :f2,
79
- "\eOR" => :f3,
80
- "\eOS" => :f4,
75
+ # xterm/gnome
76
+ "\eOA" => :up,
77
+ "\eOB" => :down,
78
+ "\eOC" => :right,
79
+ "\eOD" => :left,
80
+ "\eOE" => :clear,
81
+ "\eOF" => :end,
82
+ "\eOH" => :home,
83
+
84
+ "\eOP" => :f1, # xterm
85
+ "\eOQ" => :f2, # xterm
86
+ "\eOR" => :f3, # xterm
87
+ "\eOS" => :f4, # xterm
81
88
  "\e[[A" => :f1, # linux
82
89
  "\e[[B" => :f2, # linux
83
90
  "\e[[C" => :f3, # linux
@@ -115,11 +122,6 @@ module TTY
115
122
  "\e[21;2~" => :f22,
116
123
  "\e[23;2~" => :f23,
117
124
  "\e[24;2~" => :f24,
118
-
119
- "\eOA" => :up,
120
- "\eOB" => :down,
121
- "\eOC" => :right,
122
- "\eOD" => :left
123
125
  }
124
126
  end
125
127
  module_function :keys
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TTY
4
4
  class Reader
5
- VERSION = '0.2.0'.freeze
5
+ VERSION = '0.3.0'.freeze
6
6
  end # Reader
7
7
  end # TTY
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tty-reader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Murach
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-01 00:00:00.000000000 Z
11
+ date: 2018-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: wisper