21
21
use Symfony \Component \Console \Helper \TableCell ;
22
22
use Symfony \Component \Console \Helper \TableSeparator ;
23
23
use Symfony \Component \Console \Input \InputInterface ;
24
+ use Symfony \Component \Console \Output \ConsoleOutputInterface ;
24
25
use Symfony \Component \Console \Output \OutputInterface ;
25
26
use Symfony \Component \Console \Output \TrimmedBufferOutput ;
26
27
use Symfony \Component \Console \Question \ChoiceQuestion ;
@@ -38,6 +39,7 @@ class SymfonyStyle extends OutputStyle
38
39
public const MAX_LINE_LENGTH = 120 ;
39
40
40
41
private $ input ;
42
+ private $ output ;
41
43
private $ questionHelper ;
42
44
private $ progressBar ;
43
45
private $ lineLength ;
@@ -51,7 +53,7 @@ public function __construct(InputInterface $input, OutputInterface $output)
51
53
$ width = (new Terminal ())->getWidth () ?: self ::MAX_LINE_LENGTH ;
52
54
$ this ->lineLength = min ($ width - (int ) (\DIRECTORY_SEPARATOR === '\\' ), self ::MAX_LINE_LENGTH );
53
55
54
- parent ::__construct ($ output );
56
+ parent ::__construct ($ this -> output = $ output );
55
57
}
56
58
57
59
/**
@@ -186,15 +188,12 @@ public function caution($message)
186
188
*/
187
189
public function table (array $ headers , array $ rows )
188
190
{
189
- $ style = clone Table::getStyleDefinition ('symfony-style-guide ' );
190
- $ style ->setCellHeaderFormat ('<info>%s</info> ' );
191
-
192
- $ table = new Table ($ this );
193
- $ table ->setHeaders ($ headers );
194
- $ table ->setRows ($ rows );
195
- $ table ->setStyle ($ style );
191
+ $ this ->createTable ()
192
+ ->setHeaders ($ headers )
193
+ ->setRows ($ rows )
194
+ ->render ()
195
+ ;
196
196
197
- $ table ->render ();
198
197
$ this ->newLine ();
199
198
}
200
199
@@ -203,16 +202,13 @@ public function table(array $headers, array $rows)
203
202
*/
204
203
public function horizontalTable (array $ headers , array $ rows )
205
204
{
206
- $ style = clone Table::getStyleDefinition ('symfony-style-guide ' );
207
- $ style ->setCellHeaderFormat ('<info>%s</info> ' );
208
-
209
- $ table = new Table ($ this );
210
- $ table ->setHeaders ($ headers );
211
- $ table ->setRows ($ rows );
212
- $ table ->setStyle ($ style );
213
- $ table ->setHorizontal (true );
205
+ $ this ->createTable ()
206
+ ->setHorizontal (true )
207
+ ->setHeaders ($ headers )
208
+ ->setRows ($ rows )
209
+ ->render ()
210
+ ;
214
211
215
- $ table ->render ();
216
212
$ this ->newLine ();
217
213
}
218
214
@@ -228,10 +224,6 @@ public function horizontalTable(array $headers, array $rows)
228
224
*/
229
225
public function definitionList (...$ list )
230
226
{
231
- $ style = clone Table::getStyleDefinition ('symfony-style-guide ' );
232
- $ style ->setCellHeaderFormat ('<info>%s</info> ' );
233
-
234
- $ table = new Table ($ this );
235
227
$ headers = [];
236
228
$ row = [];
237
229
foreach ($ list as $ value ) {
@@ -252,13 +244,7 @@ public function definitionList(...$list)
252
244
$ row [] = current ($ value );
253
245
}
254
246
255
- $ table ->setHeaders ($ headers );
256
- $ table ->setRows ([$ row ]);
257
- $ table ->setHorizontal ();
258
- $ table ->setStyle ($ style );
259
-
260
- $ table ->render ();
261
- $ this ->newLine ();
247
+ $ this ->horizontalTable ($ headers , [$ row ]);
262
248
}
263
249
264
250
/**
@@ -349,6 +335,16 @@ public function createProgressBar(int $max = 0)
349
335
return $ progressBar ;
350
336
}
351
337
338
+ /**
339
+ * @see ProgressBar::iterate()
340
+ */
341
+ public function progressIterate (iterable $ iterable , int $ max = null ): iterable
342
+ {
343
+ yield from $ this ->createProgressBar ()->iterate ($ iterable , $ max );
344
+
345
+ $ this ->newLine (2 );
346
+ }
347
+
352
348
/**
353
349
* @return mixed
354
350
*/
@@ -421,6 +417,15 @@ public function getErrorStyle()
421
417
return new self ($ this ->input , $ this ->getErrorOutput ());
422
418
}
423
419
420
+ public function createTable (): Table
421
+ {
422
+ $ output = $ this ->output instanceof ConsoleOutputInterface ? $ this ->output ->section () : $ this ->output ;
423
+ $ style = clone Table::getStyleDefinition ('symfony-style-guide ' );
424
+ $ style ->setCellHeaderFormat ('<info>%s</info> ' );
425
+
426
+ return (new Table ($ output ))->setStyle ($ style );
427
+ }
428
+
424
429
private function getProgressBar (): ProgressBar
425
430
{
426
431
if (!$ this ->progressBar ) {
0 commit comments