peco_selector 0.0.4 → 0.0.5

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: eeb3f824d41626e0fbd5a587e8807f5eedd8b3e8
4
- data.tar.gz: 9eb58646aee69a1618d32006c7bacf2f00e4e349
3
+ metadata.gz: ae48f7ba611079608c1ffd67fb98dea86c6d34f9
4
+ data.tar.gz: a3e69f385d0142a5edf0f7478ddec2ae3e9f4817
5
5
  SHA512:
6
- metadata.gz: cd312aa1c0d9e2af4ea4bc2ecaff698064e8f51e11fc6b6b8d43d7bbf5426f6631402e478d489dc77809dfb731283e357eb53fd4df29bc2025f08dc598ec73eb
7
- data.tar.gz: b1ea515cd879c1d5829b3d754840b3875d51da06244f3ac584eba72e81b528f5f0e6c01b36720e23a0c41c7d06de4a7d61dd675cf1db94f9cb98ed044ed384dd
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
- def self.select_from(candidates, options = {})
7
- Selector.new.select_from(candidates, options)
8
- end
6
+ PECO_BIN = "peco"
7
+ Error = Class.new(StandardError)
8
+ PecoUnavailableError = Class.new(StandardError)
9
9
 
10
- class Selector
11
- PECO_BIN = "peco"
12
- Error = Class.new(StandardError)
10
+ def self.peco_available?
11
+ system('which', PECO_BIN, out: File::NULL)
12
+ end
13
13
 
14
- def select_from(candidates, options = {})
15
- prompt = options[:prompt] || "QUERY>"
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
- stdout_str = nil
18
- stderr_str = nil
20
+ def self.select_from(candidates, options = {})
21
+ ensure_peco_available
19
22
 
20
- Open3.popen3("#{PECO_BIN} --null --prompt #{Shellwords.escape(prompt)}") do |stdin, stdout, stderr, wait_thr|
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
- stdout_str = stdout.read
28
- stderr_str = stderr.read
25
+ stdout_str = nil
26
+ stderr_str = nil
29
27
 
30
- unless wait_thr.value.exitstatus == 0
31
- $stdout.print stdout_str
32
- $stderr.print stderr_str
33
- abort
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
- object_ids = stdout_str.strip.split("\n").map(&:to_i)
35
+ stdout_str = stdout.read
36
+ stderr_str = stderr.read
38
37
 
39
- candidates.map do |display, value|
40
- value || display
41
- end.select do |value|
42
- object_ids.include?(value.object_id)
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
@@ -1,3 +1,3 @@
1
1
  module PecoSelector
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  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
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-05-20 00:00:00.000000000 Z
11
+ date: 2015-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler