Undo all executed commands in reverse order.
(self)
| 161 | return True |
| 162 | |
| 163 | def undo(self) -> bool: |
| 164 | """Undo all executed commands in reverse order.""" |
| 165 | if not self._executed: |
| 166 | print(f"DEBUG: CompositeCommand.undo() - not executed, cannot undo") |
| 167 | return False |
| 168 | |
| 169 | print(f"DEBUG: CompositeCommand.undo() - undoing {len(self.executed_commands)} commands") |
| 170 | success_count = 0 |
| 171 | |
| 172 | for i, command in enumerate(reversed(self.executed_commands)): |
| 173 | print(f"DEBUG: Undoing command {i+1}/{len(self.executed_commands)}: {command.get_description()}") |
| 174 | undo_result = command.undo() |
| 175 | print(f"DEBUG: Command {i+1} undo returned: {undo_result}") |
| 176 | |
| 177 | if undo_result: |
| 178 | command._mark_undone() |
| 179 | success_count += 1 |
| 180 | print(f"DEBUG: Command {i+1} undone successfully") |
| 181 | else: |
| 182 | print(f"DEBUG: Command {i+1} undo FAILED") |
| 183 | # Continue with other commands even if one fails |
| 184 | |
| 185 | # Consider composite undo successful if most commands succeeded |
| 186 | # This prevents cascade failures from minor undo issues |
| 187 | success_ratio = success_count / len(self.executed_commands) if self.executed_commands else 1.0 |
| 188 | overall_success = success_ratio >= 0.5 # At least 50% must succeed |
| 189 | |
| 190 | if overall_success: |
| 191 | self._mark_undone() |
| 192 | if success_count == len(self.executed_commands): |
| 193 | print(f"DEBUG: All commands undone successfully, composite marked as undone") |
| 194 | else: |
| 195 | print(f"DEBUG: {success_count}/{len(self.executed_commands)} commands undone successfully, composite marked as undone") |
| 196 | else: |
| 197 | print(f"DEBUG: Only {success_count}/{len(self.executed_commands)} commands undone, composite undo failed") |
| 198 | |
| 199 | print(f"DEBUG: CompositeCommand.undo() returning: {overall_success}") |
| 200 | return overall_success |
| 201 | |
| 202 | def get_memory_usage(self) -> int: |
| 203 | """Calculate total memory usage of all contained commands.""" |