计算 zsh 提示的用户可见字符串的长度 [英] Count length of user-visible string for zsh prompt

查看:32
本文介绍了计算 zsh 提示的用户可见字符串的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把我当前的 git 分支放到我的多行 ZSH 提示符中.然而,这把两行搞乱了——我希望他们能很好地排队.

<预><代码>┌─(simon@charmander:s000)─[大师*]──────────────────────(~)──┐└─(127:15:44)── ──(Sat, May12)─┘

应该是:

<预><代码>┌─(simon@charmander:s000)─[大师*]─────────(~)─┐└─(127:15:44)── ──(Sat, May12)─┘

git 分支是从 oh-my-zsh 函数中获取的,git_prompt_info(),它给了我分支,脏状态,还有一堆提示转义,可以很好地为事物着色.

我如何计算将可见插入到 ZSH 提示中的字符 - 而不是提示转义序列?

解决方案

假设提示转义的字符串存储在变量 FOO 中,这将只计算用户可见的字符:

<预><代码>FOO=$(git_prompt_info)本地零='%([BSUbfksu]|([FK]|){*})'FOOLENGTH=${#${(S%%)FOO//$~零/}}

这来自这个.zshrc.

这是对其工作原理的粗略解释,自由地引用了 man zshexpnPARAMETER EXPANSION 部分.我不是 100% 确定细节,因此,如果您使用它来开发自己的等价物,请阅读相关的 man zshall 部分.

FOOLENGTH=${#${(S%%)FOO//$~zero/}} 行开始,我们得到了许多位.由内而外:

  1. $~zero:~ 确保 zero,我们将其定义为 '%([BSUbfksu]|([FB]|){*})',被视为一个模式而不是一个普通的字符串.

  2. ${(S%%)FOO//$~zero/}:这匹配${name//pattern/repl}:<块引用>

    用字符串repl替换参数名扩展中最长可能匹配的模式

    注意我们没有repl;我们将 pattern 的最长可能匹配替换为空,从而将其删除.
    (S%%)FOOFOO 进行扩展,并设置了几个标志.我不太遵循它.

  3. ${#${(S%%)FOO//$~zero/}}: ${#spec} 将替换长度替换结果的字符 spec,如果 spec 是替换.在我们的例子中,spec 是替换的结果 ${(S%%)FOO//$~zero/};所以这基本上返回了 FOO 上的正则表达式 s/zero// 结果中的字符长度,其中 zero 是上面的模式.

I'd like to put my current git branch into my multi-line ZSH prompt. However, this messes up the two lines - I'd like them to line up nicely.


┌─(simont@charmander:s000)─[master *]────────────────
───(~  )─┐  
└─(127:15:44)──                       ──(Sat,May12)─┘

should be:


┌─(simont@charmander:s000)─[master *]─────────(~  )─┐  
└─(127:15:44)──                       ──(Sat,May12)─┘

The git branch is grabbed from an oh-my-zsh function, git_prompt_info(), which gives me the branch, dirty status, and a bunch of prompt-escapes to color things nicely.

How do I count the characters that will be visibly inserted into the ZSH prompt - not the prompt escape sequences?

解决方案

Assuming that the prompt-escaped string is stored in a variable FOO, this will count only user-visible characters:

                                                                                                                                
FOO=$(git_prompt_info)                                                                                                                     
local zero='%([BSUbfksu]|([FK]|){*})'
FOOLENGTH=${#${(S%%)FOO//$~zero/}} 

This comes from this .zshrc.

This is a rough explanation of why it works, liberally quoting from man zshexpn, section PARAMETER EXPANSION. I'm not 100% sure of the details, so, if you're using this to develop your own equivalent, read the relevant man zshall sections.

Working from the line FOOLENGTH=${#${(S%%)FOO//$~zero/}}, we've got a number of bits. Going from the inside out:

  1. $~zero: The ~ ensures that zero, which we've defined as '%([BSUbfksu]|([FB]|){*})', is treated as a pattern rather than as a plain string.

  2. ${(S%%)FOO//$~zero/}: This matches ${name//pattern/repl}:

    Replace the longest possible match of pattern in the expansion of parameter name by string repl

    Note that we don't have a repl; we replace the longest possible match of pattern with nothing, thereby removing it.
    (S%%)FOO conducts an expansion on FOO with several flags set. I don't quite follow it.

  3. ${#${(S%%)FOO//$~zero/}}: ${#spec} will substitue the length in characters of the result of the substitution spec, if spec is a substitution. In our case, spec is the result of the substitution ${(S%%)FOO//$~zero/}; so this basically returns the length of characters in the result of the regular expression s/zero// on FOO, where zero is the pattern above.

这篇关于计算 zsh 提示的用户可见字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆