forked from bubbl/ssh-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
executable file
·94 lines (79 loc) · 2.22 KB
/
menu.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
import curses, os
import menulist
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
screen.keypad(1)
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_GREEN)
h = curses.color_pair(1)
n = curses.A_NORMAL
MENU = "menu"
COMMAND = "command"
EXITMENU = "exitmenu"
menu_data = menulist.menu_data
def runmenu(menu, parent):
if parent is None:
lastoption = "Exit"
else:
lastoption = "Return to %s menu" % parent['title']
optioncount = len(menu['options'])
pos=0
oldpos=None
x = None
while x != ord('\n'):
if pos != oldpos:
oldpos = pos
screen.border(1,1,1,1)
screen.addstr(2,2, menu['title'], curses.A_STANDOUT)
screen.addstr(4,2, menu['subtitle'], curses.A_BOLD)
for index in range(optioncount):
textstyle = n
if pos == index:
textstyle = h
screen.addstr(7+index,4, "%d - %s" % (index+1, menu['options'][index]['title']), textstyle)
textstyle = n
if pos == optioncount:
textstyle = h
screen.addstr(7+optioncount,4, "%d - %s" % (optioncount+1, lastoption), textstyle)
screen.refresh()
x = screen.getch()
if x >= ord('1') and x <= ord(str(optioncount+1)):
pos = x - ord('0') - 1
elif x == 258:
if pos < optioncount:
pos += 1
else: pos = 0
elif x == 259:
if pos > 0:
pos += -1
else: pos = optioncount
return pos
def processmenu(menu, parent = None):
optioncount = len(menu['options'])
exitmenu = False
while not exitmenu:
getin = runmenu(menu, parent)
if getin == optioncount:
exitmenu = True
elif menu['options'][getin]['type'] == COMMAND:
curses.def_prog_mode()
os.system('reset')
screen.clear()
os.system(menu['options'][getin]['command'])
screen.clear()
curses.reset_prog_mode()
curses.curs_set(1)
curses.curs_set(0)
elif menu['options'][getin]['type'] == MENU:
screen.clear()
processmenu(menu['options'][getin], menu)
screen.clear()
elif menu['options'][getin]['type'] == EXITMENU:
exitmenu = True
processmenu(menu_data)
curses.endwin()
os.system('clear')