-
Notifications
You must be signed in to change notification settings - Fork 4
/
annote.sh
executable file
·2629 lines (2406 loc) · 84.7 KB
/
annote.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
###############################################################################
# ANNOTATE (Terminal Utility for Notes Management)
###############################################################################
__NAME__='annote'
__VERSION__='2.5.4'
# variables
c_red="$(tput setaf 196)"
c_cyan="$(tput setaf 44)"
c_light_green_2="$(tput setaf 10)"
c_yellow="$(tput setaf 190)"
c_orange="$(tput setaf 202)"
c_light_blue="$(tput setaf 42)"
c_light_green="$(tput setaf 2)"
c_light_red="$(tput setaf 9)"
b_black="$(tput setab 16)"
b_dark_black="$(tput setab 233)"
b_light_black="$(tput setab 236)"
s_bold="$(tput bold)"
s_dim="$(tput dim)"
s_underline="$(tput smul)"
p_reset="$(tput sgr0)"
config_file="$(realpath ~)/.annote/annote.config"
u_conf_file=""
declare -A kv_conf_var=()
db_loc="$(realpath ~)/.annote/db"
declare -A db_loc_var=()
db_loc_key=""
notes_loc="$db_loc/notes"
groups_loc="$db_loc/groups"
tags_loc="$db_loc/tags"
def_group="default"
def_tag="default"
editor="vi"
editor_gui="gedit"
list_delim=" "
list_fmt="<SNO><DELIM><NID><DELIM><TITLE>"
archived="ARCHIVED"
list_sort_type="F" # 'L|l' --> latest on top,
# 'O|o' --> oldest on top,
# 'M|m' --> recent modified on top
# 'F|f' --> display as they're found
declare -A mutex_ops=()
declare -A mutex_ops_args=()
flag_new_arg4="" # empty for editor, r -> record script, c -> content, f -> file
flag_modify_arg2="" # empty for editor, r -> record script, c -> content, f -> file
flag_append_mode="y" # 'y' --> append, empty to overwrite
flag_gui_editor="" # 'y' --> use gui editor, empty use cli
flag_disable_warnings="" # 'y' --> disable warnings, empty to enable
flag_verbose="" # 'y' --> be verbose, empty no verbose
flag_no_ask="" # 'y' --> no ask, empty to ask
flag_no_pretty="" # 'y' --> no pretty, empty to prettify
flag_no_pager="" # 'y' --> no pager, empty to use pager
flag_nosafe_grpdel="" # 'y' --> enable nosafe, empty for safe
flag_open_edit="" # 'y' --> edit, empty to view only pager
flag_modify_tag_mode="" # 'o' --> overwrite, 'd' -->delete, 'a' or empty append
flag_search_mode="" # 't' --> title only, 'n' --> note only, blank to search on both
flag_strict_find="" # 'y' --> to matches strictly, blank for anywhere search
flag_list_find="" # 'y' --> to show list of notes not count, blank for count
flag_show_archived="" # 'y' --> include archived notes into list/find, blank to exclude
flag_no_header="" # 'y' --> to remove header from output
flag_null_sep="" # 'y' --> to seperate records by null, empty to seperate by newline
flag_tee="" # 'y' --> tee the captured stdin to the stdout
flag_debug_mutex_ops="$DBG_MUTEX_OPS" # 'y'|'yes' --> enable mutex multi opts execution, PS: supplying multi opts can creates confusion.
ERR_CONFIG=1
ERR_DATE=2
ERR_MUTEX_OPTS=3
ERR_ARCH=4
ERR_ARGS=5
ERR_PERM=6
if ! $(shopt -q extglob); then
shopt -s extglob
fi
function __cont {
printf '%s' "${1//$p_reset/$p_reset$2}"
}
function _color {
printf '%s' "$2$(__cont "$1" "$2")$p_reset"
}
function as_red {
printf '%s' "$(_color "$1" "$c_red")"
}
function as_cyan {
printf '%s' "$(_color "$1" "$c_cyan")"
}
function as_yellow {
printf '%s' "$(_color "$1" "$c_yellow")"
}
function as_orange {
printf '%s' "$(_color "$1" "$c_orange")"
}
function as_light_blue {
printf '%s' "$(_color "$1" "$c_light_blue")"
}
function as_light_green {
printf '%s' "$(_color "$1" "$c_light_green")"
}
function as_light_green_2 {
printf '%s' "$(_color "$1" "$c_light_green_2")"
}
function as_light_red {
printf '%s' "$(_color "$1" "$c_light_red")"
}
function on_black {
printf '%s' "$(_color "$1" "$b_black")"
}
function on_light_black {
printf '%s' "$(_color "$1" "$b_light_black")"
}
function on_dark_black {
printf '%s' "$(_color "$1" "$b_dark_black")"
}
function as_bold {
printf '%s' "$(_color "$1" "$s_bold")"
}
function as_dim {
printf '%s' "$(_color "$1" "$s_dim")"
}
function as_underline {
printf '%s' "$(_color "$1" "$s_underline")"
}
function log_plain {
printf '%b\n' "$*"
}
function log {
local sym="$(as_bold "$(as_light_blue '*')")"
if [ $# -gt 1 ]; then
sym="$1"
shift
fi
log_plain "[$sym]: $*"
}
function log_error {
log "$(as_bold "$(as_red 'ERROR')")" "$*" >&2
}
function log_info {
log "$(as_cyan 'INFO')" "$*"
}
function log_warn {
if [ "x$flag_disable_warnings" != "xy" ]; then
log "$(as_light_red 'WARN')" "$*"
fi
}
#FIXME: fix verbose logger
function log_verbose {
if [ "x$flag_verbose" = "xy" ]; then
log "$*"
fi
}
#FIXME: fix verbose logger
function log_verbose_info {
if [ "x$flag_verbose" = "xy" ]; then
log_info "$*"
fi
}
function prompt {
as_cyan "$1"
}
function prompt_error {
as_red "$(as_bold "$1")"
}
function __banner__ {
log_plain "$(as_light_green_2 '')"
log_plain "$(as_light_green_2 ' █████╗ ███╗ ██╗███╗ ██╗ ██████╗ ████████╗ █████╗ ████████╗███████╗')"
log_plain "$(as_light_green_2 ' ██╔══██╗████╗ ██║████╗ ██║██╔═══██╗╚══██╔══╝██╔══██╗╚══██╔══╝██╔════╝')"
log_plain "$(as_light_green_2 ' ███████║██╔██╗ ██║██╔██╗ ██║██║ ██║ ██║ ███████║ ██║ █████╗ ')"
log_plain "$(as_light_green_2 ' ██╔══██║██║╚██╗██║██║╚██╗██║██║ ██║ ██║ ██╔══██║ ██║ ██╔══╝ ')"
log_plain "$(as_light_green_2 ' ██║ ██║██║ ╚████║██║ ╚████║╚██████╔╝ ██║ ██║ ██║ ██║ ███████╗')"
log_plain "$(as_light_green_2 " $(as_underline '╚═╝ ╚═╝╚═╝ ╚═══╝╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝')")"
log_plain "$(as_orange " $__NAME__ (v$__VERSION__)")"
}
function help {
local idnt_l1="$1"
local spaces=" "
local idnt_sc1="$idnt_l1$spaces"
local idnt_sc2="$idnt_sc1$spaces"
local idnt_sc3="$idnt_sc2$spaces"
local fsep_1="\t"
local fsep_2="$fsep_1\t"
local fsep_3="$fsep_2\t"
local fsep_4="$fsep_3\t"
local idnt_nl_prefx="$idnt_sc3$fsep_3"
log_plain "${idnt_l1}$(as_bold "-h")|$(as_bold "--help")${fsep_3}Show help and exit."
log_plain "${idnt_l1}$(as_bold "--info")${fsep_4}Show information and exit."
log_plain "${idnt_l1}$(as_bold "-V")|$(as_bold "--version")${fsep_3}Show version and exit."
log_plain "${idnt_l1}$(as_bold "-v")|$(as_bold "--verbose")${fsep_3}Be verbose."
log_plain "${idnt_l1}$(as_bold "-S")|$(as_bold "--silent")${fsep_3}Be silent, don't ask questions, use $(as_dim "default") values whenever required."
log_plain "${idnt_l1}$(as_bold "-q")|$(as_bold "--quite")${fsep_3}Disable warning messages to print."
log_plain "${idnt_l1}$(as_bold "--strict")${fsep_3}Use strict comparisions, exact matches. Effects $(as_bold "find") action."
log_plain "${idnt_l1}$(as_bold "--gui")${fsep_4}Use GUI Editor when editing note. Effects $(as_bold "new"), and $(as_bold "modify") actions."
log_plain "${idnt_l1}$(as_bold "--no-pretty")${fsep_3}Do not prettify output."
log_plain "${idnt_l1}$(as_bold "--no-header")${fsep_3}Do not display header in the output."
log_plain "${idnt_l1}$(as_bold "-z")|$(as_bold "-0")${fsep_4}Seperate record(s) by $(as_dim "NULL") character, instead of $(as_dim "NEWLINE")(default)."
log_plain "${idnt_l1}$(as_bold "--stdout")${fsep_3}Do not use pager, just put everything on $(as_bold "stdout")."
log_plain "${idnt_l1}$(as_bold "--inc-arch")${fsep_3}Include archived notes, default is to exclude. Effects $(as_bold "list") and $(as_bold "find") actions."
log_plain "${idnt_l1}$(as_bold "--tee")${fsep_4}Redirect the captured $(as_bold 'stdin') to $(as_bold 'stdout'). Effects $(as_bold 'new'), and $(as_bold 'modify') actions, and only when"
log_plain "${idnt_nl_prefx}recording note's content through $(as_bold 'stdin')."
log_plain "${idnt_l1}$(as_bold "--delim") [$(as_bold "$(as_light_green "delimiter")")]${fsep_2}Use $(as_bold "$(as_light_green "delimiter")") to delimit the list output fields."
log_plain "${idnt_l1}$(as_bold "--format") [$(as_bold "$(as_light_green "format")")]${fsep_2}Create custom $(as_underline "note listing") format with $(as_dim "$(as_underline "<SNO>")"),$(as_dim "$(as_underline "<NID>")"),$(as_dim "$(as_underline "<TITLE>")"),$(as_dim "$(as_underline "<TAGS>")"),$(as_dim "$(as_underline "<GROUP>")"),$(as_dim "$(as_underline "<DELIM>")")."
log_plain "${idnt_l1}$(as_bold "--sort-as") [$(as_bold "$(as_light_green "sf")")]${fsep_3}Use sort flag ($(as_bold "$(as_light_green "sf")")) to sort the notes listing. $(as_bold "$(as_light_green "sf")") can be: '$(as_dim 'L')|$(as_dim 'l')', '$(as_dim 'O')|$(as_dim 'o')', '$(as_dim 'F')|$(as_dim 'f')',"
log_plain "${idnt_nl_prefx}'$(as_dim 'M')|$(as_dim 'm')'. As $(as_underline "$(as_dim 'L')")atest on the top, $(as_underline "$(as_dim 'O')")ldest on the top, random as they're $(as_underline "$(as_dim 'F')")ound(default),"
log_plain "${idnt_nl_prefx}last $(as_underline "$(as_dim 'M')")odified on the top."
log_plain "${idnt_l1}$(as_bold "-C")|$(as_bold "--config")${fsep_3}Manages config, if specified, then atleast one option has to be supplied."
log_plain "${idnt_sc1}$(as_bold "-i")|$(as_bold "--import") [$(as_bold "$(as_light_green "file")")]${fsep_2}Import config from $(as_bold "$(as_light_green "file")")."
log_plain "${idnt_sc1}$(as_bold "-x")|$(as_bold "--export") [$(as_bold "$(as_light_green "file")")]${fsep_2}Export config to $(as_bold "$(as_light_green "file")")."
log_plain "${idnt_sc1}$(as_bold "-u")|$(as_bold "--use") [$(as_bold "$(as_light_green "file")")]${fsep_2}Use $(as_bold "$(as_light_green "file")") as current instance's config."
log_plain "${idnt_sc1}$(as_bold "-s")|$(as_bold "--set") [$(as_bold "$(as_light_green "'key=value'")")]${fsep_1}Overrides $(as_bold "$(as_light_green "key")") in current instance, repeat $(as_bold "set") to override more $(as_bold "$(as_light_green "key")")s."
log_plain "${idnt_l1}$(as_bold "-D")|$(as_bold "--db") [$(as_bold "$(as_light_green "dbl_key")")]${fsep_2}Use db '$(as_bold "$(as_light_green "dbl_key")")' from config file as current instance's db location."
log_plain "${idnt_l1}$(as_bold "-n")|$(as_bold "--new")${fsep_3}Add new Note. If no sub options suplied then this will assume $(as_dim "defaults") or $(as_dim "ask")."
log_plain "${idnt_sc1}$(as_bold "-t")|$(as_bold "--title") [$(as_bold "$(as_light_green "title")")]${fsep_2}Title of note. (required)"
log_plain "${idnt_sc1}$(as_bold "-g")|$(as_bold "--group") [$(as_bold "$(as_light_green "group")")]${fsep_2}Group for note (use '$(as_bold ".")' for subgroups). Uses $(as_dim "default") group, if not specified."
log_plain "${idnt_sc1}$(as_bold "-T")|$(as_bold "--tags") [$(as_bold "$(as_light_green "tags")")]${fsep_2}Tags for note (use '$(as_bold ",")' for multiple tags). Uses $(as_dim "default") tag, if not specified."
log_plain "${idnt_sc1}$(as_bold "-r")|$(as_bold "--record")${fsep_3}Record terminal activity as note, invokes the $(as_dim "$(as_underline "script")") command."
log_plain "${idnt_sc1}$(as_bold "-c")|$(as_bold "--content") [$(as_bold "$(as_light_green "content")")]${fsep_1}Use $(as_bold "$(as_light_green "content")") as note's content."
log_plain "${idnt_sc1}$(as_bold "-f")|$(as_bold "--file") [$(as_bold "$(as_light_green "file")")]${fsep_2}Record $(as_bold "$(as_light_green "file")") as note's content, use '$(as_bold "-")' to record from $(as_bold "stdin")"
log_plain "${idnt_l1}$(as_bold "-l")|$(as_bold "--list") <$(as_bold "dbs")>${fsep_3}List out notes from db. Output controlling options can affects it's output."
log_plain "${idnt_nl_prefx}If '$(as_bold "dbs")' is supplied, then $(as_bold "--list") will only display available db(s)."
log_plain "${idnt_l1}$(as_bold "-e")|$(as_bold "--erase")|$(as_bold "--delete")"
log_plain "${idnt_sc1}$(as_bold "--note") [$(as_bold "$(as_light_green "nid")")]${fsep_3}Delete note $(as_bold "$(as_light_green "nid")")(id)."
log_plain "${idnt_sc1}$(as_bold "--group") [$(as_bold "$(as_light_green "gname")")]${fsep_2}Delete group $(as_bold "$(as_light_green "gname")")(fully qualified name), and assign $(as_dim "default") group to notes."
log_plain "${idnt_sc1}$(as_bold "--group-nosafe") [$(as_bold "$(as_light_green "gname")")]${fsep_1}Delete group $(as_bold "$(as_light_green "gname")")(fully qualified name), also deletes the notes belongs to it."
log_plain "${idnt_sc1}$(as_bold "--tag") [$(as_bold "$(as_light_green "tname")")]${fsep_2}Delete tag $(as_bold "$(as_light_green "tname")"), assign $(as_dim "default") tag, if this was only tag to that note."
log_plain "${idnt_l1}$(as_bold "-o")|$(as_bold "--open") [$(as_bold "$(as_light_green "nid")")]${fsep_3}Open note $(as_bold "$(as_light_green "nid")")(id) in Pager."
log_plain "${idnt_sc1}$(as_bold "--edit")${fsep_3}Use Editor instead of Pager to open."
log_plain "${idnt_sc1}$(as_bold "--with") [$(as_bold "$(as_light_green "cmd")")]${fsep_3}Use command $(as_bold "$(as_light_green "cmd")") to open note's file($(as_dim 'n_file')). By default, $(as_dim "n_file") will be appended"
log_plain "${idnt_nl_prefx}at the end of $(as_bold "$(as_light_green "cmd")"), but if $(as_dim "n_file") needs to be supplied in between, then use '$(as_dim '{}')'"
log_plain "${idnt_nl_prefx}in the $(as_bold "$(as_light_green "cmd")") and $(as_dim 'n_file') will replace all occurences of $(as_dim '{}')."
log_plain "${idnt_l1}$(as_bold "-m")|$(as_bold "--modify")|$(as_bold "--edit") [$(as_bold "$(as_light_green "nid")")]${fsep_1}Edit note $(as_bold "$(as_light_green "nid")")(id), if none from $(as_dim "-r"),$(as_dim "-c"),$(as_dim "-f") are present, then open note with editor."
log_plain "${idnt_sc1}$(as_bold "-t")|$(as_bold "--title") [$(as_bold "$(as_light_green "title")")]${fsep_2}Modify title of note."
log_plain "${idnt_sc1}$(as_bold "-g")|$(as_bold "--group") [$(as_bold "$(as_light_green "group")")]${fsep_2}Modify group of note (use '$(as_bold ".")' for subgroups)."
log_plain "${idnt_sc1}$(as_bold "-T")|$(as_bold "--tag") [$(as_bold "$(as_light_green "tags")")]${fsep_2}Modify tags of note (use '$(as_bold ",")' for multiple tags)."
log_plain "${idnt_sc2}$(as_bold "--append")${fsep_3}Append new tags. (default)"
log_plain "${idnt_sc2}$(as_bold "--overwrite")${fsep_2}Overwrite with new tags."
log_plain "${idnt_sc2}$(as_bold "--delete")${fsep_3}Delete $(as_bold "$(as_light_green "tags")"), if present, and assign $(as_dim "default") tag, if note left with no tags."
log_plain "${idnt_sc1}$(as_bold "-r")|$(as_bold "--record")${fsep_3}Record terminal activity as note, invokes the $(as_dim "$(as_underline "script")") command."
log_plain "${idnt_sc1}$(as_bold "-c")|$(as_bold "--content") [$(as_bold "$(as_light_green "content")")]${fsep_1}Use $(as_bold "$(as_light_green "content")") as note's content."
log_plain "${idnt_sc1}$(as_bold "-f")|$(as_bold "--file") [$(as_bold "$(as_light_green "file")")]${fsep_2}Record $(as_bold "$(as_light_green "file")") as note's content, use '$(as_bold "-")' to record from $(as_bold "stdin")"
log_plain "${idnt_sc1}$(as_bold "-O")|$(as_bold "--no-append")|$(as_bold "--overwrite")${fsep_1}Do not append to note, and overwrite with new content."
log_plain "${idnt_l1}$(as_bold "-F")|$(as_bold "--find")|$(as_bold "--search")${fsep_2}Search and list. Output controlling or strict options can affects it's behaviour."
log_plain "${idnt_sc1}$(as_bold "--tags") [$(as_bold "$(as_light_green "pattern")")]${fsep_2}Search Tags with matching $(as_bold "$(as_light_green "pattern")"), and display number of notes belong to them."
log_plain "${idnt_sc2}$(as_bold "--list")${fsep_3}Display all notes instead of count of notes."
log_plain "${idnt_sc1}$(as_bold "--group") [$(as_bold "$(as_light_green "pattern")")]${fsep_2}Search Groups with matching $(as_bold "$(as_light_green "pattern")"), and display number of notes belong to them."
log_plain "${idnt_sc2}$(as_bold "--list")${fsep_3}Display all notes instead of count of notes."
log_plain "${idnt_sc1}$(as_bold "--note") [$(as_bold "$(as_light_green "pattern")")]${fsep_2}Search notes title & content for matching $(as_bold "$(as_light_green "pattern")")."
log_plain "${idnt_sc2}$(as_bold "--title-only")${fsep_2}Limit searching of $(as_bold "$(as_light_green "pattern")") to notes title only."
log_plain "${idnt_sc2}$(as_bold "--note-only")${fsep_2}Limit searching of $(as_bold "$(as_light_green "pattern")") to notes content only."
log_plain "${idnt_sc2}$(as_bold "--with-tags") [$(as_bold "$(as_light_green "tags")")]${fsep_1}Filter notes with $(as_bold "$(as_light_green "tags")")."
log_plain "${idnt_sc2}$(as_bold "--with-group") [$(as_bold "$(as_light_green "group")")]${fsep_1}Filter notes with $(as_bold "$(as_light_green "group")")."
log_plain "${idnt_sc2}$(as_bold "--created-on") <$(as_bold "$(as_light_green "date")")>${fsep_1}Filter notes with created on $(as_bold "$(as_light_green "date")"). $(as_bold "$(as_light_green "date")") and $(as_dim "sub-options") are mutually exclusive."
log_plain "${idnt_sc3}$(as_bold "--before") [$(as_bold "$(as_light_green "date")")]${fsep_1}Filter notes that are created before $(as_bold "$(as_light_green "date")")."
log_plain "${idnt_sc3}$(as_bold "--after") [$(as_bold "$(as_light_green "date")")]${fsep_2}Filter notes that are created after $(as_bold "$(as_light_green "date")")."
log_plain "${idnt_sc2}$(as_bold "--last-edit") <$(as_bold "$(as_light_green "date")")>${fsep_1}Filter notes with modified on $(as_bold "$(as_light_green "date")"). $(as_bold "$(as_light_green "date")") and $(as_dim "sub-options") are mutually exclusive."
log_plain "${idnt_sc3}$(as_bold "--before") [$(as_bold "$(as_light_green "date")")]${fsep_1}Filter notes that are modified before $(as_bold "$(as_light_green "date")")."
log_plain "${idnt_sc3}$(as_bold "--after") [$(as_bold "$(as_light_green "date")")]${fsep_2}Filter notes that are modified after $(as_bold "$(as_light_green "date")")."
log_plain "${idnt_l1}$(as_bold "--archive") [$(as_bold "$(as_light_green "nid")")]${fsep_3}Archive note $(as_bold "$(as_light_green "nid")")(id)."
log_plain "${idnt_l1}$(as_bold "--unarchive") [$(as_bold "$(as_light_green "nid")")]${fsep_2}Unarchive note $(as_bold "$(as_light_green "nid")")(id)."
log_plain "${idnt_l1}$(as_bold "--list-archive")${fsep_3}List archived notes."
}
function _info {
__banner__
log_plain "\n$(as_bold "[$(as_yellow "NAME")]")"
log_plain "\t$(as_underline "$(as_bold "Annotate") Terminal based notes managing utility for GNU/Linux OS.")"
log_plain "\n$(as_bold "[$(as_yellow "SYNOPSIS")]")"
log_plain "\t$(as_bold "annote") $(as_underline "OPTIONS")"
log_plain "\n$(as_bold "[$(as_yellow "DESCRIPTION")]")"
log_plain "\tYou can take note from terminal, or GUI, Capture file into note, or record script or pipe"
log_plain "\tcommands output to it to save for later, it can does that."
log_plain "\t$(as_dim "\"Take notes from wherever, whenever, orcestrate in any way, and it does the thing 'notes' ")"
log_plain "\t$(as_dim "are supposed to do so, whenever, wherever, however it doen't make any difference.\"")"
log_plain "\tIt's not an App that works in isolation, it's utility, that works in integration to terminal."
log_plain "\n$(as_bold "[$(as_yellow "OPTIONS")]")"
help '\t'
log_plain "\n$(as_bold "[$(as_yellow "EXIT STATUS")]")"
log_plain "\t$(as_bold "annote") exits with status $(as_bold "0") as success, greater than $(as_bold "0") if errors occur."
log_plain "\t$(as_bold "0") --> success."
log_plain "\t$(as_bold "$ERR_CONFIG") --> 'configuration' related error."
log_plain "\t$(as_bold "$ERR_DATE") --> 'date' related error."
log_plain "\t$(as_bold "$ERR_MUTEX_OPTS") --> mutex options provided."
log_plain "\t$(as_bold "$ERR_ARCH") --> error during archive/unarchive."
log_plain "\t$(as_bold "$ERR_ARGS") --> arguments related error."
log_plain "\t$(as_bold "$ERR_PERM") --> file permissions related error."
log_plain "\n$(as_bold "[$(as_yellow "AUTHORS")]")"
log_plain "\tDinesh Saini <https://github.com/dineshsaini/>"
log_plain "\n$(as_bold "[$(as_yellow "REPORTING BUGS")]")"
log_plain "\tFor bug reports, use the issue tracker at https://github.com/grayphi/annote/issues"
}
function info {
if [ "x$flag_no_pager" = "x" ]; then
_info | less -IR
else
_info
fi
}
function version {
log_plain "$__NAME__ (v$__VERSION__)"
}
function trim {
local v="$1"
v="${v##+([[:space:]])}"
v="${v%%+([[:space:]])}"
printf '%s' "$v"
}
function import_config {
local imf="$(trim "$1")"
if [ -n "$imf" ] && [ -f "$imf" ] && [ -r "$imf" ]; then
mkdir -p "$(dirname "$config_file")"
touch "$config_file"
sed -e 's/^\s\+//' -e '/^#/d' -e 's/\s\+$//' -e 's/\s\+=/=/' \
-e 's/=\s\+/=/' -e '/^=\?$/d' "$imf" >> "$config_file"
log_info "Done importing conf file"
else
log_error "Error while importing."
exit $ERR_CONFIG
fi
}
function export_config {
local exf="$(trim "$1")"
if [ -n "$exf" ]; then
sed -e 's/^\s\+//' -e '/^#/d' -e 's/\s\+$//' -e 's/\s\+=/=/' \
-e 's/=\s\+/=/' -e '/^=\?$/d' "$config_file" > "$exf"
log_info "Done exporting conf file"
else
log_error "Error while exporting."
exit $ERR_CONFIG
fi
}
function _set_key {
local key="$1"
local value="$2"
case "$key" in
"default_group")
def_group="$value"
;;
"default_tag")
def_tag="$value"
;;
"db_loc")
db_loc="$value"
;;
"dbl_key_"*)
db_loc_var["$key"]="$value"
;;
"editor")
editor="$value"
;;
"gui_editor")
editor_gui="$value"
;;
"list_delim")
list_delim="$value"
;;
"list_format")
list_fmt="$value"
;;
"list_sort_as")
set_sort_as "$value"
;;
*)
log_warn "Ignored unknown key: '$key', check config."
;;
esac
}
function _conf_file {
local cfile="$(trim "$1")"
if [ -n "$cfile" ] && [ -f "$cfile" ] && [ -r "$cfile" ]; then
while IFS== read -r key value; do
_set_key "$(trim "$key")" "$(trim "$value")"
done < <(sed -e 's/^\s\+//' -e '/^#/d' -e 's/\s\+$//' -e '/^=\?$/d' "$cfile")
fi
}
function _load_sys_conf {
_conf_file "$config_file"
}
function _load_user_conf {
_conf_file "$u_conf_file"
}
function store_kv_pair {
IFS== read k v < <(printf '%s' "$1")
k=$(trim "$k")
v=$(trim "$v")
if [ -n "$k" ]; then
kv_conf_var["$k"]="$v"
fi
}
function _set_keys_conf {
if [[ ${#kv_conf_var[@]} -ge 1 ]]; then
for i in "${!kv_conf_var[@]}"; do
_set_key "$i" "${kv_conf_var["$i"]}"
done
fi
}
function initialize_conf {
_load_sys_conf
_load_user_conf
_set_keys_conf
if [ -n "$db_loc_key" ]; then
local v_dbl="${db_loc_var["dbl_key_$db_loc_key"]}"
if [ -n "$v_dbl" ]; then
db_loc="$v_dbl"
else
log_error "Missing key('dbl_key_$db_loc_key'), or it's value for specified db('$db_loc_key')."
exit $ERR_CONFIG
fi
fi
notes_loc="$db_loc/notes"
groups_loc="$db_loc/groups"
tags_loc="$db_loc/tags"
mkdir -p "$db_loc"
mkdir -p "$notes_loc"
mkdir -p "$groups_loc"
mkdir -p "$tags_loc"
}
function note_exists {
local nid="$1"
local retval="false"
if [ -n "$nid" ] && [ -d "$notes_loc/$nid" ]; then
retval="true"
fi
printf '%s' "$retval"
}
function tag_exists {
local tname="$1"
local retval="false"
if [ -n "$tname" ] && [ -d "$tags_loc/$tname" ]; then
retval="true"
fi
printf '%s' "$retval"
}
function group_exists {
local gname="$1"
local retval="false"
if [ -n "$gname" ] && [ -d "$groups_loc/${gname//.//}" ]; then
retval="true"
fi
printf '%s' "$retval"
}
function get_group {
local group="${1//[^A-Za-z0-9._ ]/}"
group="$(trim "$group")"
group="${group//+([[:space:]])/_}"
group="${group//+(.)/.}"
group="${group//+(_)/_}"
group="${group#.}"
group="${group%.}"
if [ -z "$group" ]; then
group="$def_group"
fi
printf '%s' "$group"
}
function get_tags {
local tags="${1//[^A-Za-z0-9,_ ]/}"
tags="$(trim "$tags")"
tags="${tags//+([[:space:]])/_}"
tags="${tags//+(,)/,}"
tags="${tags//+(_)/_}"
tags="${tags#,}"
tags="${tags%,}"
if [ -z "$tags" ]; then
tags="$def_tag"
fi
printf '%s' "$tags"
}
function get_group_loc {
local group="$1"
local gl="$groups_loc/${group//.//}"
mkdir -p "$gl"
printf '%s' "$gl"
}
function get_tag_loc {
local tag="$1"
local tl="$tags_loc/$tag"
mkdir -p "$tl"
printf '%s' "$tl"
}
function __gen_id {
printf '%s' "n$(date '+%s')"
}
function _gen_note_id {
local nid="$(__gen_id)"
while [ -d "$notes_loc/$nid" ]; do
sleep "0.07s"
nid="$(__gen_id)"
done
printf '%s' "$nid"
}
function _create_note {
local nid="$(_gen_note_id)"
local nloc="$notes_loc/$nid"
mkdir "$nloc"
touch "$nloc/$nid.title"
touch "$nloc/$nid.note"
touch "$nloc/$nid.group"
touch "$nloc/$nid.tags"
touch "$nloc/$nid.metadata"
printf '%s\n' "Created On: $(date --date="@${nid#n}" '+%A %d %B %Y %r %Z')" > "$nloc/$nid.metadata"
printf '%s' "$nid"
}
function _new_note {
local title="$1"
local group="$2"
local tags="$3"
local nid="$(_create_note)"
local nloc="$notes_loc/$nid"
printf '%s' "$title" > "$nloc/$nid.title"
local gl="$(get_group_loc "$group")"
printf '%s' "$group" > "$nloc/$nid.group"
printf '%s\n' "$nid" >> "$gl/notes.lnk"
local tag=""
while IFS= read -d, -r tag ; do
# useless check, but let it be,in case read takes last ignored empty
if [ -n "$tag" ]; then
local tl="$(get_tag_loc "$tag")"
printf '%s\n' "$tag" >> "$nloc/$nid.tags"
printf '%s\n' "$nid" >> "$tl/notes.lnk"
fi
done < <(printf '%s' "$tags,")
printf '%s\n' "Modified On: $(date '+%A %d %B %Y %r %Z')" >> "$nloc/$nid.metadata"
printf '%s' "$nid"
}
function get_note {
local nid="$1"
printf '%s' "$notes_loc/$nid/$nid.note"
}
function get_note_title {
local nid="$1"
local tf="$notes_loc/$nid/$nid.title"
if [ -f "$tf" ] && [ -r "$tf" ]; then
printf '%s' "$(< "$tf")"
fi
}
function set_note_title {
local nid="$1"
local t_new="$2"
local tf="$notes_loc/$nid/$nid.title"
if [ -f "$tf" ] && [ -w "$tf" ]; then
printf '%s' "$t_new" > "$tf"
else
log_error "Could not able to set title, check file permission."
exit $ERR_PERM
fi
}
function get_note_tags {
local nid="$1"
local tf="$notes_loc/$nid/$nid.tags"
local tags=""
if [ -f "$tf" ]; then
tags="$(tr '\n' ',' < "$tf")"
printf '%s' "${tags%,}"
fi
}
function get_note_group {
local nid="$1"
local gf="$notes_loc/$nid/$nid.group"
if [ -f "$gf" ]; then
printf '%s' "$(< "$gf")"
fi
}
function change_note_group {
local nid="$1"
local g_new="$(get_group "$2")"
local g_old="$(get_note_group "$nid")"
if [ "x$g_new" != "x$g_old" ]; then
local ngf="$notes_loc/$nid/$nid.group"
local gl="$(get_group_loc "$g_old")"
local gf="$gl/notes.lnk"
sed -i.tmp -e "/^$nid$/d" "$gf"
rm "$gf.tmp" 2>/dev/null
gl="$(get_group_loc "$g_new")"
gf="$gl/notes.lnk"
printf '%s\n' "$nid" >> "$gf"
printf '%s' "$g_new" > "$ngf"
fi
}
function update_metadata {
local nid="$1"
local key="$2"
local value="$3"
local mfile="$notes_loc/$nid/$nid.metadata"
sed -i.tmp -e "/^$key:.*$/d" "$mfile"
rm "$mfile.tmp" 2>/dev/null
printf '%s\n' "$key: $value" >> "$mfile"
}
function get_metadata {
local nid="$1"
local key="$2"
local value=""
if $(note_exists "$nid"); then
local mfile="$notes_loc/$nid/$nid.metadata"
value="$(sed -n -e "s/^$key: //p" "$mfile")"
fi
printf '%s' "$value"
}
function add_note {
local title="$(trim "$1")"
local group="$(trim "$2")"
local tags="$(trim "$3")"
local note="$4"
title="${title//+([[:space:]])/ }"
group="${group//+([[:space:]])/ }"
tags="${tags//+([[:space:]])/ }"
if [ -z "$title" ]; then
read -r -p "$(prompt "Enter title for note:") " title
while [ -z "$title" ]; do
read -r -p "$(prompt_error \
"Enter title for note (can't be empty):") " title
done
fi
if [ "x$flag_no_ask" = "x" ] && [ -z "$group" ]; then
read -r -p "$(prompt \
"Enter group (use '.' for subgroups) (enter to skip):") " group
fi
if [ "x$flag_no_ask" = "x" ] && [ -z "$tags" ]; then
read -r -p "$(prompt \
"Enter tags (',' seperated) (enter to skip):") " tags
fi
group="$(get_group "$group")"
tags="$(get_tags "$tags")"
local nid="$(_new_note "$title" "$group" "$tags")"
local nf="$(get_note "$nid")"
case "$flag_new_arg4" in
"r")
log_info "Press '$(as_bold "Ctrl + D")' when exit recording to note."
script -q "$nf"
;;
"c")
printf '%s\n' "$note" > "$nf"
;;
"f")
if [ "x$note" = "x-" ]; then
while IFS= read -r line; do
printf '%s\n' "$line" >> "$nf"
[ "x$flag_tee" = "xy" ] && printf '%s\n' "$line"
done
else
if [ -n "$note" ] && [ -r "$note" ]; then
cat "$note" > "$nf"
fi
fi
;;
"")
if [ "x$flag_gui_editor" = "xy" ]; then
$editor_gui "$nf"
else
$editor "$nf"
fi
;;
esac
update_metadata "$nid" "Modified On" "$(date '+%A %d %B %Y %r %Z')"
}
function _list_prettify_fg {
local c="$1"
local txt="$2"
local bold="$3"
if [ "x$flag_no_pretty" != "xy" ]; then
if [ $(( c % 2 )) -eq 0 ]; then
txt="$(as_cyan "$txt")"
else
txt="$(as_light_green_2 "$txt")"
fi
if [ "x$bold" = "xy" ]; then
txt="$(as_bold "$txt")"
fi
fi
printf '%s' "$txt"
}
function _list_prettify_bg {
local c="$1"
local txt="$2"
if [ "x$flag_no_pretty" != "xy" ]; then
if [ $(( c % 2 )) -eq 0 ]; then
txt="$(on_light_black "$txt")"
else
txt="$(on_dark_black "$txt")"
fi
fi
if [ "x$flag_null_sep" = "xy" ]; then
printf '%s\0' "$txt"
else
printf '%s\n' "$txt"
fi
}
function make_header {
local sno="S.No."
local nid="ID"
local ngrp="GROUP"
local ntitle="TITLE"
local ntags="TAGS"
local nc=0
if [ "x$flag_no_header" = "xy" ]; then
return
fi
local fmt="${list_fmt//<DELIM>/$list_delim}"
local _fmt="$fmt"
while [[ $_fmt =~ (\<[a-zA-Z]+\>) ]]; do
(( ++nc ))
case "${BASH_REMATCH[1]}" in
\<SNO\>)
sno="$(_list_prettify_fg "$nc" "$sno" "y")"
fmt="${fmt//<SNO>/$sno}"
_fmt="${_fmt//<SNO>/}"
;;
\<NID\>)
nid="$(_list_prettify_fg "$nc" "$nid" "y")"
fmt="${fmt//<NID>/$nid}"
_fmt="${_fmt//<NID>/}"
;;
\<GROUP\>)
ngrp="$(_list_prettify_fg "$nc" "$ngrp" "y")"
fmt="${fmt//<GROUP>/$ngrp}"
_fmt="${_fmt//<GROUP>/}"
;;
\<TAGS\>)
ntags="$(_list_prettify_fg "$nc" "$ntags" "y")"
fmt="${fmt//<TAGS>/$ntags}"
_fmt="${_fmt//<TAGS>/}"
;;
\<TITLE\>)
ntitle="$(_list_prettify_fg "$nc" "$ntitle" "y")"
fmt="${fmt//<TITLE>/$ntitle}"
_fmt="${_fmt//<TITLE>/}"
;;
*)
_fmt="${_fmt//${BASH_REMATCH[1]}/}"
(( --nc ))
;;
esac
done
if [ "x$flag_no_pretty" != "xy" ]; then
fmt="$(on_black "$fmt")"
fi
if [ "x$flag_null_sep" = "xy" ]; then
printf '%s\0' "$fmt"
else
printf '%s\n' "$fmt"
fi
}
function _list_note {
local sno="$1"
local nid="$2"
local gt_flag="$3"
local nc=0
local ngrp="$(get_note_group "$nid")"
local ntitle="$(get_note_title "$nid")"
local ntags="$(get_note_tags "$nid")"
if [ -n "$gt_flag" ]; then
if [ "x$gt_flag" = "xg" ] && [ -n "$4" ]; then
ngrp="$4"
elif [ "x$gt_flag" = "xt" ] && [ -n "$4" ]; then
ntags="$4"
fi
fi
local fmt="${list_fmt//<DELIM>/$list_delim}"
local _fmt="$fmt"
while [[ $_fmt =~ (\<[a-zA-Z]+\>) ]]; do
(( ++nc ))
case "${BASH_REMATCH[1]}" in
\<SNO\>)
sno="$(_list_prettify_fg "$nc" "$sno" "y")"
fmt="${fmt//<SNO>/$sno}"
_fmt="${_fmt//<SNO>/}"
;;
\<NID\>)
nid="$(_list_prettify_fg "$nc" "$nid")"
fmt="${fmt//<NID>/$nid}"
_fmt="${_fmt//<NID>/}"
;;
\<GROUP\>)
ngrp="$(_list_prettify_fg "$nc" "$ngrp")"
fmt="${fmt//<GROUP>/$ngrp}"
_fmt="${_fmt//<GROUP>/}"
;;
\<TAGS\>)
ntags="$(_list_prettify_fg "$nc" "$ntags")"
fmt="${fmt//<TAGS>/$ntags}"
_fmt="${_fmt//<TAGS>/}"
;;
\<TITLE\>)
ntitle="$(_list_prettify_fg "$nc" "$ntitle")"
fmt="${fmt//<TITLE>/$ntitle}"
_fmt="${_fmt//<TITLE>/}"
;;
*)
_fmt="${_fmt//${BASH_REMATCH[1]}/}"
(( --nc ))
;;
esac
done
printf '%s' "$fmt"
}
function get_all_notes {
local n_l="$(find "$notes_loc" -mindepth 1 -maxdepth 1 -type d -printf '%f,')"
printf '%s' "${n_l%,}"
}
function set_sort_as {
local sf="$(trim "$1")"
case "$sf" in
[Ll])
list_sort_type='L'
;;
[Mm])
list_sort_type='M'
;;
[Oo])
list_sort_type='O'
;;
[Ff])
list_sort_type='F'
;;
*)
log_warn "Ignoring unknown value ('$sf') for list sorting, check help for details."
;;
esac
}
function sort_notes {
local nids="$1"
case "$list_sort_type" in
[Oo])
nids="$(sort <(printf '%b' "${nids//,/\\n}") | tr '\n' ',')"
;;
[Ll])
nids="$(sort -r <(printf '%b' "${nids//,/\\n}") | tr '\n' ',')"
;;
[mM])
nids="$(for n in ${nids//,/ }; do
local lm="$(get_metadata "$n" 'Modified On')"
lm="$(date --date="$lm" "+%s")"
printf '%s %s\0' "$n $lm"
done | sort -z -n -r -k2,2 | cut -z -s -d' ' -f1 | tr '\0' ',')"
;;
esac
printf '%s' "${nids%,}"
}
function _list_notes {
local nids="$1"
local c=0
local l=""
make_header
if [ -z "$nids" ]; then
nids="$(get_all_notes)"
fi
nids="$(sort_notes "$nids")"
for n in ${nids//,/ }; do
if [[ "x$flag_show_archived" = "x" ]] && $(is_archived "$n"); then
continue;
fi
c="$(( ++c ))"
l="$(_list_note "$c" "$n")"
_list_prettify_bg "$c" "$l"
done
}
function list_notes {
if [ "x$flag_no_pager" = "xy" ]; then
_list_notes "$1"
else
_list_notes "$1" | less -IR
fi
}
function build_header {
local fmt=""
local c=0
if [ "x$flag_no_header" = "xy" ]; then
return
fi
while [[ $# -gt 0 ]]; do
local arg="$1"
shift
arg="$(_list_prettify_fg "$c" "$arg" "y")"
fmt="$fmt$arg$list_delim"
c="$(( ++c ))"
done
if [ "x$flag_no_pretty" != "xy" ]; then
fmt="$(on_black "$fmt")"
fi
if [ "x$flag_null_sep" = "xy" ]; then
printf '%s\0' "$fmt"
else
printf '%s\n' "$fmt"