UX: Accept both numbers and names for target selection

Added resolve_target_input() helper function that allows users to select
targets by either:
- Number from the displayed list (e.g., "1")
- Full target name (e.g., "default")

CHANGES:
- New resolve_target_input() function (lines 107-124)
  - Detects if input is numeric with regex ^[0-9]+$
  - Uses sed to extract Nth line from list_targets output
  - Falls back to treating input as literal target name

- Updated all target selection points:
  - edit_target() - option 3
  - delete_target() - option 4
  - Menu option 5 (run backup)
  - Menu option 6 (view details)

- Changed prompts to clarify:
  - "Enter target number or name" (was "Enter target name")
  - Better error messages for invalid numbers

USER EXPERIENCE:
Before: User had to remember and type exact target name
After: User can simply type "1" from the numbered list

This addresses the user confusion where they entered "1" expecting
it to work like a numbered menu, but got "Target '1' does not exist"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
zaphod-black 2025-11-01 22:41:32 -05:00
parent 8ca8ae2fdc
commit 2bfe067231
2 changed files with 76 additions and 4 deletions

View file

@ -119,6 +119,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Manual backups always include block device even on non-Sunday
- Script version bumped to 1.1.0 for multi-target support
### Changed
- **Target selection now accepts both numbers and names**
- All target selection prompts now accept either:
- Target number from the displayed list (e.g., "1")
- Full target name (e.g., "default")
- Applies to: Edit, Delete, Run backup, View details
- Improves user experience - no need to remember target names
- More intuitive for users familiar with numbered menus
### Fixed
- **Main menu now loops properly after actions**
- Removed `exit 0` calls from all menu options except Exit

View file

@ -104,6 +104,25 @@ get_target_config_path() {
echo "$TARGETS_DIR/$name.conf"
}
# Convert user input (number or name) to target name
resolve_target_input() {
local input="$1"
# If input is a number, get the Nth target
if [[ "$input" =~ ^[0-9]+$ ]]; then
local target_name=$(list_targets | sed -n "${input}p")
if [ -n "$target_name" ]; then
echo "$target_name"
return 0
fi
return 1
fi
# Otherwise treat as target name
echo "$input"
return 0
}
migrate_legacy_config() {
# Check if old single-target config exists
if [ -f "$CONFIG_DIR/config" ] && [ ! -d "$TARGETS_DIR" ]; then
@ -230,7 +249,18 @@ edit_target() {
list_targets | nl
echo
TARGET_NAME=$(prompt "Enter target name to edit" "")
USER_INPUT=$(prompt "Enter target number or name" "")
if [ -z "$USER_INPUT" ]; then
error "No target specified"
return 1
fi
TARGET_NAME=$(resolve_target_input "$USER_INPUT")
if [ -z "$TARGET_NAME" ]; then
error "Invalid target number: $USER_INPUT"
return 1
fi
if ! validate_target_name "$TARGET_NAME"; then
return 1
@ -283,7 +313,18 @@ delete_target() {
list_targets | nl
echo
TARGET_NAME=$(prompt "Enter target name to delete" "")
USER_INPUT=$(prompt "Enter target number or name to delete" "")
if [ -z "$USER_INPUT" ]; then
error "No target specified"
return 1
fi
TARGET_NAME=$(resolve_target_input "$USER_INPUT")
if [ -z "$TARGET_NAME" ]; then
error "Invalid target number: $USER_INPUT"
return 1
fi
if ! validate_target_name "$TARGET_NAME"; then
return 1
@ -1961,7 +2002,18 @@ main() {
echo "Available targets:"
list_targets | nl
echo
TARGET_NAME=$(prompt "Enter target name to backup" "")
USER_INPUT=$(prompt "Enter target number or name to backup" "")
if [ -z "$USER_INPUT" ]; then
error "No target specified"
continue
fi
TARGET_NAME=$(resolve_target_input "$USER_INPUT")
if [ -z "$TARGET_NAME" ]; then
error "Invalid target number: $USER_INPUT"
continue
fi
if ! validate_target_name "$TARGET_NAME"; then
continue
@ -1979,7 +2031,18 @@ main() {
echo "Available targets:"
list_targets | nl
echo
TARGET_NAME=$(prompt "Enter target name to view" "")
USER_INPUT=$(prompt "Enter target number or name to view" "")
if [ -z "$USER_INPUT" ]; then
error "No target specified"
continue
fi
TARGET_NAME=$(resolve_target_input "$USER_INPUT")
if [ -z "$TARGET_NAME" ]; then
error "Invalid target number: $USER_INPUT"
continue
fi
if validate_target_name "$TARGET_NAME"; then
show_target_detail "$TARGET_NAME"