Bash 和 True Color

Bash 和 True Color

我正在尝试构建一个具有真彩色的简单 bash 提示符,并已使用如下代码尝试了所有操作 - 没有看到问题所在,需要脚本专家提供信息:)

#set your colors
pipe_color='ff;ff;ff'
pipe_bg_color='0;0;0'
username_color='0;0;0'
username_bg_color='c0;c5;ce'
at_color='ff;ff;ff'
at_bg_color='41;4a;4c'
host_color='ff;ff;ff'
host_bg_color='00;5b;96'
workingdir_color='ff;ff;ff'
workingdir_bg_color='36;80;2d'


# leave this block alone
pipe_color_set="\x1b[38;2;${pipe_color}m"
pipe_bg_color_set="\x1b[48;2;${pipe_bg_color}m"
username_color_set="\x1b[38;2;${username_color}m"
username_bg_color_set="\x1b[48;2;${username_bg_color}m"
at_color_set="\x1b[38;2;${at_color}m"
at_bg_color_set="\x1b[48;2;${at_bg_color}m"
host_color_set="\x1b[38;2;${host_color}m"
host_bg_color_set="\x1b[48;2;${host_bg_color}m"
workingdir_color_set="\x1b[38;2;${workingdir_color}m"
workingdir_bg_color_set="\x1b[48;2;${workingdir_bg_color}m"
color_reset_set='\x1b[0m'

# leave this block alone
pipe=$(printf "${pipe_color_set}")
pipebg=$(printf "${pipe_bg_color_set}")
username=$(printf "${username_color_set}")
usernamebg=$(printf "${username_bg_color_set}")
at=$(printf "${at_color_set}")
atbg=$(printf "${at_bg_color_set}")
host=$(printf "${host_color_set}")
hostbg=$(printf "${host_bg_color_set}")
workingdir=$(printf "${workingdir_color_set}")
workingdirbg=$(printf "${workingdir_bg_color_set}")
colorreset=$(printf "${color_reset_set}")

# your PS1 prompt.  configure as desired
export PS1='\[${pipe}${pipebg}\]|\[${username}${usernamebg}\][\u]\[${pipe}${pipebg}\]|\[${at}${atbg}\]@\[${pipe}${pipebg}\]|\[${host}${hostbg}\][\h]\[${pipe}${pipebg}\]|\[${colorreset}\] '

答案1

ANSI 颜色代码使用十进制,而不是十六进制。

例如,白色是255;255;255(即\e[38;2;255;255;255m),而“工作目录背景”深绿色是54;128;45(即\e[38;2;54;128;45m)。

另外:这里不用使用 printf – 您可以直接在 PS1 中输入转义代码(例如\x1b或 )\e,bash 本身会在显示提示符时自动扩展它们。(在您的例子中,将 PS1 的单引号替换为双引号。)

pipe_color='255;255;255'
pipe_bg_color='0;0;0'
username_color='0;0;0'
username_bg_color='192;197;206'
at_color='255;255;255'
at_bg_color='65;74;76'
host_color='255;255;255'
host_bg_color='0;91;150'
workingdir_color='255;255;255'
workingdir_bg_color='54;128;45'

pipe="\e[38;2;${pipe_color}m"
pipe_bg="\e[48;2;${pipe_bg_color}m"
username="\e[38;2;${username_color}m"
username_bg="\e[48;2;${username_bg_color}m"
at="\e[38;2;${at_color}m"
at_bg="\e[48;2;${at_bg_color}m"
host="\e[38;2;${host_color}m"
host_bg="\e[48;2;${host_bg_color}m"
workingdir="\e[38;2;${workingdir_color}m"
workingdir_bg="\e[48;2;${workingdir_bg_color}m"
color_reset='\e[0m'

PS1="\[${pipe}${pipe_bg}\]|\[${username}${username_bg}\][\u]\[${pipe}${pipe_bg}\]|\[${at}${at_bg}\]@\[${pipe}${pipe_bg}\]|\[${host}${host_bg}\][\h]\[${pipe}${pipe_bg}\]|\[${color_reset}\] "

相关内容