forked from esotericnonsense/bitcoind-ncurses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotkey.py
314 lines (270 loc) · 12.5 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
#/usr/bin/env python
import curses
import global_mod as g
import tx
import block
import monitor
import peers
import wallet
import console
def change_mode(state, window, mode):
try:
g.modes.index(mode)
except ValueError:
return False
state['mode'] = mode
if mode == 'monitor':
monitor.draw_window(state, window)
elif mode == 'transaction':
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.draw_window(state, window)
elif mode == 'console':
console.draw_window(state, window)
def check(state, window, rpc_queue):
c = window.getch()
if c < 0:
return False
elif c == ord('q') or c == ord('Q'):
# quit
return True
elif c == curses.KEY_LEFT:
try:
index = g.modes.index(state['mode']) - 1
except:
pass
if index < 0:
index = len(g.modes) - 2
change_mode(state, window, g.modes[index])
elif c == curses.KEY_RIGHT:
try:
index = g.modes.index(state['mode']) + 1
except:
pass
if index > len(g.modes) - 2: # last index item is 'quit'
index = 0
change_mode(state, window, g.modes[index])
elif c == ord('m') or c == ord('M'):
change_mode(state, window, 'monitor')
elif c == ord('w') or c == ord('W'):
rpc_queue.put('listsinceblock')
change_mode(state, window, 'wallet')
elif c == ord('p') or c == ord('P'):
rpc_queue.put('getpeerinfo')
change_mode(state, window, 'peers')
elif c == ord('b') or c == ord('B'):
change_mode(state, window, 'block')
elif c == ord('t') or c == ord('T'):
change_mode(state, window, 'transaction')
elif c == ord('c') or c == ord('C'):
change_mode(state, window, 'console')
elif c == ord('g') or c == ord('G'):
if state['mode'] == "transaction":
state['mode'] = "transaction-input"
tx.draw_input_window(state, window, rpc_queue)
elif state['mode'] == "block":
state['mode'] = "block-input"
block.draw_input_window(state, window, rpc_queue)
elif state['mode'] == "console":
console.draw_input_box(state, rpc_queue)
elif c == ord('l') or c == ord('L'):
if state['mode'] == "block":
if 'blockcount' in state:
if state['blockcount'] not in state['blocks']:
s = {'getblockhash': state['blockcount']}
rpc_queue.put(s)
else:
state['blocks']['browse_height'] = state['blockcount']
block.draw_window(state, window)
elif c == curses.KEY_DOWN:
if state['mode'] == "transaction":
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'] == "block":
if 'blocks' in state:
height = str(state['blocks']['browse_height'])
if height in state['blocks']:
blockdata = state['blocks'][height]
if state['blocks']['cursor'] < (len(blockdata['tx']) - 1):
state['blocks']['cursor'] += 1
window_height = state['y'] - 6
if (state['blocks']['cursor'] - state['blocks']['offset']) > window_height-2:
state['blocks']['offset'] += 1
block.draw_transactions(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'] == "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)
elif c == curses.KEY_UP:
if state['mode'] == "transaction":
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'] == "block":
if 'blocks' in state:
if state['blocks']['cursor'] > 0:
if (state['blocks']['cursor'] - state['blocks']['offset']) == 0:
state['blocks']['offset'] -= 1
state['blocks']['cursor'] -= 1
block.draw_transactions(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'] == "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)
elif c == curses.KEY_PPAGE:
if state['mode'] == "console":
window_height = state['y'] - 3 - 2
state['console']['offset'] += window_height
console.draw_buffer(state)
elif c == curses.KEY_NPAGE:
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)
elif c == ord('\t') or c == 9:
if state['mode'] == "transaction":
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)
elif c == curses.KEY_ENTER or c == ord('\n'):
# TODO: some sort of indicator that a transaction is loading
if state['mode'] == "transaction":
if 'tx' in state:
if 'txid' in state['tx']['vin'][ state['tx']['cursor'] ]:
if state['tx']['loaded']:
state['tx']['loaded'] = 0
s = {'txid': state['tx']['vin'][ state['tx']['cursor'] ]['txid']}
rpc_queue.put(s)
elif state['mode'] == "block":
if 'blocks' in state:
if state['blocks']['browse_height'] > 0: # block 0 is not indexed
height = str(state['blocks']['browse_height'])
if height in state['blocks']:
blockdata = state['blocks'][height]
s = {'txid': blockdata['tx'][ state['blocks']['cursor'] ]}
rpc_queue.put(s)
state['mode'] = "transaction"
elif state['mode'] == "wallet":
if 'wallet' in state:
if 'transactions' in state['wallet']:
s = {'txid': state['wallet']['transactions'][ state['wallet']['cursor'] ]['txid']}
rpc_queue.put(s)
state['mode'] = "transaction"
elif c == ord("v") or c == ord("V"):
if state['mode'] == "transaction":
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']}
rpc_queue.put(s)
elif c == ord('j') or c == ord('J'):
if state['mode'] == "block":
if 'blocks' in state:
if (state['blocks']['browse_height']) > 0:
if state['blocks']['loaded'] == 1:
state['blocks']['loaded'] = 0
state['blocks']['browse_height'] -= 1
state['blocks']['cursor'] = 0
state['blocks']['offset'] = 0
if str(state['blocks']['browse_height']) in state['blocks']:
block.draw_window(state, window)
else:
s = {'getblockhash': state['blocks']['browse_height']}
rpc_queue.put(s)
elif c == ord('k') or c == ord('K'):
if state['mode'] == "block":
if 'blocks' in state:
if (state['blocks']['browse_height']) < state['blockcount']:
if state['blocks']['loaded'] == 1:
state['blocks']['loaded'] = 0
state['blocks']['browse_height'] += 1
state['blocks']['cursor'] = 0
state['blocks']['offset'] = 0
if str(state['blocks']['browse_height']) in state['blocks']:
block.draw_window(state, window)
else:
s = {'getblockhash': state['blocks']['browse_height']}
rpc_queue.put(s)
elif c == curses.KEY_HOME:
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:
s = {'getblockhash': state['blocks']['browse_height']}
rpc_queue.put(s)
elif c == curses.KEY_END:
if state['mode'] == "block":
if 'blocks' in state:
if (state['blocks']['browse_height']) < state['blockcount'] - 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:
s = {'getblockhash': state['blocks']['browse_height']}
rpc_queue.put(s)
return False