screen = new ScreenManager($output); // Custom TUI state with debug $debugState = new class extends TuiState { public function setSelectedCategory(int $index): void { $oldIndex = $this->getSelectedCategory(); parent::setSelectedCategory($index); $newIndex = $this->getSelectedCategory(); echo "\n[DEBUG] Category changed: $oldIndex โ†’ $newIndex\n"; } public function navigateUp(): void { echo "\n[DEBUG] navigateUp() called\n"; $before = $this->getSelectedCategory(); parent::navigateUp(); $after = $this->getSelectedCategory(); echo "[DEBUG] navigateUp result: $before โ†’ $after\n"; } public function navigateDown(): void { echo "\n[DEBUG] navigateDown() called\n"; $before = $this->getSelectedCategory(); parent::navigateDown(); $after = $this->getSelectedCategory(); echo "[DEBUG] navigateDown result: $before โ†’ $after\n"; } }; // Custom input handler with debug $debugInputHandler = new class($debugExecutor) extends TuiInputHandler { public function handleInput(string $key, \App\Framework\Console\Components\TuiState $state, \App\Framework\Console\CommandHistory $history): void { $keyHex = bin2hex($key); $keyDesc = match($key) { "\033[A" => "ARROW_UP", "\033[B" => "ARROW_DOWN", "\033[C" => "ARROW_RIGHT", "\033[D" => "ARROW_LEFT", "\n" => "ENTER", " " => "SPACE", "\033" => "ESC", 'q' => "Q", default => "OTHER($key)" }; echo "\n[DEBUG] INPUT: '$keyDesc' (hex: $keyHex) in view: " . $state->getCurrentView()->name . "\n"; parent::handleInput($key, $state, $history); echo "[DEBUG] After input - Selected: " . $state->getSelectedCategory() . "\n"; } }; $state = $debugState; $history = new CommandHistory(); $inputHandler = $debugInputHandler; $renderer = new TuiRenderer($output); $groupRegistry = new CommandGroupRegistry($discoveryRegistry); // Add test categories $testCategories = [ ['name' => 'Testing', 'description' => 'Test commands', 'icon' => '๐Ÿงช', 'commands' => []], ['name' => 'Demo', 'description' => 'Demo commands', 'icon' => '๐ŸŽฎ', 'commands' => []], ['name' => 'Generator', 'description' => 'Code generation', 'icon' => 'โš™๏ธ', 'commands' => []], ['name' => 'General', 'description' => 'General commands', 'icon' => '๐Ÿ“‚', 'commands' => []] ]; $state->setCategories($testCategories); $state->setCurrentView(TuiView::CATEGORIES); $state->setRunning(true); echo "โœ“ Debug TUI components ready\n"; echo "Categories: " . count($testCategories) . "\n"; echo "Selected: " . $state->getSelectedCategory() . "\n\n"; // Save terminal settings $originalSettings = trim(shell_exec('stty -g') ?: ''); // Set up terminal if ($isPhpStorm) { shell_exec('stty raw -echo min 1 time 0 2>/dev/null'); } else { shell_exec('stty -icanon -echo 2>/dev/null'); } // Hide cursor echo CursorControlCode::HIDE->format(); echo "๐Ÿš€ LIVE DEBUG TUI STARTED\n"; echo "Use arrow keys to test navigation.\n"; echo "Press 'q' to quit.\n"; echo "All navigation events will be logged below.\n\n"; // Simple TUI loop with debug while ($state->isRunning()) { // Clear and render echo ScreenControlCode::CLEAR_ALL->format(); echo CursorControlCode::POSITION->format(1, 1); // Render current state $renderer->render($state, $history); // Show debug info at bottom echo "\n" . str_repeat('=', 60) . "\n"; echo "DEBUG INFO:\n"; echo "Selected Category: " . $state->getSelectedCategory() . "\n"; echo "Current View: " . $state->getCurrentView()->name . "\n"; $category = $state->getCurrentCategory(); echo "Category Name: " . ($category ? $category['name'] : 'NULL') . "\n"; echo "Press arrow keys to test navigation...\n"; // Read input with PHPStorm-compatible method $key = fgetc(STDIN); if ($key === false) continue; if ($key === "\033") { $sequence = $key; stream_set_blocking(STDIN, false); $next = fgetc(STDIN); if ($next === false) { usleep(10000); $next = fgetc(STDIN); } if ($next !== false) { $sequence .= $next; if ($next === '[') { $third = fgetc(STDIN); if ($third === false) { usleep(10000); $third = fgetc(STDIN); } if ($third !== false) { $sequence .= $third; } } } stream_set_blocking(STDIN, true); $key = $sequence; } if ($key === 'q' || $key === 'Q') { $state->setRunning(false); break; } // Process input $inputHandler->handleInput($key, $state, $history); } } finally { // Restore terminal echo CursorControlCode::SHOW->format(); if (!empty($originalSettings)) { shell_exec("stty $originalSettings 2>/dev/null"); } echo "\nโœ“ Debug TUI session ended\n"; }