summaryrefslogtreecommitdiffstats
path: root/examples/rainbow.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-14 20:18:28 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-14 20:18:28 +0000
commitf8363b456f1ab31ee56abad579b215af195093d5 (patch)
treeb1500c675c2e0a55fb75721a854e1510acf7c862 /examples/rainbow.py
parentInitial commit. (diff)
downloadrich-upstream.tar.xz
rich-upstream.zip
Adding upstream version 9.11.0.upstream/9.11.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
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."))