<s> """<STR_LIT>""" <EOL> import re <EOL> from colorclass . codes import ANSICodeMapping , BASE_CODES <EOL> CODE_GROUPS = ( <EOL> tuple ( set ( str ( i ) for i in BASE_CODES . values ( ) if i and ( <NUM_LIT> <= i <= <NUM_LIT> or <NUM_LIT:100> <= i <= <NUM_LIT> ) ) ) , <EOL> tuple ( set ( str ( i ) for i in BASE_CODES . values ( ) if i and ( <NUM_LIT:30> <= i <= <NUM_LIT> or <NUM_LIT> <= i <= <NUM_LIT> ) ) ) , <EOL> ( '<STR_LIT:1>' , '<STR_LIT>' ) , ( '<STR_LIT:2>' , '<STR_LIT>' ) , ( '<STR_LIT:3>' , '<STR_LIT>' ) , ( '<STR_LIT:4>' , '<STR_LIT>' ) , ( '<STR_LIT:5>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> RE_ANSI = re . compile ( r'<STR_LIT>' ) <EOL> RE_COMBINE = re . compile ( r'<STR_LIT>' ) <EOL> RE_SPLIT = re . compile ( r'<STR_LIT>' ) <EOL> def prune_overridden ( ansi_string ) : <EOL> """<STR_LIT>""" <EOL> multi_seqs = set ( p for p in RE_ANSI . findall ( ansi_string ) if '<STR_LIT:;>' in p [ <NUM_LIT:1> ] ) <EOL> for escape , codes in multi_seqs : <EOL> r_codes = list ( reversed ( codes . split ( '<STR_LIT:;>' ) ) ) <EOL> try : <EOL> r_codes = r_codes [ : r_codes . index ( '<STR_LIT:0>' ) + <NUM_LIT:1> ] <EOL> except ValueError : <EOL> pass <EOL> for group in CODE_GROUPS : <EOL> for pos in reversed ( [ i for i , n in enumerate ( r_codes ) if n in group ] [ <NUM_LIT:1> : ] ) : <EOL> r_codes . pop ( pos ) <EOL> reduced_codes = '<STR_LIT:;>' . join ( sorted ( r_codes , key = int ) ) <EOL> if codes != reduced_codes : <EOL> ansi_string = ansi_string . replace ( escape , '<STR_LIT>' + reduced_codes + '<STR_LIT:m>' ) <EOL> return ansi_string <EOL> def parse_input ( tagged_string , disable_colors ) : <EOL> """<STR_LIT>""" <EOL> codes = ANSICodeMapping ( tagged_string ) <EOL> output_colors = getattr ( tagged_string , '<STR_LIT>' , tagged_string ) <EOL> for tag , replacement in ( ( '<STR_LIT:{>' + k + '<STR_LIT:}>' , '<STR_LIT>' if v is None else '<STR_LIT>' % v ) for k , v in codes . items ( ) ) : <EOL> output_colors = output_colors . replace ( tag , replacement ) <EOL> output_no_colors = RE_ANSI . sub ( '<STR_LIT>' , output_colors ) <EOL> if disable_colors : <EOL> return output_no_colors , output_no_colors <EOL> while True : <EOL> simplified = RE_COMBINE . sub ( r'<STR_LIT>' , output_colors ) <EOL> if simplified == output_colors : <EOL> break <EOL> output_colors = simplified <EOL> output_colors = prune_overridden ( output_colors ) <EOL> previous_escape = None <EOL> segments = list ( ) <EOL> for item in ( i for i in RE_SPLIT . split ( output_colors ) if i ) : <EOL> if RE_SPLIT . match ( item ) : <EOL> if item != previous_escape : <EOL> segments . append ( item ) <EOL> previous_escape = item <EOL> else : <EOL> segments . append ( item ) <EOL> output_colors = '<STR_LIT>' . join ( segments ) <EOL> return output_colors , output_no_colors </s>
