summaryrefslogtreecommitdiffstats
path: root/examples/print-text/pygments-tokens.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/pygments-tokens.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/pygments-tokens.py')
-rwxr-xr-xexamples/print-text/pygments-tokens.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/print-text/pygments-tokens.py b/examples/print-text/pygments-tokens.py
new file mode 100755
index 0000000..3470e8e
--- /dev/null
+++ b/examples/print-text/pygments-tokens.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+"""
+Printing a list of Pygments (Token, text) tuples,
+or an output of a Pygments lexer.
+"""
+import pygments
+from pygments.lexers.python import PythonLexer
+from pygments.token import Token
+
+from prompt_toolkit import print_formatted_text
+from prompt_toolkit.formatted_text import PygmentsTokens
+from prompt_toolkit.styles import Style
+
+
+def main():
+ # Printing a manually constructed list of (Token, text) tuples.
+ text = [
+ (Token.Keyword, "print"),
+ (Token.Punctuation, "("),
+ (Token.Literal.String.Double, '"'),
+ (Token.Literal.String.Double, "hello"),
+ (Token.Literal.String.Double, '"'),
+ (Token.Punctuation, ")"),
+ (Token.Text, "\n"),
+ ]
+
+ print_formatted_text(PygmentsTokens(text))
+
+ # Printing the output of a pygments lexer.
+ tokens = list(pygments.lex('print("Hello")', lexer=PythonLexer()))
+ print_formatted_text(PygmentsTokens(tokens))
+
+ # With a custom style.
+ style = Style.from_dict(
+ {
+ "pygments.keyword": "underline",
+ "pygments.literal.string": "bg:#00ff00 #ffffff",
+ }
+ )
+ print_formatted_text(PygmentsTokens(tokens), style=style)
+
+
+if __name__ == "__main__":
+ main()