| 598 | |
| 599 | |
| 600 | class PythonHistory: |
| 601 | def __init__(self, python_input: PythonInput, original_document: Document) -> None: |
| 602 | """ |
| 603 | Create an `Application` for the history screen. |
| 604 | This has to be run as a sub application of `python_input`. |
| 605 | |
| 606 | When this application runs and returns, it returns the selected lines. |
| 607 | """ |
| 608 | self.python_input = python_input |
| 609 | |
| 610 | history_mapping = HistoryMapping(self, python_input.history, original_document) |
| 611 | self.history_mapping = history_mapping |
| 612 | |
| 613 | document = Document(history_mapping.concatenated_history) |
| 614 | document = Document( |
| 615 | document.text, |
| 616 | cursor_position=document.cursor_position |
| 617 | + document.get_start_of_line_position(), |
| 618 | ) |
| 619 | |
| 620 | def accept_handler(buffer: Buffer) -> bool: |
| 621 | get_app().exit(result=self.default_buffer.text) |
| 622 | return False |
| 623 | |
| 624 | self.history_buffer = Buffer( |
| 625 | document=document, |
| 626 | on_cursor_position_changed=self._history_buffer_pos_changed, |
| 627 | accept_handler=accept_handler, |
| 628 | read_only=True, |
| 629 | ) |
| 630 | |
| 631 | self.default_buffer = Buffer( |
| 632 | name=DEFAULT_BUFFER, |
| 633 | document=history_mapping.get_new_document(), |
| 634 | on_cursor_position_changed=self._default_buffer_pos_changed, |
| 635 | read_only=True, |
| 636 | ) |
| 637 | |
| 638 | self.help_buffer = Buffer(document=Document(HELP_TEXT, 0), read_only=True) |
| 639 | |
| 640 | self.history_layout = HistoryLayout(self) |
| 641 | |
| 642 | self.app: Application[str] = Application( |
| 643 | layout=self.history_layout.layout, |
| 644 | full_screen=True, |
| 645 | style=python_input._current_style, |
| 646 | style_transformation=python_input.style_transformation, |
| 647 | mouse_support=Condition(lambda: python_input.enable_mouse_support), |
| 648 | key_bindings=create_key_bindings(self, python_input, history_mapping), |
| 649 | ) |
| 650 | |
| 651 | def _default_buffer_pos_changed(self, _: Buffer) -> None: |
| 652 | """When the cursor changes in the default buffer. Synchronize with |
| 653 | history buffer.""" |
| 654 | # Only when this buffer has the focus. |
| 655 | if self.app.current_buffer == self.default_buffer: |
| 656 | try: |
| 657 | line_no = ( |