7 void completion(const char *buf, linenoiseCompletions *lc, void *userdata) {
9 linenoiseAddCompletion(lc,"hello");
10 linenoiseAddCompletion(lc,"hello there");
14 char *hints(const char *buf, int *color, int *bold, void *userdata) {
15 if (!strcasecmp(buf,"hello")) {
24 int main(int argc, char *argv[]) {
25 const char *prompt = "hello> ";
27 char *prgname = argv[0];
29 /* Parse options, with --multiline we enable multi line editing. */
33 if (!strcmp(*argv,"--multiline")) {
34 linenoiseSetMultiLine(1);
35 printf("Multi-line mode enabled.\n");
36 } else if (!strcmp(*argv,"--fancyprompt")) {
37 prompt = "\x1b[1;31m\xf0\xa0\x8a\x9d-\xc2\xb5hello>\x1b[0m ";
38 } else if (!strcmp(*argv,"--prompt") && argc > 1) {
43 fprintf(stderr, "Usage: %s [--multiline] [--fancyprompt] [--prompt text]\n", prgname);
49 /* Set the completion callback. This will be called every time the
50 * user uses the <tab> key. */
51 linenoiseSetCompletionCallback(completion, NULL);
52 linenoiseSetHintsCallback(hints, NULL);
55 /* Load history from file. The history file is just a plain text file
56 * where entries are separated by newlines. */
57 linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
59 /* Now this is the main loop of the typical linenoise-based application.
60 * The call to linenoise() will block as long as the user types something
63 * The typed string is returned as a malloc() allocated string by
64 * linenoise, so the user needs to free() it. */
65 while((line = linenoise(prompt)) != NULL) {
66 /* Do something with the string. */
67 if (line[0] != '\0' && line[0] != '/') {
68 printf("echo: '%s'\n", line);
69 linenoiseHistoryAdd(line); /* Add to the history. */
70 linenoiseHistorySave("history.txt"); /* Save the history on disk. */
71 } else if (!strncmp(line,"/historylen",11)) {
72 /* The "/historylen" command will change the history len. */
73 int len = atoi(line+11);
74 linenoiseHistorySetMaxLen(len);
75 } else if (line[0] == '/') {
76 printf("Unreconized command: %s\n", line);