Skip to content

Commit 0c5759e

Browse files
danielzgtgjwcolin
authored andcommitted
main : Fix Ctrl+D/newline handling (ggml-org#12951)
This restores the behavior from ggml-org#491. This does not affect Ctrl+D's ability to terminate --multiline-input lines (ggml-org#1040). This also actually implements ggml-org#587: "If the user wants the text to end in a newline, this should be accomplished by explicitly adding a newline by using \ followed by return, then returning control by pressing return again." Fixes ggml-org#12949
1 parent 51619b0 commit 0c5759e

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

examples/main/main.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -865,9 +865,22 @@ int main(int argc, char ** argv) {
865865
console::set_display(console::reset);
866866
display = true;
867867

868-
// Add tokens to embd only if the input buffer is non-empty
869-
// Entering a empty line lets the user pass control back
870-
if (buffer.length() > 1) {
868+
if (buffer.empty()) { // Ctrl+D on empty line exits
869+
LOG("EOF by user\n");
870+
break;
871+
}
872+
873+
if (buffer.back() == '\n') {
874+
// Implement #587:
875+
// If the user wants the text to end in a newline,
876+
// this should be accomplished by explicitly adding a newline by using \ followed by return,
877+
// then returning control by pressing return again.
878+
buffer.pop_back();
879+
}
880+
881+
if (buffer.empty()) { // Enter key on empty line lets the user pass control back
882+
LOG_DBG("empty line, passing control back\n");
883+
} else { // Add tokens to embd only if the input buffer is non-empty
871884
// append input suffix if any
872885
if (!params.input_suffix.empty() && !params.conversation_mode) {
873886
LOG_DBG("appending input suffix: '%s'\n", params.input_suffix.c_str());
@@ -915,8 +928,6 @@ int main(int argc, char ** argv) {
915928

916929
n_remain -= line_inp.size();
917930
LOG_DBG("n_remain: %d\n", n_remain);
918-
} else {
919-
LOG_DBG("empty line, passing control back\n");
920931
}
921932

922933
input_echo = false; // do not echo this again

0 commit comments

Comments
 (0)