From 47bc5bf1e15f6db4244b2b4ed18cd3eaa03d5e51 Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Thu, 14 Apr 2022 21:37:00 -0700 Subject: [PATCH] =?UTF-8?q?pandoc-make:=20confirm=20before=20overwriting?= =?UTF-8?q?=20md=20and=20tex=20files=20(guess=20why=20I=20added=20this=20?= =?UTF-8?q?=F0=9F=99=84)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/shell/bin/pandoc-make | 44 ++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/.config/shell/bin/pandoc-make b/.config/shell/bin/pandoc-make index e79b22e..bbdc222 100755 --- a/.config/shell/bin/pandoc-make +++ b/.config/shell/bin/pandoc-make @@ -22,6 +22,17 @@ usage() { # 2) You can set default arguments in the array PANDOC_DEFAULT_ARGS and export them for this script to use } +confirm() { + echo -n "$@" + while true; do + read -r YN + case "$YN" in + [Yy]) return 0 ;; + *) return 1 ;; + esac + done +} + # Help menu (yes, I forget how to use my own programs sometimes) if [[ -z "$1" ]]; then usage && exit 1 @@ -36,16 +47,43 @@ if [[ -z "$2" ]]; then fi OUTPUT_FORMAT="$1" -SKIP_FIRST=true # don't process the first argument, it is the output format +SKIP=true # don't process the first argument, it is the output format for FILE in "$@";do - $SKIP_FIRST && SKIP_FIRST=false && continue + $SKIP && SKIP=false && continue + # PANDOC_ARGS="${PANDOC_DEFAULT_ARGS[*]:-"$PANDOC_ARGS"}" if [[ -f "$FILE" ]] && { [[ "${FILE:0:2}" != "./" ]] || [[ "${FILE:0:3}" != "../" ]]; }; then - pandoc -s "$FILE" "${PANDOC_ARGS[@]}" -o "${FILE%.*}.${OUTPUT_FORMAT##.}" + + OUTPUT_FILE="${FILE%.*}.${OUTPUT_FORMAT##.}" + + # Guard against overwriting md files and tex files + if { [[ "$OUTPUT_FORMAT" = md ]] || [[ "$OUTPUT_FORMAT" = tex ]]; } && [[ -f "$OUTPUT_FILE" ]]; then + if confirm "Overwrite $FILE? [y/N]: "; then + + # Make new file names until there is no overwriting + while [[ -f "$OUTPUT_FILE" ]]; do + OUTPUT_FILE="${FILE%.*}-$((FILE_NUM++)).${OUTPUT_FORMAT##.}" + done + echo "Not overwriting, new name: '$OUTPUT_FILE'" + else + # Give some time to ctrl-c before overwrite… + SLEEP_DURATION=1 + echo -n "'$FILE' will be overwritten in" + for N in {3..1};do + echo -n " $N" && sleep $SLEEP_DURATION + done + echo + fi + fi + + pandoc "${PANDOC_ARGS[@]}" -o "$OUTPUT_FILE" "$FILE" PANDOC_ARGS=() + else + PANDOC_ARGS+=("$FILE") + fi done