Skip to content

Commit fbbda7e

Browse files
committed
Change --multiline implementation to be toggled by EOF.
1 parent f9f1b1c commit fbbda7e

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

examples/common.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

examples/main/main.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,11 @@ int main(int argc, char ** argv) {
237237
#endif
238238
);
239239
if (params.multiline_mode) {
240+
fprintf(stderr, " - Press Return to return control to LLaMa.\n"
240241
#if defined (_WIN32)
241-
fprintf(stderr, " - [MULTILINE MODE] Press Ctrl+Z then Return (EOF) to return control to LLaMa.\n\n");
242+
" - [MULTILINE MODE] Press Ctrl+Z then Return (EOF) to toggle.\n\n");
242243
#else
243-
fprintf(stderr, " - [MULTILINE MODE] Press Ctrl+D (EOF) to return control to LLaMa.\n\n");
244+
" - [MULTILINE MODE] Press Ctrl+D (EOF) to toggle.\n\n");
244245
#endif
245246
}
246247
else {
@@ -455,7 +456,7 @@ int main(int argc, char ** argv) {
455456
printf("%s", buffer.c_str());
456457
}
457458

458-
if (!get_input_text(buffer, !params.multiline_mode)) {
459+
if (!get_input_text(buffer, params.multiline_mode)) {
459460
// input stream is bad
460461
return 1;
461462
}

0 commit comments

Comments
 (0)