I would like to read from stdin, but only if the user actually wrote something.
I don’t really want to open another thread just for this so I tried a implementation from another ziggit post: Simple solution to read a byte with timeout
So at the end I wrote this code:
const resultOrError = io.operateTimeout(.{.file_read_streaming = .{
.data = &.{&readBuffer},
.file = .stdin(),
}}, .{ .duration = .{ .raw = .zero , .clock = .awake } }) catch |err| {
if (err == error.Timeout) return;
std.log.err("Error while reading from stdin: {t}", .{err});
return;
};
const result = resultOrError.file_read_streaming catch |err| {
std.log.err("Error while reading from stdin: {t}", .{err});
return;
};
if (result == 0) return;
... // do something with the input
This works on linux and I hoped that it would also work on windows, but when I tried it I got this error:
Error while reading from stdin: ConcurrencyUnavailable
This is because of this line in std/Io/Threaded.zig:
if (o.file.flags.nonblocking) {
...
} else {
if (concurrency) return error.ConcurrencyUnavailable;
...
I am not that deep into the everything going on with how windows etc handle the stdin, but is there a way for me to make this work on linux and on windows? Or can maybe someone point me to an issue I need to look out for, so I can just mark this as currently not possible on windows. (or maybe there is already a way on 0.17.0?)
(I do want to say that this is my first time trying to write such a program, so I may miss some implementation that would be better / would solve this)