3

What am I trying to do?

I am using the rich library to print words out in different colours.

I have come up with the following program to do so:

from rich import print as rprint

rprint('[[green]1[/green]] Create new password')
print('[2] See existing passwords')
print('[3] Exit')

Output:

enter image description here

My Problem

As you can see on the image above, the square brackets which surround 1 are a brighter in color compared to the ones beneath it 2 & 3. Is there a way to make the square brackets all the same color (grey) instead of a white?

Thanks in advance.

Note:

I am aware that this does not hinder how the program works but I like things to be aesthetically pleasing and this is really bugging me for some reason.

Also, I was just testing how I could go about changing colours using rich, but I'm open to suggestions on other ways do this.

some_user_3
  • 403
  • 1
  • 5
  • 17

2 Answers2

1

Rich performs highlighting on the output, for numbers, strings, data etc. In your example it is highlighting braces, which can be helpful when you print data structures.

You can disable this feature if you construct a Console object and set highlight=False on the print method.

Here's an example:

from rich.console import Console
console = Console()
console.print('[[green]1[/green]] Create new password', highlight=False)

See the docs on highlighting for the details.

Will McGugan
  • 2,005
  • 13
  • 10
0

As the op is open to other ways, here is my way of doing..

Initiate a class with standard terminal color codes.

class bcolors:

    ResetAll = "\033[0m"

    Bold       = "\033[1m"
    Dim        = "\033[2m"
    Underlined = "\033[4m"
    Blink      = "\033[5m"
    Reverse    = "\033[7m"
    Hidden     = "\033[8m"

    ResetBold       = "\033[21m"
    ResetDim        = "\033[22m"
    ResetUnderlined = "\033[24m"
    ResetBlink      = "\033[25m"
    ResetReverse    = "\033[27m"
    ResetHidden     = "\033[28m"

    Default      = "\033[39m"
    Black        = "\033[30m"
    Red          = "\033[31m"
    Green        = "\033[32m"
    Yellow       = "\033[33m"
    Blue         = "\033[34m"
    Magenta      = "\033[35m"
    Cyan         = "\033[36m"
    LightGray    = "\033[37m"
    DarkGray     = "\033[90m"
    LightRed     = "\033[91m"
    LightGreen   = "\033[92m"
    LightYellow  = "\033[93m"
    LightBlue    = "\033[94m"
    LightMagenta = "\033[95m"
    LightCyan    = "\033[96m"
    White        = "\033[97m"

    BackgroundDefault      = "\033[49m"
    BackgroundBlack        = "\033[40m"
    BackgroundRed          = "\033[41m"
    BackgroundGreen        = "\033[42m"
    BackgroundYellow       = "\033[43m"
    BackgroundBlue         = "\033[44m"
    BackgroundMagenta      = "\033[45m"
    BackgroundCyan         = "\033[46m"
    BackgroundLightGray    = "\033[47m"
    BackgroundDarkGray     = "\033[100m"
    BackgroundLightRed     = "\033[101m"
    BackgroundLightGreen   = "\033[102m"
    BackgroundLightYellow  = "\033[103m"
    BackgroundLightBlue    = "\033[104m"
    BackgroundLightMagenta = "\033[105m"
    BackgroundLightCyan    = "\033[106m"
    BackgroundWhite        = "\033[107m"

Your Program

print(f"[{bcolors.Green}1{bcolors.ResetAll}] Create new password")
print('[2] See existing passwords')
print('[3] Exit')

Output:

enter image description here

Ailurophile
  • 2,552
  • 7
  • 21
  • 46
  • Thanks for that! It works fine now :D But i will wait for a little more time before accepting your answer as there may be other ways of doing this – some_user_3 Mar 02 '21 at 12:48
  • 1
    although this worked im going to accept Will Mcugans's answer because it is solved more easily. However, thanks for the answer :D – some_user_3 Mar 02 '21 at 14:01