On Wednesday, 27 December 2023 at 13:27:53 UTC, Adam D Ruppe wrote:
> On Wednesday, 27 December 2023 at 05:07:04 UTC, Joe wrote:
> ??????????????????????????????????????? Surely there there is a one liner library solution for this?
It is not one line because it needs a bit of setup (and teardown, but the objects' destructors do that for you) but it is close:
http://arsd-official.dpldocs.info/arsd.terminal.html#single-key
input.getch
waits for a single line, but you can use if(input.kbhit())
to see if it would block before calling it.
> This shouldn't be hard... yet it is.
Better be careful, the mods are out in force deleting posts this week that tell the hard truth. But yeah, the stdlib in D has very little activity:
https://github.com/dlang/phobos/graphs/contributors?
So you can't expect much from it. My arsd libs provide a broad set of functionality missing from it: stuff like this terminal/console stuff, window creation, basic guis, web servers, etc.
If you want to try them, you can use it from the dub system, but I recommend just git clone https://github.com/adamdruppe/arsd.git
in your working directory then import what you want and use dmd -i
to automatically include them in the build.
This does not actually work on my computer. It still blocks.
int itr = 0;
for(;;)
{
itr++;
writeln("1. closeKBThread = ", closeKBThread, ", iter = ", itr);
if (closeKBThread) return;
string op = "";
string istr = "";
while (!closeKBThread)
{
writeln("2. ", input.kbhit(), " ", closeKBThread);
if (input.kbhit())
{
istr ~= input.getch(false);
break;
}
}
writeln("final istr = ", istr);
- closeKBThread = false, iter = 1
- false false
- false false
- false false
- false false
final istr = f
- closeKBThread = false, iter = 2
- false false < now blocking
All this is just junk of me trying to figure out what was going on, but literally input.kbhit blocks after the first run(which really means it's always blocking)
My original code was
while(true)
{
if (input.kbhit()) { istr ~= input.getch(); break; }
}
and I've been trying all kinds of stuff to figure out what was going on but I believe it is the kbhit function itself. It calls getch(true) and blocks on that as if I were just using getch itself.
The issue is the same, the code does not block then after I hit a key and it goes through an iteration of the outside loop it then blocks waiting for the next input.