Skip to content

Commit 0408edb

Browse files
authored
Merge pull request #51 from tiosgz/rename-category
Rename category
2 parents 187c840 + ae68b75 commit 0408edb

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

stup

+58-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ execute()
137137
copy)
138138
execute_copy
139139
;;
140+
rename_category)
141+
execute_rename_category
142+
;;
140143
version)
141144
execute_version
142145
;;
@@ -661,6 +664,32 @@ execute_search()
661664
fi
662665
}
663666

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+
664693
# Prints usage information
665694
execute_usage()
666695
{
@@ -673,7 +702,7 @@ execute_usage()
673702
add|show|edit|configure|log|search|version)
674703
output=$("show_usage_$COMMAND")
675704
;;
676-
add-category|list-categories|set-category-description|order-categories)
705+
add-category|list-categories|set-category-description|order-categories|rename-category)
677706
output=$("show_usage_$(echo "$COMMAND" | sed 's/\-/_/g')")
678707
;;
679708
*)
@@ -806,6 +835,10 @@ parse_options()
806835
COMMAND="list_categories"
807836
shift
808837
;;
838+
--rename-category|rename-category)
839+
COMMAND="rename_category"
840+
shift
841+
;;
809842
--category-name)
810843
CATEGORY_NAME="$2"
811844
shift 2
@@ -1389,13 +1422,20 @@ trim() {
13891422
printf '%s' "$var"
13901423
}
13911424

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+
13921431
#################
13931432
# Usage helpers #
13941433
#################
13951434

13961435
# Shows usage for all commands
13971436
show_usage_all()
13981437
{
1438+
# TODO: DRY
13991439
echo -e "usage: stup command [<options>]"
14001440
echo -e "List of availabe commands:\n"
14011441

@@ -1437,6 +1477,10 @@ show_usage_all()
14371477
echo -e "$(emphasize "order-categories")"
14381478
echo -e "$(show_usage_order_categories)" | sed -e 's/^/ /'
14391479

1480+
echo ""
1481+
echo -e "$(emphasize "rename-category")"
1482+
echo -e "$(show_usage_rename_category)" | sed -e 's/^/ /'
1483+
14401484
echo ""
14411485
echo -e "$(emphasize "usage")"
14421486
echo -e "$(show_usage_usage)" | sed -e 's/^/ /'
@@ -1672,6 +1716,19 @@ usage: stup order-categories\n"
16721716
echo -e "stup uses to display the notes."
16731717
}
16741718

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+
16751732
# Shows usage for the list categories command
16761733
show_usage_list_categories()
16771734
{

0 commit comments

Comments
 (0)