git - broken bash prompt wrap line -
i'm customizing bash prompt on osx include git branch plus marks of branch state. breaks line wrap.
i know i have add \[ , \] prevent issue, doing in functions display \[ , \] litteraly.
what can escape such sequences in functions?
disclaimer: first attempts in bash scripting.
function parse_git_dirty { # todo make git status response variable # [branch+] : working dir has staged changes if [[ $(git status 2> /dev/null | grep "to committed") ]] s=$s"$(tput setaf 2)+$(tput sgr0)" fi # [branch+] : working dir has unstaged changes if [[ $(git status 2> /dev/null | grep "not staged commit") ]] s=$s"$(tput setaf 1)+$(tput sgr0)" fi # [branch+] : working dir has untracked files if [[ $(git status 2> /dev/null | grep "tracked files") ]] s=$s"$(tput setaf 1)+$(tput sgr0)" fi # [branch<] : local branch behind origin if [[ $(git status 2> /dev/null | grep "your branch behind") ]] s=$s"$(tput setaf 5)<$(tput sgr0)" fi # [branch>] : local branch ahead origin if [[ $(git status 2> /dev/null | grep "branch ahead of") ]] s=$s"$(tput setaf 5)>$(tput sgr0)" fi # [branch<>] : branches have diverged if [[ $(git status 2> /dev/null | grep "have diverged") ]] 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) ]] 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)\] "
i glad hear you've solved problem version, thought might worth pointing out git distributed helpful , thought out bash function called __git_ps1
can include in ps1
. example, use this:
export ps1='blah blah blah$(__git_ps1 " (%s)") '
if you're not in git repository, $(__git_ps1 " (%s)")
turn empty string. if are, however, format string used. show current branch, if you're in middle of merge or rebase shown instead.
by default __git_ps1
won't show whether tree dirty or there untracked files, since in repositories make irritatingly slow bash prompt appear. however, if want see information well, it'll show them if set git_ps1_showdirtystate
or git_ps1_showuntrackedfiles
non-empty.
you can find more information @ top of git-completion.sh source file.
Comments
Post a Comment