From 4882d83d27b3d18b30c99e99f1b54db9a442d4ac Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 2 Apr 2024 13:27:01 +0200 Subject: [PATCH 1/8] Added idle bars --- cli/src/main/java/de/jplag/cli/CLI.java | 4 +- ...vider.java => CliProgressBarProvider.java} | 12 +- .../java/de/jplag/cli/logger/IdleBar.java | 112 ++++++++++++++++++ .../de/jplag/logging/ProgressBarLogger.java | 6 +- .../de/jplag/logging/ProgressBarType.java | 18 ++- .../normalization/TokenStringNormalizer.java | 5 + 6 files changed, 146 insertions(+), 11 deletions(-) rename cli/src/main/java/de/jplag/cli/logger/{TongfeiProgressBarProvider.java => CliProgressBarProvider.java} (51%) create mode 100644 cli/src/main/java/de/jplag/cli/logger/IdleBar.java diff --git a/cli/src/main/java/de/jplag/cli/CLI.java b/cli/src/main/java/de/jplag/cli/CLI.java index ac79e68c0..7c5a5aebd 100644 --- a/cli/src/main/java/de/jplag/cli/CLI.java +++ b/cli/src/main/java/de/jplag/cli/CLI.java @@ -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; @@ -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); diff --git a/cli/src/main/java/de/jplag/cli/logger/TongfeiProgressBarProvider.java b/cli/src/main/java/de/jplag/cli/logger/CliProgressBarProvider.java similarity index 51% rename from cli/src/main/java/de/jplag/cli/logger/TongfeiProgressBarProvider.java rename to cli/src/main/java/de/jplag/cli/logger/CliProgressBarProvider.java index da09fff33..23baf8f1f 100644 --- a/cli/src/main/java/de/jplag/cli/logger/TongfeiProgressBarProvider.java +++ b/cli/src/main/java/de/jplag/cli/logger/CliProgressBarProvider.java @@ -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); + } } } diff --git a/cli/src/main/java/de/jplag/cli/logger/IdleBar.java b/cli/src/main/java/de/jplag/cli/logger/IdleBar.java new file mode 100644 index 000000000..a68265c54 --- /dev/null +++ b/cli/src/main/java/de/jplag/cli/logger/IdleBar.java @@ -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(); + } +} diff --git a/core/src/main/java/de/jplag/logging/ProgressBarLogger.java b/core/src/main/java/de/jplag/logging/ProgressBarLogger.java index 889d391bb..b1563677f 100644 --- a/core/src/main/java/de/jplag/logging/ProgressBarLogger.java +++ b/core/src/main/java/de/jplag/logging/ProgressBarLogger.java @@ -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 diff --git a/core/src/main/java/de/jplag/logging/ProgressBarType.java b/core/src/main/java/de/jplag/logging/ProgressBarType.java index 88e520fcc..e9772d3a7 100644 --- a/core/src/main/java/de/jplag/logging/ProgressBarType.java +++ b/core/src/main/java/de/jplag/logging/ProgressBarType.java @@ -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; } /** @@ -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; + } } diff --git a/core/src/main/java/de/jplag/normalization/TokenStringNormalizer.java b/core/src/main/java/de/jplag/normalization/TokenStringNormalizer.java index 5ece0ff1f..52a2ae08a 100644 --- a/core/src/main/java/de/jplag/normalization/TokenStringNormalizer.java +++ b/core/src/main/java/de/jplag/normalization/TokenStringNormalizer.java @@ -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. @@ -28,6 +31,7 @@ private TokenStringNormalizer() { * @return The normalized token sequence. */ public static List normalize(List tokens) { + ProgressBar progressBar = ProgressBarLogger.createProgressBar(ProgressBarType.TokenStringNormalizer, 0); SimpleDirectedGraph normalizationGraph = new NormalizationGraphConstructor(tokens).get(); List normalizedTokens = new ArrayList<>(tokens.size()); spreadKeep(normalizationGraph); @@ -50,6 +54,7 @@ public static List normalize(List tokens) { } while (!roots.isEmpty()); roots = newRoots; } + progressBar.dispose(); return normalizedTokens; } From 7338ebb51d1e0ba60544a44210e13280ddf8011e Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 9 Apr 2024 09:39:59 +0200 Subject: [PATCH 2/8] Adjusted idle bar animation. --- cli/src/main/java/de/jplag/cli/logger/IdleBar.java | 14 +++----------- .../jplag/normalization/TokenStringNormalizer.java | 5 ----- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/cli/src/main/java/de/jplag/cli/logger/IdleBar.java b/cli/src/main/java/de/jplag/cli/logger/IdleBar.java index a68265c54..93ec96718 100644 --- a/cli/src/main/java/de/jplag/cli/logger/IdleBar.java +++ b/cli/src/main/java/de/jplag/cli/logger/IdleBar.java @@ -18,8 +18,6 @@ public class IdleBar implements ProgressBar { private final String text; private int length; - String emptyLine; - private int currentPos; private int currentDirection; @@ -33,7 +31,7 @@ public IdleBar(String text) { this.text = text; try { Terminal terminal = TerminalBuilder.terminal(); - this.length = terminal.getWidth() / 2; + this.length = Math.min(terminal.getWidth() / 2, terminal.getWidth() - 50); terminal.close(); } catch (IOException e) { throw new RuntimeException(e); @@ -41,12 +39,6 @@ public IdleBar(String text) { 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() { @@ -73,7 +65,7 @@ private void run() { currentDirection *= -1; } try { - Thread.sleep(100); + Thread.sleep(200); } catch (InterruptedException ignore) { // ignore wakeup } @@ -106,7 +98,7 @@ public void step(int number) { public static void main(String[] args) throws InterruptedException { IdleBar bar = new IdleBar("Printing progress"); bar.start(); - Thread.sleep(10000); + Thread.sleep(20000); bar.dispose(); } } diff --git a/core/src/main/java/de/jplag/normalization/TokenStringNormalizer.java b/core/src/main/java/de/jplag/normalization/TokenStringNormalizer.java index 52a2ae08a..5ece0ff1f 100644 --- a/core/src/main/java/de/jplag/normalization/TokenStringNormalizer.java +++ b/core/src/main/java/de/jplag/normalization/TokenStringNormalizer.java @@ -11,9 +11,6 @@ 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. @@ -31,7 +28,6 @@ private TokenStringNormalizer() { * @return The normalized token sequence. */ public static List normalize(List tokens) { - ProgressBar progressBar = ProgressBarLogger.createProgressBar(ProgressBarType.TokenStringNormalizer, 0); SimpleDirectedGraph normalizationGraph = new NormalizationGraphConstructor(tokens).get(); List normalizedTokens = new ArrayList<>(tokens.size()); spreadKeep(normalizationGraph); @@ -54,7 +50,6 @@ public static List normalize(List tokens) { } while (!roots.isEmpty()); roots = newRoots; } - progressBar.dispose(); return normalizedTokens; } From 0a8cd8107cff28f4888d91e0cff612c16ca3cb28 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 9 Apr 2024 10:31:01 +0200 Subject: [PATCH 3/8] Added a test for idle bars. --- .../java/de/jplag/cli/logger/IdleBarTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java diff --git a/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java b/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java new file mode 100644 index 000000000..7baebc6e7 --- /dev/null +++ b/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java @@ -0,0 +1,74 @@ +package de.jplag.cli.logger; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +class IdleBarTest { + private static final String TEST_BAR_TEXT = "Test"; + private static final long TEST_TIME = 10000; + private static final long IDLE_BAR_ANIMATION_DELAY = 200; + + /** + * Tests if the output of the idle bar looks plausible + */ + @Test + void testIdleBarPlausible() { + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + PrintStream oldSystemOut = System.out; + System.setOut(new PrintStream(outputStream)); + + IdleBar idleBar = new IdleBar(TEST_BAR_TEXT); + idleBar.start(); + try { + Thread.sleep(TEST_TIME); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + idleBar.dispose(); + System.setOut(oldSystemOut); + + String result = outputStream.toString(); + String[] animationFrames = result.substring(1).split("\\r"); + Assertions.assertTrue(Math.abs(animationFrames.length - (TEST_TIME / IDLE_BAR_ANIMATION_DELAY)) <= 2, "Unexpected number of animation frames."); + + String firstFrame = animationFrames[0]; + int numberOfSpaces = firstFrame.lastIndexOf('>') - firstFrame.indexOf('<') - 3 - 1; + for (int i = 0; i < animationFrames.length; i++) { + checkIdleBarOutput(animationFrames[i], i, numberOfSpaces); + } + } + + /** + * Checks that the given string matches the expected content of an animation frame + * + * @param output The animation frame + * @param frameIndex The index of the frame + * @param numberOfSpaces The number of spaces within the bar + */ + private void checkIdleBarOutput(String output, int frameIndex, int numberOfSpaces) { + int pass = frameIndex / numberOfSpaces; + int offset = frameIndex % numberOfSpaces; + if (pass % 2 == 1) { + offset = numberOfSpaces - offset; + } + + String expectedOutput = TEST_BAR_TEXT + ' ' + '<' + + " ".repeat(offset) + "<+>" + " ".repeat(numberOfSpaces - offset) + + '>'; + + int endOfPredictableOutput = output.lastIndexOf(' '); + String predictableOutput = output.substring(0, endOfPredictableOutput); + String time = output.substring(endOfPredictableOutput + 1).trim(); + + Assertions.assertEquals(expectedOutput, predictableOutput); + Assertions.assertTrue(time.matches("[0-9]:[0-9]{2}:[0-9]{2}"), "Invalid format for time"); + + String[] timeParts = time.split(":"); + int seconds = Integer.parseInt(timeParts[0]) * 60 * 60 + Integer.parseInt(timeParts[1]) * 60 + Integer.parseInt(timeParts[2]); + int expectedTime = (int) ((IDLE_BAR_ANIMATION_DELAY * frameIndex) / 1000); + Assertions.assertTrue(Math.abs(seconds - expectedTime) < 1, "Frame time of by more than one second"); + } +} \ No newline at end of file From 1339bfcee4bce644ebbbd791924b13a825536a66 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 9 Apr 2024 10:31:32 +0200 Subject: [PATCH 4/8] spotless --- .../java/de/jplag/cli/logger/IdleBarTest.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java b/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java index 7baebc6e7..21315d5de 100644 --- a/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java +++ b/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java @@ -1,11 +1,11 @@ package de.jplag.cli.logger; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + class IdleBarTest { private static final String TEST_BAR_TEXT = "Test"; private static final long TEST_TIME = 10000; @@ -32,7 +32,8 @@ void testIdleBarPlausible() { String result = outputStream.toString(); String[] animationFrames = result.substring(1).split("\\r"); - Assertions.assertTrue(Math.abs(animationFrames.length - (TEST_TIME / IDLE_BAR_ANIMATION_DELAY)) <= 2, "Unexpected number of animation frames."); + Assertions.assertTrue(Math.abs(animationFrames.length - (TEST_TIME / IDLE_BAR_ANIMATION_DELAY)) <= 2, + "Unexpected number of animation frames."); String firstFrame = animationFrames[0]; int numberOfSpaces = firstFrame.lastIndexOf('>') - firstFrame.indexOf('<') - 3 - 1; @@ -43,9 +44,8 @@ void testIdleBarPlausible() { /** * Checks that the given string matches the expected content of an animation frame - * - * @param output The animation frame - * @param frameIndex The index of the frame + * @param output The animation frame + * @param frameIndex The index of the frame * @param numberOfSpaces The number of spaces within the bar */ private void checkIdleBarOutput(String output, int frameIndex, int numberOfSpaces) { @@ -55,9 +55,7 @@ private void checkIdleBarOutput(String output, int frameIndex, int numberOfSpace offset = numberOfSpaces - offset; } - String expectedOutput = TEST_BAR_TEXT + ' ' + '<' + - " ".repeat(offset) + "<+>" + " ".repeat(numberOfSpaces - offset) + - '>'; + String expectedOutput = TEST_BAR_TEXT + ' ' + '<' + " ".repeat(offset) + "<+>" + " ".repeat(numberOfSpaces - offset) + '>'; int endOfPredictableOutput = output.lastIndexOf(' '); String predictableOutput = output.substring(0, endOfPredictableOutput); From 2657be3744816de5129887f88c5e82ca6389ca61 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Tue, 9 Apr 2024 16:03:48 +0200 Subject: [PATCH 5/8] Improved code style for IdleBar.java --- .../java/de/jplag/cli/logger/IdleBar.java | 25 +++++++++---------- .../de/jplag/logging/ProgressBarType.java | 2 +- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/cli/src/main/java/de/jplag/cli/logger/IdleBar.java b/cli/src/main/java/de/jplag/cli/logger/IdleBar.java index 93ec96718..fe2fb723c 100644 --- a/cli/src/main/java/de/jplag/cli/logger/IdleBar.java +++ b/cli/src/main/java/de/jplag/cli/logger/IdleBar.java @@ -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; @@ -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; @@ -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; @@ -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; @@ -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; } @@ -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 } } diff --git a/core/src/main/java/de/jplag/logging/ProgressBarType.java b/core/src/main/java/de/jplag/logging/ProgressBarType.java index e9772d3a7..646bac1e7 100644 --- a/core/src/main/java/de/jplag/logging/ProgressBarType.java +++ b/core/src/main/java/de/jplag/logging/ProgressBarType.java @@ -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; From e4d4f33ab6cb4da9576804a23a18a405797c6e41 Mon Sep 17 00:00:00 2001 From: Alexander Milster Date: Wed, 17 Apr 2024 10:55:28 +0200 Subject: [PATCH 6/8] Fixed issues with idle bars --- .../test/java/de/jplag/cli/logger/IdleBarTest.java | 14 ++++++-------- .../de/jplag/clustering/ClusteringFactory.java | 7 +++++++ .../java/de/jplag/logging/ProgressBarType.java | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java b/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java index 21315d5de..88bf382db 100644 --- a/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java +++ b/cli/src/test/java/de/jplag/cli/logger/IdleBarTest.java @@ -8,9 +8,10 @@ class IdleBarTest { private static final String TEST_BAR_TEXT = "Test"; - private static final long TEST_TIME = 10000; private static final long IDLE_BAR_ANIMATION_DELAY = 200; + private static final int TARGET_FRAME_NUMBER = 5; + /** * Tests if the output of the idle bar looks plausible */ @@ -22,22 +23,19 @@ void testIdleBarPlausible() { IdleBar idleBar = new IdleBar(TEST_BAR_TEXT); idleBar.start(); - try { - Thread.sleep(TEST_TIME); - } catch (InterruptedException e) { - throw new RuntimeException(e); + while (outputStream.toString().split("\\r").length <= TARGET_FRAME_NUMBER) { + Thread.yield(); } + idleBar.dispose(); System.setOut(oldSystemOut); String result = outputStream.toString(); String[] animationFrames = result.substring(1).split("\\r"); - Assertions.assertTrue(Math.abs(animationFrames.length - (TEST_TIME / IDLE_BAR_ANIMATION_DELAY)) <= 2, - "Unexpected number of animation frames."); String firstFrame = animationFrames[0]; int numberOfSpaces = firstFrame.lastIndexOf('>') - firstFrame.indexOf('<') - 3 - 1; - for (int i = 0; i < animationFrames.length; i++) { + for (int i = 0; i < TARGET_FRAME_NUMBER; i++) { checkIdleBarOutput(animationFrames[i], i, numberOfSpaces); } } diff --git a/core/src/main/java/de/jplag/clustering/ClusteringFactory.java b/core/src/main/java/de/jplag/clustering/ClusteringFactory.java index 7351493e1..a96f06d35 100644 --- a/core/src/main/java/de/jplag/clustering/ClusteringFactory.java +++ b/core/src/main/java/de/jplag/clustering/ClusteringFactory.java @@ -12,6 +12,9 @@ import de.jplag.JPlagComparison; import de.jplag.Submission; import de.jplag.clustering.algorithm.GenericClusteringAlgorithm; +import de.jplag.logging.ProgressBar; +import de.jplag.logging.ProgressBarLogger; +import de.jplag.logging.ProgressBarType; /** * Runs the clustering according to an options object. @@ -42,6 +45,8 @@ public static List> getClusterings(Collection> getClusterings(Collection Date: Tue, 23 Apr 2024 09:35:47 +0200 Subject: [PATCH 7/8] Fixed idle bars not showing. --- .../main/java/de/jplag/cli/logger/CliProgressBarProvider.java | 4 +++- core/src/main/java/de/jplag/clustering/ClusteringFactory.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cli/src/main/java/de/jplag/cli/logger/CliProgressBarProvider.java b/cli/src/main/java/de/jplag/cli/logger/CliProgressBarProvider.java index 23baf8f1f..ebfd98dd9 100644 --- a/cli/src/main/java/de/jplag/cli/logger/CliProgressBarProvider.java +++ b/cli/src/main/java/de/jplag/cli/logger/CliProgressBarProvider.java @@ -14,7 +14,9 @@ public class CliProgressBarProvider implements ProgressBarProvider { @Override public ProgressBar initProgressBar(ProgressBarType type, int totalSteps) { if (type.isIdleBar()) { - return new IdleBar(type.getDefaultText()); + IdleBar idleBar = new IdleBar(type.getDefaultText()); + idleBar.start(); + return idleBar; } else { me.tongfei.progressbar.ProgressBar progressBar = new ProgressBarBuilder().setTaskName(type.getDefaultText()).setInitialMax(totalSteps) .setStyle(ProgressBarStyle.ASCII).build(); diff --git a/core/src/main/java/de/jplag/clustering/ClusteringFactory.java b/core/src/main/java/de/jplag/clustering/ClusteringFactory.java index a96f06d35..3db208f81 100644 --- a/core/src/main/java/de/jplag/clustering/ClusteringFactory.java +++ b/core/src/main/java/de/jplag/clustering/ClusteringFactory.java @@ -66,9 +66,9 @@ public static List> getClusterings(Collection Date: Thu, 2 May 2024 09:34:16 +0200 Subject: [PATCH 8/8] Moved logging for clusters to trace. --- core/src/main/java/de/jplag/clustering/ClusteringFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/de/jplag/clustering/ClusteringFactory.java b/core/src/main/java/de/jplag/clustering/ClusteringFactory.java index 3db208f81..de2bc1e60 100644 --- a/core/src/main/java/de/jplag/clustering/ClusteringFactory.java +++ b/core/src/main/java/de/jplag/clustering/ClusteringFactory.java @@ -81,7 +81,7 @@ private static ClusteringResult removeBadClusters(final ClusteringRe private static void logClusters(ClusteringResult result) { var clusters = new ArrayList<>(result.getClusters()); clusters.sort((first, second) -> Double.compare(second.getAverageSimilarity(), first.getAverageSimilarity())); - logger.info(CLUSTERING_RESULT, clusters.size()); + logger.trace(CLUSTERING_RESULT, clusters.size()); clusters.forEach(ClusteringFactory::logCluster); } @@ -89,7 +89,7 @@ private static void logCluster(Cluster cluster) { String members = membersToString(cluster.getMembers()); String similarity = String.format(SIMILARITY_FORMAT, cluster.getAverageSimilarity() * 100); String strength = String.format(STRENGTH_FORMAT, cluster.getCommunityStrength()); - logger.info(CLUSTER_PATTERN, similarity, strength, cluster.getMembers().size(), members); + logger.trace(CLUSTER_PATTERN, similarity, strength, cluster.getMembers().size(), members); } private static String membersToString(Collection members) {