Skip to content

Commit de6cdfb

Browse files
authored
feat(picker): review full commit message in fzf picker with wrapping (#8)
* 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. * refactor(lib): remove score from header picker and add `|` after rank
1 parent 08090ef commit de6cdfb

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

spec/cfme_spec.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Describe 'cfme'
2828
End
2929

3030
Mock fzf
31-
echo "header: 1 entry header"
31+
echo "1 | 1 entry header"
3232
End
3333

3434
Mock mock_editor

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 | 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+
1137
[[ -z "$selected_line" ]] && {
1238
echo "No selection, aborting." >&2
1339
return 1
1440
}
15-
echo "${selected_line#* }"
41+
42+
# Remove the rank number and separator
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)