Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add setHidden to ask function #171

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function terminal(): Terminal
*
* @param iterable<array-key, string>|null $autocomplete
*/
function ask(string $question, iterable $autocomplete = null): mixed
function ask(string $question, iterable $autocomplete = null, bool $setHidden = false): mixed
{
return (new Question)->ask($question, $autocomplete);
return (new Question)->ask($question, $autocomplete, $setHidden);
}
}
6 changes: 5 additions & 1 deletion src/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function getStreamableInput(): StreamableInputInterface
*
* @param iterable<array-key, string>|null $autocomplete
*/
public function ask(string $question, iterable $autocomplete = null): mixed
public function ask(string $question, iterable $autocomplete = null, bool $setHidden = false): mixed
{
$html = (new HtmlRenderer)->parse($question)->toString();

Expand All @@ -63,6 +63,10 @@ public function ask(string $question, iterable $autocomplete = null): mixed
$question->setAutocompleterValues($autocomplete);
}

if ($setHidden) {
$question->setHidden($setHidden);
}

$output = Termwind::getRenderer();

if ($output instanceof SymfonyStyle) {
Expand Down
10 changes: 5 additions & 5 deletions src/ValueObjects/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function getValue(): string
public function getChildNodes(): Generator
{
foreach ($this->node->childNodes as $node) {
yield new static($node);
yield new self($node);
}
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public function getPreviousSibling(): static|null
$node = $this->node;

while ($node = $node->previousSibling) {
$node = new static($node);
$node = new self($node);

if ($node->isEmpty()) {
$node = $node->node;
Expand All @@ -121,7 +121,7 @@ public function getPreviousSibling(): static|null
$node = $node->node;
}

return is_null($node) ? null : new static($node);
return is_null($node) ? null : new self($node);
}

/**
Expand All @@ -132,7 +132,7 @@ public function getNextSibling(): static|null
$node = $this->node;

while ($node = $node->nextSibling) {
$node = new static($node);
$node = new self($node);

if ($node->isEmpty()) {
$node = $node->node;
Expand All @@ -147,7 +147,7 @@ public function getNextSibling(): static|null
$node = $node->node;
}

return is_null($node) ? null : new static($node);
return is_null($node) ? null : new self($node);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/ask.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@

ask('<span class="bg-red ml-1">Question</span>', ['one', 'two', 'three']);
})->skip(! Terminal::hasSttyAvailable(), '`stty` is required to test autocomplete functionality');

it('renders the question with hidden input', function () {
$inputStream = getInputStream('s');
Question::setStreamableInput($input = Mockery::mock(StreamableInputInterface::class));

$input->shouldReceive('getStream')->once()->andReturn($inputStream);
$input->shouldReceive('isInteractive')->once()->andReturn(true);

renderUsing($output = Mockery::mock(OutputInterface::class));

$output->shouldReceive('write')->once()->with(' <bg=red>Password</>');
$output->shouldReceive('writeln')->once()->with('');
$answer = ask('<span class="bg-red ml-1">Password</span>', null, true);
});