1from abc import ABCMeta, abstractmethod
2from typing import TYPE_CHECKING
3
4from prompt_toolkit.formatted_text import AnyFormattedText
5
6if TYPE_CHECKING:
7    from .python_input import PythonInput
8
9__all__ = ["PromptStyle", "IPythonPrompt", "ClassicPrompt"]
10
11
12class PromptStyle(metaclass=ABCMeta):
13    """
14    Base class for all prompts.
15    """
16
17    @abstractmethod
18    def in_prompt(self) -> AnyFormattedText:
19        "Return the input tokens."
20        return []
21
22    @abstractmethod
23    def in2_prompt(self, width: int) -> AnyFormattedText:
24        """
25        Tokens for every following input line.
26
27        :param width: The available width. This is coming from the width taken
28                      by `in_prompt`.
29        """
30        return []
31
32    @abstractmethod
33    def out_prompt(self) -> AnyFormattedText:
34        "Return the output tokens."
35        return []
36
37
38class IPythonPrompt(PromptStyle):
39    """
40    A prompt resembling the IPython prompt.
41    """
42
43    def __init__(self, python_input: "PythonInput") -> None:
44        self.python_input = python_input
45
46    def in_prompt(self) -> AnyFormattedText:
47        return [
48            ("class:in", "In ["),
49            ("class:in.number", "%s" % self.python_input.current_statement_index),
50            ("class:in", "]: "),
51        ]
52
53    def in2_prompt(self, width: int) -> AnyFormattedText:
54        return [("class:in", "...: ".rjust(width))]
55
56    def out_prompt(self) -> AnyFormattedText:
57        return [
58            ("class:out", "Out["),
59            ("class:out.number", "%s" % self.python_input.current_statement_index),
60            ("class:out", "]:"),
61            ("", " "),
62        ]
63
64
65class ClassicPrompt(PromptStyle):
66    """
67    The classic Python prompt.
68    """
69
70    def in_prompt(self) -> AnyFormattedText:
71        return [("class:prompt", ">>> ")]
72
73    def in2_prompt(self, width: int) -> AnyFormattedText:
74        return [("class:prompt.dots", "...")]
75
76    def out_prompt(self) -> AnyFormattedText:
77        return []
78