Class ProgressManager

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class ProgressManager
    extends Object
    implements Closeable
    Show progress on a number of tasks. The tasks can be dynamically created and finished. E.g. if a number of large files needs to be downloaded they can be given a task each, and only a certain number of files will be downloaded at the same time. Example:
    
     try (ProgressManager progress = new ProgressManager(term, Progress.Spinner.CLOCK)) {
         Future<String> first = progress.addTask("First Task", 10000, task -> {
             // All the work
             task.accept(10000);
             return "OK";
         });
         Future<String> second = progress.addTask("Second Task", 10000, task -> {
             // All the work
             task.accept(10000);
             return "OK";
         });
    
         progress.waitAbortable();
    
         term.println("First: " + first.get());
         term.println("Second: " + second.get());
     } finally {
         term.println();
     }
     
    • Constructor Detail

      • ProgressManager

        public ProgressManager​(Terminal terminal,
                               Spinner spinner)
        Create a progress bar using the given terminal.
        Parameters:
        terminal - The terminal to use.
        spinner - The spinner to use.
      • ProgressManager

        public ProgressManager​(Terminal terminal,
                               Spinner spinner,
                               int maxTasks)
        Create a progress bar using the given terminal.
        Parameters:
        terminal - The terminal to use.
        spinner - The spinner to use.
        maxTasks - Maximum number fo concurrent inProgress.
      • ProgressManager

        protected ProgressManager​(Terminal terminal,
                                  Spinner spinner,
                                  int maxTasks,
                                  ScheduledExecutorService executor,
                                  Clock clock)
        Create a progress updater. Note that either terminal or the updater param must be set.
        Parameters:
        terminal - The terminal to print to.
        spinner - The spinner type.
        maxTasks - Max number of concurrent task count.
        executor - The executor to run updater task in.
        clock - The clock to use for timing.
    • Method Detail

      • close

        public void close()
        Close the progress and all tasks associated with it.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
      • addTask

        public <T> Future<T> addTask​(String title,
                                     ProgressManager.ProgressAsyncHandler<T> handler)
        Add a task to be done while showing progress. If there are too many tasks ongoing, the task will be queued and done when the local thread pool has available threads.
        Type Parameters:
        T - The return type for the task.
        Parameters:
        title - The progress title of the task.
        handler - The handler to do the task behind the progress being shown.
        Returns:
        The future returning the task result.
      • addTask

        public <T> Future<T> addTask​(String title,
                                     ProgressManager.ProgressHandler<T> handler)
        Add a task to be done while showing progress. If there are too many tasks ongoing, the task will be queued and done when the local thread pool has available threads.
        Type Parameters:
        T - The return type for the task.
        Parameters:
        title - The progress title of the task.
        handler - The handler to do the task behind the progress being shown.
        Returns:
        The future returning the task result.
      • lines

        protected List<String> lines()
        Returns:
        Lines shown as part of displaying progress. Available for testing.