Skip to content

feat: add arrow-key select() and multiselect() to Prompt - #10659

Merged
paulbalandan merged 1 commit into
codeigniter4:4.8from
paulbalandan:cli-prompt-select
Oct 27, 2026
Merged

paulbalandan merged 1 commit into
codeigniter4:4.8from
paulbalandan:cli-prompt-select

Conversation

@paulbalandan

Copy link
Copy Markdown
Member

Description
Follow-up to #10655. Adds Prompt::select(), which returns the chosen key, and Prompt::multiselect(), which returns the ticked options and can require at least one.

  • On a terminal: arrow keys to move, space to tick, a to toggle all, enter to confirm. Long lists scroll.
  • Elsewhere (Windows, piped input), the options are listed by number and a number is typed instead.
  • A quiet output returns the defaults. Ctrl+C restores the terminal and re-raises SIGINT.
select.mp4
Demo script (run from the framework root)
<?php

declare(strict_types=1);

use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Exceptions\CLIException;
use CodeIgniter\CLI\Verbosity;

require getcwd() . '/system/util_bootstrap.php';

CLI::init();

if (in_array('-q', $argv, true)) {
    CLI::getOutput()->setVerbosity(Verbosity::Quiet);
}

$prompt = CLI::getPrompt();

$agents = [
    'claude-code' => 'Claude Code',
    'cursor'      => 'Cursor',
    'copilot'     => 'VS Code Copilot',
    'codex'       => 'Codex CLI',
    'gemini'      => 'Gemini CLI',
    'windsurf'    => 'Windsurf',
    'zed'         => 'Zed',
];

$favourite = $prompt->select('Which agent do you use most?', $agents, 'cursor');
$used      = $prompt->multiselect('Which agents do you use?', $agents, ['claude-code', 'cursor']);

try {
    $features = $prompt->multiselect('Which features should be set up?', ['tools' => 'Tools', 'resources' => 'Resources', 'prompts' => 'Prompts'], required: true);
} catch (CLIException $e) {
    $features = $e->getMessage();
}

fwrite(STDOUT, var_export(['select' => $favourite, 'multiselect' => $used, 'required' => $features], true) . PHP_EOL);

Add -q to see the defaults returned, or pipe the answers to see the numbered fallback: printf '\n\n1,3\n' | php prompt-demo.php.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@carson-codeigniter4 carson-codeigniter4 Bot added the 4.8 PRs that target the `4.8` branch. label Oct 25, 2026
@paulbalandan paulbalandan added the new feature PRs for new features label Oct 25, 2026
@michalsn
michalsn requested a balanced review from Copilot October 26, 2026 11:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved input handling and redraw defects can cause incorrect selections, bypass required input, or hang automated commands.

5 open issues
What changed in this PR

Extends the Prompt class introduced in #10655 with interactive single- and multiple-choice menus for the next minor release.

Changes:

  • Adds keyboard navigation, scrolling, defaults, and required selections.
  • Adds terminal key reading, numbered-input fallbacks, and Ctrl+C cleanup.
  • Adds tests, usage examples, and release documentation.

Validation was limited to targeted code inspection; runtime tests and documentation validation remain for CI.

File Description
system/​CLI/​KeyReader.php Reads keypresses and manages terminal mode.
system/​CLI/​Prompt.php Implements interactive menus and numbered fallbacks.
tests/​system/​CLI/​PromptTest.php Covers navigation, defaults, rendering, and fallback behavior.
user_guide_src/​source/​cli/​cli_library/​026.php Demonstrates both selection methods.
user_guide_src/​source/​cli/​cli_library.rst Documents APIs and terminal behavior.
user_guide_src/​source/​changelogs/​v4.8.0.rst Records the new prompt features.

Review effort: Balanced

Comment thread system/CLI/Prompt.php
Comment thread system/CLI/KeyReader.php Outdated
Comment thread system/CLI/Prompt.php Outdated
Comment thread system/CLI/Prompt.php Outdated
Comment thread system/CLI/Prompt.php Outdated

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice feature! Also, it looks great.

@paulbalandan
paulbalandan merged commit 2c5057b into codeigniter4:4.8 Oct 27, 2026
59 checks passed
@paulbalandan
paulbalandan deleted the cli-prompt-select branch October 27, 2026 09:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch. new feature PRs for new features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants