44
55use OndraM \CiDetector \CiDetector ;
66use Override ;
7+ use Symfony \Component \Console \Helper \Helper ;
78use Symfony \Component \Console \Helper \ProgressBar ;
89use Symfony \Component \Console \Helper \TableSeparator ;
910use Symfony \Component \Console \Input \InputInterface ;
1415use function explode ;
1516use function implode ;
1617use function sprintf ;
17- use function str_starts_with ;
1818use function strlen ;
19- use function wordwrap ;
2019use const DIRECTORY_SEPARATOR ;
2120
2221final class ErrorsConsoleStyle extends SymfonyStyle
@@ -57,19 +56,15 @@ public function table(array $headers, array $rows): void
5756 $ terminalWidth = (new Terminal ())->getWidth () - 2 ;
5857 $ maxHeaderWidth = strlen ($ headers [0 ]);
5958 foreach ($ rows as $ row ) {
60- $ length = strlen ($ row [0 ]);
59+ $ length = Helper::width (Helper::removeDecoration ($ this ->getFormatter (), $ row [0 ]));
60+
6161 if ($ maxHeaderWidth !== 0 && $ length <= $ maxHeaderWidth ) {
6262 continue ;
6363 }
6464
6565 $ maxHeaderWidth = $ length ;
6666 }
6767
68- // manual wrapping could be replaced with $table->setColumnMaxWidth()
69- // but it's buggy for <href> lines
70- // https://github.com/symfony/symfony/issues/45520
71- // https://github.com/symfony/symfony/issues/45521
72- $ headers = $ this ->wrap ($ headers , $ terminalWidth , $ maxHeaderWidth );
7368 foreach ($ headers as $ i => $ header ) {
7469 $ newHeader = [];
7570 foreach (explode ("\n" , $ header ) as $ h ) {
@@ -79,58 +74,16 @@ public function table(array $headers, array $rows): void
7974 $ headers [$ i ] = implode ("\n" , $ newHeader );
8075 }
8176
82- foreach ($ rows as $ i => $ row ) {
83- $ rows [$ i ] = $ this ->wrap ($ row , $ terminalWidth , $ maxHeaderWidth );
84- }
85-
8677 $ table = $ this ->createTable ();
78+ // -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.
79+ $ table ->setColumnMaxWidth (1 , $ terminalWidth - $ maxHeaderWidth - 5 );
8780 array_unshift ($ rows , $ headers , new TableSeparator ());
8881 $ table ->setRows ($ rows );
8982
9083 $ table ->render ();
9184 $ this ->newLine ();
9285 }
9386
94- /**
95- * @param string[] $rows
96- * @return string[]
97- */
98- private function wrap (array $ rows , int $ terminalWidth , int $ maxHeaderWidth ): array
99- {
100- foreach ($ rows as $ i => $ column ) {
101- $ columnRows = explode ("\n" , $ column );
102- foreach ($ columnRows as $ k => $ columnRow ) {
103- if (str_starts_with ($ columnRow , '✏️ ' )) {
104- continue ;
105- }
106- $ wrapped = wordwrap (
107- $ columnRow ,
108- $ terminalWidth - $ maxHeaderWidth - 5 ,
109- );
110- if (str_starts_with ($ columnRow , '💡 ' )) {
111- $ wrappedLines = explode ("\n" , $ wrapped );
112- $ newWrappedLines = [];
113- foreach ($ wrappedLines as $ l => $ line ) {
114- if ($ l === 0 ) {
115- $ newWrappedLines [] = $ line ;
116- continue ;
117- }
118-
119- $ newWrappedLines [] = ' ' . $ line ;
120- }
121- $ columnRows [$ k ] = implode ("\n" , $ newWrappedLines );
122- } else {
123- $ columnRows [$ k ] = $ wrapped ;
124- }
125-
126- }
127-
128- $ rows [$ i ] = implode ("\n" , $ columnRows );
129- }
130-
131- return $ rows ;
132- }
133-
13487 #[Override]
13588 public function createProgressBar (int $ max = 0 ): ProgressBar
13689 {
0 commit comments