-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmole.sh
603 lines (506 loc) · 15.1 KB
/
mole.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/bin/sh
###
### mole.sh
###
### Author: Konstantin Romanets (xroman18), xroman18@stud.fit.vut.cz
### Date: 2023-03-23
### Desc: Implementation of the mole text editor for IOS Project 1
###
### VUT FIT 2023
###
unset MODE
unset FLAG_M
unset GROUP
unset FILE
unset AFTER_DATE
unset BEFORE_DATE
#DEBUG=1
unset DEBUG
export POSIXLY_CORRECT=YES
GROUP=
FILE=
EDITOR="${EDITOR:-${VISUAL:-vi}}"
error() {
>&2 echo "\033[31m[Error] $1\033[m"
exit 1
}
warn() {
>&2 echo "\033[33m[Warning] $1\033[m"
}
debug_warn() {
if [ -n "$DEBUG" ]; then
>&2 echo "\033[33m[Debug] $1\033[m"
fi
}
debug_echo() {
if [ -n "$DEBUG" ]; then
>&2 echo "\033[34m[Debug] $1\033[m"
fi
}
cmd_exists() {
if ! command -v "$1" 2>&1 /dev/null
then
false
fi
true
}
usage() {
echo "Usage: mole [-g GROUP] [FILTERS] [DIRECTORY] FILE"
}
help() {
echo "mole - Makes One's Life Easier text editor"
echo
echo "Usage: mole.sh [-g GROUP] FILE"
echo " mole.sh [-m] [FILTERS] [DIRECTORY]"
echo " mole.sh list [FILTERS] [DIRECTORY]"
echo
echo "Options:"
echo " -h Show help message"
echo " FILE The file to open in the editor or the path to the folder with files"
echo " [-g GROUP] Specifies the group of files to put FILE into"
echo " [-m] Opens a file in directory that has been opened the most"
echo " [FILTERS] Filters the files to open"
echo " [DIRECTORY] Specifies the directory to open"
echo " list Lists files in directory that have been edited using this program with groups assigned to them"
echo
echo "Filters:"
echo " -g GROUP1[,GROUP2[,...]] Filters the files by the groups they are in"
echo " -a DATE Filters the files after the specified date"
echo " -b DATE Filters the files before the specified date"
echo
echo "Date format: YYYY-MM-DD"
}
now() {
date +"%Y-%m-%d_%H-%M-%S"
}
### workarounds for macos bullshit ###
# sed -i
sh_sed() {
if [ "$(uname)" = "Darwin" ]; then
sed -i '' "$@"
else
sed -i "$@"
fi
}
# realpath -m / readlink -fm
sh_path() {
if [ -n "$*" ]; then
if [ "$(uname)" = "Darwin" ]; then
if [ "$(cmd_exists "grealpath")" ]; then
grealpath -m "$@"
elif [ "$(cmd_exists "greadlink")" ]; then
greadlink -fm "$@"
else
error "sh_path :: realpath not found"
fi
else
if [ "$(cmd_exists "realpath")" ]; then
realpath -m "$@"
elif [ "$(cmd_exists "readlink")" ]; then
readlink -fm "$@"
else
error "sh_path :: realpath not found"
fi
fi
fi
}
file_create() {
if [ -z "$1" ]; then
error "No file specified"
else
mkdir -p -- "$(dirname "$1")" && touch -- "$1"
fi
debug_echo "Created file $1"
}
env_check() {
# Check if MOLE_RC environment variable is set
if [ -z "$MOLE_RC" ]; then
error "env_check :: MOLE_RC environment variable not set"
fi
}
config_check_create() {
# if MOLE_RC file does not exist, create it
if [ ! -f "$MOLE_RC" ]; then
file_create "$MOLE_RC"
debug_echo "config_create :: molerc created"
fi
}
# molerc is a csv file
# columns: directory;file;group;count;last_opened;date_1;date_2;...;date_n
# example: config_get "directory" "/home/user"
# params: group, directory, file, count, last_opened
config_add() {
if [ -z "$1" ]; then
GRP=""
else
GRP="$1"
fi
echo "$GRP;$2;$3;$4;$5;$6" >> "$MOLE_RC"
}
# params: group, directory, file
config_find() {
LINE="$(grep "^$1;$2;$3" "$MOLE_RC")"
debug_echo "config_find :: LINE = $(echo "$LINE" | cut -d ';' -f 1-5)"
echo "$LINE"
}
# params: [index_from_top]
config_get_line() {
if [ -z "$1" ]; then
INDEX=0
else
INDEX="$1"
fi
debug_echo "config_get_line :: INDEX = $INDEX"
LINE="$(tail -n "$INDEX" "$MOLE_RC" | head -n 1)"
debug_echo "config_get_line :: LINE = $LINE"
echo "$LINE"
}
config_get_first() {
config_get_line 1
}
config_get_last() {
LINE_COUNT="$(wc -l "$MOLE_RC" | cut -d ' ' -f 1)"
config_get_line "$LINE_COUNT"
}
# params: [dir]
# returns group
config_get_last_opened_group() {
if [ -z "$1" ]; then
DIR="$(pwd)"
else
DIR="$1"
fi
# debug_echo "config_get_last_opened_group :: DIR = $DIR"
GRP="$(grep ";$DIR;" "$MOLE_RC" | sort -t ';' -k 5 -r | head -n 1 | cut -d ';' -f 1)"
debug_echo "config_get_last_opened_group :: GRP = $GRP"
echo "$GRP"
}
# params: [dir], [group]
# returns filename
config_get_last_opened() {
if [ -z "$1" ]; then
DIR="$(pwd)"
else
DIR="$1"
fi
debug_echo "config_get_last_opened :: DIR = $DIR"
if [ -z "$2" ]; then
GRP=""
else
GRP="$2"
fi
if [ -z "$GRP" ]; then
LAST_OPENED="$(grep ";$DIR;" "$MOLE_RC" | sort -t ';' -k 5 -r | head -n 1)"
# LAST_OPENED="$LINE"
# GRP="$(echo "$LINE" | cut -d ';' -f 1)"
else
LAST_OPENED="$(grep "^$GRP;$DIR;" "$MOLE_RC" | sort -t ';' -k 5 -r | head -n 1)"
fi
if [ -z "$LAST_OPENED" ]; then
echo ""
return
fi
FILEPATH="$(echo "$LAST_OPENED" | cut -d ';' -f 2)/$(echo "$LAST_OPENED" | cut -d ';' -f 3)"
debug_echo "config_get_last_opened :: GROUP = $GROUP; LAST_OPENED = $FILEPATH"
echo "$FILEPATH"
}
# params: [dir]
# returns group
config_get_most_opened_group() {
if [ -z "$1" ]; then
DIR="$(pwd)"
else
DIR="$1"
fi
GRP="$(grep ";$DIR;" "$MOLE_RC" | sort -t ';' -k 4 -r | head -n 1 | cut -d ';' -f 1)"
debug_echo "config_get_most_opened_group :: GRP = $GRP"
echo "$GRP"
}
# params: [dir]
# returns filename
config_get_most_opened() {
if [ -z "$1" ]; then
DIR="$(pwd)"
else
DIR="$1"
fi
MOST_OPENED="$(grep "$DIR;" "$MOLE_RC" | sort -t ';' -k 4 -r -n | head -n 1)"
if [ -z "$MOST_OPENED" ]; then
echo ""
return
fi
FILEPATH="$(echo "$MOST_OPENED" | cut -d ';' -f 2)/$(echo "$MOST_OPENED" | cut -d ';' -f 3)"
debug_echo "config_get_most_opened :: MOST_OPENED = $FILEPATH"
echo "$FILEPATH"
}
# params: group, directory, file
config_get_count() {
COUNT="$(grep "^$1;$2;$3" "$MOLE_RC" | cut -d ';' -f 4)"
debug_echo "config_get_count :: COUNT = $COUNT"
echo "$COUNT"
}
# increases the count of the file by 1 and appends the most recent opening date
# moving the previous date one column to the right
# params: directory, file
config_inc_count() {
COUNT="$(config_get_count "$GROUP" "$1" "$2")"
NEW_COUNT="$((COUNT + 1))"
debug_echo "config_inc_count :: NEW_COUNT = $NEW_COUNT"
LINE="$(config_find "$GROUP" "$1" "$2")"
# if [ "$(echo "$LINE" | ws -l)" -gt 1 ]; then
# LINE="$(echo "$LINE" | head -n 1)"
# fi
debug_echo "config_inc_count :: LINE = $(echo "$LINE" | cut -d ';' -f 1-5)"
GRP="$(echo "$LINE" | cut -d ';' -f 1)"
debug_echo "config_inc_count :: GRP=$GRP; GROUP=$GROUP"
if [ "$GROUP" != "" ] && [ "$GRP" = "" ] || [ "$GRP" != "$GROUP" ]; then
# NEW_LINE="$(echo "$LINE" | cut -d ';' -f 1-3);$NEW_COUNT;$(now);$(echo "$LINE" | cut -d ';' -f 5-)"
debug_echo "config_inc_count :: needs new entry"
config_add "$GROUP" "$1" "$2" "1" "$(now)"
else
NEW_LINE="$(echo "$LINE" | cut -d ';' -f 1-3);$NEW_COUNT;$(now);$(echo "$LINE" | cut -d ';' -f 5-)"
debug_echo "config_inc_count :: NEW_LINE = $(echo "$NEW_LINE" | cut -d ';' -f 1-5)"
sh_sed "s|$LINE|$NEW_LINE|g" "$MOLE_RC"
fi
}
# displays the list of all files and their assigned groups
# after_date and before_date are inclusive
# FILE1: group1, group2
# FILE2: group1, group3
# ...
#
# params: [filters: [groups], [after_date], [before_date]], [directory]
display_list() {
if [ -z "$1" ]; then
GRP=""
else
GRP="$1"
fi
if [ -z "$4" ]; then
DIR="$(pwd)"
else
DIR="$4"
fi
debug_echo "display_list :: GRP=$GRP; AFTER_DATE=$2; BEFORE_DATE=$3; DIR=$DIR"
if [ -z "$GRP" ]; then
LIST="$(grep ";$DIR;" "$MOLE_RC" | cut -d 'f' -f 1-5 |sort -t ';' -k 5 -r)"
else
LIST="$(grep "^$GRP;$DIR;" "$MOLE_RC" | cut -d 'f' -f 1-5 | sort -t ';' -k 5 -r)"
fi
if [ -n "$2" ]; then
LIST="$(echo "$LIST" | awk -F ';' -v date="$2" '$5 >= date {print}')"
fi
if [ -n "$3" ]; then
LIST="$(echo "$LIST" | awk -F ';' -v date="$3" '$5 <= date {print}')"
fi
if [ -z "$LIST" ]; then
return
fi
echo "$LIST" | awk -F ';' '
{
group = $1
file = $3
if (file != "") {
file_count[file]++
if (file_count[file] == 1) {
if (group == "") {
group = "-"
}
file_groups[file] = group
} else {
if (group == "") {
group = "-"
}
file_groups[file] = file_groups[file] ", " group
}
}
}
END {
max_len = 0
for (f in file_groups) {
len = length(f)
if (len > max_len) {
max_len = len
}
}
for (file in file_groups) {
printf "%s:%*s%s\n", file, max_len - length(file) + 1, " ", file_groups[file]
}
}
' | sort
}
# format
# FILE1;DATETIME_1;DATETIME_2;...;DATETIME_N
# FILE2;DATETIME_1;DATETIME_2;...;DATETIME_N
# ...
# params: [filters: [groups], [after_date], [before_date]], [directories]
display_log() {
if [ "$(uname)" = "Darwin" ]; then
LOG_FILE="/Users/$USER/.mole/log_${USER}_$(now)"
else
LOG_FILE="/home/$USER/.mole/log_${USER}_$(now)"
fi
file_create "$LOG_FILE"
if [ -z "$1" ]; then
GRP=""
else
GRP="$1"
fi
DIR="$4"
debug_echo "display_log :: GRP=$GRP; AFTER_DATE=$2; BEFORE_DATE=$3; DIR=$DIR"
# echo "$DIR" | while read -d ' ' path; do
# # filter out all the dirs
# grep ";$path;" "$MOLE_RC"
# done
# split $DIR using space as delimiter
if [ -n "$DIR" ]; then
if [ -z "$GRP" ]; then
# LIST="$(grep ";$DIR;" "$MOLE_RC" | cut -d 'f' -f 1-5 | sort -t ';' -k 5 -r)"
LIST="$(echo "$DIR" | tr ' ' '\n' | while read -r n; do
grep ";$n;" "$MOLE_RC"
done | sort -t ';' -k 5 -r)"
else
# LIST="$(grep "^$GRP;$DIR;" "$MOLE_RC" | cut -d 'f' -f 1-5 | sort -t ';' -k 5 -r)"
LIST="$(echo "$DIR" | tr ' ' '\n' | while read -r n; do
grep "^$GRP;$n;" "$MOLE_RC"
done | sort -t ';' -k 5 -r)"
fi
else
LIST="$(cat "$MOLE_RC")"
fi
if [ -n "$2" ]; then
LIST="$(echo "$LIST" | awk -F ';' -v date="$2" '$5 >= date {print}')"
fi
if [ -n "$3" ]; then
LIST="$(echo "$LIST" | awk -F ';' -v date="$3" '$5 <= date {print}')"
fi
if [ -z "$LIST" ]; then
return
fi
# iterate over unique files
echo "$LIST" | cut -d ';' -f 2-3 | uniq | while read -r file; do
FILEPATH="$(echo "$file" | cut -d ';' -f 1)"
FILENAME="$(echo "$file" | cut -d ';' -f 2)"
DATES="$(echo "$LIST" | grep ";$FILEPATH;$FILENAME;" | cut -d ';' -f 5- | tr '\n' ';' | sed "s|;;|;|g" | sort -r)"
echo "$FILEPATH/$FILENAME;$DATES" >> "$LOG_FILE"
done
bzip2 "$LOG_FILE"
}
parse_args() {
debug_echo "parse_args :: $*"
if [ "$1" = "list" ] || [ "$1" = "secret-log" ]; then
MODE="$1"
shift 1
fi
while getopts ":uhmg:a:b:" opt; do
case $opt in
u)
usage
exit 0
;;
h)
help
exit 0
;;
m)
FLAG_M=1
;;
g)
GROUP="$OPTARG"
;;
a)
AFTER_DATE="$OPTARG"
;;
b)
BEFORE_DATE="$OPTARG"
;;
\?)
error "parse_args :: Invalid option: -$OPTARG"
;;
:)
error "parse_args :: Option -$OPTARG requires an argument."
;;
esac
done
# if [ $OPTIND -eq 1 ]; then
# debug_warn "parse_args :: No options passed"
# fi
shift $((OPTIND - 1))
if [ -z "$MODE" ]; then
MODE="edit"
fi
# Remaining args
FILE="$*"
debug_echo "parse_args :: MODE=$MODE; M=$FLAG_M; A=$AFTER_DATE; B=$BEFORE_DATE; G=$GROUP; FILE=$FILE;"
}
start() {
env_check
config_check_create
}
editor() {
# FOLDER="$(ls)"
$EDITOR "$1"
# buck you, shellcheck
# how else am I supposed to check whether a file was created?
# shellcheck disable=SC2010
DIFF="$(ls | grep "$1")"
if [ -z "$DIFF" ]; then
debug_echo "editor :: No changes"
else
debug_echo "editor :: File $DIFF created"
FILE="$(pwd)/$FILE"
fi
}
main() {
# if ! setup_editor; then
# error "main :: No supported text editor found"
# fi
debug_echo "main :: EDITOR = $EDITOR"
start
parse_args "$@"
FILE="$(sh_path "$FILE")"
case $MODE in
edit)
debug_echo "main :: Edit mode"
if [ -z "$FILE" ] || [ -d "$FILE" ]; then
if [ -n "$FLAG_M" ]; then
debug_echo "main :: Most opened file; FILE=$FILE"
if [ -z "$GROUP" ]; then
GROUP="$(config_get_most_opened_group "$FILE")"
fi
FILE="$(config_get_most_opened "$FILE")"
else
debug_echo "main :: Most recent file; FILE=$FILE"
if [ -z "$GROUP" ]; then
GROUP="$(config_get_last_opened_group "$FILE")"
fi
FILE="$(config_get_last_opened "$FILE" "$GROUP")"
debug_echo "main :: GROUP = $GROUP"
fi
if [ -z "$FILE" ]; then
error "main :: No file specified"
fi
fi
editor "$FILE"
if [ -n "$(config_find "$GROUP" "$(dirname "$FILE")" "$(basename "$FILE")" )" ]; then
config_inc_count "$(dirname "$FILE")" "$(basename "$FILE")"
exit 0
elif [ "$(dirname "$FILE")" != "." ]; then
debug_echo "main :: File is in a directory"
config_add "$GROUP" "$(dirname "$FILE")" "$(basename "$FILE")" "1" "$(now)"
fi
;;
list)
debug_echo "main :: List mode"
display_list "$GROUP" "$AFTER_DATE" "$BEFORE_DATE" "$FILE"
;;
secret-log)
# $FILE can have multiple directories here
# as it's parsed as a remainder of the arguments
debug_echo "main :: Secret log mode"
display_log "$GROUP" "$AFTER_DATE" "$BEFORE_DATE" "$FILE"
;;
esac
exit 0
}
main "$@"