Fzf: Documentation for custom language bindings (e.g. Python, JavaScript, Ruby, R etc.)

Created on 22 Jan 2020  路  5Comments  路  Source: junegunn/fzf

  • [x] I have read through the manual page (man fzf)
  • [x] I have the latest version of fzf
  • [x] I have searched through the existing issues

Info

  • OS

    • [x] Linux

    • [x] Mac OS X

    • [ ] Windows

    • [ ] Etc.

  • Shell

    • [x] bash

    • [x] zsh

    • [x] fish

Problem / Steps to reproduce

This is more of a request for some more documentation than a bug report or feature request.

For a given language and its system call interface (see [1] for Python and [2] for R for example), how shall one construct a call for fzf with an array of strings (aka lines of a text file) as the input and go into the interactive fuzzy filtering mode supporting multiple selection etc., then return the results into an array of strings? For R, I am testing with something like this:

> filtered <- system2('fzf', input = c('my', 'test', 'strings'), stdout = TRUE)
inappropriate ioctl for device
Warning message:
In system2("fzf", input = c("my", "test", "strings"), stdout = TRUE) :
  running command ''fzf' < '/tmp/RtmpZkoqUH/file7bf174b3c1d7'' had status 2
> filtered <- system2('fzf', input = c('my', 'test', 'strings'))
inappropriate ioctl for device
> system2('fzf', input = c('my', 'test', 'strings'))
inappropriate ioctl for device
> system2('fzf', input = c('my', 'test', 'strings'), args = c('--filter', 'test')) # this works but not interactive
test

I am not asking for bindings for these languages; rather, a clear documentation for how the standard input, output and error devices need to be setup in the 3rd party system call probably should be enough (I couldn't find it in the Wiki yet and I assume vim.fzf already does something similar). Such a documentation may make it easier for users/developers to easily embed fzf into their workflow, without relying on an intermediate layer of binding package such as vim.fzf iterfzf for Python, fzf.el for Emacs etc.

[1] https://docs.python.org/3/library/subprocess.html#module-subprocess
[2] https://stat.ethz.ch/R-manual/R-devel/library/base/html/system2.html

tip

Most helpful comment

fzf gets inputs to interactive prompt from /dev/tty (normally stdin might point at /dev/tty , but since fzf always is reading items from a pipe on stdin, it has to explicitly open /dev/tty ).
In some circumstances I can see fzf complaining about 'can't open /dev/tty'.

All 5 comments

I wrote a post on the topic a few years ago:
https://junegunn.kr/2016/02/using-fzf-in-your-program

We could create a wiki page and put more examples.

Having said that, I do not think it's necessary that we build a complete collection of language binding examples. Handling standard input, error, and output in a language should not be too difficult for anyone who has enough experience with the language.

Glad to see that example; I agree that we could create a Wiki page expanding on your earlier post. I also agree there is no need to show it for collection of languages. Rather, we can describe the API (in this case, just the standard input/output and pipes?) and maybe even show a language-agnostic psudocode based in English that shows how to bind the redirections. This may be helpful as reading from the examples, I couldn't quite understand how to port that to R, Haskell, Lua or whatnot. Then we can include the examples you already have, plus maybe other's contributions when they come?

So I created the page: https://github.com/junegunn/fzf/wiki/Language-bindings
Please add more examples to it when you figure out how.

maybe even show a language-agnostic psudocode based in English that shows how to bind the redirections.

  • Start fzf process
  • Feed input to standard input of the process
  • Read standard output of the process when it finishes 馃帀

I understand that the to-be-filtered text is coming from standard input, and the filtered result goes to standard output. Hmm, after fiddling with R's system2 API, I realized I was confused where fzf gets inputs to its its interactive prompt, aka where you'd be typing the match queries when invoking fzf without --filter <query>. As you said in the new Wiki page, the filtered text during matching (finder) is sent to standard error. Does this happen continuously upon any changes to the match query, or periodically regardless of the input?

TL;DR where does fzf's finder interface gets its input (match queries) and does it dynamically push the filtered text to standard error during matching in the finder interface?

fzf gets inputs to interactive prompt from /dev/tty (normally stdin might point at /dev/tty , but since fzf always is reading items from a pipe on stdin, it has to explicitly open /dev/tty ).
In some circumstances I can see fzf complaining about 'can't open /dev/tty'.

Was this page helpful?
0 / 5 - 0 ratings