3
3
namespace PHPStan \Command ;
4
4
5
5
use OndraM \CiDetector \CiDetector ;
6
+ use Symfony \Component \Console \Helper \Helper ;
6
7
use Symfony \Component \Console \Helper \ProgressBar ;
7
8
use Symfony \Component \Console \Helper \TableSeparator ;
8
9
use Symfony \Component \Console \Input \InputInterface ;
13
14
use function explode ;
14
15
use function implode ;
15
16
use function sprintf ;
16
- use function str_starts_with ;
17
17
use function strlen ;
18
- use function wordwrap ;
19
18
use const DIRECTORY_SEPARATOR ;
20
19
21
20
final class ErrorsConsoleStyle extends SymfonyStyle
@@ -55,19 +54,15 @@ public function table(array $headers, array $rows): void
55
54
$ terminalWidth = (new Terminal ())->getWidth () - 2 ;
56
55
$ maxHeaderWidth = strlen ($ headers [0 ]);
57
56
foreach ($ rows as $ row ) {
58
- $ length = strlen ($ row [0 ]);
57
+ $ length = Helper::width (Helper::removeDecoration ($ this ->getFormatter (), $ row [0 ]));
58
+
59
59
if ($ maxHeaderWidth !== 0 && $ length <= $ maxHeaderWidth ) {
60
60
continue ;
61
61
}
62
62
63
63
$ maxHeaderWidth = $ length ;
64
64
}
65
65
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 );
71
66
foreach ($ headers as $ i => $ header ) {
72
67
$ newHeader = [];
73
68
foreach (explode ("\n" , $ header ) as $ h ) {
@@ -77,58 +72,16 @@ public function table(array $headers, array $rows): void
77
72
$ headers [$ i ] = implode ("\n" , $ newHeader );
78
73
}
79
74
80
- foreach ($ rows as $ i => $ row ) {
81
- $ rows [$ i ] = $ this ->wrap ($ row , $ terminalWidth , $ maxHeaderWidth );
82
- }
83
-
84
75
$ 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 );
85
78
array_unshift ($ rows , $ headers , new TableSeparator ());
86
79
$ table ->setRows ($ rows );
87
80
88
81
$ table ->render ();
89
82
$ this ->newLine ();
90
83
}
91
84
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
-
132
85
public function createProgressBar (int $ max = 0 ): ProgressBar
133
86
{
134
87
$ this ->progressBar = parent ::createProgressBar ($ max );
0 commit comments