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