Skip to content

Commit 6cfc3f3

Browse files
Reset the buffer, unless the accept_handler returns True (keep_text).
This is a backwards incompatible change.
1 parent adb6bf5 commit 6cfc3f3

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

prompt_toolkit/buffer.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ class Buffer(object):
134134
current input line and implements all text manipulations on top of it. It
135135
also implements the history, undo stack and the completion state.
136136
137-
:param eventloop: :class:`~prompt_toolkit.eventloop.EventLoop` instance.
138137
:param completer: :class:`~prompt_toolkit.completion.Completer` instance.
139138
:param history: :class:`~prompt_toolkit.history.History` instance.
140139
:param tempfile_suffix: The tempfile suffix (extension) to be used for the
@@ -144,8 +143,14 @@ class Buffer(object):
144143
:param name: Name for this buffer. E.g. DEFAULT_BUFFER. This is mostly
145144
useful for key bindings where we sometimes prefer to refer to a buffer
146145
by their name instead of by reference.
147-
:param accept_handler: Callback that takes this buffer as input. Called when
148-
the buffer input is accepted. (Usually when the user presses `enter`.)
146+
:param accept_handler: Called when the buffer input is accepted. (Usually
147+
when the user presses `enter`.) The accept handler receives this
148+
`Buffer` as input and should return True when the buffer text should be
149+
kept instead of calling reset.
150+
151+
In case of a `PromptSession` for instance, we want to keep the text,
152+
because we will exit the application, and only reset it during the next
153+
run.
149154
150155
Events:
151156
@@ -1608,10 +1613,16 @@ def validate_and_handle(self):
16081613

16091614
# When the validation succeeded, accept the input.
16101615
if valid:
1611-
if self.accept_handler:
1612-
self.accept_handler(self)
16131616
self.append_to_history()
16141617

1618+
if self.accept_handler:
1619+
keep_text = self.accept_handler(self)
1620+
else:
1621+
keep_text = False
1622+
1623+
if not keep_text:
1624+
self.reset()
1625+
16151626

16161627
def _only_one_at_a_time(coroutine):
16171628
"""

prompt_toolkit/shortcuts/prompt.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ def accept(buff):
338338
""" Accept the content of the default buffer. This is called when
339339
the validation succeeds. """
340340
self.app.exit(result=buff.document.text)
341+
return True # Keep text, we call 'reset' later on.
341342

342343
return Buffer(
343344
name=DEFAULT_BUFFER,

0 commit comments

Comments
 (0)