LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Blogs > freemedia2018
User Name
Password

Notices


Rate this Entry

highlighter (cli) -- highlight one or more text searches in one or more colours

Posted 04-25-2019 at 02:05 AM by freemedia2018

Code:
cat highlighter | ./highlighter "def|print| |:|highlight|return 1 2 -2 4 5 6 10"
colours: 0 (black) 1 (blue) 2 (green) 3 (cyan) 4 (red) 5 (magenta) 6 (brown) 7 (white)
8 (grey) 9 (l.blue) 10 (l.green) 11 (l.cyan) 12 (l.red) 13 (l.magenta) 14 (yellow) 15 (b.white)

backgrounds: -1 (blue) -2 (green) -3 (cyan) -4 (red) -5 (magenta) -6 (brown) -7 (white)

https://imgtc.com/i/PxhCp5H.png

Code:
#!/usr/bin/env python
# coding: utf-8 
#### license: creative commons cc0 1.0 (public domain) 
#### http://creativecommons.org/publicdomain/zero/1.0/ 
proginf = "highlighter 0.1, apr 2019 mn"
import sys
import os
from sys import stdin, stdout
from os import name as osname
nl = chr(10)

if osname == "nt":
    nl = chr(13) + chr(10) 
    try: from colorama import init ; init()
    except: pass

def colourtext(f, b):
    if f == None: f = 0
    if b == None: b = 0
    n = "0"
    if f > 7: n = "1" ; f = f - 8
    if f == 1: f = 4 ## switch ansi colours for qb colours
    elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
    if f == 3: f = 6
    elif f == 6: f = 3
    if b > 7: b = b - 8
    if b == 1: b = 4
    elif b == 4: b = 1
    if b == 3: b = 6
    elif b == 6: b = 3
    stdout.write(chr(27) + "[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
    stdout.flush()

def highlight(x, b):
    f = None
    if f == None: f = 0
    if b == None: b = 0
    n = "0"
    if f > 7: n = "1" ; f = f - 8
    if f == 1: f = 4 ## switch ansi colours for qb colours
    elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
    if f == 3: f = 6
    elif f == 6: f = 3
    if b > 7: b = b - 8
    if b == 1: b = 4
    elif b == 4: b = 1
    if b == 3: b = 6
    elif b == 6: b = 3
    stdout.write("\x1b[" + n + str(40+b) + "m") ; stdout.flush()

def prints(p): 
    stdout.write(str(p))
    sys.stdout.flush()

def left(p, s): 
    return p[:s]

def right(p, s): 
    return p[-s:]

def arrstdin():
    ps = []
    for textlines in stdin: 
        p = textlines[:]
        if p:
            if p[-1] != chr(10):
                p += chr(10)
        if osname == "nt":
            ps += [p[:-1].strip()]
        else:
            ps += [p[:-1]]
    return ps[:]

def fjoin(x, s):
    t = ""
    if len(x) : t = str(x[0])
    for c in range(len(x)):
        if c > 0: t += str(s) + str(x[c]) 
    return t

def split(x, s):
    return x.split(s)

def reverse(p): 
    if type(p) == str:
        p = map(str, p) ; p.reverse()
        p = "".join(p)
        return p

colours = ""
p = fjoin(sys.argv[1:], " ")

for each in reverse(p):
    if each in "1234567890 -":
        colours += each
    else: break

p = split(left(p, len(p) - len(colours)).strip().lower(), "|")
colours = split(reverse(colours).strip(), " ")

for each in range(len(colours)):
    try: 
        colours[each] = int(colours[each])
    except:
        colours[each] = 14
    if colours[each] < -15: colours[each] = 14
    if colours[each] > 15: colours[each] = 14

while 1:
   if len(colours) < len(p):
        colours += [14]
   else:
        break

tab = chr(9)

for textlines in arrstdin():
    printed = ""
    for each in textlines:
        printed += each
        for searches in p:
            if right(printed.lower(), len(searches)) == searches:
                prints(left(printed, len(printed) - len(searches)))
                if colours[p.index(searches)] < 0:
                    hico = [7, 7, 7, 0, 7, 7, 7, 0] * 2
                    colourtext(hico[colours[p.index(searches)] * -1], 0)
                    highlight(0, colours[p.index(searches)] * -1)
                else:
                    colourtext(colours[p.index(searches)], 0)
                prints(right(printed, len(searches)))
                printed = ""
                colourtext(7, 0)
                break
    print(printed)
Posted in Uncategorized
Views 1208 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration