-
Notifications
You must be signed in to change notification settings - Fork 27
/
hotkey.py
328 lines (271 loc) · 11 KB
/
hotkey.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#/usr/bin/env python
import curses
import global_mod as g
import tx
import monitor
import peers
import wallet
import console
import net
import forks
import footer
def change_mode(block_viewer, state, window, mode, poller):
try:
g.modes.index(mode)
except ValueError:
return False
state['mode'] = mode
block_viewer._mode = mode
if mode == 'monitor':
monitor.draw_window(state, window)
elif mode == 'tx':
tx.draw_window(state, window)
elif mode == 'peers':
peers.draw_window(state, window)
elif mode == 'wallet':
wallet.draw_window(state, window)
elif mode == 'block':
block_viewer.draw()
elif mode == 'console':
console.draw_window(state, window)
elif mode == 'net':
net.draw_window(state, window)
elif mode == 'forks':
forks.draw_window(state, window)
footer.draw_window(state)
poller.set_mode(mode)
def key_left(block_viewer, state, window, rpcc, poller):
try:
index = g.modes.index(state['mode']) - 1
if index < 0:
index = len(g.modes) - 2
change_mode(block_viewer, state, window, g.modes[index], poller)
except:
pass
def key_right(block_viewer, state, window, rpcc, poller):
try:
index = g.modes.index(state['mode']) + 1
if index > len(g.modes) - 2: # last index item is 'quit'
index = 0
change_mode(block_viewer, state, window, g.modes[index], poller)
except:
pass
def key_g(state, window, rpcc, poller):
if state['mode'] == 'tx':
state['mode'] = "transaction-input"
tx.draw_input_window(state, window, rpcc)
elif state['mode'] == "block":
return
state['mode'] = "block-input"
block.draw_input_window(state, window, rpcc)
elif state['mode'] == "console":
console.draw_input_box(state, window, rpcc)
def go_to_latest_block(state, window, rpcc, poller):
if state['mode'] == "block":
if 'mininginfo' in state:
if str(state['mininginfo']['blocks']) not in state['blocks']:
rpcc.request("getblockhash", state["mininginfo"]["blocks"])
else:
state['blocks']['browse_height'] = state['mininginfo']['blocks']
block.draw_window(state, window)
def scroll_down(state, window, rpcc, poller):
if state['mode'] == 'tx':
if 'tx' in state:
window_height = (state['y'] - 4) / 2
if state['tx']['cursor'] < (len(state['tx']['vin']) - 1) and state['tx']['mode'] == 'inputs':
state['tx']['cursor'] += 1
if (state['tx']['cursor'] - state['tx']['offset']) > window_height-2:
state['tx']['offset'] += 1
tx.draw_inputs(state)
elif state['tx']['out_offset'] < (len(state['tx']['vout_string']) - (window_height-1)) and state['tx']['mode'] == 'outputs':
state['tx']['out_offset'] += 1
tx.draw_outputs(state)
elif state['mode'] == "peers":
if 'peerinfo' in state and 'peerinfo_offset' in state:
window_height = state['y'] - 4
if state['peerinfo_offset'] < (len(state['peerinfo']) - window_height):
state['peerinfo_offset'] += 1
peers.draw_peers(state)
elif state['mode'] == "forks":
if 'chaintips' in state and 'chaintips_offset' in state:
window_height = state['y'] - 4
if state['chaintips_offset'] < (len(state['chaintips']) - window_height):
state['chaintips_offset'] += 1
forks.draw_tips(state)
elif state['mode'] == "wallet":
if 'wallet' in state:
if state['wallet']['cursor'] < (len(state['wallet']['transactions']) - 1):
state['wallet']['cursor'] += 1
window_height = state['y'] - 3
if ( (state['wallet']['cursor']*4 +1 ) - state['wallet']['offset']) > window_height-2:
state['wallet']['offset'] += 4
wallet.draw_transactions(state)
elif state['mode'] == "console":
if state['console']['offset'] > 0:
state['console']['offset'] -= 1
console.draw_buffer(state)
def scroll_up(state, window, rpcc, poller):
if state['mode'] == 'tx':
if 'tx' in state:
if state['tx']['cursor'] > 0 and state['tx']['mode'] == 'inputs':
if (state['tx']['cursor'] - state['tx']['offset']) == 0:
state['tx']['offset'] -= 1
state['tx']['cursor'] -= 1
tx.draw_inputs(state)
if state['tx']['out_offset'] > 0 and state['tx']['mode'] == 'outputs':
state['tx']['out_offset'] -= 1
tx.draw_outputs(state)
elif state['mode'] == "peers":
if 'peerinfo' in state and 'peerinfo_offset' in state:
if state['peerinfo_offset'] > 0:
state['peerinfo_offset'] -= 1
peers.draw_peers(state)
elif state['mode'] == "forks":
if 'chaintips' in state and 'chaintips_offset' in state:
if state['chaintips_offset'] > 0:
state['chaintips_offset'] -= 1
forks.draw_tips(state)
elif state['mode'] == "wallet":
if 'wallet' in state:
if state['wallet']['cursor'] > 0:
state['wallet']['cursor'] -= 1
if ((state['wallet']['cursor']*4 +1) - state['wallet']['offset']) == -3:
state['wallet']['offset'] -= 4
wallet.draw_transactions(state)
elif state['mode'] == "console":
state['console']['offset'] += 1
console.draw_buffer(state)
def scroll_up_page(state, window, rpcc, poller):
if state['mode'] == "console":
window_height = state['y'] - 3 - 2
state['console']['offset'] += window_height
console.draw_buffer(state)
def scroll_down_page(state, window, rpcc, poller):
if state['mode'] == "console":
window_height = state['y'] - 3 - 2
if state['console']['offset'] > window_height:
state['console']['offset'] -= window_height
else:
state['console']['offset'] = 0
console.draw_buffer(state)
def toggle_inputs_outputs(state, window, rpcc, poller):
if state['mode'] == 'tx':
if 'tx' in state:
if 'mode' in state['tx']:
if state['tx']['mode'] == 'inputs':
state['tx']['mode'] = 'outputs'
else:
state['tx']['mode'] = 'inputs'
tx.draw_window(state, window)
def load_transaction(block_viewer, state, window, rpcc, poller):
# TODO: some sort of indicator that a transaction is loading
if state['mode'] == 'tx':
if 'tx' in state:
if 'txid' in state['tx']['vin'][ state['tx']['cursor'] ]:
if state['tx']['loaded']:
state['tx']['loaded'] = 0
rpcc.request("getrawtransaction", state["tx"]["vin"][state["tx"]["cursor"]]["txid"], 1)
elif state['mode'] == "block":
txid = block_viewer.get_selected_txid()
if not txid:
return
rpcc.request("getrawtransaction", txid, 1)
change_mode(block_viewer, state, window, "tx", poller)
elif state['mode'] == "wallet":
if 'wallet' in state:
if 'transactions' in state['wallet']:
rpcc.request("getrawtransaction", state['wallet']['transactions'][ state['wallet']['cursor'] ]['txid'], 1)
change_mode(block_viewer, state, window, "tx", poller)
def toggle_verbose_mode(state, window, rpcc, poller):
# TODO: Re-implement verbose mode
return
if state['mode'] == 'tx':
if 'tx' in state:
if 'txid' in state['tx']:
if state['tx']['loaded']:
state['tx']['loaded'] = 0
if 'total_inputs' not in state['tx']:
s = {'txid': state['tx']['txid'], 'verbose': 1}
else:
s = {'txid': state['tx']['txid']}
rpcc.request(s)
def block_seek_back_thousand(state, window, rpcc, poller):
if state['mode'] == "block":
if 'blocks' in state:
if (state['blocks']['browse_height']) > 999:
if state['blocks']['loaded'] == 1:
state['blocks']['loaded'] = 0
state['blocks']['browse_height'] -= 1000
state['blocks']['cursor'] = 0
state['blocks']['offset'] = 0
if str(state['blocks']['browse_height']) in state['blocks']:
block.draw_window(state, window)
else:
rpcc.request("getblockhash", state["blocks"]["browse_height"])
def block_seek_forward_thousand(state, window, rpcc, poller):
if state['mode'] == "block":
if 'blocks' in state:
if (state['blocks']['browse_height']) < state['mininginfo']['blocks'] - 999:
if state['blocks']['loaded'] == 1:
state['blocks']['loaded'] = 0
state['blocks']['browse_height'] += 1000
state['blocks']['cursor'] = 0
state['blocks']['offset'] = 0
if str(state['blocks']['browse_height']) in state['blocks']:
block.draw_window(state, window)
else:
rpcc.request("getblockhash", state["blocks"]["browse_height"])
keymap = {
curses.KEY_LEFT: key_left,
curses.KEY_RIGHT: key_right,
curses.KEY_DOWN: scroll_down,
curses.KEY_UP: scroll_up,
curses.KEY_PPAGE: scroll_up_page,
curses.KEY_NPAGE: scroll_down_page,
curses.KEY_ENTER: load_transaction,
ord('\n'): load_transaction,
ord('g'): key_g,
ord('G'): key_g,
ord('\t'): toggle_inputs_outputs,
9: toggle_inputs_outputs,
ord("v"): toggle_verbose_mode,
ord("V"): toggle_verbose_mode,
}
modemap = {
ord('m'): 'monitor',
ord('M'): 'monitor',
ord('b'): 'block',
ord('B'): 'block',
ord('t'): 'tx',
ord('T'): 'tx',
ord('c'): 'console',
ord('C'): 'console',
ord('n'): 'net',
ord('N'): 'net',
ord('p'): 'peers',
ord('P'): 'peers',
ord('w'): 'wallet',
ord('W'): 'wallet',
ord('f'): 'forks',
ord('F'): 'forks',
}
def check(block_viewer, state, window, rpcc, poller):
key = window.getch()
if key < 0 or state['mode'] == 'splash':
return False
if state['mode'] == "block":
if block_viewer.handle_hotkey(key):
return False
if key in keymap:
if key in (curses.KEY_LEFT, curses.KEY_RIGHT, curses.KEY_ENTER, ord('\n')):
keymap[key](block_viewer, state, window, rpcc, poller)
else:
keymap[key](state, window, rpcc, poller)
elif key in modemap:
mode = modemap[key]
if mode == "forks":
rpcc.request('getchaintips')
change_mode(block_viewer, state, window, mode, poller)
elif key == ord('q') or key == ord('Q'): # quit
return True
return False