0

I want to read normal text input (a.k.a. not the regular ZSH editor that might have auto-completion etc.) from inside a ZLE widget. Currently when I try:

function myfunc() {
    echo -n "Your name: "
    read -re 
    echo $REPLY
}

zle -N mywidget myfunc bindkey "\C-k" mywidget

When I call myfunc, everything goes as expected but when I press C-k (binded to the keystroke through bindkey above) it's like the read command is skipped:

enter image description here

I haven't pressed enter, just C-k one time.

What I want is to have bash-like functionality and wait for my input.

I'm aware of zsh read input in zle widget but I don't want to use ZSH's minibuffer editor as it has autocompletion etc. and this Better way to read a line of user input in zsh? (e.g. with zle?) solution didn't work for me

dzervas
  • 237
  • 2
  • 12

1 Answers1

0

ZLE redirects the input to the functions to /dev/null, as described in the man page:

The standard input of the function is closed to prevent external commands from unintentionally blocking ZLE by reading from the terminal

Thus the input should be read from the tty directly, as follows:

function myfunc() {
    zle -I
    echo -n "Your name: "
    read -re < /dev/tty
    echo $REPLY
}

zle -N mywidget myfunc bindkey "\C-k" mywidget

To print text correctly while inside the widget zle -I is prepended, as described in the man page as well:

If the handler produces output to the terminal, it should call 'zle -I' before doing so (see below). The handler should not attempt to read from the terminal.

dzervas
  • 237
  • 2
  • 12