Skip to content

Commit 0e70616

Browse files
committed
add a new UNICODE_COLORFUL_BAR style
1 parent f89e7c9 commit 0e70616

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Changelog
22
* `0.9.5`:
33
- Bugfixes:
4-
- Fixed the problem of `ProgressState` not being public, thus making `setEtaFunction` useless (#147, PR #146). Thanks @deejgregor, @natanfudge !
4+
- Fixed the problem of `ProgressState` not being public, thus making `setEtaFunction` useless (#147, PR #146). Thanks @deejgregor, @natanfudge !
5+
- New functionalities:
6+
- Added a new `UNICODE_COLORFUL_BAR` style.
57
* `0.9.4`:
68
- New functionalities:
79
- In `ProgressBarBuilder`s, one can now switch whether to show the remaining time, or to provide a custom

docs/builder.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ ProgressBarBuilder pbb = new ProgressBarBuilder()
1515
.showSpeed();
1616
// or .showSpeed(new DecimalFormat("#.##")) to customize speed display
1717
.setEtaFunction(state -> ...)
18+
// This function is of type `ProgressState -> Optional<Duration>`
19+
// that should output the estimated ETA of the progress.
20+
// Returning `Optional.empty()` means that ETA is not available.
1821
for (T x : ProgressBar.wrap(collection, pbb)) {
1922
...
2023
}

src/main/java/me/tongfei/progressbar/DefaultProgressBarRenderer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,15 @@ public String render(ProgressState progress, int maxLength) {
152152
else {
153153
sb.append(Util.repeat(style.block, progressIntegralPart(progress, length)));
154154
if (progress.current < progress.max) {
155-
sb.append(style.fractionSymbols.charAt(progressFractionalPart(progress, length)));
155+
int fraction = progressFractionalPart(progress, length);
156+
if (fraction != 0) {
157+
sb.append(style.fractionSymbols.charAt(fraction));
158+
sb.append(style.delimitingSequence);
159+
}
160+
else {
161+
sb.append(style.delimitingSequence);
162+
sb.append(style.rightSideFractionSymbol);
163+
}
156164
sb.append(Util.repeat(style.space, length - progressIntegralPart(progress, length) - 1));
157165
}
158166
}

src/main/java/me/tongfei/progressbar/ProgressBarStyle.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,34 @@
77
*/
88
public enum ProgressBarStyle {
99

10-
COLORFUL_UNICODE_BLOCK("\r", "\u001b[33m│", "│\u001b[0m", '█', ' ', " ▏▎▍▌▋▊▉"),
10+
COLORFUL_UNICODE_BLOCK("\r", "\u001b[33m│", "", "│\u001b[0m", '█', ' ', " ▏▎▍▌▋▊▉", ' '),
11+
12+
COLORFUL_UNICODE_BAR("\r", "\u001b[33m", "\u001b[90m", "\u001b[0m", '━', '━', " ╸", '╺'),
1113

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

1517
/** Use only ASCII characters to draw the progress bar. */
16-
ASCII("\r", "[", "]", '=', ' ', ">");
18+
ASCII("\r", "[", "", "]", '=', ' ', ">", ' ');
1719

1820
String refreshPrompt;
1921
String leftBracket;
22+
String delimitingSequence;
2023
String rightBracket;
2124
char block;
2225
char space;
2326
String fractionSymbols;
27+
char rightSideFractionSymbol;
2428

25-
ProgressBarStyle(String refreshPrompt, String leftBracket, String rightBracket, char block, char space, String fractionSymbols) {
29+
ProgressBarStyle(String refreshPrompt, String leftBracket, String delimitingSequence, String rightBracket, char block, char space, String fractionSymbols, char rightSideFractionSymbol) {
2630
this.refreshPrompt = refreshPrompt;
2731
this.leftBracket = leftBracket;
32+
this.delimitingSequence = delimitingSequence;
2833
this.rightBracket = rightBracket;
2934
this.block = block;
3035
this.space = space;
3136
this.fractionSymbols = fractionSymbols;
37+
this.rightSideFractionSymbol = rightSideFractionSymbol;
3238
}
3339

3440
}

src/test/java/me/tongfei/progressbar/ProgressBarTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ProgressBarTest {
1919
void test() {
2020
try (ProgressBar pb = new ProgressBarBuilder()
2121
.setTaskName("Test").setInitialMax(5).setUpdateIntervalMillis(50)
22-
.setStyle(ProgressBarStyle.UNICODE_BLOCK).setUnit("K", 1024).build()) {
22+
.setStyle(ProgressBarStyle.COLORFUL_UNICODE_BAR).setUnit("K", 1024).build()) {
2323

2424
double x = 1.0;
2525
double y = x * x;

0 commit comments

Comments
 (0)