Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion spec/cfme_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Describe 'cfme'
End

Mock fzf
echo "header: 1 entry header"
echo "1 | 1 entry header"
End

Mock mock_editor
Expand Down
32 changes: 30 additions & 2 deletions src/lib/pick_from_headers.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
pick_from_headers() {
local -n headers_ref=$1
local response="$2"

# Quit if no headers are provided
if [[ ${#headers_ref[@]} -eq 0 ]]; then
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
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]* | //'
}
2 changes: 1 addition & 1 deletion src/root_command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down