summaryrefslogtreecommitdiffstats
path: root/examples/print-text/ansi.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 16:35:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 16:35:31 +0000
commit4f1a3b5f9ad05aa7b08715d48909a2b06ee2fcb1 (patch)
treee5dee7be2f0d963da4faad6517278d03783e3adc /examples/print-text/ansi.py
parentInitial commit. (diff)
downloadprompt-toolkit-4f1a3b5f9ad05aa7b08715d48909a2b06ee2fcb1.tar.xz
prompt-toolkit-4f1a3b5f9ad05aa7b08715d48909a2b06ee2fcb1.zip
Adding upstream version 3.0.43.upstream/3.0.43
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/print-text/ansi.py')
-rwxr-xr-xexamples/print-text/ansi.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/print-text/ansi.py b/examples/print-text/ansi.py
new file mode 100755
index 0000000..618775c
--- /dev/null
+++ b/examples/print-text/ansi.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+"""
+Demonstration of how to print using ANSI escape sequences.
+
+The advantage here is that this is cross platform. The escape sequences will be
+parsed and turned into appropriate Win32 API calls on Windows.
+"""
+from prompt_toolkit import print_formatted_text
+from prompt_toolkit.formatted_text import ANSI, HTML
+
+print = print_formatted_text
+
+
+def title(text):
+ print(HTML("\n<u><b>{}</b></u>").format(text))
+
+
+def main():
+ title("Special formatting")
+ print(ANSI(" \x1b[1mBold"))
+ print(ANSI(" \x1b[6mBlink"))
+ print(ANSI(" \x1b[3mItalic"))
+ print(ANSI(" \x1b[7mReverse"))
+ print(ANSI(" \x1b[4mUnderline"))
+ print(ANSI(" \x1b[9mStrike"))
+ print(ANSI(" \x1b[8mHidden\x1b[0m (Hidden)"))
+
+ # Ansi colors.
+ title("ANSI colors")
+
+ print(ANSI(" \x1b[91mANSI Red"))
+ print(ANSI(" \x1b[94mANSI Blue"))
+
+ # Other named colors.
+ title("Named colors")
+
+ print(ANSI(" \x1b[38;5;214morange"))
+ print(ANSI(" \x1b[38;5;90mpurple"))
+
+ # Background colors.
+ title("Background colors")
+
+ print(ANSI(" \x1b[97;101mANSI Red"))
+ print(ANSI(" \x1b[97;104mANSI Blue"))
+
+ print()
+
+
+if __name__ == "__main__":
+ main()