REPL multiline documentation and improvements (#401)

This commit is contained in:
Farkhod Sadykov
2023-12-19 21:59:01 +01:00
committed by GitHub
parent 3eac96b5d7
commit b0d43463af
2 changed files with 15 additions and 6 deletions
+11
View File
@@ -264,6 +264,17 @@ import requests
response = requests.get('https://localhost:443')
print(response.text)
```
To provide multiline prompt use triple quotes `"""`:
```text
sgpt --repl temp
Entering REPL mode, press Ctrl+C to exit.
>>> """
... Explain following code:
... import random
... print(random.randint(1, 10))
... """
It is a Python script that uses the random module to generate and print a random integer.
```
It is also possible to pickup conversations from chat sessions (which were created using `--chat` option) and continue them in REPL mode.
### Roles
+4 -6
View File
@@ -14,13 +14,11 @@ class ReplHandler(ChatHandler):
def __init__(self, chat_id: str, role: SystemRole) -> None:
super().__init__(chat_id, role)
def get_multiline_input(self) -> str:
@classmethod
def _get_multiline_input(cls) -> str:
multiline_input = ""
while True:
user_input = typer.prompt("...", prompt_suffix="")
while (user_input := typer.prompt("...", prompt_suffix="")) != '"""':
multiline_input += user_input + "\n"
if user_input == '"""':
break
return multiline_input
def handle(self, prompt: str, **kwargs: Any) -> None: # type: ignore
@@ -44,7 +42,7 @@ class ReplHandler(ChatHandler):
# Infinite loop until user exits with Ctrl+C.
prompt = typer.prompt(">>>", prompt_suffix=" ")
if prompt == '"""':
prompt = self.get_multiline_input()
prompt = self._get_multiline_input()
if prompt == "exit()":
# This is also useful during tests.
raise typer.Exit()