`clear` doesn't take effect immediately on Linux
Hi! I'm writing a small script and I'm using click for user input and console text formatting. In practice, I use the click.getchar() function to read a single char from stdin, and do some processing from there; I construct a string representing a visual representation of the output and I print it. But! Whenever I use click.clear() instead of os.system('clear'), I get an inconsistent behavior: the terminal doesn't get cleared at the beginning of the script,
Here is a minimal reproducible example (imports omitted for brevity): script1.py
def main():
os.system('clear')
c = click.getchar()
if __name__ == '__main__':
main()
python3 script1.py -> the terminal gets cleared and there's only the blinking cursor; this is the expected behavior. Instead, here's script2.py:
def main():
click.clear()
c = click.getchar()
if __name__ == '__main__':
main()
The terminal doesn't get cleared, I can still see my username, the command, etc. Obviously this is trivial to circumvent; my implementation is:
def clearscreen(use_click=False):
click.clear() if use_click else os.system('cls' if os.name == 'nt' else 'clear')
Something like this could be mainlined in click.clear(), for example something like (taking from the current implementation):
def clear(scroll: bool = True) -> None:
if not isatty(sys.stdout):
return
if WIN:
os.system("cls")
else:
sys.stdout.write("\033[2J\033[1;1H") if scroll else os.system("clear")
Obviously the default value would follow the current behavior for consistency. I hope I've been thorough enough. Thanks!
- OS: Ubuntu 22.04
- Python version: 3.10.4
- Click version: 8.0.3
