yes no box for file deletion confirmation

This commit is contained in:
2026-05-30 23:51:26 +08:00
parent bed888c3c3
commit 8db9a070b7
+96 -33
View File
@@ -1214,19 +1214,93 @@ do_open() {
esac esac
} }
# --- confirm overlay (yes/no popup) ---
confirm_overlay() {
_co_rows=$(term_rows); _co_cols=$(term_cols)
_co_yes=" Yes "
_co_no=" No "
_co_gap=" "
_co_content="${_co_yes}${_co_gap}${_co_no}"
# size box to fit prompt or buttons, whichever is wider
_co_w=$(( ${#_prompt} + 4 ))
[ "$_co_w" -lt $(( ${#_co_content} + 4 )) ] && _co_w=$(( ${#_co_content} + 4 ))
[ "$_co_w" -gt "$_co_cols" ] && _co_w=$((_co_cols - 2))
_co_iw=$((_co_w - 2))
_co_hl=$(printf '%*s' "$_co_iw" '' | tr ' ' '-')
_co_x=$(( (_co_cols - _co_w) / 2 )); [ "$_co_x" -lt 0 ] && _co_x=0
_co_y=$(( (_co_rows - 5) / 2 )); [ "$_co_y" -lt 1 ] && _co_y=1
_co_sel=2 # 1=Yes, 2=No (default No for safety)
while true; do
# draw box top
goto "$_co_y" "$_co_x"
printf '%s+%s+%s' "${BOLD}${CYAN}" "$_co_hl" "${RESET}"
# prompt line
goto "$((_co_y+1))" "$_co_x"
_co_pl=$((_co_iw - ${#_prompt}))
[ "$_co_pl" -lt 0 ] && _co_pl=0
_co_pad=$(printf '%*s' "$_co_pl" '')
printf '%s|%s%s%s%s|%s' "${BOLD}${CYAN}" "${RESET}" "$_prompt" "$_co_pad" "${BOLD}${CYAN}" "${RESET}"
# separator
goto "$((_co_y+2))" "$_co_x"
printf '%s|%s|%s' "${BOLD}${CYAN}" "$_co_hl" "${RESET}"
# yes/no buttons
goto "$((_co_y+3))" "$_co_x"
_co_cpad=$(( (_co_iw - ${#_co_content}) / 2 ))
[ "$_co_cpad" -lt 0 ] && _co_cpad=0
_co_left=$(printf '%*s' "$_co_cpad" '')
_co_cpad=$(( _co_iw - ${#_co_content} - _co_cpad ))
[ "$_co_cpad" -lt 0 ] && _co_cpad=0
_co_right=$(printf '%*s' "$_co_cpad" '')
printf '%s|%s%s' "${BOLD}${CYAN}" "${RESET}" "$_co_left"
if [ "$_co_sel" -eq 1 ]; then
printf '%s%s%s' "${REV}${GREEN}${BOLD}" "$_co_yes" "${RESET}"
else
printf '%s' "$_co_yes"
fi
printf '%s' "$_co_gap"
if [ "$_co_sel" -eq 2 ]; then
printf '%s%s%s' "${REV}${RED}${BOLD}" "$_co_no" "${RESET}"
else
printf '%s' "$_co_no"
fi
printf '%s%s|%s' "$_co_right" "${BOLD}${CYAN}" "${RESET}"
# box bottom
goto "$((_co_y+4))" "$_co_x"
printf '%s+%s+%s' "${BOLD}${CYAN}" "$_co_hl" "${RESET}"
# read key
IFS= read -r -n1 _co_ch 2>/dev/null || IFS= read -r _co_ch
case "$_co_ch" in
"$(printf '\033')")
IFS= read -r -n2 -t 0.1 _co_seq 2>/dev/null || _co_seq=""
case "$_co_seq" in
'[D'|'[C') # left/right arrow
if [ "$_co_sel" -eq 1 ]; then _co_sel=2; else _co_sel=1; fi ;;
*) return 1 ;; # bare esc or unrecognised sequence
esac ;;
h) _co_sel=1 ;;
l) _co_sel=2 ;;
j) _co_sel=2 ;;
k) _co_sel=1 ;;
y|Y) return 0 ;;
n|N) return 1 ;;
"$(printf '\n')"|"$(printf '\r')")
if [ "$_co_sel" -eq 1 ]; then return 0; else return 1; fi ;;
esac
done
}
do_delete() { do_delete() {
INFO_MSG="" INFO_MSG=""
_c=$(count_selected) _c=$(count_selected)
if [ "$_c" -gt 0 ]; then if [ "$_c" -gt 0 ]; then
# --- bulk delete selected --- # --- bulk delete selected ---
restore_term; show_cursor _prompt="Delete ${_c} selected items?"
goto "$(term_rows)" 1 confirm_overlay; _yes=$?
printf '%s%s Delete %d selected items? [y/N] %s' \ if [ "$_yes" -eq 0 ]; then
"${ERASE_LINE}" "${RED}${BOLD}" "$_c" "${RESET}"
IFS= read -r ans
setup_term; hide_cursor
case "$ans" in
y|Y)
_rest="$SELECTED" _rest="$SELECTED"
while [ -n "$_rest" ]; do while [ -n "$_rest" ]; do
_line="${_rest%% _line="${_rest%%
@@ -1244,45 +1318,34 @@ do_delete() {
[ "$SEL" -ge "$((COUNT - 1))" ] && SEL=$((COUNT - 2)) [ "$SEL" -ge "$((COUNT - 1))" ] && SEL=$((COUNT - 2))
[ "$SEL" -lt 0 ] && SEL=0 [ "$SEL" -lt 0 ] && SEL=0
load_entries load_entries
;; else
*) NEED_FULL_REDRAW=1 ;; NEED_FULL_REDRAW=1
esac fi
else else
# --- single delete --- # --- single delete ---
entry=$(get_entry "$SEL") entry=$(get_entry "$SEL")
_name="${entry%@}"; _name="${_name%/}" _name="${entry%@}"; _name="${_name%/}"
target="${CWD}/${_name}" target="${CWD}/${_name}"
restore_term; show_cursor _prompt="Delete \"${entry}\"?"
goto "$(term_rows)" 1 confirm_overlay; _yes=$?
printf '%s%s Delete "%s"? [y/N] %s' "${ERASE_LINE}" "${RED}${BOLD}" "$entry" "${RESET}" if [ "$_yes" -ne 0 ]; then
IFS= read -r ans NEED_FULL_REDRAW=1; return
setup_term; hide_cursor fi
case "$ans" in
y|Y)
if [ -d "$target" ]; then if [ -d "$target" ]; then
if [ -n "$(ls -A "$target" 2>/dev/null)" ]; then if [ -n "$(ls -A "$target" 2>/dev/null)" ]; then
restore_term; show_cursor _prompt="\"${entry}\" not empty. Delete ALL?"
goto "$(term_rows)" 1 confirm_overlay; _yes2=$?
printf '%s%s "%s" not empty. Delete ALL? [y/N] %s' \ if [ "$_yes2" -ne 0 ]; then
"${ERASE_LINE}" "${RED}${BOLD}" "$entry" "${RESET}" NEED_FULL_REDRAW=1; return
IFS= read -r ans2
setup_term; hide_cursor
case "$ans2" in
y|Y) rm -rf "$target" ;;
*) NEED_FULL_REDRAW=1; return ;;
esac
else
rm -rf "$target"
fi fi
fi
rm -rf "$target"
else else
rm -f "$target" rm -f "$target"
fi fi
[ "$SEL" -ge "$((COUNT - 1))" ] && SEL=$((COUNT - 2)) [ "$SEL" -ge "$((COUNT - 1))" ] && SEL=$((COUNT - 2))
[ "$SEL" -lt 0 ] && SEL=0 [ "$SEL" -lt 0 ] && SEL=0
load_entries load_entries
;;
*) NEED_FULL_REDRAW=1 ;;
esac
fi fi
} }