|
| 1 | +function __personality_choose --description "Interactively choose a personality" |
| 2 | + # Check if gum is available |
| 3 | + if not command -q gum |
| 4 | + echo "Error: 'gum' is not installed" |
| 5 | + echo "Install it with: brew install gum" |
| 6 | + return 1 |
| 7 | + end |
| 8 | + |
| 9 | + # Get current active personality |
| 10 | + set -l active_personality (sqlite3 "$CAULDRON_DATABASE" " |
| 11 | + SELECT preference_value |
| 12 | + FROM user_preferences |
| 13 | + WHERE preference_key = 'active_personality' |
| 14 | + AND project_path IS NULL |
| 15 | + LIMIT 1 |
| 16 | + " 2>/dev/null) |
| 17 | + |
| 18 | + # Get all personalities with their details |
| 19 | + set -l personalities (sqlite3 "$CAULDRON_DATABASE" " |
| 20 | + SELECT name, display_name, description, is_builtin |
| 21 | + FROM personalities |
| 22 | + ORDER BY is_builtin DESC, name ASC |
| 23 | + " 2>/dev/null) |
| 24 | + |
| 25 | + if test -z "$personalities" |
| 26 | + echo "No personalities found" |
| 27 | + return 1 |
| 28 | + end |
| 29 | + |
| 30 | + # Build options array for gum with descriptions |
| 31 | + set -l options |
| 32 | + set -l name_map |
| 33 | + |
| 34 | + for line in $personalities |
| 35 | + set -l parts (string split '|' $line) |
| 36 | + set -l name $parts[1] |
| 37 | + set -l display_name $parts[2] |
| 38 | + set -l description $parts[3] |
| 39 | + set -l is_builtin $parts[4] |
| 40 | + |
| 41 | + # Create a formatted option |
| 42 | + set -l prefix " " |
| 43 | + if test "$name" = "$active_personality" |
| 44 | + set prefix "● " |
| 45 | + end |
| 46 | + |
| 47 | + # Add builtin badge if applicable |
| 48 | + set -l badge "" |
| 49 | + if test "$is_builtin" = "1" |
| 50 | + set badge " [built-in]" |
| 51 | + end |
| 52 | + |
| 53 | + # Truncate description if too long |
| 54 | + set -l short_desc $description |
| 55 | + if test (string length "$description") -gt 60 |
| 56 | + set short_desc (string sub -l 57 "$description")"..." |
| 57 | + end |
| 58 | + |
| 59 | + # Format: "● Display Name [built-in] - Description" |
| 60 | + set -l option "$prefix$display_name$badge - $short_desc" |
| 61 | + set options $options "$option" |
| 62 | + set name_map $name_map "$name" |
| 63 | + end |
| 64 | + |
| 65 | + # Show a nice header |
| 66 | + gum style \ |
| 67 | + --border rounded \ |
| 68 | + --border-foreground 213 \ |
| 69 | + --padding "0 1" \ |
| 70 | + --margin "1 0" \ |
| 71 | + "Choose Your Familiar's Personality" |
| 72 | + |
| 73 | + # Use gum choose to select with nice styling |
| 74 | + set -l selected_index 1 |
| 75 | + set -l choice (printf "%s\n" $options | gum choose \ |
| 76 | + --height 10 \ |
| 77 | + --header "Use arrow keys to navigate, Enter to select:" \ |
| 78 | + --header.foreground 99 \ |
| 79 | + --cursor.foreground 212 \ |
| 80 | + --selected.foreground 212 \ |
| 81 | + --cursor "→ ") |
| 82 | + |
| 83 | + if test -z "$choice" |
| 84 | + echo "No personality selected" |
| 85 | + return 0 |
| 86 | + end |
| 87 | + |
| 88 | + # Find the index of the selected choice |
| 89 | + for i in (seq (count $options)) |
| 90 | + if test "$options[$i]" = "$choice" |
| 91 | + set selected_index $i |
| 92 | + break |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + set -l selected_name $name_map[$selected_index] |
| 97 | + |
| 98 | + # Ask if they want to set it globally or for the project |
| 99 | + set -l in_git_repo (git rev-parse --show-toplevel >/dev/null 2>&1; and echo "yes"; or echo "no") |
| 100 | + |
| 101 | + echo "" |
| 102 | + if test "$in_git_repo" = "yes" |
| 103 | + set -l project_name (basename (git rev-parse --show-toplevel 2>/dev/null)) |
| 104 | + |
| 105 | + set -l scope (printf "Global (all projects)\nProject: $project_name\n" | gum choose \ |
| 106 | + --header "Set personality for:" \ |
| 107 | + --header.foreground 99 \ |
| 108 | + --cursor.foreground 212 \ |
| 109 | + --selected.foreground 212 \ |
| 110 | + --cursor "→ ") |
| 111 | + |
| 112 | + if test -z "$scope" |
| 113 | + echo "Cancelled" |
| 114 | + return 0 |
| 115 | + end |
| 116 | + |
| 117 | + echo "" |
| 118 | + if string match -q "Project:*" "$scope" |
| 119 | + __personality_set --project $selected_name |
| 120 | + else |
| 121 | + __personality_set $selected_name |
| 122 | + end |
| 123 | + else |
| 124 | + __personality_set $selected_name |
| 125 | + end |
| 126 | +end |
0 commit comments