2
0

example.c 701 B

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