简体   繁体   English

绘制ZSH提示时获取当前光标位置

[英]Get current cursor position while drawing ZSH prompt

I'm trying to read into a variable the current cursor position (the current line number and column) into a variable, from within a function drawing the ZSH shell prompt.我正在尝试从绘制 ZSH shell 提示的函数中将当前光标位置(当前行号和列)读入一个变量中。 My goal is to display stuff below the prompt, only if there are enough empty lines to not cause extra scrolling.我的目标是在提示下方显示内容,仅当有足够多的空行不会导致额外滚动时。

In an interactive shell, I can use the following commands:在交互式 shell 中,我可以使用以下命令:

echo -ne "\033[6n"
read -t 1 -s -d 'R' line
line="${line##*\[}"
line="${line%;*}"
echo "XX $line XX"
# Prints: XX 2 XX"

However, if I start a clean zsh -f , and put this into a function which is executed when rendering the prompt, it stops working:但是,如果我启动一个干净的zsh -f ,并将其放入一个在呈现提示时执行的函数,它将停止工作:

setopt prompt_subst
prompt_fn(){
  echo -ne "\033[6n"
  read -t 1 -s -d 'R' line
  line="${line##*\[}"
  line="${line%;*}"
  echo "XX $line XX"
}
PROMPT='`prompt_fn` '

The ANSI escape sequence returned by the terminal gets appended to the current command (as if I had typed it on my keyboard), but is not gobbled by the read -t 1 -s -d 'R' line command above.终端返回的 ANSI 转义序列被附加到当前命令(就像我在键盘上输入一样),但不会被上面的read -t 1 -s -d 'R' line命令吞噬。 I suspect that ZSH disables access to STDIN while drawing the prompt, but I do not know how to regain access to it temporarily (normal keyboard keystrokes typed before drawing the prompt, or during the tenth of second that it takes to draw it should not be intercepted), or how to use ZLE to access that information.我怀疑 ZSH 在绘制提示时禁用了对 STDIN 的访问,但我不知道如何暂时重新获得对它的访问(在绘制提示之前键入的正常键盘击键,或在绘制提示所需的十分之一秒期间不应拦截),或如何使用 ZLE 访问该信息。

Edit: if the user already typed the beginning of a command before the prompt was shown, that input should not be discarded.编辑:如果用户在显示提示之前已经输入了命令的开头,则不应丢弃该输入。 The solution I found so far (see my own answer below) unfortunately reads and drops these characters.到目前为止,我找到的解决方案(请参阅下面我自己的答案)很不幸地读取并删除了这些字符。 This is frustrating, as when I open a new terminal window and start typing right away, the characters typed before the prompt appears are discarded.这令人沮丧,因为当我打开一个新的终端窗口并立即开始输入时,在提示出现之前输入的字符将被丢弃。

Reading directly from /dev/tty seems to mostly work, but it still gobbles any input typed between the beginning of prompt_fn and the read command.直接从/dev/tty读取似乎大部分工作,但它仍然prompt_fnprompt_fn开头和read命令之间键入的任何输入。 If the prompt_fn is doing a bit of work before getting to that point, it may cause some user input to be dropped.如果prompt_fn在到达那个点之前做了一些工作,它可能会导致一些用户输入被丢弃。 Hopefully someone will come along with a better solution.希望有人会提出更好的解决方案。

setopt prompt_subst
prompt_fn(){
  echo -ne "\033[6n" > /dev/tty
  read -t 1 -s -d 'R' line < /dev/tty
  line="${line##*\[}"
  line="${line%;*}"
  echo "XX $line XX"
}
PROMPT='`prompt_fn` '

Maybe you want to look into the minibuffer based off of these docs:也许您想根据这些文档查看minibuffer

http://zsh.sourceforge.net/Guide/zshguide04.html http://zsh.sourceforge.net/Guide/zshguide04.html

The `minibuffer' is yet another Emacs concept; `minibuffer' 是另一个 Emacs 概念; it is a prompt that appears just under the command line for you to enter some edit required by the editor itself.它是出现在命令行下的提示,供您输入编辑器本身所需的一些编辑。

I used it in a zsh plugin to print out help for the command under the cursor.我在 zsh 插件中使用它来打印光标下命令的帮助。 I only display text, but you should be able to do other things with it.我只显示文本,但你应该可以用它做其他事情。

cheat buffer example作弊缓冲区示例

You can also find some information on zsh widgets here您还可以在此处找到有关zsh 小部件的一些信息

This might be relevant:这可能是相关的:

BUFFERLINES (integer)缓冲区(整数)

The number of screen lines needed for the edit buffer currently displayed on screen (ie without any changes to the preceding parameters done after the last redisplay);当前显示在屏幕上的编辑缓冲区所需的屏幕行数(即在上次重新显示后没有对前面的参数进行任何更改); read-only.只读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM