-
Notifications
You must be signed in to change notification settings - Fork 332
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 #1866 from jplag/feature/avoidProgressBarOverlap
Avoid progress bar overlap
- Loading branch information
Showing
6 changed files
with
157 additions
and
10 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
73 changes: 73 additions & 0 deletions
73
cli/src/main/java/de/jplag/cli/logger/DelayablePrinter.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,73 @@ | ||
package de.jplag.cli.logger; | ||
|
||
import java.io.PrintStream; | ||
import java.util.PriorityQueue; | ||
import java.util.Queue; | ||
|
||
/** | ||
* Prints strings to stdout. Provides the option to delay the actual printing. | ||
*/ | ||
public class DelayablePrinter { | ||
private final Queue<String> outputQueue; | ||
private PrintStream targetStream; | ||
|
||
private boolean isDelayed; | ||
|
||
private static final class InstanceHolder { | ||
private static final DelayablePrinter instance = new DelayablePrinter(); | ||
} | ||
|
||
/** | ||
* Threadsafe singleton getter | ||
* @return The singleton instance | ||
*/ | ||
public static DelayablePrinter getInstance() { | ||
return InstanceHolder.instance; | ||
} | ||
|
||
private DelayablePrinter() { | ||
this.outputQueue = new PriorityQueue<>(); | ||
this.targetStream = System.out; | ||
this.isDelayed = false; | ||
} | ||
|
||
/** | ||
* Prints the given string to the terminal appending a line-break | ||
* @param output The string to print | ||
*/ | ||
public synchronized void println(String output) { | ||
this.outputQueue.offer(output); | ||
this.printQueue(); | ||
} | ||
|
||
/** | ||
* Stops printing to the terminal until {@link #resume()} is called | ||
*/ | ||
public synchronized void delay() { | ||
this.isDelayed = true; | ||
} | ||
|
||
/** | ||
* Resumes printing if {@link #delay()} was called | ||
*/ | ||
public synchronized void resume() { | ||
this.isDelayed = false; | ||
this.printQueue(); | ||
} | ||
|
||
/** | ||
* Changes the output stream messages are written to | ||
*/ | ||
public void setOutputStream(PrintStream printStream) { | ||
this.targetStream = printStream; | ||
} | ||
|
||
private synchronized void printQueue() { | ||
if (!this.isDelayed) { | ||
while (!this.outputQueue.isEmpty()) { | ||
this.targetStream.println(this.outputQueue.poll()); | ||
} | ||
this.targetStream.flush(); | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
cli/src/main/java/de/jplag/cli/logger/LogDelayingProgressBar.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,17 @@ | ||
package de.jplag.cli.logger; | ||
|
||
import de.jplag.logging.ProgressBar; | ||
|
||
/** | ||
* Superclass for progress bars, that delay the log output until the bar is done | ||
*/ | ||
public abstract class LogDelayingProgressBar implements ProgressBar { | ||
protected LogDelayingProgressBar() { | ||
DelayablePrinter.getInstance().delay(); | ||
} | ||
|
||
@Override | ||
public void dispose() { | ||
DelayablePrinter.getInstance().resume(); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
cli/src/test/java/de/jplag/cli/logger/DelayablePrinterTest.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,51 @@ | ||
package de.jplag.cli.logger; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DelayablePrinterTest { | ||
private static final String TEST_MESSAGE = "Hello World"; | ||
|
||
private static ByteArrayOutputStream outputStream; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
outputStream = new ByteArrayOutputStream(); | ||
DelayablePrinter.getInstance().setOutputStream(new PrintStream(outputStream)); | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() { | ||
DelayablePrinter.getInstance().setOutputStream(System.out); | ||
} | ||
|
||
@AfterEach | ||
void cleanUpAfterTest() { | ||
DelayablePrinter.getInstance().resume(); | ||
outputStream.reset(); | ||
} | ||
|
||
@Test | ||
void testDelay() { | ||
DelayablePrinter.getInstance().delay(); | ||
DelayablePrinter.getInstance().println(TEST_MESSAGE); | ||
|
||
Assertions.assertEquals("", outputStream.toString()); | ||
|
||
DelayablePrinter.getInstance().resume(); | ||
|
||
Assertions.assertEquals(TEST_MESSAGE + System.lineSeparator(), outputStream.toString()); | ||
} | ||
|
||
@Test | ||
void testDirectPrinting() { | ||
DelayablePrinter.getInstance().println(TEST_MESSAGE); | ||
Assertions.assertEquals(TEST_MESSAGE + System.lineSeparator(), outputStream.toString()); | ||
} | ||
} |