amoffat / sh

Python process launching
https://sh.readthedocs.io/en/latest/
MIT License
6.98k stars 506 forks source link

Can you run `gum filter` with sh? #687

Closed dbohdan closed 1 year ago

dbohdan commented 1 year ago

Is it possible to use gum filter through sh? gum filter is an interactive input filter similar to fzf. It reads a list of options on standard input, lets the user choose one or more options with fuzzy search, then writes the options the user chose to standard output. This means you must first feed it the input then let the user interact. On Linux it opens /dev/tty for input and displays the search TUI through standard error, leaving standard output for the result.

Here is how you can run it with subprocess.run in recent Python:

import subprocess as sp

print(
    sp.run(
        ["gum", "filter"],
        encoding="utf-8",
        input="foo\nbar\nbaz\n",
        stdout=sp.PIPE,
    ).stdout
)

Can you do this with sh? The following code lets the user interact with the TUI and captures the output but does not provide an input.

import sh
import sys
print(sh.gum("filter", _in=sys.stdin, _err=sys.stderr))

I know there are Python-based alternatives to gum filter. (For example, there is pzp.) I am asking because I am curious if it can be done. I encountered this issue when porting a shell script that used gum filter to Python.