Skip to content
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

[Feature Request] Action to remove/pop entries in the list #4185

Open
7 of 10 tasks
DanSM-5 opened this issue Jan 17, 2025 · 2 comments
Open
7 of 10 tasks

[Feature Request] Action to remove/pop entries in the list #4185

DanSM-5 opened this issue Jan 17, 2025 · 2 comments

Comments

@DanSM-5
Copy link
Contributor

DanSM-5 commented Jan 17, 2025

Checklist

  • I have read through the manual page (man fzf)
  • I have searched through the existing issues
  • For bug reports, I have checked if the bug is reproducible in the latest version of fzf

Output of fzf --version

0.57.0 (0476a65)

OS

  • Linux
  • macOS
  • Windows
  • Etc.

Shell

  • bash
  • zsh
  • fish

Problem / Steps to reproduce

I have encounter the use case in which I would like to remove 1 or more items in the list displayed in fzf.

For simplicity take the following example:

printf 'top\nmiddle\nbottom' | fzf --bind 'ctrl-a:execute(operation that makes "bottom" invalid for selection)'

Where the options top, middle and bottom are all valid options. However if you press ctrl-a to run some extra processing, then the bottom option stops being valid and should not be selected anymore.

My current solution for this is to add a reload action and filtering out the invalid option(s). However depending on the case reloading and filtering could be expensive/slow or add a lot of extra complexity. My assumption is that removing/filtering out options in fzf is probably faster and easier as the search already does that when fuzzy matching to narrow options.

The proposed feature is to include a pop action (name used for example) that could be used to remove items from the list.

  • Without arguments (pop) e.g. --bind 'ctrl-a:execute(foo {1})+pop+execute(something {+f})
    • When multiple entries are selected, they all get removed from the list.
    • When there is no selection (e.g. --no-multi), currently focused item is removed.
    • Template values ({1}, {n}, {+f}, etc.) should remain unchanged from the start of the --bind flag until the last action in the same --bind flag even if a pop action was used.
  • With arguments (pop(N)) e.g. --bind 'ctrl-a:execute(foo {1})+pop(5)+execute(something {+f})
    • Argument N represents a number, range of numbers 1..2 or space separated list 1 2 3 4..6 that represent the index of the entries to remove.
    • Templates that represent indexes are valid {n} and {+n}

This is just a high level description of how the feature could work but it is open for discussion if the feature is considered.

Thanks.

@junegunn
Copy link
Owner

While I agree that this can be useful, it's not trivial to implement. fzf maintains the items as a list of fixed-sized immutable chunks for better performance. Removing something in an immutable chunk affects the core premises of counting, matching, caching, etc. Adding a flag to each item would be another option to implement it, but it's also not without a cost (e.g. increased memory footprint, extra locking).

I'll leave this open and think about it over time.

While not ideal, there can be several strategies to deal with such situations. transform is one option.

(echo yes; echo no) | fzf --bind 'ctrl-a:transform:
  if [[ {} = yes ]]; then
    printf "execute:echo %s; read" {}
  else
    printf "change-header:ctrl-a not allowed for %s" {}
  fi
'

@DanSM-5
Copy link
Contributor Author

DanSM-5 commented Jan 17, 2025

I see, would changing the strategy from removing from the list to permanently hiding (until a reaload happens) be more feasible? I imagine there is some logic to hide entries that is already working with the fuzzy matching algorithm. The list would remain the same and be immutable but certain indexes can be marked to remain hidden and unselectable.

Also, thanks for the transform suggestion. I'll see if I can apply it in the meanwhile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants