在这个线程中,尤达建议使用以下颜色的解决方案zsh
#load colors
autoload colors && colors
for COLOR in RED GREEN YELLOW BLUE MAGENTA CYAN BLACK WHITE; do
eval $COLOR='%{$fg_no_bold[${(L)COLOR}]%}' #wrap colours between %{ %} to avoid weird gaps in autocomplete
eval BOLD_$COLOR='%{$fg_bold[${(L)COLOR}]%}'
done
eval RESET='$reset_color'
Run Code Online (Sandbox Code Playgroud)
如果我错了,请纠正我,但如果我理解正确,则autoload colors && colors允许您按名称调用颜色,而脚本的其余部分只是将它们包装在${ $}.
这让我想到了以下问题:
autoload colors && colors?该colors函数在关联数组中记录颜色名称和相似属性(粗体、下划线等)color。该数组将名称与终端属性字符串相关联,这些字符串是数字,例如00?normal, 42? bg-green, ...
echo ${(o)color}
Run Code Online (Sandbox Code Playgroud)
如果要查看数组是如何构建的,请查看函数的来源:which colors或less $^fpath/colors(N)。
该colors函数仅定义8 种标准颜色的名称和转义字符串(在关联数组fg和 中bg)。您的终端可能有更多。有关如何探索可用颜色的信息,请参阅此答案。