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