Skip to content

Update Input Dialog to use Validator #1747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/prompt_toolkit/filters/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"has_selection",
"has_suggestion",
"has_validation_error",
"has_any_validation_error",
"is_done",
"is_read_only",
"is_multiline",
Expand Down Expand Up @@ -170,6 +171,33 @@ def has_validation_error() -> bool:
return get_app().current_buffer.validation_error is not None


@Condition
def has_any_validation_error() -> bool:
"Any buffer in the layout has validation error."
from prompt_toolkit.layout.containers import Window
from prompt_toolkit.layout.controls import (
BufferControl,
SearchBufferControl,
UIControl,
)

# Extract buffer validation errors for buffer UIControl children classes
def get_buffer_from_content(content: UIControl) -> bool:
Copy link
Member

@jonathanslenders jonathanslenders Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you rename this? This doesn't return the buffer. For instance control_has_validation_error().
This PR looks good otherwise.

if isinstance(content, (BufferControl, SearchBufferControl)):
return content.buffer.validation_error is not None
else:
return False

# Get all windows for the current layout
windows = get_app().layout.find_all_windows()
# Iterate over each window in the layout and return true if any of the buffers have validation errors
for window in windows:
if isinstance(window, Window):
if get_buffer_from_content(window.content):
return True
return False


@Condition
def has_arg() -> bool:
"Enable when the input processor has an 'arg'."
Expand Down
8 changes: 6 additions & 2 deletions src/prompt_toolkit/shortcuts/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def input_dialog(
cancel_text: str = "Cancel",
completer: Completer | None = None,
validator: Validator | None = None,
validate_while_typing: FilterOrBool = False,
password: FilterOrBool = False,
style: BaseStyle | None = None,
default: str = "",
Expand All @@ -124,7 +125,9 @@ def accept(buf: Buffer) -> bool:
return True # Keep text.

def ok_handler() -> None:
get_app().exit(result=textfield.text)
textfield.buffer.validate() # validate one final time before exiting
if textfield.buffer.validation_error is None:
get_app().exit(result=textfield.text)

ok_button = Button(text=ok_text, handler=ok_handler)
cancel_button = Button(text=cancel_text, handler=_return_none)
Expand All @@ -135,6 +138,7 @@ def ok_handler() -> None:
password=password,
completer=completer,
validator=validator,
validate_while_typing=validate_while_typing,
accept_handler=accept,
)

Expand All @@ -144,7 +148,7 @@ def ok_handler() -> None:
[
Label(text=text, dont_extend_height=True),
textfield,
ValidationToolbar(),
ValidationToolbar(buffer=textfield.buffer),
],
padding=D(preferred=1, max=1),
),
Expand Down
5 changes: 5 additions & 0 deletions src/prompt_toolkit/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ def __init__(
completer: Completer | None = None,
complete_while_typing: FilterOrBool = True,
validator: Validator | None = None,
validate_while_typing: FilterOrBool = False,
accept_handler: BufferAcceptHandler | None = None,
history: History | None = None,
focusable: FilterOrBool = True,
Expand Down Expand Up @@ -215,6 +216,7 @@ def __init__(
self.read_only = read_only
self.wrap_lines = wrap_lines
self.validator = validator
self.validate_while_typing = validate_while_typing

self.buffer = Buffer(
document=Document(text, 0),
Expand All @@ -225,6 +227,9 @@ def __init__(
lambda: is_true(self.complete_while_typing)
),
validator=DynamicValidator(lambda: self.validator),
validate_while_typing=Condition(
lambda: is_true(self.validate_while_typing)
),
auto_suggest=DynamicAutoSuggest(lambda: self.auto_suggest),
accept_handler=accept_handler,
history=history,
Expand Down
23 changes: 14 additions & 9 deletions src/prompt_toolkit/widgets/toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
Condition,
FilterOrBool,
emacs_mode,
has_any_validation_error,
has_arg,
has_completions,
has_focus,
has_validation_error,
to_filter,
vi_mode,
vi_navigation_mode,
Expand Down Expand Up @@ -342,16 +342,21 @@ def __pt_container__(self) -> Container:


class ValidationToolbar:
def __init__(self, show_position: bool = False) -> None:
def __init__(
self, show_position: bool = False, buffer: Buffer | None = None
) -> None:
def get_formatted_text() -> StyleAndTextTuples:
buff = get_app().current_buffer

# If buffer not specified, use the currently focused buffer
if buffer is None:
buff = get_app().current_buffer
else:
buff = buffer
if buff.validation_error:
row, column = buff.document.translate_index_to_position(
buff.validation_error.cursor_position
)

if show_position:
row, column = buff.document.translate_index_to_position(
buff.validation_error.cursor_position
)

text = "{} (line={} column={})".format(
buff.validation_error.message,
row + 1,
Expand All @@ -367,7 +372,7 @@ def get_formatted_text() -> StyleAndTextTuples:
self.control = FormattedTextControl(get_formatted_text)

self.container = ConditionalContainer(
content=Window(self.control, height=1), filter=has_validation_error
content=Window(self.control, height=1), filter=has_any_validation_error
)

def __pt_container__(self) -> Container:
Expand Down