Created
March 28, 2020 08:28
-
-
Save skhatri/cb4c3666459c8447f660c0d13de4dd99 to your computer and use it in GitHub Desktop.
Scroll Display Example
This file contains hidden or 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
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| import java.util.stream.IntStream; | |
| class Renderer { | |
| private void line() { | |
| System.out.println("***********************"); | |
| } | |
| private void title() { | |
| System.out.println(" Some App "); | |
| } | |
| private void asciiClear() { | |
| System.out.print("\033[H\033[2J"); | |
| } | |
| private void clear() { | |
| //only one may be required | |
| asciiClear(); | |
| clearUsingSysCall(); | |
| } | |
| private void clearUsingSysCall() { | |
| try { | |
| String os = System.getProperty("os.name").toLowerCase(); | |
| ProcessBuilder processRunner = new ProcessBuilder(); | |
| if (os.indexOf("win") != -1) { | |
| processRunner.command("cmd", "/c", "cls"); | |
| } else { | |
| processRunner.command("sh", "-c", "clear"); | |
| } | |
| processRunner.inheritIO().start().waitFor(); | |
| } catch (Exception ex) { | |
| } | |
| } | |
| private List<Integer> items = IntStream.range(1, 1000).boxed().collect(Collectors.toList()); | |
| private int marker = 0; | |
| private List<String> movingMessages() { | |
| List<String> batch = items.stream().dropWhile(i -> i <= marker).map(i -> String.format("Some value %d", i)).limit(3).collect(Collectors.toList()); | |
| marker = marker + 1; | |
| return batch; | |
| } | |
| private void display(List<String> thisLot) { | |
| clear(); | |
| StringBuilder builder = new StringBuilder("\r"); | |
| thisLot.stream().forEach(item -> builder.append(item).append("\n")); | |
| line(); | |
| title(); | |
| line(); | |
| System.out.print(builder.toString()); | |
| line(); | |
| } | |
| void execute() { | |
| while (true) { | |
| List<String> thisLot = movingMessages(); | |
| display(thisLot); | |
| line(); | |
| System.out.print("Your Input [q to quit]: "); | |
| String cmd = System.console().readLine(); | |
| if ("q".equals(cmd)) { | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| public class Display { | |
| public static void main(String[] args) { | |
| new Renderer().execute(); | |
| } | |
| } | |
| /** | |
| javac Display.java && java Display | |
| **/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment