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