8000 [Console] Improve the helpfulness of %remaining% in Progress Bar · Issue #49927 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Improve the helpfulness of %remaining% in Progress Bar #49927

New issue

Have a question 8000 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
robjbrain opened this issue Apr 4, 2023 · 5 comments · Fixed by #50537
Closed

[Console] Improve the helpfulness of %remaining% in Progress Bar #49927

robjbrain opened this issue Apr 4, 2023 · 5 comments · Fixed by #50537
Labels

Comments

@robjbrain
Copy link

Description

At present the remaining time on the progress bar is not that helpful. If the remaining time is between 24 and 47 hours it will just read "1 days remaining".

Similarly if the time remaining is 1 hour 55 minutes, the text will actually display "1 hour remaining".

Obviously this isn't that helpful when planning around a process finishing.

In all my projects I now add this:

ProgressBar::setPlaceholderFormatterDefinition('remaining', function($bar) {
            $remaining = $bar->getRemaining();
            $hours = floor($remaining / 3600);
            $minutes = floor(($remaining / 60) % 60);
            $seconds = $remaining % 60;
            return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
        });

With this, when using the format: %current%/%max% [%bar%] %percent:3s%% %elapsed:6s% taken %remaining:-6s% remaining, I will get the following:

This: 5/169200 [>---------------------------] 0% 5 secs taken 46:59:55 remaining

Instead of this: 5/169200 [>---------------------------] 0% 5 secs taken 1 day remaining

Or in the case of a 1 hour 48 minute process:

This: 5/6500 [>---------------------------] 0% 5 secs taken 01:48:15 remaining

Instead of this: 5/6500 [>---------------------------] 0% 5 secs taken 1 hr remaining

This could be implemented by updating Symfony\Component\Console\Helper

https://github.com/symfony/console/blob/3582d68a64a86ec25240aaa521ec8bc2342b369b/Helper/Helper.php#L91

To use the same logic as above. Although this would have the side effect of changing all dates. So it would in fact look like this:

5/6500 [>---------------------------] 0% 00:00:05 taken 01:48:15 remaining

I don't have an issue with that, but perhaps it's not desired?

I can create a PR if this is okay, but i've never contributed to Symfony before so figured I would start by creating an issue.

Example

No response

@GromNaN
Copy link
Member
GromNaN commented Apr 4, 2023

That's a good idea, I'm also frustrated by the lack of precision of this remaining time display. The proposed format doesn't tell the unit, what do you think of adding the sub-unit only when the main value has a single digit.

Example:

14 mins
1 hr 55 mins
9 hrs 55 mins
10 hrs
3 days 2 hrs

The PR will have to target next minor release branch (2 weeks left for 6.3). For the moment, I don't know if it will be considered as a breaking change, you can start by creating a new method.


8000
@robjbrain
Copy link
Author

The sub unit could be a good solution.

Why do you say a new method would be needed? I think just changing the Helper@formatTime method should be fine.

@GromNaN
Copy link
Member
GromNaN commented Apr 5, 2023

Why do you say a new method would be needed? I think just changing the Helper@formatTime method should be fine.

Depending on how people are using this public function in their code, changing the behavior might break things. In Symfony we are very attentive to provide a smooth upgrade path.

I proposed a new method, but the solution can ba a new argument on the existing method.

@robjbrain
Copy link
Author

I guess there are a few options:

One
Add a a new placeholder such as %remaining:exact%

Two
We could also add %remaining:days% %remaining:hours% %remaining:minutes% %remaining:seconds% to add more control. This would be a non breaking change and would allow for multiple formats 0 Days 23:59:59 formatting or 0 Days 23 hours 59 minutes 59 seconds formatting. The only issue is that it would be overly verbose e.g. "0 Days 0 Hours 0 Minutes 27 Seconds"

Three
Add a second parameter to Helper@formatTime called $units.

    public static function formatTime(int|float $secs, int $units = 1)
    {
        static $timeFormats = [
            [0, '< 1 sec'],
            [1, '1 sec'],
            [2, 'secs', 1],
            [60, '1 min'],
            [120, 'mins', 60],
            [3600, '1 hr'],
            [7200, 'hrs', 3600],
            [86400, '1 day'],
            [172800, 'days', 86400],
        ];

        $response = [];

        foreach ($timeFormats as $index => $format) {
            if ($secs >= $format[0]) {
                if ((isset($timeFormats[$index + 1]) && $secs < $timeFormats[$index + 1][0])
                    || $index == \count($timeFormats) - 1
                ) {
                    if (2 == \count($format)) {
                        return $format[1];
                    }

                    $response[] = floor($secs / $format[2]).' '.$format[1];
                    if (count($response) === $units) {
                        break;
                    }
                }
            }
        }

       return implode(' ', $response);
    }

With this one the debate would be about the default value for $units. 2 seems a sensible default to me but would be a breaking change, so we may have to go with 1.

However then the question is how to modify the number of units within an application.

Perhaps %remaining:1% or %remaining:2% would be an option.

That would involve drastically modifying this method to handle options on the key.
https://github.com/symfony/console/blob/3582d68a64a86ec25240aaa521ec8bc2342b369b/Helper/ProgressBar.php#L519


"One" seems the easiest option to add in the short term.

@carsonbot
Copy link

Thank you for this issue.
There has not been a lot of activity here for a while. Has this been resolved?

@wouterj wouterj removed the Stalled label Oct 8, 2023
weaverryan added a commit to symfony-tools/carsonbot that referenced this issue Oct 8, 2023
This PR was merged into the master branch.

Discussion
----------

Do not ping stale issues with a linked PR

Filters out issues like symfony/symfony#49927 , where a PR already exists to solve it.

Commits
-------

1320297 Do not ping stale issues with a linked PR
@fabpot fabpot closed this as completed Oct 10, 2023
fabpot added a commit that referenced this issue Oct 10, 2023
…mes (maxbeckers)

This PR was merged into the 6.4 branch.

Discussion
----------

[Console] Add placeholders to ProgressBar for exact times

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #49927
| License       | MIT
| Doc PR        | symfony/symfony-docs#... TBD

This is an idea for exact times using ProgressBar based on the idea of `@GromNaN` in the issue  #49927.

Open to discuss this first way of implementing it.

I'll create the docs PR when the feature is agreed and there won't come up any bigger changes.

The Idea is to show the exact time in seconds, the ProgressBar will run / is running.

Commits
-------

b553dd9 [Console] Add Placeholders to ProgressBar for exactly times
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
0