我在OsX上定製了我的bash提示,包括git分支和分支狀態的一些標記。這打破了換行。破碎的bash提示包裝線
我知道I have to add \[ and \] to prevent this issue,但在函數中這樣做確實顯示\ [和\] litteraly。
我能做些什麼來逃避這些功能的序列?
免責聲明:這些是我第一次嘗試bash腳本。
function parse_git_dirty {
# TODO make git status response a variable
# [branch+] : working dir has staged changes
if [[ $(git status 2> /dev/null | grep "to be committed") ]]
then S=$S"$(tput setaf 2)+$(tput sgr0)"
fi
# [branch+] : working dir has unstaged changes
if [[ $(git status 2> /dev/null | grep "not staged for commit") ]]
then S=$S"$(tput setaf 1)+$(tput sgr0)"
fi
# [branch+] : working dir has untracked files
if [[ $(git status 2> /dev/null | grep "tracked files") ]]
then S=$S"$(tput setaf 1)+$(tput sgr0)"
fi
# [branch<] : local branch is behind origin
if [[ $(git status 2> /dev/null | grep "Your branch is behind") ]]
then S=$S"$(tput setaf 5)<$(tput sgr0)"
fi
# [branch>] : local branch is ahead origin
if [[ $(git status 2> /dev/null | grep "branch is ahead of") ]]
then S=$S"$(tput setaf 5)>$(tput sgr0)"
fi
# [branch<>] : branches have diverged
if [[ $(git status 2> /dev/null | grep "have diverged") ]]
then S=$S"$(tput setaf 5)<>$(tput sgr0)"
fi
echo $S
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function show_git_branch {
if [[ $(parse_git_branch) ]]
then echo "$(tput setaf 2)($(tput sgr0)$(parse_git_branch)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)"
fi
}
export PS1="\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[\$(show_git_branch)\] "
不確定問題是什麼,你的代碼似乎正常工作,使用它我得到以下提示,hutcho @ hutcho-M17x:math(master +)。順便說一句,這是一個很酷的想法。 –
我有一些(不是所有不常見的)存儲庫,其中'git status'需要大約40秒才能完成,並且您的代碼將爲每個bash提示符運行'git status'六次!我在下面添加了一個答案,提到'__git_ps1',它可能做你想做的事情,並且可以在輸出中針對不同級別的細節進行配置。 –