Last active
November 24, 2017 10:23
-
-
Save siordache/e0832eb09d7d09cde631f3840575761d to your computer and use it in GitHub Desktop.
Flickering issue with Text-IO (https://github.com/beryx/text-io/issues/8#issuecomment-346731943)
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
package org.beryx.textio.demo; | |
import org.beryx.textio.TextIoFactory; | |
import org.beryx.textio.TextTerminal; | |
import java.util.Locale; | |
import java.util.Random; | |
public class TestFlickering { | |
private static final Random rnd = new Random(); | |
public static void main(String[] args) { | |
TextTerminal<?> textTerm = TextIoFactory.getTextTerminal(); | |
// No flickering here: | |
run(textTerm, 0); | |
// Flickering due to delay between the calls to resetLine and print: | |
run(textTerm, 80); | |
} | |
public static void run(TextTerminal<?> textTerm, long dataRetrievalDuration) { | |
textTerm.printf("getData() duration: %d millis\n", dataRetrievalDuration); | |
textTerm.println("--------------------------------------------------------"); | |
textTerm.println(" Total Cancelled Diverted Airports"); | |
textTerm.println("--------------------------------------------------------"); | |
for(int i=0; i<20; i++) { | |
textTerm.resetLine(); | |
textTerm.print(getData(dataRetrievalDuration)); | |
/* | |
// In order to avoid flickering, replace the above two lines with: | |
String infoText = getData(dataRetrievalDuration); | |
textTerm.resetLine(); | |
textTerm.print(infoText); | |
*/ | |
delay(400 - dataRetrievalDuration); | |
} | |
textTerm.println();textTerm.println();textTerm.println(); | |
} | |
/** | |
* Mocks a data retrieval operation. | |
* @param duration how long it takes to retrieve the data | |
* @return the info text to be displayed | |
*/ | |
public static String getData(long duration) { | |
delay(duration); | |
int total = 5000 + rnd.nextInt(2000); | |
int cancelled = 500 + rnd.nextInt(200); | |
int diverted = 25 + rnd.nextInt(10); | |
int airports = 94 + rnd.nextInt(4); | |
return String.format(Locale.US, "%,10d %15d %14d %14d", total, cancelled, diverted, airports); | |
} | |
public static void delay(long millis) { | |
try { | |
Thread.sleep(millis); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment