Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear Windows Terminal buffer before diffing #40

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,12 @@ def run_less(self) -> "Tuple[subprocess.Popen[bytes], subprocess.Popen[bytes]]":
header_lines = [header] if header else []
output = "\n".join(header_lines + diff_lines[args.skip_lines :])

# Windows Terminal does not handle buffers properly
# Janky hack to clear its scrollback buffer until it's fixed
clear_proc = subprocess.Popen(
["echo", "-en", "\"\e[3J\""]
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Popen() starts a new process that runs asynchronously; we'd need to wait for it to finish if we do this (clear_proc.wait() or subprocess.run()/similar). Does it work if you instead do

        sys.stdout.write("\x1b7[3J\"")
        sys.stdout.flush()

?

There is a os.system("tput reset") further down which is similar to this btw, but I guess it doesn't help for whatever reason (and it doesn't do anything for the very first less call).

Copy link
Author

@Kelebek1 Kelebek1 Mar 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stdout writes don't work unfortunately. It looks like tput reset does work as well, but the issue I have with that is that it clears all of your previous console output after closing the diff, but it's nice to keep previous commands visible.


# Pipe the output through 'tail' and only then to less, to ensure the
# write call doesn't block. ('tail' has to buffer all its input before
# it starts writing.) This also means we don't have to deal with pipe
Expand Down