Fixed segfault when terminal small enough to cause label cutoff and refactored drawing code #17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There is currently a bug in progressbar where it will crash if resized too small when using a label that is a string literal (and thus modifiable memory). Really though, even though this bug is caused by using a string literal for the label, it shows a problem with how the progressbar views the label string. It either needs to take ownership of it, meaning that is responsible for memory management of it once the user has provided it (which requires the user to pass a malloc'd string and not a literal...), or it needs to leave the memory management of the label up to the caller.
The second approach seems more reasonable so that we can avoid unnecessary copying of strings or putting a burden on the user, but this means that the amount of the string that should be rendered must be either computed or stored in the bar. In other words, we can't
str[idx] = 0
a string we don't own.To this end, I started reworking how the progressbar handles rendering of the label, and I might have gotten a bit carried away and ended up refactoring most of the drawing code.
Technical changes:
progressbar
no longer modifies the given label at all. Also, it is now explicitly documented that the user is responsible for ensuring that the label continues to exist throughout the existence of the progressbar.progressbar
no longer stores the terminal type. That's an implementation detail of rendering, andstruct progressbar
is responsible for modeling a progressbar.step
andsteps
are no longer stored on theprogressbar
. Again, they're artifacts of rendering, and they do not belong in theprogressbar
object. They are now calculated during rendering.#define
macros were converted to beenum
-style constants. This allows for better typesafety and a higher likelihood that the symbols will have names in a debugger.progressbar_draw
is no longer exposed in the header or exported in the source. This is a backward compatibility breaking change. Anyone who used theprogressbar_draw
method will have to alter their code to be compatible with new versions ofprogressbar
if this PR is accepted. However, the method does not make much sense to call directly anyway, and it's documented as internal, so I'm hoping this is acceptable.progressbar_draw
no longer takes in an ETA but rather calculates it itself. This allowed for removing a lot of replicated code at various calling sites to calculate elapsed time, items remaining, etc.progressbar
no longer requires a buffer to render. Instead, it is rendered straight tostderr
.Improvements in functionality: