From e5f7a41d988de298069eda636e823f374b973bd4 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 18:33:15 +0200 Subject: Adding upstream version 3.0.26. Signed-off-by: Daniel Baumann --- examples/python-embed-with-custom-prompt.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 examples/python-embed-with-custom-prompt.py (limited to 'examples/python-embed-with-custom-prompt.py') diff --git a/examples/python-embed-with-custom-prompt.py b/examples/python-embed-with-custom-prompt.py new file mode 100755 index 0000000..d54da1d --- /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, AnyFormattedText + +from ptpython.prompt_style import PromptStyle +from ptpython.repl import embed + + +def configure(repl) -> None: + # 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) -> AnyFormattedText: + return HTML("Input[%s]: ") % ( + repl.current_statement_index, + ) + + def in2_prompt(self, width: int) -> AnyFormattedText: + return "...: ".rjust(width) + + def out_prompt(self) -> AnyFormattedText: + return HTML("Result[%s]: ") % ( + repl.current_statement_index, + ) + + repl.all_prompt_styles["custom"] = CustomPrompt() + repl.prompt_style = "custom" + + +def main() -> None: + embed(globals(), locals(), configure=configure) + + +if __name__ == "__main__": + main() -- cgit v1.2.3