-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_ps1
52 lines (45 loc) · 1.35 KB
/
bash_ps1
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
PS0='$(perl -MTime::HiRes=time -e '"'"'printf "%.3f\n", time'"'"' > /tmp/cmd_start_$$)'
format_duration() {
local ms=$1
if [ $ms -lt 1000 ]; then
printf "%dms" $ms
return
fi
local total_seconds=$(($ms / 1000))
local millis=$(($ms % 1000))
local hours=$(($total_seconds / 3600))
local minutes=$((($total_seconds % 3600) / 60))
local seconds=$(($total_seconds % 60))
if [ $hours -gt 0 ]; then
printf "%dh%dm%d.%03ds" $hours $minutes $seconds $millis
elif [ $minutes -gt 0 ]; then
printf "%dm%d.%03ds" $minutes $seconds $millis
else
if [ $millis -eq 0 ]; then
printf "%ds" $seconds
else
printf "%d.%03ds" $seconds $millis
fi
fi
}
get_formatted_path() {
echo "$PWD" | sed "s|^$HOME|~|"
}
precmd() {
local EXIT="$?"
local duration="0ms"
if [ -f /tmp/cmd_start_$$ ]; then
local start=$(<"/tmp/cmd_start_$$")
local current=$(perl -MTime::HiRes=time -e 'printf "%.3f\n", time')
local ms=$(printf "%.0f" $(echo "($current - $start) * 1000" | bc))
duration=$(format_duration $ms)
rm -f /tmp/cmd_start_$$
fi
local PROMPT_COLOR=\\[\\e[32m\\]
[[ "$EXIT" != "0" ]] && PROMPT_COLOR=\\[\\e[31m\\]
PS1="\e[1m\[\e[34m\]$(get_formatted_path)\[\e[32m\]$(parse_git_branch)\[\e[0m\] ⏱ ${duration}\n${PROMPT_COLOR}❯\[\e[0m\] "
}
PROMPT_COMMAND=precmd
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}