Is there go up line character? (Opposite of \n)

后端 未结 5 1226
暗喜
暗喜 2020-11-27 14:07

I would like to overwrite something on a line above in in a serial console. Is there a character that allows me to move up?

Thank you.

5条回答
  •  情歌与酒
    2020-11-27 14:41

    Carriage return can be used to go to the beginning of line, and ANSI code ESC A ("\033[A") can bring you up a line. This works on Linux. It can work on Windows by using the colorama package to enable ANSI codes:

    import time
    import sys
    import colorama
    
    colorama.init()
    
    print("Line 1")
    time.sleep(1)
    print("Line 2")
    time.sleep(1)
    print("Line 3 (no eol)", end="")
    sys.stdout.flush()
    time.sleep(1)
    print("\rLine 3 the sequel")
    time.sleep(1)
    print("\033[ALine 3 the second sequel")
    time.sleep(1)
    print("\033[A\033[A\033[ALine 1 the sequel")
    time.sleep(1)
    print()  # skip two lines so that lines 2 and 3 don't get overwritten by the next console prompt
    print()
    

    Output:

    > python3 multiline.py
    Line 1 the sequel
    Line 2
    Line 3 the second sequel
    >
    

    Under the hood, colorama presumably enables Console Virtual Terminal Sequences using SetConsoleMode.

提交回复
热议问题