summaryrefslogtreecommitdiff
path: root/apps/bash_shared/menu_system.sh
blob: 7477ad9b427a71e13aaebb8de8f971bd38bd3cba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env bash

# =============================================================================
# AzerothCore Menu System Library
# =============================================================================
# This library provides a unified menu system for AzerothCore scripts.
# It supports ordered menu definitions, short commands, numeric selection,
# and proper argument handling.
#
# Features:
# - Single source of truth for menu definitions
# - Automatic ID assignment (1, 2, 3...)
# - Short command aliases (c, i, q, etc.)
# - Interactive mode: numbers + long/short commands
# - Direct mode: only long/short commands (no numbers)
# - Proper argument forwarding
#
# Usage:
#   source "path/to/menu_system.sh"
#   menu_items=("command|short|description" ...)
#   menu_run "Menu Title" callback_function "${menu_items[@]}" "$@"
# =============================================================================

# Global arrays for menu state (will be populated by menu_define)
declare -a _MENU_KEYS=()
declare -a _MENU_SHORTS=()
declare -a _MENU_OPTIONS=()

# Parse menu items and populate global arrays
# Usage: menu_define array_elements...
function menu_define() {
    # Clear previous state
    _MENU_KEYS=()
    _MENU_SHORTS=()
    _MENU_OPTIONS=()
    
    # Parse each menu item: "key|short|description"
    local item key short desc
    for item in "$@"; do
        IFS='|' read -r key short desc <<< "$item"
        _MENU_KEYS+=("$key")
        _MENU_SHORTS+=("$short")
        _MENU_OPTIONS+=("$key ($short): $desc")
    done
}

# Display menu with numbered options
# Usage: menu_display "Menu Title"
function menu_display() {
    local title="$1"
    
    echo "==== $title ===="
    for idx in "${!_MENU_OPTIONS[@]}"; do
        local num=$((idx + 1))
        printf "%2d) %s\n" "$num" "${_MENU_OPTIONS[$idx]}"
    done
    echo ""
}

# Find menu index by user input (number, long command, or short command)
# Returns: index (0-based) or -1 if not found
# Usage: index=$(menu_find_index "user_input")
function menu_find_index() {
    local user_input="$1"
    
    # Try numeric selection first
    if [[ "$user_input" =~ ^[0-9]+$ ]]; then
        local num=$((user_input - 1))
        if [[ $num -ge 0 && $num -lt ${#_MENU_KEYS[@]} ]]; then
            echo "$num"
            return 0
        fi
    fi
    
    # Try long command name
    local idx
    for idx in "${!_MENU_KEYS[@]}"; do
        if [[ "$user_input" == "${_MENU_KEYS[$idx]}" ]]; then
            echo "$idx"
            return 0
        fi
    done
    
    # Try short command
    for idx in "${!_MENU_SHORTS[@]}"; do
        if [[ "$user_input" == "${_MENU_SHORTS[$idx]}" ]]; then
            echo "$idx"
            return 0
        fi
    done
    
    echo "-1"
    return 1
}

# Handle direct execution (command line arguments)
# Disables numeric selection to prevent confusion with command arguments
# Usage: menu_direct_execute callback_function "$@"
function menu_direct_execute() {
    local callback="$1"
    shift
    local user_input="$1"
    shift
    
    # Disable numeric selection in direct mode
    if [[ "$user_input" =~ ^[0-9]+$ ]]; then
        echo "Invalid option. Numeric selection is not allowed when passing arguments."
        echo "Use command name or short alias instead."
        return 1
    fi
    
    # Find command and execute
    local idx
    # try-catch
    { 
        idx=$(menu_find_index "$user_input") 
    } || 
    {
        idx=-1
    }

    if [[ $idx -ge 0 ]]; then
        "$callback" "${_MENU_KEYS[$idx]}" "$@"
        return $?
    else
        # Handle help requests directly
        if [[ "$user_input" == "--help" || "$user_input" == "help" || "$user_input" == "-h" ]]; then
            echo "Available commands:"
            printf '%s\n' "${_MENU_OPTIONS[@]}"
            return 0
        fi

        echo "Invalid option. Use --help to see available commands." >&2
        return 1
    fi
}

# Handle interactive menu selection
# Usage: menu_interactive callback_function "Menu Title"
function menu_interactive() {
    local callback="$1"
    local title="$2"
    
    while true; do
        menu_display "$title"
        read -r -p "Please enter your choice: " REPLY
        
        # Parse input to separate command from arguments
        local input_parts=()
        read -r -a input_parts <<< "$REPLY"
        local user_command="${input_parts[0]}"
        local user_args=("${input_parts[@]:1}")
        
        # Find and execute command
        local idx
        idx=$(menu_find_index "$user_command")
        if [[ $idx -ge 0 ]]; then
            # Pass the command key and any additional arguments
            "$callback" "${_MENU_KEYS[$idx]}" "${user_args[@]}"
            local exit_code=$?
            # Exit loop if callback returns 0 (e.g., quit command)
            if [[ $exit_code -eq 0 && "${_MENU_KEYS[$idx]}" == "quit" ]]; then
                break
            fi
        else
            # Handle help request
            if [[ "$REPLY" == "--help" || "$REPLY" == "help" || "$REPLY" == "h" ]]; then
                echo "Available commands:"
                printf '%s\n' "${_MENU_OPTIONS[@]}"
                echo ""
                continue
            fi

            echo "Invalid option. Please try again or use 'help' for available commands." >&2
            echo ""
        fi
    done
}

# Main menu runner function
# Usage: menu_run "Menu Title" callback_function "$@"
# The menu items array should be defined globally before calling this function
function menu_run() {
    local title="$1"
    local callback="$2"
    shift 2
    
    # Define menu from globally available menu items array
    # This expects the calling script to have set up the menu items
    
    # Handle direct execution if arguments provided
    if [[ $# -gt 0 ]]; then
        menu_direct_execute "$callback" "$@"
        return $?
    fi
    
    # Run interactive menu
    menu_interactive "$callback" "$title"
}

# Alternative menu runner that accepts menu items directly
# Usage: menu_run_with_items "Menu Title" callback_function -- "${menu_items_array[@]}" -- "$@"
function menu_run_with_items() {
    local title="$1"
    local callback="$2"
    shift 2
    
    # Parse parameters: menu items are between first and second "--"
    local menu_items=()
    local script_args=()
    
    # Skip first "--"
    if [[ "$1" == "--" ]]; then
        shift
    else
        echo "Error: menu_run_with_items requires -- separator before menu items" >&2
        return 1
    fi
    
    # Collect menu items until second "--"
    while [[ $# -gt 0 && "$1" != "--" ]]; do
        menu_items+=("$1")
        shift
    done
    
    # Skip second "--" if present
    if [[ "$1" == "--" ]]; then
        shift
    fi
    
    # Remaining args are script arguments
    script_args=("$@")
    
    # Define menu from provided array
    menu_define "${menu_items[@]}"
    
    # Handle direct execution if arguments provided
    if [[ ${#script_args[@]} -gt 0 ]]; then
        menu_direct_execute "$callback" "${script_args[@]}"
        return $?
    fi
    
    # Run interactive menu
    menu_interactive "$callback" "$title"
}

# Utility function to show available commands (for --help)
# Usage: menu_show_help
function menu_show_help() {
    echo "Available commands:"
    printf '%s\n' "${_MENU_OPTIONS[@]}"
}

# Utility function to get command key by index
# Usage: key=$(menu_get_key index)
function menu_get_key() {
    local idx="$1"
    if [[ $idx -ge 0 && $idx -lt ${#_MENU_KEYS[@]} ]]; then
        echo "${_MENU_KEYS[$idx]}"
    fi
}

# Utility function to get all command keys
# Usage: keys=($(menu_get_all_keys))
function menu_get_all_keys() {
    printf '%s\n' "${_MENU_KEYS[@]}"
}