为什么我使用丰富的库添加颜色时,它会影响周围的符号?有没有办法阻止这种情况发生?
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?
我想做什么?
我正在使用 rich
库打印不同颜色的文字。
我想出了以下程序:
from rich import print as rprint
rprint('[[green]1[/green]] Create new password')
print('[2] See existing passwords')
print('[3] Exit')
输出:
我的问题
如上图所示,1
周围的方括号比 2 & 3
下方的方括号颜色更亮。有没有办法让方括号的颜色全部相同(灰色)而不是白色?
提前致谢。
注:
我知道这不会妨碍程序的工作方式,但我喜欢美观的东西,出于某种原因这确实让我很烦恼。
此外,我只是在测试如何使用 rich
来更改颜色,但我愿意接受有关其他方法的建议。
由于操作对其他方式开放,这是我的做法..
用standard terminal color codes发起class。
class bcolors:
ResetAll = "3[0m"
Bold = "3[1m"
Dim = "3[2m"
Underlined = "3[4m"
Blink = "3[5m"
Reverse = "3[7m"
Hidden = "3[8m"
ResetBold = "3[21m"
ResetDim = "3[22m"
ResetUnderlined = "3[24m"
ResetBlink = "3[25m"
ResetReverse = "3[27m"
ResetHidden = "3[28m"
Default = "3[39m"
Black = "3[30m"
Red = "3[31m"
Green = "3[32m"
Yellow = "3[33m"
Blue = "3[34m"
Magenta = "3[35m"
Cyan = "3[36m"
LightGray = "3[37m"
DarkGray = "3[90m"
LightRed = "3[91m"
LightGreen = "3[92m"
LightYellow = "3[93m"
LightBlue = "3[94m"
LightMagenta = "3[95m"
LightCyan = "3[96m"
White = "3[97m"
BackgroundDefault = "3[49m"
BackgroundBlack = "3[40m"
BackgroundRed = "3[41m"
BackgroundGreen = "3[42m"
BackgroundYellow = "3[43m"
BackgroundBlue = "3[44m"
BackgroundMagenta = "3[45m"
BackgroundCyan = "3[46m"
BackgroundLightGray = "3[47m"
BackgroundDarkGray = "3[100m"
BackgroundLightRed = "3[101m"
BackgroundLightGreen = "3[102m"
BackgroundLightYellow = "3[103m"
BackgroundLightBlue = "3[104m"
BackgroundLightMagenta = "3[105m"
BackgroundLightCyan = "3[106m"
BackgroundWhite = "3[107m"
你的程序
print(f"[{bcolors.Green}1{bcolors.ResetAll}] Create new password")
print('[2] See existing passwords')
print('[3] Exit')
输出:
Rich 在输出上突出显示数字、字符串、数据等。在您的示例中,它突出显示了大括号,这在您打印数据结构时很有用。
如果您构造一个 Console 对象并在打印方法上设置 highlight=False
,则可以禁用此功能。
这是一个例子:
from rich.console import Console
console = Console()
console.print('[[green]1[/green]] Create new password', highlight=False)
有关详细信息,请参阅 highlighting 上的文档。
我想做什么?
我正在使用 rich
库打印不同颜色的文字。
我想出了以下程序:
from rich import print as rprint
rprint('[[green]1[/green]] Create new password')
print('[2] See existing passwords')
print('[3] Exit')
输出:
我的问题
如上图所示,1
周围的方括号比 2 & 3
下方的方括号颜色更亮。有没有办法让方括号的颜色全部相同(灰色)而不是白色?
提前致谢。
注:
我知道这不会妨碍程序的工作方式,但我喜欢美观的东西,出于某种原因这确实让我很烦恼。
此外,我只是在测试如何使用 rich
来更改颜色,但我愿意接受有关其他方法的建议。
由于操作对其他方式开放,这是我的做法..
用standard terminal color codes发起class。
class bcolors:
ResetAll = "3[0m"
Bold = "3[1m"
Dim = "3[2m"
Underlined = "3[4m"
Blink = "3[5m"
Reverse = "3[7m"
Hidden = "3[8m"
ResetBold = "3[21m"
ResetDim = "3[22m"
ResetUnderlined = "3[24m"
ResetBlink = "3[25m"
ResetReverse = "3[27m"
ResetHidden = "3[28m"
Default = "3[39m"
Black = "3[30m"
Red = "3[31m"
Green = "3[32m"
Yellow = "3[33m"
Blue = "3[34m"
Magenta = "3[35m"
Cyan = "3[36m"
LightGray = "3[37m"
DarkGray = "3[90m"
LightRed = "3[91m"
LightGreen = "3[92m"
LightYellow = "3[93m"
LightBlue = "3[94m"
LightMagenta = "3[95m"
LightCyan = "3[96m"
White = "3[97m"
BackgroundDefault = "3[49m"
BackgroundBlack = "3[40m"
BackgroundRed = "3[41m"
BackgroundGreen = "3[42m"
BackgroundYellow = "3[43m"
BackgroundBlue = "3[44m"
BackgroundMagenta = "3[45m"
BackgroundCyan = "3[46m"
BackgroundLightGray = "3[47m"
BackgroundDarkGray = "3[100m"
BackgroundLightRed = "3[101m"
BackgroundLightGreen = "3[102m"
BackgroundLightYellow = "3[103m"
BackgroundLightBlue = "3[104m"
BackgroundLightMagenta = "3[105m"
BackgroundLightCyan = "3[106m"
BackgroundWhite = "3[107m"
你的程序
print(f"[{bcolors.Green}1{bcolors.ResetAll}] Create new password")
print('[2] See existing passwords')
print('[3] Exit')
输出:
Rich 在输出上突出显示数字、字符串、数据等。在您的示例中,它突出显示了大括号,这在您打印数据结构时很有用。
如果您构造一个 Console 对象并在打印方法上设置 highlight=False
,则可以禁用此功能。
这是一个例子:
from rich.console import Console
console = Console()
console.print('[[green]1[/green]] Create new password', highlight=False)
有关详细信息,请参阅 highlighting 上的文档。