Class: PecoSelector::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/peco_selector.rb

Constant Summary collapse

PECO_BIN =
"peco"
Error =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#select_from(candidates, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/peco_selector.rb', line 14

def select_from(candidates, options = {})
  prompt = options[:prompt] || "QUERY>"
  object_ids = nil

  Open3.popen3("#{PECO_BIN} --null --prompt #{Shellwords.escape(prompt)}") do |stdin, stdout, stderr, wait_thr|
    candidates.each do |display, value|
      stdin.puts "#{display}\x00#{value.object_id}"
    end
    stdin.close

    object_ids = stdout.read.strip.split("\n").map(&:to_i)

    unless wait_thr.value.exitstatus == 0
      $stdout.print stdout.read
      $stderr.print stderr.read
      abort
    end
  end

  candidates.each_value.select do |value|
    object_ids.include?(value.object_id)
  end
end