Giter Club home page Giter Club logo

iterfzf's Introduction

iterfzf: Pythonic interface to fzf

Latest PyPI version Build status (GitHub Actions)

Demo session

iterfzf demo session

See also the API reference.

Key features

  • No dependency but only Python is required. Prebuilt fzf binary for each platform is bundled into wheels. Everything is ready by pip install iterfzf. (Note that not wheels of all supported platforms are uploaded to PyPI as they don't allow minor platforms e.g. FreeBSD. The complete wheels can be found from the GitHub releases.)
  • Consumes an iterable rather than a list. It makes UX way better when the input data is long but streamed from low latency network. It can begin to display items immediately after only part of items are ready, and before the complete items are ready.
  • Supports Python 3.8 or higher.

iterfzf.iterfzf(iterable, *, **options)

Consumes the given iterable of strings, and displays them using fzf. If a user chooses something it immediately returns the chosen things.

The following is the full list of parameters. Pass them as keyword arguments except for iterable which comes first:

iterable (required)

The only required parameter. Every element which this iterable yields is displayed immediately after each one is produced. In other words, the passed iterable is lazily consumed.

It can be an iterable of byte strings (e.g. [b'foo', b'bar']) or of Unicode strings (e.g. [u'foo', u'bar']), but must not be mixed (e.g. [u'foo', b'bar']). If they are byte strings the function returns bytes. If they are Unicode strings it returns Unicode strings. See also the encoding parameter.

sort
Sorts the result if True. False by default.
multi

True to let the user to choose more than one. A user can select items with tab/shift-tab. If multi=True the function returns a list of strings rather than a string.

False to make a user possible to choose only one. If multi=False it returns a string rather than a list.

For both modes, the function returns None if nothing is matched or a user cancelled.

False by default.

Corresponds to -m/--multi option.

bind

The key/event bindings to pass to fzf.

Dictionary of the form {KEY: ACTION} or {EVENT: ACTION}.

Corresponds to --bind=KEYBINDS option.

print_query

If True the return type is a tuple where the first element is the query the user actually typed, and the second element is the selected output as described above and depending on the state of multi.

False by default.

Corresponds to --print-query option.

New in version 0.3.0.

encoding

The text encoding name (e.g. 'utf-8', 'ascii') to be used for encoding iterable values and decoding return values. It's ignored when the iterable values are byte strings.

The Python's default encoding (i.e. sys.getdefaultencoding()) is used by default.

extended

True for extended-search mode. False to turn it off.

True by default.

True corresponds to -x/--extended option, and False corresponds to +x/--no-extended option.

exact

False for fuzzy matching, and True for exact matching.

False by default.

Corresponds to -e/--exact option.

case_sensitive

True for case sensitivity, and False for case insensitivity. None, the default, for smart-case match.

True corresponds to +i option and False corresponds to -i option.

query

The query string to be filled at first. (It can be removed by a user.)

Empty string by default.

Corresponds to -q/--query option.

prompt

The prompt sequence. ' >' by default.

Corresponds to --prompt option.

preview

The preview command to execute. None by default.

Corresponds to --preview option.

mouse

False to disable mouse. True by default.

Corresponds to --no-mouse option.

ansi

True to enable ansi colors mode. None by default.

Corresponds to --ansi option.

cycle

True to enable cycling scrolling.

False by default.

Corresponds to --cycle option.

__extra__

The iterable of extra raw options/arguments to pass to fzf.

Empty by default.

Author and license

The iterfzf library is written by Hong Minhee and distributed under GPLv3 or later.

The fzf program is written by Junegunn Choi and distributed under MIT license.

Changelog

Versioning scheme

Note that iterfzf does not follow Semantic Versioning. The version consists of its own major and minor number followed by the version of bundled fzf. For example, 1.2.3.4.5 means that iterfzf's own major version is 1, and its own minor version is 2, plus the version of fzf it bundles is 3.4.5.

/---------- 1. iterfzf's major version
|   /------ 3. bundled fzf's major version
|   |   /-- 5. bundled fzf's patch version
|   |   |
v   v   v
1.2.3.4.5
  ^   ^
  |   |
  |   \---- 4. bundled fzf's minor version
  \-------- 2. iterfzf's minor version

Version 1.5.0.54.3

To be released. Bundles fzf 0.54.3.

Version 1.4.0.54.3

Released on August 24, 2024. Bundles fzf 0.54.3.

Version 1.4.0.51.0

Released on May 7, 2024. Bundles fzf 0.51.0.

  • Added bind option. [#21, #36 by Gregory.K]

Version 1.3.0.51.0

Released on May 6, 2024. Bundles fzf 0.51.0.

  • Added sort option. [#18, #35 by Gregory.K]
  • Officially support Python 3.12.

Version 1.2.0.46.1

Released on March 6, 2024. Bundles fzf 0.46.1.

  • Close stdin before waiting to allow --select-1 to work. [#34 by Alex Wood]

Version 1.1.0.44.0

Released on November 18, 2023. Bundles fzf 0.44.0.

  • Added cycle option. [#33 by Daniele Trifirò]
  • Added __extra__ option. [#32]

Version 1.0.0.42.0

Released on September 18, 2023. Bundles fzf 0.42.0.

  • Dropped Python 2.7, 3.5, 3.6, and 3.7 supports.
  • Officially support Python 3.8, 3.9, 3.10, and 3.11.
  • Dropped FreeBSD i386, Linux i686, Linux armv8l, OpenBSD i386, and Windows 32-bit supports as fzf no longer supports them.
  • Dropped OpenBSD amd64 support.
  • Except the first parameter iterable, all parameters are enforced to be keyword-only. (Note that it's always been the recommended way to pass options, although it was not enforced.)
  • Added ansi option. [#16 by Erik Lilja]
  • The executable parameter now takes os.PathLike instead of str, which is backward compatible.
  • Added __version__ and __fzf_version__ attributes to the module.
  • Added POSIX_EXECUTABLE_NAME and WINDOWS_EXECUTABLE_NAME attributes to the module.
  • Module attribute EXECUTABLE_NAME is now a Literal['fzf', 'fzf.exe'] type, which is backward compatible with the previous str type.
  • Module attribute BUNDLED_EXECUTABLE is now Optional[pathlib.Path] type.

Version 0.5.0.20.0

Released on February 9, 2020. Bundles fzf 0.20.0.

  • Dropped Python 2.6, 3.3, and 3.4 supports.
  • Officially support Python 3.7 (it anyway had worked though).
  • Marked the package as supporting type checking by following PEP 561.
  • Added preview option. [#6 by Marc Weistroff]
  • Fixed a bug which had raised IOError by selecting an option before finished to load all options on Windows. [#3 by Jeff Rimko]

Version 0.4.0.17.3

Released on December 4, 2017. Bundles fzf 0.17.3.

Version 0.4.0.17.1

Released on October 19, 2017. Bundles fzf 0.17.1.

  • Added missing binary wheels for macOS again. (These were missing from 0.3.0.17.1, the previous release.)

Version 0.3.0.17.1

Released on October 16, 2017. Bundles fzf 0.17.1.

  • Added print_query option. [#1 by George Kettleborough]

Version 0.2.0.17.0

Released on August 27, 2017. Bundles fzf 0.17.0.

Version 0.2.0.16.11

Released on July 23, 2017. Bundles fzf 0.16.11.

Version 0.2.0.16.10

Released on July 23, 2017. Bundles fzf 0.16.10.

Version 0.2.0.16.8

Released on June 6, 2017. Bundles fzf 0.16.8.

  • Upgraded fzf from 0.16.7 to 0.16.8.

Version 0.2.0.16.7

Released on May 20, 2017. Bundles fzf 0.16.7.

  • Made sdists (source distributions) possible to be correctly installed so that older pip, can't deal with wheels, also can install iterfzf.

Version 0.1.0.16.7

Released on May 19, 2017. Bundles fzf 0.16.7. The initial release.

iterfzf's People

Contributors

awood avatar dahlia avatar dtrifiro avatar georgek avatar gregory-k avatar jeffrimko avatar lilja avatar marcw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

iterfzf's Issues

Async support

Currently iterfzf() only takes a regular iterable. As it's the async era in the Python world, we should anyhow supoort async iterables too.

Test suite

It would be helpful to start a test suite for this package.

Python crashes when result is too long

iterfzf seems to crash, when multi=True and a lot of long-ish lines are selected.

Just thought I'd open an issue, I may look into it in the next days and will open a PR if I find a fix.

Here's a snippet to test whether it "works on your system"

import iterfzf as fzf

line_length =  int(input("Input line length: ")) # use something between 50 and 100 
lines = [str(i) + '+'* line_length for i in range(200)]

fzf.iterfzf(lines, multi=True) # select ~100 elements

I have glanced at the implementation but didn't find any obvious issue, though I am no expert with subprocess.

How do I iterate over Dictionary values?

So basically I have a dictionary

d = {
"1":"foo",
"2":"bar",
"3":"spam",
"4":"Eggs"
}

I would like to be able to search through foo,bar etc and return the corresponding id. Even If I could get the key and value both of selection, that'd be fine as well.
I have tried following:

iterfzf(d,multi=True) which basically allows me to select the keys as dictionaries are iterable over keys.
then
iterfzf(d.values(),multi=True) shows nothing else other than 0 0 and exits without any errors
iterfzf(d.items(),multi=True) shows some weird behaviour and error as follows:

(tgD) > cat __init__.py                                                                                                finder
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: __init__.py
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ from iterfzf import iterfzf
   2   │ 
   3   │ 
   4   │ d = {
   5   │     "1":'Message',
   6   │     "2":'Message',
   7   │     "3":'Message',
   8   │     "4":'keke',
   9   │      }
  10   │ a = iterfzf(d.items(),multi=True)
  11   │ print(a)
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
(tgD) > python3 __init__.py                                                                                            finder
Traceback (most recent call last):
                                    File "__init__.py", line 10, in <module>
                                                                                a = iterfzf(d.items(),multi=True)
                                                                                                                   File "/home/akuma/SoopaProject/TelegramProjects/tgD/tgD/lib/python3.6/site-packages/iterfzf/__init__.py", line 81, in iterfzf
                                                                                                                      line = line.encode(encoding)
                    AttributeError: 'tuple' object has no attribute 'encode'
                                                                            Failed to read /dev/tty

It shows the error in exact disoriented manner.

I would like to know the correct way to achieve the desired result as well as once specified would go ahead and make the pr with appropriate inclusion in documentation. Thank you for this awesome module and for your generous time.
using d.iteritems gives following Error:

(tgD) > python3 __init__.py                                                                                            finder
Traceback (most recent call last):
                                    File "__init__.py", line 10, in <module>
                                                                                a = iterfzf(d.iteritems(),multi=True)
                                                                                                                     AttributeError: 'dict' object has no attribute 'iteritems'
                                                 %                                                                            (tgD) >                                                                                                           finder RC=1

EDIT: Added the iteritems error

Is there a way to sort the results by match?

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

Height Adjustment

Hi! Are there any plans to implement the "--height=HEIGHT[%]" argument included in fzf? iterfzf works perfectly for me, but a way to prevent it from taking up the whole screen would be nice.

Extra flags as a parameter feature

Hi there,

Wondering if it would make sense to allow for the hack of just passing extra parameters as a string to the iterfzf function.

This way, we could still use some flags if they are not individually implemented.

Raise exception when fzf gives 'Inappropriate ioctl for device'

In some contexts, fzf will immediately fail with 'inappropriate ioctl for device' (e.g., if you are on a non-interactive shell). From the code's perspective, this failure is silent and can only be inferred through the absence of a return value. I think it would be better to raise an exception so that it can be handled more gracefully, and to furthermore catch the error message so as to avoid side effects.

option to use our packaged version of fzf?

Hello,

Thank you for creating iterfzf. Is it possible to use the version of fzf that comes from our package manager, rather than the one that comes bundled with iterfzf? If iterfzf ever becomes unmaintained, users would be stuck on the last version of fzf that was bundled with iterfzf.

Incompatibility with PyInstaller on Windows

I tried to create an executable with PyInstaller from my script (pyinstaller --onefile script.py). This results in the following error:

Traceback (most recent call last):
  File "script.py", line 10, in <module>
    import iterfzf as fzf
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "C:\miniconda3\current\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\iterfzf\__init__.py", line 15, in <module>
  File "site-packages\pkg_resources\__init__.py", line 1134, in resource_exists
  File "site-packages\pkg_resources\__init__.py", line 1404, in has_resource
  File "site-packages\pkg_resources\__init__.py", line 1473, in _has
NotImplementedError: Can't perform this operation for unregistered loader type
[26980] Failed to execute script script

I am not sure if this problem is from PyInstaller or from iterfzf - I found a maybe related issue in their repo, with various other packages that are affected: pyinstaller/pyinstaller#2752

OS: Windows 10
Python: 3.7.3
PyInstaller: 3.5
iterfzf: 0.5.0.17.5 (installed from github; had the same issue with latest version from pip)

Release a next version

When installing iterfzf from PyPI the ansi feature does not exist. I have tried the code from this repository and it works. My assumption is that the PyPI project is not up to date with this repository.

Implementing fzf --bind

Hi,

I love your project, and use it a lot since I'm migrating my scripts to python. However, the --bind feature of fzf hasn't yet been implemented in iterfzf.

Is there a timeline for it?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.