- java.lang.Object
-
- me.tongfei.progressbar.ProgressBar
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class ProgressBar extends java.lang.Object implements java.lang.AutoCloseableA console-based progress bar with minimal runtime overhead.- Author:
- Tongfei Chen
-
-
Constructor Summary
Constructors Constructor Description ProgressBar(java.lang.String task, long initialMax)Creates a progress bar with the specific taskName name and initial maximum value.ProgressBar(java.lang.String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, long processed, java.time.Duration elapsed, ProgressBarRenderer renderer, ProgressBarConsumer consumer)Deprecated.UseProgressBarBuilderinstead.ProgressBar(java.lang.String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, java.io.PrintStream os, ProgressBarStyle style, java.lang.String unitName, long unitSize, boolean showSpeed, java.text.DecimalFormat speedFormat, java.time.temporal.ChronoUnit speedUnit, long processed, java.time.Duration elapsed)Deprecated.UseProgressBarBuilderinstead.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ProgressBarBuilderbuilder()Creates a new builder to customize a progress bar.voidclose()Stops this progress bar, effectively stops tracking the underlying process.longgetCurrent()Returns the current progress.java.time.DurationgetElapsedAfterStart()Returns the duration that this progress bar has been running after it was resumed.java.time.DurationgetElapsedBeforeStart()Returns the duration that this progress bar has been running before it was resumed.java.lang.StringgetExtraMessage()Returns the extra message at the end of the progress bar.longgetMax()Returns the maximum value of this progress bar.doublegetNormalizedProgress()Returns the progress normalized to the interval [0, 1].longgetStart()java.time.InstantgetStartInstant()Returns the instant when the progress bar started.java.lang.StringgetTaskName()Returns the name of this task.java.time.DurationgetTotalElapsed()Returns the total duration that this progress bar has been running from start, excluding the period when it has been paused.booleanisIndefinite()Checks if the progress bar is indefinite, i.e., its maximum value is unknown.ProgressBarmaxHint(long n)Gives a hint to the maximum value of the progress bar.ProgressBarpause()Pauses this current progress.voidrefresh()Prompts the progress bar to refresh.ProgressBarreset()Resets the progress bar to its initial state (where progress equals to 0).ProgressBarresume()Resumes this current progress.ProgressBarsetExtraMessage(java.lang.String msg)Sets the extra message at the end of the progress bar.ProgressBarstep()Advances this progress bar by one step.ProgressBarstepBy(long n)Advances this progress bar by a specific amount.ProgressBarstepTo(long n)Advances this progress bar to the specific progress value.static java.io.InputStreamwrap(java.io.InputStream is, java.lang.String task)Wraps anInputStreamso that when read, a progress bar is shown to track the reading progress.static java.io.InputStreamwrap(java.io.InputStream is, ProgressBarBuilder pbb)Wraps anInputStreamso that when read, a progress bar is shown to track the reading progress.static java.io.OutputStreamwrap(java.io.OutputStream os, java.lang.String task)Wraps anOutputStreamso that when written, a progress bar is shown to track the writing progress.static java.io.OutputStreamwrap(java.io.OutputStream os, ProgressBarBuilder pbb)Wraps anOutputStreamso that when written, a progress bar is shown to track the writing progress.static java.io.Readerwrap(java.io.Reader reader, java.lang.String task)Wraps aReaderso that when read, a progress bar is shown to track the reading progress.static java.io.Readerwrap(java.io.Reader reader, ProgressBarBuilder pbb)Wraps aReaderso that when read, a progress bar is shown to track the reading progress.static java.io.Writerwrap(java.io.Writer writer, java.lang.String task)Wraps aWriterso that when written, a progress bar is shown to track the writing progress.static java.io.Writerwrap(java.io.Writer writer, ProgressBarBuilder pbb)Wraps aWriterso that when written, a progress bar is shown to track the writing progress.static <T> java.lang.Iterable<T>wrap(java.lang.Iterable<T> ts, java.lang.String task)Wraps anIterableso that when iterated, a progress bar is shown to track the traversal progress.static <T> java.lang.Iterable<T>wrap(java.lang.Iterable<T> ts, ProgressBarBuilder pbb)Wraps anIterableso that when iterated, a progress bar is shown to track the traversal progress.static <T> java.util.Iterator<T>wrap(java.util.Iterator<T> it, java.lang.String task)Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.static <T> java.util.Iterator<T>wrap(java.util.Iterator<T> it, ProgressBarBuilder pbb)Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.static <T> java.util.Spliterator<T>wrap(java.util.Spliterator<T> sp, java.lang.String task)Wraps aSpliteratorso that when iterated, a progress bar is shown to track the traversal progress.static <T> java.util.Spliterator<T>wrap(java.util.Spliterator<T> sp, ProgressBarBuilder pbb)Wraps aSpliteratorso that when iterated, a progress bar is shown to track the traversal progress.static <T,S extends java.util.stream.BaseStream<T,S>>
java.util.stream.Stream<T>wrap(S stream, java.lang.String task)Wraps aStreamso that when iterated, a progress bar is shown to track the traversal progress.static <T,S extends java.util.stream.BaseStream<T,S>>
java.util.stream.Stream<T>wrap(S stream, ProgressBarBuilder pbb)Wraps aStreamso that when iterated, a progress bar is shown to track the traversal progress.static <T> java.util.stream.Stream<T>wrap(T[] array, java.lang.String task)Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.static <T> java.util.stream.Stream<T>wrap(T[] array, ProgressBarBuilder pbb)Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.
-
-
-
Constructor Detail
-
ProgressBar
public ProgressBar(java.lang.String task, long initialMax)Creates a progress bar with the specific taskName name and initial maximum value.- Parameters:
task- Task nameinitialMax- Initial maximum value
-
ProgressBar
public ProgressBar(java.lang.String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, java.io.PrintStream os, ProgressBarStyle style, java.lang.String unitName, long unitSize, boolean showSpeed, java.text.DecimalFormat speedFormat, java.time.temporal.ChronoUnit speedUnit, long processed, java.time.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(java.lang.String task, long initialMax, int updateIntervalMillis, boolean continuousUpdate, boolean clearDisplayOnFinish, long processed, java.time.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 Detail
-
stepBy
public ProgressBar stepBy(long n)
Advances this progress bar by a specific amount.- Parameters:
n- Step size
-
stepTo
public ProgressBar stepTo(long n)
Advances this progress bar to the specific progress value.- Parameters:
n- New progress value
-
step
public ProgressBar step()
Advances this progress bar by one step.
-
maxHint
public ProgressBar maxHint(long n)
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
public ProgressBar pause()
Pauses this current progress.
-
resume
public ProgressBar resume()
Resumes this current progress.
-
reset
public ProgressBar 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 interfacejava.lang.AutoCloseable- Since:
- 0.7.0
-
setExtraMessage
public ProgressBar setExtraMessage(java.lang.String msg)
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
public java.time.Instant 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
public java.time.Duration 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
public java.time.Duration 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
public java.time.Duration getTotalElapsed()
Returns the total duration that this progress bar has been running from start, excluding the period when it has been paused.
-
getTaskName
public java.lang.String getTaskName()
Returns the name of this task.
-
getExtraMessage
public java.lang.String 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
public static <T> java.util.Iterator<T> wrap(java.util.Iterator<T> it, java.lang.String task)Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.- Parameters:
it- Underlying iteratortask- Task name
-
wrap
public static <T> java.util.Iterator<T> wrap(java.util.Iterator<T> it, ProgressBarBuilder pbb)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
public static <T> java.lang.Iterable<T> wrap(java.lang.Iterable<T> ts, java.lang.String task)Wraps anIterableso that when iterated, a progress bar is shown to track the traversal progress.Sample usage:
for (T x : ProgressBar.wrap(collection, "Traversal")) { ... }- Parameters:
ts- Underlying iterabletask- Task name
-
wrap
public static <T> java.lang.Iterable<T> wrap(java.lang.Iterable<T> ts, ProgressBarBuilder pbb)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
public static java.io.InputStream wrap(java.io.InputStream is, java.lang.String task)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
public static java.io.InputStream wrap(java.io.InputStream is, ProgressBarBuilder pbb)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
public static java.io.OutputStream wrap(java.io.OutputStream os, java.lang.String task)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
public static java.io.OutputStream wrap(java.io.OutputStream os, ProgressBarBuilder pbb)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
public static java.io.Reader wrap(java.io.Reader reader, java.lang.String task)Wraps aReaderso that when read, a progress bar is shown to track the reading progress.- Parameters:
reader- Reader to be wrappedtask- Name of the progress
-
wrap
public static java.io.Reader wrap(java.io.Reader reader, ProgressBarBuilder pbb)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
public static java.io.Writer wrap(java.io.Writer writer, java.lang.String task)Wraps aWriterso that when written, a progress bar is shown to track the writing progress.- Parameters:
writer- Writer to be wrappedtask- Name of the progress
-
wrap
public static java.io.Writer wrap(java.io.Writer writer, ProgressBarBuilder pbb)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
public static <T> java.util.Spliterator<T> wrap(java.util.Spliterator<T> sp, java.lang.String task)Wraps aSpliteratorso that when iterated, a progress bar is shown to track the traversal progress.- Parameters:
sp- Underlying spliteratortask- Task name
-
wrap
public static <T> java.util.Spliterator<T> wrap(java.util.Spliterator<T> sp, ProgressBarBuilder pbb)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
public static <T,S extends java.util.stream.BaseStream<T,S>> java.util.stream.Stream<T> wrap(S stream, java.lang.String task)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
public static <T,S extends java.util.stream.BaseStream<T,S>> java.util.stream.Stream<T> wrap(S stream, ProgressBarBuilder pbb)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
public static <T> java.util.stream.Stream<T> wrap(T[] array, java.lang.String task)Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.- Parameters:
array- Array to be wrappedtask- Task name- Returns:
- Wrapped array, of type
Stream.
-
wrap
public static <T> java.util.stream.Stream<T> wrap(T[] array, ProgressBarBuilder pbb)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
public static ProgressBarBuilder builder()
Creates a new builder to customize a progress bar.
-
-