Bash 背景颜色延伸至行尾的提示
Bash prompt with background color extending to end of line
对我来说 bash-prompt-fiddling-time(必须有一天...)。
我正在尝试获取两行提示:
- 第 1 行包含位置信息,背景颜色到行尾
- 第 2 行包含时间和上一个命令的退出代码
我快到了,但我无法破解 "background color to the end of line" 部分。不完全是。
将来自多个来源的信息汇集在一起,最重要的是来自 here and here, I get this result (terminal screenshot)。
如您所见,COLUMNS 计算出了点问题:
- 没有到达行尾
- 这取决于第一行的文本长度
- 到达终端底部时情况变得更糟;然后它确实到达了行尾;错误的行...
- 另一个奇怪的事情:第二个提示行中的小 [ ];那些只有在输入命令后才会出现
这是我的 bashrc 代码:
PROMPT_COMMAND=__prompt_command
__prompt_command()
{
local EXIT="$?"
local Red='\[3[1;38;5;9m\]'
local Green='\[3[1;38;5;10m\]'
local Gray='\[3[0;38;5;248m\]'
local BgBlue='\[3[48;5;253;1;38;5;12m\]'
local BgPurple='\[3[48;5;253;1;38;5;93m\]'
local None='\[\e[0m\]'
PS1="${BgPurple}\u@\h:${BgBlue}$PWD"
printf -v TC_SPC "%${COLUMNS}s" ''
COLUMNS=$(tput cols)
PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\(.\{${COLUMNS}\}\) */\1/"`
PS1+="\n${Gray}\D{%F %T}"
if [ $EXIT != 0 ]; then
PS1+=" ${Red} O_o ${None}" # Add red if exit code non 0
else
PS1+="${Green} ^_^ ${None}"
fi
}
我尝试了更多的黑客攻击但没有成功。
哦,还有一个更复杂的sed位版本,我也试过了:
PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\(\(\o33\[[0-9;]*[a-zA-Z]\)*\)\([^\o033]\{${COLUMNS}\}\) */\1\3/"`
Different result (terminal screenshot)但还是不行。
此时我正在寻求帮助!
而不是:
printf -v TC_SPC "%${COLUMNS}s" ''
COLUMNS=$(tput cols)
PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\(.\{${COLUMNS}\}\) */\1/"`
使用:
PS1+=$'3[K' #erase to end of line
感谢 Eric 的 "erase to end of line" 提示,这是可行的解决方案。
PROMPT_COMMAND=__prompt_command # Func to gen PS1 after CMDs
__prompt_command()
{
local EXIT="$?" # This needs to be first (retrieves last commmand exit code)
local Red='\[3[1;38;5;9m\]'
local Green='\[3[1;38;5;10m\]'
local Gray='\[3[0;38;5;248m\]'
local BgBlue='\[3[48;5;253;1;38;5;12m\]'
local BgPurple='\[3[48;5;253;1;38;5;93m\]'
local None='\[\e[0m\]'
PS1="${BgPurple}\u@\h:${BgBlue}$PWD"
PS1+="3[K" # erase to end of 1st line (background color stays)
PS1+="\n${Gray}\D{%F %T}\a"
if [ $EXIT != 0 ]; then
PS1+="${Red} O_o ${None}" # Add red if exit code non 0
else
PS1+="${Green} ^_^ ${None}"
fi
PS1+="3[K" # erase to end of 2nd line (no more background color)
}
和here is the result (terminal screenshot)。又一位快乐的提示所有者...
对我来说 bash-prompt-fiddling-time(必须有一天...)。
我正在尝试获取两行提示:
- 第 1 行包含位置信息,背景颜色到行尾
- 第 2 行包含时间和上一个命令的退出代码
我快到了,但我无法破解 "background color to the end of line" 部分。不完全是。
将来自多个来源的信息汇集在一起,最重要的是来自 here and here, I get this result (terminal screenshot)。
如您所见,COLUMNS 计算出了点问题:
- 没有到达行尾
- 这取决于第一行的文本长度
- 到达终端底部时情况变得更糟;然后它确实到达了行尾;错误的行...
- 另一个奇怪的事情:第二个提示行中的小 [ ];那些只有在输入命令后才会出现
这是我的 bashrc 代码:
PROMPT_COMMAND=__prompt_command
__prompt_command()
{
local EXIT="$?"
local Red='\[3[1;38;5;9m\]'
local Green='\[3[1;38;5;10m\]'
local Gray='\[3[0;38;5;248m\]'
local BgBlue='\[3[48;5;253;1;38;5;12m\]'
local BgPurple='\[3[48;5;253;1;38;5;93m\]'
local None='\[\e[0m\]'
PS1="${BgPurple}\u@\h:${BgBlue}$PWD"
printf -v TC_SPC "%${COLUMNS}s" ''
COLUMNS=$(tput cols)
PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\(.\{${COLUMNS}\}\) */\1/"`
PS1+="\n${Gray}\D{%F %T}"
if [ $EXIT != 0 ]; then
PS1+=" ${Red} O_o ${None}" # Add red if exit code non 0
else
PS1+="${Green} ^_^ ${None}"
fi
}
我尝试了更多的黑客攻击但没有成功。
哦,还有一个更复杂的sed位版本,我也试过了:
PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\(\(\o33\[[0-9;]*[a-zA-Z]\)*\)\([^\o033]\{${COLUMNS}\}\) */\1\3/"`
Different result (terminal screenshot)但还是不行。
此时我正在寻求帮助!
而不是:
printf -v TC_SPC "%${COLUMNS}s" ''
COLUMNS=$(tput cols)
PS1=`echo $PS1 | sed "s/$/$TC_SPC/; s/^\(.\{${COLUMNS}\}\) */\1/"`
使用:
PS1+=$'3[K' #erase to end of line
感谢 Eric 的 "erase to end of line" 提示,这是可行的解决方案。
PROMPT_COMMAND=__prompt_command # Func to gen PS1 after CMDs
__prompt_command()
{
local EXIT="$?" # This needs to be first (retrieves last commmand exit code)
local Red='\[3[1;38;5;9m\]'
local Green='\[3[1;38;5;10m\]'
local Gray='\[3[0;38;5;248m\]'
local BgBlue='\[3[48;5;253;1;38;5;12m\]'
local BgPurple='\[3[48;5;253;1;38;5;93m\]'
local None='\[\e[0m\]'
PS1="${BgPurple}\u@\h:${BgBlue}$PWD"
PS1+="3[K" # erase to end of 1st line (background color stays)
PS1+="\n${Gray}\D{%F %T}\a"
if [ $EXIT != 0 ]; then
PS1+="${Red} O_o ${None}" # Add red if exit code non 0
else
PS1+="${Green} ^_^ ${None}"
fi
PS1+="3[K" # erase to end of 2nd line (no more background color)
}
和here is the result (terminal screenshot)。又一位快乐的提示所有者...