-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpro.sh
executable file
·433 lines (382 loc) · 9.45 KB
/
pro.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/bin/zsh
LOC="`dirname \"$0\"`"
# collection of directories, temp collection var
declare -a dirs
function usage {
# shows usages
# TODO dirs should have directory autocompletion
cat<<-EOF
Locates and changes directories to your projects and does other stuffs relating to them.
usage: ProLo(ProjectLocator) [l [dirs]] [a] [0-9]
[no args] show last 10 projects.
l goto last project you selected.
ls shows a list of scripts you can run for the project.
l dirs goto last project's sub directory.
a show all project directories.
0-9 goto one of the project (if you know its current project id)
-h display help
i initialize a script. Helps you to start windows and other stuffs that otherwise isn't available in ProLo.
ta (if you have 'task' installed). Shows list of task related to it. The project name must be set to directory name.
ta [tag] (if you have 'task' installed). Shows list of task related to current project and tags(dont add '+' infront of tags). The project name must be set to directory name.
new creates starter documents and directories based on _proj template.
pomo [taskno] starts the pomodoro timer for the given task id. The server must be started manually.
EOF
return;
}
# create projects file at script location
if [ ! -f ./projects ]; then
touch $LOC/projects
fi
if [ ! -f ./last ]; then
touch $LOC/last
fi
function getfromroot() {
# get all files at ROOT var
source $LOC/config
dirs=()
LS_DIRS=`ls $ROOT`
CNT=1
for dir in `ls $ROOT`
do
dirs[$CNT]="/$dir"
# echo $dir
# echo ${dirs[$CNT]}
CNT=`expr $CNT + 1`
done
}
function getfromconfig() {
# get all project directories from ./projects
# and read their path
dirs=()
CNT=1
while read -r line
do
head=`echo $line | grep -Po ".*=" | sed 's/.$//'`
# tail=`echo $line | grep -Po "\/.*"`
dirs[$CNT]=`printf "%s" $head`
CNT=`expr $CNT + 1`
done < $LOC/projects
}
function outputtofilearray() {
# deprecated
for i in $1
do
echo output
echo $i : $2
# echo $i >> $2
done
}
function createProject() {
if [ -z $1 ]; then
echo "Whats the name of project?"
return
fi
if [ ! -d $LOC/templates/_proj ]; then
echo "No _proj template found in ./templates. Create one."
return
fi
if [ -z $PROJECTROOT ]; then
echo "Please assign PROJECTROOT variable to root of your directory."
return
fi
cp -r $LOC/templates/_proj $PROJECTROOT/$1
echo created $1 in $PROJECTROOT
}
function updatelastproject() {
# appends last selected project to ./projects
# shifts the project from bottom up
# pops the older projects
if [ -z $1 ] | [ -z $2 ]; then
return
fi
duplicate=`grep -P "$1=$2" $LOC/projects`
if [[ $duplicate ]]; then
return
fi
# append string to ./projects
len=(`wc -l $LOC/projects`)
if [ $len[1] -gt 10 ]; then
temp=(`tail -n 9 $LOC/projects`)
truncate -s 0 $LOC/projects
for i in $temp
do
echo $i >> $LOC/projects
done
fi
echo $1=$2 >> $LOC/projects
}
function storeproject() {
# save the last project location to ./last
echo setting $1 as current project.
echo $1 > $LOC/last
}
function getlastprojectpath() {
# get last project's path
# TODO change last directory to contain more info
# DO NOT ECHO HERE. Unless you want to return a string
if [ -f $LOC/last ]; then
cat $LOC/last
else
echo ''
fi
}
function getlastprojectname() {
# get last project's name from actual path format.
# DO NOT ECHO HERE. unless you want to return a string
last_path=`getlastprojectpath`
if [[ $last_path != '' ]]; then
echo $last_path | sed -e "s%$PROJECTROOT/%%g" -e "s%/.*%%"
else
echo ''
fi
}
function gotolast() {
# change directory to current project.
final=$1
sec=$2
# jump to inner directory if there was a second argument
if [[ -n $sec ]]; then
final=$final/$sec
fi
if cd $final; then
storeproject $final
echo cd to: $final
else
echo could find here
sleep 1
echo cd to: $1
cd $1
echo inner directory argument was incorrect.
fi
}
function showmenu() {
# show list of all directories in $ROOT dir
if [[ $1 == 'a' ]]
then
getfromroot
else
getfromconfig
fi
# select a project
PS3='index? '
select opt in "${dirs[@]}"
do
# get the option out of the string, todo: volatile
if [ -z $opt ]; then
break
fi
if [[ $1 == 'a' ]]
then
loc=`echo $opt | grep -Po "\/.*"`
name=${opt:1}
loc="$ROOT$loc"
else
name=$opt
loc=`grep -P "^$opt" $LOC/projects | grep -Po "\/.*"`
fi
# loc=`sed -e "$opt q;d" $LOC/projects | grep -Po "\/.*"`
echo redy update
gotolast $loc ${@:2} &&
updatelastproject $name $loc
# cd $loc &&
# storeproject $loc &&
break
done
}
function getprojectat() {
# gets project at set location (from ./project)
file=`sed "$1 q;d" $LOC/projects`
head=`echo $file | grep -Po ".*=" | sed 's/.$//'`
loc=`echo $file | grep -Po "\/.*"`
echo $head : $loc
# select yes no before confirming cd
select opt in yes no
do
if [ -z $opt ]; then break; fi
if [ $opt = 'yes' ]; then
# TODO: need to add sub directory navigation
gotolast $loc &&
# storeproject $loc &&
updatelastproject $head $loc
break
fi
break
done
}
function isvalidrange() {
# check if given number lies within range of ./project 's list
# TODO get range from the file
if [ $1 -lt 10 ]; then
return 0
fi
return 1
}
function opentasklist() {
# opens task list for the lastest project
# TODO get task list for project at given number
last_project=`getlastprojectname`
if [ -z "$last_project" ]; then
echo no last project found. Open a project first.
else
echo lastest project: $last_project
echo task list for $last_project
# TODO process multiple tags coming from $2
if [ -z $2 ]; then
task project:$last_project
else
task project:$last_project +$2
fi
fi
}
function copyfilesfromtemplatedir() {
# create a copy of templates from templates dirs
source $LOC/config
echo 'Project Name?'
local projectname
read -r projectname
echo project location: $ROOT/$projectname
echo template location: $LOC/templates/$1
# cp -r $LOC/templates/$1/ $2
}
IN_SUB_DIR=false
TEMPLATE_DIR=''
function showcreateprojectmenu() {
# show list of all directories in template dir
local dirs=()
local templatedir=$1
local CNT=1
for dir in `ls $templatedir`
do
dirs[$CNT]="$dir"
# echo $dir
CNT=`expr $CNT + 1`
done
echo $dirs[@]
# select a project
PS3='index? '
select opt in "${dirs[@]}"
do
# get the option out of the string, todo: volatile
if [ -z $opt ]; then
break
fi
echo $opt
if [ $IN_SUB_DIR = false ]; then
IN_SUB_DIR=true
TEMPLATE_DIR=$opt
showcreateprojectmenu $LOC/templates/$opt
else
TEMPLATE_DIR=$TEMPLATE_DIR/$opt
copyfilesfromtemplatedir $TEMPLATE_DIR
fi
break
done
}
# shows list of script relating to that project and runs it if user chooses.
showscriptlists() {
local scriptdir=$1
local dirs=( $(ls $scriptdir) )
local latest=`getlastprojectname`
for i in $dirs
do
# lastprojectname is same name as found script dir then
if [ $i = $latest ]; then
echo found
PS3='index?'
select opt in `ls $scriptdir/$i`
do
echo seeing in
echo $scriptdir/$i/$opt
if [ -z $scriptdir/$opt ]; then;
break
# ignore directories for now
# elif [ -d $scriptdir/$opt ]; then
# showscriptlists $1/$opt
# break
elif [ -f $scriptdir/$i/$opt ]; then
echo doing
echo $scriptdir/$i/$opt
. $scriptdir/$i/$opt &
break
else
echo nothing
break
fi
done
return
fi
done
}
startpomo() {
local name=`getlastprojectname`
task project:$name list $1
sudo pomo start $1
}
# for i in `seq ${#DIRS[@]}`;
# START
# start parsing args
if [ "$1" = "" ]; then
showmenu
return
fi
while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
VALUE=`echo $1 | awk -F= '{print $2}'`
case $PARAM in
-h | --help)
usage
;;
p)
getlastprojectname
;;
a)
showmenu ${@}
;;
l)
gotolast `getlastprojectpath` ${@:2}
# FIXME: hectic exit, removing return will cause exit,??no idea why
return
;;
ls)
showscriptlists $LOC/scripts
;;
*[0-9]*)
if isvalidrange $1; then getprojectat ${@:2}; fi
;;
ta)
if [ -n `task --version` ]; then
opentasklist `getlastprojectname` ${@:2}
return
else
echo Ps install `task` using \`sudo pacman -S task\` for this feature to work.
fi
;;
create)
showcreateprojectmenu $LOC/templates
;;
new)
echo ${@:2}
createProject ${@:2}
return
;;
pomo)
echo starting pomo
if [ ${@:2} != "" ]; then
startpomo ${@:2}
shift
else
echo need task id as second args
fi
;;
i)
# initialize using a script for the current project
# find if init file has been created, else create in ./init/projectname.sh
# source the script
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
;;
esac
shift
done