From 71491708b3b2f33dddc3d81469eebfba69bf19fa Mon Sep 17 00:00:00 2001 From: devbisme Date: Tue, 4 Apr 2023 11:36:34 -0400 Subject: [PATCH] feat: Added "--prompt-end" argument to specify a string that ends a prompt in interactive mode. Useful when pasting code with multiple lines and blank lines. --- chatblade/cli.py | 29 +++++++++++++++++++++++++++-- chatblade/parser.py | 7 +++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/chatblade/cli.py b/chatblade/cli.py index c8da5f4..929d202 100644 --- a/chatblade/cli.py +++ b/chatblade/cli.py @@ -28,15 +28,40 @@ def fetch_and_cache(messages, params): def start_repl(messages, params): + + # Convert newline string from shell into an actual newline character. + prompt_end = params.prompt_end.replace("\\n", "\n") + "\n" + + query_lines = [] # Init query buffer. + while True: try: - query = Prompt.ask("[yellow]query (type 'quit' to exit): [/yellow]") + if not query_lines: + # Explanatory prompt when starting query entry. + prompt_end_str = prompt_end.replace("\n", "\\n") + query_lines.append(Prompt.ask(f"[yellow]query ({prompt_end_str} to end query; 'quit' to exit)[/yellow]")) + else: + # No prompt string after first line. + query_lines.append(Prompt.ask("")) except (EOFError, KeyboardInterrupt): rich.print("\n") exit() - if query.lower() == "quit": + if query_lines[0].lower() == "quit": + # Exit if query is a single line that's just 'quit'. exit() + # Join query lines into a complete query with terminating newline. + query = "\n".join(query_lines) + "\n" + + if not query.endswith(prompt_end): + # Continue gathering lines until the prompt end string is seen. + continue + + query_lines = [] # Clear query buffer. + + if query == "": + continue # Don't ask GPT if there's no query. + if not messages: init_msgs = ( [storage.load_prompt_file(params.prompt_file)] diff --git a/chatblade/parser.py b/chatblade/parser.py index 2a8b59c..393f5ef 100644 --- a/chatblade/parser.py +++ b/chatblade/parser.py @@ -111,6 +111,13 @@ def parse(args): type=str, help="prompt name - will load the prompt with that name at ~/.config/chatblade/name or a path to a file", ) + parser.add_argument( + "--prompt-end", + metavar="str", + type=str, + help=r"string that ends an interactive prompt and sends query to GPT. (Use \\n to indicate newline.)", + default="", # A newline is appended to this later on. + ) display_opts = parser.add_argument_group("result formatting options") display_opts.add_argument(