summaryrefslogtreecommitdiffstats
path: root/examples/rainbow.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rainbow.py')
-rw-r--r--examples/rainbow.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/examples/rainbow.py b/examples/rainbow.py
new file mode 100644
index 0000000..2121a78
--- /dev/null
+++ b/examples/rainbow.py
@@ -0,0 +1,20 @@
+"""
+
+This example demonstrates how to write a custom highlighter.
+
+"""
+
+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)):
+ text.stylize(f"color({randint(16, 255)})", index, index + 1)
+
+
+rainbow = RainbowHighlighter()
+print(rainbow("I must not fear. Fear is the mind-killer."))