Guest User

Untitled

a guest
Jan 17th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. import sys
  2. from string import printable
  3. import termios
  4. import tty
  5.  
  6. class _Getch:
  7. """Gets a single character from standard input. Does not echo to the
  8. screen."""
  9. def __init__(self):
  10. try:
  11. self.impl = _GetchWindows()
  12. except ImportError:
  13. self.impl = _GetchUnix()
  14.  
  15. def __call__(self): return self.impl()
  16. class _GetchUnix:
  17. def __init__(self):
  18. import tty, sys
  19.  
  20. def __call__(self):
  21. import sys, tty, termios
  22. fd = sys.stdin.fileno()
  23. old_settings = termios.tcgetattr(fd)
  24. try:
  25. tty.setraw(sys.stdin.fileno())
  26. # ch = sys.stdin.read(1)
  27. ch = sys.stdin.read(1)[0]
  28. finally:
  29. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  30. return ch
  31. class _GetchWindows:
  32. def __init__(self):
  33. import msvcrt
  34.  
  35. def __call__(self):
  36. import msvcrt
  37. return msvcrt.getch()
  38.  
  39. # enter: ord 13
  40. #backspace: ord 127
  41.  
  42. current_input = ""
  43. prompt_msg = ":> "
  44.  
  45. print(10*"n"+prompt_msg,end="")
  46.  
  47. getch = _Getch()
  48.  
  49. def clear_input():
  50. linelen = (len(current_input)+len(prompt_msg))
  51. sys.stdout.write("b"*linelen+" "*linelen+"b"*linelen)
  52. sys.stdout.flush()
  53.  
  54.  
  55. while(True):
  56.  
  57. ch=getch()
  58. if ord(ch)==3:# ctrl+c
  59. exit()
  60. # >>>>>>>.POINT OF INTEREST<<<<<<<<<<<
  61. if ord(ch)==13:# enter
  62. clear_input()
  63. print(current_input)
  64. current_input = ""
  65.  
  66. # print(prompt_msg,end="")
  67. sys.stdout.write(prompt_msg)
  68. sys.stdout.flush()
  69.  
  70. if ord(ch)==127 and len(current_input)>0:# backspace
  71. sys.stdout.write("b"+" "+"b")
  72. sys.stdout.flush()
  73. current_input=current_input[:-1]
  74. if ch in printable or ord(ch)>127: # printable
  75. current_input+=ch
  76. sys.stdout.write(ch)
  77. sys.stdout.flush()
Add Comment
Please, Sign In to add comment