- java.lang.Object
-
- net.morimekta.terminal.progress.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(); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ProgressManager.InternalTask<T>
Internal task on the progress.static interface
ProgressManager.ProgressAsyncHandler<T>
Interface for handling progress asynchronously setting a future value when completed..static interface
ProgressManager.ProgressHandler<T>
Interface for handling progress synchronously.
-
Constructor Summary
Constructors Modifier Constructor Description ProgressManager(Terminal terminal, Spinner spinner)
Create a progress bar using the given terminal.ProgressManager(Terminal terminal, Spinner spinner, int maxTasks)
Create a progress bar using the given terminal.protected
ProgressManager(Terminal terminal, Spinner spinner, int maxTasks, ScheduledExecutorService executor, Clock clock)
Create a progress updater.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Future<T>
addTask(String title, ProgressManager.ProgressAsyncHandler<T> handler)
Add a task to be done while showing progress.<T> Future<T>
addTask(String title, ProgressManager.ProgressHandler<T> handler)
Add a task to be done while showing progress.void
close()
Close the progress and all tasks associated with it.protected List<String>
lines()
void
waitAbortable()
Wait for all scheduled tasks to finish allowing the user to abort all tasks with <ctrl>-C.
-
-
-
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 interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
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.
-
waitAbortable
public void waitAbortable() throws IOException, InterruptedException
Wait for all scheduled tasks to finish allowing the user to abort all tasks with <ctrl>-C.- Throws:
IOException
- If interrupted by user.InterruptedException
- If interrupted by system or other threads.
-
-