diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 16:33:15 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 16:33:15 +0000 |
commit | e5f7a41d988de298069eda636e823f374b973bd4 (patch) | |
tree | a6a97f4d2ed5e59ab11cbae6b403ddebc5effe00 /ptpython/prompt_style.py | |
parent | Initial commit. (diff) | |
download | ptpython-e5f7a41d988de298069eda636e823f374b973bd4.tar.xz ptpython-e5f7a41d988de298069eda636e823f374b973bd4.zip |
Adding upstream version 3.0.26.upstream/3.0.26
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ptpython/prompt_style.py')
-rw-r--r-- | ptpython/prompt_style.py | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/ptpython/prompt_style.py b/ptpython/prompt_style.py new file mode 100644 index 0000000..96b738f --- /dev/null +++ b/ptpython/prompt_style.py @@ -0,0 +1,79 @@ +from __future__ import annotations + +from abc import ABCMeta, abstractmethod +from typing import TYPE_CHECKING + +from prompt_toolkit.formatted_text import AnyFormattedText + +if TYPE_CHECKING: + from .python_input import PythonInput + +__all__ = ["PromptStyle", "IPythonPrompt", "ClassicPrompt"] + + +class PromptStyle(metaclass=ABCMeta): + """ + Base class for all prompts. + """ + + @abstractmethod + def in_prompt(self) -> AnyFormattedText: + "Return the input tokens." + return [] + + @abstractmethod + def in2_prompt(self, width: int) -> AnyFormattedText: + """ + Tokens for every following input line. + + :param width: The available width. This is coming from the width taken + by `in_prompt`. + """ + return [] + + @abstractmethod + def out_prompt(self) -> AnyFormattedText: + "Return the output tokens." + return [] + + +class IPythonPrompt(PromptStyle): + """ + A prompt resembling the IPython prompt. + """ + + def __init__(self, python_input: PythonInput) -> None: + self.python_input = python_input + + def in_prompt(self) -> AnyFormattedText: + return [ + ("class:in", "In ["), + ("class:in.number", "%s" % self.python_input.current_statement_index), + ("class:in", "]: "), + ] + + def in2_prompt(self, width: int) -> AnyFormattedText: + return [("class:in", "...: ".rjust(width))] + + def out_prompt(self) -> AnyFormattedText: + return [ + ("class:out", "Out["), + ("class:out.number", "%s" % self.python_input.current_statement_index), + ("class:out", "]:"), + ("", " "), + ] + + +class ClassicPrompt(PromptStyle): + """ + The classic Python prompt. + """ + + def in_prompt(self) -> AnyFormattedText: + return [("class:prompt", ">>> ")] + + def in2_prompt(self, width: int) -> AnyFormattedText: + return [("class:prompt.dots", "...")] + + def out_prompt(self) -> AnyFormattedText: + return [] |