forked from mathiasbynens/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.bash_prompt
More file actions
100 lines (78 loc) · 2.53 KB
/
.bash_prompt
File metadata and controls
100 lines (78 loc) · 2.53 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function parse_git_branch() {
local s='';
local branchName='';
# Check if the current directory is in a Git repository.
if [ $(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}") = '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 the 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+='+';
fi;
# Check for unstaged changes.
if ! $(git diff-files --quiet --ignore-submodules --); then
s+='!';
fi;
# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s+='?';
fi;
# Check for stashed files.
if $(git rev-parse --verify refs/stash &>/dev/null); then
s+='$';
fi;
fi;
# Get the short symbolic ref.
# If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
# Otherwise, just give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')";
# branchName is greater than 30 characters then truncate it to 30 and add ....
if [ ${#branchName} -gt 30 ]; then
branchName="${branchName:0:30}....";
fi;
echo -e "[${1}${branchName}${blue}${s}]";
else
return;
fi;
}
function parse_hg_dirty() {
hg status 2> /dev/null | awk '$1 == "?" { print "?" } $1 != "?" { print "!" }' | sort | uniq | head -c1
}
function parse_hg_branch {
hg branch 2> /dev/null | sed -e "s/\(.*\)/[\1$(parse_hg_dirty)]/"
}
function last_two_dirs {
pwd |rev| awk -F / '{print $1,$2}' | rev | sed s_\ _/_
}
function last_dir {
pwd |rev| awk -F / '{print $1}' | rev
}
function parse_hg_git_branch {
([ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1) && is_repo_git=true || is_repo_git=false
([ -d .hg ] || hg branch > /dev/null 2>&1) && is_repo_hg=true || is_repo_hg=false
if $is_repo_git && $is_repo_hg
then
echo $(parse_hg_branch)/$(parse_git_branch)
elif $is_repo_hg
then
echo $(parse_hg_branch)
elif $is_repo_git
then
echo $(parse_git_branch)
fi
}
#Customize the prompt
if [ -n "$BASH_VERSION" ]; then
PS1="\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\$(last_dir)\[\033[00m\]\$(parse_hg_git_branch)"
case `id -u` in
0) PS1="${PS1}# ";;
*) PS1="${PS1}$ ";;
esac
elif [ -n "$ZSH_VERSION" ]; then
setopt PROMPT_SUBST
PROMPT='%F{green}%n%f:%F{blue}$(last_dir)%f$(parse_hg_git_branch)%# '
fi