Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename category #51

Merged
merged 2 commits into from Nov 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion stup
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ execute()
copy)
execute_copy
;;
rename_category)
execute_rename_category
;;
version)
execute_version
;;
Expand Down Expand Up @@ -661,6 +664,32 @@ execute_search()
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()
{
Expand All @@ -673,7 +702,7 @@ execute_usage()
add|show|edit|configure|log|search|version)
output=$("show_usage_$COMMAND")
;;
add-category|list-categories|set-category-description|order-categories)
add-category|list-categories|set-category-description|order-categories|rename-category)
output=$("show_usage_$(echo "$COMMAND" | sed 's/\-/_/g')")
;;
*)
Expand Down Expand Up @@ -806,6 +835,10 @@ parse_options()
COMMAND="list_categories"
shift
;;
--rename-category|rename-category)
COMMAND="rename_category"
shift
;;
--category-name)
CATEGORY_NAME="$2"
shift 2
Expand Down Expand Up @@ -1389,13 +1422,20 @@ trim() {
printf '%s' "$var"
}

# Escapes string for use in sed expression
# https://unix.stackexchange.com/q/32907#comment-491211 (comment on the question)
sed_escape() {
echo $1 | sed -r 's/([\$\.\*\/\[\\^])/\\\1/g' | sed 's/[]]/\[]]/g'
}

#################
# Usage helpers #
#################

# Shows usage for all commands
show_usage_all()
{
# TODO: DRY
echo -e "usage: stup command [<options>]"
echo -e "List of availabe commands:\n"

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

echo ""
echo -e "$(emphasize "rename-category")"
echo -e "$(show_usage_rename_category)" | sed -e 's/^/ /'

echo ""
echo -e "$(emphasize "usage")"
echo -e "$(show_usage_usage)" | sed -e 's/^/ /'
Expand Down Expand Up @@ -1672,6 +1716,19 @@ usage: stup order-categories\n"
echo -e "stup uses to display the notes."
}

# Shows usage for the rename category command
show_usage_rename_category()
{
echo -e "Renames a category (and all of its subcategories).
usage: stup rename-category [<options>]\n"

printf "\n%25s %s" "-c, --category" "Specifies the old name of the category to rename"
printf "\n%25s %s" " " "If omitted, stup will rename the default category"
printf "\n%25s %s" " " "Example: stup rename-category -c \"todo\""
printf "\n%25s %s" "--category-name" "Specifies the new name of the category"
printf "\n%25s %s" " " "Example: stup rename-category --category-name \"ideas\""
}

# Shows usage for the list categories command
show_usage_list_categories()
{
Expand Down