diff --git a/spec/cfme_spec.sh b/spec/cfme_spec.sh index 2ecd487..1f2d324 100644 --- a/spec/cfme_spec.sh +++ b/spec/cfme_spec.sh @@ -28,7 +28,7 @@ Describe 'cfme' End Mock fzf - echo "header: 1 entry header" + echo "1 | 1 entry header" End Mock mock_editor diff --git a/src/lib/pick_from_headers.sh b/src/lib/pick_from_headers.sh index cff680f..e243753 100644 --- a/src/lib/pick_from_headers.sh +++ b/src/lib/pick_from_headers.sh @@ -1,5 +1,6 @@ pick_from_headers() { local -n headers_ref=$1 + local response="$2" # Quit if no headers are provided if [[ ${#headers_ref[@]} -eq 0 ]]; then @@ -7,10 +8,37 @@ pick_from_headers() { return 1 fi - selected_line=$(printf '%s\n' "${headers_ref[@]}" | sort -rn | fzf --ansi --prompt="Select commit message: " --preview "echo {}") + # Prerender all full messages to a temp file + local temp_file=$(mktemp) + local count=1 # Start at 1 to match nl numbering + while IFS= read -r header; do + local header_only="${header#* }" # Remove score prefix + echo "=== MESSAGE $count ===" >>"$temp_file" + echo "$response" | yq eval ".commitMessages[] | select(.header==\"$header_only\") | .header" - >>"$temp_file" + local body=$(echo "$response" | yq eval ".commitMessages[] | select(.header==\"$header_only\") | .body // \"\"" -) + local footer=$(echo "$response" | yq eval ".commitMessages[] | select(.header==\"$header_only\") | .footer // \"\"" -) + [[ -n "$body" && "$body" != "null" ]] && echo "" >>"$temp_file" && echo "$body" >>"$temp_file" + [[ -n "$footer" && "$footer" != "null" ]] && echo "" >>"$temp_file" && echo "$footer" >>"$temp_file" + echo "" >>"$temp_file" + ((count++)) + done < <(printf '%s\n' "${headers_ref[@]}" | sort -rn) + + # Add a final marker to ensure the last message is captured correctly + echo "=== END ===" >>"$temp_file" + + selected_line=$(printf '%s\n' "${headers_ref[@]}" | sort -rn | sed 's/^[0-9]* //' | nl -w1 -s' | ' | fzf --ansi \ + --prompt="Select commit message: " \ + --preview "num=\$(echo {} | grep -o '^[0-9]*'); sed -n \"/=== MESSAGE \$num ===/,/=== MESSAGE/p\" '$temp_file' | head -n -1 | tail -n +2" \ + --preview-window=wrap) + + local exit_code=$? + rm -f "$temp_file" + [[ -z "$selected_line" ]] && { echo "No selection, aborting." >&2 return 1 } - echo "${selected_line#* }" + + # Remove the rank number and separator + echo "${selected_line}" | sed 's/^[0-9]* | //' } diff --git a/src/root_command.sh b/src/root_command.sh index a22f095..b7fdd72 100644 --- a/src/root_command.sh +++ b/src/root_command.sh @@ -45,7 +45,7 @@ print_if_not_silent "AI response received. Presenting options for selection..." # Parse AI response and let user select a commit message mapfile -t headers < <(extract_headers_from_response "$response") -selected_header="$(pick_from_headers headers)" +selected_header="$(pick_from_headers headers "$response")" selected_entry="$(select_entry "$selected_header" "$response")" # Build the commit message and open it in the user's editor for review/editing