forked from esotericnonsense/bitcoind-ncurses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getstr.py
167 lines (141 loc) · 5.38 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
import global_mod as g
import curses, time
import sys
def getstr(w, y, x, password_char = "", history = []):
window = curses.newwin(1, w, y, x)
result = ""
window.addstr("> ", curses.A_BOLD + curses.A_BLINK)
window.refresh()
window.keypad(True)
oldcursor = 0
history.append("")
history_cursor = len(history) - 1
global fs_encoding
try:
oldcursor = curses.curs_set(1)
except:
pass
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 == 27: # ESC
window.move(0, 2)
window.clrtoeol()
result = ""
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 character == curses.KEY_UP:
if history_cursor > 0:
len_old = len(result)
history_cursor -= 1
result = history[history_cursor]
if len(result) < len_old:
window.addstr(0, 2, (result).ljust(len_old))
else:
window.addstr(0, 2, result)
window.move(0, len(result) + 2)
continue
elif character == curses.KEY_DOWN:
if history_cursor + 1 < len(history):
len_old = len(result)
history_cursor += 1
result = history[history_cursor]
if len(result) < len_old:
window.addstr(0, 2, (result).ljust(len_old))
else:
window.addstr(0, 2, result)
window.move(0, len(result) + 2)
continue
elif (character < 256 and (len(chr(character).strip()) > 0 or character == 32) and len(result) < w-3): # ascii range TODO: unicode
result += chr(character)
if password_char =="":
window.addstr(chr(character))
else:
window.addstr(password_char)
window.addstr(0, 0, "> ", curses.A_BOLD + curses.color_pair(3))
window.refresh()
try:
curses.curs_set(oldcursor)
except:
pass
window.keypad(False)
return result
'''
This class provides some basic support for user keyboard input
and makes user interaction somewhat more coherent.
'''
class UserInput(object):
_window = None
_title = ""
_y = 0
def __init__(self, window, title, attribs=None):
self._window = window
self._title = title
self._window.clear()
self._draw_title(attribs)
def _draw_title(self, attribs=None):
if g.testnet:
color = curses.color_pair(2)
else:
color = curses.color_pair(1)
self._window.addstr(0, 1, g.rpc_deamon + "-ncurses " + g.version + " - " + self._title, (color + curses.A_BOLD) if attribs == None else attribs)
self._window.refresh()
self.nextline()
def nextline(self):
self._y += 1
def addline(self, string = "", attribs = None, y = -1, x = 1):
y_here = self._y if y == -1 else y
self._window.addstr(y_here, x, string, curses.A_BOLD if attribs == None else attribs)
self._window.refresh()
self._y = y_here + 1
def getstr(self, maxwidth = -1, y = -1, x = 1):
(win_x, win_y) = self._window.getmaxyx()
width = win_x-3 if maxwidth == -1 else maxwidth+3
win_y = self._y if y == -1 else y
result = sys.modules["getstr"].getstr(width, win_y, x)
self.nextline()
return result
def getpasswdstr(self, maxwidth = -1, y = -1, x = 1):
(win_x, win_y) = self._window.getmaxyx()
width = win_x-3 if maxwidth == -1 else maxwidth + 3
win_y = self._y if y == -1 else y
result = sys.modules["getstr"].getstr(width, win_y, x, "*")
self.nextline()
return result
def continue_yesno(self, defaultyes=True, message=""):
self.addline(("OK, do you want to continue? " if message == "" else message) + ("[Y/n]" if defaultyes else "[y/N]"), curses.color_pair(5) + curses.A_BOLD)
result = self.getstr(3)
if len(result.strip()) > 0:
if result[0].upper() == 'Y':
return True
else:
return False
else:
return True if defaultyes else False
def continue_enter(self, message=""):
self.addline(("Please press ENTER to continue..." if message == "" else message), curses.color_pair(5) + curses.A_BOLD)
result = self.getstr(1)
return True
def addmessageline(self, string, attribs=None, timetowait=-1, y=-1, x=1):
y_here = self._y + 1 if y == -1 else y
self._window.addstr(y_here, x, string, curses.color_pair(1) + curses.A_BOLD if attribs == None else attribs)
self._window.refresh()
self.pause(timetowait)
self._y = y_here + 1
def clear(self):
self._window.clear()
self._window.refresh()
def pause(self, timetowait = -1):
time.sleep(2.5 if timetowait == -1 else timetowait)