Class ProgressBar

java.lang.Object
me.tongfei.progressbar.ProgressBar
All Implemented Interfaces:
AutoCloseable

public class ProgressBar extends Object implements AutoCloseable
A console-based progress bar with minimal runtime overhead.
  • Field Details

  • Constructor Details

    • ProgressBar

      public ProgressBar(String task, long initialMax)
      Creates a progress bar with the specific taskName name and initial maximum value.
      Parameters:
      task - Task name
      initialMax - 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.
      Use ProgressBarBuilder instead.
      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 name
      initialMax - Initial maximum value
      updateIntervalMillis - Update interval (default value 1000 ms)
      continuousUpdate - Rerender every time the update interval happens regardless of progress count.
      style - Draw style
      showSpeed - Should the calculated speed be displayed
      speedFormat - 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.
      Use ProgressBarBuilder instead. 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 name
      initialMax - Initial maximum value
      updateIntervalMillis - Update time interval (default value 1000ms)
      continuousUpdate - Rerender every time the update interval happens regardless of progress count.
      processed - Initial completed process value
      elapsed - Initial elapsedBeforeStart second before
      renderer - Progress bar renderer
      consumer - Progress bar consumer
  • Method Details

    • 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 AutoCloseable interface which enables the try-with-resource pattern with progress bars.

      Specified by:
      close in interface AutoCloseable
      Since:
      0.7.0
    • setExtraMessage

      public ProgressBar setExtraMessage(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 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 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 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 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 String getTaskName()
      Returns the name of this task.
    • getExtraMessage

      public 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> Iterator<T> wrap(Iterator<T> it, String task)
      Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.
      Parameters:
      it - Underlying iterator
      task - Task name
    • wrap

      public 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.
      Parameters:
      it - Underlying iterator
      pbb - Progress bar builder
    • wrap

      public static <T> Iterable<T> wrap(Iterable<T> ts, String task)
      Wraps an Iterable so 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 iterable
      task - Task name
    • wrap

      public static <T> Iterable<T> wrap(Iterable<T> ts, ProgressBarBuilder pbb)
      Wraps an Iterable 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 a ProgressBarBuilder.
      Parameters:
      ts - Underlying iterable
      pbb - An instance of a ProgressBarBuilder
    • wrap

      public static InputStream wrap(InputStream is, String task)
      Wraps an InputStream so that when read, a progress bar is shown to track the reading progress.
      Parameters:
      is - Input stream to be wrapped
      task - Name of the progress
    • wrap

      public static InputStream wrap(InputStream is, ProgressBarBuilder pbb)
      Wraps an InputStream so 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 a ProgressBarBuilder.
      Parameters:
      is - Input stream to be wrapped
      pbb - An instance of a ProgressBarBuilder
    • wrap

      public static OutputStream wrap(OutputStream os, String task)
      Wraps an OutputStream so that when written, a progress bar is shown to track the writing progress.
      Parameters:
      os - Output stream to be wrapped
      task - Name of the progress
    • wrap

      public static OutputStream wrap(OutputStream os, ProgressBarBuilder pbb)
      Wraps an OutputStream so 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 a ProgressBarBuilder.
      Parameters:
      os - Output stream to be wrapped
      pbb - An instance of a ProgressBarBuilder
    • wrap

      public static Reader wrap(Reader reader, String task)
      Wraps a Reader so that when read, a progress bar is shown to track the reading progress.
      Parameters:
      reader - Reader to be wrapped
      task - Name of the progress
    • wrap

      public static Reader wrap(Reader reader, ProgressBarBuilder pbb)
      Wraps a Reader so 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 a ProgressBarBuilder.
      Parameters:
      reader - Reader to be wrapped
      pbb - An instance of a ProgressBarBuilder
    • wrap

      public static Writer wrap(Writer writer, String task)
      Wraps a Writer so that when written, a progress bar is shown to track the writing progress.
      Parameters:
      writer - Writer to be wrapped
      task - Name of the progress
    • wrap

      public static Writer wrap(Writer writer, ProgressBarBuilder pbb)
      Wraps a Writer so 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 a ProgressBarBuilder.
      Parameters:
      writer - Writer to be wrapped
      pbb - An instance of a ProgressBarBuilder
    • wrap

      public static <T> Spliterator<T> wrap(Spliterator<T> sp, String task)
      Wraps a Spliterator so that when iterated, a progress bar is shown to track the traversal progress.
      Parameters:
      sp - Underlying spliterator
      task - Task name
    • wrap

      public static <T> Spliterator<T> wrap(Spliterator<T> sp, ProgressBarBuilder pbb)
      Wraps a Spliterator 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 a ProgressBarBuilder.
      Parameters:
      sp - Underlying spliterator
      pbb - An instance of a ProgressBarBuilder
    • wrap

      public static <T, S extends BaseStream<T,S>> Stream<T> wrap(S stream, String task)
      Wraps a Stream so 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 BaseStream<T,S>> Stream<T> wrap(S stream, ProgressBarBuilder pbb)
      Wraps a Stream 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 a ProgressBarBuilder.
      Parameters:
      stream - Underlying stream (can be sequential or parallel)
      pbb - An instance of a ProgressBarBuilder
    • wrap

      public static <T> Stream<T> wrap(T[] array, String task)
      Wraps an array so that when iterated, a progress bar is shown to track the traversal progress.
      Parameters:
      array - Array to be wrapped
      task - Task name
      Returns:
      Wrapped array, of type Stream.
    • wrap

      public 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. For this function the progress bar can be fully customized by using a ProgressBarBuilder.
      Parameters:
      array - Array to be wrapped
      pbb - An instance of a ProgressBarBuilder
      Returns:
      Wrapped array, of type Stream.
    • builder

      public static ProgressBarBuilder builder()
      Creates a new builder to customize a progress bar.