import re __all__ = ("colorize",) BOLD_PAT = re.compile(r"\*\*(.+?)\*\*", re.MULTILINE) ITALIC_PAT = re.compile(r"//(.+?)//", re.MULTILINE) INTEREST_PAT = re.compile(r"\(\((.+?)\)\)", re.MULTILINE) SHADOW_PAT = re.compile(r"\{\{(.+?)\}\}", re.MULTILINE) BOLD_COL = "\u001b[1m" ITALIC_COL = "\u001b[3m" INTEREST_COL = "\u001b[34;1m" SHADOW_COL = "\u001b[30;1m" RESET_COL = "\u001b[0m" def colorize(text: str) -> str: """ Colorizes text for output on an ANSI terminal. This will use escape codes to replace things. Style guide: ((This)) is "interest" styling. This will make the text blue. {{This}} is "shadow" styling. This will make the text a dark grey (or at least, more subtle.) """ replacements = [ (INTEREST_PAT, INTEREST_COL), (SHADOW_PAT, SHADOW_COL), (BOLD_PAT, BOLD_COL), (ITALIC_PAT, ITALIC_COL), ] for (pat, col) in replacements: text = pat.sub(col + r"\1" + RESET_COL, text) return text