This commit is contained in:
q
2016-06-16 17:43:32 +03:00

View File

@@ -68,15 +68,23 @@ class bc:
bw="\033[47m"
S = '\033[1m'
U = '\033[4m'
s = '\033[2m'#strong off
U = '\033[4m'#underline
u = '\033[24m'#underline off
ic = '\033[7m'#inverse colors
io = '\033[27m'#inverse off
st = '\033[9m'#strike on
so = '\033[29m'#strike off
Z = '\033[0m'
CLR = '\033[2J'
CLREND = '\033[K'
CLRSCR = CLR+"\033[0;0H"
color_keys="pqwertyu01234567xczv;asdfghj)!@#$%^&"
color_keys="pqwertyu01234567;asdfghjPQWERTYUxXczvVbBnN"
color_list=[K, R, G, Y, B, M, C, W, k, r, g, y, b, m, c, w,
S,CLRSCR,Z,U,bk,br,bg,by,bb,bm,bc,bw,nk,nr,ng,ny,nb,nm,nc,nw ]
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):
return "\033["+str(y)+";"+str(x)+"H"
@@ -98,25 +106,44 @@ class bc:
def nocolor_string(self,s):
return ""
def end_drawing(opts):
if opts.zero_final:
sys.stdout.write(bc.Z.encode('utf-8'))
sys.exit(0)
def open_data_file(opts):
if opts.test_image:
return test_data()
if opts.filename=='-':
return sys.stdin
return open(opts.filename,'r')
def setup_options():
''' Create command line options '''
usage='''
Color image renderer in ANSI codes. Example file:
first layer of characters
====CHAR
more characters
go here
====COLOR
color codes go here (letters 1-7,0 and keys below like a piano)
zxcv: zero, bold, clearscreen, underline
12345670 : red green yellow blue magenta cyan white black (darker tone)
qwertyup : red green yellow blue magenta cyan white black (bright tone)
QWERTYUP : red green yellow blue magenta cyan white black (without changing brightness)
asdfghj; : backround colors
zc: reset colors, clearscreen
xX: bright color on, bright color off
vV: underline on, underline off
bB: strikethrough on, strikethrough off
nN: inverse on, inverse off
====EOI
end of image, start another
====PAUSE1.5
another image starts, pause for 1.5 seconds before drawing the next
====EOF
end of image file
end of file
'''
@@ -127,24 +154,84 @@ Color image renderer in ANSI codes. Example file:
parser.add_argument("-v","--version",action="version",version=__version__)
parser.add_argument("--no-color","-n",action="store_false",dest="color",default=True,
help="Disable color.")
parser.add_argument("--test",action="store_true",dest="test_image",default=False,
help="Show test image showing features.")
parser.add_argument("-z",action="store_true",dest="zero",default=False,
help="Reset coloring at the end of each line.")
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,
help="File to show")
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")
return opts
def test_data():
testimage=''' NOT VISIBLE
_ _ _ ____ ___
1 red 5 magenta / \ | \ | / ___|_ _|
2 green 6 cyan / _ \ | \| \___ \| |
3 yellow 7 white / ___ \| |\ |___) | |
4 blue 0 black /_/ \_\_| \_|____/___|
,ad8888ba, 88 88888888ba
d8"' `"8b 88 88 "8b
d8' 88 88 ,8P
88 ,adPPYba, 88 ,adPPYba, 88aaaaaa8P' ,adPPYba,
88 a8" "8a 88 a8" "8a 88""""88' I8[ ""
Y8, 8b d8 88 8b d8 88 `8b `"Y8ba,
Y8a. .a8P "8a, ,a8" 88 "8a, ,a8" 88 `8b aa ]8I
`"Y8888Y"' `"YbbdP"' 8888888888 `"YbbdP"' 88 `8b `"YbbdP"'
====CHAR
****************************************************
====COLOR
1 2 3 4 5 6 7 0 q w e r t y u p Q W EXR TxY U P
1
1 5 1
2 6 1
3 7 1
R P Q
~
E f z
e f z
w f z
w f z
w f z
u f u
7 f ;
7 f ;
====COLOR
~
~
~
~
~
v V b B bB v V
~
~ f E
~ f e
~ f w
~ f w
~ f w
; f ;
; f U
; f U
====EOI
Outputs:'''.encode('utf-8')
return testimage.split('\n')
bc=bc()
opts=setup_options()
if opts.filename=='-':
f=sys.stdin
else:
f=open(opts.filename,'r')
f=open_data_file(opts)
if opts.clear:
bc.clear()
if opts.test_image:
for row in f:
print(row)
while True:
pause=0
gray=[]
@@ -155,7 +242,7 @@ while True:
maxRow=0
for row in f:
if not row:
sys.exit(0)
end_drawing(opts)
row=row.decode('utf-8').rstrip("\n\r")
if row.startswith(NEWIMAGE):
break
@@ -170,17 +257,19 @@ while True:
colorFrame+=1
colors.append([])
continue
if colorFrame==0:
if row.startswith(NEWCHAR):
grayFrame+=1
gray.append([])
continue
gray[grayFrame].append(row)
maxRow=max(maxRow, len(row))
else:
colors[colorFrame-1].append(row)
if len(gray[0])==0:
sys.exit(0)
end_drawing(opts)
for i in range(len(gray[0])):
while maxRow>len(gray[0][i]):
gray[0][i]=gray[0][i]+" "
@@ -219,4 +308,6 @@ while True:
sys.stdout.write("\n")
if pause>0:
time.sleep(pause)
if opts.test_image:
end_drawing(opts)