-
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.
* add a table helper * added docblock for table params * dealing with the collections in the helper function appeasing static analysis * Fix colspan and make border color consistent * Remove extra whitespace from last column * Fix border when there are no headers * Allow passing rows as only argument * Add `table` playground * Remove unnecessary type --------- Co-authored-by: Jess Archer <jess@jessarcher.com>
- Loading branch information
1 parent
8544d8c
commit 85f0a1e
Showing
6 changed files
with
250 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,20 @@ | ||
<?php | ||
|
||
use function Laravel\Prompts\table; | ||
|
||
require __DIR__.'/../vendor/autoload.php'; | ||
|
||
table( | ||
['Name', 'Twitter'], | ||
[ | ||
['Taylor Otwell', '@taylorotwell'], | ||
['Dries Vints', '@driesvints'], | ||
['James Brooks', '@jbrooksuk'], | ||
['Nuno Maduro', '@enunomaduro'], | ||
['Mior Muhammad Zaki', '@crynobone'], | ||
['Jess Archer', '@jessarchercodes'], | ||
['Guus Leeuw', '@phpguus'], | ||
['Tim MacDonald', '@timacdonald87'], | ||
['Joe Dixon', '@_joedixon'], | ||
], | ||
); |
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,71 @@ | ||
<?php | ||
|
||
namespace Laravel\Prompts; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
class Table extends Prompt | ||
{ | ||
/** | ||
* The table headers. | ||
* | ||
* @var array<int, string|array<int, string>> | ||
*/ | ||
public array $headers; | ||
|
||
/** | ||
* The table rows. | ||
* | ||
* @var array<int, array<int, string>> | ||
*/ | ||
public array $rows; | ||
|
||
/** | ||
* Create a new Table instance. | ||
* | ||
* @param array<int, string|array<int, string>>|Collection<int, string|array<int, string>> $headers | ||
* @param array<int, array<int, string>>|Collection<int, array<int, string>> $rows | ||
* | ||
* @phpstan-param ($rows is null ? list<list<string>>|Collection<int, list<string>> : list<string|list<string>>|Collection<int, string|list<string>>) $headers | ||
*/ | ||
public function __construct(array|Collection $headers = [], array|Collection $rows = null) | ||
{ | ||
if ($rows === null) { | ||
$rows = $headers; | ||
$headers = []; | ||
} | ||
|
||
$this->headers = $headers instanceof Collection ? $headers->all() : $headers; | ||
$this->rows = $rows instanceof Collection ? $rows->all() : $rows; | ||
} | ||
|
||
/** | ||
* Display the table. | ||
*/ | ||
public function display(): void | ||
{ | ||
$this->prompt(); | ||
} | ||
|
||
/** | ||
* Display the table. | ||
*/ | ||
public function prompt(): bool | ||
{ | ||
$this->capturePreviousNewLines(); | ||
|
||
$this->state = 'submit'; | ||
|
||
static::output()->write($this->renderTheme()); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Get the value of the prompt. | ||
*/ | ||
public function value(): bool | ||
{ | ||
return true; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace Laravel\Prompts\Themes\Default; | ||
|
||
use Laravel\Prompts\Output\BufferedConsoleOutput; | ||
use Laravel\Prompts\Table; | ||
use Symfony\Component\Console\Helper\Table as SymfonyTable; | ||
use Symfony\Component\Console\Helper\TableStyle; | ||
|
||
class TableRenderer extends Renderer | ||
{ | ||
/** | ||
* Render the table. | ||
*/ | ||
public function __invoke(Table $table): string | ||
{ | ||
$tableStyle = (new TableStyle()) | ||
->setHorizontalBorderChars('─') | ||
->setVerticalBorderChars('│', '│') | ||
->setCellHeaderFormat($this->dim('<fg=default>%s</>')) | ||
->setCellRowFormat('<fg=default>%s</>'); | ||
|
||
if (empty($table->headers)) { | ||
$tableStyle->setCrossingChars('┼', '', '', '', '┤', '┘</>', '┴', '└', '├', '<fg=gray>┌', '┬', '┐'); | ||
} else { | ||
$tableStyle->setCrossingChars('┼', '<fg=gray>┌', '┬', '┐', '┤', '┘</>', '┴', '└', '├'); | ||
} | ||
|
||
$buffered = new BufferedConsoleOutput(); | ||
|
||
(new SymfonyTable($buffered)) | ||
->setHeaders($table->headers) | ||
->setRows($table->rows) | ||
->setStyle($tableStyle) | ||
->render(); | ||
|
||
collect(explode(PHP_EOL, trim($buffered->content(), PHP_EOL))) | ||
->each(fn ($line) => $this->line(' '.$line)); | ||
|
||
return $this; | ||
} | ||
} |
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,103 @@ | ||
<?php | ||
|
||
use Laravel\Prompts\Prompt; | ||
|
||
use function Laravel\Prompts\table; | ||
|
||
it('renders a table with headers', function ($headers, $rows) { | ||
Prompt::fake(); | ||
|
||
table($headers, $rows); | ||
|
||
Prompt::assertStrippedOutputContains(<<<'OUTPUT' | ||
┌────────────────────┬──────────────────┐ | ||
│ Name │ Twitter │ | ||
├────────────────────┼──────────────────┤ | ||
│ Taylor Otwell │ @taylorotwell │ | ||
│ Dries Vints │ @driesvints │ | ||
│ James Brooks │ @jbrooksuk │ | ||
│ Nuno Maduro │ @enunomaduro │ | ||
│ Mior Muhammad Zaki │ @crynobone │ | ||
│ Jess Archer │ @jessarchercodes │ | ||
│ Guus Leeuw │ @phpguus │ | ||
│ Tim MacDonald │ @timacdonald87 │ | ||
│ Joe Dixon │ @_joedixon │ | ||
└────────────────────┴──────────────────┘ | ||
OUTPUT); | ||
})->with([ | ||
'arrays' => [ | ||
['Name', 'Twitter'], | ||
[ | ||
['Taylor Otwell', '@taylorotwell'], | ||
['Dries Vints', '@driesvints'], | ||
['James Brooks', '@jbrooksuk'], | ||
['Nuno Maduro', '@enunomaduro'], | ||
['Mior Muhammad Zaki', '@crynobone'], | ||
['Jess Archer', '@jessarchercodes'], | ||
['Guus Leeuw', '@phpguus'], | ||
['Tim MacDonald', '@timacdonald87'], | ||
['Joe Dixon', '@_joedixon'], | ||
], | ||
], | ||
'collections' => [ | ||
collect(['Name', 'Twitter']), | ||
collect([ | ||
['Taylor Otwell', '@taylorotwell'], | ||
['Dries Vints', '@driesvints'], | ||
['James Brooks', '@jbrooksuk'], | ||
['Nuno Maduro', '@enunomaduro'], | ||
['Mior Muhammad Zaki', '@crynobone'], | ||
['Jess Archer', '@jessarchercodes'], | ||
['Guus Leeuw', '@phpguus'], | ||
['Tim MacDonald', '@timacdonald87'], | ||
['Joe Dixon', '@_joedixon'], | ||
]), | ||
], | ||
]); | ||
|
||
it('renders a table without headers', function ($rows) { | ||
Prompt::fake(); | ||
|
||
table($rows); | ||
|
||
Prompt::assertStrippedOutputContains(<<<'OUTPUT' | ||
┌────────────────────┬──────────────────┐ | ||
│ Taylor Otwell │ @taylorotwell │ | ||
│ Dries Vints │ @driesvints │ | ||
│ James Brooks │ @jbrooksuk │ | ||
│ Nuno Maduro │ @enunomaduro │ | ||
│ Mior Muhammad Zaki │ @crynobone │ | ||
│ Jess Archer │ @jessarchercodes │ | ||
│ Guus Leeuw │ @phpguus │ | ||
│ Tim MacDonald │ @timacdonald87 │ | ||
│ Joe Dixon │ @_joedixon │ | ||
└────────────────────┴──────────────────┘ | ||
OUTPUT); | ||
})->with([ | ||
'arrays' => [ | ||
[ | ||
['Taylor Otwell', '@taylorotwell'], | ||
['Dries Vints', '@driesvints'], | ||
['James Brooks', '@jbrooksuk'], | ||
['Nuno Maduro', '@enunomaduro'], | ||
['Mior Muhammad Zaki', '@crynobone'], | ||
['Jess Archer', '@jessarchercodes'], | ||
['Guus Leeuw', '@phpguus'], | ||
['Tim MacDonald', '@timacdonald87'], | ||
['Joe Dixon', '@_joedixon'], | ||
], | ||
], | ||
'collections' => [ | ||
collect([ | ||
['Taylor Otwell', '@taylorotwell'], | ||
['Dries Vints', '@driesvints'], | ||
['James Brooks', '@jbrooksuk'], | ||
['Nuno Maduro', '@enunomaduro'], | ||
['Mior Muhammad Zaki', '@crynobone'], | ||
['Jess Archer', '@jessarchercodes'], | ||
['Guus Leeuw', '@phpguus'], | ||
['Tim MacDonald', '@timacdonald87'], | ||
['Joe Dixon', '@_joedixon'], | ||
]), | ||
], | ||
]); |