-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromptconvert.sh
executable file
·281 lines (272 loc) · 8.7 KB
/
promptconvert.sh
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/bin/bash
#Get the line from the 'prompts' file that matches the name, and begin to set the
#PS1 variable to it. This will be stored in a file called 'conv_prompt' in the
#current storage directory.
#Check if the 'conv_prompts' file currently exists. If it does,
#make a backup of it and tell the user that it will re-created
storage="$HOME/.stored_prompts"
if [ ! -e $storage/conv_prompt ]; then
echo "Making 'conv_prompt' file in $storage..."
touch $storage/conv_prompt
else
echo "The 'conv_prompt' file currently exists. This one will have a"
echo "backup made of it. WARNING THIS WILL OVERWRITE A PREVIOUS"
echo "BACKUP IF IT EXISTS. Are you sure you want to continue?"
echo "yes / no"
read answer
#Check if $answer is 'yes' or 'no'
while [ "$answer" != "yes" ] && [ "$answer" != "no" ]; do
echo "Please choose only 'yes' or 'no' as the answer."
read answer
done
if [ "$answer" == "yes" ]; then
echo "Backing up the file..."
mv $storage/conv_prompt $storage/conv_prompt.bak
sleep 1
touch $storage/conv_prompt
else
echo "Exiting..."
exit 1
fi
fi
echo "Converting the file to something that the PS1 variable can use..."
#Make a function to append to the file easier.
appendfile()
{
echo -n "$1" >> $storage/conv_prompt
}
#Cycle through the 'prompts' file one line at a time, skipping any that
#begin with a '#'.
counter=0
while read line ;
#for line in `cat $storage/prompts`
do
counter=$(($counter+1))
if [[ $line =~ ^\# ]] || [[ "$line" == "" ]]; then
echo "Skipping line $counter..."
continue
fi
echo "Reading line $counter..."
#Add name of the prompt to the beginning of the line.
name=`echo $line | awk -F ';;;' '{print $1}'`
echo $name
appendfile "$name;;;"
#Loop through the different fields
echo $line | sed s/";;;"/"\n"/g | while read i ;
do
echo "Item = $i"
args=`echo $i | cut -d ':' -f 2-`
case $i in
#Write out exactly what the user wants. This feature is useful
#when a shortcut for a certain command does not exist.
"LIT:"*)
appendfile "$args"
;;
#Add a blank space to the prompt line. The LIT feature
#does not handle a blank space well.
"space"|"Space")
appendfile ' '
;;
#Git repo integration and commands
"GIT:"*)
case "$args" in
#Return the name of the current branch
"cur"*"branch")
appendfile '$(git rev-parse --abbrev-ref HEAD)'
;;
#Return the current status of the current repository
"stat"*)
appendfile '$(git status --porcelain)'
;;
*)
echo "ERROR: $i not understood."
echo "This error was found in a 'GIT:' declaration."
echo "Valid options for 'GIT': 'curbranch', and 'status'."
exit 2
;;
esac
;;
#Execute a string of commands prefixed by 'EXE:'
"EXE:"*)
appendfile '$('"`echo $i | cut -d ':' -f 2-`"')'
;;
#Determine what to change the color to
"COLOR:"*)
style=`echo $args | cut -d ',' -f 1`
case $style in
"normal"|"Normal")
appendfile '\[\e[0;'
;;
"bold"|"Bold")
appendfile '\[\e[1;'
;;
"underline"|"Underline")
appendfile '\[\e[4;'
;;
"background"|"Background")
appendfile '\[\e['
;;
*)
echo "ERROR: $i not understood."
echo "This error was found in a 'COLOR:style' declaration."
echo "Valid options for style: 'bold', 'normal', 'underline', and 'background'."
exit 2
;;
esac
colors=`echo $args | cut -d ',' -f 2`
case $colors in
"black"|"Black")
appendfile '30m\]'
;;
"light-black"|"Light-Black")
appendfile '90m\]'
;;
"red"|"Red")
appendfile '31m\]'
;;
"light-red"|"Light-Red")
appendfile '91m\]'
;;
"green"|"Green")
appendfile '32m\]'
;;
"light-green"|"Light-Green")
appendfile '92m\]'
;;
"yellow"|"Yellow")
appendfile '33m\]'
;;
"light-yellow"|"Light-Yellow")
appendfile '93m\]'
;;
"blue"|"Blue")
appendfile '34m\]'
;;
"light-blue"|"Light-Blue")
appendfile '94m\]'
;;
"purple"|"Purple")
appendfile '35m\]'
;;
"light-purple"|"Light-Purple")
appendfile '95m\]'
;;
"cyan"|"Cyan")
appendfile '36m\]'
;;
"light-cyan"|"Light-Cyan")
appendfile '96m\]'
;;
"white"|"White")
appendfile '37m\]'
;;
"light-white"|"Light-White")
appendfile '97m\]'
;;
*)
echo "ERROR: $i not understood."
echo "This error was found in a 'COLOR:colors' declaration."
echo "Valid options for style: 'black', 'red', 'green', 'yellow', 'blue',"
echo "'purple', 'cyan', and 'white'. Prefixing one of these with"
echo "'light-' will give you the bright or light version of that color."
exit 2
;;
esac
;;
#Get the Date
"date"|"Date")
appendfile '\d'
;;
#Get the name of the computer
"host"*|"machine"|"computername"|"Host"*|"Machine"|"Computername")
appendfile '\h'
;;
#Get the number of jobs
"job"*|"Job"*)
appendfile '\j'
;;
#Shell terminal device basename
"shell-basename"|"Shell-Basename")
appendfile '\l'
;;
#What kind of shell is running
"shell"|"Shell"|"shellname"|"Shellname")
appendfile '\s'
;;
#Time
"TIME:"*)
case $type in
#24 hour time
"24")
appendfile '\t'
;;
#12 hour time
"12")
appendfile '\T'
;;
#standard 12 hour time with am/pm
"norm"*|"stan"*|"Norm"*|"Stan"*)
appendfile '\@'
;;
*)
echo "ERROR: $i not understood."
echo "This error was found in a 'time' declaration."
echo "Valid options for time: '24', '12', and 'normal'."
exit 2
;;
esac
;;
#Get the username
"user"*|"User"*)
appendfile '\u'
;;
#Bash version
"ver"*|"Ver"*)
appendfile '\v'
;;
#Current directory
"curdir"|"Curdir"|"current directory"|"Current Directory")
appendfile '\w'
;;
"where"*"am"*"i"|"Where"*"am"*"i")
appendfile '\W'
;;
#Return the history number
"hist"*|"Hist"*)
appendfile '\!'
;;
#Write the command number
"com"*"num"*|"Com"*"num"*)
appendfile '\#'
;;
#Check if the user is root
"check"*"root"|"Check"*"Root")
appendfile '\$'
;;
#Add a newline
"new"*"line"|"New"*"Line"|"New"*"line")
appendfile '\n'
;;
#Add an escape character
"esc"*|"Esc"*)
appendfile '\e'
;;
#bell
"bell"|"Bell")
appendfile '\a'
;;
*)
echo "Entry $i not understood. If this item is the name,"
echo "then ignore this error. Otherwise, please take another"
echo "look at your 'prompts' file."
;;
esac
done
#Add a blank line at the end of the cycle, so that the prompts do not
#interfere with each other.
echo "" >> $storage/conv_prompt
done < $storage/prompts
#Conversion into a prompt should be complete, tell the user
echo "The conversion is complete. The different prompts can"
echo "be called by using the name as an argument at any time."
exit 0