123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import dotenv
- import getpass
- import platform
- import os
- from prompt_toolkit.shortcuts import prompt
- from prompt_toolkit import print_formatted_text, HTML
- from prompt_toolkit.styles import Style
- from prompt_toolkit.cursor_shapes import CursorShape
- from prompt_toolkit.shortcuts import button_dialog
- from prompt_toolkit.application import run_in_terminal
- from prompt_toolkit.key_binding import KeyBindings
- from prompt_toolkit import PromptSession
- from prompt_toolkit.history import FileHistory
- dotenv.load_dotenv()
- # bindings = KeyBindings()
- # @bindings.add('c-m')
- # def _(event):
- # " Say 'hello' when `c-t` is pressed. "
- # def print_hello():
- # print('hello world')
- # run_in_terminal(print_hello)
- # @bindings.add('c-x')
- # def _(event):
- # " Exit when `c-x` is pressed. "
- # event.app.exit()
- class PromptTheme:
- def __init__(self, appname):
- self.message = [
- ('class:username', getpass.getuser()),
- ('class:at', '@'),
- ('class:host', platform.node()),
- ('class:colon', ':'),
- ('class:appname', appname),
- ('class:pound', '# '),
- ]
- self.style = Style.from_dict({
- '': '#ffffff',
- 'username': '#884444',
- 'at': '#00aa00',
- 'colon': '#ffffff',
- 'pound': '#00aa00',
- 'appname': '#00ffff bg:#444400',
- 'path': 'ansicyan underline',
- 'info': 'black bg:white',
- 'warning': 'yellow bg:white',
- 'error': 'red bg:white',
- 'bottom-toolbar': '#ffffff bg:#333333',
-
- })
- if os.getenv('PROMPT_THEME'):
- theme = os.getenv('PROMPT_THEME')
- if theme == 'dark_plus':
- self.style = Style.from_dict({
- '': '#ffffff',
- 'username': '#884444',
- 'at': '#00aa00',
- 'colon': '#ffffff',
- 'pound': '#00aa00',
- 'appname': '#00ffff bg:#444400',
- 'path': 'ansicyan underline',
- 'info': 'black bg:white',
- 'warning': 'red bg:yellow',
- 'error': 'red bg:white',
- 'bottom-toolbar': '#ffffff bg:#333333',
- })
- class Prompt:
- def __init__(self, appname='chitchat'):
- theme = PromptTheme(appname)
- self.message = theme.message
- self.style = theme.style
- self.bottom_toolbar_text = ' Press [Ctrl+X] to exit '
- # if history file is not present, create it
- history_file = os.path.expanduser('~/.aiassistant_history')
- if not os.path.exists(history_file):
- with open(history_file, 'w') as f:
- pass
-
- self.session = PromptSession(history=FileHistory(history_file))
- def bottom_toolbar(self):
- return [('class:bottom-toolbar', f"@aiassistant:~ {self.bottom_toolbar_text}")]
- def print(self, message, tag='info'):
- print_formatted_text(HTML(f"<{tag}>{message}</{tag}>"), style=self.style)
- def get_input_mode(self):
- return button_dialog(
- title='Input Mode',
- text='What input mode do you want to use?',
- buttons=[
- ('Text', 'text'),
- ('Voice', 'voice'),
- ('Surprise me', None)
- ],
- ).run()
-
- def get(self):
- return self.session.prompt(
- self.message,
- style=self.style,
- cursor=CursorShape.BLINKING_BLOCK,
- bottom_toolbar=self.bottom_toolbar,
- )
|