implement a timeout option for `read` ala bash's `read -t timeout`
It would be useful to have a mode for read to fail immediately when there is nothing to read from stdin.
As the simpler version of read -n 0 -t 0 (from Bash) there could be just a switch (-n? from no wait) to just return !=0 when there was nothing awaiting on stdin (instead of showing a prompt).
As it can be achieved with the following code I don't know if a read modification is really needed.
function myfunc
if not tty >/dev/null
read input
else
set input $argv
end
echo $input
end
That's not quite the same thing; Bash's read -n 0 -t 0 checks if there is anything to read on STDIN, regardless of whether it is connected to a TTY or not.
Consider the pathological case echo -n | myfunc correct. This produces empty output instead of correct.
In Bash you could do
function myfunc {
read -n 0 -t 0 input || input=$1
echo $input;
}
I just hit this issue while trying to do some tricks with STDIN. Any news on this front?
Nobody is working on it. What tricks were you hoping to do?
Stuff that queries the terminal using VT or xterm commands where the terminal will reply by emitting stuff on stdin kind of needs a timeout in my experience.
Simple example, capturing the resulting stdin of this is usually done with read with a maybe a 0.1s or less timeout on other shells. This is querying the configured background color:
printf \e]11\;?\a
It might "return" nothing at all, or maybe a very predictable escape, or a string that is unpredictable to us. I think there is actually no way to grab the output with fish.
this would be useful - if only to facilitate bash script porting, more compact code