Term.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2016 Philemon <mmgen-py@yandex.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen_node_tools.Term: terminal routines for MMGen node tools
  20. """
  21. import sys,os,termios
  22. def get_keypress(prompt="",esc_sequences=False):
  23. import time,tty,select
  24. sys.stderr.write(prompt)
  25. fd = sys.stdin.fileno()
  26. # old = termios.tcgetattr(fd) # see below
  27. tty.setcbreak(fd) # must do this, even if it was set at program launch
  28. def osread_chk(n):
  29. while True:
  30. try:
  31. return os.read(fd,n)
  32. except:
  33. time.sleep(0.1)
  34. # Must use os.read() for unbuffered read, otherwise select() will never return true
  35. s = osread_chk(1)
  36. if esc_sequences:
  37. if s == '\x1b':
  38. if select.select([sys.stdin],[],[],0)[0]:
  39. s += osread_chk(2)
  40. # Leave the term in cbreak mode, restore at exit
  41. # termios.tcsetattr(fd, termios.TCSADRAIN, old)
  42. return s