Skip to content

[Console] Adding optional columns to Table #47448

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

Open
wants to merge 6 commits into
base: 7.4
Choose a base branch
from

Conversation

jlslew
Copy link

@jlslew jlslew commented Sep 1, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #44916
License MIT
Doc PR symfony/symfony-docs#...
BC Break I think no

Create a file in the root directory of a Symfony project with the following code, then run php FILENAME table

#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

$application = new Application('jlslew', '1.0.0');

$application->add(new class extends Command {
    protected function configure(): void
    {
        $this->setName('table');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        (new Table($output))->setHeaders(['ISBN', 'Title', 'Author', 'Year'])
            ->setRows([
                ['99921-58-10-7', 'Divine Comedy', 'Dante Alighieri', 1990],
                ['9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens', 1995],
                ['960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien', 2000],
                ['80-902734-1-6', 'And Then There Were None', 'Agatha Christie', 2005],
                new TableSeparator(),
                [new TableCell('This value spans 3 columns.', ['colspan' => 3])],
                [new TableCell('This value spans 2 columns.', ['colspan' => 2]), 'J. K. Rowling', 2010],
            ])
            ->setMaxWidth(60) // Change this value to see the cols 1 and 3 get dropped
            ->setOptionalColumns([1, 3])
            ->render();

        return Command::SUCCESS;
    }
});

$application->run();

@jlslew
Copy link
Author

jlslew commented Sep 1, 2022

TODO: Right now it drops the largest columns first, but might think about maximizing the max-width by dropping the appropriate columns

EDIT: After a good night sleep, I think for a first implementation it is good enough and not too crazy.

@carsonbot
Copy link

Hey!

I think @yoannrenard has recently worked with this code. Maybe they can help review this?

Cheers!

Carsonbot

@OskarStark OskarStark changed the title [Console] Feature/44916 Adding optional columns to Table [Console] Adding optional columns to Table Sep 8, 2022
@94noni
Copy link
Contributor

94noni commented Nov 4, 2022

I like it :)

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 May 23, 2023
@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.1 Nov 15, 2023
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Console] Adding optional columns to Table
6 participants