yap-shell-addon-history-search 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1dada9ffa8c190994aacc85d42c19d2dd5387029
4
+ data.tar.gz: 7e2e88558230a01ff42c4fe4ec92b4a4f401c2f2
5
+ SHA512:
6
+ metadata.gz: a18877352da26122f1011dd3f709f4d81eb23f6e387378f8a780027740208a868f71f43d89875a20733ac40814253c4ce6d70b0f4920183d3e8a6543a55458fa
7
+ data.tar.gz: 456be63f837eb0da266b8f3dc4118b460a36764067d03b9fc24e2ec71a5e41be2122bc48e58be7c84fd63cf7bab8cc907af4ad44d108f7c8865f25142f441e5b
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ wiki/
@@ -0,0 +1 @@
1
+ 2.3.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yap-shell-addon-history-search.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Zach Dennis
4
+
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # history-search for yap-shell
2
+
3
+ Welcome to your new yap addon! In this directory, you'll find the files you need to be able to package up your addon into a gem. Put your Ruby code in the file `lib/yap-shell-addon-history-search`.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'yap-shell-addon-history-search'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install yap-shell-addon-history-search
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yap-shell-addon-history-search.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "yap/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,205 @@
1
+ require 'yap/addon'
2
+ require 'yap-shell-addon-history-search/version'
3
+
4
+ module YapShellAddonHistorySearch
5
+ class Addon < ::Yap::Addon::Base
6
+ self.export_as :'history-search'
7
+
8
+ attr_reader :editor
9
+
10
+ def initialize_world(world)
11
+ @editor = world.editor
12
+ end
13
+
14
+ def prompt_user_to_search
15
+ label_text = "(reverse-search): "
16
+ search_label = ::TerminalLayout::Box.new(
17
+ content: label_text,
18
+ style: {
19
+ display: :inline,
20
+ height: 1
21
+ }
22
+ )
23
+ search_box = ::TerminalLayout::InputBox.new(
24
+ content: "",
25
+ style: {
26
+ display: :inline,
27
+ height: 1
28
+ }
29
+ )
30
+ search_box.name = "focused-input-box"
31
+
32
+ Treefell['shell'].puts "editor.content_box.children: #{editor.content_box.children.inspect}"
33
+ history_search_env = editor.new_env
34
+ history_search = Search.new(editor.history,
35
+ line: ::RawLine::LineEditor.new(::RawLine::Line.new, sync_with: -> { search_box }),
36
+ keys: history_search_env.keys,
37
+ search: -> (term:, result:){
38
+ # when there is no match, result will be nil, #to_s clears out the content
39
+ editor.input_box.content = result.to_s
40
+ },
41
+ done: -> (execute:, result:){
42
+ editor.content_box.children = []
43
+ editor.pop_env
44
+ editor.focus_input_box(editor.input_box)
45
+ editor.overwrite_line(result.to_s)
46
+ editor.process_line if execute
47
+ }
48
+ )
49
+
50
+ editor.push_env history_search_env
51
+ editor.push_keyboard_input_processor(history_search)
52
+
53
+ editor.content_box.children = [search_label, search_box]
54
+ editor.focus_input_box(search_box)
55
+ end
56
+
57
+ class Search
58
+ attr_reader :keys
59
+
60
+ def initialize(history, keys:, line:, search:, done:)
61
+ @history = history
62
+ @keys = keys
63
+ @line = line
64
+ @search_proc = search || -> {}
65
+ @done_proc = done || -> {}
66
+
67
+ initialize_key_bindings
68
+ end
69
+
70
+ def initialize_key_bindings
71
+ @keys.bind(:return){ execute }
72
+ @keys.bind(:ctrl_j){ accept }
73
+ @keys.bind(:ctrl_r){ search_again_backward }
74
+ @keys.bind(:ctrl_n){ search_again_forward }
75
+ @keys.bind(:ctrl_a){ @line.move_to_beginning_of_input }
76
+ @keys.bind(:ctrl_e){ @line.move_to_end_of_input }
77
+ @keys.bind(:ctrl_k){ @line.kill_forward }
78
+ @keys.bind(:backspace) do
79
+ @line.delete_left_character
80
+ perform_search(type: @last_search_was)
81
+ end
82
+ @keys.bind(:escape){ cancel }
83
+ @keys.bind(:ctrl_c){ cancel }
84
+ end
85
+
86
+ def read_bytes(bytes)
87
+ if bytes.any?
88
+ Treefell['shell'].puts "history search found bytes: #{bytes.inspect}"
89
+
90
+ search_bytes = process_bytes(bytes)
91
+
92
+ if search_bytes.any?
93
+ Treefell['shell'].puts "history searching with bytes=#{bytes.inspect}"
94
+ search_with_bytes(bytes)
95
+ end
96
+ end
97
+ end
98
+
99
+ private
100
+
101
+ # given [1,2,3] first try [1,2,3]. If no key binding
102
+ # matches, then try [1,2]. If no keybinding matches try [1].
103
+ # Execution should flow in left to right, positional order.
104
+ # This returns an array of left over bytes in the order they
105
+ # appeared that did not match any keybinding
106
+ def process_bytes bytes, leftover=[]
107
+ if bytes.empty?
108
+ leftover
109
+ elsif @keys[bytes]
110
+ @keys[bytes].call
111
+ leftover
112
+ else
113
+ process_bytes(
114
+ bytes[0..-2],
115
+ [bytes[-1]].concat(leftover)
116
+ )
117
+ end
118
+ end
119
+
120
+ def accept
121
+ @done_proc.call(execute: false, result: result)
122
+ end
123
+
124
+ def cancel
125
+ @done_proc.call(execute: false, result: nil)
126
+ end
127
+
128
+ def execute
129
+ @done_proc.call(execute: true, result: result)
130
+ end
131
+
132
+ def found_match(result:)
133
+ @search_proc.call(term: @line.text, result: result)
134
+ end
135
+
136
+ def no_match_found
137
+ @last_match_index = nil
138
+ @search_proc.call(term: @line.text, result: result)
139
+ end
140
+
141
+ def result
142
+ @history2search[@last_match_index] if @last_match_index
143
+ end
144
+
145
+ def search_again_backward
146
+ Treefell['shell'].puts "history searching again backward"
147
+ if @last_search_was == :forward
148
+ @last_match_index = @history.length - @last_match_index - 1
149
+ @history2search = @history.reverse
150
+ end
151
+ perform_search(starting_index: @last_match_index, type: :backward)
152
+ end
153
+
154
+ def search_again_forward
155
+ Treefell['shell'].puts "history searching again forward"
156
+ if @last_search_was == :backward
157
+ @last_match_index = @history.length - @last_match_index - 1
158
+ @history2search = @history
159
+ end
160
+ perform_search(starting_index: @last_match_index, type: :forward)
161
+ end
162
+
163
+ def search_with_bytes(bytes)
164
+ part = bytes.map(&:chr).join
165
+ @line.write(part.scan(/[[:print:]]/).join)
166
+ @history2search = @history.reverse
167
+ perform_search(type: :backward)
168
+ end
169
+
170
+ def perform_search(starting_index: -1, type:)
171
+ if @line.text.empty?
172
+ no_match_found
173
+ return
174
+ end
175
+
176
+ # fuzzy search
177
+ characters = @line.text.split('').map { |ch| Regexp.escape(ch) }
178
+ fuzzy_search_regex = /#{characters.join('.*?')}/
179
+
180
+ # non-fuzzy-search
181
+ # fuzzy_search_regex = /#{Regexp.escape(@line.text)}/
182
+
183
+ Treefell['shell'].puts "history search matching on regex=#{fuzzy_search_regex.inspect} starting_index=#{starting_index}"
184
+ @last_search_was = type
185
+
186
+ match = @history2search.detect.with_index do |item, i|
187
+ next if i <= starting_index
188
+
189
+ # Treefell['shell'].puts "history search matching #{(item + @line.text).inspect} =~ #{fuzzy_search_regex}"
190
+ md = (item + @line.text).match(fuzzy_search_regex)
191
+ if md && md.end(0) <= item.length
192
+ # Treefell['shell'].puts "history search match #{item} at #{i}"
193
+ @last_match_index = i
194
+ end
195
+ end
196
+
197
+ if match
198
+ found_match(result: match)
199
+ else
200
+ no_match_found
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,3 @@
1
+ module YapShellAddonHistorySearch
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/lib/yap-shell-addon-history-search/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'yap-shell-addon-history-search'
5
+ spec.version = YapShellAddonHistorySearch::VERSION
6
+ spec.authors = ['Your name']
7
+ spec.email = 'you@example.com'
8
+ spec.date = Date.today.to_s
9
+
10
+ spec.summary = 'history-search summary goes here.'
11
+ spec.description = 'history-search description goes here.'
12
+ spec.homepage = ''
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(/^(test|spec|features)\//) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(/^exe\//) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "yap-shell", "~> 0.6"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 11.2"
24
+ spec.add_development_dependency "rspec", "~> 3.4"
25
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yap-shell-addon-history-search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Your name
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yap-shell
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '11.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '11.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ description: history-search description goes here.
70
+ email: you@example.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".ruby-version"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/yap-shell-addon-history-search.rb
82
+ - lib/yap-shell-addon-history-search/version.rb
83
+ - yap-shell-addon-history-search.gemspec
84
+ homepage: ''
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.5.1
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: history-search summary goes here.
108
+ test_files: []
109
+ has_rdoc: