[py-svn] r36270 - py/dist/py/bin

cfbolz at codespeak.net cfbolz at codespeak.net
Mon Jan 8 14:51:15 CET 2007


Author: cfbolz
Date: Mon Jan  8 14:51:13 2007
New Revision: 36270

Modified:
   py/dist/py/bin/py.lookup
Log:
color the search term coloured in the output


Modified: py/dist/py/bin/py.lookup
==============================================================================
--- py/dist/py/bin/py.lookup	(original)
+++ py/dist/py/bin/py.lookup	Mon Jan  8 14:51:13 2007
@@ -3,6 +3,7 @@
 import sys, os
 sys.path.insert(0, os.path.dirname(__file__))
 from _findpy import py
+from py.__.misc.terminal_helper import ansi_print, terminal_width
 import re
 
 curdir = py.path.local()
@@ -17,6 +18,18 @@
 parser.add_option("-C", "--context", action="store", type="int", dest="context",
             default=0, help="How many lines of output to show")
 
+def find_indexes(search_line, string):
+    indexes = []
+    before = 0
+    while 1:
+        i = search_line.find(string, before)
+        if i == -1:
+            break
+        indexes.append(i)
+        before = i + len(string)
+    return indexes
+
+
 if __name__ == '__main__':
     (options, args) = parser.parse_args()
     string = args[0]
@@ -34,11 +47,20 @@
             else:
                 searchlines = lines
             for i, (line, searchline) in enumerate(zip(lines, searchlines)): 
-                if searchline.find(string) != -1:
-                    if not options.context:
-                        print "%s:%d:  %s" %(x.relto(curdir), i+1, line.rstrip())
-                    else:
-                        context = (options.context)/2
-                        for count in range(max(0, i-context), min(len(lines) - 1, i+context+1)):
-                            print "%s:%d:  %s" %(x.relto(curdir), count+1, lines[count].rstrip())
-                        print "-"*50
+                indexes = find_indexes(searchline, string)
+                if not indexes:
+                    continue
+                if not options.context:
+                    sys.stdout.write("%s:%d: " %(x.relto(curdir), i+1))
+                    last_index = 0
+                    for index in indexes:
+                        sys.stdout.write(line[last_index: index])
+                        ansi_print(line[index: index+len(string)],
+                                   file=sys.stdout, esc=31, newline=False)
+                        last_index = index + len(string)
+                    sys.stdout.write(line[last_index:] + "\n")
+                else:
+                    context = (options.context)/2
+                    for count in range(max(0, i-context), min(len(lines) - 1, i+context+1)):
+                        print "%s:%d:  %s" %(x.relto(curdir), count+1, lines[count].rstrip())
+                    print "-" * terminal_width



More information about the pytest-commit mailing list