Skip to content

Commit c29090b

Browse files
committed
fix: pass prompt text to readline in CLI::prompt() so backspace does not erase it
1 parent a826e69 commit c29090b

4 files changed

Lines changed: 120 additions & 5 deletions

File tree

‎system/CLI/CLI.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,12 @@ public static function prompt(string $field, $options = null, $validation = null
254254
$default = $options[0];
255255
}
256256

257-
static::fwrite(STDOUT, $field . (trim($field) !== '' ? ' ' : '') . $extraOutput . ': ');
258257
static::$lastWrite = 'write';
259258

260-
// Read the input from keyboard.
261-
$input = trim(static::$io->input());
262-
$input = ($input === '') ? (string) $default : $input;
259+
// The reader renders the prompt itself, so readline redraws repaint it instead of erasing it.
260+
$prompt = sprintf('%s%s%s: ', $field, trim($field) !== '' ? ' ' : '', $extraOutput);
261+
$input = trim(static::$io->input($prompt));
262+
$input = $input === '' ? (string) $default : $input;
263263

264264
if ($validation !== []) {
265265
while (! static::validate('"' . trim($field) . '"', $input, $validation)) {

‎system/CLI/InputOutput.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function input(?string $prefix = null): string
4646
return readline($prefix); // @codeCoverageIgnore
4747
}
4848

49-
echo $prefix;
49+
// self:: skips MockInputOutput's fwrite override, whose filter bookkeeping must not nest inside input().
50+
self::fwrite(STDOUT, $prefix ?? '');
5051

5152
$input = fgets(fopen('php://stdin', 'rb'));
5253

‎tests/system/CLI/CLITest.php‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Exceptions\RuntimeException;
1818
use CodeIgniter\Superglobals;
1919
use CodeIgniter\Test\CIUnitTestCase;
20+
use CodeIgniter\Test\Mock\MockInputOutput;
2021
use CodeIgniter\Test\PhpStreamWrapper;
2122
use CodeIgniter\Test\StreamFilterTrait;
2223
use PHPUnit\Framework\Attributes\DataProvider;
@@ -143,6 +144,118 @@ public function testPromptInputZero(): void
143144
$this->assertSame('0', $output);
144145
}
145146

147+
public function testPromptPassesPromptTextToInputReader(): void
148+
{
149+
$io = new class () extends InputOutput {
150+
public ?string $receivedPrefix = null;
151+
152+
public function input(?string $prefix = null): string
153+
{
154+
$this->receivedPrefix = $prefix;
155+
156+
return 'red';
157+
}
158+
};
159+
CLI::setInputOutput($io);
160+
161+
$output = CLI::prompt('What is your favorite color?');
162+
163+
CLI::resetInputOutput();
164+
165+
$this->assertSame('red', $output);
166+
$this->assertSame('What is your favorite color? : ', $io->receivedPrefix);
167+
}
168+
169+
public function testPromptPassesDefaultOptionInPromptText(): void
170+
{
171+
$io = new class () extends InputOutput {
172+
public ?string $receivedPrefix = null;
173+
174+
public function input(?string $prefix = null): string
175+
{
176+
$this->receivedPrefix = $prefix;
177+
178+
return '';
179+
}
180+
};
181+
CLI::setInputOutput($io);
182+
183+
$output = CLI::prompt('What is your favorite color?', 'red');
184+
185+
CLI::resetInputOutput();
186+
187+
$this->assertSame('red', $output);
188+
$this->assertSame(
189+
sprintf('What is your favorite color? [%s]: ', CLI::color('red', 'green')),
190+
$io->receivedPrefix,
191+
);
192+
}
193+
194+
public function testPromptByKeyPassesPromptTextToInputReader(): void
195+
{
196+
$io = new class () extends InputOutput {
197+
public ?string $receivedPrefix = null;
198+
199+
public function input(?string $prefix = null): string
200+
{
201+
$this->receivedPrefix = $prefix;
202+
203+
return '1';
204+
}
205+
};
206+
CLI::setInputOutput($io);
207+
208+
$output = CLI::promptByKey('Select your hobbies:', ['Playing game', 'Sleep', 'Badminton']);
209+
210+
CLI::resetInputOutput();
211+
212+
$this->assertSame('1', $output);
213+
$this->assertSame(
214+
PHP_EOL . sprintf('[%s, 1, 2]: ', CLI::color('0', 'green')),
215+
$io->receivedPrefix,
216+
);
217+
}
218+
219+
public function testPromptByMultipleKeysPassesPromptTextToInputReader(): void
220+
{
221+
$io = new class () extends InputOutput {
222+
public ?string $receivedPrefix = null;
223+
224+
public function input(?string $prefix = null): string
225+
{
226+
$this->receivedPrefix = $prefix;
227+
228+
return '0,1';
229+
}
230+
};
231+
CLI::setInputOutput($io);
232+
233+
$output = CLI::promptByMultipleKeys('Select your hobbies:', ['Playing game', 'Sleep', 'Badminton']);
234+
235+
CLI::resetInputOutput();
236+
237+
$this->assertSame([0 => 'Playing game', 1 => 'Sleep'], $output);
238+
$this->assertSame(
239+
'You can specify multiple values separated by commas.' . PHP_EOL
240+
. sprintf('[%s, 1, 2] : ', CLI::color('0', 'green')),
241+
$io->receivedPrefix,
242+
);
243+
}
244+
245+
public function testInputWritesPrefixToStdout(): void
246+
{
247+
$io = new MockInputOutput();
248+
$io->setInputs(['blue']);
249+
CLI::setInputOutput($io);
250+
251+
$output = CLI::input('Name: ');
252+
253+
CLI::resetInputOutput();
254+
255+
$this->assertSame('blue', $output);
256+
$this->assertSame('Name: blue' . PHP_EOL, $io->getOutput());
257+
}
258+
146259
public function testPromptByKey(): void
147260
{
148261
PhpStreamWrapper::register();

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Deprecations
3434
Bugs Fixed
3535
**********
3636

37+
- **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.
3738
- **CLIRequest:** Fixed a bug where ``parseCommand()`` could throw a TypeError when ``argv`` is missing.
3839
- **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.
3940
- **Helpers:** Fixed a bug where ``get_dir_file_info()`` returned incomplete entries for subdirectories and missing files instead of omitting them.

0 commit comments

Comments
 (0)