forked from koaning/bespoken
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.py
More file actions
244 lines (195 loc) · 8.79 KB
/
demo.py
File metadata and controls
244 lines (195 loc) · 8.79 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
from textual.app import App, ComposeResult
from textual.widgets import Input, Static, Label
from textual.containers import Container, VerticalScroll, Horizontal
from textual.events import Key
from pathlib import Path
import os
from textual_autocomplete import AutoComplete
from textual_autocomplete._autocomplete import DropdownItem, TargetState
class CustomAutoComplete(AutoComplete):
def get_search_string(self, state: TargetState) -> str:
# Get the last word including special prefixes like @ or /
text = state.text
cursor_pos = state.cursor_position
# Find the start of the current word (look backwards for space)
word_start = cursor_pos
while word_start > 0 and text[word_start - 1] not in ' \t\n':
word_start -= 1
# Return the word from start to cursor position
return text[word_start:cursor_pos]
def should_show_dropdown(self, search_string: str) -> bool:
"""Always show dropdown when we have candidates"""
# Show dropdown when:
# 1. Search string contains @ (file paths)
# 2. Search string starts with / (commands)
# 3. Search string ends with / (directory navigation)
should_show = "@" in search_string or search_string.startswith("/") or search_string.endswith("/")
# If we should show and we're at a directory boundary, make sure dropdown is visible
if should_show and search_string.endswith("/") and hasattr(self, '_dropdown'):
self.call_later(lambda: self.action_show())
return should_show
def apply_completion(self, value: str, state: TargetState) -> None:
"""Replace the current word with the selected completion"""
text = state.text
cursor_pos = state.cursor_position
# Find the start of the current word (look backwards for space or start of string)
word_start = cursor_pos
while word_start > 0 and text[word_start - 1] not in ' \t\n':
word_start -= 1
# Find the end of the current word (look forwards for space or end of string)
word_end = cursor_pos
while word_end < len(text) and text[word_end] not in ' \t\n':
word_end += 1
# Replace the current word with the completion
new_text = text[:word_start] + value + text[word_end:]
new_cursor_pos = word_start + len(value)
# Update the input
self.target.value = new_text
self.target.cursor_position = new_cursor_pos
# Rebuild the dropdown with the new state
new_target_state = self._get_target_state()
search_string = self.get_search_string(new_target_state)
self._rebuild_options(new_target_state, search_string)
# If we just completed a directory (ends with /), force refresh the dropdown
if value.endswith("/"):
# Force the autocomplete to update with the new directory contents
self.call_after_refresh(self._handle_target_update)
class DynamicDataApp(App[None]):
CSS = """
#output-container {
height: 1fr;
padding: 1;
align-vertical: bottom;
}
#input-container {
height: 3;
dock: bottom;
}
Input {
dock: bottom;
}
#prompt-label {
width: 2;
content-align: center middle;
color: $primary;
}
Horizontal {
height: 3;
}
.user-message {
color: $primary;
margin-bottom: 1;
}
.bot-message {
color: $success;
margin-bottom: 1;
}
"""
def compose(self) -> ComposeResult:
# Output area at the top
with VerticalScroll(id="output-container"):
pass # Start empty, welcome message will be added in on_mount
# Input area at the bottom
with Container(id="input-container"):
with Horizontal():
yield Label(">", id="prompt-label")
input_widget = Input(placeholder="Type @ for files, / for commands...", id="chat-input")
yield input_widget
self.autocomplete = CustomAutoComplete(input_widget, candidates=self.candidates_callback)
yield self.autocomplete
def candidates_callback(self, state: TargetState) -> list[DropdownItem]:
# Get the current word at cursor position
text = state.text
cursor_pos = state.cursor_position
# Find the start of the current word
word_start = cursor_pos
while word_start > 0 and text[word_start - 1] not in ' \t\n':
word_start -= 1
current_word = text[word_start:cursor_pos]
# Check if we're looking for file paths
if current_word.startswith("@"):
return self.get_file_candidates(current_word[1:]) # Remove @ prefix
# Check if we're looking for commands
elif current_word.startswith("/"):
return self.get_command_candidates(current_word)
# Otherwise return empty list (no suggestions for regular words in this example)
return []
def get_command_candidates(self, prefix: str) -> list[DropdownItem]:
"""Get command candidates that start with the given prefix"""
all_commands = [
"/quit",
"/start",
"/stop",
"/help",
"/clear",
"/status"
]
filtered = [cmd for cmd in all_commands if cmd.startswith(prefix)]
return [DropdownItem(cmd, prefix="📝 ") for cmd in filtered]
def get_file_candidates(self, partial_path: str) -> list[DropdownItem]:
"""Get file/directory candidates based on partial path"""
print(f"DEBUG: Getting file candidates for: '{partial_path}'")
try:
# Handle cases like "src/" where we want contents of src
if partial_path.endswith("/"):
base_dir = partial_path.rstrip("/") if partial_path.rstrip("/") else "."
prefix = ""
elif not partial_path:
base_dir = "."
prefix = ""
else:
# Split into directory and partial filename
if "/" in partial_path:
base_dir = os.path.dirname(partial_path)
prefix = os.path.basename(partial_path)
else:
base_dir = "."
prefix = partial_path
# Get all items in the directory
items = []
base_path = Path(base_dir)
if base_path.exists() and base_path.is_dir():
for item in base_path.iterdir():
if prefix and not item.name.startswith(prefix):
continue
# Build the path relative to current directory
if partial_path and "/" in partial_path:
# Keep the directory part
rel_path = os.path.join(os.path.dirname(partial_path), item.name)
else:
rel_path = item.name
# Add @ prefix back and indicate if it's a directory
if item.is_dir():
items.append(DropdownItem(f"@{rel_path}/", prefix="📁 "))
else:
items.append(DropdownItem(f"@{rel_path}", prefix="📄 "))
return sorted(items, key=lambda x: x.value)[:20] # Limit to 20 results
except (OSError, PermissionError):
return []
def on_mount(self) -> None:
"""Focus the input when the app starts"""
# Add welcome message
output_container = self.query_one("#output-container")
output_container.mount(Static("Welcome! Try typing @ for files or / for commands", classes="bot-message"))
# Focus the input
self.query_one("#chat-input").focus()
def on_input_submitted(self, event: Input.Submitted) -> None:
"""Handle when user presses Enter"""
if not event.value:
return
# Add user message
output_container = self.query_one("#output-container")
output_container.mount(Static(f"[dim]{event.value}[/dim]", classes="user-message"))
# Add bot echo response
output_container.mount(Static(f"{event.value}", classes="bot-message"))
# Clear input
event.input.value = ""
# Scroll to bottom
output_container.scroll_end()
def on_key(self, event: Key) -> None:
# Handle Ctrl+C to quit
if event.key == "ctrl+c":
self.exit()
if __name__ == "__main__":
app = DynamicDataApp()
app.run()