-
-
Notifications
You must be signed in to change notification settings - Fork 0
GH-57 Add Progessbar #57
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
Changes from all commits
b0f7db4
24fa44a
482047f
2ec90a6
28a9bf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.eternalcode.commons.progressbar; | ||
|
||
public class ProgressBar { | ||
|
||
private final String filledToken; | ||
|
||
private final String emptyToken; | ||
|
||
private final boolean showBrackets; | ||
private final String leftBracket; | ||
private final String rightBracket; | ||
private final String bracketColor; | ||
|
||
private final int length; | ||
|
||
private ProgressBar(Builder builder) { | ||
this.leftBracket = builder.leftBracket; | ||
this.rightBracket = builder.rightBracket; | ||
this.bracketColor = builder.bracketColor; | ||
this.length = builder.length; | ||
this.showBrackets = builder.showBrackets; | ||
|
||
this.filledToken = builder.filledColor + builder.filledChar; | ||
this.emptyToken = builder.emptyColor + builder.emptyChar; | ||
} | ||
|
||
public String render(double progress) { | ||
double clampedProgress = Math.max(0, Math.min(1, progress)); | ||
int filled = (int) (this.length * clampedProgress); | ||
|
||
StringBuilder bar = new StringBuilder(); | ||
|
||
if (this.showBrackets) { | ||
bar.append(this.bracketColor).append(this.leftBracket); | ||
} | ||
|
||
for (int i = 0; i < this.length; i++) { | ||
if (i < filled) { | ||
bar.append(this.filledToken); | ||
} | ||
else { | ||
bar.append(this.emptyToken); | ||
} | ||
} | ||
|
||
if (this.showBrackets) { | ||
bar.append(this.bracketColor).append(this.rightBracket); | ||
} | ||
|
||
return bar.toString(); | ||
} | ||
|
||
public String render(int current, int max) { | ||
if (max <= 0) { | ||
return this.render(1.0); | ||
} | ||
double progress = (double) current / max; | ||
return this.render(progress); | ||
} | ||
|
||
public String render(long current, long max) { | ||
if (max <= 0) { | ||
return this.render(1.0); | ||
} | ||
double progress = (double) current / max; | ||
return this.render(progress); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
private String filledChar = "█"; | ||
private String emptyChar = "░"; | ||
private String filledColor = ""; | ||
private String emptyColor = ""; | ||
private String leftBracket = "["; | ||
private String rightBracket = "]"; | ||
private String bracketColor = ""; | ||
private int length = 10; | ||
private boolean showBrackets = true; | ||
|
||
public Builder filledChar(String filledChar) { | ||
this.filledChar = filledChar; | ||
return this; | ||
} | ||
|
||
public Builder emptyChar(String emptyChar) { | ||
this.emptyChar = emptyChar; | ||
return this; | ||
} | ||
|
||
public Builder filledColor(String filledColor) { | ||
this.filledColor = filledColor; | ||
return this; | ||
} | ||
|
||
public Builder emptyColor(String emptyColor) { | ||
this.emptyColor = emptyColor; | ||
return this; | ||
} | ||
|
||
public Builder leftBracket(String leftBracket) { | ||
this.leftBracket = leftBracket; | ||
return this; | ||
} | ||
|
||
public Builder rightBracket(String rightBracket) { | ||
this.rightBracket = rightBracket; | ||
return this; | ||
} | ||
|
||
public Builder brackets(String left, String right) { | ||
this.leftBracket = left; | ||
this.rightBracket = right; | ||
return this; | ||
} | ||
|
||
public Builder bracketColor(String bracketColor) { | ||
this.bracketColor = bracketColor; | ||
return this; | ||
} | ||
|
||
public Builder length(int length) { | ||
if (length <= 0) { | ||
throw new IllegalArgumentException("Length must be positive"); | ||
} | ||
this.length = length; | ||
return this; | ||
} | ||
|
||
public Builder showBrackets(boolean showBrackets) { | ||
this.showBrackets = showBrackets; | ||
return this; | ||
} | ||
|
||
public Builder hideBrackets() { | ||
this.showBrackets = false; | ||
return this; | ||
} | ||
|
||
public ProgressBar build() { | ||
return new ProgressBar(this); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.eternalcode.commons.time; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
public class SimpleDurationUtil { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
private static final Duration ONE_SECOND = Duration.ofSeconds(1); | ||
private static final String ZERO_SECONDS = "0s"; | ||
|
||
private static final TemporalAmountParser<Duration> WITHOUT_MILLIS_FORMAT = new DurationParser() | ||
.withUnit("s", ChronoUnit.SECONDS) | ||
.withUnit("m", ChronoUnit.MINUTES) | ||
.withUnit("h", ChronoUnit.HOURS) | ||
.withUnit("d", ChronoUnit.DAYS) | ||
.withUnit("w", ChronoUnit.WEEKS) | ||
.withUnit("mo", ChronoUnit.MONTHS) | ||
.withUnit("y", ChronoUnit.YEARS) | ||
.roundOff(ChronoUnit.MILLIS); | ||
|
||
private static final TemporalAmountParser<Duration> STANDARD_FORMAT = new DurationParser() | ||
.withUnit("ms", ChronoUnit.MILLIS) | ||
.withUnit("s", ChronoUnit.SECONDS) | ||
.withUnit("m", ChronoUnit.MINUTES) | ||
.withUnit("h", ChronoUnit.HOURS) | ||
.withUnit("d", ChronoUnit.DAYS) | ||
.withUnit("w", ChronoUnit.WEEKS) | ||
.withUnit("mo", ChronoUnit.MONTHS) | ||
.withUnit("y", ChronoUnit.YEARS); | ||
|
||
public static String format(Duration duration, boolean removeMillis) { | ||
if (removeMillis) { | ||
if (duration.toMillis() < ONE_SECOND.toMillis()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return ZERO_SECONDS; | ||
} | ||
|
||
return WITHOUT_MILLIS_FORMAT.format(duration); | ||
} | ||
|
||
return STANDARD_FORMAT.format(duration); | ||
} | ||
|
||
public static String format(Duration duration) { | ||
return format(duration, false); | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,114 @@ | ||||||||||
package com.eternalcode.commons.progressbar; | ||||||||||
|
||||||||||
import org.junit.jupiter.api.Test; | ||||||||||
|
||||||||||
import static org.junit.jupiter.api.Assertions.*; | ||||||||||
|
||||||||||
class ProgressBarTest { | ||||||||||
|
||||||||||
@Test | ||||||||||
void testRenderFullProgress() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(5) | ||||||||||
.build(); | ||||||||||
String rendered = bar.render(1.0); | ||||||||||
|
||||||||||
assertTrue(rendered.contains("█")); | ||||||||||
assertFalse(rendered.contains("░")); | ||||||||||
assertEquals(7, rendered.length()); | ||||||||||
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assertions in this test can be more specific. Instead of checking for character containment and length separately, you can assert the exact expected string. This makes the test more robust and clearly states the expected outcome.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testRenderEmptyProgress() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(5) | ||||||||||
.build(); | ||||||||||
String rendered = bar.render(0.0); | ||||||||||
|
||||||||||
assertTrue(rendered.contains("░")); | ||||||||||
assertFalse(rendered.contains("█")); | ||||||||||
assertEquals(7, rendered.length()); | ||||||||||
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make this test more robust and clear, consider asserting against the exact expected string instead of checking for character containment and length.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testRenderHalfProgress() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(4) | ||||||||||
.build(); | ||||||||||
String rendered = bar.render(0.5); | ||||||||||
|
||||||||||
assertTrue(rendered.contains("█")); | ||||||||||
assertTrue(rendered.contains("░")); | ||||||||||
assertEquals(6, rendered.length()); | ||||||||||
Comment on lines
+40
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testRenderIntOverMax() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(3) | ||||||||||
.build(); | ||||||||||
String rendered = bar.render(5, 3); | ||||||||||
|
||||||||||
assertEquals("[███]", rendered); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testRenderIntWithZeroMax() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(3) | ||||||||||
.build(); | ||||||||||
String rendered = bar.render(0, 0); | ||||||||||
|
||||||||||
assertEquals("[███]", rendered); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testHideBrackets() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(3) | ||||||||||
.hideBrackets() | ||||||||||
.build(); | ||||||||||
String rendered = bar.render(1.0); | ||||||||||
|
||||||||||
assertEquals("███", rendered); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testCustomCharacters() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(4) | ||||||||||
.filledChar("#") | ||||||||||
.emptyChar("-") | ||||||||||
.brackets("{", "}") | ||||||||||
.build(); | ||||||||||
|
||||||||||
String rendered = bar.render(0.5); | ||||||||||
assertEquals("{##--}", rendered); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testNegativeProgressClampedToZero() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(3) | ||||||||||
.build(); | ||||||||||
String rendered = bar.render(-1.0); | ||||||||||
|
||||||||||
assertEquals("[░░░]", rendered); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testProgressGreaterThanOneClampedToOne() { | ||||||||||
ProgressBar bar = ProgressBar.builder() | ||||||||||
.length(3) | ||||||||||
.build(); | ||||||||||
String rendered = bar.render(2.0); | ||||||||||
|
||||||||||
assertEquals("[███]", rendered); | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
void testInvalidLengthThrowsException() { | ||||||||||
assertThrows(IllegalArgumentException.class, () -> ProgressBar.builder().length(0)); | ||||||||||
assertThrows(IllegalArgumentException.class, () -> ProgressBar.builder().length(-5)); | ||||||||||
} | ||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.