diff --git a/README.md b/README.md index b9ab56c..7d6c0ab 100644 --- a/README.md +++ b/README.md @@ -549,6 +549,19 @@ $ stup copy --from 2020-01-15 --to 2020-02-01 $ stup copy --to tomorrow -c blocking ``` +If no notes are found on the `--from` date, stup will prompt the user to copy from the last date before that date: +``` +Failed to find notes for category personal on Thursday March 24, 2022. Looked up for file: /path/to/.stup/personal/2022/03/2022-03-24.md +Do you want to copy the last notes added in this category before Thursday March 24, 2022 with stup? (y/n) y +About to copy notes from Wednesday March 23, 2022 to Friday March 25, 2022 for category personal + +- Worked on some PRs + + +>>> Copy this note [y,n,q,a]?: +``` + + ### Add a new category To add a new category to save notes into use the following command. diff --git a/stup b/stup index da4abc6..3d9d683 100755 --- a/stup +++ b/stup @@ -331,7 +331,33 @@ execute_copy() 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 + read -e -p "Do you want to copy the last notes added in this category before $(display_date $FROM_AT) with stup? (y/n) " copy_from_last_entries + + if [ "$(resolve_boolean "$copy_from_last_entries")" = "true" ]; then + if [[ $CATEGORY ]]; then + copy_from_root_path="$REPOSITORY_ROOT/$CATEGORY" + else + copy_from_root_path="$REPOSITORY_ROOT" + fi + + # Find last date before the copy_from_date given + IFS=$'\n' read -r -d '' -a date < <(find "$copy_from_root_path" -type f | grep -Po '[0-9]{4}-[0-9]{2}-[0-9]{2}(?=\.md$)' | awk -v date="$FROM_AT" '$1 < date { print }' | sort | uniq | tail -1) + + if [ -z "$date" ]; then + print_error "Failed to find notes for category $CATEGORY before $(display_date $FROM_AT)." + exit 1 + fi + + FROM_AT=$date + + YEAR=$(echo $date | cut -d- -f1) + MONTH=$(echo $date | cut -d- -f2) + DAY=$(echo $date | cut -d- -f3) + + copy_from_file="$copy_from_root_path/$YEAR/$MONTH/$YEAR-$MONTH-$DAY.md" + else + exit 0 + fi fi if [ "$copy_from_file" = $target_file ]; then