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

Skip to content

[Console] Add title table #26933

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

Merged
merged 1 commit into from
Jul 11, 2018
Merged

Conversation

maidmaid
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets /
License MIT
Doc PR /

Why not just add a title to console tables? Inspired by the famous file manager Midnight Commander.

screenshot from 2018-04-15 20-16-26

TABLE
),
array(
'Boooooooooooooooooooooooooooooooooooooooooooooooooooooooooks',
Copy link
Contributor

Choose a reason for hiding this comment

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

what about truncating instead of ignoring?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, it's better. Done.

@@ -350,7 +361,7 @@ public function render()
*
* Example: <code>+-----+-----------+-------+</code>
*/
private function renderRowSeparator(int $type = self::SEPARATOR_MID)
private function renderRowSeparator(int $type = self::SEPARATOR_MID, $title = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

string $title = null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -290,6 +292,13 @@ public function setRow($column, array $row)
return $this;
}

public function setTitle($title): self
Copy link
Contributor

Choose a reason for hiding this comment

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

?string $title?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@maidmaid maidmaid force-pushed the console-table-title branch from 6d1bbfb to 706bab2 Compare April 15, 2018 14:25
@nicolas-grekas nicolas-grekas added this to the 4.2 milestone Apr 15, 2018
'Boooooooooooooooooooooooooooooooooooooooooooooooooooooooooks',
'default',
<<<'TABLE'
+ Booooooooooooooooooooooooooooooooooooooooooooooooooooooo... +
Copy link
Member

Choose a reason for hiding this comment

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

What if we leave at least 1 separator character for long titles in the head? Instead of this:

+ Booooooooooooooooooooooooooooooooooooooooooooooooooooooo... +
| ISBN          | Title                    | Author           |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
| ...

This:

+- Booooooooooooooooooooooooooooooooooooooooooooooooooooo... -+
| ISBN          | Title                    | Author           |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
| ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. That looks much better!

@@ -72,6 +72,18 @@ public static function substr($string, $from, $length = null)
return mb_substr($string, $from, $length, $encoding);
}

/**
* Returns the text within a portion of a string, using mb_substr if it is available.
Copy link
Member

Choose a reason for hiding this comment

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

We could remove ... using mb_substr if it is available from the description because it's an internal detail and in the PHPdoc we usually only explain what we do but not how we do that.

Also, the description is a bit confusing Returns the text within a portion of a string ... because the function name means Replace a part of the given string...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had put the mb_substr detail to be consistent with the others methods of this helper, but effectively, that isn't really useful, so it's deleted now.

About the description, I reworded to Replaces text within a portion of a string (the same description than in the PHP doc)

@@ -378,6 +389,17 @@ private function renderRowSeparator(int $type = self::SEPARATOR_MID)
$markup .= $column === $count - 1 ? $rightChar : $midChar;
}

if (null !== $title) {
$lenT = Helper::strlenWithoutDecoration($formatter = $this->output->getFormatter(), $formattedTitle = sprintf($format = $this->style->getTitleFormat(), $title));
Copy link
Member

Choose a reason for hiding this comment

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

Let's use better variable names here to ease future maintenance. For example:

$lenT -> $titleTextLength
$lenM -> $titleMarkupLength

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed :)

@maidmaid maidmaid force-pushed the console-table-title branch 2 times, most recently from 30a72f8 to 47183ed Compare April 16, 2018 13:46
@fabpot
Copy link
Member

fabpot commented Apr 16, 2018

Would it make sense to allow the title to be at the bottom as well?

@maidmaid
Copy link
Contributor Author

@fabpot What do you mean by "at the bottom"? The second line of the header or the very last line of the table?

@fabpot
Copy link
Member

fabpot commented Apr 16, 2018

I was thinking about the last line of the table.

@maidmaid
Copy link
Contributor Author

IMO, shown the title only in the first line is enough.

@fabpot
Copy link
Member

fabpot commented Apr 19, 2018

Not sure I understand the "enough" part. I was thinking about letting the user choose between displaying the title at the top or at the bottom (depending on what you want to achieve, doing one or the other might make more sense).

@ro0NL
Copy link
Contributor

ro0NL commented Apr 19, 2018

What about setHeaderTitle + setFooterTitle? Agree a title at the bottom might be useful, e.g. pagination info or some resultset status text.

@yceruto
Copy link
Member

yceruto commented Apr 19, 2018

Also sometimes the table is too big, showing the title in both places (top/bottom) would be nice, avoids scrolling.

@maidmaid maidmaid force-pushed the console-table-title branch from 47183ed to 57da8ae Compare April 24, 2018 08:19
@maidmaid
Copy link
Contributor Author

As suggested, I added setHeaderTitle() and setFooterTitle() methods:

screenshot from 2018-04-24 10-21-30

@maidmaid maidmaid force-pushed the console-table-title branch from 57da8ae to 8a6396f Compare April 25, 2018 04:12
return $this;
}

public function setfooterTitle(?string $title): self
Copy link
Contributor

Choose a reason for hiding this comment

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

s/footer/Footer/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

return $this;
}

public function setfooterTitle(?string $title): self
Copy link
Contributor

Choose a reason for hiding this comment

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

method should be renamed to setFooterTitle

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@maidmaid maidmaid force-pushed the console-table-title branch from 8a6396f to 63f973f Compare July 3, 2018 16:42
/**
* Replaces text within a portion of a string.
*/
public static function substr_replace(string $string, string $replacement, int $start, int $length = null): string
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this actually complies with CS ... should we prefer substrReplace() (given strlenWithoutDecoration also)

but i understand the reasoning.. i don't mind really :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe, someone like @nicolas-grekas could help us here ?

Copy link
Member

Choose a reason for hiding this comment

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

the logic is used once: let's inline it instead of creating a new public method

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inlined!

return substr_replace($string, $replacement, $start, $length);
}

return mb_substr($string, 0, $start).$replacement.mb_substr($string, $start + $length);
Copy link
Contributor

Choose a reason for hiding this comment

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

from http://php.net/manual/en/function.substr-replace.php

i think we should default $length = strlen($string); here as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That looks working as expected https://3v4l.org/C5Nhs, do I miss something ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like PHP treats null as int(0) 🤔

https://3v4l.org/f02sM

@maidmaid maidmaid force-pushed the console-table-title branch from 63f973f to 957568f Compare July 11, 2018 08:47
@@ -17,6 +17,7 @@
* Helper is the base class for all helper classes.
*
* @author Fabien Potencier <[email protected]>
* @author Dany Maillard <[email protected]>
Copy link
Member

Choose a reason for hiding this comment

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

can be removed now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, I just removed it.

@maidmaid maidmaid force-pushed the console-table-title branch from 957568f to 6bf9eeb Compare July 11, 2018 09:51
@fabpot
Copy link
Member

fabpot commented Jul 11, 2018

Thank you @maidmaid.

@fabpot fabpot merged commit 6bf9eeb into symfony:master Jul 11, 2018
fabpot added a commit that referenced this pull request Jul 11, 2018
This PR was merged into the 4.2-dev branch.

Discussion
----------

[Console] Add title table

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | /
| License       | MIT
| Doc PR        | /

Why not just add a title to console tables? Inspired by the famous file manager [Midnight Commander](https://en.wikipedia.org/wiki/Midnight_Commander).

![screenshot from 2018-04-15 20-16-26](https://user-images.githubusercontent.com/4578773/38777361-eb6aaa72-40e9-11e8-8ae4-055fe80272c3.png)

Commits
-------

6bf9eeb Add title table
@maidmaid maidmaid deleted the console-table-title branch July 11, 2018 10:00
@javiereguiluz
Copy link
Member

I've created symfony/symfony-docs#10294 to not forget about adding the docs for this feature. @maidmaid it'd be great if you could help us with the docs for this feature. Thanks!

@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
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.

8 participants