Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from string import printable
- import termios
- import tty
- class _Getch:
- """Gets a single character from standard input. Does not echo to the
- screen."""
- def __init__(self):
- try:
- self.impl = _GetchWindows()
- except ImportError:
- self.impl = _GetchUnix()
- def __call__(self): return self.impl()
- class _GetchUnix:
- def __init__(self):
- import tty, sys
- def __call__(self):
- import sys, tty, termios
- fd = sys.stdin.fileno()
- old_settings = termios.tcgetattr(fd)
- try:
- tty.setraw(sys.stdin.fileno())
- # ch = sys.stdin.read(1)
- ch = sys.stdin.read(1)[0]
- finally:
- termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
- return ch
- class _GetchWindows:
- def __init__(self):
- import msvcrt
- def __call__(self):
- import msvcrt
- return msvcrt.getch()
- # enter: ord 13
- #backspace: ord 127
- current_input = ""
- prompt_msg = ":> "
- print(10*"n"+prompt_msg,end="")
- getch = _Getch()
- def clear_input():
- linelen = (len(current_input)+len(prompt_msg))
- sys.stdout.write("b"*linelen+" "*linelen+"b"*linelen)
- sys.stdout.flush()
- while(True):
- ch=getch()
- if ord(ch)==3:# ctrl+c
- exit()
- # >>>>>>>.POINT OF INTEREST<<<<<<<<<<<
- if ord(ch)==13:# enter
- clear_input()
- print(current_input)
- current_input = ""
- # print(prompt_msg,end="")
- sys.stdout.write(prompt_msg)
- sys.stdout.flush()
- if ord(ch)==127 and len(current_input)>0:# backspace
- sys.stdout.write("b"+" "+"b")
- sys.stdout.flush()
- current_input=current_input[:-1]
- if ch in printable or ord(ch)>127: # printable
- current_input+=ch
- sys.stdout.write(ch)
- sys.stdout.flush()
Add Comment
Please, Sign In to add comment