-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatla.sh
547 lines (457 loc) · 12.3 KB
/
atla.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# vi: ts=4 sts=4 shiftwidth=4 expandtab
# filename : atla.sh
# DESCRIPTION : Dizin Atla_X
# AUTHOR : Seçkin KILINÇ <[email protected]>
# VERSION : v0.1
# HISTORY :
# - 24.04.2015 (v0.1): İlk Sürüm
#
## setting for the linux terminal
# CTRL + Up/Down/Right/Left Arrow
bind '"\e[1;5A":"atla_ctrl_up\C-m"' 2> /dev/null
bind '"\e[1;5B":"atla_ctrl_down\C-m"' 2> /dev/null
bind '"\e[1;5C":"atla_ctrl_right\C-m"' 2> /dev/null
bind '"\e[1;5D":"atla_ctrl_left\C-m"' 2> /dev/null
## setting for the putty
# CTRL + Up/Down/Right/Left Arrow
bind '"\eOA":"atla_ctrl_up\C-m"' 2> /dev/null
bind '"\eOB":"atla_ctrl_down\C-m"' 2> /dev/null
bind '"\eOC":"atla_ctrl_right\C-m"' 2> /dev/null
bind '"\eOD":"atla_ctrl_left\C-m"' 2> /dev/null
## setting for the screen in the putty
# ALT + Up/Down/Right/Left Arrow
bind '"\e\e[A":"atla_ctrl_up\C-m"' 2> /dev/null
bind '"\e\e[B":"atla_ctrl_down\C-m"' 2> /dev/null
bind '"\e\e[C":"atla_ctrl_right\C-m"' 2> /dev/null
bind '"\e\e[D":"atla_ctrl_left\C-m"' 2> /dev/null
export ATLA_HOME=$HOME/.atla/
export ATLA_FNAME_ABS=$ATLA_HOME/dirlist
export ATLA_FNAME_TEMP=$ATLA_HOME/dirlist.tmp
export ATLA_FNAME_SAVE=$ATLA_HOME/dirlist.save
export ATLA_FNAME_STACK=$ATLA_HOME/dirlist.stack
export ATLA_READLINK=$HOME/.atla/readlink.sh
# Brief: Print usage
# Usage: atla_print_usage
function atla_print_usage
{
echo "atla - Dizin Atla_X";
echo;
echo "[ Seçkin KILINÇ <[email protected]>]";
echo;
echo "[seckinkilinc.tk>]";
echo;
echo "Dizin Atlama Basit Script Kullanım Kılavuzu";
echo;
echo "Kullanım: ";
echo " atla : Dizini Yazar";
echo " atla [index] : Dizini Index Ayarlar";
echo " atla add : Bulunduğun Dizini Ekler";
echo " atla add [dir] : Dizin Ekler";
echo " atla rm : Bulunduğun Dizini Siler";
echo " atla rm [index] : Index Dizinini Siler";
echo " atla save <filename> : Ayarları Dosyaya Yazar";
echo " atla load <filename> : Ayarları Dosyadan Alır";
echo " atla clean : Herşeyi Siler";
echo " atla help : Kullanım Klavuzu";
echo;
echo "Key Map (normal):";
echo " CTRL + Yukarı OK : Öncekine Atlar";
echo " CTRL + Aşağı OK : Sonrakine Atlar";
echo " CTRL + Sol OK : Aşağı Atlar";
echo " CTRL + Sağ OK : Yukarı Atlar";
echo;
echo "Key Map (putty + screen):";
echo " ALT + Yukarı OK : Öncekine Atlar";
echo " ALT + Aşağı OK : Sonrakine Atlar";
echo " ALT + Sol OK : Aşağı Atlar";
echo " ALT + Sağ OK : Yukarı Atlar";
echo;
echo "Key Map (mac os):";
echo " ESC + Yukarı OK :Öncekine Atlar";
echo " ESC + Aşağı OK : Sonrakine Atlar";
echo " ESC + Sol OK : Aşağı Atlar";
echo " ESC + Sağ OK : Yukarı Atlar";
echo;
echo "# ekstra kısayollar";
echo "alias nnd=nano /etc/nginx/sites-available/default";
echo "alias ser=service $1 $2";
echo "alias sers=service $1 status";
echo "alias serr=service $1 restart";
echo "alias apin=apt install $1";
echo "alias remcd=rm -rf ./*";
echo "alias remcda=rm -rf ./.*";
echo;
}
# Brief:
# Usage: atla_ctrl_up
function atla_ctrl_up
{
# clear stack
rm -f $ATLA_FNAME_STACK;
# move previous directory
clear;
atla_prev;
# print current dir list
echo "[ Dizin Atla_X ]";
echo;
echo "[ Seçkin KILINÇ <[email protected]>]";
echo;
echo "[seckinkilinc.tk>]";
echo;
atla_dirs;
# delete last history
history -d $(($HISTCMD-1));
}
# Brief:
# Usage: atla_ctrl_down
function atla_ctrl_down
{
# clear stack
rm -f $ATLA_FNAME_STACK;
# move next directory
clear;
atla_next;
# print current dir list
echo "[ Dizin Atla_X ]";
echo;
echo "[ Seçkin KILINÇ <[email protected]>]";
echo;
echo "[seckinkilinc.tk>]";
echo;
atla_dirs;
# delete last history
history -d $(($HISTCMD-1));
}
# Brief:
# Usage: atla_ctrl_left
function atla_ctrl_left
{
is_direct_line=0;
is_diffrent_line=1;
clear;
# discards stack
grep "$PWD" $ATLA_FNAME_STACK > /dev/null 2>&1;
if [ $? = $is_diffrent_line ]; then
rm -f $ATLA_FNAME_STACK;
fi
atla_push_dir_into_stack;
# go down
cd ..;
# print current dir
echo "[ Dizin Atla_X ]";
echo;
echo "[ Seçkin KILINÇ <[email protected]>]";
echo;
echo "[seckinkilinc.tk>]";
echo;
echo -e " TOP: \033[7m`head -n1 $atla_FNAME_STACK`\033[27m";
echo " PWD: $PWD";
echo;
echo "(Daha Fazla Bilgi İçin 'atla help' yazabilirsiniz.)";
echo;
# delete last history
history -d $(($HISTCMD-1));
}
# Brief:
# Usage: atla_ctrl_right
function atla_ctrl_right
{
clear;
if [ -e $ATLA_FNAME_STACK ]; then
# go up
cd $(tail -n1 $ATLA_FNAME_STACK);
# pop stack
atla_pop_dir_from_stack;
fi
# print current dir
echo "[ Dizin Atla_X ]";
echo;
echo "[ Seçkin KILINÇ <[email protected]>]";
echo;
echo "[seckinkilinc.tk>]";
echo;
if [ -e $ATLA_FNAME_STACK ]; then
echo -e " TOP: \033[7m`head -n1 $ATLA_FNAME_STACK`\033[27m";
else
echo -e " TOP: \033[7m$PWD\033[27m";
fi
echo " PWD: $PWD";
echo;
echo "(Daha Fazla Bilgi İçin 'atla help' yazabilirsiniz.)";
echo;
# delete last history
history -d $(($HISTCMD-1));
}
# Brief: Push a current directory into stack
# Usage: atla_push_dir_into_stack
function atla_push_dir_into_stack
{
if [ -e $ATLA_FNAME_STACK ] \
&& [ $(tail -n1 $ATLA_FNAME_STACK) != $PWD ] \
&& [ $PWD != "/" ]; then
echo $PWD >> $ATLA_FNAME_STACK;
elif ! [ -e $ATLA_FNAME_STACK ]; then
echo $PWD >> $ATLA_FNAME_STACK;
fi
}
# Brief: Pop a last directory from stack
# Usage: atla_pop_dir_from_stack
function atla_pop_dir_from_stack
{
# delete last line at $ATLA_FNAME_STACK
if [ -e $ATLA_FNAME_STACK ];then
sed -itmp '$ d' $ATLA_FNAME_STACK
rm -f $ATLA_FNAME_STACK"tmp"
fi
# if file size 0 then delete $ATLA_FNAME_STACK file
! [ -s $ATLA_FNAME_STACK ] && rm -f $ATLA_FNAME_STACK;
}
# Brief: Remove directories that is not exist
# Usage: atla_reload_only_exist_dir
function atla_reload_only_exist_dir
{
rm -f $ATLA_FNAME_TEMP && touch $ATLA_FNAME_TEMP;
! [ -f $ATLA_FNAME_ABS ] && touch $ATLA_FNAME_ABS;
while read line; do
if [ -d $line ]; then
echo "$line" >> $ATLA_FNAME_TEMP;
fi
done < <(cat $ATLA_FNAME_ABS)
mv -f $ATLA_FNAME_TEMP $ATLA_FNAME_ABS;
}
# Brief: Display directory list
# Usage: atla_dirs
function atla_dirs
{
LINE_COUNT=$(wc -l $ATLA_FNAME_ABS | awk '{print $1}');
if [ "$LINE_COUNT" == "0" ]; then
echo "(Henüz hiçbir klasör eklemediniz. Yardım için : 'atla --help')";
return;
fi
atla_reload_only_exist_dir;
while read line;
do
directory=$(echo $line | awk '{print $2}');
#Point out current directory
if [[ $PWD == $directory ]];then
echo -e " \033[7m$line\033[27m";
else
echo " $line";
fi
done < <(cat -n $ATLA_FNAME_ABS)
echo;
echo "(Daha Fazla Bilgi İçin 'atla help' yazabilirsiniz.)";
echo;
}
# Brief:
# Usage:
function atla_add
{
# Check param
if ! test -d "$1"; then
echo "error: Invalid parameter" 1>&2
return 1;
fi
echo $($ATLA_READLINK $1) >> $ATLA_FNAME_ABS;
atla_reload_only_exist_dir;
sort -u $ATLA_FNAME_ABS -o $ATLA_FNAME_TEMP;
mv -f $ATLA_FNAME_TEMP $ATLA_FNAME_ABS;
return;
}
# Brief:
# Usage:
function atla_clean
{
rm -f $ATLA_FNAME_ABS && touch $ATLA_FNAME_ABS;
}
# Brief: Save a directory list to the filepath.
# Usage: atla_save <filepath>
function atla_save
{
# error handling
[ "$#" = 0 ] && return 0;
atla_reload_only_exist_dir;
cp $ATLA_FNAME_ABS $1;
}
# Brief: Load a directory list from the filepath.
# Usage: atla_load <filepath>
function atla_load
{
# error handling
[ "$#" = 0 ] && return 0;
cp $1 $ATLA_FNAME_ABS;
# error handling
atla_reload_only_exist_dir;
}
# Brief: Remove a directory.
# Usage: atla_rm <filepath>
function atla_rm
{
# Check argument whether number.
if [ ! $(echo $1 | sed -n '/^[0-9][0-9]*$/p') ]; then
echo "error: Input number" 1>&2
atla_print_usage;
return;
fi
atla_reload_only_exist_dir;
rm -f $ATLA_FNAME_TEMP && touch $ATLA_FNAME_TEMP;
# Save all directoryes without target.
while read line;
do
index=$(echo $line | awk '{print $1}');
directory=$(echo $line | awk '{print $2}');
[ "$index" -ne "$1" ] && echo $directory >> $ATLA_FNAME_TEMP;
done < <(cat -n $ATLA_FNAME_ABS)
mv -f $ATLA_FNAME_TEMP $ATLA_FNAME_ABS;
return 0;
}
function atla_rm_by_dirname
{
rm -f $ATLA_FNAME_TEMP && touch $ATLA_FNAME_TEMP;
while read line;
do
if [[ ! "$line" == "$($ATLA_READLINK $1)" ]]; then
echo "$line" >> $ATLA_FNAME_TEMP;
fi
done < <(cat $ATLA_FNAME_ABS)
mv -f $ATLA_FNAME_TEMP $ATLA_FNAME_ABS;
}
function atla_next
{
index_curr=0;
atla_reload_only_exist_dir;
while read line;
do
index=$(echo "$line" | awk '{print $1}')
directory=$(echo $line | awk '{print $2}')
if [[ $PWD == $directory ]];then
index_curr=$index;
fi
done < <(cat -n $ATLA_FNAME_ABS)
index_curr=$(expr $index_curr + 1);
index_end=$(wc -l $ATLA_FNAME_ABS | awk '{print $1}');
#roll back
if [ $index_curr -gt $index_end ]; then
index_curr=1;
fi
#change directory
atla_go $index_curr;
return 0;
}
function atla_prev
{
index_curr=0;
atla_reload_only_exist_dir;
while read line;
do
index=$(echo $line | awk '{print $1}');
directory=$(echo $line | awk '{print $2}');
if [[ $PWD == $directory ]];then
index_curr=$index;
fi
done < <(cat -n $ATLA_FNAME_ABS)
index_curr=$(expr $index_curr - 1);
index_end=$(wc -l $ATLA_FNAME_ABS | awk '{print $1}');
#roll back
if [ $index_curr -lt 1 ]; then
index_curr=$index_end;
fi
#change directory
atla $index_curr;
return 0;
}
# Brief: Change a current directory using index
# Usage: atla_go <index>
function atla_go
{
# Check argument whether number.
if [ ! $(echo $1 | sed -n '/^[0-9][0-9]*$/p') ]; then
atla_print_usage;
return;
fi
# get directory by index
ATLA_TARGET=$(cat -n $ATLA_FNAME_ABS | grep "^[[:space:]]*$1[[:space:]]" | awk '{print $2}');
# go
! [ -z $ATLA_TARGET ] && cd $ATLA_TARGET;
}
function atla
{
if [ ! -f $ATLA_FNAME_ABS ]; then
touch $ATLA_FNAME_ABS
fi
case $1 in
"")
atla_dirs;
;;
help)
atla_print_usage;
;;
add)
if [ $# -gt 2 ]; then
atla_print_usage;
return;
fi
if [[ $# == 1 ]]; then
atla_add "."
else
atla_add $2
fi
;;
rm)
if [ $# -gt 2 ]; then
atla_print_usage;
return;
elif [ $# -eq 1 ]; then
atla_rm_by_dirname ".";
elif [ $# -eq 2 ]; then
atla_rm $2;
fi
;;
next)
if [ $# -gt 1 ]; then
atla_print_usage;
return;
fi
atla_next $2;
;;
prev)
if [ $# -gt 1 ]; then
atla_print_usage;
return;
fi
atla_prev $2;
;;
clean)
if [ $# -gt 1 ]; then
atla_print_usage;
return;
fi
atla_clean;
;;
save)
if [ $# -gt 2 ]; then
atla_print_usage;
return;
fi
atla_save $2;
;;
load)
if [ $# -gt 2 ]; then
atla_print_usage;
return;
fi
atla_load $2;
;;
*)
atla_go $1
;;
esac
}
# ekstra kısayollar
alias nnd='nano /etc/nginx/sites-available/default'
alias ser='service $1 $2'
alias sers='service $1 status'
alias serr='service $1 restart'
alias apin='apt install $1'
alias remcd='rm -rf ./*'
alias remcda='rm -rf ./.*'