Package net.morimekta.console.terminal
Class ProgressManager
- java.lang.Object
-
- net.morimekta.console.terminal.ProgressManager
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class ProgressManager extends java.lang.Object implements java.lang.AutoCloseable
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 static interface
ProgressManager.ProgressAsyncHandler<T>
static interface
ProgressManager.ProgressHandler<T>
-
Constructor Summary
Constructors Constructor Description ProgressManager(Terminal terminal, Progress.Spinner spinner)
Create a progress bar using the given terminal.ProgressManager(Terminal terminal, Progress.Spinner spinner, int max_tasks)
Create a progress bar using the given terminal.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> java.util.concurrent.Future<T>
addTask(java.lang.String title, long total, ProgressManager.ProgressAsyncHandler<T> handler)
Add a task to be done while showing progress.<T> java.util.concurrent.Future<T>
addTask(java.lang.String title, long total, 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 java.util.List<java.lang.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(@Nonnull Terminal terminal, @Nonnull Progress.Spinner spinner)
Create a progress bar using the given terminal.- Parameters:
terminal
- The terminal to use.spinner
- The spinner to use.
-
ProgressManager
public ProgressManager(@Nonnull Terminal terminal, @Nonnull Progress.Spinner spinner, int max_tasks)
Create a progress bar using the given terminal.- Parameters:
terminal
- The terminal to use.spinner
- The spinner to use.max_tasks
- Maximum number fo concurrent inProgress.
-
-
Method Detail
-
close
public void close()
Close the progress and all tasks associated with it.- Specified by:
close
in interfacejava.lang.AutoCloseable
-
waitAbortable
public void waitAbortable() throws java.io.IOException, java.lang.InterruptedException
Wait for all scheduled tasks to finish allowing the user to abort all tasks with <ctrl>-C.- Throws:
java.io.IOException
- If interrupted by user.java.lang.InterruptedException
- If interrupted by system or other threads.
-
addTask
public <T> java.util.concurrent.Future<T> addTask(java.lang.String title, long total, 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.total
- The total progress to complete.handler
- The handler to do the task behind the progress being shown.- Returns:
- The future returning the task result.
-
addTask
public <T> java.util.concurrent.Future<T> addTask(java.lang.String title, long total, 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.total
- The total progress to complete.handler
- The handler to do the task behind the progress being shown.- Returns:
- The future returning the task result.
-
lines
protected java.util.List<java.lang.String> lines()
-
-