forked from veged/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 4
/
prompt.bash
134 lines (123 loc) · 3.18 KB
/
prompt.bash
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env bash
function __upwards {
local h
local t
h="$1"
t=""
[ "${h}" != "/" ] || h=""
while [ -n "${h}" ]; do
"${@:2}" "${h}" "${t}" || return 0
[ -z "${t}" ] || t="/${t}"
t="${h##*/}${t}"
h="${h%/*}"
done
"${@:2}" "${h}" "${t}" || return 1
return 0
}
function __prompt {
echo -n "$(__prompt_path "$1")"
}
function __prompt_path {
local path
local arc_path
local res
path="$1"
arc_path="$(__upwards "$(pwd)" __up_arcadia_path)"
path=${path%/}
if [ -z "${arc_path}" ]; then
echo -n "${path}"
else
res="${arc_path#/}"
[ -n "${res}" ] || res="^"
echo -n "${path%${arc_path}}/${res}"
fi
}
function __prompt_vcs {
__prompt_git=()
__prompt_git_bare=false
__prompt_arc=()
__prompt_arcanum=()
__upwards "$(pwd -P)" __up_vcs
for entry in "${__prompt_git[@]}"; do
echo -n " (${entry})"
done
for entry in "${__prompt_arc[@]}"; do
echo -n " (${entry})"
done
for entry in "${__prompt_arcanum[@]}"; do
echo -n " (${entry})"
done
}
function __up_arcadia_path {
if [ -f "$1/.arcadia.root" ]; then
echo "/$2"
return 1
fi
return 0
}
function __up_vcs {
local name
name="$1"
name="${name##*/}"
if [ -f "$1/HEAD" -a -d "$1/refs" -a -d "$1/objects" ]; then
__prompt_git+=("!$(__git_info "$1")")
__prompt_git_bare=true
fi
if ! ${__prompt_git_bare} && [ -d "$1/.git" ]; then
__prompt_git+=("$(__git_info "$1/.git")")
fi
if [ -d "$1/.arc" -a -f "$1/.arc/HEAD" ]; then
__prompt_arc+=("$(__arc_info "$1/.arc")")
fi
if [ -d "$1/.arcanum" -a -f "$1/.arcanum/pr" ]; then
__prompt_arcanum+=("$(__arcanum_info "$1/.arcanum")")
fi
return 0
}
function __git_info {
[ -f "$1/HEAD" ] || return 1
local git_head
local git_ref
git_head="$(cat "$1/HEAD" 2>/dev/null)"
if [ "${git_head:0:5}" = "ref: " ]; then
git_ref="${git_head#ref: }"
git_ref="${git_ref#refs/}"
echo -n "${git_ref}"
else
echo -n "[${git_head:0:7}]"
fi
}
function __arc_info {
[ -f "$1/HEAD" ] || return 1
local arc_remote
local arc_id
local arc_symbolic
while read -r line; do
if [ "${line:0:7}" == "Remote:" ]; then
arc_remote="${line#Remote: }"
elif [ "${line:0:3}" == "Id:" ]; then
arc_id="${line#Id: }"
elif [ "${line:0:9}" == "Symbolic:" ]; then
arc_symbolic="${line#Symbolic: }"
fi
done <<< "$(cat "$1/HEAD" 2>/dev/null)"
arc_remote="${arc_remote#\"}"
arc_remote="${arc_remote%\"}"
arc_id="${arc_id#\"}"
arc_id="${arc_id%\"}"
arc_symbolic="${arc_symbolic#\"}"
arc_symbolic="${arc_symbolic%\"}"
if [ -n "${arc_symbolic}" ]; then
echo -n "${arc_symbolic}"
elif [ -n "${arc_id}" ]; then
echo -n "[${arc_id}]"
fi
if [ -n "${arc_remote}" ]; then
echo -n ":${arc_remote}"
fi
}
function __arcanum_info {
[ -f "$1/pr" ] || return 1
echo -n "$(cat "$1/pr")"
}
export PS1="${HOSTNAME} \$(__prompt \"\w\")\[\033[34m\$(__prompt_vcs) \[\033[32m\]\$\[\033[00m\] "