prompt.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import dotenv
  2. import getpass
  3. import platform
  4. import os
  5. from prompt_toolkit.shortcuts import prompt
  6. from prompt_toolkit import print_formatted_text, HTML
  7. from prompt_toolkit.styles import Style
  8. from prompt_toolkit.cursor_shapes import CursorShape
  9. from prompt_toolkit.shortcuts import button_dialog
  10. from prompt_toolkit.application import run_in_terminal
  11. from prompt_toolkit.key_binding import KeyBindings
  12. from prompt_toolkit import PromptSession
  13. from prompt_toolkit.history import FileHistory
  14. dotenv.load_dotenv()
  15. # bindings = KeyBindings()
  16. # @bindings.add('c-m')
  17. # def _(event):
  18. # " Say 'hello' when `c-t` is pressed. "
  19. # def print_hello():
  20. # print('hello world')
  21. # run_in_terminal(print_hello)
  22. # @bindings.add('c-x')
  23. # def _(event):
  24. # " Exit when `c-x` is pressed. "
  25. # event.app.exit()
  26. class PromptTheme:
  27. def __init__(self, appname):
  28. self.message = [
  29. ('class:username', getpass.getuser()),
  30. ('class:at', '@'),
  31. ('class:host', platform.node()),
  32. ('class:colon', ':'),
  33. ('class:appname', appname),
  34. ('class:pound', '# '),
  35. ]
  36. self.style = Style.from_dict({
  37. '': '#ffffff',
  38. 'username': '#884444',
  39. 'at': '#00aa00',
  40. 'colon': '#ffffff',
  41. 'pound': '#00aa00',
  42. 'appname': '#00ffff bg:#444400',
  43. 'path': 'ansicyan underline',
  44. 'info': 'black bg:white',
  45. 'warning': 'yellow bg:white',
  46. 'error': 'red bg:white',
  47. 'bottom-toolbar': '#ffffff bg:#333333',
  48. })
  49. if os.getenv('PROMPT_THEME'):
  50. theme = os.getenv('PROMPT_THEME')
  51. if theme == 'dark_plus':
  52. self.style = Style.from_dict({
  53. '': '#ffffff',
  54. 'username': '#884444',
  55. 'at': '#00aa00',
  56. 'colon': '#ffffff',
  57. 'pound': '#00aa00',
  58. 'appname': '#00ffff bg:#444400',
  59. 'path': 'ansicyan underline',
  60. 'info': 'black bg:white',
  61. 'warning': 'red bg:yellow',
  62. 'error': 'red bg:white',
  63. 'bottom-toolbar': '#ffffff bg:#333333',
  64. })
  65. class Prompt:
  66. def __init__(self, appname='chitchat'):
  67. theme = PromptTheme(appname)
  68. self.message = theme.message
  69. self.style = theme.style
  70. self.bottom_toolbar_text = ' Press [Ctrl+X] to exit '
  71. # if history file is not present, create it
  72. history_file = os.path.expanduser('~/.aiassistant_history')
  73. if not os.path.exists(history_file):
  74. with open(history_file, 'w') as f:
  75. pass
  76. self.session = PromptSession(history=FileHistory(history_file))
  77. def bottom_toolbar(self):
  78. return [('class:bottom-toolbar', f"@aiassistant:~ {self.bottom_toolbar_text}")]
  79. def print(self, message, tag='info'):
  80. print_formatted_text(HTML(f"<{tag}>{message}</{tag}>"), style=self.style)
  81. def get_input_mode(self):
  82. return button_dialog(
  83. title='Input Mode',
  84. text='What input mode do you want to use?',
  85. buttons=[
  86. ('Text', 'text'),
  87. ('Voice', 'voice'),
  88. ('Surprise me', None)
  89. ],
  90. ).run()
  91. def get(self):
  92. return self.session.prompt(
  93. self.message,
  94. style=self.style,
  95. cursor=CursorShape.BLINKING_BLOCK,
  96. bottom_toolbar=self.bottom_toolbar,
  97. )