Last active
April 5, 2021 14:24
-
-
Save paulc/a1484337201a2f34e88401bac10e3130 to your computer and use it in GitHub Desktop.
Python fzf wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shlex,subprocess | |
def fzf(input,multi=False,args="",outf=lambda b:b.decode()): | |
cmd = ["fzf","--read0","--print0",*(["--multi"] if multi else []),*shlex.split(args)] | |
enc = lambda b:{bytes:lambda v:v,str:lambda v:v.encode()}.get(type(b),lambda v:str(v).encode())(b) | |
p = subprocess.run(cmd,input=b"\x00".join([enc(i) for i in input or []]),stdout=subprocess.PIPE) | |
if multi: | |
return [outf(i) for i in p.stdout.rstrip(b"\x00").split(b"\x00")] | |
else: | |
return outf(p.stdout.rstrip(b"\x00")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment