1

I know that a similar question has been asked here. The methods work well when the size of the string is not exceeded from a single line. Please consider the following code, which does not have a long string:

import time

for x in range(100):
    print(x, "times", end="\r")

    time.sleep(0.2)

Now consider the following code. You can see that it just clears the exceeded line not all characters of the printed string. I want the same behavior of the above code.

import time

for x in range(100):
    print(x, "timesjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj", end="\r")

    time.sleep(0.2)

Please note that I use VS code. How can I clear all characters when the size of the string is long?

mohammad
  • 175
  • 2
  • 13
  • 1
    What do you mean by overwrite long string? Post expected output :) – gajendragarg Feb 20 '22 at 15:52
  • Is this not answered in your linked post? `end='\x1b[1K\r'` – Alex Feb 20 '22 at 15:52
  • 1
    I think OP's issue is that the text spans multiple lines in the terminal, which makes `\r` not work for returning to the start of the original line. I might suggest using `curses` or similar if you need to overwrite multiple lines of text. – Samwise Feb 20 '22 at 15:56
  • @Alex No, the `end='\x1b[1K\r'` does not work for me. I think it justifies the different lengths of strings. However, it does not work for my purpose. – mohammad Feb 20 '22 at 15:57
  • @mohammad do you really need to clear the terminal? You can get the terminal size in advance with `shutil.get_terminal_size`. Then just print the first n characters. – AlexNe Feb 20 '22 at 16:01
  • `\r` doesn't clear anything; it only moves the cursor (if the terminal so interprets `\r` in that way). It's the subsequent text that *overwrites* whatever is there. – chepner Feb 20 '22 at 16:39
  • `'\x1b[1K'` *does* clear from the cursor back to the beginning of the line, *if* your terminal accepts ANSI escape codes. – chepner Feb 20 '22 at 16:40
  • It's better to use a library like `ncurses`, though, for terminal-independent manipulation. – chepner Feb 20 '22 at 16:41

2 Answers2

1

Maybe you can try this:

import time
import sys

CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'

for x in range(100):
    print(x, "timesjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj")
    sys.stdout.write(CURSOR_UP_ONE*2)
    sys.stdout.write(ERASE_LINE) 
    
    time.sleep(0.2)

And the count of CURSOR_UP_ONE depends on the length of the string and the width of the terminal window.

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
0

You can get the terminal size in advance:

from shutil import get_terminal_size


def print_width(s: str):
    tw, th = get_terminal_size((50, 20))

    if len(s) > tw:
        head, tail = s[:tw], s[tw:]
        print(head)
    else:
        print(s)


if __name__ == "__main__":
    my_really_long_str = "timesjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj"

    print_width(my_really_long_str)
AlexNe
  • 926
  • 6
  • 22
  • This function does the same thing. It just clears the latest exceeded line. However, I want to clear all characters of the printed string. – mohammad Feb 20 '22 at 16:18
  • Just to be clear, if the string is too long you don't want to see it on the terminal? – AlexNe Feb 20 '22 at 16:19
  • No, I want to overwrite the printed string. Consider that the size of the string is always long and I just want to overwrite the string without going to the new line. – mohammad Feb 20 '22 at 16:22
  • So you already have text on the console and if your newly printed string + what's already there exceeds the console width, you want to clear? – AlexNe Feb 20 '22 at 16:25
  • I edited the post. Please re-read it again if possible. – mohammad Feb 20 '22 at 16:33