lib/ronin/cli/commands/grep.rb in ronin-2.0.0 vs lib/ronin/cli/commands/grep.rb in ronin-2.0.1
- old
+ new
@@ -134,11 +134,11 @@
# Additional file arguments to grep.
#
def run(*files)
unless @pattern
print_error "must specify a pattern to search for"
- exit -1
+ exit(-1)
end
super(*files)
end
@@ -149,12 +149,12 @@
# The input stream to grep.
#
def process_input(input)
filename = filename_of(input)
- input.each_line(chomp: true).each_with_index do |line,index|
- match_line(line, filename: filename, line_number: index+1)
+ input.each_line(chomp: true).with_index do |line,index|
+ match_line(line, filename: filename, line_number: index + 1)
end
end
#
# Returns the file name for the IO stream.
@@ -182,27 +182,29 @@
#
# @option kwargs [Integer] :line_number
#
def match_line(line,**kwargs)
index = 0
+
printed_prefix = false
+ only_matching = options[:only_matching]
while (match = line.match(@pattern,index))
unless printed_prefix
print_line_prefix(**kwargs)
printed_prefix = true
end
match_start, match_stop = match.offset(0)
# print the text before the match, unless --only-matching is enabled
- print(line[index...match_start]) unless options[:only_matching]
+ print(line[index...match_start]) unless only_matching
print_match(match)
index = match_stop
end
- unless options[:only_matching]
+ unless only_matching
# print the rest of the line, if we've had at least one match
puts(line[index..]) if index > 0
end
end