Skip to content

Commit

Permalink
Merge pull request #155 from ctongfei/0.10.0
Browse files Browse the repository at this point in the history
0.10.0
  • Loading branch information
ctongfei authored Aug 11, 2023
2 parents 1ffa06c + 141d09c commit 5af6755
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 25 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog
* `0.10.0`:
- New functionalities:
- Make `me.tongfei.progressbar` a Java 9 module (#149, #153, #154). Thanks @clo-vis, @Kamillaova, @bowbahdoe !
- Enables the builder pattern for `ProgressBarStyle` (PR #152). Thanks @PakhomovAlexander !
- Dependency version bump.
* `0.9.5`:
- Bugfixes:
- Fixed the problem of `ProgressState` not being public, thus making `setEtaFunction` useless (#147, PR #146). Thanks @deejgregor, @natanfudge !
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Maven:
<dependency>
<groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId>
<version>0.9.4</version>
<version>0.10.0</version>
</dependency>
```

Expand Down
4 changes: 2 additions & 2 deletions docs/builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Since `0.7.0`, apart from standard constructors, you can also use the so-called
All `setXXX()` (also `showSpeed`) clauses below are optional.

``` java
ProgressBarBuilder pbb = new ProgressBarBuilder()
ProgressBarBuilder pbb = ProgressBar.builder()
.setInitialMax(<initial max>)
.setStyle(ProgressBarStyle.<style>)
.setTaskName(<taskName name>)
Expand All @@ -21,4 +21,4 @@ ProgressBarBuilder pbb = new ProgressBarBuilder()
for (T x : ProgressBar.wrap(collection, pbb)) {
...
}
```
```
16 changes: 16 additions & 0 deletions docs/styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ ProgressBarBuilder pbb = new ProgressBarBuilder()
.setStyle(ProgressBarStyle.<STYLE>);
```


Since `0.10.0` you can customize the progress bar style also with a builder:

``` java
ProgressBarBuilder pbb = ProgressBar.builder()
// ...
.setStyle(ProgressBarStyle.builder()
.colorCode((byte) 33) // the ANSI color code
.leftBracket("{")
.rightBracket("}")
.block('-')
.rightSideFractionSymbol('+')
.build()
)
// ...
```
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
site_name: 'Progressbar'
site_description: 'A terminal progress bar for Java/JVM'
site_author: 'Tongfei Chen'
copyright: 'Copyright &copy; 2015-2022 Tongfei Chen and contributors'
copyright: 'Copyright &copy; 2015-2023 Tongfei Chen and contributors'

theme:
name: 'material'
Expand Down
28 changes: 20 additions & 8 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.5</version>
<version>0.10.0</version>
<name>progressbar</name>
<description>A terminal-based progress bar for JVM</description>
<url>http://github.com/ctongfei/progressbar</url>
Expand Down Expand Up @@ -125,40 +125,44 @@
<id>deejgregor</id>
<name>DJ Gregor</name>
</developer>
<developer>
<id>PakhomovAlexander</id>
<name>Aleksandr Pakhomov</name>
</developer>
</developers>

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

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<version>5.9.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
<version>5.9.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0</version>
<version>2.0.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.0</version>
<version>2.0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -179,7 +183,11 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<version>3.11.0</version>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -200,6 +208,7 @@
<version>3.0.0</version>
<configuration>
<doclint>all,-missing</doclint>
<detectJavaApiLink>false</detectJavaApiLink>
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -248,7 +257,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<version>3.0.0</version>
<configuration>
<useModulePath>false</useModulePath> <!-- tests use classpath -->
</configuration>
</plugin>

</plugins>
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/me/tongfei/progressbar/ProgressBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ProgressBar(String task, long initialMax) {
this(
task, initialMax, 1000, false, false,
System.err, ProgressBarStyle.COLORFUL_UNICODE_BLOCK,
"", 1, false, null,
"", 1L, false, null,
ChronoUnit.SECONDS, 0L, Duration.ZERO
);
}
Expand All @@ -50,7 +50,7 @@ public ProgressBar(String task, long initialMax) {
* @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 style Draw style
* @param showSpeed Should the calculated speed be displayed
* @param speedFormat Speed number format
* @deprecated Use {@link ProgressBarBuilder} instead.
Expand Down Expand Up @@ -480,4 +480,9 @@ public static <T> Stream<T> wrap(T[] array, ProgressBarBuilder pbb) {
return wrap(Arrays.stream(array), pbb);
}

/** Creates a new builder to customize a progress bar. */
public static ProgressBarBuilder builder() {
return new ProgressBarBuilder();
}

}
30 changes: 19 additions & 11 deletions src/main/java/me/tongfei/progressbar/ProgressBarStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@
* @author Tongfei Chen
* @since 0.5.1
*/
public enum ProgressBarStyle {

COLORFUL_UNICODE_BLOCK("\r", "\u001b[33m│", "", "│\u001b[0m", '█', ' ', " ▏▎▍▌▋▊▉", ' '),

COLORFUL_UNICODE_BAR("\r", "\u001b[33m", "\u001b[90m", "\u001b[0m", '━', '━', " ╸", '╺'),

/** Use Unicode block characters to draw the progress bar. */
UNICODE_BLOCK("\r", "|", "", "|", '█', ' ', " ▏▎▍▌▋▊▉", ' '),

/** Use only ASCII characters to draw the progress bar. */
ASCII("\r", "[", "", "]", '=', ' ', ">", ' ');
public class ProgressBarStyle {

String refreshPrompt;
String leftBracket;
Expand All @@ -37,4 +27,22 @@ public enum ProgressBarStyle {
this.rightSideFractionSymbol = rightSideFractionSymbol;
}

public static ProgressBarStyle COLORFUL_UNICODE_BLOCK =
new ProgressBarStyle("\r", "\u001b[33m│", "", "│\u001b[0m", '█', ' ', " ▏▎▍▌▋▊▉", ' ');

public static ProgressBarStyle COLORFUL_UNICODE_BAR =
new ProgressBarStyle("\r", "\u001b[33m", "\u001b[90m", "\u001b[0m", '━', '━', " ╸", '╺');

/** Use Unicode block characters to draw the progress bar. */
public static ProgressBarStyle UNICODE_BLOCK =
new ProgressBarStyle("\r", "|", "", "|", '█', ' ', " ▏▎▍▌▋▊▉", ' ');

/** Use only ASCII characters to draw the progress bar. */
public static ProgressBarStyle ASCII =
new ProgressBarStyle("\r", "[", "", "]", '=', ' ', ">", ' ');

/** Creates a builder to build a custom progress bar style. */
public static ProgressBarStyleBuilder builder() {
return new ProgressBarStyleBuilder();
}
}
85 changes: 85 additions & 0 deletions src/main/java/me/tongfei/progressbar/ProgressBarStyleBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package me.tongfei.progressbar;

/**
* Builder for {@link ProgressBarStyle}s.
*
* @author Aleksandr Pakhomov
* @since 0.10.0
*/
public class ProgressBarStyleBuilder {
private static final String ESC_CODE = "\u001b[";

private ProgressBarStyle style = new ProgressBarStyle("\r", "[", "", "]", '=', ' ', ">", ' ');
private byte colorCode = 0;

/** Set refresh prompt. Default "\r". */
public ProgressBarStyleBuilder refreshPrompt(String refreshPrompt) {
style.refreshPrompt = refreshPrompt;
return this;
}

/** Set left bracket. Default "[". */
public ProgressBarStyleBuilder leftBracket(String leftBracket) {
style.leftBracket = leftBracket;
return this;
}

/** Set delimiting sequence. Default "". */
public ProgressBarStyleBuilder delimitingSequence(String delimitingSequence) {
style.delimitingSequence = delimitingSequence;
return this;
}

/** Set right bracket. Default "]". */
public ProgressBarStyleBuilder rightBracket(String rightBracket) {
style.rightBracket = rightBracket;
return this;
}

/** Set block character. Default "=" */
public ProgressBarStyleBuilder block(char block) {
style.block = block;
return this;
}

/** Set space character. Default " " */
public ProgressBarStyleBuilder space(char space) {
style.space = space;
return this;
}

/** Set fraction symbols. */
public ProgressBarStyleBuilder fractionSymbols(String fractionSymbols) {
style.fractionSymbols = fractionSymbols;
return this;
}

/** Set right side fraction symbol. */
public ProgressBarStyleBuilder rightSideFractionSymbol(char rightSideFractionSymbol) {
style.rightSideFractionSymbol = rightSideFractionSymbol;
return this;
}

/** Set ANSI color code. Default 0 (no color). Must be in {0, ..., 255}. */
public ProgressBarStyleBuilder colorCode(byte code) {
this.colorCode = code;
return this;
}

/** Build {@link ProgressBarStyle}. */
public ProgressBarStyle build() {
boolean colorDefined = colorCode != 0;

if (colorDefined && style.leftBracket.contains(ESC_CODE)) {
throw new IllegalArgumentException("The color code is overridden with left bracket escape code. "
+ "Please, remove the escape sequence from the left bracket or do not use color code.");
}

String prefix = colorDefined ? (ESC_CODE + colorCode + "m") : "";
String postfix = colorDefined ? ESC_CODE + "0m" : "";
style.leftBracket = prefix + style.leftBracket;
style.rightBracket = style.rightBracket + postfix;
return style;
}

}
4 changes: 4 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module me.tongfei.progressbar {
requires org.jline;
exports me.tongfei.progressbar;
}
81 changes: 81 additions & 0 deletions src/test/java/me/tongfei/progressbar/CustomProgressBarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package me.tongfei.progressbar;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/** @author Aleksandr Pakhomov */
public class CustomProgressBarTest {

ProgressBarBuilder builder;

private static void simulateProgress(ProgressBar bar) throws InterruptedException {
int x = 0;
while (x < 10000) {
bar.step();
Thread.sleep(1);
x++;
}
}

@BeforeEach
void setUp() {
builder = new ProgressBarBuilder()
.setUnit("k", 1000)
.setInitialMax(10000);
}

@Test
void defaultProgressBarStyle() throws InterruptedException {
// Given default
try (ProgressBar bar = builder.build()) {
// Expect display default progress bar style:
// 100% │███████████████████████████████████████████│ 10/10k (0:00:12 / 0:00:00)
simulateProgress(bar);
}
}

@Test
void customPredefinedProgressBarStyle() throws InterruptedException {
// Given ASCII progress bar style that is taken from ProgressBarStyle enum
builder.setStyle(ProgressBarStyle.ASCII);
try (ProgressBar bar = builder.build()) {
// Expect display custom progress bar style:
// 50% [=================> ] 5/10k (0:00:06 / 0:00:12)
simulateProgress(bar);
}
}

@Test
void customUserDefinedProgressBarStyleWithColor() throws InterruptedException {
// Given custom progress bar style
builder.setStyle(
ProgressBarStyle.builder()
.colorCode((byte) 36)
.leftBracket("{")
.rightBracket("}")
.block('-')
.rightSideFractionSymbol('+')
.build()
);
try (ProgressBar bar = builder.build()) {
// Expect display custom progress bar style:
// 50% {-------------------+ } 5/10k (0:00:06 / 0:00:12)
simulateProgress(bar);
}
}

@Test
void customColorCannotBeUsedWithEscapeSymbol() {
// Given draw style with both color code and escape symbols
ProgressBarStyleBuilder drawStyleBuilder = ProgressBarStyle.builder()
.colorCode((byte) 33) // yellow
.leftBracket("\u001b[36m{"); // but this overrides color code

// Expect
assertThrows(IllegalArgumentException.class, drawStyleBuilder::build);
}

}

0 comments on commit 5af6755

Please sign in to comment.