Class: InteractiveGrep::Grepper
- Inherits:
-
Object
- Object
- InteractiveGrep::Grepper
- Defined in:
- lib/interactive_grep/grepper.rb
Constant Summary collapse
- DEFAULT_PATTERN =
%r{.}.freeze
- DEFAULT_PROGRESS_INDICATOR_COUNT =
in verbose mode, show signs of life after processing every N lines
100.freeze
- DEBUG =
false.freeze
Instance Attribute Summary collapse
-
#file_names ⇒ Object
readonly
Returns the value of attribute file_names.
-
#initial_pattern ⇒ Object
Returns the value of attribute initial_pattern.
-
#progress_indicator_count ⇒ Object
Returns the value of attribute progress_indicator_count.
Class Method Summary collapse
Instance Method Summary collapse
- #current_file ⇒ Object
- #current_pattern_string ⇒ Object
- #default_pattern? ⇒ Boolean
- #ending ⇒ Object
- #gz? ⇒ Boolean
- #increment_file_index ⇒ Object
-
#initialize(options = nil) ⇒ Grepper
constructor
A new instance of Grepper.
- #matches?(_line, _pattern) ⇒ Boolean
- #reset_file_index ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Grepper
Returns a new instance of Grepper.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/interactive_grep/grepper.rb', line 12 def initialize( = nil ) ||= {} @progress_indicator_count = [:progress_indicator_count] || DEFAULT_PROGRESS_INDICATOR_COUNT @verbose = .has_key?("verbose") ? [ "verbose" ] : DEBUG # puts "verbose: #{@verbose.inspect}" @mode = [ "mode" ] || "normal" @gz = [ "gz" ] @initial_pattern = !![ "pattern" ] ? %r{#{[ "pattern" ]}} : DEFAULT_PATTERN @current_pattern = @initial_pattern reset_file_index @file_names = self.class.globs_to_files( [ "files" ] ) if !current_file || !File.exists?( current_file ) raise "must specify at least one valid file (and/or file_glob)" end puts "using:\n\tfiles: #{file_names}\n\tending in: #{ending}\n\tinitial_pattern: #{current_pattern_string}\n" if verbose? end |
Instance Attribute Details
#file_names ⇒ Object (readonly)
Returns the value of attribute file_names.
9 10 11 |
# File 'lib/interactive_grep/grepper.rb', line 9 def file_names @file_names end |
#initial_pattern ⇒ Object
Returns the value of attribute initial_pattern.
10 11 12 |
# File 'lib/interactive_grep/grepper.rb', line 10 def initial_pattern @initial_pattern end |
#progress_indicator_count ⇒ Object
Returns the value of attribute progress_indicator_count.
10 11 12 |
# File 'lib/interactive_grep/grepper.rb', line 10 def progress_indicator_count @progress_indicator_count end |
Class Method Details
.globs_to_files(files_or_globs) ⇒ Object
32 33 34 35 |
# File 'lib/interactive_grep/grepper.rb', line 32 def self.globs_to_files( files_or_globs ) files_or_globs = files_or_globs.is_a?( Array ) ? files_or_globs : [ files_or_globs ] files_or_globs.map {|file_or_glob| Dir.glob( file_or_glob ) }.flatten end |
Instance Method Details
#current_file ⇒ Object
53 54 55 |
# File 'lib/interactive_grep/grepper.rb', line 53 def current_file file_names[ file_index ] end |
#current_pattern_string ⇒ Object
41 42 43 |
# File 'lib/interactive_grep/grepper.rb', line 41 def current_pattern_string default_pattern? ? "<match every line>" : current_pattern end |
#default_pattern? ⇒ Boolean
37 38 39 |
# File 'lib/interactive_grep/grepper.rb', line 37 def default_pattern? DEFAULT_PATTERN == current_pattern end |
#ending ⇒ Object
45 46 47 |
# File 'lib/interactive_grep/grepper.rb', line 45 def ending File.extname( current_file ) end |
#gz? ⇒ Boolean
49 50 51 |
# File 'lib/interactive_grep/grepper.rb', line 49 def gz? @gz || ending[/.?gz$/] end |
#increment_file_index ⇒ Object
61 62 63 |
# File 'lib/interactive_grep/grepper.rb', line 61 def increment_file_index @file_index += 1 end |
#matches?(_line, _pattern) ⇒ Boolean
65 66 67 68 69 |
# File 'lib/interactive_grep/grepper.rb', line 65 def matches?(_line, _pattern) return _line =~ _pattern rescue ArgumentError return false end |
#reset_file_index ⇒ Object
57 58 59 |
# File 'lib/interactive_grep/grepper.rb', line 57 def reset_file_index @file_index = 0 end |
#run ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/interactive_grep/grepper.rb', line 71 def run counter = 0 results = [] @current_pattern = initial_pattern while line = next_line # if line[@current_pattern] if matches?(line, @current_pattern) line.strip! counter += 1 unless just_count? || ( verbose? && interactive? ) results << line end @current_pattern = prompt( line ) end puts "." if verbose? && 0 == (counter % progress_indicator_count) end reset_file_index puts "no more files.\n" if verbose? if just_count? puts "matched #{counter} line(s)\ndone.\n" counter else puts "done.\n" if verbose? results end end |