summaryrefslogtreecommitdiffstats
path: root/examples/print-text/print-formatted-text.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 17:35:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 17:35:20 +0000
commite106bf94eff07d9a59771d9ccc4406421e18ab64 (patch)
treeedb6545500e39df9c67aa918a6125bffc8ec1aee /examples/print-text/print-formatted-text.py
parentInitial commit. (diff)
downloadprompt-toolkit-e106bf94eff07d9a59771d9ccc4406421e18ab64.tar.xz
prompt-toolkit-e106bf94eff07d9a59771d9ccc4406421e18ab64.zip
Adding upstream version 3.0.36.upstream/3.0.36upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/print-text/print-formatted-text.py')
-rwxr-xr-xexamples/print-text/print-formatted-text.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/print-text/print-formatted-text.py b/examples/print-text/print-formatted-text.py
new file mode 100755
index 0000000..4b09ae2
--- /dev/null
+++ b/examples/print-text/print-formatted-text.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+"""
+Example of printing colored text to the output.
+"""
+from prompt_toolkit import print_formatted_text
+from prompt_toolkit.formatted_text import ANSI, HTML, FormattedText
+from prompt_toolkit.styles import Style
+
+print = print_formatted_text
+
+
+def main():
+ style = Style.from_dict(
+ {
+ "hello": "#ff0066",
+ "world": "#44ff44 italic",
+ }
+ )
+
+ # Print using a a list of text fragments.
+ text_fragments = FormattedText(
+ [
+ ("class:hello", "Hello "),
+ ("class:world", "World"),
+ ("", "\n"),
+ ]
+ )
+ print(text_fragments, style=style)
+
+ # Print using an HTML object.
+ print(HTML("<hello>hello</hello> <world>world</world>\n"), style=style)
+
+ # Print using an HTML object with inline styling.
+ print(
+ HTML(
+ '<style fg="#ff0066">hello</style> '
+ '<style fg="#44ff44"><i>world</i></style>\n'
+ )
+ )
+
+ # Print using ANSI escape sequences.
+ print(ANSI("\x1b[31mhello \x1b[32mworld\n"))
+
+
+if __name__ == "__main__":
+ main()