【发布时间】:2017-04-11 04:38:30
【问题描述】:
上下文:我想右对齐部分提示。这样做时,我目前的方法是计算它的左右分量的长度,并用空格填充中间分量。
问题:当字符串可能包含 unicode(例如 git status)时,处理%G(请参阅prompt expansion)。可能实际的问题是我没有正确掌握它。在关于how to signal zsh that there are characters to be output 的另一个线程答案中建议使用%G,这可能是我困惑的根源。下面的 sn-p 说明了这个问题:
strlen() {
FOO=$1
local invisible='%([BSUbfksu]|([FB]|){*})' # (1)
LEN=${#${(S%%)FOO//$~invisible/}}
echo $LEN
}
local blob="%{↓%G%}"
echo $blob $(strlen $blob) # (2) Unexpectedly gives 0
local blob="↓"
echo $blob $(strlen $blob) # (3) Gives the wanted output of 1
# but then this result would tell us to not use %G for unicode
strlen 函数来自this tentative explanation of counting user-visible string。不幸的是,对于invisible 部分 # (1) 没有明确的完整解释,也欢迎任何额外的参考/解释。
问题:我应该什么时候真正使用%G?还是我应该按照上面 sn-p 的建议放弃它?
【问题讨论】: