Skip to content

Extract Looping Mechanisms #154

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

Closed
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
19 changes: 16 additions & 3 deletions src/Concerns/FakesInputOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Prompts\Concerns;

use Closure;
use Laravel\Prompts\Output\BufferedConsoleOutput;
use Laravel\Prompts\Terminal;
use PHPUnit\Framework\Assert;
Expand Down Expand Up @@ -29,13 +30,25 @@ public static function fake(array $keys = []): void
$mock->shouldReceive('lines')->byDefault()->andReturn(24);
$mock->shouldReceive('initDimensions')->byDefault();

foreach ($keys as $key) {
static::fakeKeyPresses($keys, function (string $key) use ($mock) {
$mock->shouldReceive('read')->once()->andReturn($key);
}
});

static::$terminal = $mock;

self::setOutput(new BufferedConsoleOutput());
static::setOutput(new BufferedConsoleOutput());
}

/**
* Implementation of the looping mechanism for simulating key presses.
*
* @param array<string> $keys
*/
public static function fakeKeyPresses(array $keys, Closure $closure): void
{
foreach ($keys as $key) {
$closure($key);
}
}

/**
Expand Down
35 changes: 31 additions & 4 deletions src/Prompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Laravel\Prompts\Exceptions\FormRevertedException;
use Laravel\Prompts\Output\ConsoleOutput;
use Laravel\Prompts\Support\Nothing;
use RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
Expand Down Expand Up @@ -122,7 +123,7 @@ public function prompt(): mixed
$this->hideCursor();
$this->render();

while (($key = static::terminal()->read()) !== null) {
$result = $this->runLoop(function (string $key): mixed {
$continue = $this->handleKeyPress($key);

$this->render();
Expand All @@ -142,12 +143,38 @@ public function prompt(): mixed

return $this->value();
}
}

// `null` is a valid return value for this loop
// so we'll return an instance of Nothing to
// indicate that the loop should continue.
return new Nothing;
});

return $result;
} finally {
$this->clearListeners();
}
}

public function runLoop(callable $callable): mixed
{
while(($key = static::terminal()->read()) !== null) {
$result = $callable($key);

if (! $this->is_nothing($result)) {
return $result;
}
}
}

/**
* Check if the provided item is an instance of Nothing.
*/
public function is_nothing(mixed $item): bool
{
return is_object($item) && is_a($item, Nothing::class);
}

/**
* Register a callback to be invoked when a user cancels a prompt.
*/
Expand Down Expand Up @@ -179,15 +206,15 @@ protected function capturePreviousNewLines(): void
*/
public static function setOutput(OutputInterface $output): void
{
self::$output = $output;
static::$output = $output;
}

/**
* Get the current output instance.
*/
protected static function output(): OutputInterface
{
return self::$output ??= new ConsoleOutput();
return static::$output ??= new ConsoleOutput();
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/Support/Nothing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Laravel\Prompts\Support;

/**
* Nothing.
*
* `null` is a valid return value, so we use `Nothing`
* to indicate that a given loop should continue.
*/
class Nothing
{
//
}