python3inizing scripts

This commit is contained in:
Ville Rantanen
2020-10-12 11:37:33 +03:00
parent 877acc0d35
commit 52db6b9bf4
8 changed files with 1328 additions and 934 deletions

View File

@@ -1,104 +1,156 @@
# Python library for ansi colorization
import re,sys
import re, sys
class code:
K="\033[1;30m"
R="\033[1;31m"
G="\033[1;32m"
B="\033[1;34m"
Y="\033[1;33m"
M="\033[1;35m"
C="\033[1;36m"
W="\033[1;37m"
K = "\033[1;30m"
R = "\033[1;31m"
G = "\033[1;32m"
B = "\033[1;34m"
Y = "\033[1;33m"
M = "\033[1;35m"
C = "\033[1;36m"
W = "\033[1;37m"
k="\033[0;30m"
r="\033[0;31m"
g="\033[0;32m"
b="\033[0;34m"
y="\033[0;33m"
m="\033[0;35m"
c="\033[0;36m"
w="\033[0;37m"
k = "\033[0;30m"
r = "\033[0;31m"
g = "\033[0;32m"
b = "\033[0;34m"
y = "\033[0;33m"
m = "\033[0;35m"
c = "\033[0;36m"
w = "\033[0;37m"
bk="\033[40m"
br="\033[41m"
bg="\033[42m"
by="\033[43m"
bb="\033[44m"
bm="\033[45m"
bc="\033[46m"
bw="\033[47m"
bk = "\033[40m"
br = "\033[41m"
bg = "\033[42m"
by = "\033[43m"
bb = "\033[44m"
bm = "\033[45m"
bc = "\033[46m"
bw = "\033[47m"
S = '\033[1m'#strong
s = '\033[2m'#strong off
U = '\033[4m'#underline
u = '\033[24m'#underline off
Z = '\033[0m'#zero colors
ic = '\033[7m'#inverse colors
io = '\033[27m'#inverse off
st = '\033[9m'#strike on
so = '\033[29m'#strike off
CLR = '\033[2J'
CLREND = '\033[K'
CLRBEG = '\033[1K'
CLRSCR = CLR+"\033[0;0H"
S = "\033[1m" # strong
s = "\033[2m" # strong off
U = "\033[4m" # underline
u = "\033[24m" # underline off
Z = "\033[0m" # zero colors
ic = "\033[7m" # inverse colors
io = "\033[27m" # inverse off
st = "\033[9m" # strike on
so = "\033[29m" # strike off
CLR = "\033[2J"
CLREND = "\033[K"
CLRBEG = "\033[1K"
CLRSCR = CLR + "\033[0;0H"
color_keys="K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,s,U,u,Z,ic,io,st,so,bk,br,bg,by,bb,bm,bc,bw,CLR,CLREND,CLRBEG,CLRSCR".split(",")
color_list=[K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,s,U,u,Z,ic,io,st,so,bk,br,bg,by,bb,bm,bc,bw,CLR,CLREND,CLRBEG,CLRSCR]
custom_match=re.compile(r'(\${)([0-9;]*[ABCDEFGHJKSTfminsu]+)(})')
color_keys = "K,R,G,B,Y,M,C,W,k,r,g,b,y,m,c,w,S,s,U,u,Z,ic,io,st,so,bk,br,bg,by,bb,bm,bc,bw,CLR,CLREND,CLRBEG,CLRSCR".split(
","
)
color_list = [
K,
R,
G,
B,
Y,
M,
C,
W,
k,
r,
g,
b,
y,
m,
c,
w,
S,
s,
U,
u,
Z,
ic,
io,
st,
so,
bk,
br,
bg,
by,
bb,
bm,
bc,
bw,
CLR,
CLREND,
CLRBEG,
CLRSCR,
]
custom_match = re.compile(r"(\${)([0-9;]*[ABCDEFGHJKSTfminsu]+)(})")
def pos(self,y,x):
def pos(self, y, x):
""" Go to absolute position """
return "\033["+str(y)+";"+str(x)+"H"
def column(self,x):
""" Go to absolute column """
return "\033["+str(x)+"G"
return "\033[" + str(y) + ";" + str(x) + "H"
def posprint(self, y,x,s):
def column(self, x):
""" Go to absolute column """
return "\033[" + str(x) + "G"
def posprint(self, y, x, s):
""" Print string at a location """
sys.stdout.write( self.pos(y,x) + str(s) )
sys.stdout.write(self.pos(y, x) + str(s))
self.flush()
def clear(self):
sys.stdout.write( self.CLRSCR+self.pos(0,0) )
def clear_to_end(self):
sys.stdout.write( self.CLREND )
def clear_to_beginning(self):
sys.stdout.write( self.CLRBEG )
sys.stdout.write(self.CLRSCR + self.pos(0, 0))
def up(self,n=1):
sys.stdout.write( "\033["+str(n)+"A" )
def down(self,n=1):
sys.stdout.write( "\033["+str(n)+"B" )
def right(self,n=1):
sys.stdout.write( "\033["+str(n)+"C" )
def left(self,n=1):
sys.stdout.write( "\033["+str(n)+"D" )
def up_line(self,n=1):
sys.stdout.write( "\033["+str(n)+"F" )
def down_line(self,n=1):
sys.stdout.write( "\033["+str(n)+"E" )
def clear_to_end(self):
sys.stdout.write(self.CLREND)
def clear_to_beginning(self):
sys.stdout.write(self.CLRBEG)
def up(self, n=1):
sys.stdout.write("\033[" + str(n) + "A")
def down(self, n=1):
sys.stdout.write("\033[" + str(n) + "B")
def right(self, n=1):
sys.stdout.write("\033[" + str(n) + "C")
def left(self, n=1):
sys.stdout.write("\033[" + str(n) + "D")
def up_line(self, n=1):
sys.stdout.write("\033[" + str(n) + "F")
def down_line(self, n=1):
sys.stdout.write("\033[" + str(n) + "E")
def save(self):
""" Save cursor position """
sys.stdout.write( "\033[s" )
sys.stdout.write("\033[s")
def restore(self):
""" Restore cursor position """
sys.stdout.write( "\033[u" )
sys.stdout.write("\033[u")
def color_string(self,s):
for i,c in enumerate(self.color_keys):
s=s.replace("${"+c+"}",self.color_list[i])
def color_string(self, s):
for i, c in enumerate(self.color_keys):
s = s.replace("${" + c + "}", self.color_list[i])
return self.custom_color(s)
def nocolor_string(self,s):
for i,c in enumerate(self.color_keys):
s=s.replace("${"+c+"}","")
def nocolor_string(self, s):
for i, c in enumerate(self.color_keys):
s = s.replace("${" + c + "}", "")
return self.custom_nocolor(s)
def custom_color(self,s):
return self.custom_match.sub('\033[\\2',s)
def custom_nocolor(self,s):
return self.custom_match.sub('',s)
def custom_color(self, s):
return self.custom_match.sub("\033[\\2", s)
def custom_nocolor(self, s):
return self.custom_match.sub("", s)
def get_keys(self):
return self.color_keys
@@ -109,8 +161,8 @@ class code:
def demo():
""" Print all control sequences """
c=code()
unformatted="""${S}ANSI CODES
c = code()
unformatted = """${S}ANSI CODES
==========${Z}
${S}Fo${U}rm${st}at${u}ti${ic}ng${Z}
${S}==========${Z}
@@ -142,4 +194,4 @@ ${S}=====================${Z}
C .right() Right y;xH .pos() Absolute Position
D .left() Left """
return(c.color_string(unformatted))
return c.color_string(unformatted)