Class ProgressBar

java.lang.Object
com.codename1.cli.ProgressBar
All Implemented Interfaces:
AutoCloseable

public final class ProgressBar extends Object implements AutoCloseable

A progress bar with a percentage, a count, a rate and the time left:

upload  ##################--------------  58%  5.8/10.0 MB  1.2 MB/s  0:03

(drawn with block characters on a UTF-8 terminal).

try (ProgressBar bar = new ProgressBar("upload", files.size())) {
    for (File f : files) {
        send(f);
        bar.step();
    }
}

or, for a loop, track(Iterable, String):

for (File f : ProgressBar.track(files, "upload")) {
    send(f);
}

Stepping is cheap and safe from any thread. A bar made on its own draws itself; several at once belong in a MultiProgress. Off a terminal the bar prints milestone lines instead of redrawing (see MultiProgress).

  • Constructor Summary

    Constructors
    Constructor
    Description
    ProgressBar(String label, long total)
    A bar on its own, for total units of work; a total of 0 or less is unknown, and the bar then animates without a percentage.
  • Method Summary

    Modifier and Type
    Method
    Description
    Shows the count, the total and the rate as sizes (5.8 MB) rather than numbers.
    void
    Completes the bar if nothing else did and, for a bar made on its own, stops drawing it.
    void
    Marks the work complete: the bar fills and shows the time it took.
    void
    Marks the work failed: the bar stops where it is, in red.
    long
    The amount done.
    long
    The total, 0 or less when unknown.
    boolean
    Whether the bar has completed or failed.
    The label.
    setMessage(String message)
    Shows message after the numbers: the file being worked on, say.
    setTotal(long total)
    Changes the total; 0 or less makes it unknown.
    Advances by one.
    stepBy(long n)
    Advances by n.
    stepTo(long n)
    Sets the amount done.
    static <T> Iterable<T>
    track(Iterable<T> items, String label)
    Wraps items so that iterating them steps a bar labelled label, which completes when the iteration ends.
    unit(String unit)
    Units shown after the count, files in 3/10 files.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ProgressBar

      public ProgressBar(String label, long total)
      A bar on its own, for total units of work; a total of 0 or less is unknown, and the bar then animates without a percentage.
  • Method Details

    • track

      public static <T> Iterable<T> track(Iterable<T> items, String label)
      Wraps items so that iterating them steps a bar labelled label, which completes when the iteration ends. A Collection's size is the total; any other iterable has an unknown one.
    • label

      public String label()
      The label.
    • unit

      public ProgressBar unit(String unit)
      Units shown after the count, files in 3/10 files.
    • bytes

      public ProgressBar bytes()
      Shows the count, the total and the rate as sizes (5.8 MB) rather than numbers.
    • step

      public ProgressBar step()
      Advances by one.
    • stepBy

      public ProgressBar stepBy(long n)
      Advances by n.
    • stepTo

      public ProgressBar stepTo(long n)
      Sets the amount done.
    • setTotal

      public ProgressBar setTotal(long total)
      Changes the total; 0 or less makes it unknown.
    • setMessage

      public ProgressBar setMessage(String message)
      Shows message after the numbers: the file being worked on, say.
    • getCurrent

      public long getCurrent()
      The amount done.
    • getTotal

      public long getTotal()
      The total, 0 or less when unknown.
    • isDone

      public boolean isDone()
      Whether the bar has completed or failed.
    • complete

      public void complete()
      Marks the work complete: the bar fills and shows the time it took.
    • fail

      public void fail()
      Marks the work failed: the bar stops where it is, in red.
    • close

      public void close()
      Completes the bar if nothing else did and, for a bar made on its own, stops drawing it.
      Specified by:
      close in interface AutoCloseable