= 'something'
text = f'{text}-<fg red><bg #f0ffff>{text}</fg></bg>-{text}'
text_w_tag print(colorize(text_w_tag))
Colorizer
Colorize text via ansi escape code
- Fore: foreground
- Back: background
- style: font style
SystemStream
SystemStream ()
Initialize self. See help(type(self)) for accurate signature.
AnsiColorizer
AnsiColorizer ()
For text
parameter, you can add color tag. Start with <tag> end with </tag>.
Some usage:
= 'something'
text
# 1. blue text tag:
f'<blue>{text}</fg>'
f'<blue>{text}</blue>'
# 2. specify fg:
f'<fg red>{text}</fg>'
# 3. specify bg:
f'<bg purple>{text}</bg>'
# 4. style:
f'<bold>{text}</bold>'
# 5. support rgb format:
f'<255, 255, 255>{text}</fg>'
f'<fg 255, 255, 255>{text}</fg>'
f'<bg 255, 255, 255>{text}</bg>'
# 6. support hex format:
f'<#FFFFFF>{text}</fg>'
f'<fg #FFFFFF>{text}</fg>'
f'<bg #FFFFFF>{text}</bg>'
# 7. support 8-bits color:
f'<50>{text}</fg>'
f'<fg 50>{text}</fg>'
f'<bg 50>{text}</bg>'
# 8. mix of above is ok:
f'<fg red>{text}--<bg green>{text}--</fg>{text}--</bg>{text}'
Here is the sample usage:
And some other complex uasge:
Code
= ('one', 'two', 'three', 'four', 'five')
test_strings = [
test_templates '{0}',
'<blue>{0}</fg>',
'<red>{0}</red>--<bg green>{1}</bg green>',
'{0}--<red>{1}</red>--<fg red><bg green>{2}</bg>--{3}</fg>',
'{0}--<50>{1}</fg>--<fg 155><bg 78>{2}</bg></fg>',
'<bold>{0}--<fg 180, 46, 78>{1}</fg></bold>--<bg 152, 167, 52>{2}</bg>',
'<underline>{0}--<180, 46, 78>{1}</fg>--<bold>{1}--<bg 152, 167, 52>{2}</underline>--{3}</bold>--{4}</bg>',
'<bg #59FFAE>{0}--<#AAAA00>{1}--</bg>{2}</fg>--{3}',
]
for template in test_templates:
print(colorize(template.format(*test_strings)))
colorize
also integrates with AnsiColor
:
print(colorize('something1', fore=5, back='#ffeeaa', style='bold'))
print(colorize('something2', fore='r', back='y', style='underline'))
ColorStream
ColorStream (fore=None, back=None, style=None, autoreset=True, filename=None, streams='stdout')
Enables context managers to work as decorators to colorize the sys.stdout
or sys.stderr
Some usage:
with ColorStream(fore='red'):
print('text')
@ColorStream(fore=Fore.dark_orange)
def foo():
print('FOO')
with ColorStream(fore='#ff0000', back='(10, 25, 119)'):
print('#ff0000')
print('#ff0000', file=sys.stderr)
with ColorStream(fore=50, back='(10, 25, 119)', streams='stderr'):
print('50')
print('50', file=sys.stderr)
with ColorStream(fore=Fore.dark_violet, autoreset=False):
print('autoreset off, affect next text')
with ColorStream(back=Back.light_green, style=(Style.underline, Style.bold)):
print('add background, underline, bold and autoreset')
with ColorStream(fore='red'):
print('Due to autoreset above, It only have red color')
print('Already leave context, show default color')
@ColorStream(fore=Fore.dark_orange)
def foo():
print('dark_orange')
print('It would not affect sys.stderr', file=sys.stderr)
foo()
It could also output color text to text file:
with ColorStream(fore=Fore.blueviolet, filename='test_Colorstream.txt'):
print('test_Colorstream: ')
print('It would print blueviolet texts, and write the text to test_Colorstream.txt')
!cat test_Colorstream.txt