Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Add progress indicator helper #12119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed

[Console] Add progress indicator helper #12119

wants to merge 8 commits into from

Conversation

kbond
Copy link
Member

@kbond kbond commented Oct 3, 2014

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR todo

This is an alternative to the ProgressBar helper without a max inspired by the npm cli. You can use the ProgressBar but IMO it doesn't look great or fallback nicely in non-ansi environments.

A lot of work needs to be done still but thought I would get some comments on this idea.

Example code
$progress = new ProgressIndicator($output);
$progress->start('Starting...');

for ($i = 0; $i < 100; $i++) {
    usleep(25000);
    $progress->advance();

    switch ($i) {
        case 20:
            $progress->setMessage('Just started...');
            break;
        case 50:
            $progress->setMessage('Half way...');
            break;
        case 90:
            $progress->setMessage('Almost Done...');
            break;
    }
}

$progress->finish('Done.');
Screenshot

output

@Nicofuma
Copy link
Contributor

Nicofuma commented Oct 5, 2014

It could be helpfull

@stof stof added Console RFC RFC = Request For Comments (proposals about features that you want to be discussed) labels Oct 6, 2014
@fabpot
Copy link
Member

fabpot commented Sep 14, 2015

@kbond Still willing to finish this one? I think this is indeed a good idea.

@kbond
Copy link
Member Author

kbond commented Sep 14, 2015

@fabpot sure, I rebased and fixed some CS issues.

One thing I noticed with the animation is it sometimes looks strange - you can see it in the above gif after the "Half way..." message it just alternates between / and \ only. I wonder if we should use some kind of timer to ensure the animation is smooth...?

Anyway, anything that needs to be changed/fixed, let me know.

@keradus
Copy link
Member

keradus commented Sep 23, 2015

Tests fail on Windows: https://ci.appveyor.com/project/fabpot/symfony/build/1.0.846/job/vcf2cxf0l2wxw52s#L550 , could you help with that?

@kbond
Copy link
Member Author

kbond commented Sep 23, 2015

It can't be from the PR - I haven't added any tests yet.

@fabpot
Copy link
Member

fabpot commented Oct 6, 2015

@kbond Can you add some tests?

@kbond
Copy link
Member Author

kbond commented Oct 6, 2015

@fabpot added.

@kbond
Copy link
Member Author

kbond commented Oct 9, 2015

I rebased to master. The console component's tests are failing but is unrelated to this PR.

$this->indicatorCurrent = 0;
} else {
++$this->indicatorCurrent;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just do a simple incrementation here (++$this->indicatorCurrent;), and then use a modulo where it is used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!

@kbond
Copy link
Member Author

kbond commented Oct 9, 2015

I added a speed trap so the indicator doesn't change more than once per 100 (default) milliseconds.

*/
public function getStartTime()
{
return $this->startTime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start time can probably be removed altogether as it's not used anywhere. It's useful in the progress bar as we display time information, but as this is not the case here, I don't see how it can be useful. Or would it be useful to add the possibility to add the elapsed time in the format?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind.

@kbond
Copy link
Member Author

kbond commented Oct 13, 2015

To make the indicator immutable, should I remove some public setters? setFormat, setIndicatorChangeInterval, setIndicatorValues could be moved to the constructor. The user could potentially change these values during rendering and that could cause problems.

@fabpot
Copy link
Member

fabpot commented Oct 13, 2015

👍 for making it immutable; if the list of arguments on the constructor is too large, you can also keep the setter but throw an exception when used after the indicator is started.

@stof
Copy link
Member

stof commented Oct 13, 2015

I agree with @fabpot. changing the display in the middle does not make much sense to me (and could even cause issues).

@kbond
Copy link
Member Author

kbond commented Oct 13, 2015

Ok, I made it immutable and ensured it can be used multiple times per instance.

Unless there are any other changes, this is good to go.

* Gets the current indicator message.
*
* @return string|null
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is also internal, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just getMessage - I will mark as such. Unless you want me to remove setMessage and just use advance to set a new message.

@fabpot
Copy link
Member

fabpot commented Oct 13, 2015

👍

@fabpot
Copy link
Member

fabpot commented Oct 19, 2015

@kbond Looks like the tests on Windows do not pass.

@kbond
Copy link
Member Author

kbond commented Oct 19, 2015

Damn, ok I will have a look. It's hard to see what the problem is from the output.

@kbond
Copy link
Member Author

kbond commented Oct 19, 2015

Tests on Windows are passing now!

@fabpot fabpot changed the title [PoC][Console] Add progress indicator helper [Console] Add progress indicator helper Oct 19, 2015
@fabpot
Copy link
Member

fabpot commented Oct 19, 2015

Thank you @kbond.

@fabpot fabpot closed this Oct 19, 2015
fabpot added a commit that referenced this pull request Oct 19, 2015
This PR was squashed before being merged into the 3.0-dev branch (closes #12119).

Discussion
----------

[Console] Add progress indicator helper

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | *todo*

This is an alternative to the `ProgressBar` helper *without a max* inspired by the `npm` cli.  You can use the `ProgressBar` but IMO it doesn't look great or fallback nicely in non-ansi environments.

A lot of work needs to be done still but thought I would get some comments on this idea.

##### Example code

```php
$progress = new ProgressIndicator($output);
$progress->start('Starting...');

for ($i = 0; $i < 100; $i++) {
    usleep(25000);
    $progress->advance();

    switch ($i) {
        case 20:
            $progress->setMessage('Just started...');
            break;
        case 50:
            $progress->setMessage('Half way...');
            break;
        case 90:
            $progress->setMessage('Almost Done...');
            break;
    }
}

$progress->finish('Done.');
```

##### Screenshot

![output](https://cloud.githubusercontent.com/assets/127811/4511167/95302026-4b31-11e4-824e-5cb26f96e4cb.gif)

Commits
-------

abf389c [Console] Add progress indicator helper
@kbond
Copy link
Member Author

kbond commented Oct 31, 2015

I noticed this was merged into 3.0 but not 2.8 - I thought 3.0 was not getting any new features that are not in 2.8.

@xabbuh
Copy link
Member

xabbuh commented Nov 1, 2015

@kbond Looks like a mistake to me (see #16409).

fabpot added a commit that referenced this pull request Nov 4, 2015
This PR was merged into the 2.8 branch.

Discussion
----------

[Console] Add progress indicator helper

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12119
| License       | MIT
| Doc PR        |

Commits
-------

be0a364 [Console] Add progress indicator helper
@fabpot fabpot mentioned this pull request Nov 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Console RFC RFC = Request For Comments (proposals about features that you want to be discussed)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants