def main(): arguments = docopt(__doc__, version=__version__) html_parser = HTMLParser() t = Terminal() output = [] try: limit = int(arguments['--limit']) except: limit = 50 # Sigh. Numbers are so hard to type. url = '{server}/{tree}/search'.format(server=arguments['--server'], tree=arguments['--tree']) response = requests.get(url, params={ 'q': ' '.join(arguments['<query>']), 'redirect': False, 'format': 'json', 'case': not arguments['--case-insensitive'], 'limit': limit, 'offset': 0 }) if response.status_code != 200: output.append('Search failed: DXR returned {status} {reason}'.format( status=response.status_code, reason=response.reason, )) else: response_json = response.json() formatter = Terminal256Formatter(style=arguments['--style']) if len(response_json['results']) < 1: output.append('No results found.') else: for result in response_json['results']: output.append(t.green(result['path'])) # Find maximum line number length so we can line them up. max_log = max(log10(l['line_number']) for l in result['lines']) lineno_len = int(ceil(max_log)) line_template = (u' {t.yellow}{{lineno:>{lineno_len}}}{t.normal} {{line}}' .format(t=t, lineno_len=lineno_len)) try: lexer = get_lexer_for_filename(result['path']) except ClassNotFound: lexer = None # No highlighting for you! for line_result in result['lines']: line = html_parser.unescape(line_result['line']) # Find highlight term and remove <b> tags. match = re.search(r'<b>(?P<term>.+)</b>', line) highlight_term = match.group('term') if match else None line = line.replace('<b>', '').replace('</b>', '') # Highlight normally and add underline. if not arguments['--no-highlight'] and lexer: line = highlight(line, lexer, formatter).strip('\n') line = line.replace(highlight_term, t.underline_bold(highlight_term)) output.append(line_template.format( lineno=unicode(line_result['line_number']), line=line )) output.append('') output = '\n'.join(output) if arguments['--pager']: p = subprocess.Popen(arguments['--pager'], stdin=subprocess.PIPE, shell=True) p.communicate(input=output) p.wait() else: print output