Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions commando/ui/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def factory_reset(state_manager):
pause()


def explore_category(state_manager):
def explore_category(state_manager, search_command_fn):
all_known = state_manager.get_all_known_commands()
categories = sorted(list(set(data["category"] for data in all_known.values())))

Expand All @@ -163,7 +163,14 @@ def explore_category(state_manager):
for cmd, data in all_known.items():
if data["category"] == selected_cat:
print(f" • {CYAN}{BOLD}{cmd}{RESET}: {data['desc']}")
pause()

print(f"\n{YELLOW}Options:{RESET}")
print(f" - Type a command name to search for it.")
print(" - Press Enter to return to the menu.")

search_choice = input(f"\n{GREEN}➜ {RESET}").strip()
if search_choice:
search_command_fn(search_choice.split()[0].lower())
else:
print(f"{RED}Invalid selection.{RESET}")
pause()
Expand All @@ -188,17 +195,33 @@ def manage_imports(state_manager):
print(f" • {badge} {CYAN}{BOLD}{cmd}{RESET}: {short_desc}")

print(f"\n{YELLOW}Options:{RESET}")
print(f" - Type a command name to {RED}DELETE{RESET} it from the database.")
print(f" - Type a command name (or comma-separated list) to {RED}DELETE{RESET} from the database.")
print(f" - Type {RED}all{RESET} to clear all custom imports.")
print(" - Press Enter to return to the menu.")

choice = input(f"\n{GREEN}➜ {RESET}").strip().lower()
if choice in custom_guide:
del custom_guide[choice]
save_json(CUSTOM_DICT_FILE, custom_guide)
print(f"{YELLOW}Successfully deleted '{choice}'.{RESET}")
if choice == "all":
confirm = input(f"{RED}Are you sure you want to delete ALL custom imports? (y/n): {RESET}").strip().lower()
if confirm == 'y':
custom_guide.clear()
state_manager.save_custom()
print(f"{YELLOW}Successfully deleted all custom imports.{RESET}")
pause()
elif choice:
print(f"{RED}Command not found in custom imports.{RESET}")
deleted = []
not_found = []
for cmd in dict.fromkeys(c.strip() for c in choice.split(",") if c.strip()):
if cmd in custom_guide:
del custom_guide[cmd]
deleted.append(cmd)
else:
not_found.append(cmd)

if deleted:
state_manager.save_custom()
print(f"{YELLOW}Successfully deleted: {', '.join(deleted)}{RESET}")
if not_found:
print(f"{RED}Not found in custom imports: {', '.join(not_found)}{RESET}")
pause()
Comment on lines 202 to 225

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There are two improvement opportunities here:

  1. Encapsulation: Instead of calling save_json(CUSTOM_DICT_FILE, custom_guide) directly, we should use the state_manager.save_custom() method to respect the encapsulation of the StateManager class.
  2. Duplicate Handling: If the user inputs duplicate commands in the comma-separated list (e.g., ls, ls), the second occurrence will trigger the not_found block and display a confusing message. We can use dict.fromkeys to deduplicate the input list while preserving the user's input order.
    choice = input(f"\\n{GREEN}{RESET}").strip().lower()\n    if choice == "all":\n        confirm = input(f"{RED}Are you sure you want to delete ALL custom imports? (y/n): {RESET}").strip().lower()\n        if confirm == 'y':\n            custom_guide.clear()\n            state_manager.save_custom()\n            print(f"{YELLOW}Successfully deleted all custom imports.{RESET}")\n        pause()\n    elif choice:\n        deleted = []\n        not_found = []\n        for cmd in dict.fromkeys(c.strip() for c in choice.split(\",\") if c.strip()):\n            if cmd in custom_guide:\n                del custom_guide[cmd]\n                deleted.append(cmd)\n            else:\n                not_found.append(cmd)\n\n        if deleted:\n            state_manager.save_custom()\n            print(f"{YELLOW}Successfully deleted: {', '.join(deleted)}{RESET}")\n        if not_found:\n            print(f"{RED}Not found in custom imports: {', '.join(not_found)}{RESET}")\n        pause()



Expand Down Expand Up @@ -430,7 +453,7 @@ def main_loop(state_manager, search_command_fn, auto_scan_fn):
search_command_fn(cmd)
continue
elif raw_input == "2":
explore_category(state_manager)
explore_category(state_manager, search_command_fn)
continue
elif raw_input == "3":
manage_imports(state_manager)
Expand Down
Loading