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. -hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMdyymMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +``` +``` +cp437 ansi parsing +``` +## prerequisites +01. `apt install python3` +## example of parsing, loads 'work.ans' +13. `python3 dr1p4ns1.py` +## example of test data +13. `python3 test.py` +## included files overview +``` +dr1p4ns1.py - main file, reads file.ans and attempts to parse and display +work.ans - this ansi file is loaded by dr1p4ns1.py, this is the logic testing +test.py - used to generate test ansi file and presents table data +``` +- _dr1p4ns1.py +- _work.ans +- _test.py +``` diff --git a/dr1p4ns1.py b/dr1p4ns1.py new file mode 100644 index 0000000..bf2c453 --- /dev/null +++ b/dr1p4ns1.py @@ -0,0 +1,226 @@ +import sys +####################################################################################################################### +class C: + ################################################################################################################### + commands = { + 'A': 'up', + 'B': 'down', + 'C': 'forward', + 'D': 'back', + 'H': 'position', + 'f': 'position', + 'J': 'erase', + 'K': 'erase_line', + 's': 'save_cursor', + 'u': 'restore_cursor', + 'm': 'color', + 'R': 'report_cursor_position' + } + ################################################################################################################### + ascii_table=""" + ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒ\ + áíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐\ + └┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀\ + αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■  + """ + ################################################################################################################### + for r in (('\n',''),(' ','')): + ascii_table=ascii_table.replace(*r) + ################################################################################################################### + asc2uni=dict(zip([i for i in range(128,256)],[ord(c) for c in ascii_table])) + ################################################################################################################### + asc2chr=dict(zip([i for i in range(128,256)],[c for c in ascii_table])) + ################################################################################################################### + def __init__(self,ansifile="",width=80,height=25): + self.width=width + self.height=height + self.openansi(ansifile) + self.code='\x1b[0m' + print("\x1bc\x1b[1;31m[ DR1P LOADED ]\x1b[0m\n") + ################################################################################################################### + def openansi(self,s): + try: + f=open(s,'rb') + self.ansifile=f.read().decode('cp437') + f.close() + except: + print(f'\n\x1b[1;31m[ ERROR: "{s}" FILE NOT FOUND ] - *USAGE: `python3 {sys.argv[0]} work.ans`\x1b[0m') + sys.exit(1) + ################################################################################################################### + def printascii(self): + for i,_ in enumerate(self.ascii_table): + _int=str(i+128).zfill(3) + _hex=hex(i+128)[2:].upper().zfill(2) + _ord=str(ord(_)).zfill(4) + print(f'{_} - {_int} - {_hex} - {_ord}') + ################################################################################################################### + def printrange(self,n,nn): + for index,_ in enumerate(c.ansifile): + h=hex(_)[2:].upper() + i=str(_).zfill(3) + s='' + if _>47 and _<127: + s=chr(_) + else: + s='.' + l=str(index).zfill(len(str(len(c.ansifile)))) + if index >=n and index <=nn: + print(f"{l} {i} {h} {s}") + if index==nn: break + ################################################################################################################### + def printhex(self): + ########################################### + theasc=[] + thehex=[] + buffer='' + ########################################### + for _ in self.ansifile: + buffer+=hex(ord(_)).upper()[2:].zfill(2) + ########################################### + for _ in self.chunk(buffer,32): + subchunks='' + for i,__ in enumerate(self.chunk(_,2)): + if i>0 and i%4==0: subchunks+=" " + subchunks+=f'{__} ' + thehex.append(subchunks.strip()) + ########################################### + for __ in thehex: + buffer='' + for _ in __.split(): + i=int(_,16) + if i>47 and i<127: + buffer+=chr(i) + else: + buffer+='.' + theasc.append(buffer) + ########################################### + for _ in range(len(thehex)): + old=buffer + buffer={thehex[_]} + if not buffer == old: + print(f"{hex(16 * _)[2:].zfill(8)} {thehex[_]} {theasc[_]}") + ################################################################################################################### + def hexdump(self,visible=True): + if visible == True: + self.printhex() + self.codes=[] + code_start=0 + code_end=0 + code_state=False + buffer='' + for i,_ in enumerate(self.ansifile): + if code_state==False: + if _ == '\x1b': + code_state=True + code_start=i + elif _ == '\x1a': + code=self.ansifile[i:] + self.codes.append([code,i,len(self.ansifile)]) + else: + if 64 <= ord(_) <= 126: + if _ in self.commands: + code_state=False + code_end=i+1 + code=self.ansifile[code_start:code_end] + self.codes.append([code,code_start,code_end]) + ################################################################################################################### + def findall(self,s,w): + return [i for i in range(len(s)) if s.startswith(w, i)] + ################################################################################################################### + def getsauce(self): + try: + self.ansifile[-128:] + self.ansifile_xwidth=ord(c.ansifile[-128:][96]) + self.ansifile_yheight=ord(c.ansifile[-128:][98]) + self.width=self.ansifile_xwidth + self.height=self.ansifile_yheight + print(f'\x1b[31m[ SAUCE INFO FOUND ] - X: {self.width} Y: {self.height}\x1b[0m\n') + except: + print('\x1b[31m[ NO SAUCE INFO FOUND ]\x1b[0m\n') + ################################################################################################################### + def chunk(self,s,n): + return [s[i:i+n] for i in range(0,len(s),n)] + ################################################################################################################### + def rgb(self,t='',r=0,g=0,b=0): + """colorize text with rgb values""" + return f"\033[38;2;{r};{g};{b}m{t}\033[38;2;255;255;255m" + ################################################################################################################### + def vga(self,t='',i=0): + """colorize text using vga color set""" + vga = ['#000000','#aa0000','#00aa00','#aa5500','#0000aa','#aa00aa','#00aaaa','#aaaaaa', + '#555555','#ff5555','#55ff55','#ffff55','#5555ff','#ff55ff','#55ffff','#ffffff',] + try: + r,g,b=int(vga[i][1:][0:2],16),int(vga[i][1:][2:4],16),int(vga[i][1:][4:6],16) + except: + r,g,b=0,0,0 + return f"\033[38;2;{r};{g};{b}m{t}\033[38;2;255;255;255m" +####################################################################################################################### +if __name__ == "__main__": + ################################################################ + try: + input_file=sys.argv[1] + except: + input_file='work.ans' + c=C(ansifile=input_file,width=80) + c.getsauce() + c.hexdump(visible=False) + ################################################################ + op=[] + fb=c.ansifile + xnew=0 + xold=0 + ############ + for _ in c.codes: + xnew=_[1] + distance=xnew-xold + if not distance == 0: + gap=fb[xold:xold+distance] + op.append(gap) + op.append(_[0]) + xold=_[2] + ################################################################ + optype=[] + ############ + for i,_op in enumerate(op): + if _op.startswith('\x1b'): + if _op.endswith('m'): + optype.append(2) + ######################################################## + elif _op.endswith('C'): + optype.append(3) + ######################################################## + else: + optype.append(0) + ############################################################ + elif _op.startswith('\x1a'): + optype.append(4) + ############################################################ + else: + optype.append(1) + ################################################################ + offset=0 + processed=[] + processing='' + ############ + for i,_optype in enumerate(optype): + if _optype==2: + processing+=op[i] + offset+=len(op[i]) + ############################################################ + elif _optype==3: + count=int(op[i].split('[')[1].split('C')[0]) + processing+=' '*count + ############################################################ + elif _optype==1: + processing+=op[i] + ############################################################ + if len(processing)-offset > c.width: + while len(processing)-offset > c.width: + deconcatenate=processing[0:c.width+offset] + processed.append(deconcatenate) + processing=processing.replace(deconcatenate,'',1) + offset=0 + ################################################################# + for _ in processed: print(_) + print('\x1b[1;31m[ COMPLETED ]\x1b[0m\n') +##################################################################### diff --git a/test.py b/test.py new file mode 100644 index 0000000..e940ad5 --- /dev/null +++ b/test.py @@ -0,0 +1,21 @@ +ascii_table=""" +ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒ +áíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐ +└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀ +αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■  +""".replace('\n','') + +for i,_ in enumerate(ascii_table): + _int=str(i+128).zfill(3) + _hex=hex(i+128)[2:].upper().zfill(2) + _ord=str(ord(_)).zfill(4) + print(f'{_} - {_int} - {_hex} - {_ord}') + +asc2uni=dict(zip([i for i in range(128,256)],[ord(c) for c in ascii_table])) +asc2chr=dict(zip([i for i in range(128,256)],[c for c in ascii_table])) +print(asc2uni[255]) + +f=open('test.ans','wb') +f.write(bytearray([27,91,48,109])) +f.write(bytearray([i for i in range(128,256)])) +f.close() diff --git a/work.ans b/work.ans new file mode 100644 index 0000000..a95df2b --- /dev/null +++ b/work.ans @@ -0,0 +1,10 @@ +  ۰     ܲ ۰     ۰     ۰      ۰      ۰                                         ݲ        ߲           ߲ܲ  ߲  ߲߰    ܲ    ۲            ߰۲               ܰ       ܰ  ۲    ۲ ۲  ۲  ޲ ۲    ߲   ߲    ܲ     ܲ       ۲         ߰              ߰   ߰  ܲܲ     ܰ ߲      ܰ     ۲ ܲ ܲ     ۲ ݱ  ߲߲ ܰ ޲   ۱ ܲ ޲ ߰     ۱ݲ ߲            ߲޲      ܲ              ݰ  ߲  ۲ ܰ       ܲ۲  ܱ  ݰ ܰ  ۲  ߲    ܲ ۲      ܰ  ߱۲ ۲  ݰ ۲ ۲   ۲ݰ ۲۲ܱ߲ܲ  ܰ ߲۲ ۲      ߲        ۲  ޱ ޲ܰ ۲۲  ۲ ޲۲޲ ۲۲     ۲    ۲޲  ۲    ܲ۲    ۰   ۲  ݱ     ݰ          ܲܲ۲۲  ݲ   ޱ۲   ݲ    ޲޲   ۲   ۲ ۲  ܲ  ߲ ޲ ޲        ޲  ݱ ߲  ޲   ߲۲߰  ݲ   ۲۲  ۲  ߲    ۱߲    ߲   ߲           ߲   ܲ޲    ܲ      ܰ         ۲    ޲ ۲         ۲  ܲ  ۲۰۲ ۲ ۲   ܰ۲ܲܲܲ۱  ޲ ۲۲ ۲ ۲  ۲  ۲ ۲޲   ۲ ޲  ۰ ߰     ܲ             ۰      ۰        +    ܰ ߲ ߲        ߲ ۲߲ܰ ۲۲۲߲   ܲ ۲    ݰ  ܲ   ۲   +߲߲ݰ    ߲     +  ޲  +  + +    +ܲ + +SAUCE00Trash Panda Alpha King Blocktronics 20180608kPIBM VGA \ No newline at end of file