-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from ctongfei/0.10.0
0.10.0
- Loading branch information
Showing
11 changed files
with
241 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/me/tongfei/progressbar/ProgressBarStyleBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
src/test/java/me/tongfei/progressbar/CustomProgressBarTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |