Skip to content

Commit

Permalink
Improved code style for IdleBar.java
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Apr 9, 2024
1 parent 1339bfc commit 2657be3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions cli/src/main/java/de/jplag/cli/logger/IdleBar.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.jplag.cli.logger;

import java.io.IOException;
import java.io.PrintStream;

import org.apache.commons.lang3.time.DurationFormatUtils;
import org.jline.terminal.Terminal;
Expand All @@ -12,6 +13,8 @@
* Prints an idle progress bar, that does not count upwards.
*/
public class IdleBar implements ProgressBar {
private final PrintStream output;

private final Thread runner;

private long startTime;
Expand All @@ -24,6 +27,7 @@ public class IdleBar implements ProgressBar {
private boolean running = false;

public IdleBar(String text) {
this.output = System.out;
this.runner = new Thread(this::run);
this.length = 50;
this.currentDirection = -1;
Expand All @@ -33,8 +37,8 @@ public IdleBar(String text) {
Terminal terminal = TerminalBuilder.terminal();
this.length = Math.min(terminal.getWidth() / 2, terminal.getWidth() - 50);
terminal.close();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (IOException ignore) {
// ignore exceptions here. If we cannot access the terminal, we guess a width
}
if (this.length < 10) {
this.length = 10;
Expand All @@ -53,21 +57,22 @@ public void dispose() {
try {
this.runner.join();
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
}
System.out.println();
this.output.println();
}

private void run() {
while (running) {
System.out.print('\r');
System.out.print(printLine());
this.output.print('\r');
this.output.print(printLine());
if (currentPos == 0 || currentPos == length - 1) {
currentDirection *= -1;
}
try {
Thread.sleep(200);
} catch (InterruptedException ignore) {
// ignore wakeup
Thread.currentThread().interrupt();
}
currentPos += currentDirection;
}
Expand All @@ -93,12 +98,6 @@ private String printLine() {

@Override
public void step(int number) {
}

public static void main(String[] args) throws InterruptedException {
IdleBar bar = new IdleBar("Printing progress");
bar.start();
Thread.sleep(20000);
bar.dispose();
// does nothing, because the idle bar has no steps
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/de/jplag/logging/ProgressBarType.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public enum ProgressBarType {
LOADING("Loading Submissions ", false),
PARSING("Parsing Submissions ", false),
COMPARING("Comparing Submissions", false),
TokenStringNormalizer("Normalizing token Sequence ", true);
TOKEN_STRING_NORMALIZER("Normalizing token Sequence ", true);

private final String defaultText;
private final boolean isIdleBar;
Expand Down

0 comments on commit 2657be3

Please sign in to comment.