]> git.lizzy.rs Git - linenoise.git/blobdiff - example.c
Big cleanups to linenoise
[linenoise.git] / example.c
index 960e8c5471f156a979f88e18c566b3d7334e82dc..cb51a0af8f9259eca7504261e63bb35627f3cc5c 100644 (file)
--- a/example.c
+++ b/example.c
@@ -2,18 +2,29 @@
 #include <stdlib.h>
 #include "linenoise.h"
 
+#ifndef NO_COMPLETION
+void completion(const char *buf, linenoiseCompletions *lc) {
+    if (buf[0] == 'h') {
+        linenoiseAddCompletion(lc,"hello");
+        linenoiseAddCompletion(lc,"hello there");
+    }
+}
+#endif
+
 int main(void) {
-    char buf[1024];
-    int retval;
+    char *line;
 
-    while(1) {
-        retval = linenoise(buf,1024,"hello> ");
-        if (retval > 0) {
-            printf("echo: '%s'\n", buf);
-            linenoiseHistoryAdd(buf);
-        } else if (retval == -1) {
-            exit(1);
+#ifndef NO_COMPLETION
+    linenoiseSetCompletionCallback(completion);
+#endif
+    linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
+    while((line = linenoise("hello> ")) != NULL) {
+        if (line[0] != '\0') {
+            printf("echo: '%s'\n", line);
+            linenoiseHistoryAdd(line);
+            linenoiseHistorySave("history.txt"); /* Save every new entry */
         }
+        free(line);
     }
     return 0;
 }