Skip to content

Commit 0d59d5b

Browse files
committed
refactor(lib): enhance pick_from_headers to display full commit messages with fzf preview
Modified pick_from_headers function to prerender full commit messages (header, body, footer) into a temporary file and integrate with fzf's preview window. This allows users to see detailed message contents before selecting.
1 parent 08090ef commit 0d59d5b

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/lib/pick_from_headers.sh

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,44 @@
11
pick_from_headers() {
22
local -n headers_ref=$1
3+
local response="$2"
34

45
# Quit if no headers are provided
56
if [[ ${#headers_ref[@]} -eq 0 ]]; then
67
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
78
return 1
89
fi
910

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 | nl -w1 -s' ' | fzf --ansi \
30+
--prompt="Select commit message: " \
31+
--preview "sed -n '/=== MESSAGE {1} ===/,/=== 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+
1137
[[ -z "$selected_line" ]] && {
1238
echo "No selection, aborting." >&2
1339
return 1
1440
}
15-
echo "${selected_line#* }"
41+
42+
# Remove the line number and score prefix
43+
echo "${selected_line#* }" | sed 's/^[0-9]* //'
1644
}

src/root_command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ print_if_not_silent "AI response received. Presenting options for selection..."
4545

4646
# Parse AI response and let user select a commit message
4747
mapfile -t headers < <(extract_headers_from_response "$response")
48-
selected_header="$(pick_from_headers headers)"
48+
selected_header="$(pick_from_headers headers "$response")"
4949
selected_entry="$(select_entry "$selected_header" "$response")"
5050

5151
# Build the commit message and open it in the user's editor for review/editing

0 commit comments

Comments
 (0)