Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions system/CLI/InputOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ public function input(?string $prefix = null): string
// readline() can't be tested.
if ($this->readlineSupport && ENVIRONMENT !== 'testing') {
// @codeCoverageIgnoreStart
// Libedit reports "EditLine wrapper" and mangles the markers, so only GNU readline gets them.
if ($prefix !== null && ! str_contains(readline_info('library_version'), 'EditLine')) {
$prefix = $this->markAnsiNonPrinting($prefix);
$prompt = $this->readlinePrompt($prefix, readline_info('library_version'));

if ($prompt !== null) {
return readline($prompt);
}

return readline($prefix);
// The library cannot render the prompt, so write it ourselves and let readline() only read the line.
self::fwrite(STDOUT, $prefix ?? '');

return readline();
// @codeCoverageIgnoreEnd
}

Expand Down Expand Up @@ -86,6 +90,31 @@ public function fwrite($handle, string $string): void
fwrite($handle, $string);
}

/**
* Builds the prompt handed to readline(), or returns null when the caller has to write
* the prompt to STDOUT itself because the line-editing library cannot render it.
*
* GNU readline gets ANSI sequences wrapped in its non-printing markers so line-redraw
* column accounting skips them. Libedit reports "EditLine wrapper" and mangles the
* markers, so it gets the raw prompt. Windows builds use WinEditLine, which prints ANSI
* sequences literally, and readline_info() omits the version there (php-src guards it
* with #ifndef PHP_WIN32), so they get no prompt.
*
* @param mixed $libraryVersion The value of readline_info('library_version')
*/
private function readlinePrompt(?string $prefix, mixed $libraryVersion): ?string
{
if ($prefix === null || ! is_string($libraryVersion)) {
return null;
}

if (str_contains($libraryVersion, 'EditLine')) {
return $prefix;
}

return $this->markAnsiNonPrinting($prefix);
}

/**
* Wraps ANSI escape sequences in readline's non-printing markers so line-redraw column accounting skips them.
*/
Expand Down
37 changes: 37 additions & 0 deletions tests/system/CLI/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,43 @@ public function testMarkAnsiNonPrintingWrapsEscapeSequences(): void
$this->assertSame('Name: ', $wrap('Name: '));
}

#[DataProvider('provideReadlinePromptDependsOnLibrary')]
public function testReadlinePromptDependsOnLibrary(mixed $libraryVersion, ?string $expected): void
{
$build = $this->getPrivateMethodInvoker(new InputOutput(), 'readlinePrompt');

$this->assertSame(
$expected,
$build(sprintf('What is your favorite color? [%s]: ', CLI::color('red', 'green')), $libraryVersion),
);
}

/**
* @return iterable<string, array{0: mixed, 1: string|null}>
*/
public static function provideReadlinePromptDependsOnLibrary(): iterable
{
yield 'GNU readline gets the prompt with non-printing markers' => [
'8.2',
"What is your favorite color? [\x01\e[0;32m\x02red\x01\e[0m\x02]: ",
];

yield 'libedit gets the raw prompt' => [
'EditLine wrapper',
"What is your favorite color? [\e[0;32mred\e[0m]: ",
];

// Official Windows builds use WinEditLine, which exposes no library version.
yield 'WinEditLine gets no prompt so the caller writes it' => [null, null];
}

public function testReadlinePromptWithoutPrefixReturnsNull(): void
{
$build = $this->getPrivateMethodInvoker(new InputOutput(), 'readlinePrompt');

$this->assertNull($build(null, '8.2'));
}

public function testPromptByKey(): void
{
PhpStreamWrapper::register();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Bugs Fixed

- **CLI:** Fixed a bug where pressing backspace in a ``CLI::prompt()`` erased the prompt text when the ``readline`` extension is enabled. The prompt is now passed to ``readline()`` so line redraws repaint it.
ANSI color codes in the prompt (e.g., option defaults) are wrapped in readline's non-printing markers under GNU readline so cursor positioning stays accurate.
On Windows, where the ``readline`` extension is built on WinEditLine, the prompt is written to STDOUT first because WinEditLine reports no library version and prints ANSI sequences literally.
- **CLIRequest:** Fixed a bug where ``parseCommand()`` could throw a TypeError when ``argv`` is missing.
- **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes).
- **Content Security Policy:** Fixed a bug where empty ``Content-Security-Policy``, ``Content-Security-Policy-Report-Only``, and ``Reporting-Endpoints`` response headers were generated when no corresponding values existed.
Expand Down
Loading