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