Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Read a line from user without clearing screen below the prompt while allowing user to use arrow keys to make edits in middle of line



(Leaving this on -workers because of the possible bug mentioned at the end.)

On Thu, Dec 29, 2022 at 9:38 PM OG Code Poet <ogcodepoet@xxxxxxxxx> wrote:
>
> Using Bash ``tput cup 0 0; read -e -p "Enter input: " userinput`` works well for getting a line of user input:
>
> * It does not clear screen below the prompt
> * It allows user to use arrow keys to go to middle of line and edit a mistake they might have made while typing

Bash is invoking the GNU "readline" library for the "read" builtin.
Unlike ZLE, which despite having "line" in its name attempts to be a
reasonable multi-line editor, readline is designed to do exactly what
its name says, so it doesn't do any other screen prep.

> How can this be achieved in zsh?

Closest I can come up with is this:

vared-finish() { tput rc }
zle -N vared-finish
tput sc
vared -f vared-finish -c -p "%{$(tput cup 0 0)%}Enter input: " userinput

However, this is not entirely satisfactory because the finish widget
is not invoked if this is interrupted by ^C or ^G (send-break), and
vared then clears the screen before exiting.  This is possibly a bug?
Shouldn't the finish widget(s) always be run when the line editor
exits, even if it exits "abnormally"?  (This affects the PS1 ZLE as
well.)




Messages sorted by: Reverse Date, Date, Thread, Author