forked from Factorio-Access/FactorioAccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fa_menu.py
272 lines (251 loc) · 9.39 KB
/
fa_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
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
from typing import Any,Callable
import re
import weakref
from translations import translate,localised_str,tprint
import fa_paths
from fa_arg_parse import args
def getAffirmation():
while True:
i = input()
if i == 'yes' or i == 'Yes' or i == 'YES' or i == 'y' or i == 'Y':
return True
elif i == 'no' or i == 'No' or i == 'n' or i == 'N' or i == 'NO':
return False
else:
print("Invalid input, please type either Yes or No")
def getBoolean():
while True:
i = input()
if i == 'true' or i == 'True' or i == 't' or i == 'T' or i == 'TRUE':
return 'true'
elif i == 'false' or i == 'False' or i == 'f' or i == 'F' or i == 'FALSE':
return 'false'
else:
print("Invalid input, please type either true or false")
def getNum():
while True:
i = input()
try:
result = float(i)
return str(result)
except:
print("Invalid input, please enter a number.\n")
def select_option(options,prompt='Select an option:',one_indexed=True):
pre_prompt=None
while True:
print("\033c",end='')
if pre_prompt:
tprint(pre_prompt)
tprint(prompt)
for i, val in enumerate(options):
tprint(f'{i + one_indexed}:', val)
i=input()
if not i.isdigit():
if i=='debug':
args.fa_debug = True
print("debug output")
for name,path in fa_paths.__dict__.items():
if type(path)==str:
print(f'{name:20}:{path}')
pre_prompt="Invalid input, please enter a number."
continue
i=int(i)-one_indexed
if i >= len(options):
pre_prompt="Option too high, please enter a smaller number."
continue
if i<0:
pre_prompt="Options start at 1. Please enter a larger number."
continue
return i
def back_func(*args):
return 1
def do_menu(branch, name, zero_item=("Back",0)):
if callable(branch):
return branch()
if zero_item:
old_b = branch
branch = {zero_item[0]:zero_item[1]}
branch.update(old_b)
while True:
expanded_branch={}
for option, result in branch.items():
if callable(option):
generated_menu=option()
if not generated_menu:
continue
if type(generated_menu)==str:
expanded_branch[generated_menu]=result
else:
for opt, res in option().items():
expanded_branch[opt]=lambda res=res:result(res)
else:
expanded_branch[option]=result
keys=list(expanded_branch)
opt = select_option(keys, prompt=f"{name}:", one_indexed= not zero_item)
if zero_item and opt==0:
return zero_item[1]
key = keys[opt]
ret = do_menu(expanded_branch[key],key)
try:
if ret > 0 and zero_item and zero_item[1]==0:
return ret-1
except:
print(expanded_branch[key],key,"returned",ret)
raise ValueError()
back_menu_item=None
class menu_item(object):
__slots__=["name","submenu","desc","add_back"]
def __init__(self,
name:localised_str|Callable|dict,
submenu:Callable|dict,
desc:localised_str|None = None,
add_back=True) -> None:
self.add_back=add_back
self.name=name
self.desc=desc
if isinstance(submenu,dict):
arr_sub=[]
next_desc=None
for sub_name, sub in submenu.items():
if isinstance(sub,menu_item):
arr_sub.append(sub)
continue
if isinstance(sub_name,str) and sub_name.startswith("_desc"):
next_desc=sub
continue
arr_sub.append(menu_item(sub_name,sub,next_desc))
next_desc=None
submenu=arr_sub
self.submenu=submenu
if not callable(self.submenu) and add_back:
self.submenu=[back_menu_item]+self.submenu
def get_names(self,*args):
if callable(self.name):
return self.name(*args)
return self.name
def get_menu_name(self,*args):
if callable(self.name):
return self.name(*args)
return self.name
def get_header(self,*args):
ret=self.get_menu_name(*args)
if self.desc is not None:
if callable(self.desc):
add=self.desc(*args)
else:
add=self.desc
ret=("",ret,"\n",add)
return ret
def __call__(self,*args):
if callable(self.submenu):
ret = self.submenu(*args)
try:
ret=int(ret)
except:
print(self.submenu, "returned a non integer with arguments:", args)
raise ValueError()
return ret
while True:
options={}
for submenu in self.submenu:
sub_options=submenu.get_names(*args)
if isinstance(sub_options,dict):
for sub_name, sub_arg in sub_options.items():
options[sub_name]=(submenu,(sub_arg,))
else:
options[sub_options]=(submenu,())
keys=list(options)
opts=[translate(key) for key in keys]
selected_menu,arg = options[keys[select_option(opts, prompt=translate(self.get_header(*args)), one_indexed= not self.add_back)]]
ret = selected_menu(*(args+arg))
if ret > 0 and self.add_back:
return ret-1
back_menu_item=menu_item(("gui.cancel",),back_func,False)
class setting_menu(menu_item):
__slots__=["myname","desc","default","val","submenu"]
def __init__(
self,
name:localised_str,
desc:localised_str|None = None,
default:Any = '',
val:Any = '')-> None:
self.myname = name
self.desc = desc
self.default = default
self.val = val
#self.submenu = self.edit
def name(self,*args):
return ("",self.myname,":",self.val_to_string())
def get_options(self):
pass
def input_to_val(self,inp:str):
pass
def val_to_string(self):
return str(self.val)
def __call__(self,*args):
while True:
tprint(self.get_header(*args))
tprint(("fa-l.current-setting",self.val_to_string()))
potential_val=input(translate(('fa-l.new-setting-prompt',)))
if potential_val:
try:
self.input_to_val(potential_val.strip())
except:
print("Invalid value")
continue
return 0
class setting_menu_str(setting_menu):
def __init__(self, name: localised_str, desc: localised_str | None, default: str, val: str) -> None:
super().__init__(name, desc, default, val)
def input_to_val(self, inp: str):
self.val=inp
class setting_menu_int(setting_menu):
def __init__(self, name: localised_str, desc: localised_str | None =None, default: int =0, val: int =0) -> None:
super().__init__(name, desc, default, val)
def input_to_val(self, inp: str):
self.val=int(inp)
class setting_menu_float(setting_menu):
def __init__(self, name: localised_str, desc: localised_str | None =None, default: float =1, val: float =1) -> None:
super().__init__(name, desc, default, val)
def input_to_val(self, inp: str):
self.val=float(inp)
class setting_menu_bool(setting_menu):
def __init__(self, name: localised_str , desc: localised_str | None =None, default=True, val=True) -> None:
super().__init__(name, desc, default, val)
def val_to_string(self):
return translate(("gui-map-generator.enabled",) if self.val else ("gui-mod-info.status-disabled",))
def name(self,*args):
ret=("",self.myname,":",self.val_to_string())
if self.desc:
ret=ret+(" ",self.desc)
return ret
def __call__(self,*args):
self.val = not self.val
return 0
class setting_menu_option(menu_item):
def __init__(self, name: localised_str, val, desc=None) -> None:
self.name=name
self.val=val
self.desc=desc
def get_names(self, *args):
if self.desc:
return ("",self.name,":",self.desc)
return self.name
def __call__(self, *args):
self.parent().val = self.val
return 1
class setting_menu_options(setting_menu):
def __init__(self, name: localised_str, submenu:dict,desc: localised_str | None =None, default: Any = '', val: Any = '') -> None:
super().__init__(name, desc, default, val)
self.submenu = [subval if isinstance(subval,setting_menu_option) else setting_menu_option(subname,subval) for subname,subval in submenu.items()]
for sub in self.submenu:
sub.parent=weakref.ref(self)
self.add_back=True
self.submenu = [back_menu_item]+self.submenu
def val_to_string(self):
maybe=[sub.name for sub in self.submenu[1:] if sub.val==self.val]
if len(maybe):
return translate(maybe[0])
return self.val
def __call__(self,*args):
return menu_item.__call__(self,*args)