Update menu_functions.py Code Structure: Removed redundant conditions and streamlined logic.

Rescanning Logic: If the user doesn't confirm a single device, it re-prompts for target address or rescans.
This commit is contained in:
Ved Shah 2024-08-18 13:56:27 +05:30 committed by GitHub
parent 2d37faf6cf
commit 9f642e2090
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 25 deletions

View File

@ -8,36 +8,44 @@ import logging as log
def get_target_address(): def get_target_address():
blue = "\033[94m" blue = "\033[94m"
reset = "\033[0m" reset = "\033[0m"
print(f"\n What is the target address{blue}? {reset}Leave blank and we will scan for you{blue}!{reset}") prompt = f"{blue}?{reset} Leave blank and we will scan for you{blue}!{reset}"
target_address = input(f"\n {blue}> ")
if target_address == "": print(f"\nWhat is the target address{prompt}")
target_address = input(f"\n{blue}> {reset}").strip()
if not target_address:
devices = scan_for_devices() devices = scan_for_devices()
if devices: if not devices:
# Check if the returned list is from known devices or scanned devices return
if len(devices) == 1 and isinstance(devices[0], tuple) and len(devices[0]) == 2:
# A single known device was chosen, no need to ask for selection if len(devices) == 1 and isinstance(devices[0], tuple) and len(devices[0]) == 2:
# I think it would be better to ask, as sometimes I do not want to chose this device and actually need solely to scan for actual devices. addr, name = devices[0]
confirm = input(f"\n Would you like to register this device{blue}:\n{reset}{devices[0][1]} {devices[0][0]}{blue}? {blue}({reset}y{blue}/{reset}n{blue}) {blue}").strip().lower() confirm = input(
if confirm == 'y' or confirm == 'yes': f"\nWould you like to register this device{blue}:\n{reset}{name} {addr}{blue}? {reset}(y/n){blue} "
return devices[0][0] ).strip().lower()
elif confirm != 'y' or 'yes': if confirm in {'y', 'yes'}:
return return addr
else: else:
# Show list of scanned devices for user selection return get_target_address() # Rescan if the user doesn't confirm
for idx, (addr, name) in enumerate(devices):
print(f"{reset}[{blue}{idx + 1}{reset}] {blue}Device Name{reset}: {blue}{name}, {blue}Address{reset}: {blue}{addr}") # Show list of scanned devices for user selection
selection = int(input(f"\n{reset}Select a device by number{blue}: {blue}")) - 1 print("\nAvailable devices:")
if 0 <= selection < len(devices): for idx, (addr, name) in enumerate(devices, 1):
target_address = devices[selection][0] print(f"{reset}[{blue}{idx}{reset}] {blue}Device Name{reset}: {blue}{name}, Address{reset}: {blue}{addr}")
else:
print("\nInvalid selection. Exiting.") try:
return selection = int(input(f"\n{reset}Select a device by number{blue}: {reset}")) - 1
else: if 0 <= selection < len(devices):
target_address = devices[selection][0]
else:
print(f"\n{reset}Invalid selection. Exiting.")
return
except ValueError:
print(f"\n{reset}Invalid input. Exiting.")
return return
elif not is_valid_mac_address(target_address): elif not is_valid_mac_address(target_address):
print("\nInvalid MAC address format. Please enter a valid MAC address.") print(f"\n{reset}Invalid MAC address format. Please enter a valid MAC address.")
return return get_target_address() # Prompt again for correct MAC address
return target_address return target_address