Skip to content

Commit

Permalink
Added idle bars
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoOfTwelve committed Apr 2, 2024
1 parent 665e7f1 commit 4882d83
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cli/src/main/java/de/jplag/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import de.jplag.JPlag;
import de.jplag.JPlagResult;
import de.jplag.Language;
import de.jplag.cli.logger.CliProgressBarProvider;
import de.jplag.cli.logger.CollectedLoggerFactory;
import de.jplag.cli.logger.TongfeiProgressBarProvider;
import de.jplag.cli.server.ReportViewer;
import de.jplag.clustering.ClusteringOptions;
import de.jplag.clustering.Preprocessing;
Expand Down Expand Up @@ -85,7 +85,7 @@ public static void main(String[] args) {
ParseResult parseResult = cli.parseOptions(args);

if (!parseResult.isUsageHelpRequested() && !(parseResult.subcommand() != null && parseResult.subcommand().isUsageHelpRequested())) {
ProgressBarLogger.setProgressBarProvider(new TongfeiProgressBarProvider());
ProgressBarLogger.setProgressBarProvider(new CliProgressBarProvider());
switch (cli.options.mode) {
case RUN -> cli.runJPlag(parseResult);
case VIEW -> cli.runViewer(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
/**
* A ProgressBar provider, that used the tongfei progress bar library underneath, to show progress bars on the cli.
*/
public class TongfeiProgressBarProvider implements ProgressBarProvider {
public class CliProgressBarProvider implements ProgressBarProvider {
@Override
public ProgressBar initProgressBar(ProgressBarType type, int totalSteps) {
me.tongfei.progressbar.ProgressBar progressBar = new ProgressBarBuilder().setTaskName(type.getDefaultText()).setInitialMax(totalSteps)
.setStyle(ProgressBarStyle.ASCII).build();
return new TongfeiProgressBar(progressBar);
if (type.isIdleBar()) {
return new IdleBar(type.getDefaultText());
} else {
me.tongfei.progressbar.ProgressBar progressBar = new ProgressBarBuilder().setTaskName(type.getDefaultText()).setInitialMax(totalSteps)
.setStyle(ProgressBarStyle.ASCII).build();
return new TongfeiProgressBar(progressBar);
}
}
}
112 changes: 112 additions & 0 deletions cli/src/main/java/de/jplag/cli/logger/IdleBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package de.jplag.cli.logger;

import java.io.IOException;

import org.apache.commons.lang3.time.DurationFormatUtils;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;

import de.jplag.logging.ProgressBar;

/**
* Prints an idle progress bar, that does not count upwards.
*/
public class IdleBar implements ProgressBar {
private final Thread runner;

private long startTime;
private final String text;
private int length;

String emptyLine;

private int currentPos;
private int currentDirection;

private boolean running = false;

public IdleBar(String text) {
this.runner = new Thread(this::run);
this.length = 50;
this.currentDirection = -1;
this.currentPos = 0;
this.text = text;
try {
Terminal terminal = TerminalBuilder.terminal();
this.length = terminal.getWidth() / 2;
terminal.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (this.length < 10) {
this.length = 10;
}

StringBuilder empty = new StringBuilder();
empty.append('\r');
empty.append(" ".repeat(Math.max(0, length + 4 + text.length() + 10)));
empty.append('\r');
this.emptyLine = empty.toString();
}

public void start() {
this.startTime = System.currentTimeMillis();
this.running = true;
this.runner.start();
}

@Override
public void dispose() {
this.running = false;
try {
this.runner.join();
} catch (InterruptedException ignored) {
}
System.out.println();
}

private void run() {
while (running) {
System.out.print('\r');
System.out.print(printLine());
if (currentPos == 0 || currentPos == length - 1) {
currentDirection *= -1;
}
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
// ignore wakeup
}
currentPos += currentDirection;
}
}

private String printLine() {
StringBuilder line = new StringBuilder();
line.append(this.text).append(' ');

line.append('<');
line.append(" ".repeat(Math.max(0, currentPos)));
line.append("<+>");
line.append(" ".repeat(Math.max(0, length - currentPos - 1)));
line.append('>');

long timeRunning = System.currentTimeMillis() - this.startTime;
line.append(' ');
String duration = DurationFormatUtils.formatDuration(timeRunning, "H:mm:ss");
line.append(duration);

return line.toString();
}

@Override
public void step(int number) {
}

public static void main(String[] args) throws InterruptedException {
IdleBar bar = new IdleBar("Printing progress");
bar.start();
Thread.sleep(10000);
bar.dispose();
}
}
6 changes: 5 additions & 1 deletion core/src/main/java/de/jplag/logging/ProgressBarLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ private static class DummyBar implements ProgressBar {

public DummyBar(ProgressBarType type, int totalSteps) {
this.currentStep = 0;
logger.info("{} ({})", type.getDefaultText(), totalSteps);
if (type.isIdleBar()) {
logger.info("{} - started", type.getDefaultText());
} else {
logger.info("{} ({})", type.getDefaultText(), totalSteps);
}
}

@Override
Expand Down
18 changes: 14 additions & 4 deletions core/src/main/java/de/jplag/logging/ProgressBarType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
* The available processes. Used as a hint for the ui, which step JPlag is currently performing.
*/
public enum ProgressBarType {
LOADING("Loading Submissions "),
PARSING("Parsing Submissions "),
COMPARING("Comparing Submissions");
LOADING("Loading Submissions ", false),
PARSING("Parsing Submissions ", false),
COMPARING("Comparing Submissions", false),
TokenStringNormalizer("Normalizing token Sequence ", true);

private final String defaultText;
private final boolean isIdleBar;

ProgressBarType(String defaultText) {
ProgressBarType(String defaultText, boolean isIdleBar) {
this.defaultText = defaultText;
this.isIdleBar = isIdleBar;
}

/**
Expand All @@ -20,4 +23,11 @@ public enum ProgressBarType {
public String getDefaultText() {
return defaultText;
}

/**
* @return True, if this bar should be rendered as an idle bar instead.
*/
public boolean isIdleBar() {
return isIdleBar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.jgrapht.graph.SimpleDirectedGraph;

import de.jplag.Token;
import de.jplag.logging.ProgressBar;
import de.jplag.logging.ProgressBarLogger;
import de.jplag.logging.ProgressBarType;

/**
* Performs token sequence normalization.
Expand All @@ -28,6 +31,7 @@ private TokenStringNormalizer() {
* @return The normalized token sequence.
*/
public static List<Token> normalize(List<Token> tokens) {
ProgressBar progressBar = ProgressBarLogger.createProgressBar(ProgressBarType.TokenStringNormalizer, 0);
SimpleDirectedGraph<Statement, MultipleEdge> normalizationGraph = new NormalizationGraphConstructor(tokens).get();
List<Token> normalizedTokens = new ArrayList<>(tokens.size());
spreadKeep(normalizationGraph);
Expand All @@ -50,6 +54,7 @@ public static List<Token> normalize(List<Token> tokens) {
} while (!roots.isEmpty());
roots = newRoots;
}
progressBar.dispose();
return normalizedTokens;
}

Expand Down

0 comments on commit 4882d83

Please sign in to comment.