-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
403 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
use function Laravel\Prompts\info; | ||
use function Laravel\Prompts\note; | ||
use function Laravel\Prompts\progress; | ||
use function Laravel\Prompts\table; | ||
use function Laravel\Prompts\text; | ||
use function Laravel\Prompts\watch; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
watch( | ||
function () { | ||
static $iteration = 0; | ||
static $items = []; | ||
|
||
if (count($items) === 5) { | ||
array_shift($items); | ||
} | ||
|
||
$items[] = [$iteration += 1, (new Datetime())->format(DateTime::RFC850)]; | ||
|
||
if (count($items) === 5) { | ||
info(sprintf('Now the table just scrolls, %s and counting...', $iteration)); | ||
} else { | ||
info('Filling up the table...'); | ||
} | ||
|
||
progress('a nice progressbar', 5)->advance($iteration % 5); | ||
|
||
$ralph = text('Ralph', default: 'Ralph Wiggum: I\'m ignored!'); | ||
|
||
note($ralph); | ||
|
||
table( | ||
[ | ||
'Iteration', | ||
'DateTime' | ||
], | ||
$items | ||
); | ||
}, | ||
1, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Laravel\Prompts\Themes\Default; | ||
|
||
use Laravel\Prompts\Output\BufferedConsoleOutput; | ||
use Laravel\Prompts\Watch; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class WatchRenderer extends Renderer | ||
{ | ||
/** | ||
* buffers the output from a Buffered Prompt and flushes the output to Prompts | ||
* in order to utilize Prompts neat way of updating lines | ||
*/ | ||
public function __invoke(Watch $watch, OutputInterface $originalOutput): string | ||
{ | ||
$bufferedOutput = new BufferedConsoleOutput(); | ||
|
||
$watch::setOutput($bufferedOutput); | ||
|
||
($watch->watch)(); | ||
|
||
$watch::setOutput($originalOutput); | ||
|
||
return $bufferedOutput->fetch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
namespace Laravel\Prompts; | ||
|
||
use Closure; | ||
use Mockery\MockInterface; | ||
use PHPUnit\Framework\Assert; | ||
use RuntimeException; | ||
use ValueError; | ||
|
||
class Watch extends Prompt | ||
{ | ||
/** | ||
* How many times to fake an iteration. | ||
*/ | ||
protected static int $fakeTimes = 1; | ||
|
||
/** | ||
* count of faked iterations. | ||
*/ | ||
protected int $fakedTimes = 0; | ||
|
||
/** | ||
* Faking sleep or not. | ||
*/ | ||
protected static bool $fakeSleep = true; | ||
|
||
/** | ||
* The amount of seconds slept during intervals in total. | ||
*/ | ||
protected static int $sleptSeconds = 0; | ||
|
||
/** | ||
* The closure to execute on interval. | ||
*/ | ||
public Closure $watch; | ||
|
||
/** | ||
* The interval between updates. | ||
*/ | ||
protected int $interval; | ||
|
||
/** | ||
* Create a new Watch instance. | ||
*/ | ||
public function __construct(callable $watch, ?int $interval = 2) | ||
{ | ||
static::$sleptSeconds = 0; | ||
$this->watch = $watch(...); | ||
$this->interval = $interval ?? 2; | ||
|
||
if ($this->interval < 0) { | ||
throw new ValueError('watch interval must be greater than or equal to 0'); | ||
} | ||
} | ||
|
||
public function display(): void | ||
{ | ||
$this->prompt(); | ||
} | ||
|
||
/** | ||
* displays the watched output and updates after the specified interval. | ||
*/ | ||
public function render(): void | ||
{ | ||
$faked = static::isFaked(); | ||
|
||
static::interactive(false); | ||
|
||
while (!$faked || $this->fakedTimes < static::$fakeTimes) { | ||
|
||
parent::render(); | ||
|
||
if ($faked) { | ||
$this->fakedTimes++; | ||
|
||
if ($this->fakedTimes >= static::$fakeTimes) { | ||
|
||
if (static::$terminal instanceof MockInterface) { | ||
$this->state = 'submit'; | ||
static::$terminal->expects('read')->zeroOrMoreTimes()->andReturn(false); | ||
} | ||
static::$fakeSleep = true; | ||
break; | ||
} | ||
|
||
if (static::$fakeSleep) { | ||
static::$sleptSeconds += $this->interval; | ||
continue; | ||
} | ||
} | ||
|
||
sleep($this->interval); | ||
} | ||
} | ||
|
||
/** | ||
* Render the prompt using the active theme. | ||
* Overrides default behaviour to pass along the current output. | ||
*/ | ||
protected function renderTheme(): string | ||
{ | ||
$renderer = static::getRenderer(); | ||
|
||
return $renderer($this, static::output()); | ||
} | ||
|
||
/** | ||
* Get the value of the prompt. | ||
*/ | ||
public function value(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Tell Prompt how many iterations to fake. | ||
*/ | ||
public static function fakeTimes(int $times): void | ||
{ | ||
if (!static::isFaked()) { | ||
throw new RuntimeException('Prompt must be faked before faking iterations.'); | ||
} | ||
|
||
static::$fakeTimes = $times; | ||
} | ||
|
||
/** | ||
* Asserts the amount of seconds slept during intervals in total. | ||
*/ | ||
public static function assertSecondsSleptBetweenIntervals(int $seconds): void | ||
{ | ||
if (!static::isFaked()) { | ||
throw new RuntimeException('Prompt must be faked before asserting.'); | ||
} | ||
|
||
Assert::assertEquals($seconds, static::$sleptSeconds); | ||
} | ||
|
||
/** | ||
* By default, when Prompt is faked, the intervals are faked. | ||
* Use this to actually sleep between updates. | ||
*/ | ||
public static function shouldNotFakeIntervalSleep(): void | ||
{ | ||
if (!self::isFaked()) { | ||
throw new RuntimeException('Not faking sleep makes no sense when not faking Prompt.'); | ||
} | ||
|
||
static::$fakeSleep = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.