Search code examples
pythonrich

When I use the rich library to add colour, why does it affect the symbols around it? Is there a way to stop this from happening?


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.


Solution

  • 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.