Skip to content

Commit 4127d6a

Browse files
committed
Integrate linenoise
We get prompt history etc. Signed-off-by: Eric Curtin <[email protected]>
1 parent f11e4b6 commit 4127d6a

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

examples/run/linenoise.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com>
1313
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
14+
* Copyright (c) 2025, Eric Curtin <ericcurtin17 at gmail dot com>
1415
*
1516
* All rights reserved.
1617
*

examples/run/linenoise.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
* Guerrilla line editing library against the idea that a line editing lib
44
* needs to be 20,000 lines of C code.
55
*
6-
* See linenoise.c for more information.
6+
* See linenoise.cpp for more information.
77
*
88
* ------------------------------------------------------------------------
99
*
1010
* Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com>
1111
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
12+
* Copyright (c) 2025, Eric Curtin <ericcurtin17 at gmail dot com>
1213
*
1314
* All rights reserved.
1415
*

examples/run/run.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "common.h"
2727
#include "json.hpp"
28+
#include "linenoise.h"
2829
#include "llama-cpp.h"
2930

3031
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(_WIN32)
@@ -807,24 +808,44 @@ static int generate(LlamaData & llama_data, const std::string & prompt, std::str
807808
batch = llama_batch_get_one(&new_token_id, 1);
808809
}
809810

811+
printf("\033[0m");
810812
return 0;
811813
}
812814

813-
static int read_user_input(std::string & user) {
814-
std::getline(std::cin, user);
815+
static int read_user_input(std::string & user_input) {
816+
static const char * prompt_prefix = "> ";
817+
#ifdef WIN32
818+
printf(
819+
"\r%*s"
820+
"\r\033[0m%s",
821+
get_terminal_width(), " ", prompt_prefix);
822+
823+
std::getline(std::cin, user_input);
815824
if (std::cin.eof()) {
816825
printf("\n");
817826
return 1;
818827
}
828+
#else
829+
std::unique_ptr<char, decltype(&std::free)> line(const_cast<char *>(linenoise(prompt_prefix)), free);
830+
if (!line) {
831+
return 1;
832+
}
819833

820-
if (user == "/bye") {
834+
user_input = line.get();
835+
#endif
836+
837+
if (user_input == "/bye") {
821838
return 1;
822839
}
823840

824-
if (user.empty()) {
841+
if (user_input.empty()) {
825842
return 2;
826843
}
827844

845+
#ifndef WIN32
846+
linenoiseHistoryAdd(line.get());
847+
#endif
848+
828849
return 0; // Should have data in happy path
829850
}
830851

@@ -865,10 +886,6 @@ static int handle_user_input(std::string & user_input, const std::string & user)
865886
return 0; // No need for interactive input
866887
}
867888

868-
printf(
869-
"\r%*s"
870-
"\r\033[32m> \033[0m",
871-
get_terminal_width(), " ");
872889
return read_user_input(user_input); // Returns true if input ends the loop
873890
}
874891

0 commit comments

Comments
 (0)