Class: Ace::Search::Molecules::FzfIntegrator

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/search/molecules/fzf_integrator.rb

Overview

Integrates with fzf for interactive result selection This is a molecule - composed operation using fzf

Class Method Summary collapse

Class Method Details

.build_fzf_options(options) ⇒ Object

Build fzf command with options



59
60
61
62
63
64
65
66
67
# File 'lib/ace/search/molecules/fzf_integrator.rb', line 59

def self.build_fzf_options(options)
  args = ["fzf"]
  args << "--multi" unless options[:single]
  args << "--preview='cat {3}'" if options[:preview]
  args << "--delimiter='|'"
  args << "--with-nth=2.."

  args.join(" ")
end

.format_result_for_fzf(result, index) ⇒ Object

Format a result for fzf display



32
33
34
35
36
37
38
39
40
41
# File 'lib/ace/search/molecules/fzf_integrator.rb', line 32

def self.format_result_for_fzf(result, index)
  case result[:type]
  when :file
    "#{index}|FILE|#{result[:path]}"
  when :match
    "#{index}|MATCH|#{result[:path]}:#{result[:line]}|#{result[:text]}"
  else
    "#{index}|#{result[:type]}|#{result[:path]}"
  end
end

.run_fzf(lines, options = {}) ⇒ Object

Run fzf and get selected indices



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ace/search/molecules/fzf_integrator.rb', line 44

def self.run_fzf(lines, options = {})
  fzf_options = build_fzf_options(options)
  input = lines.join("\n")

  stdout, _stderr, status = Open3.capture3(fzf_options, stdin_data: input)

  return [] unless status.success?

  # Parse selected lines and extract indices
  stdout.lines.map do |line|
    line.split("|").first.to_i
  end
end

.select(results, options = {}) ⇒ Array<Hash>

Show results in fzf for interactive selection

Parameters:

  • results (Array<Hash>)

    Search results

  • options (Hash) (defaults to: {})

    fzf options

Returns:

  • (Array<Hash>)

    Selected results



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ace/search/molecules/fzf_integrator.rb', line 15

def self.select(results, options = {})
  return results unless Atoms::ToolChecker.fzf_available?
  return results if results.empty?

  # Format results for fzf display
  formatted_lines = results.map.with_index do |result, idx|
    format_result_for_fzf(result, idx)
  end

  # Run fzf
  selected_indices = run_fzf(formatted_lines, options)

  # Return selected results
  selected_indices.map { |idx| results[idx] }
end