-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpick_from_headers.sh
More file actions
44 lines (37 loc) · 1.88 KB
/
pick_from_headers.sh
File metadata and controls
44 lines (37 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# 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
}
# Remove the rank number and separator
echo "${selected_line}" | sed 's/^[0-9]* | //'
}