FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-cli-menu-template/python-cli-menu-template.py at master · jorgeacortes/python-cli-menu-template · GitHub
jorgeacortes
/
python-cli-menu-template
Public template
Notifications
You must be signed in to change notification settings
Fork
2
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-cli-menu-template
/
python-cli-menu-template.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
248 lines (223 loc) · 5.49 KB
Breadcrumbs
python-cli-menu-template
/
python-cli-menu-template.py
Copy path
File metadata and controls
248 lines (223 loc) · 5.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#title :Simple Python Menu
#description :Template for a custom scripting menu
#author :@cortesjorgea
#date :10/2018
#updated :04/2020
#version :2.0
#usage :simple_python_menu.py
#notes :Update section USER CONFIG dicts and reimplement methods
# of menu1 and menu2 classes at BACKEND section.
#python_version :3.8.0
#=======================================================================
import
sys
,
os
,
signal
,
colorama
colorama
.
init
()
# Color init for Windows
# =======================
# DEFINES
# =======================
colors
=
{
"info"
:
"35m"
,
#Orange for info messages
"error"
:
"31m"
,
#Red for error messages
"ok"
:
"32m"
,
#Green for success messages
"menu2c"
:
"
\033
[46m"
,
#Light blue menu
"menu1c"
:
"
\033
[44m"
,
#Blue menu
"close"
:
"
\033
[0m"
#Color coding close
}
cc
=
"
\033
[0m"
ct
=
"
\033
[101m"
cs
=
"
\033
[41m"
c1
=
colors
[
"menu1c"
]
c2
=
colors
[
"menu2c"
]
# =======================
# USER CONFIG
# =======================
programtitle
=
"SimplePythonMenu"
# Fill the options as needed.
menu2_colors
=
{
"ct"
:
ct
,
"cs"
:
cs
,
"opt"
:
c2
}
menu2_options
=
{
"title"
:
"Second menu"
,
"1"
:
"Option 1"
,
"2"
:
"Option 2"
,
"3"
:
"Option 3"
,
"4"
:
"Option 4"
,
"5"
:
"Option 5"
,
"a"
:
"Option a"
,
"s"
:
"Option s"
,
"d"
:
"Option d"
,
"e"
:
"Option e"
,
"f"
:
"Option f"
,
"9"
:
"Main menu"
,
"0"
:
"Quit (or use CNTRL+C)"
,
}
menu1_colors
=
{
"ct"
:
ct
,
"cs"
:
cs
,
"opt"
:
c1
}
menu1_options
=
{
"title"
:
"First menu"
,
"1"
:
"Option 1"
,
"2"
:
"Option 2"
,
"3"
:
"Option 3"
,
"4"
:
"Option 4"
,
"5"
:
"Option 5"
,
"a"
:
"Option a"
,
"s"
:
"Option s"
,
"d"
:
"Option d"
,
"e"
:
"Option e"
,
"f"
:
"Option f"
,
"9"
:
"Second menu"
,
"0"
:
"Quit (or use CNTRL+C)"
,
}
# =======================
# HELPERS
# =======================
def
printWithColor
(
color
,
string
):
print
(
"
\033
["
+
colors
[
color
]
+
" "
+
string
+
cc
)
def
printError
():
printWithColor
(
"error"
,
"Error!!"
)
return
1
def
printSuccess
():
printWithColor
(
"ok"
,
"Success!!"
)
return
0
# Exit program
def
exit
():
sys
.
exit
()
#Handles the CNTRL+C to leave properly the script
def
sigint_handler
(
signum
,
frame
):
print
(
"CNTRL+C exit"
)
sys
.
exit
(
0
)
# =======================
# ACTIONS
# =======================
# Menu template
class
menu_template
():
def
__init__
(
self
,
options
,
colors
):
self
.
menu_width
=
50
# Width in characters of the printed menu
self
.
options
=
options
self
.
colors
=
colors
# =======================
# Menu prints
# =======================
def
createMenuLine
(
self
,
letter
,
color
,
length
,
text
):
menu
=
color
+
" ["
+
letter
+
"] "
+
text
line
=
" "
*
(
length
-
len
(
menu
))
return
menu
+
line
+
cc
def
createMenu
(
self
,
size
):
line
=
self
.
colors
[
"ct"
]
+
" "
+
programtitle
line
+=
" "
*
(
size
-
len
(
programtitle
)
-
6
)
line
+=
cc
print
(
line
)
# Title
line
=
self
.
colors
[
"cs"
]
+
" "
+
self
.
options
[
"title"
]
line
+=
" "
*
(
size
-
len
(
self
.
options
[
"title"
])
-
6
)
line
+=
cc
print
(
line
)
# Subtitle
for
key
in
self
.
options
:
if
(
key
!=
"title"
):
print
(
self
.
createMenuLine
(
key
,
self
.
colors
[
"opt"
],
size
,
self
.
options
[
key
]))
def
printMenu
(
self
):
self
.
createMenu
(
self
.
menu_width
)
# =======================
# Action calls
# =======================
def
action
(
self
,
ch
):
if
ch
==
'1'
:
self
.
method_1
()
elif
ch
==
'2'
:
self
.
method_2
()
elif
ch
==
'3'
:
self
.
method_3
()
elif
ch
==
'4'
:
self
.
method_4
()
elif
ch
==
'5'
:
self
.
method_5
()
elif
ch
==
'a'
:
self
.
method_a
()
elif
ch
==
's'
:
self
.
method_s
()
elif
ch
==
'd'
:
self
.
method_d
()
elif
ch
==
'e'
:
self
.
method_e
()
elif
ch
==
'f'
:
self
.
method_f
()
elif
(
ch
==
''
):
pass
# Print menu again
elif
ch
==
'0'
:
sys
.
exit
()
else
:
printError
()
# =======================
# Empty methods
# =======================
def
method_1
(
self
):
pass
def
method_2
(
self
):
pass
def
method_3
(
self
):
pass
def
method_4
(
self
):
pass
def
method_5
(
self
):
pass
def
method_a
(
self
):
pass
def
method_s
(
self
):
pass
def
method_d
(
self
):
pass
def
method_e
(
self
):
pass
def
method_f
(
self
):
pass
# =======================
# BACKEND
# =======================
# Create here custom actions.
# First menu actions. Implement each method.
class
menu1
(
menu_template
):
pass
# Second menu actions. Implement each method.
class
menu2
(
menu_template
):
pass
# =======================
# MAIN PROGRAM
# =======================
class
menu_handler
:
def
__init__
(
self
):
self
.
current_menu
=
"main"
self
.
m1
=
menu1
(
menu1_options
,
menu1_colors
)
self
.
m2
=
menu2
(
menu2_options
,
menu2_colors
)
def
menuExecution
(
self
):
if
(
self
.
current_menu
==
"main"
):
self
.
m1
.
printMenu
()
else
:
self
.
m2
.
printMenu
()
choice
=
input
(
" >> "
)
if
(
self
.
current_menu
==
"main"
):
if
(
choice
==
"9"
):
self
.
current_menu
=
"second"
else
:
self
.
actuator
(
0
,
choice
)
else
:
if
(
choice
==
'9'
):
self
.
current_menu
=
"main"
else
:
self
.
actuator
(
1
,
choice
)
print
(
"
\n
"
)
def
actuator
(
self
,
type
,
ch
):
if
type
==
0
:
self
.
m1
.
action
(
ch
)
else
:
self
.
m2
.
action
(
ch
)
# Main Program
if
__name__
==
"__main__"
:
x
=
menu_handler
()
signal
.
signal
(
signal
.
SIGINT
,
sigint_handler
)
while
True
:
x
.
menuExecution
()
Back
|
FazBrowse Home
|
New Git URL