Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7a30e50

Browse files
committed
[Console] added support for multiline formats in ProgressBar
1 parent1aa7b8c commit7a30e50

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

‎src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ProgressBar
4848
private$percent;
4949
private$lastMessagesLength;
5050
private$barCharOriginal;
51+
private$formatLineCount;
5152

5253
staticprivate$formatters;
5354

@@ -224,6 +225,7 @@ public function getProgressCharacter()
224225
publicfunctionsetFormat($format)
225226
{
226227
$this->format =$format;
228+
$this->formatLineCount =substr_count($format,"\n");
227229
}
228230

229231
/**
@@ -248,7 +250,7 @@ public function start()
248250
$this->barCharOriginal ='';
249251

250252
if (null ===$this->format) {
251-
$this->format =$this->determineBestFormat();
253+
$this->setFormat($this->determineBestFormat());
252254
}
253255

254256
if (!$this->max) {
@@ -351,7 +353,7 @@ public function display()
351353
*/
352354
publicfunctionclear()
353355
{
354-
$this->overwrite('');
356+
$this->overwrite(str_repeat("\n",$this->formatLineCount));
355357
}
356358

357359
/**
@@ -364,12 +366,17 @@ private function overwrite($message)
364366
$length = Helper::strlen($message);
365367

366368
// append whitespace to match the last line's length
369+
// FIXME: on each line!!!!!
370+
// FIXME: max of each line for lastMessagesLength or an array?
367371
if (null !==$this->lastMessagesLength &&$this->lastMessagesLength >$length) {
368372
$message =str_pad($message,$this->lastMessagesLength,"\x20",STR_PAD_RIGHT);
369373
}
370374

371-
//carriage return
375+
//move back to the beginning of the progress bar before redrawing it
372376
$this->output->write("\x0D");
377+
if ($this->formatLineCount) {
378+
$this->output->write(sprintf("\033[%dA",$this->formatLineCount));
379+
}
373380
$this->output->write($message);
374381

375382
$this->lastMessagesLength = Helper::strlen($message);

‎src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,26 @@ public function testAddingPlaceholderFormatter()
319319
);
320320
}
321321

322+
publicfunctiontestMultilineFormat()
323+
{
324+
$bar =newProgressBar($output =$this->getOutputStream(),3);
325+
$bar->setFormat("%bar%\nfoobar");
326+
327+
$bar->start();
328+
$bar->advance();
329+
$bar->clear();
330+
$bar->finish();
331+
332+
rewind($output->getStream());
333+
$this->assertEquals(
334+
$this->generateOutput(">---------------------------\nfoobar").
335+
$this->generateOutput("=========>------------------\nfoobar").
336+
$this->generateOutput("\n").
337+
$this->generateOutput("============================\nfoobar"),
338+
stream_get_contents($output->getStream())
339+
);
340+
}
341+
322342
protectedfunctiongetOutputStream($decorated =true)
323343
{
324344
returnnewStreamOutput(fopen('php://memory','r+',false), StreamOutput::VERBOSITY_NORMAL,$decorated);
@@ -334,6 +354,8 @@ protected function generateOutput($expected)
334354

335355
$this->lastMessagesLength =strlen($expectedout);
336356

337-
return"\x0D".$expectedout;
357+
$count =substr_count($expected,"\n");
358+
359+
return"\x0D".($count ?sprintf("\033[%dA",$count) :'').$expectedout;
338360
}
339361
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp