33namespace PHPStan \Command ;
44
55use OndraM \CiDetector \CiDetector ;
6+ use Symfony \Component \Console \Helper \Helper ;
67use Symfony \Component \Console \Helper \ProgressBar ;
78use Symfony \Component \Console \Helper \TableSeparator ;
89use Symfony \Component \Console \Input \InputInterface ;
1314use function explode ;
1415use function implode ;
1516use function sprintf ;
16- use function str_starts_with ;
1717use function strlen ;
18- use function wordwrap ;
1918use const DIRECTORY_SEPARATOR ;
2019
2120final class ErrorsConsoleStyle extends SymfonyStyle
@@ -55,19 +54,15 @@ public function table(array $headers, array $rows): void
5554$ terminalWidth = (new Terminal ())->getWidth () - 2 ;
5655$ maxHeaderWidth = strlen ($ headers [0 ]);
5756foreach ($ rows as $ row ) {
58- $ length = strlen ($ row [0 ]);
57+ $ length = Helper::width (Helper::removeDecoration ($ this ->getFormatter (), $ row [0 ]));
58+
5959if ($ maxHeaderWidth !== 0 && $ length <= $ maxHeaderWidth ) {
6060continue ;
6161}
6262
6363$ maxHeaderWidth = $ length ;
6464}
6565
66- // manual wrapping could be replaced with $table->setColumnMaxWidth()
67- // but it's buggy for <href> lines
68- // https://github.com/symfony/symfony/issues/45520
69- // https://github.com/symfony/symfony/issues/45521
70- $ headers = $ this ->wrap ($ headers , $ terminalWidth , $ maxHeaderWidth );
7166foreach ($ headers as $ i => $ header ) {
7267$ newHeader = [];
7368foreach (explode ("\n" , $ header ) as $ h ) {
@@ -77,58 +72,16 @@ public function table(array $headers, array $rows): void
7772$ headers [$ i ] = implode ("\n" , $ newHeader );
7873}
7974
80- foreach ($ rows as $ i => $ row ) {
81- $ rows [$ i ] = $ this ->wrap ($ row , $ terminalWidth , $ maxHeaderWidth );
82- }
83-
8475$ table = $ this ->createTable ();
76+ // -5 because there are 5 padding spaces: One on each side of the table, one on each side of a cell and one between columns.
77+ $ table ->setColumnMaxWidth (1 , $ terminalWidth - $ maxHeaderWidth - 5 );
8578array_unshift ($ rows , $ headers , new TableSeparator ());
8679$ table ->setRows ($ rows );
8780
8881$ table ->render ();
8982$ this ->newLine ();
9083}
9184
92- /**
93- * @param string[] $rows
94- * @return string[]
95- */
96- private function wrap (array $ rows , int $ terminalWidth , int $ maxHeaderWidth ): array
97- {
98- foreach ($ rows as $ i => $ column ) {
99- $ columnRows = explode ("\n" , $ column );
100- foreach ($ columnRows as $ k => $ columnRow ) {
101- if (str_starts_with ($ columnRow , '✏️ ' )) {
102- continue ;
103- }
104- $ wrapped = wordwrap (
105- $ columnRow ,
106- $ terminalWidth - $ maxHeaderWidth - 5 ,
107- );
108- if (str_starts_with ($ columnRow , '💡 ' )) {
109- $ wrappedLines = explode ("\n" , $ wrapped );
110- $ newWrappedLines = [];
111- foreach ($ wrappedLines as $ l => $ line ) {
112- if ($ l === 0 ) {
113- $ newWrappedLines [] = $ line ;
114- continue ;
115- }
116-
117- $ newWrappedLines [] = ' ' . $ line ;
118- }
119- $ columnRows [$ k ] = implode ("\n" , $ newWrappedLines );
120- } else {
121- $ columnRows [$ k ] = $ wrapped ;
122- }
123-
124- }
125-
126- $ rows [$ i ] = implode ("\n" , $ columnRows );
127- }
128-
129- return $ rows ;
130- }
131-
13285public function createProgressBar (int $ max = 0 ): ProgressBar
13386{
13487$ this ->progressBar = parent ::createProgressBar ($ max );
0 commit comments