rich/examples/rainbow.py

21 lines
441 B
Python
Raw Permalink Normal View History

2020-06-01 15:08:06 +00:00
"""
This example demonstrates how to write a custom highlighter.
"""
2020-05-08 12:36:12 +00:00
from random import randint
from rich import print
from rich.highlighter import Highlighter
class RainbowHighlighter(Highlighter):
def highlight(self, text):
for index in range(len(text)):
2020-08-08 16:38:57 +00:00
text.stylize(f"color({randint(16, 255)})", index, index + 1)
2020-05-08 12:36:12 +00:00
rainbow = RainbowHighlighter()
print(rainbow("I must not fear. Fear is the mind-killer."))