/
stup
executable file
·1859 lines (1617 loc) · 55.2 KB
/
stup
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
#!/bin/bash
# SOURCE: https://github.com/iridakos/stup
# MIT License
#
# Copyright (c) 2020 Lazarus Lazaridis
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#############
# Variables #
#############
# TODO: remove unused
NOTES=()
TEXT_BOLD=$(tput bold)
TEXT_BLACK=$(tput setaf 0)
TEXT_RED=$(tput setaf 1)
TEXT_GREEN=$(tput setaf 2)
TEXT_YELLOW=$(tput setaf 3)
TEXT_LIME_YELLOW=$(tput setaf 190)
TEXT_POWDER_BLUE=$(tput setaf 153)
TEXT_BLUE=$(tput setaf 4)
TEXT_MAGENTA=$(tput setaf 5)
TEXT_CYAN=$(tput setaf 6)
TEXT_WHITE=$(tput setaf 7)
TEXT_BRIGHT=$(tput bold)
TEXT_NORMAL=$(tput sgr0)
TEXT_BLINK=$(tput blink)
TEXT_REVERSE=$(tput smso)
TEXT_UNDERLINE=$(tput smul)
TEXT_NORMAL=$(tput sgr0)
TEXT_GREY=$(tput setaf 248)
NOTES_PREFIX='- '
# Version
# TODO: update version upon new releases
VERSION="v1.0.0"
# Some defaults (can be changed from configuration file)
DEFAULT_COMMAND="show"
DEFAULT_SHOW_AT="yesterday"
DEFAULT_LOG_DAYS=2
DEFAULT_ADD_AT="today"
DEFAULT_EDIT_AT="today"
DEFAULT_COPY_TO="today"
DEFAULT_COPY_FROM="yesterday"
STUP_PAGER="${PAGER:-more}"
# Use gdate instead of date when available.
if hash gdate 2>/dev/null; then
date()
{
gdate "$@"
}
fi
# Stup entry point
stup()
{
resolve_configuration_file
parse_options "$@"
if [ "$COMMAND" != "configure" ] && [ "$USAGE" != "true" ] && [ "$COMMAND" != "version" ]; then
ensure_configured
load_configuration
fi
if [ -z "$COMMAND" ] && (! [ "$USAGE" = "true" ] ); then
COMMAND="$DEFAULT_COMMAND"
fi
if ! [ -z "${PAGER_SWITCH+x}" ]; then
STUP_PAGER="$PAGER_SWITCH"
fi
execute
}
######################
# Command executions #
######################
# Issues the execution of the proper command
execute()
{
if [ "$USAGE" = "true" ]; then
execute_usage
return
fi
case "$COMMAND" in
show)
execute_show
;;
add)
execute_add
;;
edit)
execute_edit
;;
configure)
execute_configure
;;
add_category)
execute_add_category
;;
list_categories)
execute_list_categories
;;
order_categories)
execute_order_categories
;;
set_category_description)
execute_set_category_description
;;
log)
execute_log
;;
search)
execute_search
;;
copy)
execute_copy
;;
rename_category)
execute_rename_category
;;
version)
execute_version
;;
*)
print_error "Unknown command: '$COMMAND'"
exit 1
;;
esac
}
# Shows the notes added for a specific date
execute_show()
{
if [[ $1 ]]; then
AT="$1"
fi
resolve_date
extract_date_components
if [[ $CATEGORY ]]; then
target_file=$(resolve_target_file "$CATEGORY")
if [ -f "$target_file" ]; then
print_info "Displaying notes of category $CATEGORY added for $(success_color "$(display_date "$AT")")."
local description_file="$REPOSITORY_ROOT/$CATEGORY/description.md"
if [ -f "$description_file" ]; then
description="$(emphasize "$(cat $description_file)")"
else
description="$(emphasize "# Notes in category: $category")"
fi
print_warning "\n $description\n"
sed -e 's/^\-/•/' -e 's/^/ /' "$target_file"
echo ""
else
print_warning "No entries found for $(display_date $AT) in category $CATEGORY."
read -e -p "Do you want to retrieve the last notes added in this category with stup? (y/n) " search_last_entries
if [ "$(resolve_boolean "$search_last_entries")" = "true" ]; then
resolve_last_at
execute_show "$AT"
else
exit 0
fi
fi
else
load_categories
# Check if there are any entries for the given date
local entries_found=false
for category in "${CATEGORIES[@]}"; do
local target_file=$(resolve_target_file "$category")
if [ -s "$target_file" ]; then
entries_found=true
break
fi
done
if [ $entries_found == true ]; then
echo -e "Displaying notes added for $(display_date "$AT")."
for category in "${CATEGORIES[@]}"; do
local description_file="$REPOSITORY_ROOT/$category/description.md"
local target_file=$(resolve_target_file "$category")
local empty_category=false
if [ -s "$description_file" ]; then
description="$(emphasize "$(cat $description_file)")"
else
description="$(emphasize "# Notes in category: $category")"
fi
if [ -s "$target_file" ]; then
IFS=$'\n' read -d '' -r -a contents < <(cat "$target_file" | tr -d '[:space:]')
if [ ${#contents[@]} -eq 0 ]; then
empty_category=true
fi
else
empty_category=true
fi
if [ "$empty_category" = "true" ]; then
if [ "$INCLUDE_EMPTY" = true ]; then
print_warning "\n $description\n"
print_error " None."
fi
else
print_warning "\n $description\n"
sed -e 's/^\-/•/' -e 's/^/ /' "$target_file"
fi
done
echo ""
else
print_warning "No entries found for $(display_date $AT) in any category."
read -e -p "Do you want to retrieve the last notes added with stup? (y/n) " search_last_entries
if [ "$(resolve_boolean "$search_last_entries")" = "true" ]; then
resolve_last_at
execute_show "$AT"
else
exit 0
fi
fi
fi
}
# Adds notes to a specific date
execute_add()
{
resolve_required_category
resolve_date
extract_date_components
local number_of_notes=${#NOTES[@]}
if [ $number_of_notes -eq 0 ]; then
print_error "You haven't specified any notes to add."
print_hint "Use the --note or -n flag."
exit 1
fi
target_file="$(resolve_target_file $CATEGORY)"
if ! [ -f "$target_file" ]; then
mkdir -p "${target_file%/*}" && touch "$target_file"
fi
for item in "${NOTES[@]}"; do
echo "$NOTES_PREFIX$item" >> $target_file
done
echo -e "Successfully added $(emphasize "$number_of_notes") notes in category $(emphasize "$CATEGORY") for $(display_date $AT)"
}
# Opens the file keeping the notes of a specific date for edit in user's default editor
execute_edit()
{
resolve_required_category
resolve_date
extract_date_components
local target_file="$(resolve_target_file "$CATEGORY")"
print_info "About to edit: $target_file"
if ! [ -f "$target_file" ]; then
print_warning "Notes for category \"$CATEGORY\" on $(display_date "$AT") does not exist."
read -e -p "Do you want to create and edit the file? (y/n) " response
if ! [ "$(resolve_boolean "$response")" = 'true' ]; then
echo 'Aborted.'
exit 0
fi
fi
local target_directory="$(dirname "$target_file")"
mkdir -p "$target_directory"
edit_file "$target_file"
}
# Copies notes from one spcified date to another
execute_copy()
{
resolve_required_category
extract_from_to_dates
# Required for the target file
AT="$TO_AT"
extract_date_components
extract_copy_from_date_components
target_file="$(resolve_target_file $CATEGORY)"
if ! [ -f "$target_file" ]; then
mkdir -p "${target_file%/*}" && touch "$target_file"
fi
copy_from_file="$(resolve_copy_from_file $CATEGORY)"
if ! [ -s "$copy_from_file" ]; then
print_error "Failed to find notes for category $CATEGORY on $(display_date $FROM_AT). Looked up for file: $copy_from_file"
exit 1
fi
if [ "$copy_from_file" = $target_file ]; then
print_error "Origin date and target date must be different"
exit 1
fi
print_info "About to copy notes from $(display_date $FROM_AT) to $(display_date $TO_AT) for category $(emphasize $CATEGORY)"
IFS=$'\n\r' read -d '' -r -a notes_to_copy < $copy_from_file
add_all=false
for note in "${notes_to_copy[@]}"; do
if ! [ "$add_all" = true ]; then
ask=true
while [ "$ask" = true ]; do
ask=false
if [ "$invalid_input" = true ]; then
print_error "
y - Add note
n - Skip note
q - Skip all remaining notes
a - Add all remaining notes
"
fi
invalid_input=false
echo -e "\n\n$note\n\n"
read -e -p ">>> Copy this note? (yes/no/quit/all) " response
response="$(resolve_boolean "$response" "$(trim $response)")"
# TODO: move this into `resolve_ynqa` as soon as it's needed elsewhere
case "$response" in
true|false)
add_note="$response"
;;
q|Q|[qQ][uU][iI][tT])
exit 0
;;
a|A|[aA][lL][lL])
add_note=true
add_all=true
;;
*)
ask=true
invalid_input=true
;;
esac
done
fi
if [ "$add_note" = true ]; then
echo $note >> $target_file
fi
# Displaying only in case prompt is being skipped and all items are being
# added.
if [ "$add_all" = true ]; then
# Removing the prefix
added_note="$(sed "s/$NOTES_PREFIX//" <<< $note)"
echo -e "Added $(emphasize "$added_note") in category $(emphasize "$CATEGORY") for $(display_date $AT)"
fi
done
}
# Adds a category
execute_add_category()
{
if [ -z "$CATEGORY_NAME" ]; then
print_error "Missing required argument --category-name"
exit 1
fi
if [ -d "$REPOSITORY_ROOT/$CATEGORY_NAME" ]; then
print_warning "Category '$CATEGORY_NAME' directory already exists. Ensuring its presence in the categories configuration file as well."
else
mkdir -p "$REPOSITORY_ROOT/$CATEGORY_NAME"
print_success "Successfully created category '$CATEGORY_NAME'"
fi
store_category_configuration "$CATEGORY_NAME"
if ! [ -z "$CATEGORY_DESCRIPTION" ]; then
echo "$CATEGORY_DESCRIPTION" > "$REPOSITORY_ROOT/$CATEGORY_NAME/description.md"
fi
}
# Shows current categories
execute_list_categories()
{
load_categories
printf '%s\n' "${CATEGORIES[@]}"
}
# Opens the categories configuration file for edit
execute_order_categories()
{
local conf="$(resolve_categories_configuration_file)"
if ! [ -f "$conf" ]; then
print_error "Couldn't find categories configuration file: $conf"
exit 1
fi
edit_file "$conf"
}
# Updates the description of a given category
execute_set_category_description()
{
if [ -z "$CATEGORY_DESCRIPTION" ]; then
echo "Missing required argument --category-description"
exit 1
fi
if [ -z "$CATEGORY" ]; then
echo "No category specified. Changing the default: $DEFAULT_CATEGORY"
CATEGORY="$DEFAULT_CATEGORY"
fi
echo -e "$CATEGORY_DESCRIPTION" > "$REPOSITORY_ROOT/$CATEGORY/description.md"
}
# Configures stup via a wizard
execute_configure()
{
if ! [ -f "$CONFIG_FILE" ]; then
fist_time=true
touch -a "$CONFIG_FILE"
echo -e "categories_root=$HOME/stup\ndefault_category=personal\n" >> $CONFIG_FILE
fi
load_configuration
wizard "categories_root"
wizard "default_category"
wizard "stup_editor"
wizard "pager"
store_configuration
read -e -p "The config file contains some more goodies, would you like to edit it now? (y/n) " response
if ! [ "$(resolve_boolean "$response")" = true ]; then
exit 0
fi
edit_file "$CONFIG_FILE"
exit 0
}
# Logs note entries per date/category
execute_log()
{
load_categories
extract_from_to_dates
if (! [ -z "$FROM_AT" ] && [ "$FROM_AT" = "$TO_AT" ] ); then
AT="$FROM_AT"
COMMAND="show"
execute_show
exit
fi
if [ -n "$FROM_AT" ] && [ -n "$TO_AT" ] && [ -n "$DAYS" ]; then
print_error "You can use any pair of [\"--from\", \"--to\", \"--days\"], but not all three"
exit 1
fi
# Get the dates by which to filter
if [ -n "$DAYS" ] && [ -z "$FROM_AT" ] && [ -z "$TO_AT" ]; then
echo -e "$(print_info "Displaying notes for the last ${DAYS} days when notes exist")"
if [[ $CATEGORY ]]; then
target_path="$REPOSITORY_ROOT/$CATEGORY/"
else
target_path="$REPOSITORY_ROOT/"
fi
IFS=$'\n' read -r -d '' -a dates < <(find "$target_path" -type f | grep -Po '[0-9]{4}-[0-9]{2}-[0-9]{2}(?=\.md$)' | sort | uniq | tail -"$DAYS")
else
if [ -n "$DAYS" ]; then
if [ -n "$FROM_AT" ] && [ -z "$TO_AT" ]; then
TO_AT="$(date -d "$FROM_AT +$DAYS days" '+%F')"
elif [ -n "$TO_AT" ] && [ -z "$FROM_AT" ]; then
FROM_AT="$(date -d "$TO_AT $DAYS days ago" '+%F')"
fi
fi
if (! [ -z "$FROM_AT" ] && ! [ -z "$TO_AT" ]); then
echo -e "$(print_info "Displaying notes from") $(display_date "$FROM_AT") $(print_info "to") $(display_date "$TO_AT")"
target_command="sed -n "/$FROM_AT/,/$TO_AT/p""
elif ! [ -z "$FROM_AT" ]; then
echo -e "$(print_info "Displaying notes from") $(display_date "$FROM_AT") $(print_info "and after")"
target_command="sed -n "/$FROM_AT/,//p""
elif ! [ -z "$TO_AT" ]; then
echo -e "$(print_info "Displaying notes until") $(display_date "$TO_AT")"
target_command="sed "/$TO_AT/q""
else
# TODO: we should prompt for confirmation here: "Are you sure you want to view all of your notes?"
echo -e "$(print_info "Displaying all notes")"
target_command='tr -d ""'
fi
IFS=$'\n' read -d '' dates < <(
echo -e "$FROM_AT\n$TO_AT\n$(find $REPOSITORY_ROOT -path "$REPOSITORY_ROOT/*/*/*/*.md")" |
sed -e 's/.*\///' -e 's/\.[^.]*$//' |
sort -u |
$target_command
)
fi
local target_categories
if [ -n "$CATEGORY" ]; then
target_categories=("$CATEGORY")
else
target_categories=( "${CATEGORIES[@]}" )
fi
local output=""
for current_date in ${dates[@]}; do
AT="$current_date"
extract_date_components
local category_count=0
local category_output=""
for category in "${target_categories[@]}"; do
local target_file="$(resolve_target_file "$category")"
if [ -s "$target_file" ]; then
category_count=$((category_count + 1))
if [ "$category_count" -gt "1" ]; then
category_output+="\n"
fi
category_output+="$(printf "%-10s %s" "Category:" "$(print_warning "$category")")"
category_output+="\n\n"
category_output+="$(sed -e 's/^-/•/' -e 's/^/ /' "$target_file")\n"
fi
done
if [ -n "$category_output" ]; then
output+="$(printf "%-10s %s" "Date:" "$(emphasize "$(display_date "$AT")")")"
output+="\n"
output+="$category_output"
output+="\n"
fi
done
if [ -z "$output" ]; then
print_warning "Nothing found."
else
pager "$(echo -e "\n$output")"
fi
}
# Searches for notes
execute_search()
{
load_categories
extract_from_to_dates
if [ -z "$CATEGORY" ]; then
echo -e "$(print_info "Searching for term '$QUERY' in all categories.")"
else
echo -e "$(print_info "Searching for term '$QUERY' in category $(emphasize $CATEGORY).")"
fi
if (! [ -z "$FROM_AT" ] && ! [ -z "$TO_AT" ]); then
echo -e "$(print_info "Searching notes from") $(display_date "$FROM_AT") $(print_info "to") $(display_date "$TO_AT")"
target_command="sed -n "/$FROM_AT/,/$TO_AT/p""
elif ! [ -z "$FROM_AT" ]; then
echo -e "$(print_info "Searching notes from") $(display_date "$FROM_AT") $(print_info "and after")"
target_command="sed -n "/$FROM_AT/,//p""
elif ! [ -z "$TO_AT" ]; then
echo -e "$(print_info "Searching notes until") $(display_date "$TO_AT")"
target_command="sed "/$TO_AT/q""
else
echo -e "$(print_info "Searching all notes")"
target_command='tr -d ""'
fi
IFS=$'\n' read -d '' dates < <(
echo -e "$FROM_AT\n$TO_AT\n$(find $REPOSITORY_ROOT -path "$REPOSITORY_ROOT/*/*/*/*.md")" |
sed -e 's/.*\///' -e 's/\.[^.]*$//' |
sort -u |
$target_command
)
local target_categories
category_max_length=-1
if [ -n "$CATEGORY" ]; then
target_categories=("$CATEGORY")
category_max_length=$(chrlen=${#CATEGORY})
else
target_categories=( "${CATEGORIES[@]}" )
for c in "${CATEGORIES[@]}"; do
if [ ${#c} -gt $category_max_length ]; then
category_max_length=${#c}
fi
done
fi
category_max_length=$(( $category_max_length + 2 ))
local output=""
for current_date in ${dates[@]}; do
AT="$current_date"
extract_date_components
local category_count=0
local category_output=""
for category in "${target_categories[@]}"; do
local target_file="$(resolve_target_file "$category")"
if [ -s "$target_file" ]; then
category_count=$((category_count + 1))
local category_lines=()
while read line; do
if [[ $line =~ "$QUERY" ]]; then
category_lines+=("$line")
fi
done < "$target_file"
if [ ${#category_lines[@]} -gt 0 ]; then
for line in "${category_lines[@]}"; do
category_output+="$(printf "%s $(print_warning %"$category_max_length"s) %s" "[$current_date]" "[$category]" "$line\n")"
done
fi
fi
done
if [ -n "$category_output" ]; then
output+="$category_output"
fi
done
if [ -z "$output" ]; then
print_warning "Nothing found."
else
echo -e "\n$output" | more
fi
}
# Renames a category
execute_rename_category()
{
if [ -z "$CATEGORY_NAME" ]; then
print_error "Missing required argument: new category name (--category-name)"
exit 1
fi
resolve_required_category
mkdir -p "$(dirname "$REPOSITORY_ROOT/$CATEGORY_NAME")"
if [ -e "$REPOSITORY_ROOT/$CATEGORY_NAME" ] && ! [ -z "$(ls -A "$REPOSITORY_ROOT/$CATEGORY_NAME")" ]; then
print_error "Category can't be renamed, new location in filesystem is already in use"
exit 1
fi
mv "$REPOSITORY_ROOT/$CATEGORY" "$REPOSITORY_ROOT/$CATEGORY_NAME"
# Rename the category, including subcategories
sed -i -e 's/^'"$(sed_escape "$CATEGORY")"'/'"$(sed_escape "$CATEGORY_NAME")"'/g' "$(resolve_categories_configuration_file)"
if [ "$CATEGORY" = "$DEFAULT_CATEGORY" ]; then
DEFAULT_CATEGORY="$CATEGORY_NAME"
store_configuration
fi
print_success "Successfully renamed category \"$CATEGORY\" to \"$CATEGORY_NAME\""
}
# Prints usage information
execute_usage()
{
local output;
if [ -z "$COMMAND" ]; then
output="$(show_usage_all)"
else
case "$COMMAND" in
add|show|edit|configure|log|search|version)
output=$("show_usage_$COMMAND")
;;
add-category|list-categories|set-category-description|order-categories|rename-category)
output=$("show_usage_$(echo "$COMMAND" | sed 's/\-/_/g')")
;;
*)
print_error "Unknown command $COMMAND"
exit 1
;;
esac
fi
pager "$output"
}
execute_version()
{
echo "stup version $VERSION"
}
##########################
# Command helper methods #
##########################
# Ensures stup is configured
ensure_configured()
{
if ! [ -f "$CONFIG_FILE" ]; then
print_error "Failed to find configuration file: $CONFIG_FILE"
print_hint "Have you configured stup? If not, use: stup configure"
exit 1
fi
load_configuration
if [ -z "$REPOSITORY_ROOT" ]; then
print_error "Invalid stup configuration - missing categories root"
print_hint "Use \"stup configure\" to re-configure stup"
exit 1
fi
if [ -z "$DEFAULT_CATEGORY" ]; then
print_error "Invalid stup configuration - missing default category"
print_hint "Use \"stup configure\" to re-configure stup"
exit 1
fi
if ! [ -d "$REPOSITORY_ROOT" ]; then
print_error "Invalid stup configuration - categories root directory does not exist"
print_hint "Use \"stup configure\" to re-configure stup"
exit 1
fi
if ! [ -f "$(resolve_categories_configuration_file)" ]; then
print_error "Invalid stup configuration - missing categories configuration file"
print_hint "Use \"stup configure\" to re-configure stup"
exit 1
fi
}
# Parses the user's options
parse_options()
{
while [[ $# -gt 0 ]]; do
case "$1" in
today|tomorrow|yesterday)
AT="$1"
shift
;;
week|month|year|previous-week|previous-month|previous-year)
FROM_TO_ALIAS="$1"
if [ -z "$COMMAND" ];then
COMMAND="log"
fi
shift
;;
--from)
if [ -z "$2" ]; then
print_error "Flag \"--from\" was present but no \"from\" argument was passed"
exit 1
fi
FROM_AT="$2"
if [ -z "$COMMAND" ]; then
COMMAND="log"
fi
shift 2
;;
--to)
if [ -z "$2" ]; then
print_error "Flag \"--to\" was present but no \"to\" argument was passed"
exit 1
fi
TO_AT="$2"
if [ -z "$COMMAND" ]; then
COMMAND="log"
fi
shift 2
;;
--days)
if [ -z "$COMMAND" ]; then
COMMAND="log"
fi
if [ -z "$2" ]; then
DAYS="$DEFAULT_LOG_DAYS"
shift
else
DAYS="$2"
shift 2
fi
;;
--add|add)
COMMAND="add"
shift
;;
--show|show)
COMMAND="show"
shift
;;
--include-empty)
INCLUDE_EMPTY=true
shift
;;
--edit|edit)
COMMAND="edit"
shift
;;
@|-@|--at)
if [ -z "$2" ]; then
print_error "Flag \"@/--at\" was present but no \"@/at\" argument was passed"
exit 1
fi
AT="$2"
shift 2
;;
-h|--help|usage)
USAGE="true"
if [ -z "$2" ]; then
shift
else
COMMAND="$2"
shift 2
fi
;;
--add-category|add-category)
COMMAND="add_category"
shift
;;
--order-categories|order-categories)
COMMAND="order_categories"
shift
;;
--set-category-description|set-category-description)
COMMAND="set_category_description"
shift
;;
--list-categories|list-categories|categories)
COMMAND="list_categories"
shift
;;
--rename-category|rename-category)
COMMAND="rename_category"
shift
;;
--category-name)
if [ -z "$2" ]; then
print_error "Flag \"--category-name\" was present but no \"category-name\" argument was passed"
exit 1
fi
CATEGORY_NAME="$2"
shift 2
;;
--category-description)
if [ -z "$2" ]; then
print_error "Flag \"--category-description\" was present but no \"category-description\" argument was passed"
exit 1
fi
CATEGORY_DESCRIPTION="$2"
shift 2
;;
-c|--category)
load_configuration
load_categories
if [[ ! " ${CATEGORIES[@]} " =~ " ${2} " ]]; then
print_error "Category \"$2\" does not exist."
exit 1
fi
CATEGORY="$2"
shift 2
;;
-n|--note)
if [ -z "$2" ]; then
print_error "Flag \"--note\" was present but no \"note\" argument was passed"
exit 1
fi
NOTES+=("$2")
shift 2
;;
--configure|configure)
COMMAND="configure"
shift
;;
--log|log)
COMMAND="log"
shift
;;
--search|search)
COMMAND="search"
QUERY="$2"
if [ -z "$QUERY" ]; then
print_error "You haven't specified the query to search"
exit 1
fi
shift 2
;;
--copy|copy)
COMMAND="copy"
shift
;;
--pager)
if [ -z "$2" ]; then
print_error "Flag \"--pager\" was present but no \"pager\" argument was passed"
exit 1
fi
PAGER_SWITCH="$2"
shift 2
;;
--no-pager)
PAGER_SWITCH="false"
shift
;;
--) # end argument parsing
shift
break
;;
-v|--version)
COMMAND="version"
shift
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag \"$1\"" >&2
exit 1
;;
*)
echo "Error: Unknown command \"$1\"" >&2
exit 1
;;
esac
done
}
# Starts the configuration wizard
wizard()
{
case "$1" in
categories_root)
if [ -z "$REPOSITORY_ROOT" ]; then
REPOSITORY_ROOT="$HOME/stup"
fi
if [[ "$2" ]]; then
print_error "$2"