Class: Ripgrep::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ripgrep/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(verbose: false) ⇒ Client

Returns a new instance of Client.



9
10
11
# File 'lib/ripgrep/client.rb', line 9

def initialize(verbose: false)
  @verbose = verbose
end

Instance Method Details

#exec(*args, opts) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ripgrep/client.rb', line 13

def exec(*args, opts)
  unless opts.is_a? Hash
    args << opts
    opts = {}
  end
  verbose = opts[:verbose].nil? ? @verbose : !!opts[:verbose]
  cli_options = opts[:options]&.flat_map do |key, val|
    next [] unless val
    key = key.to_s.tr('_', '-')

    if val.is_a? Array
      # For arrays, create multiple --key=val options
      val.map { |v| "--#{key}=#{v}" }
    elsif val.is_a? TrueClass
      # For true, create --key option without value
      ["--#{key}"]
    elsif val.is_a? String
      # For strings, always pass through (empty string becomes --key=)
      ["--#{key}=#{val}"]
    else
      # For other values, convert to string
      ["--#{key}=#{val}"]
    end
  end&.compact || []
  puts "cli_options: #{cli_options}" if verbose
  cli_arguments = cli_options + args
  cli_arguments << (opts[:path] || '.')
  puts "cli_arguments: #{cli_arguments}" if verbose
  Core.exec(*cli_arguments, verbose: verbose)
end

#run(&block) ⇒ Object



44
45
46
# File 'lib/ripgrep/client.rb', line 44

def run(&block)
  instance_eval(&block)
end