zsh-users
 help / color / mirror / code / Atom feed
* How to call zle -U while drawing the prompt?
@ 2017-05-26 14:45 Dupéron Georges
  2017-05-27  0:10 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Dupéron Georges @ 2017-05-26 14:45 UTC (permalink / raw)
  To: zsh-users

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`'


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How to call zle -U while drawing the prompt?
  2017-05-26 14:45 How to call zle -U while drawing the prompt? Dupéron Georges
@ 2017-05-27  0:10 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2017-05-27  0:10 UTC (permalink / raw)
  To: zsh-users; +Cc: Duperon Georges

On May 26,  4:45pm, Duperon Georges wrote:
}
} * 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?

Make your call to zle -U from zle-line-init.

    autoload add-zle-hook-widget
    unget_user_input() {
      [[ -n "$userinput" ]] && zle -U "$userinput"
      unset userinput
    }
    add-zle-hook-widget line-init unget_user_input

If you're using a version of zsh that doesn't have add-zle-hook-widget
you can just do

    zle-line-init() {
      [[ -n "$userinput" ]] && zle -U "$userinput"
      unset userinput
    }
    zle -N zle-line-init

Obviously to do this userinput must be a global rather than a local.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-05-27  0:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-26 14:45 How to call zle -U while drawing the prompt? Dupéron Georges
2017-05-27  0:10 ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).