fish-shell icon indicating copy to clipboard operation
fish-shell copied to clipboard

implement a timeout option for `read` ala bash's `read -t timeout`

Open szpak opened this issue 12 years ago • 7 comments

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).

szpak avatar Nov 21 '13 11:11 szpak

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

szpak avatar Nov 22 '13 10:11 szpak

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;
}

zanchey avatar Nov 27 '13 13:11 zanchey

I just hit this issue while trying to do some tricks with STDIN. Any news on this front?

ravishi avatar Oct 20 '21 14:10 ravishi

Nobody is working on it. What tricks were you hoping to do?

ridiculousfish avatar Oct 22 '21 01:10 ridiculousfish

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.

floam avatar Oct 22 '21 05:10 floam

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.

floam avatar Oct 22 '21 05:10 floam

this would be useful - if only to facilitate bash script porting, more compact code

arigit avatar May 28 '24 00:05 arigit