Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rewrite previous line in console?

Tags:

java

I'm trying to create process animation in my console app. Is it possible to rewrite previous lines for this needs? I know about \r but it works only with current line.

If it is not possible, how could I achieve animation effect? Thanks.

My console is standard Ubuntu 12.04 terminal emulator.


Thanks to @MrSmith42 I made this simple demo, which shows way to overwrite lines:

public class Flush {
    public static void main(String[] args) {
        for(int i = 0; i < 5; i++) {
            System.out.println("**********************************");
        }
        // ESC[5A - cursor up 5 times
        // \r - cursor return to begin of line
        // ESC[J - erase to end of screen
        System.out.print("\033[5A\r\033[J");
        for(int i = 0; i < 5; i++) {
            System.out.println("##################################");
        }
    }
}
like image 952
bsiamionau Avatar asked Feb 24 '13 12:02

bsiamionau


1 Answers

That depends on your console. Lot of consols support vt100 commands which allow e.g. changing the position of the cursor or change he color of text or background.

I use it a lot to make colored debug output in my java programs to the shell.

If the link is dead use this google search https://www.google.de/search?q=vt100+comands&oq=vt100+comands

like image 175
MrSmith42 Avatar answered Sep 22 '22 22:09

MrSmith42