#!/usr/bin/env python3 """ Simple example showing how to use selectui as a library. """ import os import sys sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) from selectui import SelectUI def select_programming_language(): """Let user select a programming language.""" languages = [ { "title": "Python", "subtitle": "High-level, interpreted language", "extension": ".py", "year": "1991" }, { "title": "JavaScript", "subtitle": "Dynamic web programming language", "extension": ".js", "year": "1995" }, { "title": "Rust", "subtitle": "Systems programming language", "extension": ".rs", "year": "2010" }, { "title": "Go", "subtitle": "Concurrent programming language", "extension": ".go", "year": "2009" }, { "title": "TypeScript", "subtitle": "Typed superset of JavaScript", "extension": ".ts", "year": "2012" }, ] app = SelectUI( items=languages, oneshot=True, title_key="title", subtitle_key="subtitle", info_key="year" ) app.run() return app.selected_item def select_from_simple_list(): """Select from a simple list of items.""" items = [ {"title": "Option A"}, {"title": "Option B"}, {"title": "Option C"}, {"title": "Option D"}, ] app = SelectUI( items=items, oneshot=True ) app.run() return app.selected_item def select_file(): """Select a file from current directory.""" import os files = [] for filename in os.listdir('.'): if os.path.isfile(filename): size = os.path.getsize(filename) files.append({ "title": filename, "subtitle": f"{size} bytes", "path": os.path.abspath(filename) }) if not files: print("No files found in current directory", file=sys.stderr) return None app = SelectUI( items=files, oneshot=True, title_key="title", subtitle_key="subtitle" ) app.run() return app.selected_item if __name__ == "__main__": print("SelectUI Library Example\n", file=sys.stderr) print("Choose an example:", file=sys.stderr) print("1. Select a programming language", file=sys.stderr) print("2. Select from simple list", file=sys.stderr) print("3. Select a file", file=sys.stderr) choice = input("\nEnter choice (1-3): ") result = None if choice == "1": result = select_programming_language() elif choice == "2": result = select_from_simple_list() elif choice == "3": result = select_file() else: print("Invalid choice", file=sys.stderr) sys.exit(1) if result: print("\nYou selected:", file=sys.stderr) import json print(json.dumps(result, indent=2), file=sys.stderr) else: print("\nNo selection made", file=sys.stderr)