@@ -137,6 +137,9 @@ execute()
137
137
copy)
138
138
execute_copy
139
139
;;
140
+ rename_category)
141
+ execute_rename_category
142
+ ;;
140
143
version)
141
144
execute_version
142
145
;;
@@ -661,6 +664,32 @@ execute_search()
661
664
fi
662
665
}
663
666
667
+ # Renames a category
668
+ execute_rename_category ()
669
+ {
670
+ if [ -z " $CATEGORY_NAME " ]; then
671
+ print_error " Missing required argument: new category name (--category-name)"
672
+ exit 1
673
+ fi
674
+ resolve_required_category
675
+
676
+ mkdir -p " $( dirname " $REPOSITORY_ROOT /$CATEGORY_NAME " ) "
677
+ if [ -e " $REPOSITORY_ROOT /$CATEGORY_NAME " ] && ! [ -z " $( ls -A " $REPOSITORY_ROOT /$CATEGORY_NAME " ) " ]; then
678
+ print_error " Category can't be renamed, new location in filesystem is already in use"
679
+ exit 1
680
+ fi
681
+ mv " $REPOSITORY_ROOT /$CATEGORY " " $REPOSITORY_ROOT /$CATEGORY_NAME "
682
+ # Rename the category, including subcategories
683
+ sed -i -e ' s/^' " $( sed_escape " $CATEGORY " ) " ' /' " $( sed_escape " $CATEGORY_NAME " ) " ' /g' " $( resolve_categories_configuration_file) "
684
+
685
+ if [ " $CATEGORY " = " $DEFAULT_CATEGORY " ]; then
686
+ DEFAULT_CATEGORY=" $CATEGORY_NAME "
687
+ store_configuration
688
+ fi
689
+
690
+ print_success " Successfully renamed category \" $CATEGORY \" to \" $CATEGORY_NAME \" "
691
+ }
692
+
664
693
# Prints usage information
665
694
execute_usage ()
666
695
{
@@ -673,7 +702,7 @@ execute_usage()
673
702
add|show|edit|configure|log|search|version)
674
703
output=$( " show_usage_$COMMAND " )
675
704
;;
676
- add-category|list-categories|set-category-description|order-categories)
705
+ add-category|list-categories|set-category-description|order-categories|rename-category )
677
706
output=$( " show_usage_$( echo " $COMMAND " | sed ' s/\-/_/g' ) " )
678
707
;;
679
708
* )
@@ -806,6 +835,10 @@ parse_options()
806
835
COMMAND=" list_categories"
807
836
shift
808
837
;;
838
+ --rename-category|rename-category)
839
+ COMMAND=" rename_category"
840
+ shift
841
+ ;;
809
842
--category-name)
810
843
CATEGORY_NAME=" $2 "
811
844
shift 2
@@ -1389,13 +1422,20 @@ trim() {
1389
1422
printf ' %s' " $var "
1390
1423
}
1391
1424
1425
+ # Escapes string for use in sed expression
1426
+ # https://unix.stackexchange.com/q/32907#comment-491211 (comment on the question)
1427
+ sed_escape () {
1428
+ echo $1 | sed -r ' s/([\$\.\*\/\[\\^])/\\\1/g' | sed ' s/[]]/\[]]/g'
1429
+ }
1430
+
1392
1431
# ################
1393
1432
# Usage helpers #
1394
1433
# ################
1395
1434
1396
1435
# Shows usage for all commands
1397
1436
show_usage_all ()
1398
1437
{
1438
+ # TODO: DRY
1399
1439
echo -e " usage: stup command [<options>]"
1400
1440
echo -e " List of availabe commands:\n"
1401
1441
@@ -1437,6 +1477,10 @@ show_usage_all()
1437
1477
echo -e " $( emphasize " order-categories" ) "
1438
1478
echo -e " $( show_usage_order_categories) " | sed -e ' s/^/ /'
1439
1479
1480
+ echo " "
1481
+ echo -e " $( emphasize " rename-category" ) "
1482
+ echo -e " $( show_usage_rename_category) " | sed -e ' s/^/ /'
1483
+
1440
1484
echo " "
1441
1485
echo -e " $( emphasize " usage" ) "
1442
1486
echo -e " $( show_usage_usage) " | sed -e ' s/^/ /'
@@ -1672,6 +1716,19 @@ usage: stup order-categories\n"
1672
1716
echo -e " stup uses to display the notes."
1673
1717
}
1674
1718
1719
+ # Shows usage for the rename category command
1720
+ show_usage_rename_category ()
1721
+ {
1722
+ echo -e " Renames a category (and all of its subcategories).
1723
+ usage: stup rename-category [<options>]\n"
1724
+
1725
+ printf " \n%25s %s" " -c, --category" " Specifies the old name of the category to rename"
1726
+ printf " \n%25s %s" " " " If omitted, stup will rename the default category"
1727
+ printf " \n%25s %s" " " " Example: stup rename-category -c \" todo\" "
1728
+ printf " \n%25s %s" " --category-name" " Specifies the new name of the category"
1729
+ printf " \n%25s %s" " " " Example: stup rename-category --category-name \" ideas\" "
1730
+ }
1731
+
1675
1732
# Shows usage for the list categories command
1676
1733
show_usage_list_categories ()
1677
1734
{
0 commit comments