www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - readline / Gnu readline

reply Michael <michaelhusmann gmail.com> writes:
I am new to D.
I would like to use the Gnu readline function in D. Is there a 
module that i can use?
Jan 29 2020
next sibling parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
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
Jan 29 2020
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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); } --- 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.
Jan 29 2020
next sibling parent reply bachmeier <no spam.net> writes:
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); } --- 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; } } }
Jan 29 2020
parent Adam D. Ruppe <destructionator gmail.com> writes:
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!
Jan 29 2020
prev sibling parent reply Michael <michaelhusmann gmail.com> writes:
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); } --- 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.
Jan 29 2020
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
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?
Jan 29 2020
parent reply Michael <michaelhusmann gmail.com> writes:
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.
Jan 29 2020
parent reply Mike Parker <aldacron gmail.com> writes:
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
Jan 30 2020
parent Michael <michaelhusmann gmail.com> writes:
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
Jan 30 2020
prev sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 30 January 2020 at 06:12:32 UTC, Michael wrote:
 I did exactly just what you proposed.
yeah i often just leave my random filenames in there, in this case rl was one of them. (if you don't put `.d` at the end of a filename, dmd will add it automatically). Generally a "module X is in file X which cannot be read" error means you should: 1) double-check any filenames on the command line. Make sure no typos etc and the files all exist. With my samples that frequently means changing the filename from whatever nonsense I left in my lazy copy/paste :P 2) If all the files are in place, this error can also be caused by a mismatch between the `module` name in an imported file and the `import` statement that is trying to use it. Those should always match.
Jan 30 2020
parent Michael <michaelhusmann gmail.com> writes:
On Thursday, 30 January 2020 at 13:23:17 UTC, Adam D. Ruppe wrote:
 On Thursday, 30 January 2020 at 06:12:32 UTC, Michael wrote:
 I did exactly just what you proposed.
yeah i often just leave my random filenames in there, in this case rl was one of them. (if you don't put `.d` at the end of a filename, dmd will add it automatically). Generally a "module X is in file X which cannot be read" error means you should: 1) double-check any filenames on the command line. Make sure no typos etc and the files all exist. With my samples that frequently means changing the filename from whatever nonsense I left in my lazy copy/paste :P 2) If all the files are in place, this error can also be caused by a mismatch between the `module` name in an imported file and the `import` statement that is trying to use it. Those should always match.
I'm getting a bit more familiar with D. Really nice. Just wrote a program consisting of several files. Importing works fine now and also GNU readline. Thanks for your help
Jan 30 2020