summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/linenoise/example.c
diff options
context:
space:
mode:
author cracyc <cracyc@users.noreply.github.com>2017-05-11 17:28:14 -0500
committer cracyc <cracyc@users.noreply.github.com>2017-05-13 16:57:47 -0500
commit4ebc18aeec6929bd1396daef1289df9821422834 (patch)
tree759538020e787b21eb45113acb485c628185871b /3rdparty/linenoise/example.c
parent3553a7bf422d50d331c2cf59c9405f190be41f28 (diff)
linenoise: replace linenoise-ng with a different port that is simpler and uses a different UTF8 parser [Carl]
plugins/console: better completions [Carl]
Diffstat (limited to '3rdparty/linenoise/example.c')
-rw-r--r--3rdparty/linenoise/example.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/3rdparty/linenoise/example.c b/3rdparty/linenoise/example.c
new file mode 100644
index 00000000000..f6ce462e6b1
--- /dev/null
+++ b/3rdparty/linenoise/example.c
@@ -0,0 +1,67 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "linenoise.h"
+
+#ifndef NO_COMPLETION
+void completion(const char *buf, linenoiseCompletions *lc, void *userdata) {
+ if (buf[0] == 'h') {
+ linenoiseAddCompletion(lc,"hello");
+ linenoiseAddCompletion(lc,"hello there");
+ }
+}
+#endif
+
+int main(int argc, char *argv[]) {
+ char *line;
+#ifdef HAVE_MULTILINE
+ /* Note: multiline support has not yet been integrated */
+ 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 {
+ fprintf(stderr, "Usage: %s [--multiline]\n", prgname);
+ exit(1);
+ }
+ }
+#endif
+
+#ifndef NO_COMPLETION
+ /* Set the completion callback. This will be called every time the
+ * user uses the <tab> key. */
+ linenoiseSetCompletionCallback(completion, NULL);
+#endif
+
+ /* 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;
+}