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



On 12/30/22, 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
>
> How can this be achieved in zsh? I have tried two techniques; either it
> clears screen below prompt or it does not allow using arrow to go back to
> middle of line to edit it.
>
> 1. Using ``vared``:
> ```
> tput cup 0 0; userinput=""; vared -p "Enter input: " userinput
> ```
> ``vared`` seems to clear screen below the prompt, so this clears the entire
> screen before showing the prompt.
>
> 2. Using ``read``:
>
> ```
> tput cup 0 0; printf "Enter input: "; read -r userinput
> ```
> This does not clear the screen below the prompt, but does not allow using
> arrow keys to go to middle of the line and make an edit ("delete" key
> works, but is too much to ask users to delete and retype everything from
> the point of typo).
>
> Is there a way out? Perhaps it could be possible to trick vared to believe
> there are no lines below, so it clears just 1 line (the line of prompt). If
> it is not possible in zsh, I am open to an external POSIX way of getting
> user input on first line of the screen.
>
> P.S. Also posted in unix.stackexchange.com:
> https://unix.stackexchange.com/q/730022/456507

Without more context this sounds like a very weird request, surely
printing stuff at 0,0 will just overlap whatever existing text was
there, resulting in a corrupted screen output? Perhaps you will have
better luck switching to the alternate screen while prompting for the
string, and then switching back, in effect giving you the appearance
of a full screen program (more likely to direct the user's attention
to the top of the terminal). (enable with echo -n '\e[?47h' and
disable with echo -n '\e[?47l')

PS
this is probably more of a zsh-users question than zsh-workers

-- 
Mikael Magnusson




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