peco_selector 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/lib/peco_selector.rb +35 -28
- data/lib/peco_selector/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae48f7ba611079608c1ffd67fb98dea86c6d34f9
|
4
|
+
data.tar.gz: a3e69f385d0142a5edf0f7478ddec2ae3e9f4817
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c83b5a6f49ca6ccac0ff750b565b86d7d2505a9e7e896eb43af3808475082bbdebfc2dc2e4f708e5105de589698ea8d0697782774c6cc8c341be56eddad8d14f
|
7
|
+
data.tar.gz: 776c4ba8ca4afb714a3e5e783d08a357fdd8d048d6fa1254fc5ded5485efddfe0a4b54aaafbb7a4b81dfb6b25800d33fe65cb5e1d6cbf1c2dfe50ab1ad8c4ade
|
data/README.md
CHANGED
@@ -27,6 +27,12 @@ result = PecoSelector.select_from([['bob', bob], ['alice', alice]])
|
|
27
27
|
result # => [bob]
|
28
28
|
```
|
29
29
|
|
30
|
+
```ruby
|
31
|
+
result = PecoSelector.select_from(['bob', 'alice'])
|
32
|
+
|
33
|
+
result # => ['bob']
|
34
|
+
```
|
35
|
+
|
30
36
|
## Contributing
|
31
37
|
|
32
38
|
1. Fork it ( https://github.com/[my-github-username]/peco_selector/fork )
|
data/lib/peco_selector.rb
CHANGED
@@ -3,44 +3,51 @@ require 'open3'
|
|
3
3
|
require 'shellwords'
|
4
4
|
|
5
5
|
module PecoSelector
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
PECO_BIN = "peco"
|
7
|
+
Error = Class.new(StandardError)
|
8
|
+
PecoUnavailableError = Class.new(StandardError)
|
9
9
|
|
10
|
-
|
11
|
-
PECO_BIN
|
12
|
-
|
10
|
+
def self.peco_available?
|
11
|
+
system('which', PECO_BIN, out: File::NULL)
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
def self.ensure_peco_available
|
15
|
+
unless peco_available?
|
16
|
+
raise PecoUnavailableError, "Peco command is unavailable. (see https://github.com/peco/peco#installation)"
|
17
|
+
end
|
18
|
+
end
|
16
19
|
|
17
|
-
|
18
|
-
|
20
|
+
def self.select_from(candidates, options = {})
|
21
|
+
ensure_peco_available
|
19
22
|
|
20
|
-
|
21
|
-
candidates.each do |display, value|
|
22
|
-
value ||= display
|
23
|
-
stdin.puts "#{display}\x00#{value.object_id}"
|
24
|
-
end
|
25
|
-
stdin.close
|
23
|
+
prompt = options[:prompt] || "QUERY>"
|
26
24
|
|
27
|
-
|
28
|
-
|
25
|
+
stdout_str = nil
|
26
|
+
stderr_str = nil
|
29
27
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
28
|
+
Open3.popen3("#{PECO_BIN} --null --prompt #{Shellwords.escape(prompt)}") do |stdin, stdout, stderr, wait_thr|
|
29
|
+
candidates.each do |display, value|
|
30
|
+
value ||= display
|
31
|
+
stdin.puts "#{display}\x00#{value.object_id}"
|
35
32
|
end
|
33
|
+
stdin.close
|
36
34
|
|
37
|
-
|
35
|
+
stdout_str = stdout.read
|
36
|
+
stderr_str = stderr.read
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
unless wait_thr.value.exitstatus == 0
|
39
|
+
$stdout.print stdout_str
|
40
|
+
$stderr.print stderr_str
|
41
|
+
abort
|
43
42
|
end
|
44
43
|
end
|
44
|
+
|
45
|
+
object_ids = stdout_str.strip.split("\n").map(&:to_i)
|
46
|
+
|
47
|
+
candidates.map do |display, value|
|
48
|
+
value || display
|
49
|
+
end.select do |value|
|
50
|
+
object_ids.include?(value.object_id)
|
51
|
+
end
|
45
52
|
end
|
46
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: peco_selector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryota Arai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|