Skip to content
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

[5.5] Allow setting column styles for tables in Artisan commands #21169

Merged
merged 3 commits into from
Sep 14, 2017

Conversation

cwt137
Copy link
Contributor

@cwt137 cwt137 commented Sep 13, 2017

The current table method in the Illuminate\Console\Command class doesn't allow you to set the column style. Because of this, you can't do certain things, like right align items. It is doable, as per this Symfony blog post.

This patch makes possible what is described in the blog post by adding a new optional parameter to the end of the table method. It takes an array where the key is the column index and the value is either the column style name, or an instance of Symfony\Component\Console\Helper\TableStyle.

With the changes in the patch, the equivelent to the example in the blog post looks like this for an Artisan command:

public function handle()
{
    $headers = ['#', 'Path', 'Size'];

    $rows = [
        [1, 'autoload.php', '183'],
        [2, 'ApplicationTest.php', '247,794'],
        [3, 'CommandTest.php', '14,965'],
        [4, 'ListCommandTest.php', '2,369']
    ];

    $rightAligned = new TableStyle();
    $rightAligned->setPadType(STR_PAD_LEFT);

    $columnStyles = [2 => $rightAligned];

    $this->table($headers, $rows, 'default', $columnStyles);
}

Artisan command output:

+---+---------------------+---------+
| # | Path                |    Size |
+---+---------------------+---------+
| 1 | autoload.php        |     183 |
| 2 | ApplicationTest.php | 247,794 |
| 3 | CommandTest.php     |  14,965 |
| 4 | ListCommandTest.php |   2,369 |
+---+---------------------+---------+

@tillkruss tillkruss changed the title Allow setting column styles for tables in Artisan commands [5.5] Allow setting column styles for tables in Artisan commands Sep 13, 2017
@tillkruss
Copy link
Contributor

Your indentation is off, can you fix that so the review is easier?

@cwt137
Copy link
Contributor Author

cwt137 commented Sep 13, 2017

I ran phpcs on the file and fixed the lines of code I changed, but there still seems to be some errors from previous commits. I did not fix those.

{
$table = new Table($this->output);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a space here that's unnecessary.

@taylorotwell taylorotwell merged commit b06be42 into laravel:5.5 Sep 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants