This directory contains examples of how to use and extend the menu system in Code Assistant Manager.
The custom_menu_example.py demonstrates how to create custom menu classes by inheriting from the Menu base class.
python examples/custom_menu_example.pyA menu that displays items with alternating colors (yellow, green, cyan, blue).
from code_assistant_manager.menu import Menu, Colors
class ColoredMenu(Menu):
"""A custom menu that displays items with different colors."""
def _draw_menu_items(self, highlighted_idx: int = -1):
# Custom implementation with alternating colors
...A menu that adds item numbers as prefixes in the display text.
class NumberedPrefixMenu(Menu):
"""A custom menu that adds item numbers to the display."""
def _draw_menu_items(self, highlighted_idx: int = -1):
# Add [N] prefix to each item
...A basic menu with arrow key navigation but no filtering.
from code_assistant_manager.menu import SimpleMenu
menu = SimpleMenu(
title="Select an Option",
items=["Option 1", "Option 2", "Option 3"],
cancel_text="Cancel"
)
success, idx = menu.display()A menu with dynamic filtering capability. Users can type to filter items.
from code_assistant_manager.menu import FilterableMenu
menu = FilterableMenu(
title="Select a Model",
items=["gpt-4", "gpt-3.5-turbo", "claude-3", "claude-2"],
cancel_text="Cancel"
)
success, idx = menu.display()To create a custom menu, inherit from Menu and implement these abstract methods:
_calculate_menu_height()- Calculate the height of your menu_draw_menu_items(highlighted_idx)- Draw the menu items_get_prompt_text()- Return the prompt text for user inputdisplay()- Handle the display and user interaction logic
Optional methods you can override:
_handle_navigation_key(char)- Custom navigation key handling_handle_selection()- Custom selection handling_draw_menu(highlighted_idx)- Complete menu drawing logic
from code_assistant_manager.menu import Menu
from typing import Tuple, Optional
class MyCustomMenu(Menu):
"""My custom menu implementation."""
def _calculate_menu_height(self) -> int:
# Calculate and return menu height
return len(self.items) + 5
def _draw_menu_items(self, highlighted_idx: int = -1):
# Draw menu items with custom styling
for i, item in enumerate(self.items):
# Your custom drawing logic here
pass
def _get_prompt_text(self) -> str:
return "Your custom prompt: "
def display(self) -> Tuple[bool, Optional[int]]:
# You can either implement custom display logic
# or reuse SimpleMenu or FilterableMenu's display method
from code_assistant_manager.menu import SimpleMenu
return SimpleMenu.display(self)When creating a custom menu, you have access to these properties:
self.title- Menu titleself.items- List of menu itemsself.cancel_text- Text for cancel optionself.max_attempts- Maximum input attemptsself.key_provider- Optional key provider (for testing)self.selected_idx- Currently selected indexself.term_width- Terminal widthself.term_height- Terminal heightself.max_item_len- Calculated menu widthself.left_margin- Left margin for centering
self._draw_border_top()- Draw top borderself._draw_border_bottom()- Draw bottom borderself._draw_separator()- Draw horizontal separatorself._draw_title()- Draw menu titleself._draw_item(num, item, is_highlighted)- Draw a single itemself._draw_cancel_option(is_highlighted)- Draw cancel optionself._get_key()- Get a single key from userself._clear_screen()- Clear the terminalself._redraw_with_prompt()- Redraw menu with prompt