forked from MikeMcQuaid/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash_prompt.sh
79 lines (63 loc) · 2.16 KB
/
bash_prompt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# bash_prompt
# Example:
# thompsongl@gregorys-mbp: ~/src/dotfiles (boxen)[+!?$]
# $
get_git_branch() {
local branch_name
# Get the short symbolic ref
branch_name=$(git symbolic-ref --quiet --short HEAD 2> /dev/null) ||
# If HEAD isn't a symbolic ref, get the short SHA
branch_name=$(git rev-parse --short HEAD 2> /dev/null) ||
# Otherwise, just give up
branch_name="(unknown)"
printf $branch_name
}
prompt_git() {
local s=""
local branchName=""
# check if the current directory is in a git repository
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; printf "%s" $?) == 0 ]; then
# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == "false" ]; then
# ensure index is up to date
git update-index --really-refresh -q &>/dev/null
# check for uncommitted changes in the index
if ! $(git diff --quiet --ignore-submodules --cached); then
s="$s+";
fi
# check for unstaged changes
if ! $(git diff-files --quiet --ignore-submodules --); then
s="$s!";
fi
# check for untracked files
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s="$s?";
fi
# check for stashed files
if $(git rev-parse --verify refs/stash &>/dev/null); then
s="$s$";
fi
fi
# get the short symbolic ref
# if HEAD isn't a symbolic ref, get the short SHA
# otherwise, just give up
branchName=$(get_git_branch)
[ -n "$s" ] && s="[$s]"
printf "%s" "$1($branchName)$s"
else
return
fi
}
function EXT_COLOR () { echo -ne "\[\033[38;5;$1m\]"; }
GRAY="`EXT_COLOR 242`"
RESET="\[\033[0m\]"
PS1="\n" # Newline
PS1+="${GRAY}\u" # Username
PS1+="${GRAY}@" # @
PS1+="${GRAY}\h" # Host
PS1+="${GRAY}: " # :
PS1+="${GRAY}\w" # Working directory
PS1+="\$(prompt_git \"${GRAY} ${GRAY}\")" # git repository details
PS1+="\n" # Newline
PS1+="\[${RESET}\]\$ " # $ (and reset color)