diff options
author | 2023-03-07 05:33:37 +1100 | |
---|---|---|
committer | 2023-03-07 05:33:37 +1100 | |
commit | 8384223ac82670d39e1f40834dc4375f43c02a94 (patch) | |
tree | 1c2f43c825753e82f323b710bcbe226c0864de58 /3rdparty/linenoise/example.c | |
parent | e9ecdc9a6eb5b1cced5423cac2bde6fa8b20f5b7 (diff) |
Updated forked linenoise to latest upstream.
This removes the need to force it to build as C++, and adds proper UTF-8
support for Windows.
Since this is a fork of linenoise, there's no hope for getting
lua-linenoise to sync with it upstream. I made the bare minimum changes
to keep it working, but didn't add bindings for new functionality (e.g.
multi-line editing).
Diffstat (limited to '3rdparty/linenoise/example.c')
-rw-r--r-- | 3rdparty/linenoise/example.c | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/3rdparty/linenoise/example.c b/3rdparty/linenoise/example.c index f6ce462e6b1..29a95590b2f 100644 --- a/3rdparty/linenoise/example.c +++ b/3rdparty/linenoise/example.c @@ -5,50 +5,70 @@ #ifndef NO_COMPLETION void completion(const char *buf, linenoiseCompletions *lc, void *userdata) { + (void)userdata; if (buf[0] == 'h') { linenoiseAddCompletion(lc,"hello"); linenoiseAddCompletion(lc,"hello there"); } } + +char *hints(const char *buf, int *color, int *bold, void *userdata) { + (void)userdata; + if (!strcasecmp(buf,"hello")) { + *color = 35; + *bold = 0; + return " World"; + } + return NULL; +} #endif int main(int argc, char *argv[]) { + const char *prompt = "hello> "; char *line; -#ifdef HAVE_MULTILINE - /* Note: multiline support has not yet been integrated */ char *prgname = argv[0]; + const char *initial; /* Parse options, with --multiline we enable multi line editing. */ - while(argc > 1) { + while(argc > 1 && argv[1][0] == '-') { argc--; argv++; if (!strcmp(*argv,"--multiline")) { linenoiseSetMultiLine(1); printf("Multi-line mode enabled.\n"); + } else if (!strcmp(*argv,"--fancyprompt")) { + prompt = "\x1b[1;31m\xf0\xa0\x8a\x9d-\xc2\xb5hello>\x1b[0m "; + } else if (!strcmp(*argv,"--prompt") && argc > 1) { + argc--; + argv++; + prompt = *argv; } else { - fprintf(stderr, "Usage: %s [--multiline]\n", prgname); + fprintf(stderr, "Usage: %s [--multiline] [--fancyprompt] [--prompt text]\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); + linenoiseSetHintsCallback(hints, 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 */ + initial = (argc > 1) ? argv[1] : ""; + /* 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) { + while((line = linenoiseWithInitial(prompt, initial)) != NULL) { + initial = ""; /* Do something with the string. */ if (line[0] != '\0' && line[0] != '/') { printf("echo: '%s'\n", line); |