Fixed issue grep "x" | cat doesn't work
Modified logic around selection of lines and coloring parts of line, which also rendered fn_predicate obsolete
@@ -24,21 +24,17 @@ def main(args):
pattern = re.compile(ns.pattern, flags=flags)
- if ns.invert:
- def fn_predicate(line, newline):
- return line == newline
- else:
- def fn_predicate(line, newline):
- return line != newline
-
# Do not try to grep directories
files = [f for f in ns.files if not os.path.isdir(f)]
fileinput.close() # in case it is not closed
try:
for line in fileinput.input(files):
+ if bool(pattern.search(line)) != ns.invert:
+ if ns.invert: # optimize: if ns.invert, then no match, so no highlight color needed
+ newline = line
+ else:
newline = re.sub(pattern, lambda m: _stash.text_color(m.group(), 'red'), line)
- if fn_predicate(line, newline):
if fileinput.isstdin():
fmt = u'{lineno}: {line}'
else:
