2025-12-20 21:04:21 +02:00
|
|
|
# deck.chat: T-Deck generic chat UI helper library for CircuitPython
|
|
|
|
|
# Depends on tdeck_repl for providing working input on T-Decks
|
|
|
|
|
#
|
|
|
|
|
# Created by Luxferre in 2025, released into public domain
|
|
|
|
|
|
2025-12-21 10:48:40 +02:00
|
|
|
from deck.input import input
|
2025-12-21 14:05:03 +02:00
|
|
|
from deck.pager import print_paged
|
2025-12-20 21:04:21 +02:00
|
|
|
|
|
|
|
|
class DeckChat:
|
2025-12-21 14:05:03 +02:00
|
|
|
def __init__(self, start_state={}, chat_prefix='> ', command_prefix='/'):
|
2025-12-20 21:04:21 +02:00
|
|
|
"""Init the chat class"""
|
|
|
|
|
self.command_prefix = command_prefix # prefix that commands start with
|
|
|
|
|
self.chat_prefix = chat_prefix # prefix to write before input
|
|
|
|
|
self.command_handlers = { # command handler storage
|
2025-12-20 21:22:06 +02:00
|
|
|
'default': lambda m,s: (m,s) # echo handler by default
|
2025-12-20 21:04:21 +02:00
|
|
|
}
|
|
|
|
|
self.state = start_state # internal state object (impl-specific)
|
|
|
|
|
|
|
|
|
|
def command(self, command, handler):
|
|
|
|
|
"""
|
|
|
|
|
Register a new handler inside the chat instance.
|
2025-12-20 21:24:56 +02:00
|
|
|
The handler accepts two parameters: a message and a state object,
|
|
|
|
|
and returns the response and the new state object.
|
2025-12-20 21:26:07 +02:00
|
|
|
If the returned state object is None, then the chat loop exits.
|
2025-12-20 21:24:56 +02:00
|
|
|
Non-command handler is registered under the 'default' command.
|
2025-12-20 21:04:21 +02:00
|
|
|
"""
|
|
|
|
|
cmd = command.strip().lower()
|
|
|
|
|
if cmd.startswith(self.command_prefix):
|
|
|
|
|
cmd = cmd[1:]
|
|
|
|
|
self.command_handlers[cmd] = handler
|
|
|
|
|
|
|
|
|
|
def _detect_command(self, msg):
|
|
|
|
|
"""
|
2025-12-20 21:26:54 +02:00
|
|
|
Split a chat line into the command (or 'default' if none detected)
|
2025-12-20 21:04:21 +02:00
|
|
|
and the rest of the message.
|
|
|
|
|
"""
|
|
|
|
|
res = ('default', msg)
|
|
|
|
|
if msg.startswith(self.command_prefix):
|
2025-12-20 22:50:08 +02:00
|
|
|
parts = msg.split(' ')
|
|
|
|
|
cmd = parts[0].lower().strip()[1:]
|
|
|
|
|
arg = ' '.join(parts[1:]).strip() if len(parts) > 1 else None
|
2025-12-20 21:04:21 +02:00
|
|
|
if cmd in self.command_handlers:
|
|
|
|
|
res = (cmd, arg)
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
"""Start the chat loop"""
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
2025-12-20 21:20:07 +02:00
|
|
|
input_msg = input(self.chat_prefix).strip()
|
2025-12-21 08:40:28 +02:00
|
|
|
if len(input_msg) == 0: continue
|
2025-12-20 21:04:21 +02:00
|
|
|
cmd, msg = self._detect_command(input_msg)
|
2025-12-20 21:19:15 +02:00
|
|
|
output, self.state = self.command_handlers[cmd](msg, self.state)
|
2025-12-21 14:05:03 +02:00
|
|
|
print_paged(output.strip())
|
2025-12-20 21:04:21 +02:00
|
|
|
if self.state is None:
|
|
|
|
|
break
|
2025-12-21 10:48:40 +02:00
|
|
|
except (KeyboardInterrupt, EOFError):
|
|
|
|
|
print()
|
|
|
|
|
break
|