Saturday, January 7, 2023

(Guide with code) The colorama names are too long so I made shorter ones

So you're apparently supposed to use colorama like this:

import colorama

# Initialize the library
colorama.init()

# Foreground colors
print(colorama.Fore.BLACK + "Black text")
print(colorama.Fore.RED + "Red text")
print(colorama.Fore.GREEN + "Green text")
print(colorama.Fore.YELLOW + "Yellow text")
print(colorama.Fore.BLUE + "Blue text")
print(colorama.Fore.MAGENTA + "Magenta text")
print(colorama.Fore.CYAN + "Cyan text")
print(colorama.Fore.WHITE + "White text")

# Reset the foreground color
print(colorama.Fore.RESET + "Text with the foreground color reset to the default")

# Background colors
print(colorama.Back.BLACK + "Black background")
print(colorama.Back.RED + "Red background")
print(colorama.Back.GREEN + "Green background")
print(colorama.Back.YELLOW + "Yellow background")
print(colorama.Back.BLUE + "Blue background")
print(colorama.Back.MAGENTA + "Magenta background")
print(colorama.Back.CYAN + "Cyan background")
print(colorama.Back.WHITE + "White background")

# Reset the background color
print(colorama.Back.RESET + "Text with the background color reset to the default")

# Style options
print(colorama.Style.DIM + "Dim text")
print(colorama.Style.NORMAL + "Normal text")
print(colorama.Style.BRIGHT + "Bright text")
print(colorama.Style.RESET_ALL + "Text with all styles reset to the default")

print("\n**************************************************************")
print("Now, the same thing but using f-strings to print the statments")
print("**************************************************************")

# Foreground colors
print(f"{colorama.Fore.BLACK}Black text")
print(f"{colorama.Fore.RED}Red text")
print(f"{colorama.Fore.GREEN}Green text")
print(f"{colorama.Fore.YELLOW}Yellow text")
print(f"{colorama.Fore.BLUE}Blue text")
print(f"{colorama.Fore.MAGENTA}Magenta text")
print(f"{colorama.Fore.CYAN}Cyan text")
print(f"{colorama.Fore.WHITE}White text")

# Reset the foreground color
print(f"{colorama.Fore.RESET}Text with the foreground color reset to the default")

# Background colors
print(f"{colorama.Back.BLACK}Black background")
print(f"{colorama.Back.RED}Red background")
print(f"{colorama.Back.GREEN}Green background")
print(f"{colorama.Back.YELLOW}Yellow background")
print(f"{colorama.Back.BLUE}Blue background")
print(f"{colorama.Back.MAGENTA}Magenta background")
print(f"{colorama.Back.CYAN}Cyan background")
print(f"{colorama.Back.WHITE}White background")

# Reset the background color
print(f"{colorama.Back.RESET}Text with the background color reset to the default")

# Style options
print(f"{colorama.Style.DIM}Dim text")
print(f"{colorama.Style.NORMAL}Normal text")
print(f"{colorama.Style.BRIGHT}Bright text")
print(f"{colorama.Style.RESET_ALL}Text with all styles reset to the default")

I don't know what you think, but I think those names are too long and annoying, so I created shorter versions:

import colorama

# Initialize the library
colorama.init()

# Create a lookup table of shorter variable names for the Fore options
F = {
    "BLK": colorama.Fore.BLACK,
    "RED": colorama.Fore.RED,
    "GRN": colorama.Fore.GREEN,
    "YEL": colorama.Fore.YELLOW,
    "BLU": colorama.Fore.BLUE,
    "MAG": colorama.Fore.MAGENTA,
    "CYN": colorama.Fore.CYAN,
    "WHT": colorama.Fore.WHITE,
    "RST": colorama.Fore.RESET,
}

# Create a lookup table of shorter variable names for the Back options
B = {
    "BLK": colorama.Back.BLACK,
    "RED": colorama.Back.RED,
    "GRN": colorama.Back.GREEN,
    "YEL": colorama.Back.YELLOW,
    "BLU": colorama.Back.BLUE,
    "MAG": colorama.Back.MAGENTA,
    "CYN": colorama.Back.CYAN,
    "WHT": colorama.Back.WHITE,
    "RST": colorama.Back.RESET,
}

# Create a lookup table of shorter variable names for the Style options
S = {
    "DIM": colorama.Style.DIM,
    "NRM": colorama.Style.NORMAL,
    "BRT": colorama.Style.BRIGHT,
    "RST": colorama.Style.RESET_ALL,
}


# Print text with different colors and styles
print(f"{F['BLK']}{S['DIM']}Black text with dim style")
print(f"{F['RED']}{S['NRM']}Red text with normal style")
print(f"{F['GRN']}{S['BRT']}Green text with bright style")
print(f"{F['YEL']}{S['RST']}Yellow text with all styles reset")

# Print text with different backgrounds and styles
print(f"{B['BLK']}{S['DIM']}Text with black background and dim style")
print(f"{B['RED']}{S['NRM']}Text with red background and normal style")
print(f"{B['GRN']}{S['BRT']}Text with green background and bright style")
print(f"{B['YEL']}{S['RST']}Text with yellow background and all styles reset")

# Print text with different colors, backgrounds, and styles
print(f"{F['BLU']}{B['CYN']}{S['DIM']}Blue text on cyan background with dim style")
print(f"{F['MAG']}{B['WHT']}{S['NRM']}Magenta text on white background with normal style")
print(f"{F['CYN']}{B['BLK']}{S['BRT']}Cyan text on black background with bright style")
print(f"{F['WHT']}{B['RED']}{S['RST']}White text on red background with all styles reset")

# Print text with multiple styles on the same line
print(f"{S['DIM']}{F['BLK']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}text")
print(f"{S['DIM']}{F['RED']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}text")
print(f"{S['DIM']}{F['GRN']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}text")
print(f"{S['DIM']}{F['YEL']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}text")

# Print text with different colors, backgrounds, and styles
print(f"{F['BLU']}{B['CYN']}{S['DIM']}Blue text on cyan background with dim style")
print(f"{F['MAG']}{B['WHT']}{S['NRM']}Magenta text on white background with normal style")
print(f"{F['CYN']}{B['BLK']}{S['BRT']}Cyan text on black background with bright style")
print(f"{F['WHT']}{B['RED']}{S['RST']}White text on red background with all styles reset")

# Print text with different colors, backgrounds, and styles using multiple styles on the same line
print(f"{F['BLU']}{B['CYN']}{S['DIM']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}Blue text on cyan background")
print(f"{F['MAG']}{B['WHT']}{S['DIM']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}Magenta text on white background")
print(f"{F['CYN']}{B['BLK']}{S['DIM']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}Cyan text on black background")
print(f"{F['WHT']}{B['RED']}{S['DIM']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}White text on red background")

# Print all of the possible combinations of colors, backgrounds, and styles using multiple styles on the same line
for fg in F:
    for bg in B:
        print(f"{F[fg]}{B[bg]}{S['DIM']}Dim {S['BRT']}Bright {S['RST']}Normal {F['RST']}Text with {fg} foreground on {bg} background")

That's better right? Let me know what you think in the comments!


No comments:

Post a Comment