Skip to content

[Console] Adding optional columns to Table #44916

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
Seldaek opened this issue Jan 5, 2022 · 16 comments · May be fixed by #47448
Open

[Console] Adding optional columns to Table #44916

Seldaek opened this issue Jan 5, 2022 · 16 comments · May be fixed by #47448
Labels

Comments

@Seldaek
Copy link
Member

Seldaek commented Jan 5, 2022

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().

@dsentker
Copy link

dsentker commented Jan 9, 2022

I like the idea, but IMHO the name "optional" is wrong. That sounds more like a "show column if user enables it".

What about $table->setTruncatedColumns() ?

@Seldaek
Copy link
Member Author

Seldaek commented Jan 9, 2022

Yeah I'm happy to improve naming. allowTruncateColumns or something might be even better as it's really not truncating them unless they overrun the limit, but what's really missing is an implementation ;)

@GromNaN
Copy link
Member

GromNaN commented Jan 9, 2022

I like this idea. For naming, we can reverse the POV and call it setExtraColumns
Regarding UX, a message make clear that more columns can be displayed if the terminal is larger. What should be the behavior when the output piped?

@kaznovac
Copy link
Contributor

may I suggest to keep the meaning in the web context - setResponsiveColumns? (It's known term to web developers, but I'm not sure if developer would then assume the columns would resize with the terminal...)

@dsentker
Copy link

For me, setResponsiveColumns is a good method name.

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@Seldaek
Copy link
Member Author

Seldaek commented Jul 12, 2022

Nope, still would be nice to have.

@carsonbot carsonbot removed the Stalled label Jul 12, 2022
@jlslew
Copy link

jlslew commented Aug 31, 2022

Hello @carsonbot and all,

  • How should we handle multiple columns? Do we get (max-width - sum(fixed-width)) / number of drop-able cols)?
  • When do we know they are drop-able? Do we assume a fixed min-width or do we allow the user to pass in a configuration?
  • If there are multiple columns, do we drop all of them or do we have kind of a priority list; the higher the priority is the first to be dropped?

@Seldaek
Copy link
Member Author

Seldaek commented Aug 31, 2022

  • max-width - fixed-width-columns gives you the space left to fit optional columns in
  • as per my example in OP I'd say $table->setOptionalColumns([1]); would trigger this behavior, if they are optional and they don't fit they are removed entirely
  • I'd keep it simple but up to you (or whoever does the work). I would try to fit as many optional columns as can fit in order they come in (left to right), keeping in mind that optional ones may appear in between non-optional ones.

@jlslew
Copy link

jlslew commented Aug 31, 2022

Let's say we have max-width of 80 characters with col[0] = 20 chars, col[1] = 40 chars and col[2] = 30 chars
Setting col[2] to optional would display

  • a table with a width of 80 chars containing col[0], col[1] and col[2] (truncated to 20 chars)?
  • a table with a width of 60 chars containing col[0] and col[1]?

If we go with truncated to 20 chars, what is the min-width allowed? Since a column truncated to 1 char is technically still a valid one.

they come in (left to right) and are dropped from right to left
I get it now, will give a try.

@jlslew jlslew linked a pull request Sep 1, 2022 that will close this issue
@Seldaek
Copy link
Member Author

Seldaek commented Sep 1, 2022

Good question about the truncation..

Could be ->setColumnOptions([0 => 'truncate', 3 => 'skip']); where skip just skips it entirely, truncate truncates down to 1 char if needed? And by default it would just be shown no matter what.

You may want an array of options per column to have max flexibility/forward capability, so you could add priorities or min length or whatever.. Up to you if you wanna go crazy with this :) I just thought it'd be nice to have basic capability built-in to simplify consumer code.

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@kaznovac
Copy link
Contributor

kaznovac commented Mar 3, 2023

Nope, still would be nice to have.

@carsonbot carsonbot removed the Stalled label Mar 3, 2023
@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@carsonbot
Copy link

Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3

@Seldaek
Copy link
Member Author

Seldaek commented Sep 22, 2023

Yes yes, still would be nice, just needs someone with time and motivation 🤞🏻

@carsonbot carsonbot removed the Stalled label Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants