-
Notifications
You must be signed in to change notification settings - Fork 142
Add -I/--show-indexes flag for filter mode to output 0-based indexes #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add -I/--show-indexes flag for filter mode to output 0-based indexes #202
Conversation
Co-authored-by: elifarley <519940+elifarley@users.noreply.github.com>
Co-authored-by: elifarley <519940+elifarley@users.noreply.github.com>
Co-authored-by: elifarley <519940+elifarley@users.noreply.github.com>
… page quote Co-authored-by: elifarley <519940+elifarley@users.noreply.github.com>
Co-authored-by: elifarley <519940+elifarley@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds an -I/--show-indexes flag to fzy's filter mode that outputs 0-based indexes of matched items instead of their string values, enabling more robust scripting when working with formatted input data.
Key Changes:
- Added index tracking to the search result structure to preserve original positions during sorting
- Implemented
choices_getindex()function with bounds checking to retrieve original indexes - Added CLI flag
-I/--show-indexeswith corresponding option parsing and documentation
Reviewed Changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/choices.h | Added index field to scored_result struct and declared choices_getindex() function |
| src/choices.c | Implemented index tracking in search worker and added choices_getindex() with bounds checking |
| src/options.h | Added show_indexes field to options struct |
| src/options.c | Added -I/--show-indexes flag parsing and help text |
| src/fzy.c | Modified filter output to print indexes when flag is set |
| test/test_choices.c | Added test case to verify index tracking through search and sort |
| fzy.1 | Updated man page with flag documentation and usage example |
| _codeql_detected_source_root | Added CodeQL configuration file |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (n < c->available) { | ||
| return c->results[n].score; | ||
| } else { | ||
| return 0; | ||
| } |
Copilot
AI
Nov 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding bounds checking to choices_getscore() changes its behavior for out-of-bounds access from potentially undefined behavior to returning 0. This is inconsistent with choices_get() which has no bounds checking. Either add bounds checking to choices_get() as well, or remove it from both functions to maintain consistent error handling across the API.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
What
The filter mode (
-e/--show-matches) outputs matched values. This adds a flag to output their original 0-based indexes instead.Why
When using
fzyin scripts (specifically in filter mode,-e), it is often far more robust to receive the index of the selected item rather than its string value.This becomes critical when the input list is pre-formatted with rich content. Consider a script that displays a list of git branches, formatting each line with:
If
fzyreturns the full selected string, the calling script must parse this complex line, full of control characters and extra data, just to figure out which original item was selected. This process is brittle and error-prone.The
-Iflag provides a much cleaner solution. The script receives a simple integer (the index), which it can directly use to reference its original, unformatted data array. This makes index-based selection patterns simple and reliable.Changes
Core implementation:
indexfield toscored_resultstruct to preserve original positions during searchchoices_getindex()with bounds checkingfzy.cto emit indexes when flag is setCLI interface:
-I/--show-indexesflag to options parsingTesting:
test_choices_get_index()to verify index tracking through search and sortUsage
Without the flag,
fzyoutputs the matched values, sorted by relevance:With the
-Iflag,fzyoutputs the 0-based indexes of those same matches:This enables robust index-based selection patterns in scripts: