Skip to content

Commit

Permalink
add fix_terminal_after_binary_output.py
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed May 12, 2024
1 parent c244d6c commit de6cd67
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions python/fix_terminal_after_binary_output.py
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()

0 comments on commit de6cd67

Please sign in to comment.