]> git.phdru.name Git - dotfiles.git/blob - admin/prog/bash_prompt
Feat(recode-filenames-recursive): Allow to omit parameters
[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 _recurse_submodules() {
47     local command="$@"
48     local output="$($command 2>/dev/null)"
49     if [ -n "$output" ]; then
50         echo -n "$output"
51         return
52     fi
53     git submodule --quiet foreach $command 2>/dev/null
54 }
55
56 prompt_git() {
57     local s=""
58     local branchName=""
59
60     # check if the current directory is in a git repository
61     if git rev-parse --is-inside-work-tree &>/dev/null; then
62
63         # check if the current directory is in .git before running git checks
64         if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == "false" ]; then
65
66             # ensure index is up to date
67             #git update-index --really-refresh -q &>/dev/null
68
69             # check for unstaged changes
70             if [ -n "$(_recurse_submodules git ls-files --modified :/)" ]; then
71                 s="$s*";
72             fi
73
74             # check for uncommitted changes in the index
75             if [ -n "$(_recurse_submodules git diff --ignore-submodules --cached)" ]; then
76                 s="$s+";
77             fi
78
79             # check for stashed files
80             if [ -n "$(_recurse_submodules git rev-parse --verify refs/stash)" ]; then
81                 s="$s$";
82             fi
83
84             # check for untracked files
85             if [ -n "$(_recurse_submodules git ls-files --others --exclude-standard :/)" ]; then
86                 s="$s%";
87             fi
88
89         fi
90
91         # get the short symbolic ref
92         # if HEAD isn't a symbolic ref, get the short SHA
93         # otherwise, just give up
94         branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
95                       git rev-parse --short HEAD 2> /dev/null || \
96                       printf "(unknown)")"
97
98         up=`git rev-parse --abbrev-ref @{u} 2>/dev/null`
99         if [ -n "$up" -a "$up" != "@{u}" ]; then
100             local left right
101             set -- `git rev-list --count --left-right @{u}...HEAD`
102             left=$1
103             right=$2
104             if [ "$left" -gt 0 ]; then
105                 s="$s-$left"
106             fi
107             if [ "$right" -gt 0 ]; then
108                 s="$s+$right"
109             fi
110         fi
111
112         [ -n "$s" ] && s=" $s"
113         printf " (%s)" "$branchName$s"
114     fi
115 }
116
117 #if test -x /usr/bin/git >/dev/null 2>&1; then
118 #   if [ "`type -t __git_ps1`" != function ]; then
119 #      git_sh_prompt=`git --exec-path`/git-sh-prompt
120 #      test -r $git_sh_prompt && . $git_sh_prompt || :
121 #   fi
122 #   if [ "`type -t __git_ps1`" = function ]; then
123 #      GIT_PS1_SHOWDIRTYSTATE=true
124 #      GIT_PS1_SHOWSTASHSTATE=true
125 #      GIT_PS1_SHOWUNTRACKEDFILES=true
126 #      GIT_PS1_SHOWUPSTREAM=verbose
127 #   fi
128 #fi
129
130 set_prompt() {
131       PS1=''
132       local _SHORT_PROMPT='[\A`cgmem_which_prompt`] \u@${HOSTNAME::5}:`short_curdir`'
133       # display the user, host and current working directory
134       # in the terminal title
135       case "$TERM" in
136          *rxvt*|screen*|*term*|vt100)
137             PS1+="\033]0;${debian_chroot:+($debian_chroot)}\u@\h:\w\007" # Set xterm title/icon
138             case "$TERM" in
139                screen*)
140                   PS1+="\033P${PS1}\033\\\\" # Set xterm title/icon under screen/tmux
141                   if [ -z "$MC_SID" ]; then
142                      PS1+="\033k${_SHORT_PROMPT}\033\\\\" # Set screen/tmux caption
143                   fi
144                ;;
145             esac
146          ;;
147       esac
148
149    # This is for .screenrc: shelltitle "\$ |$SHELL"
150    #PS1+='\033k\033\\'
151
152    PS1="\[$PS1\]"
153    PS1+='${debian_chroot:+($debian_chroot)}'
154    PS1+=${_SHORT_PROMPT}
155    if test -x /usr/bin/git >/dev/null 2>&1; then
156       #if [ "`type -t __git_ps1`" = function ]; then
157       #   PS1+='$(__git_ps1)'
158       #else
159          PS1+='$(prompt_git)'
160       #fi
161    fi
162    PS1+=' $SHLVL\$ '
163 }