| 523 | |
| 524 | |
| 525 | class URWIDInteraction(repl.Interaction): |
| 526 | def __init__(self, config, statusbar, frame): |
| 527 | super().__init__(config) |
| 528 | self.statusbar = statusbar |
| 529 | self.frame = frame |
| 530 | urwid.connect_signal(statusbar, "prompt_result", self._prompt_result) |
| 531 | self.callback = None |
| 532 | |
| 533 | def confirm(self, q, callback): |
| 534 | """Ask for yes or no and call callback to return the result""" |
| 535 | |
| 536 | def callback_wrapper(result): |
| 537 | callback(result.lower() in (_("y"), _("yes"))) |
| 538 | |
| 539 | self.prompt(q, callback_wrapper, single=True) |
| 540 | |
| 541 | def notify(self, s, n=10, wait_for_keypress=False): |
| 542 | return self.statusbar.message(s, n) |
| 543 | |
| 544 | def prompt(self, s, callback=None, single=False): |
| 545 | """Prompt the user for input. The result will be returned via calling |
| 546 | callback. Note that there can only be one prompt active. But the |
| 547 | callback can already start a new prompt.""" |
| 548 | |
| 549 | if self.callback is not None: |
| 550 | raise Exception("Prompt already in progress") |
| 551 | |
| 552 | self.callback = callback |
| 553 | self.statusbar.prompt(s, single=single) |
| 554 | self.frame.set_focus("footer") |
| 555 | |
| 556 | def _prompt_result(self, text): |
| 557 | self.frame.set_focus("body") |
| 558 | if self.callback is not None: |
| 559 | # The callback might want to start another prompt, so reset it |
| 560 | # before calling the callback. |
| 561 | callback = self.callback |
| 562 | self.callback = None |
| 563 | callback(text) |
| 564 | |
| 565 | def file_prompt(self, s: str) -> str | None: |
| 566 | raise NotImplementedError |
| 567 | |
| 568 | |
| 569 | class URWIDRepl(repl.Repl): |