]> git.lizzy.rs Git - linenoise.git/blob - example.c
Use CMake
[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     (void)userdata;
9     if (buf[0] == 'h') {
10         linenoiseAddCompletion(lc,"hello");
11         linenoiseAddCompletion(lc,"hello there");
12     }
13 }
14
15 char *hints(const char *buf, int *color, int *bold, void *userdata) {
16     (void)userdata;
17     if (!strcasecmp(buf,"hello")) {
18         *color = 35;
19         *bold = 0;
20         return " World";
21     }
22     return NULL;
23 }
24 #endif
25
26 int main(int argc, char *argv[]) {
27     const char *prompt = "hello> ";
28     char *line;
29     char *prgname = argv[0];
30
31     /* Parse options, with --multiline we enable multi line editing. */
32     while(argc > 1) {
33         argc--;
34         argv++;
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) {
41             argc--;
42             argv++;
43             prompt = *argv;
44         } else {
45             fprintf(stderr, "Usage: %s [--multiline] [--fancyprompt] [--prompt text]\n", prgname);
46             exit(1);
47         }
48     }
49
50 #ifndef NO_COMPLETION
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);
55 #endif
56
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 */
60
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
63      * and presses enter.
64      *
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);
79         }
80         free(line);
81     }
82     return 0;
83 }