]> git.phdru.name Git - dotfiles.git/blob - admin/prog/bash_prompt
.shellrc: Do not reset `PS1`
[dotfiles.git] / admin / prog / bash_prompt
1 cgmem_which_prompt() {
2    local _cgmem_which
3    _cgmem_which="`cgmem_which 2>/dev/null`"
4    if [ -n "$_cgmem_which" ]; then
5       _cgmem_which=" $_cgmem_which"
6    fi
7    echo "$_cgmem_which"
8 }
9
10 last_cmd_status() {
11     local _last_status=$?
12     if [ $_last_status -eq 0 ]; then
13         echo '+'
14     elif [ $_last_status -ge 1 ]; then
15         echo '-'
16     else
17         echo '?'
18     fi
19 }
20
21 # Cut directories to 20% of the terminal width; add space for 3 dots
22 _DIR_LENGTH=`awk "END { print int(0.2 * ${COLUMNS:-80}) }" </dev/null`
23 _DIR_LENGTH_DOTS=`expr $_DIR_LENGTH + 3`
24
25 short_curdir() {
26    if [ "$PWD" = / ]; then
27       echo /
28       return
29    fi
30    if [ "$PWD" = "$HOME" ]; then
31       echo "~"
32       return
33    fi
34    local _short_curdir
35    _short_curdir="${PWD##*/}" # cut all directories, get base name
36    if [ "${#_short_curdir}" -gt $_DIR_LENGTH_DOTS ]; then
37       _short_curdir="${_short_curdir::$_DIR_LENGTH}..." # cut long string
38    fi
39    echo "${_short_curdir}"
40 }
41
42 # bash_prompt; adapted from
43 # https://github.com/necolas/dotfiles/blob/master/shell/bash_prompt and
44 # https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
45
46 prompt_git() {
47     local s=""
48     local branchName=""
49
50     # check if the current directory is in a git repository
51     if git rev-parse --is-inside-work-tree &>/dev/null; then
52
53         # check if the current directory is in .git before running git checks
54         if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == "false" ]; then
55
56             # ensure index is up to date
57             #git update-index --really-refresh -q &>/dev/null
58
59             # check for unstaged changes
60             if [ -n "$(git ls-files --modified :/)" ]; then
61                 s="$s*";
62             fi
63
64             # check for uncommitted changes in the index
65             if ! $(git diff --quiet --ignore-submodules --cached); then
66                 s="$s+";
67             fi
68
69             # check for stashed files
70             if $(git rev-parse --verify refs/stash &>/dev/null); then
71                 s="$s$";
72             fi
73
74             # check for untracked files
75             if [ -n "$(git ls-files --others --exclude-standard :/)" ]; then
76                 s="$s%";
77             fi
78
79         fi
80
81         # get the short symbolic ref
82         # if HEAD isn't a symbolic ref, get the short SHA
83         # otherwise, just give up
84         branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
85                       git rev-parse --short HEAD 2> /dev/null || \
86                       printf "(unknown)")"
87
88         up=`git rev-parse --abbrev-ref @{u} 2>/dev/null`
89         if [ -n "$up" -a "$up" != "@{u}" ]; then
90             local left right
91             set -- `git rev-list --count --left-right @{u}...HEAD`
92             left=$1
93             right=$2
94             if [ "$left" -gt 0 ]; then
95                 s="$s-$left"
96             fi
97             if [ "$right" -gt 0 ]; then
98                 s="$s+$right"
99             fi
100         fi
101
102         [ -n "$s" ] && s=" $s"
103         printf " (%s)" "$branchName$s"
104     fi
105 }
106
107 #if test -x /usr/bin/git >/dev/null 2>&1; then
108 #   if [ "`type -t __git_ps1`" != function ]; then
109 #      git_sh_prompt=`git --exec-path`/git-sh-prompt
110 #      test -r $git_sh_prompt && . $git_sh_prompt || :
111 #   fi
112 #   if [ "`type -t __git_ps1`" = function ]; then
113 #      GIT_PS1_SHOWDIRTYSTATE=true
114 #      GIT_PS1_SHOWSTASHSTATE=true
115 #      GIT_PS1_SHOWUNTRACKEDFILES=true
116 #      GIT_PS1_SHOWUPSTREAM=verbose
117 #   fi
118 #fi
119
120 set_prompt() {
121       PS1=''
122       local _SHORT_PROMPT='[\A`cgmem_which_prompt`] \u@${HOSTNAME::5}:`short_curdir`'
123       # display the user, host and current working directory
124       # in the terminal title
125       case "$TERM" in
126          *rxvt*|screen*|*term*|vt100)
127             PS1+="\033]0;${debian_chroot:+($debian_chroot)}\u@\h:\w\007" # Set xterm title/icon
128             case "$TERM" in
129                screen*)
130                   PS1+="\033P${PS1}\033\\\\" # Set xterm title/icon under screen/tmux
131                   if [ -z "$MC_SID" ]; then
132                      PS1+="\033k${_SHORT_PROMPT}\033\\\\" # Set screen/tmux caption
133                   fi
134                ;;
135             esac
136          ;;
137       esac
138
139    # This is for .screenrc: shelltitle "\$ |$SHELL"
140    #PS1+='\033k\033\\'
141
142    PS1="\[$PS1\]"
143    PS1+='${debian_chroot:+($debian_chroot)}'
144    PS1+=${_SHORT_PROMPT}
145    if test -x /usr/bin/git >/dev/null 2>&1; then
146       #if [ "`type -t __git_ps1`" = function ]; then
147       #   PS1+='$(__git_ps1)'
148       #else
149          PS1+='$(prompt_git)'
150       #fi
151    fi
152    PS1+=' $SHLVL\$ '
153 }