-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
510 lines (407 loc) · 19.9 KB
/
main.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import curses
from curses.textpad import Textbox, rectangle
import string
import json
import requests,urllib
NUM_LINES = 1
NUM_COLS = 80
POST_REQUEST_NUM_LINES = 5
POST_REQUEST_NUM_COLS = 80
PADDING = 2
TOP,LEFT = 0,0
options = ["GET","POST","PATCH","DELETE"]
CTRL_X = 24
CTRL_D = 4
REQUEST_TITLE = "Request"
RESPONSE_TITLE = "Response"
ENTER = 10
global_stdscr = None
last_key_pressed = 0
chosen_option = "▼ "+options[0]
selected_option = 0
box = None
edit_box_message = ""
INIT_CURSES = 0
move_indexes = {"move_to_dropdown":1,"edit_message_box":2,"request":3,"toggle_dropdown":4,"click_request":5,"add_key_value_pair_below":6}
chosen_move_index = move_indexes["edit_message_box"]
post_request_body_boxes = []
PLUS_POS_Y = 1
selected_box = -1
key_or_val_selector = "key"
def setup_brand(stdscr):
global global_stdscr
global_stdscr = stdscr
height,width = stdscr.getmaxyx()
if height<50 or width<75:
return
TOP = height//8
LEFT = width//10
P_START_INDEX = 0
P_END_INDEX = 4
O_START_INDEX = 6
O_END_INDEX = 10
S_START_INDEX = 12
S_END_INDEX = 16
T_START_INDEX = 18
T_END_INDEX = 22
E_START_INDEX = 24
E_END_INDEX = 28
R_START_INDEX = 30
R_END_INDEX = 34
M_START_INDEX = 38
M_END_INDEX = 42
A_START_INDEX = 44
A_END_INDEX = 48
N_START_INDEX = 50
N_END_INDEX = 54
# Designing P
for i in range(1, TOP):
stdscr.chgat(i,LEFT+P_START_INDEX, 1, curses.A_REVERSE)
for i in range(1,4):
stdscr.chgat(1, LEFT + i, 1, curses.A_REVERSE)
stdscr.chgat(3, LEFT + i, 1, curses.A_REVERSE)
for i in range(1, 4):
stdscr.chgat(i,LEFT+P_END_INDEX, 1, curses.A_REVERSE)
#Designing O
for i in range(1,TOP):
stdscr.chgat(i,LEFT+O_START_INDEX,1,curses.A_REVERSE)
for i in range(1,O_END_INDEX-O_START_INDEX):
stdscr.chgat(1,LEFT+O_START_INDEX+i,1,curses.A_REVERSE)
stdscr.chgat(TOP-1,LEFT+O_START_INDEX+i,1,curses.A_REVERSE)
for i in range(1,TOP):
stdscr.chgat(i,LEFT+O_END_INDEX,1,curses.A_REVERSE)
#Designing S
for i in range(1,TOP//2+1):
stdscr.chgat(i,LEFT+S_START_INDEX,1,curses.A_REVERSE)
for i in range(TOP//2+2,TOP):
stdscr.chgat(i,LEFT+S_START_INDEX,1,curses.A_REVERSE)
for i in range(1,S_END_INDEX-S_START_INDEX):
stdscr.chgat(1,LEFT+S_START_INDEX+i,1,curses.A_REVERSE)
stdscr.chgat(3,LEFT+S_START_INDEX+i,1,curses.A_REVERSE)
stdscr.chgat(5,LEFT+S_START_INDEX+i,1,curses.A_REVERSE)
stdscr.chgat(1, LEFT + S_END_INDEX, 1, curses.A_REVERSE)
for i in range(TOP//2,TOP):
stdscr.chgat(i,LEFT+S_END_INDEX,1,curses.A_REVERSE)
#Designing T
for i in range(T_END_INDEX-T_START_INDEX+1):
stdscr.chgat(1,LEFT+T_START_INDEX+i,1,curses.A_REVERSE)
for i in range(1,TOP):
stdscr.chgat(i,LEFT+T_START_INDEX+(T_END_INDEX-T_START_INDEX)//2,1,curses.A_REVERSE)
#Designing E
for i in range(1,TOP):
stdscr.chgat(i,LEFT+E_START_INDEX,1,curses.A_REVERSE)
for i in range(1,E_END_INDEX-E_START_INDEX+1):
stdscr.chgat(1,LEFT+E_START_INDEX+i,1,curses.A_REVERSE)
stdscr.chgat(3,LEFT+E_START_INDEX+i,1,curses.A_REVERSE)
stdscr.chgat(5,LEFT+E_START_INDEX+i,1,curses.A_REVERSE)
# Designing R
for i in range(1,R_END_INDEX-R_START_INDEX):
stdscr.chgat(1,LEFT+R_START_INDEX+i,1,curses.A_REVERSE)
stdscr.chgat(3,LEFT+R_START_INDEX+i,1,curses.A_REVERSE)
for i in range(1,TOP):
stdscr.chgat(i,LEFT+R_START_INDEX,1,curses.A_REVERSE)
stdscr.chgat(4,LEFT+R_START_INDEX+1,1,curses.A_REVERSE)
stdscr.chgat(TOP-1,LEFT+R_END_INDEX,1,curses.A_REVERSE)
for i in range(1,4):
stdscr.chgat(i, LEFT+R_END_INDEX, 1, curses.A_REVERSE)
#Designing M
for i in range(1,TOP):
stdscr.chgat(i,LEFT+M_START_INDEX,1,curses.A_REVERSE)
for i in range(1,TOP):
stdscr.chgat(i,LEFT+M_END_INDEX,1,curses.A_REVERSE)
for i in range(1,M_END_INDEX-M_START_INDEX):
stdscr.chgat(i,LEFT+(M_END_INDEX+M_START_INDEX)//2,1,curses.A_REVERSE)
#Desinging A
for i in range(1,TOP):
stdscr.chgat(i,LEFT+A_START_INDEX,1,curses.A_REVERSE)
for i in range(1,TOP):
stdscr.chgat(i,LEFT+A_END_INDEX,1,curses.A_REVERSE)
stdscr.chgat(1,LEFT+A_START_INDEX,A_END_INDEX-A_START_INDEX,curses.A_REVERSE)
stdscr.chgat(3,LEFT+A_START_INDEX,A_END_INDEX-A_START_INDEX,curses.A_REVERSE)
#Designing N
for i in range(1,TOP):
stdscr.chgat(i,LEFT+N_START_INDEX,1,curses.A_REVERSE)
for i in range(1,TOP):
stdscr.chgat(i,LEFT+N_END_INDEX,1,curses.A_REVERSE)
for i in range(1,N_END_INDEX-N_START_INDEX):
stdscr.chgat(i,LEFT+N_START_INDEX+i,1,curses.A_REVERSE)
def move_to_dropdown():
global chosen_option,global_stdscr,chosen_move_index,last_key_pressed
height,width = global_stdscr.getmaxyx()
global_stdscr.chgat(height // 5,width//10,len(chosen_option),curses.A_REVERSE)
last_key_pressed = global_stdscr.getch()
if last_key_pressed == curses.KEY_RIGHT:
chosen_move_index = move_indexes["edit_message_box"]
elif last_key_pressed == ENTER:
chosen_move_index = move_indexes["toggle_dropdown"]
def toggle_dropdown():
global global_stdscr,options,last_key_pressed,selected_option,chosen_move_index,chosen_option,post_request_body_boxes,PLUS_POS_Y
rectangle(global_stdscr,TOP+3,LEFT-PADDING,TOP+len(options)*PADDING+1,LEFT+4*PADDING)
unselected_options = [option for option in options if option!=chosen_option[2:]]
for i,option in enumerate(unselected_options):
global_stdscr.addstr(TOP+2+PADDING*(i+1),LEFT,option)
if selected_option==i:
global_stdscr.chgat(TOP+2+PADDING*(i+1),LEFT,len(option),curses.A_REVERSE)
last_key_pressed = global_stdscr.getch()
if last_key_pressed == curses.KEY_DOWN:
selected_option +=1
if selected_option>2:
selected_option=2
elif last_key_pressed == curses.KEY_UP:
selected_option-=1
if selected_option<0:
selected_option=0
chosen_move_index = move_indexes["move_to_dropdown"]
elif last_key_pressed == ENTER:
chosen_option = "▼ "+unselected_options[selected_option]
selected_option = 0
chosen_move_index = move_indexes["move_to_dropdown"]
post_request_body_boxes = []
PLUS_POS_Y = 1
def navigator(keystroke):
global edit_box_message,INIT_CURSES,chosen_move_index,last_key_pressed
if keystroke == CTRL_X:
last_key_pressed = keystroke
return 7
if keystroke==curses.KEY_LEFT:
chosen_move_index = move_indexes["move_to_dropdown"]
last_key_pressed = 7
return 7
if keystroke==curses.KEY_RIGHT:
chosen_move_index = move_indexes["request"]
last_key_pressed = 7
return 7
if keystroke==curses.KEY_BACKSPACE:
edit_box_message = edit_box_message[:-1]
if chr(keystroke).lower() in string.ascii_lowercase or chr(keystroke).isdigit() or chr(keystroke) in ["/", ":", ".","?","=","&"]:
edit_box_message+=chr(keystroke)
if keystroke == curses.KEY_DOWN and (chosen_option[2:]=="POST" or chosen_option[2:] == "GET"):
chosen_move_index = move_indexes["add_key_value_pair_below"]
return 7
last_key_pressed = keystroke
return keystroke
response_data = None
response_data_url = None
response_data_cursor_y = 0
start_index_response_data = None
end_index_response_data = None
def format_data(response_data, height, width):
global response_data_cursor_y,start_index_response_data,end_index_response_data
response_data = response_data[start_index_response_data:end_index_response_data]
allowed_width = width-3 - (LEFT + len(chosen_option)+len(REQUEST_TITLE) + NUM_COLS + 4*PADDING+1)
for i in range(end_index_response_data):
if i<len(response_data):
global_stdscr.addstr(3+i, LEFT + len(chosen_option) + len(REQUEST_TITLE) + NUM_COLS + 4 * PADDING + 2,response_data[i][:allowed_width])
if i==response_data_cursor_y-start_index_response_data and chosen_move_index == move_indexes["click_request"]:
global_stdscr.chgat(3+i,LEFT + len(chosen_option) + len(REQUEST_TITLE) + NUM_COLS + 4 * PADDING + 2,len(response_data[i][:allowed_width]),curses.A_REVERSE)
def click_request():
global response_data,last_key_pressed,edit_box_message,response_data_cursor_y,chosen_move_index,response_data_url,global_stdscr
global start_index_response_data,end_index_response_data
# populate response div
height,width = global_stdscr.getmaxyx()
if not response_data or (response_data!=None and edit_box_message!=response_data_url):
try:
chosen_option_stripped = chosen_option[2:]
if chosen_option_stripped == "GET":
payload = {}
for box in post_request_body_boxes:
if box["key"] != "" and box["value"] != "":
payload[box["key"]] = int(box["value"]) if box["value"].isdigit() else box["value"]
payload = urllib.parse.urlencode(payload)
if payload != "":
payload="?"+payload
response_data = json.dumps(requests.get(edit_box_message+payload).json(),indent=1,sort_keys=True).split("\n")
elif chosen_option_stripped == "POST":
headers = {'content-type': 'application/json'}
payload = {}
for box in post_request_body_boxes:
if box["key"] != "" and box["value"]!="":
payload[box["key"]] = int(box["value"]) if box["value"].isdigit() else box["value"]
response_data = json.dumps(requests.post(edit_box_message, data=json.dumps(payload), headers=headers).json(),indent=1,sort_keys=True).split("\n")
# elif chosen_option_stripped == "PATCH":
# response_data = json.dumps(requests.get(edit_box_message).json(),indent=1,sort_keys=True).split("\n")
# elif chosen_option_stripped == "DELETE":
# response_data = json.dumps(requests.get(edit_box_message).json(),indent=1,sort_keys=True).split("\n")
format_data(response_data, height, width)
except:
format_data(["There was an error processing the url","or there was no data returned..."],height, width)
else:
format_data(response_data, height, width)
response_data_url = edit_box_message
last_key_pressed = global_stdscr.getch()
if last_key_pressed == curses.KEY_LEFT:
chosen_move_index = move_indexes["request"]
elif last_key_pressed == curses.KEY_DOWN:
response_data_cursor_y += 1
if response_data_cursor_y > len(response_data)-1:
response_data_cursor_y = len(response_data)-1
elif response_data_cursor_y>=end_index_response_data:
start_index_response_data+=1
end_index_response_data+=1
elif last_key_pressed == curses.KEY_UP:
response_data_cursor_y -=1
if response_data_cursor_y <start_index_response_data and start_index_response_data > 0:
start_index_response_data-=1
end_index_response_data-=1
if response_data_cursor_y < 0:
response_data_cursor_y=0
def request():
global edit_box_message,last_key_pressed,chosen_move_index
global_stdscr.chgat(TOP, LEFT + len(chosen_option) + NUM_COLS + 2*PADDING+1,len(REQUEST_TITLE),curses.A_REVERSE)
last_key_pressed = global_stdscr.getch()
if last_key_pressed == curses.KEY_LEFT:
chosen_move_index = move_indexes["edit_message_box"]
elif last_key_pressed == ENTER:
chosen_move_index = move_indexes["click_request"]
elif last_key_pressed == curses.KEY_RIGHT:
if response_data:
chosen_move_index = move_indexes["click_request"]
def edit_box():
global box,NUM_LINES,NUM_COLS,TOP,LEFT,chosen_option,edit_box_message
editwin = curses.newwin(NUM_LINES, NUM_COLS - 1, TOP, LEFT + len(chosen_option) + 2)
box = Textbox(editwin)
for char in edit_box_message:
box.do_command(char)
box.edit(navigator)
def post_navigator(keystroke):
global last_key_pressed,post_request_body_boxes,selected_box,chosen_move_index,key_or_val_selector
last_key_pressed = keystroke
if keystroke == CTRL_X:
last_key_pressed = keystroke
return 7
elif keystroke == curses.KEY_UP:
if selected_box == 0:
chosen_move_index = move_indexes["edit_message_box"]
return 7
elif keystroke == curses.KEY_DOWN:
if selected_box==len(post_request_body_boxes)-1:
selected_box=len(post_request_body_boxes)
else:
selected_box = selected_box+1 if selected_box < len(post_request_body_boxes) else len(post_request_body_boxes)
return 7
elif keystroke == curses.KEY_RIGHT:
key_or_val_selector = "value"
return 7
elif keystroke == curses.KEY_LEFT:
key_or_val_selector = "key"
return 7
elif keystroke==curses.KEY_BACKSPACE:
post_request_body_boxes[selected_box][key_or_val_selector] = post_request_body_boxes[selected_box][key_or_val_selector][:-1]
post_request_body_boxes[selected_box][key_or_val_selector+"_box"]["cursor"]-=1
if len(post_request_body_boxes[selected_box][key_or_val_selector])==0:
post_request_body_boxes[selected_box][key_or_val_selector + "_box"]["cursor"]=0
# could add ctrl_d to delete the cell for more user friendly experience
if chr(keystroke).lower() in string.ascii_lowercase or chr(keystroke).isdigit():
post_request_body_boxes[selected_box][key_or_val_selector]+=chr(keystroke)
post_request_body_boxes[selected_box][key_or_val_selector + "_box"]["cursor"]+=1
return keystroke
def update_key_value_table():
global global_stdscr,post_request_body_boxes
global PLUS_POS_Y
PLUS_POS_Y = 1
for i,box in enumerate(post_request_body_boxes):
rectangle(global_stdscr, TOP+POST_REQUEST_NUM_LINES+i*2*PADDING,
LEFT+PADDING, TOP+POST_REQUEST_NUM_LINES+PADDING+i*2*PADDING,
LEFT + NUM_COLS//2 -1 + 3 * PADDING)
rectangle(global_stdscr, TOP + POST_REQUEST_NUM_LINES+i*2*PADDING,
LEFT + NUM_COLS // 2 +3*PADDING, TOP + POST_REQUEST_NUM_LINES+PADDING+i*2*PADDING,
LEFT + NUM_COLS + 3 * PADDING)
PLUS_POS_Y+=4
global_stdscr.addstr(box["key_box"]["y"],box["key_box"]["x"],box["key"])
global_stdscr.addstr(box["value_box"]["y"], box["value_box"]["x"],box["value"])
if i == 0:
if chosen_option[2:] == "POST":
global_stdscr.addstr(TOP+POST_REQUEST_NUM_LINES-1,LEFT+2*PADDING,"Key")
global_stdscr.addstr(TOP+POST_REQUEST_NUM_LINES-1,LEFT+NUM_COLS//2 + 4*PADDING,"Value")
elif chosen_option[2:] == "GET":
global_stdscr.addstr(TOP + POST_REQUEST_NUM_LINES - 1, LEFT + 2 * PADDING, "Parameters")
global_stdscr.refresh()
def add_key_value_pair_below():
global global_stdscr,last_key_pressed,post_request_body_boxes,PLUS_POS_Y,selected_box,chosen_move_index
if len(post_request_body_boxes) == 0 or selected_box==len(post_request_body_boxes):
global_stdscr.chgat(TOP + POST_REQUEST_NUM_LINES - 1 + PLUS_POS_Y, LEFT + POST_REQUEST_NUM_COLS + 2 * PADDING, 1,curses.A_REVERSE)
update_key_value_table()
if selected_box != -1 and selected_box != len(post_request_body_boxes):
curses.curs_set(True)
post_request_body_boxes[selected_box][key_or_val_selector+"_box"]["box"].edit(post_navigator)
elif selected_box== -1 or len(post_request_body_boxes)==selected_box:
last_key_pressed = global_stdscr.getch()
if last_key_pressed == ENTER:
key_editwin = curses.newwin(NUM_LINES, NUM_COLS // 2 - 2,
TOP + POST_REQUEST_NUM_LINES + len(post_request_body_boxes) * 2 * PADDING+1,
LEFT + PADDING + 1)
key_box = Textbox(key_editwin)
value_editwin = curses.newwin(NUM_LINES, NUM_COLS // 2 - 2,
TOP + POST_REQUEST_NUM_LINES + len(post_request_body_boxes) * 2 * PADDING+1,
LEFT + 3*PADDING + NUM_COLS//2 +1)
value_box = Textbox(value_editwin)
selected_box = len(post_request_body_boxes)
post_request_body_boxes.append({"key":"","value":"","key_box":
{"box":key_box,"x":LEFT + PADDING + 1,
"y":TOP + POST_REQUEST_NUM_LINES+len(post_request_body_boxes)*2*PADDING+1,
"cursor":0},
"value_box":{"box":value_box,"x":LEFT + PADDING + NUM_COLS//2 + 1 + 2*PADDING,
"y":TOP + POST_REQUEST_NUM_LINES+len(post_request_body_boxes)*2*PADDING+1,
"cursor":0}})
PLUS_POS_Y+=4
elif last_key_pressed == curses.KEY_UP:
if selected_box==-1:
chosen_move_index = move_indexes["edit_message_box"]
else:
selected_box = selected_box-1 if selected_box>0 else 0
def setup():
global last_key_pressed,box,INIT_CURSES,global_stdscr,chosen_option,TOP,LEFT,chosen_move_index,edit_box_message,PLUS_POS_Y
global start_index_response_data,end_index_response_data,NUM_COLS,POST_REQUEST_NUM_COLS
while last_key_pressed!=CTRL_X:
global_stdscr.clear()
height, width = global_stdscr.getmaxyx()
TOP = height // 5
LEFT = width // 10
start_index_response_data = 0
end_index_response_data = height - 5
NUM_COLS = width // 3
POST_REQUEST_NUM_COLS = NUM_COLS
setup_brand(global_stdscr)
INIT_CURSES = LEFT + len(chosen_option) + 2
# options
global_stdscr.addstr(TOP, LEFT, chosen_option)
# editbox
rectangle(global_stdscr, TOP - PADDING, LEFT - PADDING, TOP + PADDING, LEFT + len(chosen_option) + NUM_COLS + PADDING)
global_stdscr.addstr(TOP, LEFT + len(chosen_option) + 2,edit_box_message)
# request button
global_stdscr.addstr(TOP, LEFT + len(chosen_option) + 2*PADDING +1 + NUM_COLS,REQUEST_TITLE)
rectangle(global_stdscr, TOP - PADDING, LEFT + len(chosen_option) + NUM_COLS + 2*PADDING, TOP + PADDING, LEFT + len(chosen_option)+len(REQUEST_TITLE) + NUM_COLS + 2*PADDING+1)
# response div
global_stdscr.addstr(1, LEFT + len(chosen_option)+len(REQUEST_TITLE) + NUM_COLS + 4*PADDING+1,RESPONSE_TITLE)
rectangle(global_stdscr,2, LEFT + len(chosen_option)+len(REQUEST_TITLE) + NUM_COLS + 4*PADDING+1,height-2,width-2)
if chosen_option[2:] == "POST" or chosen_option[2:] == "GET":
# request post div extra data
global_stdscr.addstr(TOP + POST_REQUEST_NUM_LINES-1 + PLUS_POS_Y, LEFT+POST_REQUEST_NUM_COLS+2*PADDING, "+")
rectangle(global_stdscr,TOP+POST_REQUEST_NUM_LINES-PADDING + PLUS_POS_Y,
LEFT+POST_REQUEST_NUM_COLS+PADDING,TOP+POST_REQUEST_NUM_LINES+PLUS_POS_Y,LEFT+POST_REQUEST_NUM_COLS+3*PADDING)
update_key_value_table()
if response_data:
format_data(response_data, height, width)
global_stdscr.refresh()
curses.curs_set(False)
if chosen_move_index == move_indexes["move_to_dropdown"]:
move_to_dropdown()
elif chosen_move_index == move_indexes["request"]:
request()
elif chosen_move_index == move_indexes["edit_message_box"]:
curses.curs_set(True)
edit_box()
elif chosen_move_index == move_indexes["toggle_dropdown"]:
toggle_dropdown()
elif chosen_move_index == move_indexes["click_request"]:
click_request()
elif chosen_move_index == move_indexes["add_key_value_pair_below"]:
add_key_value_pair_below()
def draw_menu(stdscr):
global global_stdscr
global_stdscr = stdscr
setup()
def main():
curses.wrapper(draw_menu)
if __name__ == "__main__":
main()