1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
#!/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 <example_number>", 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()
|