Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 12 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ cpp-linenoise

Multi-platform (Unix, Windows) C++ header-only linenoise-based readline library.

This library gathered code from following excellent libraries, clean it up, and put it into a C++ header file for convenience.
This version of cpp-linenoise is derived from https://github.com/yhirose/cpp-linenoise,
which is in turn assembled from the following libraries:

* `linenoise.h` and `linenoise.c` ([antirez/linenoise](https://github.com/antirez/linenoise))
* `ANSI.c` ([adoxa/ansicon](https://github.com/adoxa/ansicon))
Expand All @@ -21,27 +22,29 @@ Usage

const auto path = "history.txt";

linenoise::linenoiseState l("hello> ");

// Setup completion words every time when a user types
linenoise::SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) {
l.SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) {
if (editBuffer[0] == 'h') {
completions.push_back("hello");
completions.push_back("hello there");
}
});

// Enable the multi-line mode
linenoise::SetMultiLine(true);
l.EnableMultiLine();

// Set max length of the history
linenoise::SetHistoryMaxLen(4);
l.SetHistoryMaxLen(4);

// Load history
linenoise::LoadHistory(path);
l.LoadHistory(path);

while (true) {
// Read line
std::string line;
auto quit = linenoise::Readline("hello> ", line);
auto quit = l.Readline(line);

if (quit) {
break;
Expand All @@ -50,44 +53,17 @@ while (true) {
cout << "echo: '" << line << "'" << endl;

// Add text to history
linenoise::AddHistory(line.c_str());
l.AddHistory(line.c_str());
}

// Save history
linenoise::SaveHistory(path);
l.SaveHistory(path);
```

API
---

```c++
namespace linenoise;

std::string Readline(const char* prompt);

void SetMultiLine(bool multiLineMode);

typedef std::function<void (const char* editBuffer, std::vector<std::string>& completions)> CompletionCallback;

void SetCompletionCallback(CompletionCallback fn);

bool SetHistoryMaxLen(size_t len);

bool LoadHistory(const char* path);

bool SaveHistory(const char* path);

bool AddHistory(const char* line);

const std::vector<std::string>& GetHistory();
```

Tested compilers
----------------

* Visual Studio 2015
* Clang 3.5
* GCC 6.3.1
The public methods on the linenoiseState class are considered the public API.

License
-------
Expand Down
25 changes: 14 additions & 11 deletions example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ int main(int argc, const char** argv)
{
const auto path = "history.txt";

#ifdef _WIN32
const char *prompt = "hello> ";
#else
const char *prompt = "\033[32mこんにちは\x1b[0m> ";
#endif
linenoise::linenoiseState l(prompt);

// Enable the multi-line mode
linenoise::SetMultiLine(true);
l.EnableMultiLine(true);

// Set max length of the history
linenoise::SetHistoryMaxLen(4);
l.SetHistoryMaxLen(4);

// Setup completion words every time when a user types
linenoise::SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) {
l.SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) {
if (editBuffer[0] == 'h') {
#ifdef _WIN32
completions.push_back("hello こんにちは");
Expand All @@ -27,15 +34,11 @@ int main(int argc, const char** argv)
});

// Load history
linenoise::LoadHistory(path);
l.LoadHistory(path);

while (true) {
std::string line;
#ifdef _WIN32
auto quit = linenoise::Readline("hello> ", line);
#else
auto quit = linenoise::Readline("\033[32mこんにちは\x1b[0m> ", line);
#endif
auto quit = l.Readline(line);

if (quit) {
break;
Expand All @@ -44,10 +47,10 @@ int main(int argc, const char** argv)
cout << "echo: '" << line << "'" << endl;

// Add line to history
linenoise::AddHistory(line.c_str());
l.AddHistory(line.c_str());

// Save history
linenoise::SaveHistory(path);
l.SaveHistory(path);
}

return 0;
Expand Down
Loading