-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_input.py
More file actions
64 lines (50 loc) · 1.85 KB
/
user_input.py
File metadata and controls
64 lines (50 loc) · 1.85 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
from textual.widget import Widget
from textual.app import ComposeResult
from textual.widgets import TextArea
from textual.binding import Binding
from textual.events import DescendantBlur
from agent_chat_cli.components.caret import Caret
from agent_chat_cli.components.flex import Flex
from agent_chat_cli.core.actions import Actions
from agent_chat_cli.utils.enums import ControlCommand
class UserInput(Widget):
BINDINGS = [
Binding("enter", "submit", "Submit", priority=True),
]
def __init__(self, actions: Actions) -> None:
super().__init__()
self.actions = actions
def compose(self) -> ComposeResult:
with Flex():
yield Caret()
yield TextArea(
"",
show_line_numbers=False,
soft_wrap=True,
)
def on_mount(self) -> None:
input_widget = self.query_one(TextArea)
input_widget.focus()
def on_descendant_blur(self, event: DescendantBlur) -> None:
if isinstance(event.widget, TextArea):
event.widget.focus(scroll_visible=False)
async def on_key(self, event) -> None:
if event.key == "ctrl+j":
event.stop()
event.prevent_default()
input_widget = self.query_one(TextArea)
input_widget.insert("\n")
async def action_submit(self) -> None:
input_widget = self.query_one(TextArea)
user_message = input_widget.text.strip()
if not user_message:
return
if user_message.lower() == ControlCommand.EXIT.value:
self.actions.quit()
return
input_widget.clear()
if user_message.lower() == ControlCommand.CLEAR.value:
await self.actions.interrupt()
await self.actions.new()
return
await self.actions.submit_user_message(user_message)