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

How to call zle -U while drawing the prompt?



Hello!

I am trying to get the current cursor position (in lines and columns
w.r.t. the top left corner of the console) while drawing the ZSH
prompt. My actual goal is to display more or less verbose information
below the "actual" prompt line depending on how much space is
available.

In order to know the current cursor position, I need to send the
escape sequence \e[6n to the terminal, and it replies with \e[Y;XR .

If the user typed some input before the cursor position is read (e.g.
during the "sleep 1" in the example below, or while .zshrc is
initially running when opening a new terminal), then these characters
are discarded.

I tried to re-add these characters to ZSH's input buffer by calling
zle -U "$userinput", but I get the error "can only be called from
widget function".

How can I:

* Call zle -U while drawing the prompt?
* Or otherwise force zsh to either read all input just before I send
    \e[6n to the terminal?

Thanks!
Georges Dupéron

# This is the short .zshrc I use:
setopt prompt_subst
MY_PS1(){
  local stuff line userinput

  # simulate some work:
  sleep 1

  # request the cursor position
  echo -ne "\033[6n" > /dev/tty
  read -s -d 'R' stuff < /dev/tty
  line="${stuff##*\[}"
  line="${line%;*}"
  userinput="${stuff%\[*}"

  # re-inject the already-typed text
  zle -U "$userinput"

  echo "prompt:"
}
PS1='`MY_PS1`'



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