异步RPROMPT?

Zev*_*erg 5 prompt zsh shell-script

我的一个朋友在 StackOverflow 上发布了这个,我想我们可能更有可能在这里得到答案。他的帖子指的是速度,但我们现在知道我们用来解析 git status 的 python 脚本很慢,并且在某些时候我们打算重写它以使其更快。然而,RPROMPT异步设置的问题对我来说仍然很有趣,所以我想我会在这里引用他的问题:

自从我开始使用 git 以来,我已经设置了 RPROMPT 来显示当前分支。我最近一直在使用一些“花哨的”脚本来显示未/暂存的文件计数和其他有用的一目了然的东西。( https://github.com/olivierverdier/zsh-git-prompt/tree/master )

使用它一两个星期后,它的性能开始困扰我。

是否有更快的方法来获取此信息,或者是否有异步编写 RPROMPT 的方法?我不想在计算 RPROMPT 时等待输入命令,并且会非常高兴它在我的主 PROMPT 稍晚出现。

没有冒犯上述脚本;这很棒。我只是不耐烦。

Mat*_*att 4

这是一个使用后台作业和信号来异步更新提示的解决方案。

这个想法是让你的提示函数产生一个后台作业,该作业构建提示,将其写入文件,然后向父 shell 发送已完成的信号。当父 shell 收到信号时,它从文件中读取提示并重绘提示。

在你的提示函数中,输入:

function async-build-prompt {
    # Simulate a function that takes a couple seconds to build the prompt.
    # Replace this line with your actual function.
    sleep 2 && RPROMPT=$(date)

    # Save the prompt in a temp file so the parent shell can read it.
    printf "%s" $RPROMPT > ${TMPPREFIX}/prompt.$$

    # Signal the parent shell to update the prompt.
    kill --signal USR2 $$
}

# Build the prompt in a background job.
async-build-prompt &!
Run Code Online (Sandbox Code Playgroud)

在你的 .zshrc 中,输入:

function TRAPUSR2 {
    RPROMPT=$(cat "${TMPPREFIX}/prompt.$$")

    # Force zsh to redisplay the prompt.
    zle && zle reset-prompt
}
Run Code Online (Sandbox Code Playgroud)