|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import abc |
| 3 | +import typing as t |
| 4 | + |
| 5 | + |
| 6 | +class Command: |
| 7 | + def __init__(self, can_undo: bool, name: str) -> None: |
| 8 | + self.can_undo: bool = can_undo |
| 9 | + self.name: str = name |
| 10 | + |
| 11 | + @abc.abstractmethod |
| 12 | + def do(self) -> bool: |
| 13 | + ... |
| 14 | + |
| 15 | + @abc.abstractmethod |
| 16 | + def undo(self) -> bool: |
| 17 | + ... |
| 18 | + |
| 19 | + |
| 20 | +class CommandProcessor: |
| 21 | + def __init__(self, max_commands: int) -> None: |
| 22 | + self._max_commands = max_commands |
| 23 | + |
| 24 | + self._history: t.List[Command] = [] |
| 25 | + self._current_command_idx: t.Optional[int] = None |
| 26 | + |
| 27 | + def can_undo(self) -> bool: |
| 28 | + """ |
| 29 | + Returns true if the currently-active command can be undone, false |
| 30 | + otherwise. |
| 31 | + """ |
| 32 | + current_command = self.get_current_command() |
| 33 | + if current_command is None: |
| 34 | + return False |
| 35 | + else: |
| 36 | + return current_command.can_undo |
| 37 | + |
| 38 | + def can_redo(self) -> bool: |
| 39 | + """ |
| 40 | + Returns true if the currently-active command can be redone, false |
| 41 | + otherwise. |
| 42 | + """ |
| 43 | + next_command = self.get_next_command() |
| 44 | + if next_command is None: |
| 45 | + return False |
| 46 | + else: |
| 47 | + return True |
| 48 | + |
| 49 | + def redo(self) -> bool: |
| 50 | + """ |
| 51 | + Executes (redoes) the current command (the command that has just been |
| 52 | + undone if any). |
| 53 | + """ |
| 54 | + next_command = self.get_next_command() |
| 55 | + |
| 56 | + # no command to redo in the history |
| 57 | + if next_command is None: |
| 58 | + return False |
| 59 | + |
| 60 | + if next_command.do() is False: |
| 61 | + return False |
| 62 | + |
| 63 | + self._increment_current_command() |
| 64 | + |
| 65 | + return True |
| 66 | + |
| 67 | + def undo(self) -> bool: |
| 68 | + """ |
| 69 | + Undoes the last command executed. |
| 70 | + """ |
| 71 | + current_command = self.get_current_command() |
| 72 | + |
| 73 | + # no command available |
| 74 | + if current_command is None: |
| 75 | + return False |
| 76 | + |
| 77 | + # command cant be undone |
| 78 | + if not current_command.can_undo: |
| 79 | + return False |
| 80 | + |
| 81 | + # error on undo |
| 82 | + if current_command.undo() is False: |
| 83 | + return False |
| 84 | + |
| 85 | + # set current command to previous command |
| 86 | + self._decrement_current_command() |
| 87 | + |
| 88 | + return True |
| 89 | + |
| 90 | + def submit(self, command: Command) -> None: |
| 91 | + """ |
| 92 | + Submits a new command to the command processor. |
| 93 | +
|
| 94 | + The command processor calls Command.do to execute the command; if it |
| 95 | + succeeds, the command is stored in the history list, and the associated |
| 96 | + edit menu (if any) updated appropriately. If it fails, the command is |
| 97 | + deleted immediately. |
| 98 | + """ |
| 99 | + command.do() |
| 100 | + self.store(command) |
| 101 | + |
| 102 | + def store(self, command: Command) -> None: |
| 103 | + """ |
| 104 | + Just store the command without executing it. |
| 105 | +
|
| 106 | + Any command that has been undone will be chopped off the history list. |
| 107 | + """ |
| 108 | + # We must chop off the current 'branch', so that |
| 109 | + # we're at the end of the command list. |
| 110 | + if self._current_command_idx is None: |
| 111 | + self.clear_commands() |
| 112 | + else: |
| 113 | + self._history = self._history[: self._current_command_idx + 1] |
| 114 | + |
| 115 | + # Limit history length. Remove fist commands from history |
| 116 | + # if an overflow occures |
| 117 | + if len(self._history) >= self._max_commands: |
| 118 | + if self._current_command_idx is None: |
| 119 | + raise ValueError("history and current_command_idx are out of sync") |
| 120 | + self._history.pop(0) |
| 121 | + self._current_command_idx = self._current_command_idx - 1 |
| 122 | + |
| 123 | + # append command to history |
| 124 | + self._current_command_idx = len(self._history) |
| 125 | + self._history.append(command) |
| 126 | + |
| 127 | + def clear_commands(self): |
| 128 | + """ |
| 129 | + Deletes all commands in the list and sets the current command pointer to None. |
| 130 | + """ |
| 131 | + self._history.clear() |
| 132 | + self._current_command_idx = None |
| 133 | + |
| 134 | + def get_current_command(self) -> t.Optional[Command]: |
| 135 | + """ |
| 136 | + Returns the current command. |
| 137 | + """ |
| 138 | + if self._current_command_idx is None: |
| 139 | + return None |
| 140 | + |
| 141 | + return self._history[self._current_command_idx] |
| 142 | + |
| 143 | + def get_next_command(self) -> t.Optional[Command]: |
| 144 | + """ |
| 145 | + Returns the next command. |
| 146 | + """ |
| 147 | + if self._current_command_idx is None: |
| 148 | + next_command_idx = 0 |
| 149 | + else: |
| 150 | + next_command_idx = self._current_command_idx + 1 |
| 151 | + |
| 152 | + if next_command_idx >= len(self._history): |
| 153 | + return None |
| 154 | + |
| 155 | + return self._history[next_command_idx] |
| 156 | + |
| 157 | + def _decrement_current_command(self) -> None: |
| 158 | + if self._current_command_idx is None: |
| 159 | + return |
| 160 | + if self._current_command_idx > 0: |
| 161 | + self._current_command_idx -= 1 |
| 162 | + else: |
| 163 | + self._current_command_idx = None |
| 164 | + |
| 165 | + def _increment_current_command(self) -> None: |
| 166 | + if self._current_command_idx is None: |
| 167 | + next_command_idx = 0 |
| 168 | + else: |
| 169 | + next_command_idx = self._current_command_idx + 1 |
| 170 | + |
| 171 | + if next_command_idx >= len(self._history): |
| 172 | + return |
| 173 | + |
| 174 | + self._current_command_idx = next_command_idx |
0 commit comments