|
1 | 1 | pick_from_headers() { |
2 | 2 | local -n headers_ref=$1 |
| 3 | + local response="$2" |
3 | 4 |
|
4 | 5 | # Quit if no headers are provided |
5 | 6 | if [[ ${#headers_ref[@]} -eq 0 ]]; then |
6 | 7 | echo "No headers available to pick from. Maybe the AI response is empty or incorrectly formatted? You can test this by running the command again with the -r flag." >&2 |
7 | 8 | return 1 |
8 | 9 | fi |
9 | 10 |
|
10 | | - selected_line=$(printf '%s\n' "${headers_ref[@]}" | sort -rn | fzf --ansi --prompt="Select commit message: " --preview "echo {}") |
| 11 | + # Prerender all full messages to a temp file |
| 12 | + local temp_file=$(mktemp) |
| 13 | + local count=1 # Start at 1 to match nl numbering |
| 14 | + while IFS= read -r header; do |
| 15 | + local header_only="${header#* }" # Remove score prefix |
| 16 | + echo "=== MESSAGE $count ===" >>"$temp_file" |
| 17 | + echo "$response" | yq eval ".commitMessages[] | select(.header==\"$header_only\") | .header" - >>"$temp_file" |
| 18 | + local body=$(echo "$response" | yq eval ".commitMessages[] | select(.header==\"$header_only\") | .body // \"\"" -) |
| 19 | + local footer=$(echo "$response" | yq eval ".commitMessages[] | select(.header==\"$header_only\") | .footer // \"\"" -) |
| 20 | + [[ -n "$body" && "$body" != "null" ]] && echo "" >>"$temp_file" && echo "$body" >>"$temp_file" |
| 21 | + [[ -n "$footer" && "$footer" != "null" ]] && echo "" >>"$temp_file" && echo "$footer" >>"$temp_file" |
| 22 | + echo "" >>"$temp_file" |
| 23 | + ((count++)) |
| 24 | + done < <(printf '%s\n' "${headers_ref[@]}" | sort -rn) |
| 25 | + |
| 26 | + # Add a final marker to ensure the last message is captured correctly |
| 27 | + echo "=== END ===" >>"$temp_file" |
| 28 | + |
| 29 | + selected_line=$(printf '%s\n' "${headers_ref[@]}" | sort -rn | sed 's/^[0-9]* //' | nl -w1 -s' | ' | fzf --ansi \ |
| 30 | + --prompt="Select commit message: " \ |
| 31 | + --preview "num=\$(echo {} | grep -o '^[0-9]*'); sed -n \"/=== MESSAGE \$num ===/,/=== MESSAGE/p\" '$temp_file' | head -n -1 | tail -n +2" \ |
| 32 | + --preview-window=wrap) |
| 33 | + |
| 34 | + local exit_code=$? |
| 35 | + rm -f "$temp_file" |
| 36 | + |
11 | 37 | [[ -z "$selected_line" ]] && { |
12 | 38 | echo "No selection, aborting." >&2 |
13 | 39 | return 1 |
14 | 40 | } |
15 | | - echo "${selected_line#* }" |
| 41 | + |
| 42 | + # Remove the rank number and separator |
| 43 | + echo "${selected_line}" | sed 's/^[0-9]* | //' |
16 | 44 | } |
0 commit comments