Skip to content

Commit 1eb06c6

Browse files
committed
feat(interactor): add promptHidden (unix only)
1 parent 3628331 commit 1eb06c6

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/IO/Interactor.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,25 +280,48 @@ public function choices(string $text, array $choices, $default = null, bool $cas
280280
*/
281281
public function prompt(string $text, $default = null, callable $fn = null, int $retry = 3)
282282
{
283-
$error = 'Invalid value. Please try again!';
283+
$error = 'Invalid value. Please try again!';
284+
$hidden = \func_get_args()[4] ?? false;
285+
$readFn = $hidden ? 'readHidden' : 'read';
284286

285287
$this->writer->yellow($text)->comment(null !== $default ? " [$default]: " : ': ');
286288

287289
try {
288-
$input = $this->reader->read($default, $fn);
290+
$input = $this->reader->{$readFn}($default, $fn);
289291
} catch (\Exception $e) {
290292
$error = $e->getMessage();
291293
}
292294

293295
if ($retry > 0 && (isset($e) || \strlen($input ?? '') === 0)) {
294296
$this->writer->bgRed($error, true);
295297

296-
return $this->prompt($text, $default, $fn, $retry - 1);
298+
return $this->prompt($text, $default, $fn, $retry - 1, $hidden);
297299
}
298300

299301
return $input ?? $default;
300302
}
301303

304+
/**
305+
* Prompt user for secret input like password. Currently for unix only.
306+
*
307+
* @param string $text Prompt text.
308+
* @param callable|null $fn The sanitizer/validator for user input
309+
* Any exception message is printed as error.
310+
* @param int $retry How many more times to retry on failure.
311+
*
312+
* @return mixed
313+
*/
314+
public function promptHidden(string $text, callable $fn = null, int $retry = 3)
315+
{
316+
$winOS = '\\' === \DIRECTORY_SEPARATOR;
317+
318+
if ($winOS) {
319+
$this->writer->error('Hidden input not supported, Press Ctrl+C if you would like to abort', true);
320+
}
321+
322+
return $this->prompt($text, null, $fn, $retry, !$winOS);
323+
}
324+
302325
/**
303326
* Show choices list.
304327
*

0 commit comments

Comments
 (0)