Linenoise with rax.
This commit is contained in:
parent
7d93262c81
commit
2d3c17c152
|
|
@ -47,4 +47,4 @@ linenoise.o: linenoise.c linenoise.h
|
||||||
cc -c linenoise.c -o linenoise.o
|
cc -c linenoise.c -o linenoise.o
|
||||||
|
|
||||||
tryrax: try_rax.c
|
tryrax: try_rax.c
|
||||||
cc try_rax.c rax.o -lm -o tryrax
|
cc try_rax.c rax.o linenoise.o -lm -o tryrax
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,38 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include "linenoise.h"
|
||||||
#include "rax.h"
|
#include "rax.h"
|
||||||
|
|
||||||
|
|
||||||
|
rax *rt;
|
||||||
|
|
||||||
#define INSERT(key) raxInsert(rt, (unsigned char*)(key), sizeof((key)), NULL, NULL);
|
#define INSERT(key) raxInsert(rt, (unsigned char*)(key), sizeof((key)), NULL, NULL);
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
completion(const char *buf, linenoiseCompletions *lc)
|
||||||
|
{
|
||||||
|
int n = strnlen(buf, 1024);
|
||||||
|
raxIterator iter;
|
||||||
|
raxStart(&iter, rt);
|
||||||
|
raxSeek(&iter, ">=", (unsigned char*)buf, n);
|
||||||
|
while(raxNext(&iter)) {
|
||||||
|
if (strncmp((const char *)iter.key, buf, n))
|
||||||
|
break;
|
||||||
|
linenoiseAddCompletion(lc, (const char *)iter.key);
|
||||||
|
}
|
||||||
|
raxStop(&iter);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main()
|
main()
|
||||||
{
|
{
|
||||||
rax *rt = raxNew();
|
char *line;
|
||||||
|
linenoiseSetCompletionCallback(completion);
|
||||||
|
rt = raxNew();
|
||||||
INSERT("=")
|
INSERT("=")
|
||||||
INSERT(">")
|
INSERT(">")
|
||||||
INSERT("<")
|
INSERT("<")
|
||||||
|
|
@ -177,6 +201,16 @@ main()
|
||||||
INSERT("_\\/_")
|
INSERT("_\\/_")
|
||||||
INSERT("/\\")
|
INSERT("/\\")
|
||||||
INSERT("\\/")
|
INSERT("\\/")
|
||||||
|
/*
|
||||||
raxShow(rt);
|
raxShow(rt);
|
||||||
}
|
*/
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
line = linenoise("eh? ");
|
||||||
|
if (NULL == line) {
|
||||||
|
printf("\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
linenoiseHistoryAdd(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue