Is it possible to rewrite previous line in console?

ε祈祈猫儿з 提交于 2021-02-18 10:21:55

问题


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("##################################");
        }
    }
}

回答1:


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



来源:https://stackoverflow.com/questions/15051688/is-it-possible-to-rewrite-previous-line-in-console

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!