Class: Exfuz::FuzzyFinderCommand

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

Constant Summary collapse

CMDS =
{ fzf: %w[fzf -m], peco: 'peco', percol: 'percol', sk: %w[sk -m] }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command_type: :fzf) ⇒ FuzzyFinderCommand

Returns a new instance of FuzzyFinderCommand.



9
10
11
12
# File 'lib/exfuz/fuzzy_finder_command.rb', line 9

def initialize(command_type: :fzf)
  @selected = ''
  @cmd = CMDS[command_type] || CMDS[:fzf]
end

Instance Attribute Details

#selectedObject (readonly)

Returns the value of attribute selected.



5
6
7
# File 'lib/exfuz/fuzzy_finder_command.rb', line 5

def selected
  @selected
end

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/exfuz/fuzzy_finder_command.rb', line 14

def run
  stdio = IO.popen(@cmd, 'r+')
  fiber = Fiber.new do |init_line|
    puts_line(stdio, init_line)
    loop do
      line = Fiber.yield
      puts_line(stdio, line)
    end
  end

  yield fiber
ensure
  stdio.close_write
  @selected = stdio.each_line(chomp: true).to_a
  stdio.close_read
end