#!/usr/bin/env python3 """ Example demonstrating how to use selectui as a library. This shows various ways to integrate SelectUI into your Python applications. """ import sys from selectui import SelectUI def example_basic(): """Basic usage: simple item selection.""" print("Example 1: Basic item selection\n", file=sys.stderr) items = [ {"title": "Python", "subtitle": "High-level programming language"}, {"title": "JavaScript", "subtitle": "Dynamic web programming language"}, {"title": "Rust", "subtitle": "Systems programming language"}, {"title": "Go", "subtitle": "Concurrent programming language"}, {"title": "Java", "subtitle": "Object-oriented programming language"}, ] app = SelectUI( items=items, oneshot=True, title_key="title", subtitle_key="subtitle" ) app.run() if app.selected_item: print(f"\nYou selected: {app.selected_item['title']}", file=sys.stderr) return app.selected_item return None def example_custom_keys(): """Using custom keys for title/subtitle.""" print("Example 2: Custom keys for title/subtitle\n", file=sys.stderr) items = [ {"name": "Alice", "role": "Engineer", "department": "Backend"}, {"name": "Bob", "role": "Designer", "department": "Product"}, {"name": "Charlie", "role": "Manager", "department": "Engineering"}, {"name": "Diana", "role": "Developer", "department": "Frontend"}, ] app = SelectUI( items=items, oneshot=True, title_key="name", subtitle_key="role", info_key="department" ) app.run() if app.selected_item: print(f"\nYou selected: {app.selected_item['name']}", file=sys.stderr) return app.selected_item return None def example_continuous_mode(): """Continuous selection mode (not oneshot).""" print("Example 3: Continuous selection mode\n", file=sys.stderr) print("Press Enter multiple times to select items, q/Esc to quit\n", file=sys.stderr) items = [ {"title": "Item 1", "value": "A"}, {"title": "Item 2", "value": "B"}, {"title": "Item 3", "value": "C"}, {"title": "Item 4", "value": "D"}, ] # Capture selections to a list selections = [] def capture_selection(item): selections.append(item) app = SelectUI( items=items, oneshot=False, # Allow multiple selections title_key="title" ) app.run() print(f"\nTotal selections made: {len(selections)}", file=sys.stderr) return selections def example_events_mode(): """Events mode with custom key handlers.""" print("Example 4: Events mode\n", file=sys.stderr) print("Press Ctrl+D to delete, Ctrl+E to edit, Enter to select\n", file=sys.stderr) items = [ {"title": "Task 1", "status": "pending"}, {"title": "Task 2", "status": "in-progress"}, {"title": "Task 3", "status": "completed"}, ] app = SelectUI( items=items, oneshot=False, events_mode=True, title_key="title", subtitle_key="status" ) app.run() def example_single_line(): """Single-line display (no subtitle).""" print("Example 5: Single-line display\n", file=sys.stderr) items = [ {"filename": "config.json"}, {"filename": "package.json"}, {"filename": "README.md"}, {"filename": "setup.py"}, ] app = SelectUI( items=items, oneshot=True, title_key="filename", subtitle_key=None # No subtitle = single-line display ) app.run() if app.selected_item: print(f"\nYou selected: {app.selected_item['filename']}", file=sys.stderr) return app.selected_item return None def example_command_mode(): """Command mode with dynamic item generation.""" print("Example 6: Command mode with dynamic search\n", file=sys.stderr) print("Type to search, Tab to switch between input/search modes\n", file=sys.stderr) # Use find command to search for files # The {} will be replaced with user input app = SelectUI( items=[], oneshot=True, command_template='find . -type f -name "*{}*" 2>/dev/null | head -20 | while read f; do echo "{\\"title\\": \\"$f\\"}"; done' ) app.run() if app.selected_item: print(f"\nYou selected: {app.selected_item.get('title', 'N/A')}", file=sys.stderr) return app.selected_item return None def main(): """Run examples based on command line argument.""" if len(sys.argv) < 2: print("Usage: python library_usage.py ", file=sys.stderr) print("\nAvailable examples:", file=sys.stderr) print(" 1 - Basic item selection", file=sys.stderr) print(" 2 - Custom keys for title/subtitle", file=sys.stderr) print(" 3 - Continuous selection mode", file=sys.stderr) print(" 4 - Events mode", file=sys.stderr) print(" 5 - Single-line display", file=sys.stderr) print(" 6 - Command mode with dynamic search", file=sys.stderr) sys.exit(1) example_num = sys.argv[1] examples = { "1": example_basic, "2": example_custom_keys, "3": example_continuous_mode, "4": example_events_mode, "5": example_single_line, "6": example_command_mode, } if example_num not in examples: print(f"Error: Example {example_num} not found", file=sys.stderr) sys.exit(1) # Run the selected example result = examples[example_num]() # Output result as JSON to stdout (if any) if result: import json print(json.dumps(result, indent=2)) if __name__ == "__main__": main()