Skip to content

Commit

Permalink
fix(prompt): show correct confirm dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
lt-mayonesa committed Nov 9, 2023
1 parent b92430a commit c36bbb1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
10 changes: 10 additions & 0 deletions hexagon/support/input/prompt/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class InquiryType(Enum):
INT = auto()
FLOAT = auto()
SECRET = auto()
BOOLEAN = auto()


def _determine_expected_inquiry(
Expand Down Expand Up @@ -112,6 +113,8 @@ def _determine_expected_inquiry(
query = InquiryType.ENUM if not searchable else InquiryType.ENUM_SEARCHABLE
elif issubclass(type_, Path):
query = InquiryType.PATH if not searchable else InquiryType.PATH_SEARCHABLE
elif issubclass(type_, bool):
query = InquiryType.BOOLEAN
elif issubclass(type_, int):
query = InquiryType.INT
elif issubclass(type_, float):
Expand Down Expand Up @@ -154,6 +157,7 @@ def query_field(self, model_field: ModelField, model_class, **kwargs):
InquiryType.INT: setup_int,
InquiryType.FLOAT: setup_float,
InquiryType.SECRET: setup_secret,
InquiryType.BOOLEAN: setup_bool,
}

inq, mapper = setups.get(inquiry_type, setup_string)(
Expand Down Expand Up @@ -220,6 +224,8 @@ def confirm(*args, **kwargs):
if not options.hints_disabled:
# not adding confirm_letter and reject_letter as hints because it seems redundant
kwargs["long_instruction"] = HintsBuilder().with_enter_cancel_skip().build()
if "validate" in kwargs:
del kwargs["validate"] # validate is not supported by confirm
return inquirer.confirm(*args, **kwargs).execute()

@staticmethod
Expand Down Expand Up @@ -402,3 +408,7 @@ def setup_secret(self: Prompt, **_) -> (Callable, Callable):

def setup_string(self: Prompt, **_) -> (Callable, Callable):
return self.text, lambda x: x


def setup_bool(self: Prompt, **_) -> (Callable, Callable):
return self.confirm, lambda x: x
39 changes: 38 additions & 1 deletion tests_e2e/__specs/execute_tool_with_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def test_tool_args_class_can_be_used_to_prompt():
os_env_vars={"HEXAGON_THEME": "no_border"},
)
.then_output_should_be(
"input the person's name:", discard_until_first_match=True
["input the person's name:"], discard_until_first_match=True
)
.input("John")
.then_output_should_be(["name: John"], discard_until_first_match=True)
Expand Down Expand Up @@ -590,6 +590,43 @@ def test_should_prompt_fuzzy_file_search():
)


def test_should_prompt_yes_no_confirmation():
(
as_a_user(__file__)
.run_hexagon(
["prompt", "prompt_boolean"],
)
.then_output_should_be(
["Do you want to continue? (Y/n)"],
)
.write("y")
.then_output_should_be(
[
"Do you want to continue? Yes",
"proceed: True",
],
)
.exit()
)
(
as_a_user(__file__)
.run_hexagon(
["prompt", "prompt_boolean"],
)
.then_output_should_be(
["Do you want to continue? (Y/n)"],
)
.write("n")
.then_output_should_be(
[
"Do you want to continue? No",
"proceed: False",
],
)
.exit()
)


def test_should_handle_prompt_on_access_true():
(
as_a_user(__file__)
Expand Down
8 changes: 8 additions & 0 deletions tests_e2e/execute_tool_with_args/python_module_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Args(ToolArgs):
choices=["a sentence to match", "a sentence not to match", "something else"],
)
fuzzy_file_input: OptionalArg[FilePath] = Arg(None, searchable=True, glob="*.txt")
proceed: OptionalArg[bool] = Arg(
None, prompt_default=True, prompt_message="Do you want to continue?"
)

@validator("age")
def validate_age(cls, arg):
Expand Down Expand Up @@ -74,6 +77,7 @@ def main(
"prompt_fuzzy_search": prompt_fuzzy_search,
"prompt_fuzzy_file": prompt_fuzzy_file,
"prompt_multiple_times": prompt_multiple_times,
"prompt_boolean": prompt_boolean,
}

cases.get(cli_args.test.value, default)(cli_args)
Expand Down Expand Up @@ -129,6 +133,10 @@ def prompt_multiple_times(cli_args):
log.result(f"name: {cli_args.name.prompt()}")


def prompt_boolean(cli_args):
log.result(f"proceed: {cli_args.proceed.prompt()}")


def default(cli_args):
log.result(f"test: {cli_args.test.prompt()}")
log.result(f"test: {cli_args.test.prompt()}")
Expand Down

0 comments on commit c36bbb1

Please sign in to comment.