Search code examples
javabluej

Erasing previous output lines in java


I have been making a terminal styled... thing, and the only problem I'm still having trouble with (so far) is the loading screen. I simply wanted to add the illusion that it needed to load, nothing too complex. I need to delete the previous output line so it will load like, you know, a loading screen. I have looked through a lot of Q/As here and everything I tried didn't help I always either got random text or the text wasn't where it was supposed to be, or it plain didn't work. I am using Bluej.

Current Code:

public static void main(String [] args){
    try{
        System.out.println("▒█▀▀▀█ █▀▀█ █▀▀█ █▀▀█ ▀▀█▀▀ █▀▀█ █▀▀▄");
        Thread.sleep(1000);
        System.out.println("░▀▀▀▄▄ █░░█ █▄▄█ █▄▄▀ ░░█░░ █▄▄█ █░░█");
        Thread.sleep(1000);
        System.out.println("▒█▄▄▄█ █▀▀▀ ▀░░▀ ▀░▀▀ ░░▀░░ ▀░░▀ ▀░░▀");
        Thread.sleep(1000);
        System.out.println("o()xxxxxx[{:::::::::::::::::::::::::::::::>");
            Thread.sleep(1000);
            
            System.out.println("Loading... 0%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 13%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 42%");
            Thread.sleep(2000);
            //Code goes here
            System.out.println("Loading... 74%");
            Thread.sleep(4000);
            //Code goes here
            System.out.println("Loading... 92%");
            Thread.sleep(1000);
            //Code goes here
            System.out.println("Loading... 100%");
    }
    catch (InterruptedException e) {
        System.err.format("IOException: %s%n", e);
    }
}

I would like to post the full output, but the art won't mix with this, so here's what it says, minus the art:

Spartan

Loading... 0%

Loading... 13%

Loading... 42%

Loading... 74%

Loading... 92%

Loading... 100%

It shouldn't show several "Loading..."s

I am open to any suggestions, within reason.


Solution

  • have been making a terminal styled... thing

    System.out.println("▒█▀▀▀█ █▀▀█ █▀▀█ █▀▀█ ▀▀█▀▀ █▀▀█ █▀▀▄");

    The problem is, System.out is 'standard output' and not a terminal. Sure, out of the box, on pretty much any OS, if you type java -jar yourapp.jar, 'standard output' is hooked up to the terminal, and terminals as a fundamental concept support the notion of overwriting/erasing previously printed text.

    The problem is, the abstraction of 'standard output' allows you to hook up almost anything to standard out. You can write: java -jar yourapp.jar >/dev/printer and your printer will start rattling off your asciiart skills.

    Now you see the problem: It is a bit tricky to instruct the printer to grow a pair of legs, hop off cupboard, jump over to the desk of the gal that just grabbed the piece of paper with your ascii art and 'loading' text, and unprint the text.

    That's why System.out has no eraseLine method: Not all takes on what System.out is representing support such a principle.

    Fortunately (?), terminals do support it, but, they do so by sending them 'commands' which takes the form of sending them creative character sequences that the terminal then interprets as 'oh, you want me to hop over to this position, change the background colour to green, the foreground colour to blue. Okay, got it'.

    Unfortunately, there are many terminals out there (windows dos boxes, but once you get to the unix space, hoo boy, a list as long as my leg) and these differ in what magic sequences you have to send.

    Thus, get a library that took care of it. I'm sure you'll be very interested in this: Lanterna.