diff --git a/README.md b/README.md index 6da5a6c..dab2171 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,11 @@ cp437 ansi parsing ``` dr1p4ns1.py - main file, reads 'work.ans' and attempts to parse and display work.ans - this ansi file is loaded by 'dr1p4ns1.py', this is the logic testing +work.asc - escape codes/colors stripped in text for test referencing test.py - used to generate test ansi file and presents table data ``` - dr1p4ns1.py - work.ans +- work.asc - test.py ``` diff --git a/dr1p4ns1.py b/dr1p4ns1.py index 1294974..f14966f 100644 --- a/dr1p4ns1.py +++ b/dr1p4ns1.py @@ -1,8 +1,43 @@ -############################################################################# SOF -################################################################# CLASS DR1P4NS1 +################################################################################################ SOF + +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMh+MMMMMMMMMMMMMMhsMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMm/ oMMMMMMMMMMMMMMm +NMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMy` yMMMMMMMMMMMMMMM- -mMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMs+dMMMMMMMMMM+ sMMMMMMMMMMMMMMM- `dMMMMMMMMMMms/NMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMM+ .omMMMMMM: -MMMMMMMMMMMMMMo `yMMMMMMMy: `dMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMM- /dMMM+ sMMMMMMMMMMMMh `hMMMNo` sMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMM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MMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMN. /MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMN+` `+NMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNs. -hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM +MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMdyymMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM + +##################################################################################### CLASS DR1P4NS1 + class dr1p4ns1: - ############################################################################ + ################################################################################# + commands = { 'A': 'up', 'B': 'down', @@ -18,25 +53,29 @@ class dr1p4ns1: 'R': 'report_cursor_position' } - ############################################################################ + ################################################################################# + asc_table=""" ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒ\ áíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐\ └┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀\ αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■  """ + ################################################################################# - ############################################################################ for r in (('\n',''),(' ','')): asc_table=asc_table.replace(*r) - ############################################################################ + ################################################################################# + asc2uni=dict(zip([i for i in range(128,256)],[ord(c) for c in asc_table])) - ############################################################################ + ################################################################################# + asc2chr=dict(zip([i for i in range(128,256)],[c for c in asc_table])) - ############################################################################ + ################################################################################# + def __init__(self,ansifile="",width=80,height=25,debug=False): self.DEBUG=debug self.width=width @@ -48,19 +87,22 @@ class dr1p4ns1: print("\x1bc\x1b[1;31m[ DR1P ]\x1b[0m\n") self.boot() - ############################################################################ + ################################################################################# + def openansi(self,s): f=open(s,'rb') self.ansifile=f.read().decode('cp437') f.close() - ############################################################################ + ################################################################################# + def openascii(self): f=open('work.asc','r') self.reference=f.read() f.close() - ############################################################################ + ################################################################################# + def printascii(self): for i,_ in enumerate(self.asc_table): _int=str(i+128).zfill(3) @@ -68,7 +110,8 @@ class dr1p4ns1: _ord=str(ord(_)).zfill(4) print(f'{_} - {_int} - {_hex} - {_ord}') - ############################################################################ + ################################################################################# + def printrange(self,n,nn): for index,_ in enumerate(self.ansifile): h=hex(_)[2:].upper() @@ -84,7 +127,8 @@ class dr1p4ns1: if index==nn: break - ############################################################################ + ################################################################################# + def printhex(self): theasc=[] thehex=[] @@ -99,7 +143,7 @@ class dr1p4ns1: thehex.append(subchunks.strip()) for __ in thehex: buffer='' - for _ in __.split(): + for _ in __.split(): i=int(_,16) if i>47 and i<127: buffer+=chr(i) @@ -112,7 +156,8 @@ class dr1p4ns1: if not buffer == old: print(f"{hex(16 * _)[2:].zfill(8)} {thehex[_]} {theasc[_]}") - ############################################################################ + ################################################################################# + def hexdump(self): if self.DEBUG: print("\n\x1b[1;31m[ HEXDUMP ]\x1b[0m\n") @@ -137,11 +182,13 @@ class dr1p4ns1: 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): SPASS='\x1b[1;31m[ SAUCE INFO FOUND ] - X: {} Y: {}\x1b[0m\n' SFAIL='\x1b[1;31m[ NO SAUCE INFO FOUND ]\x1b[0m\n' @@ -158,7 +205,8 @@ class dr1p4ns1: except: print(SFAIL) - ############################################################################ + ################################################################################# + def cmpans(self,s1,s2,n1=0,n2=0): _s1=''; _s2=''; _sn1=''; _sn2='' _s1=self.stripcodes(s1) @@ -179,11 +227,13 @@ class dr1p4ns1: n1+=self.width n2+=self.width - ############################################################################ - def chunk(self,s,n): + ################################################################################# + + def chunk(self,s,n): return [s[i:i+n] for i in range(0,len(s),n)] - ############################################################################ + ################################################################################# + def stripcodes(self,s): buffer=s code_start=0 @@ -203,12 +253,14 @@ class dr1p4ns1: buffer=buffer.replace(code,'') return buffer - ############################################################################ + ################################################################################# + 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', @@ -223,11 +275,13 @@ class dr1p4ns1: r,g,b=0,0,0 return f"\033[38;2;{r};{g};{b}m{t}\033[38;2;255;255;255m" - ############################################################################ + ################################################################################# + def invert_dict(self,d): return {v: k for k, v in d.items()} - ############################################################################ + ################################################################################# + def boot(self): self.getsauce() self.hexdump() @@ -250,7 +304,7 @@ class dr1p4ns1: optype.append(2) elif _op.endswith('C'): optype.append(3) - else: + else: optype.append(0) elif _op.startswith('\x1a'): optype.append(4) @@ -260,7 +314,7 @@ class dr1p4ns1: offsets=[] processed=[] uniqued=[] - uniques=[] + uniques=[] processing='' for i,_optype in enumerate(optype): if _optype==2: @@ -301,16 +355,16 @@ class dr1p4ns1: self.cmpans(s1=s1,s2=s2,n1=0,n2=self.width) if i >= 128: break - for i,_ in enumerate(self.cmps): + for i,_ in enumerate(self.cmps): print(_+f' - {str(i).zfill(4)}') if self.DEBUG: print("\n \x1b[1;31m[ PROCESSED ANSI ]\x1b[0m\n") for _ in processed: print(_) print('\n\x1b[1;31m[ DR0P ]\x1b[0m') -########################################################################### MAIN +############################################################################################### MAIN -if __name__ == "__main__": +if __name__ == "__main__": d=dr1p4ns1(ansifile='work.ans',width=80,debug=False) -############################################################################ EOF +################################################################################################ EOF