Skip to content

Commit b371ebf

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

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

examples/run/run.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "common.h"
2727
#include "json.hpp"
2828
#include "llama-cpp.h"
29+
#include "linenoise.h"
2930

3031
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(_WIN32)
3132
[[noreturn]] static void sigint_handler(int) {
@@ -807,21 +808,22 @@ 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+
std::getline(std::cin, user_input);
815817
if (std::cin.eof()) {
816818
printf("\n");
817819
return 1;
818820
}
819821

820-
if (user == "/bye") {
822+
if (user_input == "/bye") {
821823
return 1;
822824
}
823825

824-
if (user.empty()) {
826+
if (user_input.empty()) {
825827
return 2;
826828
}
827829

@@ -858,18 +860,47 @@ static int apply_chat_template_with_error_handling(LlamaData & llama_data, const
858860
return 0;
859861
}
860862

863+
struct c_str {
864+
const char* data = nullptr;
865+
866+
~c_str() {
867+
free(const_cast<char*>(data));
868+
}
869+
};
870+
861871
// Helper function to handle user input
862872
static int handle_user_input(std::string & user_input, const std::string & user) {
863873
if (!user.empty()) {
864874
user_input = user;
865875
return 0; // No need for interactive input
866876
}
867877

878+
static const char* prompt_prefix = "> ";
879+
#ifdef WIN32
868880
printf(
869881
"\r%*s"
870-
"\r\033[32m> \033[0m",
871-
get_terminal_width(), " ");
882+
"\r\033[0m%s",
883+
get_terminal_width(), " ", prompt_prefix);
872884
return read_user_input(user_input); // Returns true if input ends the loop
885+
#else
886+
c_str line;
887+
line.data = linenoise(prompt_prefix);
888+
if (!line.data) {
889+
return 1;
890+
}
891+
892+
user_input = line.data;
893+
if (user_input == "/bye") {
894+
return 1;
895+
}
896+
897+
if (user_input.empty()) {
898+
return 2;
899+
}
900+
901+
linenoiseHistoryAdd(line.data);
902+
return 0;
903+
#endif
873904
}
874905

875906
static bool is_stdin_a_terminal() {
@@ -961,13 +992,7 @@ static std::string read_pipe_data() {
961992
}
962993

963994
static void ctrl_c_handling() {
964-
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
965-
struct sigaction sigint_action;
966-
sigint_action.sa_handler = sigint_handler;
967-
sigemptyset(&sigint_action.sa_mask);
968-
sigint_action.sa_flags = 0;
969-
sigaction(SIGINT, &sigint_action, NULL);
970-
#elif defined(_WIN32)
995+
#if defined(_WIN32)
971996
auto console_ctrl_handler = +[](DWORD ctrl_type) -> BOOL {
972997
return (ctrl_type == CTRL_C_EVENT) ? (sigint_handler(SIGINT), true) : false;
973998
};

0 commit comments

Comments
 (0)