Skip to content

Commit 7346416

Browse files
Fixed python-embed-with-custom-prompt example.
1 parent 75dceaa commit 7346416

5 files changed

Lines changed: 30 additions & 44 deletions

File tree

examples/python-embed-with-custom-prompt.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,32 @@
55
from __future__ import unicode_literals
66

77
from pygments.token import Token
8+
from prompt_toolkit.formatted_text import HTML
89

910
from ptpython.prompt_style import PromptStyle
1011
from ptpython.repl import embed
1112

1213

1314
def configure(repl):
14-
# There are several ways to override the prompt.
15-
16-
# 1. Probably, the best is to add a new PromptStyle to `all_prompt_styles`
17-
# and activate it. This way, the other styles are still selectable from
18-
# the menu.
15+
# Probably, the best is to add a new PromptStyle to `all_prompt_styles` and
16+
# activate it. This way, the other styles are still selectable from the
17+
# menu.
1918
class CustomPrompt(PromptStyle):
20-
def in_tokens(self, cli):
21-
return [
22-
(Token.In, "Input["),
23-
(Token.In.Number, "%s" % repl.current_statement_index),
24-
(Token.In, "] >>: "),
25-
]
26-
27-
def in2_tokens(self, cli, width):
28-
return [(Token.In, "...: ".rjust(width))]
29-
30-
def out_tokens(self, cli):
31-
return [
32-
(Token.Out, "Result["),
33-
(Token.Out.Number, "%s" % repl.current_statement_index),
34-
(Token.Out, "]: "),
35-
]
36-
37-
repl.all_prompt_styles["custom"] = CustomPrompt()
38-
repl.prompt_style = "custom"
19+
def in_prompt(self):
20+
return HTML("<ansigreen>Input[%s]</ansigreen>: ") % (
21+
repl.current_statement_index,
22+
)
3923

40-
# 2. Assign a new callable to `get_input_prompt_tokens`. This will always take effect.
41-
## repl.get_input_prompt_tokens = lambda cli: [(Token.In, '[hello] >>> ')]
24+
def in2_prompt(self, width):
25+
return "...: ".rjust(width)
4226

43-
# 3. Also replace `get_input_prompt_tokens`, but still call the original. This inserts
44-
# a prefix.
27+
def out_prompt(self):
28+
return HTML("<ansired>Result[%s]</ansired>: ") % (
29+
repl.current_statement_index,
30+
)
4531

46-
## original = repl.get_input_prompt_tokens
47-
## repl.get_input_prompt_tokens = lambda cli: [(Token.In, '[prefix]')] + original(cli)
32+
repl.all_prompt_styles["custom"] = CustomPrompt()
33+
repl.prompt_style = "custom"
4834

4935

5036
def main():

ptpython/completer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def get_completions(
129129
for c in self.dictionary_completer.get_completions(
130130
document, complete_event
131131
):
132-
if c.text not in '[.':
132+
if c.text not in "[.":
133133
# If we get the [ or . completion, still include the other
134134
# completions.
135135
has_dict_completions = True

ptpython/prompt_style.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from abc import ABCMeta, abstractmethod
22
from typing import TYPE_CHECKING
33

4-
from prompt_toolkit.formatted_text import StyleAndTextTuples
4+
from prompt_toolkit.formatted_text import AnyFormattedText
55

66
if TYPE_CHECKING:
77
from .python_input import PythonInput
@@ -15,12 +15,12 @@ class PromptStyle(metaclass=ABCMeta):
1515
"""
1616

1717
@abstractmethod
18-
def in_prompt(self) -> StyleAndTextTuples:
18+
def in_prompt(self) -> AnyFormattedText:
1919
" Return the input tokens. "
2020
return []
2121

2222
@abstractmethod
23-
def in2_prompt(self, width: int) -> StyleAndTextTuples:
23+
def in2_prompt(self, width: int) -> AnyFormattedText:
2424
"""
2525
Tokens for every following input line.
2626
@@ -30,7 +30,7 @@ def in2_prompt(self, width: int) -> StyleAndTextTuples:
3030
return []
3131

3232
@abstractmethod
33-
def out_prompt(self) -> StyleAndTextTuples:
33+
def out_prompt(self) -> AnyFormattedText:
3434
" Return the output tokens. "
3535
return []
3636

@@ -43,17 +43,17 @@ class IPythonPrompt(PromptStyle):
4343
def __init__(self, python_input: "PythonInput") -> None:
4444
self.python_input = python_input
4545

46-
def in_prompt(self) -> StyleAndTextTuples:
46+
def in_prompt(self) -> AnyFormattedText:
4747
return [
4848
("class:in", "In ["),
4949
("class:in.number", "%s" % self.python_input.current_statement_index),
5050
("class:in", "]: "),
5151
]
5252

53-
def in2_prompt(self, width: int) -> StyleAndTextTuples:
53+
def in2_prompt(self, width: int) -> AnyFormattedText:
5454
return [("class:in", "...: ".rjust(width))]
5555

56-
def out_prompt(self) -> StyleAndTextTuples:
56+
def out_prompt(self) -> AnyFormattedText:
5757
return [
5858
("class:out", "Out["),
5959
("class:out.number", "%s" % self.python_input.current_statement_index),
@@ -67,11 +67,11 @@ class ClassicPrompt(PromptStyle):
6767
The classic Python prompt.
6868
"""
6969

70-
def in_prompt(self) -> StyleAndTextTuples:
70+
def in_prompt(self) -> AnyFormattedText:
7171
return [("class:prompt", ">>> ")]
7272

73-
def in2_prompt(self, width: int) -> StyleAndTextTuples:
73+
def in2_prompt(self, width: int) -> AnyFormattedText:
7474
return [("class:prompt.dots", "...")]
7575

76-
def out_prompt(self) -> StyleAndTextTuples:
76+
def out_prompt(self) -> AnyFormattedText:
7777
return []

ptpython/python_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def __init__(
205205
self.extra_key_bindings = extra_key_bindings or KeyBindings()
206206

207207
# Settings.
208-
self.title: AnyFormattedText = ''
208+
self.title: AnyFormattedText = ""
209209
self.show_signature: bool = False
210210
self.show_docstring: bool = False
211211
self.show_meta_enter_message: bool = True

ptpython/repl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
PygmentsTokens,
2222
merge_formatted_text,
2323
)
24-
from prompt_toolkit.formatted_text.utils import fragment_list_width
24+
from prompt_toolkit.formatted_text import fragment_list_width, to_formatted_text
2525
from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context
2626
from prompt_toolkit.shortcuts import clear_title, print_formatted_text, set_title
2727
from prompt_toolkit.utils import DummyContext
@@ -152,7 +152,7 @@ def compile_with_flags(code: str, mode: str):
152152
locals["_"] = locals["_%i" % self.current_statement_index] = result
153153

154154
if result is not None:
155-
out_prompt = self.get_output_prompt()
155+
out_prompt = to_formatted_text(self.get_output_prompt())
156156

157157
try:
158158
result_str = "%r\n" % (result,)

0 commit comments

Comments
 (0)