Skip to content

Commit e2d118a

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

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,7 @@ jobs:
796796
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}
797797
run: |
798798
Copy-Item LICENSE .\build\bin\Release\llama.cpp.txt
799+
Copy-Item .\examples\run\linenoise.cpp\LICENSE .\build\bin\Release\linenoise.cpp.txt
799800
7z a llama-${{ steps.tag.outputs.name }}-bin-win-${{ matrix.build }}.zip .\build\bin\Release\*
800801
801802
- name: Upload artifacts

examples/run/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set(TARGET llama-run)
2-
add_executable(${TARGET} run.cpp linenoise.cpp)
2+
add_executable(${TARGET} run.cpp linenoise.cpp/linenoise.cpp)
33
install(TARGETS ${TARGET} RUNTIME)
44
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT})
55
target_compile_features(${TARGET} PRIVATE cxx_std_17)

examples/run/linenoise.cpp/LICENSE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
2+
Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
3+
Copyright (c) 2025, Eric Curtin <ericcurtin17 at gmail dot com>
4+
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
13+
* Redistributions in binary form must reproduce the above copyright notice,
14+
this list of conditions and the following disclaimer in the documentation
15+
and/or other materials provided with the distribution.
16+
17+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

examples/run/linenoise.cpp renamed to examples/run/linenoise.cpp/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 renamed to examples/run/linenoise.cpp/linenoise.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* linenoise.h -- VERSION 1.0
22
*
33
* Guerrilla line editing library against the idea that a line editing lib
4-
* needs to be 20,000 lines of C code.
4+
* 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.cpp/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)