dahlia / iterfzf

Pythonic interface to fzf, a CLI fuzzy finder
https://pypi.python.org/pypi/iterfzf
GNU General Public License v3.0
164 stars 19 forks source link

Is there a way to sort the results by match? #18

Closed SqrtRyan closed 5 months ago

SqrtRyan commented 3 years ago

First off, thank you so much for this package - it's soooo useful!

That being said, is there a way to sort the results by how well they match? When I'm searching for large file paths, it's kinda annoying that irrelevant queries pop up first just by random chance.

For example, given the query 'abc', and file paths

/files/And/Butterflies/and/Cookies /abc /things/And/stuff/But/Crackers

it's annoying that /abc, the obvious best match, isn't on the far top or the far bottom. Is there a way to remedy this? Sorting is pretty fast in python, so I wouldn't mind waiting for it to re-sort the entire list every time the screen updates if nessecary

Thank you, Ryan

SqrtRyan commented 3 years ago

(Also, since it's not 100% clear what makes one match better than another, perhaps there's a way to pass it a custom sorting function somehow - so that I could tailor it to my use cases?)

jamc-code commented 3 years ago

(Also, since it's not 100% clear what makes one match better than another, perhaps there's a way to pass it a custom sorting function somehow - so that I could tailor it to my use cases?)

Hi Ryan! If I understood this question correctly, you should try putting a ' before you type in the prompt. Alternatively, have you tried passing the -e|--exact flag with iterfzf(exact=True)? It won't necessarily sort then better, but it does make the filtering a little bit cleaner as it removes the fuzzy finding. If these don't work, I can look making into a patch for you.

gikeymarcia commented 3 years ago

@SqrtRyan I've made a pull request #23 which should fix this issue. I was using iterfzf and noticed the poor matching (as compared to raw CLI fzf) and discovered it is because of a flag passed to the executable on every run. Disabling this flag seems to put higher quality matches at the top.

Gregory-K commented 5 months ago

@SqrtRyan, @gikeymarcia

Anyway, to brake legacy compatibility or not, is a decision for @dahlia to make (or by voting? I don't know the ways this project is managed).

In the meanwhile, if one doesn't want to mess with the lib's __init__.py (and do it every time it gets updated), here is a monkey patch that can go into your python script: (terrible failed attempt) here is an example to override the hardcoded '--no-sort':

selected = iterfzf(
   iterable, multi=False, query=query, cycle=True,
   __extra__=['--sort']  # override '--no-sort'
)
dahlia commented 5 months ago

I'm in favor of adding a sort=True option to the iterfzf() function. Please open a pull request and I'll review it.

Gregory-K commented 5 months ago

Just to be certain,

  1. sort=True as in replacing the, till now, default --no-sort (def. True)

OR

  1. sort=True as just an override for the default --no-sort (def. false)
dahlia commented 5 months ago

I think the default should be sort=False.