Execute a command and add to history. Args: command: Command to execute Returns: True if successful, False otherwise
(self, command: CommandBase)
| 31 | self._performance_monitor = PerformanceMonitor() |
| 32 | |
| 33 | def execute_command(self, command: CommandBase) -> bool: |
| 34 | """ |
| 35 | Execute a command and add to history. |
| 36 | |
| 37 | Args: |
| 38 | command: Command to execute |
| 39 | |
| 40 | Returns: |
| 41 | True if successful, False otherwise |
| 42 | """ |
| 43 | # Performance monitoring for NFR1 |
| 44 | start_time = time.perf_counter() |
| 45 | |
| 46 | try: |
| 47 | print(f"\n=== COMMAND HISTORY EXECUTE START ===") |
| 48 | print(f"DEBUG: Executing command: {command.get_description()}") |
| 49 | print(f"DEBUG: Command type: {type(command).__name__}") |
| 50 | print(f"DEBUG: Current history size: {len(self.commands)}") |
| 51 | print(f"DEBUG: Current index: {self.current_index}") |
| 52 | |
| 53 | # Execute the command |
| 54 | print(f"DEBUG: Calling command.execute()...") |
| 55 | result = command.execute() |
| 56 | print(f"DEBUG: Command.execute() returned: {result}") |
| 57 | |
| 58 | if not result: |
| 59 | print(f"DEBUG: Command execution failed, not adding to history") |
| 60 | return False |
| 61 | |
| 62 | command._mark_executed() |
| 63 | print(f"DEBUG: Command marked as executed") |
| 64 | |
| 65 | # Remove any commands ahead of current position (redo history) |
| 66 | if self.current_index < len(self.commands) - 1: |
| 67 | removed_commands = self.commands[self.current_index + 1:] |
| 68 | for cmd in removed_commands: |
| 69 | self._memory_usage -= cmd.get_memory_usage() |
| 70 | self.commands = self.commands[:self.current_index + 1] |
| 71 | print(f"DEBUG: Removed {len(removed_commands)} commands from redo history") |
| 72 | |
| 73 | # Add command to history |
| 74 | self.commands.append(command) |
| 75 | self.current_index += 1 |
| 76 | self._memory_usage += command.get_memory_usage() |
| 77 | |
| 78 | print(f"DEBUG: Added command to history at index {self.current_index}") |
| 79 | print(f"DEBUG: History size now: {len(self.commands)}") |
| 80 | |
| 81 | # Maintain depth and memory limits |
| 82 | self._enforce_limits() |
| 83 | |
| 84 | # Performance check |
| 85 | elapsed_ms = (time.perf_counter() - start_time) * 1000 |
| 86 | self._performance_monitor.record_execution(command, elapsed_ms) |
| 87 | |
| 88 | if elapsed_ms > 100: # NFR1 requirement |
| 89 | logger.warning( |
| 90 | f"Command '{command.get_description()}' exceeded 100ms: " |