Created
April 19, 2023 15:49
-
-
Save bengerman13/87f3d909eb8025fd97abbf8c93e2e53c to your computer and use it in GitHub Desktop.
Abusing locals in the name of laziness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| This is a lazy shell text formatting tool. | |
| Use it like this: | |
| ``` | |
| import fmt | |
| print(fmt.red("This text is red")) | |
| print(fmt.bold(fmt.green("this is bold and green")) | |
| print(f"Would you like some {fmt.green("eggs")} and {fmt.green("ham")}") | |
| ``` | |
| """ | |
| COLOR_RESET = 0 | |
| FORMATS = { | |
| "WHITE": 37, | |
| "CYAN": 36, | |
| "MAGENTA": 35, | |
| "BLUE": 34, | |
| "YELLOW": 33, | |
| "GREEN": 32, | |
| "RED": 31, | |
| "BLACK": 30, | |
| "BRIGHT_WHITE": 97, | |
| "BRIGHT_CYAN": 96, | |
| "BRIGHT_MAGENTA": 95, | |
| "BRIGHT_BLUE": 94, | |
| "BRIGHT_YELLOW": 93, | |
| "BRIGHT_GREEN": 92, | |
| "BRIGHT_RED": 91, | |
| "BRIGHT_BLACK": 90, | |
| "BOLD": 1, | |
| "DIM": 2, | |
| "ITALIC": 3, | |
| "UNDERLINE": 4, | |
| "BLINK": 5, | |
| "REVERSE": 7, | |
| "HIDDEN": 8, | |
| "STRIKE": 9, | |
| } | |
| FORMAT_RESETS = { | |
| "WHITE": COLOR_RESET, | |
| "CYAN": COLOR_RESET, | |
| "MAGENTA": COLOR_RESET, | |
| "BLUE": COLOR_RESET, | |
| "YELLOW": COLOR_RESET, | |
| "GREEN": COLOR_RESET, | |
| "RED": COLOR_RESET, | |
| "BLACK": COLOR_RESET, | |
| "BRIGHT_WHITE": COLOR_RESET, | |
| "BRIGHT_CYAN": COLOR_RESET, | |
| "BRIGHT_MAGENTA": COLOR_RESET, | |
| "BRIGHT_BLUE": COLOR_RESET, | |
| "BRIGHT_YELLOW": COLOR_RESET, | |
| "BRIGHT_GREEN": COLOR_RESET, | |
| "BRIGHT_RED": COLOR_RESET, | |
| "BRIGHT_BLACK": COLOR_RESET, | |
| "BOLD": 22, | |
| "DIM": 22, | |
| "ITALIC": 23, | |
| "UNDERLINE": 24, | |
| "BLINK": 25, | |
| "REVERSE": 27, | |
| "HIDDEN": 28, | |
| "STRIKE": 29, | |
| } | |
| def to_control(control: int) -> str: | |
| return f"\033[{control}m" | |
| def format(string, format): | |
| return f"{to_control(FORMATS[format])}{string}{to_control(FORMAT_RESETS[format])}" | |
| for format_name in FORMATS: | |
| locals()[format_name.lower()] = lambda x, f=format_name: format(x, f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment