Repository: pallets/click

Issue:
`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()
```
`python3 script2.py` -> see: ![image](https://user-images.githubusercontent.com/61554895/168082063-b9b65889-7c3f-4a9b-a61e-0888ec3b9321.png)

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


Discussion:
Using `click.echo` instead of `sys.stdin.write` ensures that the control code is flushed and takes effect before the next terminal interaction. Until a fix is released, you can manually do `sys.stdout.flush()` after `click.clear()`.

Also, there's no need for the `if WIN` section anymore. Colorama is always required on Windows now, and it handles the ANSI clear all code.
Yes I was thinking it had to do something with the flushing of the buffer. The last snippet of code I pasted was modified from `click.clear()`, maybe it can be rearranged with your suggestions like:
```
def clear() -> None:
    if not isatty(sys.stdout):
        return
    echo("\033[2J\033[1;1H", nl=False)
```
What do you think?
Yes, I tried that earlier on Linux and Windows and it worked. You're welcome to make a PR, or I will later.
If it's not urgent I'll do it in a few hours (tomorrow)

Resolving patch:
diff --git a/src/click/termui.py b/src/click/termui.py
--- a/src/click/termui.py
+++ b/src/click/termui.py
@@ -1,14 +1,12 @@
 import inspect
 import io
 import itertools
-import os
 import sys
 import typing as t
 from gettext import gettext as _
 
 from ._compat import isatty
 from ._compat import strip_ansi
-from ._compat import WIN
 from .exceptions import Abort
 from .exceptions import UsageError
 from .globals import resolve_color_default
@@ -443,10 +441,9 @@ def clear() -> None:
     """
     if not isatty(sys.stdout):
         return
-    if WIN:
-        os.system("cls")
-    else:
-        sys.stdout.write("\033[2J\033[1;1H")
+
+    # ANSI escape \033[2J clears the screen, \033[1;1H moves the cursor
+    echo("\033[2J\033[1;1H", nl=False)
 
 
 def _interpret_color(


Test patch: