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

View File

@@ -1,72 +1,62 @@
#!/usr/bin/env python #!/usr/bin/env python3
import sys, os import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__))) sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import ansicodes import ansicodes
from argparse import ArgumentParser from argparse import ArgumentParser
def setup_options(): def setup_options():
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument( parser.add_argument("-v", default=False, action="store_true")
"-v",
default = False,
action = "store_true"
)
parser.add_argument( parser.add_argument(
"cs", "cs",
type = str, type=str,
default = "cp437", default="cp437",
nargs = '?', nargs="?",
help = "Character set to show, ex. ascii, cp437, cp850, cp1250, latin1 ..." help="Character set to show, ex. ascii, cp437, cp850, cp1250, latin1 ...",
) )
opts=parser.parse_args() opts = parser.parse_args()
return opts return opts
def long_format(table): def long_format(table):
bc = ansicodes.code() bc = ansicodes.code()
for x,col in enumerate(table): for x, col in enumerate(table):
for y,row in enumerate(col): for y, row in enumerate(col):
bc.posprint( bc.posprint(
y + 2, y + 2, 1 + x * 7, bc.color_string("${c}%03d: ${G}%s${Z}" % (row))
1 + x*7,
bc.color_string(
"${c}%03d: ${G}%s${Z}"%( row )
)
) )
def short_format(table): def short_format(table):
bc = ansicodes.code() bc = ansicodes.code()
for x,col in enumerate(table): for x, col in enumerate(table):
for y,row in enumerate(col): for y, row in enumerate(col):
bc.posprint( bc.posprint(x + 3, y * 2 + 1, bc.color_string("${G}%s${Z}" % (row[1],)))
x + 3, print("")
y * 2 + 1,
bc.color_string(
"${G}%s${Z}"%( row[1], )
)
)
print('')
if __name__ == "__main__": if __name__ == "__main__":
opts = setup_options() opts = setup_options()
table=[[] for x in range(8)] table = [[] for x in range(8)]
for c in range(0,256): for c in range(0, 256):
col = c/32 col = int(c / 32)
table[col].append((c,chr(c).decode(opts.cs,'ignore').encode('utf-8','ignore'))) table[col].append(
#sys.stdout.write(chr(c).decode('latin1')) (c, chr(c))
)
# sys.stdout.write(chr(c).decode('latin1'))
# remove control chars # remove control chars
table.pop(0) table.pop(0)
bc = ansicodes.code() bc = ansicodes.code()
bc.clear() bc.clear()
if opts.v: if opts.v:
lines = int(25 - len(opts.cs)/2 - 2) lines = int(25 - len(opts.cs) / 2 - 2)
else: else:
lines = int(32 - len(opts.cs)/2 - 2) lines = int(32 - len(opts.cs) / 2 - 2)
print(bc.color_string("="*lines + " ${Y}%s${Z} "%( opts.cs, ) + "="*lines)) print(bc.color_string("=" * lines + " ${Y}%s${Z} " % (opts.cs,) + "=" * lines))
if opts.v: if opts.v:
long_format(table) long_format(table)
else: else:
short_format(table) short_format(table)
print('') print("")

View File

@@ -1,69 +1,75 @@
#!/usr/bin/env python #!/usr/bin/env python3
import sys import sys
import os import os
import pwd import pwd
from argparse import ArgumentParser from argparse import ArgumentParser
MINSIZE=0 MINSIZE = 0
def setup_options(): def setup_options():
parser=ArgumentParser(description="List file usage") parser = ArgumentParser(description="List file usage")
parser.add_argument('startpath', action="append", nargs='+') parser.add_argument("startpath", action="append", nargs="+")
options=parser.parse_args() options = parser.parse_args()
return options return options
def add_recurse(db,options,start):
for path,dirs,files in os.walk(start,followlinks=False): def add_recurse(db, options, start):
files=clean_syms(files,path) for path, dirs, files in os.walk(start, followlinks=False):
files = clean_syms(files, path)
for f in files: for f in files:
filename=os.path.join(path,f) filename = os.path.join(path, f)
stats=os.stat(filename) stats = os.stat(filename)
owner=stats.st_uid owner = stats.st_uid
size=stats.st_size size = stats.st_size
if not owner in db: if not owner in db:
db[owner]=0 db[owner] = 0
db[owner]+=size db[owner] += size
return db return db
def clean_syms(files,path):
nonsyms=[] def clean_syms(files, path):
nonsyms = []
for f in files: for f in files:
if not os.path.islink(os.path.join(path,f)): if not os.path.islink(os.path.join(path, f)):
nonsyms.append(f) nonsyms.append(f)
return nonsyms return nonsyms
def print_db(db): def print_db(db):
namedb=dict() namedb = dict()
for u in db: for u in db:
try: try:
namedb[pwd.getpwuid(u).pw_name]=humanize_size(db[u]) namedb[pwd.getpwuid(u).pw_name] = humanize_size(db[u])
except: except:
namedb[str(u)]=humanize_size(db[u]) namedb[str(u)] = humanize_size(db[u])
names=namedb.keys() names = list(namedb.keys())
names.sort() names.sort()
for n in names: for n in names:
print(n+": "+namedb[n]) print(n + ": " + namedb[n])
def humanize_size(size,precision=1):
if size==None: def humanize_size(size, precision=1):
return 'nan' if size == None:
suffixes=['B','KB','MB','GB','TB'] return "nan"
suffixes = ["B", "KB", "MB", "GB", "TB"]
suffixIndex = 0 suffixIndex = 0
defPrecision=0 defPrecision = 0
while size > 1024: while size > 1024:
suffixIndex += 1 #increment the index of the suffix suffixIndex += 1 # increment the index of the suffix
size = size/1024.0 #apply the division size = size / 1024.0 # apply the division
defPrecision=precision defPrecision = precision
return "%.*f%s"%(defPrecision,size,suffixes[suffixIndex]) return "%.*f%s" % (defPrecision, size, suffixes[suffixIndex])
def main(): def main():
options=setup_options(); options = setup_options()
db=dict() db = dict()
for f in options.startpath[0]: for f in options.startpath[0]:
db=add_recurse(db,options,f) db = add_recurse(db, options, f)
print_db(db) print_db(db)
sys.exit(0) sys.exit(0)
main()
main()

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -1,108 +1,149 @@
#!/usr/bin/env python #!/usr/bin/env python3
import sys,os,re import sys, os, re
from argparse import ArgumentParser, RawDescriptionHelpFormatter from argparse import ArgumentParser, RawDescriptionHelpFormatter
sys.path.append(os.path.dirname(os.path.realpath(__file__))) sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import ansicodes as ansi import ansicodes as ansi
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>" __author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
__version__ = "0.3" __version__ = "0.3"
''' Rules modified from mistune project ''' """ Rules modified from mistune project """
def setup_options(): def setup_options():
bc = ansi.code() bc = ansi.code()
''' Create command line options ''' """ Create command line options """
usage = ''' usage = (
"""
Markdown syntax color in ansi codes. Markdown syntax color in ansi codes.
Special syntaxes: Special syntaxes:
- Colors: insert string e.g. ${C}. - Colors: insert string e.g. ${C}.
- Any ANSI control code: ${3A}, ${1;34;42m}, see the table.. - Any ANSI control code: ${3A}, ${1;34;42m}, see the table..
''' + ansi.demo() """
+ ansi.demo()
parser = ArgumentParser(
formatter_class = RawDescriptionHelpFormatter,
description = usage,
epilog = __author__
) )
parser.add_argument("-v","--version",action="version",version=__version__) parser = ArgumentParser(
parser.add_argument("-D",action="store_true",dest="debug",default=False, formatter_class=RawDescriptionHelpFormatter,
help="Debug mode") description=usage,
parser.add_argument("--dc",action="store_true",dest="dark_colors",default=False, epilog=__author__,
help="Use dark colorscheme, better for white background terminals.") )
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
help="Disable color.") parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument("--print-template",action="store_true",dest="print_template",default=False, parser.add_argument(
help="Print customizable color template.") "-D", action="store_true", dest="debug", default=False, help="Debug mode"
parser.add_argument("--template",action="store",type=str,dest="template",default=None, )
help="Use a custom template file for colorization.") parser.add_argument(
parser.add_argument("-z",action="store_true",dest="zero",default=False, "--dc",
help="Reset coloring at the end of each line.") action="store_true",
parser.add_argument("-Z",action="store_false",dest="zero_final",default=True, dest="dark_colors",
help="Disable reset of colors at the end of file.") default=False,
parser.add_argument("filename",type=str, nargs='?', help="Use dark colorscheme, better for white background terminals.",
help="File to show, - for stdin") )
opts=parser.parse_args() parser.add_argument(
"--no-color",
"-n",
action="store_false",
dest="color",
default=True,
help="Disable color.",
)
parser.add_argument(
"--print-template",
action="store_true",
dest="print_template",
default=False,
help="Print customizable color template.",
)
parser.add_argument(
"--template",
action="store",
type=str,
dest="template",
default=None,
help="Use a custom template file for colorization.",
)
parser.add_argument(
"-z",
action="store_true",
dest="zero",
default=False,
help="Reset coloring at the end of each line.",
)
parser.add_argument(
"-Z",
action="store_false",
dest="zero_final",
default=True,
help="Disable reset of colors at the end of file.",
)
parser.add_argument(
"filename", type=str, nargs="?", help="File to show, - for stdin"
)
opts = parser.parse_args()
return opts return opts
def parse(data): def parse(data):
data=[[None,row] for row in data] data = [[None, row] for row in data]
block='text' block = "text"
new_block='text' new_block = "text"
multiline_block=False multiline_block = False
# Parse styles # Parse styles
for i,line in enumerate(data): for i, line in enumerate(data):
row=line[1] row = line[1]
if line[0] is not None: if line[0] is not None:
# Previous lines have set the style already # Previous lines have set the style already
continue continue
for match in blocks: for match in blocks:
if block_match[match]['re'].match(row): if block_match[match]["re"].match(row):
new_block=match new_block = match
if match.startswith('multiline'): if match.startswith("multiline"):
if multiline_block: if multiline_block:
multiline_block=False multiline_block = False
else: else:
multiline_block=match multiline_block = match
break break
if multiline_block: if multiline_block:
new_block=multiline_block new_block = multiline_block
# Lists must end with empty line # Lists must end with empty line
if new_block not in ('empty','list_bullet') and block.startswith('list_'): if new_block not in ("empty", "list_bullet") and block.startswith("list_"):
new_block='list_loose' new_block = "list_loose"
if 'mod' in block_match[match]: if "mod" in block_match[match]:
# Style sets block in previous or next lines # Style sets block in previous or next lines
data[i+block_match[match]['mod']['pos']][0]=block_match[match]['mod']['name'] data[i + block_match[match]["mod"]["pos"]][0] = block_match[match]["mod"][
data[i][0]=new_block "name"
if new_block!=block: ]
block=new_block data[i][0] = new_block
if new_block != block:
block = new_block
return data return data
def colorize(data, remove_colors = False, dark_colors = False, debug = False): def colorize(data, remove_colors=False, dark_colors=False, debug=False):
# Start inserting colors, and printing # Start inserting colors, and printing
bc = ansi.code() bc = ansi.code()
colorized = [] colorized = []
cs = 'dc' if dark_colors else 'bc' cs = "dc" if dark_colors else "bc"
csc = cs + 'c' csc = cs + "c"
for i, line in enumerate(data): for i, line in enumerate(data):
row = line[1] row = line[1]
block = line[0] block = line[0]
multiline_block = block.startswith('multiline') multiline_block = block.startswith("multiline")
if multiline_block: if multiline_block:
row = block_match[block][csc] + row row = block_match[block][csc] + row
if block_match[block][cs]: if block_match[block][cs]:
row = block_match[block]['re'].sub(block_match[block][cs], row) row = block_match[block]["re"].sub(block_match[block][cs], row)
# No coloring inside block_code, nor multiline # No coloring inside block_code, nor multiline
if not (multiline_block or block == 'block_code'): if not (multiline_block or block == "block_code"):
for match in inlines: for match in inlines:
if inline_match[match]['re'].search(row): if inline_match[match]["re"].search(row):
row = inline_match[match]['re'].sub( row = inline_match[match]["re"].sub(
inline_match[match][cs] + block_match[block][csc], inline_match[match][cs] + block_match[block][csc], row
row
) )
if remove_colors: if remove_colors:
colored = bc.nocolor_string(row) colored = bc.nocolor_string(row)
@@ -114,31 +155,34 @@ def colorize(data, remove_colors = False, dark_colors = False, debug = False):
colorized.append(colored) colorized.append(colored)
return colorized return colorized
def md_re_compile(d): def md_re_compile(d):
''' Returns a re.compiled dict ''' """ Returns a re.compiled dict """
n={} n = {}
for t in d: for t in d:
n[t]={} n[t] = {}
for i in d[t]: for i in d[t]:
n[t][i]=d[t][i] n[t][i] = d[t][i]
try: try:
if n[t]['re']: if n[t]["re"]:
n[t]['re']=re.compile(n[t]['re']) n[t]["re"] = re.compile(n[t]["re"])
except err: except err:
print("Error compiling: %s"%(n[t]['re'])) print("Error compiling: %s" % (n[t]["re"]))
sys.exit(1) sys.exit(1)
return n return n
def read_data2(fp): def read_data2(fp):
data = [] data = []
# Read data # Read data
for row in f: for row in f:
if not row: if not row:
continue continue
row = row.decode('utf-8').rstrip("\n\r ") row = row.decode("utf-8").rstrip("\n\r ")
data.append(row) data.append(row)
return data return data
def read_data3(fp): def read_data3(fp):
data = [] data = []
# Read data # Read data
@@ -149,14 +193,16 @@ def read_data3(fp):
data.append(row) data.append(row)
return data return data
def write_colored2(colored, opts): def write_colored2(colored, opts):
for c in colored: for c in colored:
sys.stdout.write(c.encode('utf-8')) sys.stdout.write(c.encode("utf-8"))
if opts.zero: if opts.zero:
sys.stdout.write(bc.Z) sys.stdout.write(bc.Z)
sys.stdout.write("\n") sys.stdout.write("\n")
if opts.zero_final: if opts.zero_final:
sys.stdout.write(bc.Z.encode('utf-8')) sys.stdout.write(bc.Z.encode("utf-8"))
def write_colored3(colored, opts): def write_colored3(colored, opts):
for c in colored: for c in colored:
@@ -168,112 +214,207 @@ def write_colored3(colored, opts):
sys.stdout.write(bc.Z) sys.stdout.write(bc.Z)
# re: regular expression, bc: bright colors, bcc: continue with this color after inline # re: regular expression, bc: bright colors, bcc: continue with this color after inline
# dc: dark colors, dcc: continued color after inline # dc: dark colors, dcc: continued color after inline
block_match_str={ block_match_str = {
'block_code': {'re':'^( {4}[^*])(.*)$', "block_code": {
'bc' :'${Z}${c}\\1\\2', 'bcc':'${Z}${c}', "re": "^( {4}[^*])(.*)$",
'dc' :'${Z}${m}\\1\\2', 'dcc':'${Z}${m}'}, # code "bc": "${Z}${c}\\1\\2",
'multiline_code' : {'re':'^ *(`{3,}|~{3,}) *(\S*)', "bcc": "${Z}${c}",
'bc' :'${Z}${c}\\1\\2', 'bcc':'${Z}${c}', "dc": "${Z}${m}\\1\\2",
'dc' :'${Z}${m}\\1\\2', 'dcc':'${Z}${m}'}, # ```lang "dcc": "${Z}${m}",
'block_quote': {'re':'^(>[ >]* )', }, # code
'bc':'${K}\\1${Z}','bcc':'${Z}', "multiline_code": {
'dc':'${Y}\\1${Z}','dcc':'${Z}'}, # > > quote "re": "^ *(`{3,}|~{3,}) *(\S*)",
'hrule': {'re':'^ {0,3}[-*_]([-*_]){2,}$', "bc": "${Z}${c}\\1\\2",
'bc':'False','bcc':'${Z}', "bcc": "${Z}${c}",
'dc':'False','dcc':'${Z}'}, # ---- "dc": "${Z}${m}\\1\\2",
'heading' : {'re':'^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)', "dcc": "${Z}${m}",
'bc':'${W}\\1 ${U}\\2${Z}','bcc':'${W}${U}', }, # ```lang
'dc':'${B}\\1 ${U}\\2${Z}','dcc':'${B}${U}'}, # # heading "block_quote": {
'lheading' : {'re':'^(=+|-+)$', "re": "^(>[ >]* )",
'bc':'${W}\\1', 'bcc':'${W}', "bc": "${K}\\1${Z}",
'dc':'${B}\\1', 'dcc':'${B}', "bcc": "${Z}",
'mod':{'pos':-1,'name':'lheading.mod'}}, # ======= under header "dc": "${Y}\\1${Z}",
'lheading.mod' : {'re':'^([^\n]+)', "dcc": "${Z}",
'bc':'${W}\\1', 'bcc':'${W}', }, # > > quote
'dc':'${B}\\1', 'dcc':'${B}'}, # over the ======= under header "hrule": {
'list_bullet': {'re':'^( *)([*+-]|[\d\.]+)( +)', "re": "^ {0,3}[-*_]([-*_]){2,}$",
'bc':'\\1${y}\\2${Z}\\3','bcc':'${Z}', "bc": "False",
'dc':'\\1${r}\\2${Z}\\3','dcc':'${Z}'}, # * or 1. "bcc": "${Z}",
'list_loose': {'re':'None', "dc": "False",
'bc':'False','bcc':'${Z}', "dcc": "${Z}",
'dc':'False','dcc':'${Z}'}, }, # ----
'text': {'re':'^([^\n]+)', "heading": {
'bc':'${Z}\\1','bcc':'${Z}', "re": "^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)",
'dc':'${Z}\\1','dcc':'${Z}'}, "bc": "${W}\\1 ${U}\\2${Z}",
'empty': {'re':'(^$)', "bcc": "${W}${U}",
'bc':'${Z}\\1','bcc':'${Z}', "dc": "${B}\\1 ${U}\\2${Z}",
'dc':'${Z}\\1','dcc':'${Z}'}, "dcc": "${B}${U}",
'preformatted': {'re': 'a^', # Never matches anything }, # # heading
'bc':'','bcc':'', "lheading": {
'dc':'','dcc':''}, "re": "^(=+|-+)$",
"bc": "${W}\\1",
"bcc": "${W}",
"dc": "${B}\\1",
"dcc": "${B}",
"mod": {"pos": -1, "name": "lheading.mod"},
}, # ======= under header
"lheading.mod": {
"re": "^([^\n]+)",
"bc": "${W}\\1",
"bcc": "${W}",
"dc": "${B}\\1",
"dcc": "${B}",
}, # over the ======= under header
"list_bullet": {
"re": "^( *)([*+-]|[\d\.]+)( +)",
"bc": "\\1${y}\\2${Z}\\3",
"bcc": "${Z}",
"dc": "\\1${r}\\2${Z}\\3",
"dcc": "${Z}",
}, # * or 1.
"list_loose": {
"re": "None",
"bc": "False",
"bcc": "${Z}",
"dc": "False",
"dcc": "${Z}",
},
"text": {
"re": "^([^\n]+)",
"bc": "${Z}\\1",
"bcc": "${Z}",
"dc": "${Z}\\1",
"dcc": "${Z}",
},
"empty": {
"re": "(^$)",
"bc": "${Z}\\1",
"bcc": "${Z}",
"dc": "${Z}\\1",
"dcc": "${Z}",
},
"preformatted": {
"re": "a^", # Never matches anything
"bc": "",
"bcc": "",
"dc": "",
"dcc": "",
},
} }
block_match=md_re_compile(block_match_str) block_match = md_re_compile(block_match_str)
blocks=['block_quote', 'multiline_code','hrule', 'heading','lheading','list_bullet', 'block_code', 'text', 'empty'] blocks = [
"block_quote",
"multiline_code",
"hrule",
"heading",
"lheading",
"list_bullet",
"block_code",
"text",
"empty",
]
inline_match_str={ inline_match_str = {
'bold1': {'re':r'(^| |})(_[^_]+_)', "bold1": {
'bc':'\\1${W}\\2','dc':'\\1${W}\\2'}, # _bold_ "re": r"(^| |})(_[^_]+_)",
'bold2': {'re':r'(^| |})(\*{1,2}[^\*]+\*{1,2})', "bc": "\\1${W}\\2",
'bc':'\\1${W}\\2','dc':'\\1${W}\\2'}, # **bold** "dc": "\\1${W}\\2",
'code': {'re':r'([`]+[^`]+[`]+)', }, # _bold_
'bc':'${c}\\1','dc':'${m}\\1'}, # `code` "bold2": {
'code_special': {'re':r'([`]+[^`]+[`]+)([!>])', "re": r"(^| |})(\*{1,2}[^\*]+\*{1,2})",
'bc':'${c}\\1${g}\\2','dc':'${m}\\1${r}\\2'}, # `code`! or `code`> for markslider "bc": "\\1${W}\\2",
'link': {'re':r'(\[)([^\]]+)(\])\(([^\)]+)\)', "dc": "\\1${W}\\2",
'bc':'${B}\\1${Z}\\2${B}\\3(${U}\\4${u})', }, # **bold**
'dc':'${b}\\1${Z}\\2${b}\\3(${U}\\4${u})'}, # [text](link) "code": {"re": r"([`]+[^`]+[`]+)", "bc": "${c}\\1", "dc": "${m}\\1"}, # `code`
'image': {'re':r'(!\[[^\]]+\]\([^\)]+\))', "code_special": {
'bc':'${r}\\1','dc':'${g}\\1'}, # ![text](image) "re": r"([`]+[^`]+[`]+)([!>])",
'underline': {'re':r'(^|\W)(__)([^_]+)(__)', "bc": "${c}\\1${g}\\2",
'bc':'\\1\\2${U}\\3${Z}\\4','dc':'\\1\\2${U}\\3${Z}\\4'}, # __underline__ "dc": "${m}\\1${r}\\2",
'strikethrough': {'re':r'(~~)([^~]+)(~~)', }, # `code`! or `code`> for markslider
'bc':'\\1${st}\\2${so}\\3','dc':'\\1${st}\\2${so}\\3'}, # ~~strike~~ "link": {
'checkbox': {'re':r'(\[[x ]\])', "re": r"(\[)([^\]]+)(\])\(([^\)]+)\)",
'bc':'${y}\\1','dc':'${r}\\1'}, # [x] [ ] "bc": "${B}\\1${Z}\\2${B}\\3(${U}\\4${u})",
"dc": "${b}\\1${Z}\\2${b}\\3(${U}\\4${u})",
}, # [text](link)
"image": {
"re": r"(!\[[^\]]+\]\([^\)]+\))",
"bc": "${r}\\1",
"dc": "${g}\\1",
}, # ![text](image)
"underline": {
"re": r"(^|\W)(__)([^_]+)(__)",
"bc": "\\1\\2${U}\\3${Z}\\4",
"dc": "\\1\\2${U}\\3${Z}\\4",
}, # __underline__
"strikethrough": {
"re": r"(~~)([^~]+)(~~)",
"bc": "\\1${st}\\2${so}\\3",
"dc": "\\1${st}\\2${so}\\3",
}, # ~~strike~~
"checkbox": {"re": r"(\[[x ]\])", "bc": "${y}\\1", "dc": "${r}\\1"}, # [x] [ ]
} }
inline_match = md_re_compile(inline_match_str) inline_match = md_re_compile(inline_match_str)
inlines = ['bold1','bold2','code_special','code','image','link','underline','strikethrough', 'checkbox'] inlines = [
"bold1",
"bold2",
"code_special",
"code",
"image",
"link",
"underline",
"strikethrough",
"checkbox",
]
if __name__ == "__main__": if __name__ == "__main__":
opts=setup_options() opts = setup_options()
if opts.print_template: if opts.print_template:
import json import json
print(json.dumps({'about':'re: regular expression, bc: bright colors, bcc: continue with this color after inline, dc: dark colors, dcc: continued color after inline. "blocks" and "inlines" list keys of matchers. ',
'blocks':blocks, print(
'block_match':block_match_str, json.dumps(
'inline_match':inline_match_str, {
'inlines':inlines}, "about": 're: regular expression, bc: bright colors, bcc: continue with this color after inline, dc: dark colors, dcc: continued color after inline. "blocks" and "inlines" list keys of matchers. ',
indent=2,sort_keys=True)) "blocks": blocks,
"block_match": block_match_str,
"inline_match": inline_match_str,
"inlines": inlines,
},
indent=2,
sort_keys=True,
)
)
sys.exit(0) sys.exit(0)
if opts.template: if opts.template:
import json import json
template=json.load(open(opts.template,'r'))
if 'inlines' in template: inlines=template['inlines'] template = json.load(open(opts.template, "r"))
if 'blocks' in template: blocks=template['blocks'] if "inlines" in template:
if 'block_match' in template: inlines = template["inlines"]
block_match_str=template['block_match'] if "blocks" in template:
block_match=md_re_compile(block_match_str) blocks = template["blocks"]
if 'inline_match' in template: if "block_match" in template:
inline_match_str=template['inline_match'] block_match_str = template["block_match"]
inline_match=md_re_compile(inline_match_str) block_match = md_re_compile(block_match_str)
if "inline_match" in template:
inline_match_str = template["inline_match"]
inline_match = md_re_compile(inline_match_str)
bc = ansi.code() bc = ansi.code()
if opts.filename == "-" or opts.filename == None: if opts.filename == "-" or opts.filename == None:
f = sys.stdin f = sys.stdin
else: else:
f = open(opts.filename, 'r') f = open(opts.filename, "r")
if (sys.version_info > (3, 0)): if sys.version_info > (3, 0):
data = read_data3(f) data = read_data3(f)
else: else:
data = read_data2(f) data = read_data2(f)
data = parse(data) data = parse(data)
colored = colorize(data, not opts.color, opts.dark_colors, opts.debug) colored = colorize(data, not opts.color, opts.dark_colors, opts.debug)
if (sys.version_info > (3, 0)): if sys.version_info > (3, 0):
write_colored3(colored, opts) write_colored3(colored, opts)
else: else:
write_colored2(colored, opts) write_colored2(colored, opts)

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# coding=utf-8 # coding=utf-8
# #
# Copyright 2016 Ville Rantanen # Copyright 2016 Ville Rantanen
@@ -17,61 +17,83 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
'''Color by ${X} notation.''' """Color by ${X} notation."""
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>" __author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
__version__ = "0.1" __version__ = "0.1"
import sys,os,argparse,re import sys, os, argparse, re
from argparse import ArgumentParser from argparse import ArgumentParser
sys.path.append(os.path.dirname(os.path.realpath(__file__))) sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import ansicodes as ansi import ansicodes as ansi
def setup_options(): def setup_options():
''' Create command line options ''' """ Create command line options """
usage=''' usage = (
"""
Color notation renderer in ANSI codes Color notation renderer in ANSI codes
Special syntaxes: Special syntaxes:
* Colors: insert string ${X}, where X values in the table below. * Colors: insert string ${X}, where X values in the table below.
''' + ansi.demo() """
+ ansi.demo()
)
parser=ArgumentParser(description=usage, parser = ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter, description=usage,
epilog=__author__) formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__author__,
)
parser.add_argument("-v","--version",action="version",version=__version__) parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True, parser.add_argument(
help="Disable color.") "--no-color",
parser.add_argument("-z",action="store_true",dest="zero",default=False, "-n",
help="Reset coloring at the end of each line.") action="store_false",
parser.add_argument("-Z",action="store_false",dest="zero_final",default=True, dest="color",
help="Disable reset of colors at the end of file.") default=True,
parser.add_argument("filename",type=str, help="Disable color.",
help="File to show, - for stdin") )
opts=parser.parse_args() parser.add_argument(
"-z",
action="store_true",
dest="zero",
default=False,
help="Reset coloring at the end of each line.",
)
parser.add_argument(
"-Z",
action="store_false",
dest="zero_final",
default=True,
help="Disable reset of colors at the end of file.",
)
parser.add_argument("filename", type=str, help="File to show, - for stdin")
opts = parser.parse_args()
return opts return opts
bc=ansi.code()
opts=setup_options() bc = ansi.code()
if opts.filename=="-": opts = setup_options()
f=sys.stdin if opts.filename == "-":
f = sys.stdin
else: else:
f=open(opts.filename,'r') f = open(opts.filename, "r")
for row in f: for row in f:
if not row: if not row:
continue continue
if type(row) == bytes: if type(row) == bytes:
row = row.decode('utf-8') row = row.decode("utf-8")
row=row.rstrip("\n\r ") row = row.rstrip("\n\r ")
if opts.color: if opts.color:
colored=bc.color_string(row) colored = bc.color_string(row)
else: else:
colored=bc.nocolor_string(row) colored = bc.nocolor_string(row)
sys.stdout.write(colored) sys.stdout.write(colored)
if opts.zero and opts.color: if opts.zero and opts.color:
sys.stdout.write(bc.Z) sys.stdout.write(bc.Z)
sys.stdout.write("\n") sys.stdout.write("\n")
if opts.zero_final and opts.color: if opts.zero_final and opts.color:
sys.stdout.write(bc.Z) sys.stdout.write(bc.Z)

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python3
# coding=utf-8 # coding=utf-8
# #
# Copyright 2016 Ville Rantanen # Copyright 2016 Ville Rantanen
@@ -17,110 +17,158 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
'''Color by notation.''' """Color by notation."""
__author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>" __author__ = "Ville Rantanen <ville.q.rantanen@gmail.com>"
__version__ = "0.2" __version__ = "0.2"
NEWCHAR="====CHAR" NEWCHAR = "====CHAR"
NEWCOLOR="====COLOR" NEWCOLOR = "====COLOR"
NEWIMAGE="====EOI" NEWIMAGE = "====EOI"
ENDOFFILE="====EOF" ENDOFFILE = "====EOF"
PAUSE="====PAUSE" PAUSE = "====PAUSE"
import sys,os,argparse,re,time import sys, os, argparse, re, time
from argparse import ArgumentParser from argparse import ArgumentParser
class bc: class bc:
K="\033[1;30m" K = "\033[1;30m"
R="\033[1;31m" R = "\033[1;31m"
G="\033[1;32m" G = "\033[1;32m"
Y="\033[1;33m" Y = "\033[1;33m"
B="\033[1;34m" B = "\033[1;34m"
M="\033[1;35m" M = "\033[1;35m"
C="\033[1;36m" C = "\033[1;36m"
W="\033[1;37m" W = "\033[1;37m"
k="\033[0;30m" k = "\033[0;30m"
r="\033[0;31m" r = "\033[0;31m"
g="\033[0;32m" g = "\033[0;32m"
y="\033[0;33m" y = "\033[0;33m"
b="\033[0;34m" b = "\033[0;34m"
m="\033[0;35m" m = "\033[0;35m"
c="\033[0;36m" c = "\033[0;36m"
w="\033[0;37m" w = "\033[0;37m"
nk="\033[30m" nk = "\033[30m"
nr="\033[31m" nr = "\033[31m"
ng="\033[32m" ng = "\033[32m"
ny="\033[33m" ny = "\033[33m"
nb="\033[34m" nb = "\033[34m"
nm="\033[35m" nm = "\033[35m"
nc="\033[36m" nc = "\033[36m"
nw="\033[37m" nw = "\033[37m"
bk="\033[40m" bk = "\033[40m"
br="\033[41m" br = "\033[41m"
bg="\033[42m" bg = "\033[42m"
by="\033[43m" by = "\033[43m"
bb="\033[44m" bb = "\033[44m"
bm="\033[45m" bm = "\033[45m"
bc="\033[46m" bc = "\033[46m"
bw="\033[47m" bw = "\033[47m"
S = '\033[1m' S = "\033[1m"
s = '\033[2m'#strong off s = "\033[2m" # strong off
U = '\033[4m'#underline U = "\033[4m" # underline
u = '\033[24m'#underline off u = "\033[24m" # underline off
ic = '\033[7m'#inverse colors ic = "\033[7m" # inverse colors
io = '\033[27m'#inverse off io = "\033[27m" # inverse off
st = '\033[9m'#strike on st = "\033[9m" # strike on
so = '\033[29m'#strike off so = "\033[29m" # strike off
Z = '\033[0m' Z = "\033[0m"
CLR = '\033[2J' CLR = "\033[2J"
CLREND = '\033[K' CLREND = "\033[K"
CLRSCR = CLR+"\033[0;0H" CLRSCR = CLR + "\033[0;0H"
color_keys="pqwertyu01234567;asdfghjPQWERTYUxXczvVbBnN" color_keys = "pqwertyu01234567;asdfghjPQWERTYUxXczvVbBnN"
color_list=[K, R, G, Y, B, M, C, W, k, r, g, y, b, m, c, w, color_list = [
bk,br,bg,by,bb,bm,bc,bw,nk,nr,ng,ny,nb,nm,nc,nw, K,
S,s,CLRSCR,Z,U,u,st,so,ic,io] R,
G,
Y,
B,
M,
C,
W,
k,
r,
g,
y,
b,
m,
c,
w,
bk,
br,
bg,
by,
bb,
bm,
bc,
bw,
nk,
nr,
ng,
ny,
nb,
nm,
nc,
nw,
S,
s,
CLRSCR,
Z,
U,
u,
st,
so,
ic,
io,
]
def pos(self,y,x): def pos(self, y, x):
return "\033["+str(y)+";"+str(x)+"H" return "\033[" + str(y) + ";" + str(x) + "H"
def posprint(self, y,x,s): def posprint(self, y, x, s):
sys.stdout.write( (self.pos(y,x) + str(s)).encode('utf-8') ) sys.stdout.write((self.pos(y, x) + str(s)).encode("utf-8"))
def clear(self): def clear(self):
sys.stdout.write( (self.CLR+self.pos(0,0)).encode('utf-8') ) sys.stdout.write((self.CLR + self.pos(0, 0)).encode("utf-8"))
def clear_to_end(self):
sys.stdout.write( self.CLREND.encode('utf-8') )
def color_char(self,s): def clear_to_end(self):
for i,c in enumerate(self.color_keys): sys.stdout.write(self.CLREND.encode("utf-8"))
def color_char(self, s):
for i, c in enumerate(self.color_keys):
if c in s: if c in s:
s=s.replace(c,self.color_list[i]) s = s.replace(c, self.color_list[i])
return s return s
return "" return ""
def nocolor_string(self,s):
def nocolor_string(self, s):
return "" return ""
def end_drawing(opts): def end_drawing(opts):
if opts.zero_final: if opts.zero_final:
sys.stdout.write(bc.Z.encode('utf-8')) sys.stdout.write(bc.Z.encode("utf-8"))
sys.exit(0) sys.exit(0)
def open_data_file(opts): def open_data_file(opts):
if opts.test_image: if opts.test_image:
return test_data() return test_data()
if opts.filename=='-': if opts.filename == "-":
return sys.stdin return sys.stdin
return open(opts.filename,'r') return open(opts.filename, "r")
def setup_options(): def setup_options():
''' Create command line options ''' """ Create command line options """
usage=''' usage = """
Color image renderer in ANSI codes. Example file: Color image renderer in ANSI codes. Example file:
first layer of characters first layer of characters
@@ -145,33 +193,63 @@ Color image renderer in ANSI codes. Example file:
====EOF ====EOF
end of file end of file
''' """
parser=ArgumentParser(description=usage, parser = ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter, description=usage,
epilog=__author__) formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__author__,
)
parser.add_argument("-v","--version",action="version",version=__version__) parser.add_argument("-v", "--version", action="version", version=__version__)
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True, parser.add_argument(
help="Disable color.") "--no-color",
parser.add_argument("--test",action="store_true",dest="test_image",default=False, "-n",
help="Show test image showing features.") action="store_false",
parser.add_argument("-z",action="store_true",dest="zero",default=False, dest="color",
help="Reset color codes at the end of each line.") default=True,
parser.add_argument("-Z",action="store_false",dest="zero_final",default=True, help="Disable color.",
help="Disable reset of colors at the end of file.") )
parser.add_argument("-c",action="store_true",dest="clear",default=False, parser.add_argument(
help="Clear screen first") "--test",
parser.add_argument("filename",type=str, nargs='?', default=None, action="store_true",
help="File to show, - for stdin") dest="test_image",
opts=parser.parse_args() default=False,
if not opts.test_image and opts.filename==None: help="Show test image showing features.",
)
parser.add_argument(
"-z",
action="store_true",
dest="zero",
default=False,
help="Reset color codes at the end of each line.",
)
parser.add_argument(
"-Z",
action="store_false",
dest="zero_final",
default=True,
help="Disable reset of colors at the end of file.",
)
parser.add_argument(
"-c",
action="store_true",
dest="clear",
default=False,
help="Clear screen first",
)
parser.add_argument(
"filename", type=str, nargs="?", default=None, help="File to show, - for stdin"
)
opts = parser.parse_args()
if not opts.test_image and opts.filename == None:
parser.error("Need a file or - as argument") parser.error("Need a file or - as argument")
return opts return opts
def test_data(): def test_data():
testimage=''' NOT VISIBLE testimage = ''' NOT VISIBLE
_ _ _ ____ ___ _ _ _ ____ ___
1 red 5 magenta / \ | \ | / ___|_ _| 1 red 5 magenta / \ | \ | / ___|_ _|
2 green 6 cyan / _ \ | \| \___ \| | 2 green 6 cyan / _ \ | \| \___ \| |
@@ -221,98 +299,100 @@ u f u
; f U ; f U
; f U ; f U
====EOI ====EOI
Outputs:'''.encode('utf-8') Outputs:'''.encode(
return testimage.split('\n') "utf-8"
)
return testimage.split("\n")
bc=bc()
opts=setup_options() bc = bc()
f=open_data_file(opts) opts = setup_options()
endoffile=False f = open_data_file(opts)
endoffile = False
if opts.clear: if opts.clear:
bc.clear() bc.clear()
if opts.test_image: if opts.test_image:
for row in f: for row in f:
print(row) print(row)
while True: while True:
pause=0 pause = 0
gray=[] gray = []
gray.append([]) gray.append([])
colors=[] colors = []
colorFrame=0 colorFrame = 0
grayFrame=0 grayFrame = 0
maxRow=0 maxRow = 0
for row in f: for row in f:
if not row: if not row:
end_drawing(opts) end_drawing(opts)
row=row.decode('utf-8').rstrip("\n\r") row = row.decode("utf-8").rstrip("\n\r")
if row.startswith(NEWIMAGE): if row.startswith(NEWIMAGE):
break break
if row.startswith(ENDOFFILE): if row.startswith(ENDOFFILE):
endoffile=True endoffile = True
if endoffile: if endoffile:
break break
if row.startswith(PAUSE): if row.startswith(PAUSE):
try: try:
new_value=row[len(PAUSE):].strip() new_value = row[len(PAUSE) :].strip()
pause=float(new_value) pause = float(new_value)
except: except:
pause=0 pause = 0
continue continue
if row.startswith(NEWCOLOR): if row.startswith(NEWCOLOR):
colorFrame+=1 colorFrame += 1
colors.append([]) colors.append([])
continue continue
if colorFrame==0: if colorFrame == 0:
if row.startswith(NEWCHAR): if row.startswith(NEWCHAR):
grayFrame+=1 grayFrame += 1
gray.append([]) gray.append([])
continue continue
gray[grayFrame].append(row) gray[grayFrame].append(row)
maxRow=max(maxRow, len(row)) maxRow = max(maxRow, len(row))
else: else:
colors[colorFrame-1].append(row) colors[colorFrame - 1].append(row)
if len(gray[0])==0: if len(gray[0]) == 0:
end_drawing(opts) end_drawing(opts)
for i in range(len(gray[0])): for i in range(len(gray[0])):
while maxRow>len(gray[0][i]): while maxRow > len(gray[0][i]):
gray[0][i]=gray[0][i]+" " gray[0][i] = gray[0][i] + " "
for i in range(len(gray[0])): for i in range(len(gray[0])):
for frame in colors: for frame in colors:
if len(frame)<i+1: if len(frame) < i + 1:
frame.append('') frame.append("")
while len(gray[0][i])>len(frame[i]): while len(gray[0][i]) > len(frame[i]):
frame[i]=frame[i]+" " frame[i] = frame[i] + " "
if len(gray)>1: if len(gray) > 1:
for layer in gray[1:]: for layer in gray[1:]:
if len(layer)<i+1: if len(layer) < i + 1:
layer.append('') layer.append("")
while len(gray[0][i])>len(layer[i]): while len(gray[0][i]) > len(layer[i]):
layer[i]=layer[i]+" " layer[i] = layer[i] + " "
for i in range(len(gray[0])): for i in range(len(gray[0])):
for c in range(len(gray[0][i])): for c in range(len(gray[0][i])):
if opts.color: if opts.color:
for frame in colors: for frame in colors:
try: try:
if frame[i][c]!=" ": if frame[i][c] != " ":
sys.stdout.write(bc.color_char(frame[i][c]).encode('utf-8')) sys.stdout.write(bc.color_char(frame[i][c]).encode("utf-8"))
except IndexError: except IndexError:
pass pass
char=" " char = " "
for layer in gray: for layer in gray:
if layer[i][c]==" ": continue if layer[i][c] == " ":
char=layer[i][c] continue
char = layer[i][c]
sys.stdout.write(char.encode('utf-8')) sys.stdout.write(char.encode("utf-8"))
if opts.color and opts.zero: if opts.color and opts.zero:
sys.stdout.write(bc.Z.encode('utf-8')) sys.stdout.write(bc.Z.encode("utf-8"))
sys.stdout.write("\n") sys.stdout.write("\n")
if pause>0: if pause > 0:
time.sleep(pause) time.sleep(pause)
if opts.test_image: if opts.test_image:
end_drawing(opts) end_drawing(opts)