From 72552708e65a3970ed82dd08d401dfcdef53749a Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Wed, 31 Jul 2019 13:25:58 +0200 Subject: [PATCH] add linenoise (#74) --- examples/linenoise/CMakeLists.txt | 22 +++++++++++ examples/linenoise/main.cpp | 64 +++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 examples/linenoise/CMakeLists.txt create mode 100644 examples/linenoise/main.cpp diff --git a/examples/linenoise/CMakeLists.txt b/examples/linenoise/CMakeLists.txt new file mode 100644 index 0000000..2d52917 --- /dev/null +++ b/examples/linenoise/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +# ---- Dependencies ---- + +include(../../cmake/CPM.cmake) + +CPMAddPackage( + NAME linenoise + GIT_TAG 1.0 + GITHUB_REPOSITORY antirez/linenoise +) + +if(linenoise_ADDED) + add_library(linenoise ${linenoise_SOURCE_DIR}/linenoise.c) + target_include_directories(linenoise PUBLIC ${linenoise_SOURCE_DIR}) +endif() + +# ---- Executable ---- + +add_executable(CPMlinenoiseExample "main.cpp") +set_target_properties(CPMlinenoiseExample PROPERTIES CXX_STANDARD 17) +target_link_libraries(CPMlinenoiseExample linenoise) diff --git a/examples/linenoise/main.cpp b/examples/linenoise/main.cpp new file mode 100644 index 0000000..a2f0936 --- /dev/null +++ b/examples/linenoise/main.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include "linenoise.h" + + +void completion(const char *buf, linenoiseCompletions *lc) { + if (buf[0] == 'h') { + linenoiseAddCompletion(lc,"hello"); + linenoiseAddCompletion(lc,"hello there"); + } +} + +int main(int argc, char **argv) { + char *line; + char *prgname = argv[0]; + + /* Parse options, with --multiline we enable multi line editing. */ + while(argc > 1) { + argc--; + argv++; + if (!strcmp(*argv,"--multiline")) { + linenoiseSetMultiLine(1); + printf("Multi-line mode enabled.\n"); + } else if (!strcmp(*argv,"--keycodes")) { + linenoisePrintKeyCodes(); + exit(0); + } else { + fprintf(stderr, "Usage: %s [--multiline] [--keycodes]\n", prgname); + exit(1); + } + } + + /* Set the completion callback. This will be called every time the + * user uses the key. */ + linenoiseSetCompletionCallback(completion); + + /* Load history from file. The history file is just a plain text file + * where entries are separated by newlines. */ + linenoiseHistoryLoad("history.txt"); /* Load the history at startup */ + + /* Now this is the main loop of the typical linenoise-based application. + * The call to linenoise() will block as long as the user types something + * and presses enter. + * + * The typed string is returned as a malloc() allocated string by + * linenoise, so the user needs to free() it. */ + while((line = linenoise("hello> ")) != NULL) { + /* Do something with the string. */ + if (line[0] != '\0' && line[0] != '/') { + printf("echo: '%s'\n", line); + linenoiseHistoryAdd(line); /* Add to the history. */ + linenoiseHistorySave("history.txt"); /* Save the history on disk. */ + } else if (!strncmp(line,"/historylen",11)) { + /* The "/historylen" command will change the history len. */ + int len = atoi(line+11); + linenoiseHistorySetMaxLen(len); + } else if (line[0] == '/') { + printf("Unreconized command: %s\n", line); + } + free(line); + } + return 0; +}