-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fix_terminal_after_binary_output.py
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
https://stackoverflow.com/questions/7938402 | ||
Terminal in broken state (invisible text / no echo) after exit() during input() / raw_input() | ||
""" | ||
|
||
import os | ||
import sys | ||
import atexit | ||
import subprocess | ||
|
||
def main_inner(): | ||
# break terminal | ||
sys.stdout.buffer.write(os.urandom(1024)) | ||
|
||
def main(): | ||
# register the exit handler only in the main function | ||
# when main_inner is called from somewhere else | ||
# then the caller is responsible for cleanup | ||
atexit.register(exit_handler) | ||
main_inner() | ||
|
||
def exit_handler(): | ||
# fix terminal after binary output | ||
# no. "stty sane" fails to reset the terminal cursor | ||
# stty is part of coreutils | ||
#subprocess.call(["stty", "sane"]) | ||
# tput is part of ncurses | ||
subprocess.call(["tput", "init"]) | ||
|
||
if __name__ == "__main__": | ||
main() |