199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdio.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stdlib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <string.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "linenoise.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid completion(const char *buf, linenoiseCompletions *lc) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (buf[0] == 'h') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseAddCompletion(lc,"hello");
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseAddCompletion(lc,"hello there");
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint main(int argc, char **argv) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *line;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char *prgname = argv[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Parse options, with --multiline we enable multi line editing. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while(argc > 1) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome argc--;
199767f8919635c4928607450d9e0abb932109ceToomas Soome argv++;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strcmp(*argv,"--multiline")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseSetMultiLine(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Multi-line mode enabled.\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (!strcmp(*argv,"--keycodes")) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoisePrintKeyCodes();
199767f8919635c4928607450d9e0abb932109ceToomas Soome exit(0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else {
199767f8919635c4928607450d9e0abb932109ceToomas Soome fprintf(stderr, "Usage: %s [--multiline] [--keycodes]\n", prgname);
199767f8919635c4928607450d9e0abb932109ceToomas Soome exit(1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set the completion callback. This will be called every time the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * user uses the <tab> key. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseSetCompletionCallback(completion);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Load history from file. The history file is just a plain text file
199767f8919635c4928607450d9e0abb932109ceToomas Soome * where entries are separated by newlines. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Now this is the main loop of the typical linenoise-based application.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The call to linenoise() will block as long as the user types something
199767f8919635c4928607450d9e0abb932109ceToomas Soome * and presses enter.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The typed string is returned as a malloc() allocated string by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * linenoise, so the user needs to free() it. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome while((line = linenoise("hello> ")) != NULL) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Do something with the string. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (line[0] != '\0' && line[0] != '/') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("echo: '%s'\n", line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseHistoryAdd(line); /* Add to the history. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseHistorySave("history.txt"); /* Save the history on disk. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (!strncmp(line,"/historylen",11)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The "/historylen" command will change the history len. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome int len = atoi(line+11);
199767f8919635c4928607450d9e0abb932109ceToomas Soome linenoiseHistorySetMaxLen(len);
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (line[0] == '/') {
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("Unreconized command: %s\n", line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome free(line);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome}