Skip to content

Commit

Permalink
REPL multiline documentation and improvements (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheR1D authored Dec 19, 2023
1 parent 3eac96b commit b0d4346
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions sgpt/handlers/repl_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,7 +42,7 @@ def handle(self, prompt: str, **kwargs: Any) -> None: # type: ignore
# 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()
Expand Down

0 comments on commit b0d4346

Please sign in to comment.