Jump to page: 1 2
Thread overview
readline / Gnu readline
Jan 29, 2020
Michael
Jan 29, 2020
Ferhat Kurtulmuş
Jan 29, 2020
Adam D. Ruppe
Jan 29, 2020
bachmeier
Jan 29, 2020
Adam D. Ruppe
Jan 30, 2020
Michael
Jan 30, 2020
Mike Parker
Jan 30, 2020
Michael
Jan 30, 2020
Mike Parker
Jan 30, 2020
Michael
Jan 30, 2020
Adam D. Ruppe
Jan 30, 2020
Michael
January 29, 2020
I am new to D.
I would like to use the Gnu readline function in D. Is there a module that i can use?
January 29, 2020
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?

Found this. But code.dlang.org is having some issues right now. Try accessing it after some time.

https://code.dlang.org/packages/readline
January 29, 2020
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.
January 29, 2020
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;
		}
  }
}

January 29, 2020
On Wednesday, 29 January 2020 at 22:10:04 UTC, bachmeier wrote:
> That's pretty cool. I didn't know anything about this. Taking the example from here:

Yeah, readline is a really nice lib, super simple interface. I think it is in large part responsible for the GPL's success too since it is so useful, so simple, and so correctly licensed :)

And knowing how extern(C) is so easy is good to know too - using any C lib from D  can be done similarly and for a lot of libs it is actually this simple.

BTW I also have a comparable function in my terminal.d:

http://dpldocs.info/experimental-docs/arsd.terminal.html#get-line

though of course then you gotta download my code file and add it to your build (which is easy but still). mine also works on windows!
January 30, 2020
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.

Dear Adam,

I did exactly just what you proposed.

When 'dmd rl -L-lreadline' in the command line. I do get the following error:
Error: module rl is in file 'rl.d' which cannot be read.

So probably I'm missing something unfortunately I don't know what.
January 30, 2020
On Thursday, 30 January 2020 at 06:12:32 UTC, Michael wrote:

> When 'dmd rl -L-lreadline' in the command line. I do get the following error:
> Error: module rl is in file 'rl.d' which cannot be read.
>
> So probably I'm missing something unfortunately I don't know what.

Is your source file named rl.d? And are you running dmd in the source file's directory?
January 30, 2020
On Thursday, 30 January 2020 at 06:15:54 UTC, Mike Parker wrote:

> Is your source file named rl.d? And are you running dmd in the source file's directory?

No, I did not. Changed it now and it works with dmd. Great!
Tried the same with rdmd I'm getting a linker error.


January 30, 2020
On Thursday, 30 January 2020 at 06:27:36 UTC, Michael wrote:
> On Thursday, 30 January 2020 at 06:15:54 UTC, Mike Parker wrote:
>
>> Is your source file named rl.d? And are you running dmd in the source file's directory?
>
> No, I did not. Changed it now and it works with dmd. Great!
> Tried the same with rdmd I'm getting a linker error.

Take a look at the rdmd documentation:

https://dlang.org/rdmd.html

There you'll see this:

rdmd [dmd and rdmd options] progfile[.d] [program arguments]

That means any arguments you pass on the command line after the source file name will be passed to your program. Compiler options need to go before the source file name.

rdmd -L-lreadline mysource.d
January 30, 2020
On Thursday, 30 January 2020 at 08:13:16 UTC, Mike Parker wrote:
> That means any arguments you pass on the command line after the source file name will be passed to your program. Compiler options need to go before the source file name.
>
> rdmd -L-lreadline mysource.d

That works, thanks Mike
« First   ‹ Prev
1 2