Updating a progress bar is fairly simple in conceptual terms, but it always takes a slightly different bit of code to do it right:
- What if the sequence is backwards?
- What if you're incrementing in units of 2, or 3, or 10?
- How big is the progress jump?
It makes sense to abstract the process and stick it in a struct. The progressBarUpdater does just that, and really is as simple as:
- Create a struct instance
- Initialize it with a reference to a progress bar, and the known start and end values of the calculation
- Call update() for each loop iteration.
In code, starting with a 2-line initialization, it looks like this:
pbu = progressBarUpdater() pbu.init pb1 startValue endValue
And to update the progress bar, this is the only command you need to place in the loop:
The pbUpdater struct already knows everything it needs to know, so it's just one call.