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