bash/.bashrc.d/99-prompt.bash (view raw)
1red="\e[31m"
2grn="\e[32m"
3ylw="\e[33m"
4cyn="\e[36m"
5blu="\e[34m"
6prp="\e[35m"
7bprp="\e[35;1m"
8gry="\e[94m"
9rst="\e[0m"
10
11git_branch() {
12 [[ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" = "true" ]] && {
13 local git_status="$(git status 2> /dev/null)"
14 local on_branch="On branch ([^${IFS}]*)"
15 local on_commit="HEAD detached at ([^${IFS}]*)"
16 status="$(git status --porcelain 2> /dev/null)"
17 local exit="$?"
18
19 if [[ "$exit" -eq 0 ]]; then
20 if [[ "${#status}" -eq 0 ]]; then
21 color="${grn}"
22 sym="•"
23 else
24 color="${red}"
25 sym="×"
26 fi
27 else
28 printf ''
29 fi
30
31
32 if [[ $git_status =~ $on_branch ]]; then
33 local branch=${BASH_REMATCH[1]}
34 printf '%b' " ($branch $color$sym$rst)"
35 elif [[ $git_status =~ $on_commit ]]; then
36 local commit=${BASH_REMATCH[1]}
37 printf '%b' " ($commit $color$sym$rst)"
38 fi
39 }
40}
41
42short_pwd() {
43 wd=$(dirs +0)
44 sed 's:\([^/]\)[^/]*/:\1/:g' <<< "${wd/#$HOME/\~}"
45}
46
47prompt_pwd() {
48 printf '%b' "\001${cyn}\002$(short_pwd)\001${rst}\002"
49}
50
51rootornot() {
52 [[ "$(id -u)" -eq 0 ]] &&
53 printf '%b' "\001${red}\002#\001${rst}\002"
54}
55
56PS1='\n$(prompt_pwd)$(git_branch)\n▲$(rootornot) '
57PS2="> "