Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.9.3 #123

Merged
merged 16 commits into from
Jan 22, 2022
Merged

0.9.3 #123

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog
* `0.9.3`:
- New functionalities:
- Supports for wrapping around `java.io.OutputStream`s and `java.io.Writer`s (#114). Thanks @azachar !
- Added `continuousUpdate` boolean parameter to various constructors and the `ProgressUpdateAction` so that long-running processes don't take forever to print something (#121, PR #120). Thanks @gaoagong !
- Performance improvements:
- Improved performance in rendered string building (PR #107). Thanks @heroesleo65 !
- Improved performance in `ConsoleProgressBarConsumer::accept` (PR #106). Thanks @heroesleo65 !
- Bugfixes:
- Displays a progress bar immediately after it starts, regardless of whether it has made any progress (#117). Thanks @azachar !
- Closing a progress bar will now force the progress bar to refresh (PR #110). Thanks @kmtong !
- Using a default `DecimalFormat` object if `isSpeedShown` is true as it will otherwise throw a `NullPointerException` during rendering (#121, PR #120). Thanks @gaoagong !
- Dependency bump. Specifically:
- Supports Apple M1 due to https://github.com/jline/jline3/issues/688 (PR #119). Thanks @snuyanzin !

* `0.9.3`:
- New functionalities:
- Adding `continuousUpdate` boolean parameter to various constructors and the `ProgressUpdateAction` so that long-running processes don't take forever to print something.
- Bugfixes:
- Using a default `DecimalFormat` object if `isSpeedShown` is true as it will otherwise throw a `NullPointerException` during rendering.
* `0.9.2`:
- New functionalities:
- Supports for wrapping around `java.io.Reader`s.
Expand Down
6 changes: 4 additions & 2 deletions docs/declarative-usage.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
Since Progressbar `0.6.0`, declarative usage is the preferred way of using a progress bar.

Basically, one can wrap a stream/collection with `ProgressBar.wrap(...)` so that when iterating over it, a progress bar automatically tracks its progress. The type of your collection does not change after wrapped with a progress bar.
The collection types supported are:
Basically, one can wrap a stream/collection with `ProgressBar.wrap(...)` so that when iterating/reading/writing over it, a progress bar automatically tracks its progress. The type of your collection/stream does not change after wrapped with a progress bar.
The collection/stream types supported are:

- `T[]`;
- `java.lang.Iterable<T>`;
- `java.util.Iterator<T>`;
- `java.io.InputStream` (can be regarded as an `Iterator<Byte>`);
- `java.io.Reader` (can be regarded as an `Iterator<Char>`);
- `java.io.OutputStream` (dual of `InputStream`);
- `java.io.Writer` (dual of `Reader`);
- `java.util.Spliterator<T>`;
- `java.util.Stream<T>` (actually any `S` such that `S extends BaseStream<T, S>`. When wrapping over a primitive stream, boxing overhead may be incurred).

Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
site_name: 'Progressbar 0.9.2'
site_name: 'Progressbar 0.9.3'
site_description: 'A terminal progress bar for Java/JVM'
site_author: 'Tongfei Chen'
copyright: 'Copyright &copy; 2015-2021 Tongfei Chen and contributors'
Expand Down
23 changes: 17 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId>
<version>0.9.2</version>
<version>0.9.3</version>
<name>progressbar</name>
<description>A terminal-based progress bar for JVM</description>
<url>http://github.com/ctongfei/progressbar</url>
Expand Down Expand Up @@ -110,40 +110,51 @@
<id>heroesleo65</id>
<name>Ilya Korobtsev</name>
</developer>
<developer>
<id>kmtong</id>
<name>KM Tong</name>
</developer>
<developer>
<id>snuyanzin</id>
<name>Sergey Nuyanzin</name>
</developer>
<developer>
<id>gaoagong</id>
</developer>
</developers>

<dependencies>
<dependency>
<groupId>org.jline</groupId>
<artifactId>jline</artifactId>
<version>3.20.0</version>
<version>3.21.0</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.2</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.31</version>
<version>1.7.32</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.31</version>
<version>1.7.32</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ public int getMaxRenderedLength() {

@Override
public void accept(String str) {
int displayLength = StringDisplayUtils.getStringDisplayLength(str);
int acceptedLength = Math.min(displayLength, getMaxRenderedLength());
out.print(CARRIAGE_RETURN + StringDisplayUtils.trimDisplayLength(str, acceptedLength));
out.print(CARRIAGE_RETURN + StringDisplayUtils.trimDisplayLength(str, getMaxRenderedLength()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected DefaultProgressBarRenderer(
this.unitName = unitName;
this.unitSize = unitSize;
this.isSpeedShown = isSpeedShown;
this.speedFormat = speedFormat;
this.speedFormat = isSpeedShown && speedFormat == null ? new DecimalFormat() : speedFormat;
this.speedUnit = speedUnit;
}

Expand Down Expand Up @@ -98,6 +98,9 @@ protected String speed(ProgressState progress, Duration elapsed) {
}

public String render(ProgressState progress, int maxLength) {
if (maxLength <= 0) {
return "";
}

Instant currTime = Instant.now();
Duration elapsed = Duration.between(progress.startInstant, currTime);
Expand Down Expand Up @@ -126,7 +129,7 @@ public String render(ProgressState progress, int maxLength) {

int length = maxLength - prefixLength - suffixLength;

StringBuilder sb = new StringBuilder();
StringBuilder sb = new StringBuilder(maxLength);
sb.append(prefix);

// case of indefinite progress bars
Expand Down
69 changes: 63 additions & 6 deletions src/main/java/me/tongfei/progressbar/ProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

import static me.tongfei.progressbar.Util.createConsoleConsumer;

import java.io.InputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.io.*;
import java.text.DecimalFormat;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
Expand Down Expand Up @@ -36,7 +34,7 @@ public class ProgressBar implements AutoCloseable {
* @param initialMax Initial maximum value
*/
public ProgressBar(String task, long initialMax) {
this(task, initialMax, 1000, System.err, ProgressBarStyle.COLORFUL_UNICODE_BLOCK, "", 1, false, null, ChronoUnit.SECONDS, 0L, Duration.ZERO);
this(task, initialMax, 1000, false, System.err, ProgressBarStyle.COLORFUL_UNICODE_BLOCK, "", 1, false, null, ChronoUnit.SECONDS, 0L, Duration.ZERO);
}

/**
Expand All @@ -45,14 +43,17 @@ public ProgressBar(String task, long initialMax) {
* @param task Task name
* @param initialMax Initial maximum value
* @param updateIntervalMillis Update interval (default value 1000 ms)
* @param continuousUpdate Rerender every time the update interval happens regardless of progress count.
* @param style Output style (default value ProgressBarStyle.UNICODE_BLOCK)
* @param showSpeed Should the calculated speed be displayed
* @param speedFormat Speed number format
* @deprecated Use {@link ProgressBarBuilder} instead.
*/
public ProgressBar(
String task,
long initialMax,
int updateIntervalMillis,
boolean continuousUpdate,
PrintStream os,
ProgressBarStyle style,
String unitName,
Expand All @@ -63,7 +64,7 @@ public ProgressBar(
long processed,
Duration elapsed
) {
this(task, initialMax, updateIntervalMillis, processed, elapsed,
this(task, initialMax, updateIntervalMillis, continuousUpdate, processed, elapsed,
new DefaultProgressBarRenderer(style, unitName, unitSize, showSpeed, speedFormat, speedUnit),
createConsoleConsumer(os)
);
Expand All @@ -75,22 +76,25 @@ public ProgressBar(
* @param task Task name
* @param initialMax Initial maximum value
* @param updateIntervalMillis Update time interval (default value 1000ms)
* @param continuousUpdate Rerender every time the update interval happens regardless of progress count.
* @param processed Initial completed process value
* @param elapsed Initial elapsedBeforeStart second before
* @param renderer Progress bar renderer
* @param consumer Progress bar consumer
* @deprecated Use {@link ProgressBarBuilder} instead. Will be private in future versions.
*/
public ProgressBar(
String task,
long initialMax,
int updateIntervalMillis,
boolean continuousUpdate,
long processed,
Duration elapsed,
ProgressBarRenderer renderer,
ProgressBarConsumer consumer
) {
this.progress = new ProgressState(task, initialMax, processed, elapsed);
this.action = new ProgressUpdateAction(progress, renderer, consumer);
this.action = new ProgressUpdateAction(progress, renderer, consumer, continuousUpdate);
scheduledTask = Util.executor.scheduleAtFixedRate(
action, 0, updateIntervalMillis, TimeUnit.MILLISECONDS
);
Expand Down Expand Up @@ -152,6 +156,12 @@ public ProgressBar resume() {
return this;
}

/** Resets the progress bar to its initial state (where progress equals to 0). */
public ProgressBar reset() {
progress.reset();
return this;
}

/**
* <p>Stops this progress bar, effectively stops tracking the underlying process.</p>
* <p>Implements the {@link AutoCloseable} interface which enables the try-with-resource
Expand Down Expand Up @@ -204,6 +214,13 @@ public String getExtraMessage() {
return progress.getExtraMessage();
}

/**
* Prompts the progress bar to refresh. Normally a user should not call this function.
*/
public void refresh() {
action.refresh();
}

// STATIC WRAPPER METHODS

/**
Expand Down Expand Up @@ -276,6 +293,26 @@ public static InputStream wrap(InputStream is, ProgressBarBuilder pbb) {
return new ProgressBarWrappedInputStream(is, pbb.build());
}

/**
* Wraps an {@link OutputStream} so that when written, a progress bar is shown to track the writing progress.
* @param os Output stream to be wrapped
* @param task Name of the progress
*/
public static OutputStream wrap(OutputStream os, String task) {
ProgressBarBuilder pbb = new ProgressBarBuilder().setTaskName(task);
return wrap(os, pbb);
}

/**
* Wraps an {@link 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 {@link ProgressBarBuilder}.
* @param os Output stream to be wrapped
* @param pbb An instance of a {@link ProgressBarBuilder}
*/
public static OutputStream wrap(OutputStream os, ProgressBarBuilder pbb) {
return new ProgressBarWrappedOutputStream(os, pbb.build());
}

/**
* Wraps a {@link Reader} so that when read, a progress bar is shown to track the reading progress.
* @param reader Reader to be wrapped
Expand All @@ -296,6 +333,26 @@ public static Reader wrap(Reader reader, ProgressBarBuilder pbb) {
return new ProgressBarWrappedReader(reader, pbb.build());
}

/**
* Wraps a {@link Writer} so that when written, a progress bar is shown to track the writing progress.
* @param writer Writer to be wrapped
* @param task Name of the progress
*/
public static Writer wrap(Writer writer, String task) {
ProgressBarBuilder pbb = new ProgressBarBuilder().setTaskName(task);
return wrap(writer, pbb);
}

/**
* Wraps a {@link 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 {@link ProgressBarBuilder}.
* @param writer Writer to be wrapped
* @param pbb An instance of a {@link ProgressBarBuilder}
*/
public static Writer wrap(Writer writer, ProgressBarBuilder pbb) {
return new ProgressBarWrappedWriter(writer, pbb.build());
}

/**
* Wraps a {@link Spliterator} so that when iterated, a progress bar is shown to track the traversal progress.
* @param sp Underlying spliterator
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/tongfei/progressbar/ProgressBarBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ProgressBarBuilder {
private String task = "";
private long initialMax = -1;
private int updateIntervalMillis = 1000;
private boolean continuousUpdate = false;
private ProgressBarStyle style = ProgressBarStyle.COLORFUL_UNICODE_BLOCK;
private ProgressBarConsumer consumer = null;
private String unitName = "";
Expand Down Expand Up @@ -51,6 +52,11 @@ public ProgressBarBuilder setUpdateIntervalMillis(int updateIntervalMillis) {
return this;
}

public ProgressBarBuilder continuousUpdate() {
this.continuousUpdate = true;
return this;
}

public ProgressBarBuilder setConsumer(ProgressBarConsumer consumer) {
this.consumer = consumer;
return this;
Expand Down Expand Up @@ -98,6 +104,7 @@ public ProgressBar build() {
task,
initialMax,
updateIntervalMillis,
continuousUpdate,
processed,
elapsed,
new DefaultProgressBarRenderer(style, unitName, unitSize, showSpeed, speedFormat, speedUnit),
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/me/tongfei/progressbar/ProgressState.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ synchronized void resume() {
startInstant = Instant.now();
}

synchronized void reset() {
start = 0;
current = 0;
startInstant = Instant.now();
elapsedBeforeStart = Duration.ZERO;
}

synchronized void kill() {
alive = false;
}
Expand Down
Loading