Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Adding optional columns to Table #44916

Open
@Seldaek

Description

@Seldaek

Description

There are cases where you don't want to wrap content because it may be too big and wrapping does make the output much messier and hard to read.

One example is composer search foo where we do not care to show the full package description if it is too long. So you get output like this:

foolz/sphinxql-query-builder              A PHP query builder for SphinxQL. Uses MySQLi to connect to the...
dflydev/placeholder-resolver              Given a data source representing key => value pairs, resolve pl...
muffin/footprint                          CakePHP plugin to allow passing currently logged in user to mod...
kyranb/footprints                         A simple registration attribution tracking solution for Laravel...

Where the description was capped at the terminal width.

The way it's achieved is by calculating everything manually as using Table does not let you achieve this:

https://github.com/composer/composer/blob/711f436b2431ebf2845ee89a556ffde619212405/src/Composer/Command/SearchCommand.php#L101-L117

I believe having a concept of optional columns would solve this, whereas Table::calculateColumnsWidth could compute the max width of everything, then reduce the optional columns until everything fits on screen, and if not possible drop these columns entirely.

Example

This would be the above code from Composer rewritten to use the Table if optional columns where supported:

            $table = new Table($output);
            $table->setStyle('compact');
            $table->setOptionalColumns([1]); // setting the second (0-based) column to be optional
            foreach ($results as $result) {
                $description = isset($result['description']) ? $result['description'] : '';
                $warning = !empty($result['abandoned']) ? '<warning>! Abandoned !</warning> ' : '';

                $table->addRow([$result['name'], $warning . $description]);
            }
            $table->render();

The code is much leaner, doesn't have to deal with all the width calculations, and as a bonus it probably fixes bugs because calculateColumnsWidth does width calculation much smarter than strlen().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions