| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include "linenoise.h" |
| 4 | |
| 5 | |
| 6 | void completion(const char *buf, linenoiseCompletions *lc) { |
| 7 | if (buf[0] == 'h') { |
| 8 | linenoiseAddCompletion(lc,"hello"); |
| 9 | linenoiseAddCompletion(lc,"hello there"); |
| 10 | } |
| 11 | } |
| 12 | |
| 13 | int main(void) { |
| 14 | char *line; |
| 15 | |
| 16 | linenoiseSetCompletionCallback(completion); |
| 17 | linenoiseHistoryLoad("history.txt"); /* Load the history at startup */ |
| 18 | while((line = linenoise("hello> ")) != NULL) { |
| 19 | if (line[0] != '\0') { |
| 20 | printf("echo: '%s'\n", line); |
| 21 | linenoiseHistoryAdd(line); |
| 22 | linenoiseHistorySave("history.txt"); /* Save every new entry */ |
| 23 | } |
| 24 | free(line); |
| 25 | } |
| 26 | return 0; |
| 27 | } |