ctongfei / progressbar

Terminal-based progress bar for Java / JVM
http://tongfei.me/progressbar/
MIT License
1.07k stars 102 forks source link

How to show an option to stop the progress after the progress has started #132

Open shirishpandharikar opened 2 years ago

shirishpandharikar commented 2 years ago

We have a requirement where we monitor the progress of background tasks via events. The progress bar works just fine. We have a requirement where in we also need to show the user an option to stop the background task. For this we want to show the user a prompt on the console E.g. Press X to exit: after the progress is displayed. How can this be achieved? The progress bar seems to take over and the user is not able to input anything. Below is how we expect it

0% [>                                  ]    243/100000  (0:00:12 / 1:23:51)
Press X to exit: 

If the user inputs X and presses enter the program will stop the background task and exit. Any help here would be really appreciated.

ctongfei commented 2 years ago

You can do something like the following, which I think should work (let me know if it does not work):

progressBar.pause();
boolean exit = /* prompt the user response */;
if (exit) progressBar.close() else progressBar.resume();
shirishpandharikar commented 2 years ago

You can do something like the following, which I think should work (let me know if it does not work):

progressBar.pause();
boolean exit = /* prompt the user response */;
if (exit) progressBar.close() else progressBar.resume();

Thanks for the quick reply. I think I did not explain the requirement correctly. The progressbar should show the progress as long as the task is running. While the progressbar is being updated continuously at some interval the user should also be shown an option to stop the background task via a prompt. Here is what I understand from your solution:

  1. Pause the progressbar before showing the prompt to the user
  2. Accept the input from the user
  3. If the input matches the stop/cancel command close the progressbar
  4. If the input does not match resume the progress

If the task is running and the user does not give any input the above solution will not show any progress and just wait. We want the progress bar to continue as long the user does not input anything.

ctongfei commented 2 years ago

Thanks. I now understand your request. This is unfortunately not possible right now: the constant printing of the progress bar will interfere with normal output stream. To fix this one need to track the coordinate location of the progress bar on the screen while normal text is printing -- which is difficult. It may be supported in a future version.