On Wednesday, 29 January 2020 at 21:15:08 UTC, Adam D. Ruppe
wrote:
On Wednesday, 29 January 2020 at 20:01:32 UTC, Michael wrote:
I am new to D.
I would like to use the Gnu readline function in D. Is there a
module that i can use?
just define it yourself
---
// this line right here is all you need to call the function
extern(C) char* readline(const char*);
import core.stdc.stdio;
void main() {
char* a = readline("prompt> ");
printf("%s\n", a);
}
---
# and also link it in at the command line with -L-lreadline
dmd rl -L-lreadline
readline is so simple you don't need to do anything fancier. If
you need history and such too you just define those functions
as well.
That's pretty cool. I didn't know anything about this. Taking the
example from here:
https://eli.thegreenplace.net/2016/basics-of-using-the-readline-library/
You can basically run the same code (I modified it to end on
empty input. This is complete with history:
extern(C) {
char* readline(const char*);
void add_history(const char *string);
}
import core.stdc.stdio;
import core.stdc.string;
import core.stdc.stdlib;
void main() {
char* buf;
while ((buf = readline(">> ")) !is null) {
if (strlen(buf) > 0) {
add_history(buf);
printf("[%s]\n", buf);
free(buf);
} else {
break;
}
}
}