#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  highlight.py
#  shelly
#

"""
Highlight the matching regular expression in each line.
"""

import sys
import optparse
import re

from clint.textui import colored
from clint.packages import colorama


def highlight(pattern, istream=sys.stdin, ostream=sys.stdout, color='red'):
    if color not in colored.COLORS:
        raise ValueError('expected one of red/blue/green, got %s' % color)
    color = color.upper()
    regex = re.compile(pattern)
    for l in istream:
        matches = [m.span() for m in regex.finditer(l)]
        if not matches:
            ostream.write(l)
        else:
            matches.reverse()
            i = 0
            while matches:
                start, stop = matches.pop()
                ostream.write(l[i:start])
                ostream.write('%s%s%s' % (
                        getattr(colorama.Fore, color),
                        l[start:stop],
                        colorama.Fore.RESET,
                    ))
                i = stop
            ostream.write(l[i:])


def _create_option_parser():
    usage = \
"""%prog highlight [options] pattern

Operates like a mixture of grep and cat. Passes stdin through to stdout, but
highlighting the given pattern wherever it's found."""

    parser = optparse.OptionParser(usage)
    parser.add_option('--blue', action='store_true', dest='blue')
    parser.add_option('--red', action='store_true', dest='red',
            default=True)
    parser.add_option('--green', action='store_true', dest='green')

    return parser


def main(argv):
    parser = _create_option_parser()
    (options, args) = parser.parse_args(argv)

    if len(args) != 1:
        parser.print_help()
        sys.exit(1)

    color = 'blue' if options.blue else 'green' if options.green else 'red'
    highlight(*args, color=color)


if __name__ == '__main__':
    main(sys.argv[1:])

@@ -1060,16 +1060,466 @@
 %5Bi:%5D)%0A%0A%0A
+def highlight_fields(field_list, delimiter, color, istream=sys.stdin,%0A        ostream=sys.stdout):%0A    color = color.upper()%0A    for l in istream:%0A        parts = l.split(delimiter)%0A        for i in field_list:%0A            parts%5Bi%5D = '%25s%25s%25s' %25 (%0A                        getattr(colorama.Fore, color),%0A                        parts%5Bi%5D,%0A                        colorama.Fore.RESET,%0A                    )%0A        ostream.write(delimiter.join(parts))%0A%0A%0A
 def _cre
@@ -1589,16 +1589,55 @@
  pattern
+%0A%25prog highlight %5Boptions%5D -f %3Cfieldno%3E
 %0A%0AOperat
@@ -2033,309 +2033,785 @@
 n')%0A
-%0A    return parser%0A%0A%0Adef main(argv):%0A    parser = _create_option_parser()%0A    (options, args) = parser.parse_args(argv)%0A%0A    if len(args) != 1:%0A        parser.print_help()%0A        sys.exit(1
+    parser.add_option('-f', '--fields', action='store', dest='fields',%0A            help='A comma-separated list of field numbers to highlight')%0A    parser.add_option('-d', '--delimiter', action='store', dest='delimiter',%0A            default=',', help='The delimiter between fields.')%0A%0A    return parser%0A%0A%0Adef main(argv):%0A    parser = _create_option_parser()%0A    (options, args) = parser.parse_args(argv)%0A%0A    color = 'blue' if options.blue else 'green' if options.green else 'red'%0A    if options.fields and len(args) == 0:%0A        fields = map(int, options.fields.split(',')
 )%0A
-%0A
     
-color = 'blue' if options.blue else 'green' if options.green else 'red'%0A    highlight(*args, color=color
+    highlight_fields(fields, options.delimiter, color)%0A    elif not options.fields and len(args) == 1:%0A        highlight(*args, color=color)%0A    else:%0A        parser.print_help()%0A        sys.exit(1
 )%0A%0A%0A

Add the ability to highlight a CSV field.