From 73966bc983024bc38256426f0610f378f5a78063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pazdiora?= Date: Sun, 9 Apr 2023 21:05:11 +0200 Subject: [PATCH] Change --multiline implementation to be toggled by EOF. --- examples/common.cpp | 44 ++++++++++++++++++++++++++---------------- examples/main/main.cpp | 7 ++++--- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/examples/common.cpp b/examples/common.cpp index ebaa97374..737b15d97 100644 --- a/examples/common.cpp +++ b/examples/common.cpp @@ -334,7 +334,7 @@ void gpt_print_usage(char * argv_0, const gpt_params & params) { fprintf(stderr, " run in interactive mode and poll user input upon seeing PROMPT (can be\n"); fprintf(stderr, " specified more than once for multiple prompts).\n"); fprintf(stderr, " --color colorise output to distinguish prompt and user input from generations\n"); - fprintf(stderr, " --multiline multiline mode (use Ctrl+D on Linux/Mac and Ctrl+Z then Return on Windows to send input)\n"); + fprintf(stderr, " --multiline multiline mode (use Ctrl+D on Linux/Mac and Ctrl+Z then Return on Windows to toggle multiline)\n"); fprintf(stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for <= 0)\n"); fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads); fprintf(stderr, " -p PROMPT, --prompt PROMPT\n"); @@ -455,45 +455,55 @@ void win32_utf8_encode(const std::wstring & wstr, std::string & str) { } #endif -bool get_input_text(std::string & input_text, bool escape_newline_mode) { +bool get_input_text(std::string & input_text, bool eof_toggled_multiline_mode) { bool another_line = true; + bool is_eof_multiline_toggled = false; do { std::string line; #if defined(_WIN32) + auto & stdcin = std::wcin; std::wstring wline; - if (!std::getline(std::wcin, wline)) { + if (!std::getline(stdcin, wline)) { // input stream is bad or EOF received - if (std::wcin.bad()) { + if (stdcin.bad()) { fprintf(stderr, "%s: error: input stream bad\n", __func__); return 1; } } - if (std::wcin.eof()) { - another_line = false; - std::wcin.clear(); - std::wcin.seekg(0, std::ios::beg); - } win32_utf8_encode(wline, line); #else - if (!std::getline(std::cin, line)) { + auto & stdcin = std::cin; + if (!std::getline(stdcin, line)) { // input stream is bad or EOF received - if (std::wcin.bad()) { + if (stdcin.bad()) { fprintf(stderr, "%s: error: input stream bad\n", __func__); return 1; } } - if (std::cin.eof()) { - another_line = false; - std::cin.clear(); - std::cin.seekg(0, std::ios::beg); - } #endif - if (escape_newline_mode) { + if (stdcin.eof()) { + stdcin.clear(); + stdcin.seekg(0, std::ios::beg); + if (!eof_toggled_multiline_mode) { + another_line = false; + } else { + is_eof_multiline_toggled = !is_eof_multiline_toggled; + if (is_eof_multiline_toggled) { + input_text += line; + continue; + } + } + } + if (!eof_toggled_multiline_mode) { if (line.empty() || line.back() != '\\') { another_line = false; } else { line.pop_back(); // Remove the continue character } + } else { + if (!is_eof_multiline_toggled) { + another_line = false; + } } input_text += line; if (another_line) { diff --git a/examples/main/main.cpp b/examples/main/main.cpp index 5da2326d4..0f8ef88cb 100644 --- a/examples/main/main.cpp +++ b/examples/main/main.cpp @@ -238,10 +238,11 @@ int main(int argc, char ** argv) { #endif ); if (params.multiline_mode) { + fprintf(stderr, " - Press Return to return control to LLaMa.\n" #if defined (_WIN32) - fprintf(stderr, " - [MULTILINE MODE] Press Ctrl+Z then Return (EOF) to return control to LLaMa.\n\n"); + " - [MULTILINE MODE] Press Ctrl+Z then Return (EOF) to toggle.\n\n"); #else - fprintf(stderr, " - [MULTILINE MODE] Press Ctrl+D (EOF) to return control to LLaMa.\n\n"); + " - [MULTILINE MODE] Press Ctrl+D (EOF) to toggle.\n\n"); #endif } else { @@ -456,7 +457,7 @@ int main(int argc, char ** argv) { printf("%s", buffer.c_str()); } - if (!get_input_text(buffer, !params.multiline_mode)) { + if (!get_input_text(buffer, params.multiline_mode)) { // input stream is bad return 1; }