Skip to content

Distribute and run LLMs with a single file.

Have CTRL-J be used for multi-line input

Justine Tunneyca7f4cedf78a
diff --git a/llamafile/bestline.c b/llamafile/bestline.c
index 2ece22ba..a028c760 100644
--- a/llamafile/bestline.c
+++ b/llamafile/bestline.c
@@ -64,6 +64,7 @@
│ CTRL-P PREVIOUS HISTORY │
│ CTRL-R SEARCH HISTORY │
│ CTRL-G CANCEL SEARCH │
+│ CTRL-J INSERT NEWLINE │
│ ALT-< BEGINNING OF HISTORY │
│ ALT-> END OF HISTORY │
│ ALT-F FORWARD WORD │
@@ -3101,6 +3102,7 @@ static ssize_t bestlineEdit(int stdin_fd, int stdout_fd, const char *prompt, con
char **obuf) {
ssize_t rc;
char seq[16];
+ const char *promptnotnull, *promptlastnl;
size_t nread;
int pastemode;
struct rune rune;
@@ -3113,11 +3115,13 @@ static ssize_t bestlineEdit(int stdin_fd, int stdout_fd, const char *prompt, con
l.buf[0] = 0;
l.ifd = stdin_fd;
l.ofd = stdout_fd;
- l.prompt = prompt ? prompt : "";
+ promptnotnull = prompt ? prompt : "";
+ promptlastnl = strrchr(promptnotnull, '\n');
+ l.prompt = promptlastnl ? promptlastnl + 1 : promptnotnull;
l.ws = GetTerminalSize(l.ws, l.ifd, l.ofd);
abInit(&l.full);
bestlineHistoryAdd("");
- bestlineWriteStr(l.ofd, l.prompt);
+ bestlineWriteStr(l.ofd, promptnotnull);
init = init ? init : "";
bestlineEditInsert(&l, init, strlen(init));
while (1) {
@@ -3204,11 +3208,20 @@ static ssize_t bestlineEdit(int stdin_fd, int stdout_fd, const char *prompt, con
return -1;
}
break;
- case '\r':
- if (nread >= 2 && seq[1] == '\n')
- memmove(seq, seq + 1, --nread);
- // fallthrough
- case '\n': {
+ case '\n':
+ l.final = 1;
+ bestlineEditEnd(&l);
+ bestlineRefreshLineForce(&l);
+ l.final = 0;
+ abAppend(&l.full, l.buf, l.len);
+ l.prompt = "... ";
+ abAppends(&l.full, "\n");
+ l.len = 0;
+ l.pos = 0;
+ bestlineWriteStr(stdout_fd, "\r\n");
+ bestlineRefreshLineForce(&l);
+ break;
+ case '\r': {
char is_finished = 1;
char needs_strip = 0;
if (historylen) {
@@ -3552,8 +3565,7 @@ char *bestlineRaw(const char *prompt, int infd, int outfd) {
* capabilites.
*/
char *bestlineInit(const char *prompt, const char *init) {
- if (prompt && *prompt &&
- (strchr(prompt, '\n') || strchr(prompt, '\t') || strchr(prompt + 1, '\r'))) {
+ if (prompt && *prompt && (strchr(prompt, '\t') || strchr(prompt + 1, '\r'))) {
errno = EINVAL;
return 0;
}