def main(): |
(options, args) = parser.parse_args() |
string = args[0] |
if options.ignorecase: |
string = string.lower() |
for x in curdir.visit('*.py', rec): |
try: |
s = x.read() |
except py.error.ENOENT: |
pass |
searchs = s |
if options.ignorecase: |
searchs = s.lower() |
if s.find(string) != -1: |
lines = s.splitlines() |
if options.ignorecase: |
searchlines = s.lower().splitlines() |
else: |
searchlines = lines |
for i, (line, searchline) in enumerate(zip(lines, searchlines)): |
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 |