Class ProgressBar
java.lang.Object
me.tongfei.progressbar.ProgressBar
- All Implemented Interfaces:
AutoCloseable
A console-based progress bar with minimal runtime overhead.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ProgressUpdateActionprivate final ProgressStateprivate final ScheduledFuture<?> -
Constructor Summary
ConstructorsConstructorDescriptionProgressBar(String task, long initialMax) Creates a progress bar with the specific taskName name and initial maximum value.ProgressBar(String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, long processed, Duration elapsed, ProgressBarRenderer renderer, ProgressBarConsumer consumer) Deprecated.ProgressBar(String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, PrintStream os, ProgressBarStyle style, String unitName, long unitSize, boolean showSpeed, DecimalFormat speedFormat, ChronoUnit speedUnit, long processed, Duration elapsed) Deprecated.UseProgressBarBuilderinstead. -
Method Summary
Modifier and TypeMethodDescriptionstatic ProgressBarBuilderbuilder()Creates a new builder to customize a progress bar.voidclose()Stops this progress bar, effectively stops tracking the underlying process.longReturns the current progress.Returns the duration that this progress bar has been running after it was resumed.Returns the duration that this progress bar has been running before it was resumed.Returns the extra message at the end of the progress bar.longgetMax()Returns the maximum value of this progress bar.doubleReturns the progress normalized to the interval [0, 1].longgetStart()Returns the instant when the progress bar started.Returns the name of this task.Returns the total duration that this progress bar has been running from start, excluding the period when it has been paused.booleanChecks if the progress bar is indefinite, i.e., its maximum value is unknown.maxHint(long n) Gives a hint to the maximum value of the progress bar.pause()Pauses this current progress.voidrefresh()Prompts the progress bar to refresh.reset()Resets the progress bar to its initial state (where progress equals to 0).resume()Resumes this current progress.setExtraMessage(String msg) Sets the extra message at the end of the progress bar.step()Advances this progress bar by one step.stepBy(long n) Advances this progress bar by a specific amount.stepTo(long n) Advances this progress bar to the specific progress value.static InputStreamwrap(InputStream is, String task) Wraps anInputStreamso that when read, a progress bar is shown to track the reading progress.static InputStreamwrap(InputStream is, ProgressBarBuilder pbb) Wraps anInputStreamso that when read, a progress bar is shown to track the reading progress.static OutputStreamwrap(OutputStream os, String task) Wraps anOutputStreamso that when written, a progress bar is shown to track the writing progress.static OutputStreamwrap(OutputStream os, ProgressBarBuilder pbb) Wraps anOutputStreamso that when written, a progress bar is shown to track the writing progress.static ReaderWraps aReaderso that when read, a progress bar is shown to track the reading progress.static Readerwrap(Reader reader, ProgressBarBuilder pbb) Wraps aReaderso that when read, a progress bar is shown to track the reading progress.static WriterWraps aWriterso that when written, a progress bar is shown to track the writing progress.static Writerwrap(Writer writer, ProgressBarBuilder pbb) Wraps aWriterso that when written, a progress bar is shown to track the writing progress.static <T> Iterable<T> Wraps anIterableso that when iterated, a progress bar is shown to track the traversal progress.static <T> Iterable<T> wrap(Iterable<T> ts, ProgressBarBuilder pbb) Wraps anIterableso that when iterated, a progress bar is shown to track the traversal progress.static <T> Iterator<T> Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.static <T> Iterator<T> wrap(Iterator<T> it, ProgressBarBuilder pbb) Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.static <T> Spliterator<T> wrap(Spliterator<T> sp, String task) Wraps aSpliteratorso that when iterated, a progress bar is shown to track the traversal progress.static <T> Spliterator<T> wrap(Spliterator<T> sp, ProgressBarBuilder pbb) Wraps aSpliteratorso that when iterated, a progress bar is shown to track the traversal progress.static <T, S extends BaseStream<T,S>>
Stream<T> Wraps aStreamso that when iterated, a progress bar is shown to track the traversal progress.static <T, S extends BaseStream<T,S>>
Stream<T> wrap(S stream, ProgressBarBuilder pbb) Wraps aStreamso that when iterated, a progress bar is shown to track the traversal progress.static <T> Stream<T> Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.static <T> Stream<T> wrap(T[] array, ProgressBarBuilder pbb) Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.
-
Field Details
-
progress
-
action
-
scheduledTask
-
-
Constructor Details
-
ProgressBar
Creates a progress bar with the specific taskName name and initial maximum value.- Parameters:
task- Task nameinitialMax- Initial maximum value
-
ProgressBar
public ProgressBar(String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, PrintStream os, ProgressBarStyle style, String unitName, long unitSize, boolean showSpeed, DecimalFormat speedFormat, ChronoUnit speedUnit, long processed, Duration elapsed) Deprecated.UseProgressBarBuilderinstead.Creates a progress bar with the specific taskName name, initial maximum value, customized update interval (default 1000 ms), the PrintStream to be used, and output style.- Parameters:
task- Task nameinitialMax- Initial maximum valueupdateIntervalMillis- Update interval (default value 1000 ms)continuousUpdate- Rerender every time the update interval happens regardless of progress count.style- Draw styleshowSpeed- Should the calculated speed be displayedspeedFormat- Speed number format
-
ProgressBar
public ProgressBar(String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, long processed, Duration elapsed, ProgressBarRenderer renderer, ProgressBarConsumer consumer) Deprecated.UseProgressBarBuilderinstead. Will be private in future versions.Creates a progress bar with the specific name, initial maximum value, customized update interval (default 1s), and the provided progress bar renderer (ProgressBarRenderer) and consumer (ProgressBarConsumer).- Parameters:
task- Task nameinitialMax- Initial maximum valueupdateIntervalMillis- Update time interval (default value 1000ms)continuousUpdate- Rerender every time the update interval happens regardless of progress count.processed- Initial completed process valueelapsed- Initial elapsedBeforeStart second beforerenderer- Progress bar rendererconsumer- Progress bar consumer
-
-
Method Details
-
stepBy
Advances this progress bar by a specific amount.- Parameters:
n- Step size
-
stepTo
Advances this progress bar to the specific progress value.- Parameters:
n- New progress value
-
step
Advances this progress bar by one step. -
maxHint
Gives a hint to the maximum value of the progress bar.- Parameters:
n- Hint of the maximum value. A value of -1 indicates that the progress bar is indefinite.
-
pause
Pauses this current progress. -
resume
Resumes this current progress. -
reset
Resets the progress bar to its initial state (where progress equals to 0). -
close
public void close()Stops this progress bar, effectively stops tracking the underlying process.
Implements the
AutoCloseableinterface which enables the try-with-resource pattern with progress bars.- Specified by:
closein interfaceAutoCloseable- Since:
- 0.7.0
-
setExtraMessage
Sets the extra message at the end of the progress bar.- Parameters:
msg- New message
-
getCurrent
public long getCurrent()Returns the current progress. -
getMax
public long getMax()Returns the maximum value of this progress bar. -
getStart
public long getStart() -
getNormalizedProgress
public double getNormalizedProgress()Returns the progress normalized to the interval [0, 1]. -
getStartInstant
Returns the instant when the progress bar started. If a progress bar is resumed after a pause, it returns the instant when the progress was resumed. -
getElapsedBeforeStart
Returns the duration that this progress bar has been running before it was resumed. If a progress bar starts afresh, it should return zero. -
getElapsedAfterStart
Returns the duration that this progress bar has been running after it was resumed. If a progress bar has not been paused before, it should return the total duration starting from creation. -
getTotalElapsed
Returns the total duration that this progress bar has been running from start, excluding the period when it has been paused. -
getTaskName
Returns the name of this task. -
getExtraMessage
Returns the extra message at the end of the progress bar. -
isIndefinite
public boolean isIndefinite()Checks if the progress bar is indefinite, i.e., its maximum value is unknown. -
refresh
public void refresh()Prompts the progress bar to refresh. Normally a user should not call this function. -
wrap
-
wrap
Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.- Parameters:
it- Underlying iteratorpbb- Progress bar builder
-
wrap
-
wrap
Wraps anIterableso that when iterated, a progress bar is shown to track the traversal progress. For this function the progress bar can be fully customized by using aProgressBarBuilder.- Parameters:
ts- Underlying iterablepbb- An instance of aProgressBarBuilder
-
wrap
Wraps anInputStreamso that when read, a progress bar is shown to track the reading progress.- Parameters:
is- Input stream to be wrappedtask- Name of the progress
-
wrap
Wraps anInputStreamso that when read, a progress bar is shown to track the reading progress. For this function the progress bar can be fully customized by using aProgressBarBuilder.- Parameters:
is- Input stream to be wrappedpbb- An instance of aProgressBarBuilder
-
wrap
Wraps anOutputStreamso that when written, a progress bar is shown to track the writing progress.- Parameters:
os- Output stream to be wrappedtask- Name of the progress
-
wrap
Wraps anOutputStreamso that when written, a progress bar is shown to track the writing progress. For this function the progress bar can be fully customized by using aProgressBarBuilder.- Parameters:
os- Output stream to be wrappedpbb- An instance of aProgressBarBuilder
-
wrap
-
wrap
Wraps aReaderso that when read, a progress bar is shown to track the reading progress. For this function the progress bar can be fully customized by using aProgressBarBuilder.- Parameters:
reader- Reader to be wrappedpbb- An instance of aProgressBarBuilder
-
wrap
-
wrap
Wraps aWriterso that when written, a progress bar is shown to track the writing progress. For this function the progress bar can be fully customized by using aProgressBarBuilder.- Parameters:
writer- Writer to be wrappedpbb- An instance of aProgressBarBuilder
-
wrap
Wraps aSpliteratorso that when iterated, a progress bar is shown to track the traversal progress.- Parameters:
sp- Underlying spliteratortask- Task name
-
wrap
Wraps aSpliteratorso that when iterated, a progress bar is shown to track the traversal progress. For this function the progress bar can be fully customized by using aProgressBarBuilder.- Parameters:
sp- Underlying spliteratorpbb- An instance of aProgressBarBuilder
-
wrap
Wraps aStreamso that when iterated, a progress bar is shown to track the traversal progress.- Parameters:
stream- Underlying stream (can be sequential or parallel)task- Task name
-
wrap
Wraps aStreamso that when iterated, a progress bar is shown to track the traversal progress. For this function the progress bar can be fully customized by using aProgressBarBuilder.- Parameters:
stream- Underlying stream (can be sequential or parallel)pbb- An instance of aProgressBarBuilder
-
wrap
-
wrap
Wraps an array so that when iterated, a progress bar is shown to track the traversal progress. For this function the progress bar can be fully customized by using aProgressBarBuilder.- Parameters:
array- Array to be wrappedpbb- An instance of aProgressBarBuilder- Returns:
- Wrapped array, of type
Stream.
-
builder
Creates a new builder to customize a progress bar.
-
ProgressBarBuilderinstead.