summaryrefslogtreecommitdiff
path: root/prompt
blob: 777f98a8ef0593a5ec6e889014a5e61c137d3d94 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
##################################################################### CONFIG
default_user=lvv  # default user is not displayed
default_host="ahp"      # default host is not displayed
default_domain="lvvnet" # default domain is not deplayed, remote host is alwais shown
 
    # dir, rc, root color 
if [ "`tput colors`" -ge 8 ];  then #  Colors
dir_color='CYAN'
rc_color='red'
root_id_color='magenta'
else #  B/W
dir_color='bw_bold'
rc_color='bw_bold'
fi
 
                                                    # where is user color?
 
    # host color
    TOSHA_host_color='yellow'
    TASHA_host_color='cyan'
       AL_host_color='geen'
       SH_host_color='blue'
      LVV_host_color='blue'
      AHP_host_color='white'
 
    # git color     
    git_clean=blue         # nothing to commit (working directory clean)
    git_modified=red       # # Changed but not updated:
    git_added=green        # # Changes to be committed:
    git_untracked=BLUE     # # Untracked files:
 
 
##################################################################### 
    LC_ALL=C
# if label non empty, append 1 space
label=${1:+$1 }
 
 
# echo "*** /etc/prompt  on A,  TERM=$TERM"
unset PROMPT_COMMAND
 
#######  work aroud for MC bug
if [ -z "$TERM" -o "$TERM" = "dumb" -o -n "$MC_SID" ]; then
unset PROMPT_COMMAND
PS1='\w> '
return 0
fi
 
export who_where
 
 
set_shell_title() { 
 
xterm_title() { echo  -n "]2;${@}" ; }
 
screen_title() { 
FIXME: run this only if screen is in xterm (how to test for this?)
xterm_title  "sCRn  $label$plain_who_where $@" 
 
FIXME $STY not enherited though "su -"
[ "$STY" ] && screen -S $STY -X title "$@"
}
 
case $TERM in
 
screen*)                                                    
screen_title "$@"
;;
 
xterm* | rxvt* | gnome-terminal | konsole | eterm | wterm )       
# is there a capability which we can to test 
# for "set term title-bat" and its escapes?
#echo -n "]2;$label$plain_who_where $1"
xterm_title  "$label$plain_who_where $@"
;;
 
*)                                                     
;;
esac
}
 
export -f set_shell_title
 
###################################################### ID (user name)
id=`id -un`
id=${id#$default_user}
 
###################################################### TTY
tty=`tty`
tty=`echo $tty | sed "s:/dev/pts/:p:; s:/dev/tty::" ` # RH tty devs
tty=`echo $tty | sed "s:/dev/vc/:vc:" ` # gentoo tty devs
 
if [[ "$TERM" = "screen" ]] ;  then
 
# [ "$WINDOW" = "" ] && WINDOW="?"
#
# # if under screen then make tty name look like s1-p2
# # tty="${WINDOW:+s}$WINDOW${WINDOW:+-}$tty"
# tty="${WINDOW:+s}$WINDOW"  # replace tty name with screen number
tty="$WINDOW"  # replace tty name with screen number
fi
 
# we don't need tty name under X11
case $TERM in
xterm* | rxvt* | gnome-terminal | konsole | eterm | wterm )  unset tty ;;
*)                                                               ;;
esac
 
########################################################## ANSI 
FIXME: color config should be at the top
#
#   black?    0 8   
# red       1 9
# green     2 10
# yellow    3 11
# blue      4 12
# magenta   5 13
# cyan      6 14
# white     7 15
#
# terminfo setaf/setab - sets ansi foreground/background
# terminfo sgr0 - resets all atributes
# terminfo colors - number of colors
 
### if term support colors,  then use color prompt, else bold
 
              black='\['`tput sgr0; tput setaf 0`'\]'
                red='\['`tput sgr0; tput setaf 1`'\]'
              green='\['`tput sgr0; tput setaf 2`'\]'
             yellow='\['`tput sgr0; tput setaf 3`'\]'
               blue='\['`tput sgr0; tput setaf 4`'\]'
            magenta='\['`tput sgr0; tput setaf 5`'\]'
               cyan='\['`tput sgr0; tput setaf 6`'\]'
              white='\['`tput sgr0; tput setaf 7`'\]'
 
              BLACK='\['`tput setaf 0; tput bold`'\]'
                RED='\['`tput setaf 1; tput bold`'\]'
              GREEN='\['`tput setaf 2; tput bold`'\]'
             YELLOW='\['`tput setaf 3; tput bold`'\]'
               BLUE='\['`tput setaf 4; tput bold`'\]'
            MAGENTA='\['`tput setaf 5; tput bold`'\]'
               CYAN='\['`tput setaf 6; tput bold`'\]'  # why 14 dosn't work?
              WHITE='\['`tput setaf 7; tput bold`'\]'
 
            bw_bold='\['`tput bold`'\]'
               bell=`tput bel`
 
       colors_reset='\['`tput sgr0`'\]'
 
# Workaround for UTF readline(?) bug. Dissable bell when UTF
locale |grep -qi UTF && bell=''
 
 
    eval dir_color="\$$dir_color"
    eval rc_color="\$$rc_color"
    eval root_id_color="\$$root_id_color"
 
 
########################################################### HOST
### we don't display home host/domain  $SSH_* set by SSHD or keychain
 
# is sshd our perent?
if  [[ "$SSH_AUTH_SOCK" ]] || 
for ((pid=$$; $pid != 1 ; pid=`ppid_of $pid`)); do ps h -o command -p $pid; done | grep -q sshd
then 
host=${HOSTNAME}
#host=`hostname --short`
#host=`echo ${host%$default_host} | tr a-z A-Z`
host=`echo ${host} | tr a-z A-Z`
        eval host_color=\$${host}_host_color
else
host=""
fi 
 
    eval host_color="\$$host_color"
 
# we already should have short host name, but just in case
host=${host%.localdoman}
host=${host%.$default_domain}
 
 
########################################################### WHO_WHERE 
# who_ware. Is constant.  Looks like
#  [user@]host[-tty]
 
color_who_where="${id:+$id@}$host_color$host${tty:+ $tty}"
plain_who_where="${id:+$id@}$host"
 
# remove trailing "@" if any
color_who_where="${color_who_where%@}"
plain_who_where="${plain_who_where%@}"
 
# add traling " "
color_who_where="$color_who_where "
plain_who_where="$plain_who_where "
 
# if  $who_where==" "  then  who_where=""
color_who_where="${color_who_where## }"
plain_who_where="${plain_who_where## }"
 
 
# if root then highlight who_where
if [ "$id" == "root" ]  ; then 
color_who_where="$root_id_color$color_who_where$colors_reset"
fi
 
 
############################################################### PROMPT_COMMAND
 
PROMPT_COMMAND='
 
rc="$?"
 
if [[ "$rc" = "0" ]]; then 
rc=""
else
#rc="$rc_color$rc$colors_reset$bell "
rc="$rc_color$rc$colors_reset "
fi
 
set_shell_title "$PWD/" 
 
# truncate $PWD to $max
max=35
front=7
head=${PWD:0:$front}"..."
 
    # LOCAL 
        # GIT 
            if [[ -d .git  ||  -d ../.git  || -d ../../.git ]];   then
 
                ### status
                # clean(blue)       nothing to commit (working directory clean)
                # modified(red)     # Changed but not updated:
                # added(green)      # Changes to be committed:
                # untracked(yellow) # Untracked files:
                
                eval `git status |
                        sed -n "
                            s/^# On branch /branch=/p
                            s/^nothing to commit (working directory clean)/status=clean/p
                            s/^# Untracked files:/status=untracked/p
                            s/^# Changed but not updated:/status=modified/p
                            s/^# Changes to be committed:/status=added/p
                        " 
                `
 
                ### compose local label
                eval git_color="\${git_$status}"
                eval esc_string="\$$git_color"
                local="${branch+$esc_string ($branch) }"
 
            fi 
    #########################
 
#PS1="$label$rc'$color_who_where$dir_color'${head:10*(${#PWD}<max)}${PWD:(${#PWD}>max)*(${#PWD}-max):max}> '$colors_reset'"
PS1="$label$rc'$color_who_where$dir_color'\w$local$dir_color> '$colors_reset'"
    unset local
'
#echo \"$color_who_where\"
unset rc id tty bell default_user default_host
unset rc_colors dir_color root_id_color 
 
# vim: set syntax=sh: