keyboard_map 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 25ca369fcb1d0e1d108dbeb543d3b0a284cfff550886c6ce5d1ee4857450ea58
4
+ data.tar.gz: 67a599e04714db40a08f13c57203de64be1319a627505e88b47769cc96aacd2c
5
+ SHA512:
6
+ metadata.gz: 3e8ff18adc0ce259a0570c552370adc5b06827f35fd57f759466e2efb913cb51144905268549dfaeea3fa81d3ae1e45d18856d5a20f1c3bd2daf1357362a4a1f
7
+ data.tar.gz: 58975bc916c978e5778a4082960b00924d49d15ae7fc0e86d4c17df4e0a69e52072ca8a7ba2b86201dd3d99fa53678f98300374ea958c17708305304bcdf1fbb
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ *~
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in keyboard_map.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Vidar Hokstad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # KeyboardMap
2
+
3
+ Process key-presses and map escape-sequences to symbols.
4
+
5
+ Dealing with raw keyboard input is painful because something like cursor-up
6
+ can return several different sequences depending on terminal, *and* because
7
+ there is no terribly simple algorithm determining what represents the
8
+ end of a single sequence. In fact some software relies on key-presses being
9
+ slow enough to set a timeout and read character by character.
10
+
11
+ KeyboardMap allows you to handle the reads in whichever way you prefer.
12
+ It simply provides a simple state machine that will return an array of
13
+ the keyboard events found so far.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'keyboard_map'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install keyboard_map
30
+
31
+ ## Usage
32
+
33
+ TODO: Write usage instructions here
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vidarh/keyboard_map.
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ desc "Install bundle"
9
+ task :bundle do
10
+ system("bundle install --path=./vendor/bundle")
11
+ end
12
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "keyboard_map"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "keyboard_map/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "keyboard_map"
8
+ spec.version = KeyboardMap::VERSION
9
+ spec.authors = ["Vidar Hokstad"]
10
+ spec.email = ["vidar@hokstad.com"]
11
+
12
+ spec.summary = "Read characters from the console and map special keys to symbols"
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/vidarh/keyboard_map"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,3 @@
1
+ class KeyboardMap
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,232 @@
1
+ # coding: utf-8
2
+ # frozen-string-literals: true
3
+
4
+ require "keyboard_map/version"
5
+ require 'set'
6
+
7
+ class KeyboardMap
8
+ attr_reader :buf
9
+
10
+ class Event
11
+ attr_reader :modifiers,:key
12
+
13
+
14
+ def initialize(key,*modifiers)
15
+ @modifiers = Set[*modifiers.map(&:to_sym)]
16
+ @key = key
17
+ end
18
+
19
+ def to_sym
20
+ (modifiers.to_a.sort << key).join("_").to_sym
21
+ end
22
+
23
+ def ==(ev)
24
+ case ev
25
+ when Event
26
+ return @modifiers == ev.modifiers && @key == ev.key
27
+ when Symbol
28
+ return self.to_sym == ev
29
+ else
30
+ return self.to_s == ev
31
+ end
32
+ end
33
+ end
34
+
35
+ SINGLE_KEY_EVENT = {
36
+ "\t" => :tab,
37
+ "\r" => :enter,
38
+ "\u007F" => :backspace
39
+ }.freeze
40
+
41
+ # \e[ starts a CSI sequence. This maps the final character in the CSI
42
+ # sequence to a key and how to interpret the parameters.
43
+ CSI_BASIC_MAP = {
44
+ "A" => :up,
45
+ "B" => :down,
46
+ "C" => :right,
47
+ "D" => :left,
48
+ "E" => :keypad_5,
49
+ "F" => :end,
50
+ "H" => :home,
51
+ "P" => :f1,
52
+ "Q" => :f2,
53
+ "R" => :f3,
54
+ "S" => :f4,
55
+ }.freeze
56
+
57
+ # \e[{parameter1}{;...}~ from parameter1 => key
58
+ CSI_TILDE_MAP = {
59
+ "2" => :insert,
60
+ "3" => :delete,
61
+ "5" => :page_up,
62
+ "6" => :page_down,
63
+ "11" => :f1,
64
+ "12" => :f2,
65
+ "13" => :f3,
66
+ "14" => :f4,
67
+ "15" => :f5,
68
+ "17" => :f6,
69
+ "18" => :f7,
70
+ "19" => :f8,
71
+ "20" => :f9,
72
+ "21" => :f10,
73
+ "23" => :f11,
74
+ "24" => :f12,
75
+ "200" => :start_paste,
76
+ "201" => :end_paste,
77
+ }.freeze
78
+
79
+ # Map of simple/non-parameterised escape sequences to symbols
80
+ ESCAPE_MAP = {
81
+ "\e[Z" => Event.new(:tab,:shift),
82
+ "\eOP" => :f1,
83
+ "\eOQ" => :f2,
84
+ "\eOR" => :f3,
85
+ "\eOS" => :f4
86
+ }.freeze
87
+
88
+ CSI_FINAL_BYTE = 0x40..0x7e
89
+
90
+ @@key_events = {}
91
+ def self.event(key,*modifiers)
92
+ e = Event.new(key,*modifiers)
93
+ k = e.to_sym
94
+ @@key_events[k] ||= e
95
+ @@key_events[k]
96
+ end
97
+
98
+ def meta(key)
99
+ self.class.event(key,:meta)
100
+ end
101
+
102
+ ESC = "\e"
103
+
104
+ def initialize
105
+ @tmp = ""
106
+ @buf = ""
107
+ @state = :text
108
+ end
109
+
110
+ def call(input)
111
+ @buf << input
112
+ run
113
+ end
114
+
115
+ def map_escape(seq)
116
+ if sym = ESCAPE_MAP[seq]
117
+ return sym
118
+ end
119
+ return Event.new(seq,:esc)
120
+ end
121
+
122
+ def ss3(ch)
123
+ tmp = @tmp
124
+ tmp << ch
125
+ @tmp = ""
126
+ @state = :text
127
+ return map_escape(tmp)
128
+ end
129
+
130
+ def map_modifiers(mod)
131
+ return [] if mod.nil?
132
+ mod = mod.to_i - 1
133
+ [].tap do |m|
134
+ m << :shift if (mod & 1) == 1
135
+ m << :meta if (mod & 2) == 2
136
+ m << :ctrl if (mod & 4) == 4
137
+ end
138
+ end
139
+
140
+ def map_csi(seq)
141
+ if sym = ESCAPE_MAP[seq]
142
+ return sym
143
+ end
144
+ final = seq[-1]
145
+ params = String(seq[2..-2]).split(";")
146
+ modifiers = []
147
+ if final == "~"
148
+ key = CSI_TILDE_MAP[params[0]]
149
+ if key
150
+ modifiers = map_modifiers(params[1])
151
+ end
152
+ else
153
+ key = CSI_BASIC_MAP[final]
154
+ modifiers = map_modifiers(params[1]) if key && params.first == "1" && params.size == 2
155
+ end
156
+
157
+ return Event.new(key,*Array(modifiers)) if key
158
+ return Event.new((params << final).join("_"), :csi)
159
+ end
160
+
161
+
162
+ def csi(ch)
163
+ @tmp << ch
164
+ return nil if !CSI_FINAL_BYTE.member?(ch.ord)
165
+ @state = :text
166
+ tmp = @tmp
167
+ @tmp = ""
168
+ return map_csi(tmp)
169
+ end
170
+
171
+ def esc(ch)
172
+ if ch == "["
173
+ @state = :csi
174
+ @tmp << ch
175
+ return nil
176
+ elsif ch == "O"
177
+ @state = :ss3
178
+ @tmp << ch
179
+ return nil
180
+ elsif ch == "\t"
181
+ @state = :text
182
+ @tmp = ""
183
+ return meta(:tab)
184
+ elsif ch == "\e"
185
+ return :esc
186
+ end
187
+ @state = :text
188
+ @tmp = ""
189
+ return meta(ch)
190
+ end
191
+
192
+ def text(ch)
193
+ if ch == ESC
194
+ @state = :esc
195
+ out = @tmp.empty? ? nil : @tmp
196
+ @tmp = ESC.dup
197
+ return out
198
+ end
199
+
200
+ if m = SINGLE_KEY_EVENT[ch]
201
+ tmp = @tmp
202
+ @tmp = ""
203
+ return [self.class.event(m)] if tmp.empty?
204
+ return [tmp, self.class.event(m)]
205
+ end
206
+
207
+ if ch.ord < 32
208
+ tmp = @tmp
209
+ @tmp = ""
210
+ ev = self.class.event((ch.ord+96).chr,:ctrl)
211
+ return [ev] if tmp.empty?
212
+ return [tmp,ev]
213
+ end
214
+
215
+ @tmp << ch
216
+ nil
217
+ end
218
+
219
+ def run
220
+ out = []
221
+ while !@buf.empty?
222
+ ch = @buf.slice!(0)
223
+ r = send(@state,ch)
224
+ out.concat(Array(r)) if r
225
+ end
226
+ if !@tmp.empty? && @state == :text
227
+ out << @tmp
228
+ @tmp = ""
229
+ end
230
+ out
231
+ end
232
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keyboard_map
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vidar Hokstad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Read characters from the console and map special keys to symbols
56
+ email:
57
+ - vidar@hokstad.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - keyboard_map.gemspec
72
+ - lib/keyboard_map.rb
73
+ - lib/keyboard_map/version.rb
74
+ homepage: https://github.com/vidarh/keyboard_map
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Read characters from the console and map special keys to symbols
98
+ test_files: []