highlight change color of words in a file to std.out.

I wanted a way for the color of the similar words I find to be highlighted when you print the file.  print() in python is basically std.out()

so here's the bit I added to the previous post to make the similar words green when it prints out to command shell. I also added a line count, cause thats handy too.

update:  I realized there was a bug in the original.  Lists did not properly highlight the original word correctly, or the whole word like I wanted it to.  11-25-2017
The whole parser is in the newer posts.  CTRL F with parser python
args.file is the file it reads
args.similate is the word searched for
args.ignore is to tell the parser to ignore Case of letters

def highlight(args):
    with open(args.file) as f:
        linecount = 0
        count = 0
        # green
        stop_color = '\x1b[0m'
        color = '\x1b[1;32;40m'
        # blue highlighted
        solidblue = '\x1b[2;30;46m'
        for line in f:
            index = 0
            linecount += 1
            newline = line.split()
            if not args.ignore:
                for words in newline:
                    current = newline[index]
                    index += 1
                    if args.similate in current:   
                        words = args.similate                     
                        count += 1
                        newcolor = color + words + stop_color
                        line = line.replace(words, newcolor)
            elif args.ignore:
                for words in newline:
                    current = newline[index]
                    current1 = current.lower()
                    similate1 = args.similate
                    similate1 = similate1.lower()
                    index += 1
                    if similate1 in current1:   
                        count += 1
                        newcolor = color + current + stop_color
                        line = line.replace(current, newcolor)
            print(f"{linecount}  {line}", end='')

        print(f" \n {solidblue} similarity count =  {count}{stop_color} ")


Popular posts from this blog

Statistics, Python, Making a Stem Plot

Pandas Python and a little probability

JavaScript Ascii animation with while loops and console.log