计算 zsh 提示的用户可见字符串的长度 [英] Count length of user-visible string for zsh prompt
问题描述
我想把我当前的 git
分支放到我的多行 ZSH 提示符中.然而,这把两行搞乱了——我希望他们能很好地排队.
应该是:
<预><代码>┌─(simon@charmander:s000)─[大师*]─────────(~)─┐└─(127:15:44)── ──(Sat, May12)─┘git
分支是从 oh-my-zsh
函数中获取的,git_prompt_info()
,它给了我分支,脏状态,还有一堆提示转义,可以很好地为事物着色.
我如何计算将可见插入到 ZSH 提示中的字符 - 而不是提示转义序列?
假设提示转义的字符串存储在变量 FOO
中,这将只计算用户可见的字符:
这来自这个.zshrc
.
这是对其工作原理的粗略解释,自由地引用了 man zshexpn
,PARAMETER EXPANSION
部分.我不是 100% 确定细节,因此,如果您使用它来开发自己的等价物,请阅读相关的 man zshall
部分.
从 FOOLENGTH=${#${(S%%)FOO//$~zero/}}
行开始,我们得到了许多位.由内而外:
$~zero
:~
确保zero
,我们将其定义为'%([BSUbfksu]|([FB]|){*})'
,被视为一个模式而不是一个普通的字符串.${(S%%)FOO//$~zero/}
:这匹配${name//pattern/repl}
:><块引用>用字符串repl替换参数名扩展中最长可能匹配的模式
注意我们没有
repl
;我们将pattern
的最长可能匹配替换为空,从而将其删除.(S%%)FOO
对FOO
进行扩展,并设置了几个标志.我不太遵循它.${#${(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:
$~zero
: The~
ensures thatzero
, which we've defined as'%([BSUbfksu]|([FB]|){*})'
, is treated as a pattern rather than as a plain string.${(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 ofpattern
with nothing, thereby removing it.
(S%%)FOO
conducts an expansion onFOO
with several flags set. I don't quite follow it.${#${(S%%)FOO//$~zero/}}
:${#spec}
will substitue the length in characters of the result of the substitutionspec
, ifspec
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 expressions/zero//
onFOO
, wherezero
is the pattern above.
这篇关于计算 zsh 提示的用户可见字符串的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!