[docs]defget_color_mapping(items:list[str],excluded_colors:Optional[list]=None)->dict[str,str]:"""Get mapping for items to a support color. Args: items: The items to map to colors. excluded_colors: The colors to exclude. Returns: The mapping of items to colors. """colors=list(_TEXT_COLOR_MAPPING.keys())ifexcluded_colorsisnotNone:colors=[cforcincolorsifcnotinexcluded_colors]color_mapping={item:colors[i%len(colors)]fori,iteminenumerate(items)}returncolor_mapping
[docs]defget_colored_text(text:str,color:str)->str:"""Get colored text. Args: text: The text to color. color: The color to use. Returns: The colored text. """color_str=_TEXT_COLOR_MAPPING[color]returnf"\u001b[{color_str}m\033[1;3m{text}\u001b[0m"
[docs]defget_bolded_text(text:str)->str:"""Get bolded text. Args: text: The text to bold. Returns: The bolded text. """returnf"\033[1m{text}\033[0m"
[docs]defprint_text(text:str,color:Optional[str]=None,end:str="",file:Optional[TextIO]=None)->None:"""Print text with highlighting and no end characters. If a color is provided, the text will be printed in that color. If a file is provided, the text will be written to that file. Args: text: The text to print. color: The color to use. Defaults to None. end: The end character to use. Defaults to "". file: The file to write to. Defaults to None. """text_to_print=get_colored_text(text,color)ifcolorelsetextprint(text_to_print,end=end,file=file)iffile:file.flush()# ensure all printed content are written to file