This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Make it possible to append rows to a rendered table in a console command. #2064
Comments
freekmurze
changed the title
Make it possible to append rows in a table in a console command.
Make it possible to append rows to a rendered table in a console command.
Feb 10, 2020
<?php
namespace Illuminate\Console;
use Illuminate\Support\Traits\ForwardsCalls;
class OutputStyle
{
use ForwardsCalls;
// ...
public function __call($method, $parameters) {
return $this->forwardCallTo($this->output, $method, $parameters);
}
} This would allow you to use as long as it being executed inside $section = $this->output->section();
$this->table = new Table($section);
$this->table->setHeaders(['Column', 'Another column']);
$this->table->render();
$this->table->appendRow(['Value', 'Another Value']); Unfortunately |
<?php
namespace Illuminate\Console;
class OutputStyle extends SymfonyStyle
{
// ...
+ /**
+ * Get the underlying Symfony output implementation.
+ *
+ * @return \Symfony\Component\Console\Output\OutputInterface
+ */
+ public function getOutput()
+ {
+ return $this->output;
+ }
} This gives us access to the Symfony\Component\Console\Output\ConsoleOutput instance. <?php
namespace Illuminate\Console\Concerns;
trait InteractsWithIO
{
// ...
/**
* Format input to textual table.
*
* @param array $headers
* @param \Illuminate\Contracts\Support\Arrayable|array $rows
* @param string $tableStyle
* @param array $columnStyles
- * @return void
+ * @return \Symfony\Component\Console\Helper\Table
*/
public function table($headers, $rows, $tableStyle = 'default', array $columnStyles = [])
{
- $table = new Table($this->output);
+ $table = new Table($this->output->getOutput()->section());
if ($rows instanceof Arrayable) {
$rows = $rows->toArray();
}
$table->setHeaders((array) $headers)->setRows($rows)->setStyle($tableStyle);
foreach ($columnStyles as $columnIndex => $columnStyle) {
$table->setColumnStyle($columnIndex, $columnStyle);
}
$table->render();
+
+ return $table;
}
// ...
} Now, then creating a table, the public function handle()
{
$table = $this->table(
['Column', 'Another column'],
[]
);
$table->appendRow(['Value', 'Another Value']);
$table->appendRow(['Value', 'Another Value']);
}
|
@adam-prickett You should PR that to the framework. I you won't, I'll do it 😄 |
@freekmurze On the way! |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Symfony provides a handy
Table
console helper that allows rows to be appended to an already rendered table.Unfortunately, it's currently impossible to use this method in Laravel.
When newing up
Symfony\Component\Console\Helper\Table
it requires aConsoleSectionOutput
to be passed it.A fully constructed
ConsoleSectionOutput
is available when callingsection()
onSymfony\Component\Console\Output\ConsoleOutputInterface
. An object that implements this interface is available onIlluminate\Console\Command
as$output
. Unfortunately, this property is marked asprotected
.In my project, I've just made this property
public
so I can just reach in and get the section.I'm not advocating making this property
public
, but rather for an easy way to be able to useappendRow
.The text was updated successfully, but these errors were encountered: