]> git.lizzy.rs Git - linenoise.git/blob - example.c
Guard inclusion of stringbuf.h
[linenoise.git] / example.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "linenoise.h"
5
6 #ifndef NO_COMPLETION
7 void completion(const char *buf, linenoiseCompletions *lc, void *userdata) {
8     if (buf[0] == 'h') {
9         linenoiseAddCompletion(lc,"hello");
10         linenoiseAddCompletion(lc,"hello there");
11     }
12 }
13
14 char *hints(const char *buf, int *color, int *bold, void *userdata) {
15     if (!strcasecmp(buf,"hello")) {
16         *color = 35;
17         *bold = 0;
18         return " World";
19     }
20     return NULL;
21 }
22 #endif
23
24 int main(int argc, char *argv[]) {
25     const char *prompt = "hello> ";
26     char *line;
27     char *prgname = argv[0];
28
29     /* Parse options, with --multiline we enable multi line editing. */
30     while(argc > 1) {
31         argc--;
32         argv++;
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) {
39             argc--;
40             argv++;
41             prompt = *argv;
42         } else {
43             fprintf(stderr, "Usage: %s [--multiline] [--fancyprompt] [--prompt text]\n", prgname);
44             exit(1);
45         }
46     }
47
48 #ifndef NO_COMPLETION
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);
53 #endif
54
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 */
58
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
61      * and presses enter.
62      *
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);
77         }
78         free(line);
79     }
80     return 0;
81 }