Class: RubyEdit::Grep

Inherits:
Command show all
Defined in:
lib/ruby_edit/grep.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#command, #cursor, #editor, #exec_exist?, #execute, #generator, #pager, #platform, #prompt, #screen, #which

Constructor Details

#initialize(options) ⇒ Grep

Returns a new instance of Grep.



9
10
11
12
# File 'lib/ruby_edit/grep.rb', line 9

def initialize(options)
  @path       = options[:path] || '*'
  @expression = options[:expression]
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



7
8
9
# File 'lib/ruby_edit/grep.rb', line 7

def result
  @result
end

Instance Method Details

#search(output: $stdout, errors: $stderr) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby_edit/grep.rb', line 14

def search(output: $stdout, errors: $stderr)
  if empty_expression?
    output.puts 'No expression given'
    return false
  end
  @result = run "grep -irn #{@path} -e '#{@expression}'" do |out, err|
    errors << err if err
  end
rescue TTY::Command::ExitError => error
  output.puts error
end