This repository was archived by the owner on Jan 4, 2020. It is now read-only.
forked from Gallopsled/pwntools
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathui.py
191 lines (169 loc) · 5.84 KB
/
ui.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import time
from . import term
from .log import getLogger
log = getLogger(__name__)
def yesno(prompt, default=None):
"""Presents the user with prompt (typically in the form of question) which
the user must answer yes or no.
Arguments:
prompt (str): The prompt to show
default: The default option; `True` means "yes"
Returns:
`True` if the answer was "yes", `False` if "no"
"""
if not isinstance(default, (bool, type(None))):
raise ValueError('yesno(): default must be a boolean or None')
if term.term_mode:
term.output(' [?] %s [' % prompt)
yesfocus, yes = term.text.bold('Yes'), 'yes'
nofocus, no = term.text.bold('No'), 'no'
hy = term.output(yesfocus if default is True else yes)
term.output('/')
hn = term.output(nofocus if default is False else no)
term.output(']\n')
cur = default
while True:
k = term.key.get()
if k in ('y', 'Y', '<left>') and cur is not True:
cur = True
hy.update(yesfocus)
hn.update(no)
elif k in ('n', 'N', '<right>') and cur is not False:
cur = False
hy.update(yes)
hn.update(nofocus)
elif k == '<enter>':
if cur is not None:
return cur
else:
prompt = ' [?] %s [%s/%s] ' % (prompt,
'Yes' if default is True else 'yes',
'No' if default is False else 'no',
)
while True:
opt = input(prompt).lower()
if opt == '' and default is not None:
return default
elif opt in ('y', 'yes'):
return True
elif opt in ('n', 'no'):
return False
print('Please answer yes or no')
def options(prompt, opts, default=None):
"""Presents the user with a prompt (typically in the
form of a question) and a number of options.
Arguments:
prompt (str): The prompt to show
opts (list): The options to show to the user
default: The default option to choose
Returns:
The users choice in the form of an integer.
"""
if not isinstance(default, (int, type(None))):
raise ValueError('options(): default must be a number or None')
if term.term_mode:
numfmt = '%' + str(len(str(len(opts)))) + 'd) '
print(' [?] ' + prompt)
hs = []
space = ' '
arrow = term.text.bold_green(' => ')
cur = default
for i, opt in enumerate(opts):
h = term.output(arrow if i == cur else space, frozen=False)
num = numfmt % (i + 1)
term.output(num)
term.output(opt + '\n', indent=len(num) + len(space))
hs.append(h)
ds = ''
prev = 0
while True:
prev = cur
was_digit = False
k = term.key.get()
if k == '<up>':
if cur is None:
cur = 0
else:
cur = max(0, cur - 1)
elif k == '<down>':
if cur is None:
cur = 0
else:
cur = min(len(opts) - 1, cur + 1)
elif k == 'C-<up>':
cur = 0
elif k == 'C-<down>':
cur = len(opts) - 1
elif k in ('<enter>', '<right>'):
if cur is not None:
return cur
elif k in tuple('1234567890'):
was_digit = True
d = str(k)
n = int(ds + d)
if n > 0 and n <= len(opts):
ds += d
elif d != '0':
ds = d
n = int(ds)
cur = n - 1
if prev != cur:
if prev is not None:
hs[prev].update(space)
if was_digit:
hs[cur].update(term.text.bold_green('%5s> ' % ds))
else:
hs[cur].update(arrow)
else:
linefmt = ' %' + str(len(str(len(opts)))) + 'd) %s'
while True:
print(' [?] ' + prompt)
for i, opt in enumerate(opts):
print(linefmt % (i + 1, opt))
s = ' Choice '
if default:
s += '[%s] ' % str(default)
try:
x = int(input(s) or default)
except (ValueError, TypeError):
continue
if x >= 1 and x <= len(opts):
return x
def pause(n=None):
"""Waits for either user input or a specific number of seconds."""
if n is None:
if term.term_mode:
log.info('Paused (press any to continue)')
term.getkey()
else:
log.info('Paused (press enter to continue)')
input('')
elif isinstance(n, int):
with log.waitfor("Waiting") as l:
for i in range(n, 0, -1):
l.status('%d... ' % i)
time.sleep(1)
l.success()
else:
raise ValueError('pause(): n must be a number or None')
def more(text):
"""more(text)
Shows text like the command line tool ``more``.
It not in term_mode, just prints the data to the screen.
Arguments:
text(str): The text to show.
Returns:
:const:`None`
"""
if term.term_mode:
lines = text.split('\n')
h = term.output(term.text.reverse('(more)'), float=True, frozen=False)
step = term.height - 1
for i in range(0, len(lines), step):
for l in lines[i:i + step]:
print(l)
if i + step < len(lines):
term.key.get()
h.delete()
else:
print(text)