-
Notifications
You must be signed in to change notification settings - Fork 27
/
getstr.py
38 lines (30 loc) · 1016 Bytes
/
getstr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
import curses
def getstr(w, y, x):
window = curses.newwin(1, w, y, x)
result = ""
window.addstr("> ", curses.A_BOLD + curses.A_BLINK)
window.refresh()
window.keypad(True)
while True:
try:
character = -1
while (character < 0):
character = window.getch()
except:
break
if character == curses.KEY_ENTER or character == ord('\n'):
break
elif character == curses.KEY_BACKSPACE or character == 127:
if len(result):
window.move(0, len(result)+1)
window.delch()
result = result[:-1]
continue
elif (137 > character > 31 and len(result) < w-3): # ascii range TODO: unicode
result += chr(character)
window.addstr(chr(character))
window.addstr(0, 0, "> ", curses.A_BOLD + curses.color_pair(3))
window.refresh()
window.keypad(False)
return result