-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from tempestphp/static-components
Static components
- Loading branch information
Showing
43 changed files
with
990 additions
and
146 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Components; | ||
|
||
interface HasStaticComponent | ||
{ | ||
public function getStaticComponent(): StaticComponent; | ||
} |
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
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,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Console\Components\Interactive; | ||
|
||
use Closure; | ||
use Generator; | ||
use Tempest\Console\Components\HasStaticComponent; | ||
use Tempest\Console\Components\InteractiveComponent; | ||
use Tempest\Console\Components\Static\StaticProgressBarComponent; | ||
use Tempest\Console\Components\StaticComponent; | ||
|
||
final readonly class ProgressBarComponent implements InteractiveComponent, HasStaticComponent | ||
{ | ||
public function __construct( | ||
private iterable $data, | ||
private Closure $handler, | ||
/** @var null|Closure(int $step, int $count): string $format */ | ||
private ?Closure $format = null, | ||
) { | ||
} | ||
|
||
public function render(): Generator | ||
{ | ||
$result = []; | ||
|
||
$count = iterator_count($this->data); | ||
$step = 1; | ||
|
||
$format = $this->format ?? function (int $step, int $count): string { | ||
$width = 30; | ||
|
||
$progress = (int) round(($step / $count) * $width); | ||
|
||
if ($step === $count) { | ||
$bar = sprintf( | ||
'[%s]', | ||
str_repeat('=', $width), | ||
); | ||
} else { | ||
$bar = sprintf( | ||
'[%s>%s]', | ||
str_repeat('=', max(0, $progress)), | ||
str_repeat(' ', $width - $progress), | ||
); | ||
} | ||
|
||
return sprintf( | ||
'%s (%s/%s)', | ||
$bar, | ||
$step, | ||
$count, | ||
) . PHP_EOL; | ||
}; | ||
|
||
foreach ($this->data as $item) { | ||
yield $format($step, $count); | ||
|
||
$result[] = ($this->handler)($item); | ||
|
||
$step += 1; | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function getStaticComponent(): StaticComponent | ||
{ | ||
return new StaticProgressBarComponent( | ||
data: $this->data, | ||
handler: $this->handler, | ||
format: $this->format, | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.