@@ -334,7 +334,7 @@ void gpt_print_usage(char * argv_0, const gpt_params & params) {
334334 fprintf (stderr, " run in interactive mode and poll user input upon seeing PROMPT (can be\n " );
335335 fprintf (stderr, " specified more than once for multiple prompts).\n " );
336336 fprintf (stderr, " --color colorise output to distinguish prompt and user input from generations\n " );
337- fprintf (stderr, " --multiline multiline mode (use Ctrl+D on Linux/Mac and Ctrl+Z then Return on Windows to send input )\n " );
337+ fprintf (stderr, " --multiline multiline mode (use Ctrl+D on Linux/Mac and Ctrl+Z then Return on Windows to toggle multiline )\n " );
338338 fprintf (stderr, " -s SEED, --seed SEED RNG seed (default: -1, use random seed for <= 0)\n " );
339339 fprintf (stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n " , params.n_threads );
340340 fprintf (stderr, " -p PROMPT, --prompt PROMPT\n " );
@@ -452,45 +452,55 @@ void win32_utf8_encode(const std::wstring & wstr, std::string & str) {
452452}
453453#endif
454454
455- bool get_input_text (std::string & input_text, bool escape_newline_mode ) {
455+ bool get_input_text (std::string & input_text, bool eof_toggled_multiline_mode ) {
456456 bool another_line = true ;
457+ bool is_eof_multiline_toggled = false ;
457458 do {
458459 std::string line;
459460#if defined(_WIN32)
461+ auto & stdcin = std::wcin;
460462 std::wstring wline;
461- if (!std::getline (std::wcin , wline)) {
463+ if (!std::getline (stdcin , wline)) {
462464 // input stream is bad or EOF received
463- if (std::wcin .bad ()) {
465+ if (stdcin .bad ()) {
464466 fprintf (stderr, " %s: error: input stream bad\n " , __func__);
465467 return 1 ;
466468 }
467469 }
468- if (std::wcin.eof ()) {
469- another_line = false ;
470- std::wcin.clear ();
471- std::wcin.seekg (0 , std::ios::beg);
472- }
473470 win32_utf8_encode (wline, line);
474471#else
475- if (!std::getline (std::cin, line)) {
472+ auto & stdcin = std::cin;
473+ if (!std::getline (stdcin, line)) {
476474 // input stream is bad or EOF received
477- if (std::wcin .bad ()) {
475+ if (stdcin .bad ()) {
478476 fprintf (stderr, " %s: error: input stream bad\n " , __func__);
479477 return 1 ;
480478 }
481479 }
482- if (std::cin.eof ()) {
483- another_line = false ;
484- std::cin.clear ();
485- std::cin.seekg (0 , std::ios::beg);
486- }
487480#endif
488- if (escape_newline_mode) {
481+ if (stdcin.eof ()) {
482+ stdcin.clear ();
483+ stdcin.seekg (0 , std::ios::beg);
484+ if (!eof_toggled_multiline_mode) {
485+ another_line = false ;
486+ } else {
487+ is_eof_multiline_toggled = !is_eof_multiline_toggled;
488+ if (is_eof_multiline_toggled) {
489+ input_text += line;
490+ continue ;
491+ }
492+ }
493+ }
494+ if (!eof_toggled_multiline_mode) {
489495 if (line.empty () || line.back () != ' \\ ' ) {
490496 another_line = false ;
491497 } else {
492498 line.pop_back (); // Remove the continue character
493499 }
500+ } else {
501+ if (!is_eof_multiline_toggled) {
502+ another_line = false ;
503+ }
494504 }
495505 input_text += line;
496506 if (another_line) {
0 commit comments