SHARE
TWEET

Restore Terminal Session

zacjszewczyk Aug 27th, 2019 197 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Original article: http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
  2. # Hat-tip to "dkrmr" for this tip, to restore terminal state after entering and then exiting this Python command line.
  3. # dkrmr's original Pastebin: https://pastebin.com/sxgknepk
  4.  
  5. diff --git a/source.py b/change.py
  6. index 340dffd..e33ea1f 100644
  7. --- a/source.py
  8. +++ b/change.py
  9. @@ -1,9 +1,11 @@
  10. -import sys, tty
  11. +import sys, tty, termios
  12.  def command_line():
  13. +    backup = termios.tcgetattr(sys.stdin)
  14.      tty.setraw(sys.stdin)
  15.      while True:
  16.          char = sys.stdin.read(1)
  17.          if ord(char) == 3: # CTRL-C
  18. +            termios.tcsetattr(sys.stdin, termios.TCSAFLUSH, backup)
  19.              break;
  20.          print ord(char)
  21.          sys.stdout.write(u"\u001b[1000D") # Move all the way left
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top