| 10 | |
| 11 | |
| 12 | class Console: |
| 13 | def __init__(self, history_dir=".history"): |
| 14 | os.makedirs(history_dir, exist_ok=True) |
| 15 | self.prompt_history = FileHistory(os.path.join(history_dir, "question")) |
| 16 | self.prompt_file_history = FileHistory(os.path.join(history_dir, "file")) |
| 17 | self.prompt_auto_suggest = AutoSuggestFromHistory() |
| 18 | self.prompt_style = Style.from_dict({"prompt": "#7fff00 bold", "": "#ADD8E6"}) |
| 19 | self._console = RawConsole() |
| 20 | |
| 21 | def print(self, text, render=True): |
| 22 | if render: |
| 23 | self._console.print(Markdown(text)) |
| 24 | else: |
| 25 | self._console.print(text) |
| 26 | |
| 27 | def info(self, text): |
| 28 | self._console.print(f"[blue]{text}[/blue]") |
| 29 | |
| 30 | def info2(self, text): |
| 31 | self._console.print(f"[cyan]{text}[/cyan]") |
| 32 | |
| 33 | def warning(self, text): |
| 34 | self._console.print(f"[yellow]{text}[/yellow]") |
| 35 | |
| 36 | def error(self, text): |
| 37 | self._console.print(f"[red]{text}[/red]") |
| 38 | |
| 39 | def bot_prompt(self): |
| 40 | self._console.print(f"\n[green][bold]Bot: [/bold][/green]") |
| 41 | |
| 42 | def print_history_item(self, idx, entry): |
| 43 | summary = entry[:self._console.width - 20] |
| 44 | self._console.print(f"[bold]{idx:>4}: [/bold] {summary}...") |
| 45 | |
| 46 | def file_prompt(self): |
| 47 | return prompt( |
| 48 | "File: ", |
| 49 | history=self.prompt_file_history, |
| 50 | completer=PathCompleter(), |
| 51 | auto_suggest=self.prompt_auto_suggest, |
| 52 | style=self.prompt_style, |
| 53 | ) |
| 54 | |
| 55 | def user_prompt(self, long=False): |
| 56 | if long: |
| 57 | return prompt( |
| 58 | "> ", |
| 59 | multiline=True, |
| 60 | history=self.prompt_history, |
| 61 | auto_suggest=self.prompt_auto_suggest, |
| 62 | style=self.prompt_style, |
| 63 | prompt_continuation="> ", |
| 64 | ) |
| 65 | else: |
| 66 | return prompt( |
| 67 | "You: ", |
| 68 | history=self.prompt_history, |
| 69 | auto_suggest=self.prompt_auto_suggest, |