From 0d59d5b5d10dd3a485b9bad081082a48dfb54667 Mon Sep 17 00:00:00 2001 From: Kamiel de Visser Date: Thu, 30 Oct 2025 18:13:57 +0100 Subject: [PATCH 1/2] 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. --- src/lib/pick_from_headers.sh | 32 ++++++++++++++++++++++++++++++-- src/root_command.sh | 2 +- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/lib/pick_from_headers.sh b/src/lib/pick_from_headers.sh index cff680f..ed3f843 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 | nl -w1 -s' ' | fzf --ansi \ + --prompt="Select commit message: " \ + --preview "sed -n '/=== MESSAGE {1} ===/,/=== 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 line number and score prefix + 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 From 3c0a1fd77a7baa53db35e533ff631c449a36a986 Mon Sep 17 00:00:00 2001 From: Kamiel de Visser Date: Thu, 30 Oct 2025 18:19:43 +0100 Subject: [PATCH 2/2] refactor(lib): remove score from header picker and add `|` after rank --- spec/cfme_spec.sh | 2 +- src/lib/pick_from_headers.sh | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) 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 ed3f843..e243753 100644 --- a/src/lib/pick_from_headers.sh +++ b/src/lib/pick_from_headers.sh @@ -26,9 +26,9 @@ pick_from_headers() { # 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 | nl -w1 -s' ' | fzf --ansi \ + selected_line=$(printf '%s\n' "${headers_ref[@]}" | sort -rn | sed 's/^[0-9]* //' | nl -w1 -s' | ' | fzf --ansi \ --prompt="Select commit message: " \ - --preview "sed -n '/=== MESSAGE {1} ===/,/=== MESSAGE/p' '$temp_file' | head -n -1 | tail -n +2" \ + --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=$? @@ -39,6 +39,6 @@ pick_from_headers() { return 1 } - # Remove the line number and score prefix - echo "${selected_line#* }" | sed 's/^[0-9]* //' + # Remove the rank number and separator + echo "${selected_line}" | sed 's/^[0-9]* | //' }