Progress Bar¶
| Spice | |
|---|---|
ProgressBar struct¶
A single-line CLI progress bar with percentage, counter, elapsed time and ETA.
The bar redraws itself in place by returning the cursor to the start of the line, so nothing else should be printed to stdout while it is active. Output looks like this:
Downloading [================> ] 42% 420/1000 00:12 ETA 00:17
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct a progress bar
Parameters
| Name | Type | Description |
|---|---|---|
total |
unsigned long |
Number of steps that make up 100 % |
label |
string |
Optional label, printed in front of the bar (default: "") |
width |
unsigned int |
Number of characters between the brackets (default: DEFAULT_PROGRESS_BAR_WIDTH) |
Methods¶
setStyle¶
| Spice | |
|---|---|
Customize the characters the bar is drawn with
Parameters
| Name | Type | Description |
|---|---|---|
fillChar |
char |
Character for the completed part |
headChar |
char |
Character at the tip of the completed part |
emptyChar |
char |
Character for the remaining part |
setShowCounter¶
| Spice | |
|---|---|
Show or hide the current/total counter, e.g. if the steps are no meaningful unit for the user
Parameters
| Name | Type | Description |
|---|---|---|
showCounter |
bool |
Show the counter or not |
setLabel¶
| Spice | |
|---|---|
Change the label, printed in front of the bar. Takes effect with the next redraw.
Parameters
| Name | Type | Description |
|---|---|---|
label |
string |
New label |
setTotal¶
| Spice | |
|---|---|
Change the number of steps that make up 100 %, e.g. when more work is discovered on the way. The current progress is clamped to the new total.
Parameters
| Name | Type | Description |
|---|---|---|
total |
unsigned long |
New total |
start¶
| Spice | |
|---|---|
Start the progress bar. This resets the clock the ETA is calculated from and draws the initial bar. Calling it is optional, the first update starts the bar implicitly.
update¶
| Spice | |
|---|---|
Set the progress to an absolute value. Values above the total are clamped.
Parameters
| Name | Type | Description |
|---|---|---|
current |
unsigned long |
Number of completed steps |
setCurrent¶
| Spice | |
|---|---|
Set the progress to an absolute value without drawing the bar. Values above the total are clamped. Useful to render the bar yourself via format().
Parameters
| Name | Type | Description |
|---|---|---|
current |
unsigned long |
Number of completed steps |
tick¶
| Spice | |
|---|---|
Advance the progress by the given number of steps
Parameters
| Name | Type | Description |
|---|---|---|
steps |
unsigned long |
Number of steps completed since the last call (default: 1ul) |
finish¶
| Spice | |
|---|---|
Mark the progress bar as complete, draw it a last time and move the cursor to the next line
redraw¶
| Spice | |
|---|---|
Draw the bar right away, e.g. after changing the label. Does nothing if the bar was not started yet or is finished.
clear¶
| Spice | |
|---|---|
Remove the bar from the console and return the cursor to the start of the line, e.g. to print something else. The next update draws the bar again.
getTotal¶
| Spice | |
|---|---|
Retrieve the total number of steps
Returns: unsigned long — Total steps
getCurrent¶
| Spice | |
|---|---|
Retrieve the number of completed steps
Returns: unsigned long — Completed steps
getProgress¶
| Spice | |
|---|---|
Retrieve the progress as fraction between 0 and 1
Returns: double — Progress fraction
getElapsedMicros¶
| Spice | |
|---|---|
Retrieve the time elapsed since the bar was started
Returns: long — Elapsed time in microseconds
getEtaMicros¶
| Spice | |
|---|---|
Estimate the remaining time, assuming the average rate so far stays constant
Parameters
| Name | Type | Description |
|---|---|---|
elapsedMicros |
long |
Time elapsed since the start |
Returns: long — Estimated remaining time in microseconds, or -1 if no estimate is possible yet
format¶
| Spice | |
|---|---|
Build the progress bar line for a given elapsed time, without drawing it
Parameters
| Name | Type | Description |
|---|---|---|
elapsedMicros |
long |
Time elapsed since the start |
Returns: String — Formatted progress bar line
Functions¶
formatDuration¶
| Spice | |
|---|---|
Format a duration as mm:ss, or hhss if it is one hour or longer
Parameters
| Name | Type | Description |
|---|---|---|
micros |
long |
Duration in microseconds, negative for unknown |
Returns: String — Formatted duration, or --:-- if unknown
isStdoutTerminal¶
| Spice | |
|---|---|
Check whether stdout is attached to a terminal. A progress bar should usually only be shown if it is, because the carriage returns it uses to redraw itself clutter redirected output.
Returns: bool — Terminal or not