summaryrefslogtreecommitdiffstats
path: root/examples/python-embed-with-custom-prompt.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 17:36:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 17:36:10 +0000
commitf0d475e3b5d4f226d24837256efc697271b1c611 (patch)
treeeeb27b686fce91e13efc5043dcf47d131c392b56 /examples/python-embed-with-custom-prompt.py
parentInitial commit. (diff)
downloadptpython-upstream.tar.xz
ptpython-upstream.zip
Adding upstream version 3.0.22.upstream/3.0.22upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rwxr-xr-xexamples/python-embed-with-custom-prompt.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py
new file mode 100755
index 0000000..968aedc
--- /dev/null
+++ b/examples/python-embed-with-custom-prompt.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+"""
+Example of embedding a Python REPL, and setting a custom prompt.
+"""
+from prompt_toolkit.formatted_text import HTML
+
+from ptpython.prompt_style import PromptStyle
+from ptpython.repl import embed
+
+
+def configure(repl):
+ # Probably, the best is to add a new PromptStyle to `all_prompt_styles` and
+ # activate it. This way, the other styles are still selectable from the
+ # menu.
+ class CustomPrompt(PromptStyle):
+ def in_prompt(self):
+ return HTML("<ansigreen>Input[%s]</ansigreen>: ") % (
+ repl.current_statement_index,
+ )
+
+ def in2_prompt(self, width):
+ return "...: ".rjust(width)
+
+ def out_prompt(self):
+ return HTML("<ansired>Result[%s]</ansired>: ") % (
+ repl.current_statement_index,
+ )
+
+ repl.all_prompt_styles["custom"] = CustomPrompt()
+ repl.prompt_style = "custom"
+
+
+def main():
+ embed(globals(), locals(), configure=configure)
+
+
+if __name__ == "__main__":
+ main()