blob: 2faf38d6273e3c2a75d9553527228bb8292800d4 (
plain) (
blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
__tmp="`mktemp -d`"
__trylock() {
local dir
dir="$__tmp/$1.lock"
mkdir "$dir" &>/dev/null
return $?
}
__queue_init() {
local r
local w
mkdir "$__tmp/$1.read.lock" ; r=$?
mkdir "$__tmp/$1.write.lock" ; w=$?
if [ "$r" '=' '0' -a "$w" '=' '0' ] ; then
return 0
else
return 1
fi
}
__queue_try_read() {
__trylock "$1.read"
return $?
}
__queue_try_write() {
__trylock "$1.write"
return $?
}
__queue_make_readable() {
rm -r "$__tmp/$1.read.lock" &>/dev/null
return $?
}
__queue_make_writable() {
rm -r "$__tmp/$1.write.lock" &>/dev/null
return $?
}
__read() {
cat "$__tmp/$1" 2> /dev/null
return $?
}
__write() {
cat > "$__tmp/$1" 2> /dev/null
return $?
}
__revparse_head() {
head="`git -C /spack rev-parse $@ HEAD 2>/dev/null`"
result="$?"
if [ "$result" '!=' '0' ] ; then
head="`git --git-dir=/spack/.git \\
--work-tree=/spack rev-parse $@ HEAD 2>/dev/null`"
result="$?"
fi
echo "$head"
return $result
}
__git_head() {
head="`__revparse_head --abbrev-ref`"
if [ "$?" '=' '0' ] ; then
if [ "$head" '=' 'HEAD' ] ; then
head="`__revparse_head | cut -c1-8`..."
fi
echo "$head"
fi
}
__update_prompt() {
local prompt
prompt=''
linux_distro="$DISTRO"
if [ -n "$linux_distro" ] ; then
linux_distro='\[\e[1;34m\][\[\e[0;34m\]'"$linux_distro"'\[\e[1;34m\]]'
if [ -n "$prompt" ] ; then
prompt="$prompt "
fi
prompt="$prompt$linux_distro"
fi
git_head="`__git_head`"
if [ -n "$git_head" ] ; then
git_head='\[\e[1;32m\](\[\e[0;32m\]'"$git_head"'\[\e[1;32m\])'
if [ -n "$prompt" ] ; then
prompt="$prompt "
fi
prompt="$prompt$git_head"
fi
if [ -n "$prompt" ] ; then
prompt="$prompt "
fi
prompt="$prompt"'\[\e[0;m\]\W: '
echo "$prompt" | __write prompt
}
set -m
(
__queue_init query
__queue_init prompt
__update_prompt
__queue_make_readable prompt
__queue_make_writable query
while sleep 0.010 ; do
last_q_time=''
while sleep 0.010 ; do
q_time="`date +%s%N`"
if __queue_try_read query ; then
last_q_time="$q_time"
__queue_make_writable query
fi
if [ -n "$last_q_time" -a \
"$(( (q_time - last_q_time)/10000000 > 100 ))" '=' '1' ] ; then
break
fi
done
__update_prompt
__queue_make_readable prompt
done
) &>/dev/null &
set +m
__update_prompt_main_first_call=1
__update_prompt_main() {
if [ "$__update_prompt_main_first_call" '=' '1' ] ; then
while sleep 0.001 ; do
if __queue_try_read prompt ; then
PS1="`__read prompt`"
break
fi
done
__update_prompt_main_first_call=0
else
if __queue_try_read prompt ; then
PS1="`__read prompt`"
fi
fi
if __queue_try_write query ; then
__queue_make_readable query
fi
}
PROMPT_COMMAND=__update_prompt_main
|