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