Commits

Bernhard Leiner committed db63efa

add --noheading command line feature (ack feature)

with the argument, the filename is included at the start of each matching line

Comments (0)

Files changed (4)

psslib/defaultpssoutputformatter.py

             do_colors=True,
             match_color_str=None,
             filename_color_str=None,
+            do_heading=True,
             prefix_filename_to_file_matches=True,
             show_column_of_first_match=False,
             stream=None):
         self.do_colors = do_colors
         self.prefix_filename_to_file_matches = prefix_filename_to_file_matches
+        self.do_heading = do_heading
+        self.inline_filename = (True if prefix_filename_to_file_matches and not do_heading
+                                else False)
         self.show_column_of_first_match = show_column_of_first_match
 
         self.style_match = (decode_colorama_color(match_color_str) or
         self.stream = stream or sys.stdout
 
     def start_matches_in_file(self, filename):
-        if self.prefix_filename_to_file_matches:
+        if self.prefix_filename_to_file_matches and self.do_heading:
             self._emit_colored(filename, self.style_filename)
             self._emitline()
 
     def end_matches_in_file(self, filename):
         self._emitline()
 
-    def matching_line(self, matchresult):
+    def matching_line(self, matchresult, filepath):
+        if self.inline_filename:
+            self._emit('%s:' % filepath)
         self._emit('%s:' % matchresult.matching_lineno)
         first_match_range = matchresult.matching_column_ranges[0]
         if self.show_column_of_first_match:
                 chunk = line[match_end:next_start]
             self._emit(chunk)
 
-    def context_line(self, line, lineno):
+    def context_line(self, line, lineno, filepath):
+        if self.inline_filename:
+            self._emit('%s-' % filepath)
         self._emit('%s-' % lineno)
         if self.show_column_of_first_match:
             self._emit('1-')
         match_color_str=None,
         filename_color_str=None,
         do_break=True,
+        do_heading=True,
         prefix_filename_to_file_matches=True,
         show_column_of_first_match=False,
         ncontext_before=0,
             do_colors=do_colors,
             match_color_str=match_color_str,
             filename_color_str=filename_color_str,
+            do_heading=do_heading,
             prefix_filename_to_file_matches=prefix_filename_to_file_matches,
             show_column_of_first_match=show_column_of_first_match)
 
                         prev_was_blank = True
                         continue
                     elif result == LINE_MATCH:
-                        output_formatter.matching_line(match)
+                        output_formatter.matching_line(match, filepath)
                     elif result == LINE_CONTEXT:
                         if prev_was_blank and had_context:
                             output_formatter.context_separator()
-                        output_formatter.context_line(line, n)
+                        output_formatter.context_line(line, n, filepath)
                         had_context = True
                     prev_was_blank = False
             else:
                 # just show the matches without considering context
                 for match in matches:
-                    output_formatter.matching_line(match)
+                    output_formatter.matching_line(match, filepath)
 
             if do_break:
                 output_formatter.end_matches_in_file(filepath)
                 match_color_str=options.color_match,
                 filename_color_str=options.color_filename,
                 do_break=options.do_break,
+                do_heading=options.do_heading,
                 prefix_filename_to_file_matches=options.prefix_filename,
                 show_column_of_first_match=options.show_column,
                 ncontext_before=ncontext_before,
     group_output.add_option('--nobreak',
         action='store_false', dest='do_break', default=True,
         help='Print no break between results from different files')
+    group_output.add_option('--noheading',
+        action='store_false', dest='do_heading', default=True,
+        help="Print no file name heading above each file's results")
     optparser.add_option_group(group_output)
 
     group_filefinding = optparse.OptionGroup(optparser, 'File finding')
         self.output.append(
             ('END_MATCHES', os.path.normpath(relpath)))
         
-    def matching_line(self, matchresult):
+    def matching_line(self, matchresult, filename):
         self.output.append(('MATCH',
             (matchresult.matching_lineno, matchresult.matching_column_ranges)))
 
-    def context_line(self, line, lineno):
+    def context_line(self, line, lineno, filename):
         self.output.append(('CONTEXT', lineno))
 
     def context_separator(self):