-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathprompt_style.py
86 lines (64 loc) · 2.22 KB
/
prompt_style.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations
import sys
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.
It will set `sys.ps1` to let some programs know they are run in a REPL.
See: https://github.com/TylerYep/torchinfo/issues/216
"""
def __init__(self) -> None:
sys.ps1 = getattr(sys, "ps1", ">>> ")
@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:
super().__init__()
self.python_input = python_input
def in_prompt(self) -> AnyFormattedText:
return [
("class:in", "In ["),
("class:in.number", f"{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", f"{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 []