Skip to content

Instantly share code, notes, and snippets.

@siordache
Last active November 24, 2017 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siordache/e0832eb09d7d09cde631f3840575761d to your computer and use it in GitHub Desktop.
Save siordache/e0832eb09d7d09cde631f3840575761d to your computer and use it in GitHub Desktop.
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