mirror of
https://github.com/pdxlocations/contact.git
synced 2026-08-09 10:23:22 +02:00
automatic types
This commit is contained in:
@@ -150,6 +150,15 @@ def draw_help_window(
|
||||
)
|
||||
|
||||
|
||||
def get_input_type_for_field(field) -> type:
|
||||
if field.type in (field.TYPE_INT32, field.TYPE_UINT32, field.TYPE_INT64):
|
||||
return int
|
||||
elif field.type in (field.TYPE_FLOAT, field.TYPE_DOUBLE):
|
||||
return float
|
||||
else:
|
||||
return str
|
||||
|
||||
|
||||
def settings_menu(stdscr: object, interface: object) -> None:
|
||||
curses.update_lines_cols()
|
||||
|
||||
@@ -268,7 +277,8 @@ def settings_menu(stdscr: object, interface: object) -> None:
|
||||
break
|
||||
|
||||
elif selected_option == "Export Config File":
|
||||
filename = get_text_input("Enter a filename for the config file", None)
|
||||
|
||||
filename = get_text_input("Enter a filename for the config file", None, None)
|
||||
if not filename:
|
||||
logging.info("Export aborted: No filename provided.")
|
||||
menu_state.start_index.pop()
|
||||
@@ -327,7 +337,7 @@ def settings_menu(stdscr: object, interface: object) -> None:
|
||||
|
||||
elif selected_option == "Config URL":
|
||||
current_value = interface.localNode.getURL()
|
||||
new_value = get_text_input(f"Config URL is currently: {current_value}", None)
|
||||
new_value = get_text_input(f"Config URL is currently: {current_value}", None, None)
|
||||
if new_value is not None:
|
||||
current_value = new_value
|
||||
overwrite = get_list_input(f"Are you sure you want to load this config?", None, ["Yes", "No"])
|
||||
@@ -396,7 +406,7 @@ def settings_menu(stdscr: object, interface: object) -> None:
|
||||
if selected_option in ["longName", "shortName", "isLicensed"]:
|
||||
if selected_option in ["longName", "shortName"]:
|
||||
new_value = get_text_input(
|
||||
f"{human_readable_name} is currently: {current_value}", selected_option
|
||||
f"{human_readable_name} is currently: {current_value}", selected_option, None
|
||||
)
|
||||
new_value = current_value if new_value is None else new_value
|
||||
menu_state.current_menu[selected_option] = (field, new_value)
|
||||
@@ -416,7 +426,9 @@ def settings_menu(stdscr: object, interface: object) -> None:
|
||||
menu_state.start_index.pop()
|
||||
|
||||
elif selected_option in ["latitude", "longitude", "altitude"]:
|
||||
new_value = get_text_input(f"{human_readable_name} is currently: {current_value}", selected_option)
|
||||
new_value = get_text_input(
|
||||
f"{human_readable_name} is currently: {current_value}", selected_option, None
|
||||
)
|
||||
new_value = current_value if new_value is None else new_value
|
||||
menu_state.current_menu[selected_option] = (field, new_value)
|
||||
|
||||
@@ -455,17 +467,26 @@ def settings_menu(stdscr: object, interface: object) -> None:
|
||||
menu_state.start_index.pop()
|
||||
|
||||
elif field.type == 13: # Field type 13 corresponds to UINT32
|
||||
new_value = get_text_input(f"{human_readable_name} is currently: {current_value}", selected_option)
|
||||
input_type = get_input_type_for_field(field)
|
||||
new_value = get_text_input(
|
||||
f"{human_readable_name} is currently: {current_value}", selected_option, input_type
|
||||
)
|
||||
new_value = current_value if new_value is None else int(new_value)
|
||||
menu_state.start_index.pop()
|
||||
|
||||
elif field.type == 2: # Field type 13 corresponds to INT64
|
||||
new_value = get_text_input(f"{human_readable_name} is currently: {current_value}", selected_option)
|
||||
input_type = get_input_type_for_field(field)
|
||||
new_value = get_text_input(
|
||||
f"{human_readable_name} is currently: {current_value}", selected_option, input_type
|
||||
)
|
||||
new_value = current_value if new_value is None else float(new_value)
|
||||
menu_state.start_index.pop()
|
||||
|
||||
else: # Handle other field types
|
||||
new_value = get_text_input(f"{human_readable_name} is currently: {current_value}", selected_option)
|
||||
input_type = get_input_type_for_field(field)
|
||||
new_value = get_text_input(
|
||||
f"{human_readable_name} is currently: {current_value}", selected_option, input_type
|
||||
)
|
||||
new_value = current_value if new_value is None else new_value
|
||||
menu_state.start_index.pop()
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ from contact.ui.dialog import dialog
|
||||
from contact.utilities.validation_rules import get_validation_for
|
||||
|
||||
|
||||
def get_text_input(prompt: str, selected_config: str) -> Optional[str]:
|
||||
def get_text_input(prompt: str, selected_config: str, input_type: str) -> Optional[str]:
|
||||
"""Handles user input with wrapped text for long prompts."""
|
||||
height = 8
|
||||
width = 80
|
||||
@@ -44,7 +44,7 @@ def get_text_input(prompt: str, selected_config: str) -> Optional[str]:
|
||||
validation = get_validation_for(selected_config)
|
||||
max_length = validation.get("max_length")
|
||||
fixed_length = validation.get("fixed_length")
|
||||
input_type = validation.get("type", str)
|
||||
# input_type = validation.get("type", str)
|
||||
user_input = ""
|
||||
|
||||
# Start user input after the prompt text
|
||||
|
||||
@@ -1,32 +1,7 @@
|
||||
class InputType:
|
||||
STRING = str
|
||||
INTEGER = int
|
||||
FLOAT = float
|
||||
|
||||
|
||||
validation_rules = {
|
||||
"shortName": {
|
||||
"max_length": 4,
|
||||
"type": InputType.STRING,
|
||||
},
|
||||
"longName": {
|
||||
"max_length": 32,
|
||||
"type": InputType.STRING,
|
||||
},
|
||||
"fixed_pin": {
|
||||
"max_length": 6,
|
||||
"fixed_length": 6,
|
||||
"type": InputType.INTEGER,
|
||||
},
|
||||
"adc_multiplier_override": {
|
||||
"type": InputType.FLOAT,
|
||||
},
|
||||
"button_gpio": {
|
||||
"type": InputType.INTEGER,
|
||||
},
|
||||
"buzzer_gpio": {
|
||||
"type": InputType.INTEGER,
|
||||
},
|
||||
"shortName": {"max_length": 4},
|
||||
"longName": {"max_length": 32},
|
||||
"fixed_pin": {"max_length": 6, "fixed_length": 6},
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user