www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Prompting using stdio

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
I thought

int main(string[] args)
{
     import std.stdio;
     write(`Press enter to continue: `);
     stdout.flush;
     auto line = readln();
     writeln("Read ", line);
     return 0;
}

would function as a good prompting but it doesn't.

I outputs the string given to write *after* I've pressed return. 
Why?
Sep 28 2014
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sun, Sep 28, 2014 at 02:16:29PM +0000, "Nordlöw" via Digitalmars-d-learn
wrote:
 I thought
 
 int main(string[] args)
 {
     import std.stdio;
     write(`Press enter to continue: `);
     stdout.flush;
     auto line = readln();
     writeln("Read ", line);
     return 0;
 }
 
 would function as a good prompting but it doesn't.
 
 I outputs the string given to write *after* I've pressed return. Why?
It's an OS limitation. If you're on Posix, you need to switch your terminal to cbreak mode, otherwise the program doesn't actually receive any data until after you press Return. I'm not sure what the Windows equivalent is, but a similar thing happens there -- the input line is buffered by the OS and the program doesn't see it until Return is pressed. You might be interested in Adam Ruppe's terminal.d library, that handles such low-level details for you: https://github.com/adamdruppe/arsd/blob/master/terminal.d T -- People who are more than casually interested in computers should have at least some idea of what the underlying hardware is like. Otherwise the programs they write will be pretty weird. -- D. Knuth
Sep 28 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 28 September 2014 at 14:48:03 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
 On Sun, Sep 28, 2014 at 02:16:29PM +0000, "Nordlöw" via 
 Digitalmars-d-learn wrote:
 I thought
 
 int main(string[] args)
 {
     import std.stdio;
     write(`Press enter to continue: `);
     stdout.flush;
     auto line = readln();
     writeln("Read ", line);
     return 0;
 }
 
 would function as a good prompting but it doesn't.
 
 I outputs the string given to write *after* I've pressed 
 return. Why?
It's an OS limitation. If you're on Posix, you need to switch your terminal to cbreak mode, otherwise the program doesn't actually receive any data until after you press Return. I'm not sure what the Windows equivalent is, but a similar thing happens there -- the input line is buffered by the OS and the program doesn't see it until Return is pressed.
That's not his problem though - he doesn't see "Press enter to continue" in the terminal before he enters text. However, I can't reproduce it with DMD master on Linux, it works for me as intended.
Sep 28 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Sunday, 28 September 2014 at 15:36:17 UTC, Marc SchĂĽtz wrote:
 However, I can't reproduce it with DMD master on Linux, it 
 works for me as intended.
My fault. I was too clever and call the program through a wrapper to rdmd I call rdmd-dev containing #!/usr/bin/env bash # See also: http://forum.dlang.org/thread/qvznmjdmdvkyyrkhbkgy forum.dlang.org if type ddemangle &> /dev/null; then exec rdmd -L--export-dynamic -color=on -g -gs -debug $* 2>&1 | ddemangle else exec rdmd -L--export-dynamic -color=on -g -gs -debug $* 2>&1 fi The piping to ddemangle messed things up... Thanks anyway.
Sep 28 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Sunday, 28 September 2014 at 16:01:06 UTC, Nordlöw wrote:
 The piping to ddemangle messed things up...

 Thanks anyway.
Does anybody have a better solution to this? I would still like the linker error output be ddemangled...
Sep 28 2014
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 28 September 2014 at 16:02:44 UTC, Nordlöw wrote:
 On Sunday, 28 September 2014 at 16:01:06 UTC, Nordlöw wrote:
 The piping to ddemangle messed things up...

 Thanks anyway.
Does anybody have a better solution to this? I would still like the linker error output be ddemangled...
You can redirect stderr to ddemangle: rdmd ... 2> >( ddemangle )
Sep 28 2014