我在OsX上自定义我的bash提示符,包括git branch以及分支状态的一些标记.这打破了换行.
我知道我必须添加\ [和\]来防止这个问题,但在函数中这样做会显示\ [和\] 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)\] "
Run Code Online (Sandbox Code Playgroud)
我很高兴听到你已经解决了你的版本的问题,但我认为值得指出git已经分发了一个有用的,经过深思熟虑的bash函数__git_ps1,你可以将其包含在你的版本中PS1.例如,您可以像这样使用它:
export PS1='blah blah blah$(__git_ps1 " (%s)") '
Run Code Online (Sandbox Code Playgroud)
如果您不在git存储库中,$(__git_ps1 " (%s)")则会变为空字符串.但是,如果您是,那么将使用格式字符串.这通常会显示您当前的分支,但是如果您正处于合并或者将会显示的rebase中间.
默认情况下,__git_ps1不会显示树是否为脏或有未跟踪的文件,因为在某些存储库中,这可能会使您的bash提示出现极其缓慢.但是,如果你想看到这个信息,以及,它会告诉他们,如果你设置GIT_PS1_SHOWDIRTYSTATE或GIT_PS1_SHOWUNTRACKEDFILES到一些非空.
您可以在git-completion.sh源文件的顶部找到更多信息.
| 归档时间: |
|
| 查看次数: |
2071 次 |
| 最近记录: |