Skip to content

Instantly share code, notes, and snippets.

@paulc
Last active April 5, 2021 14:24
Show Gist options
  • Save paulc/a1484337201a2f34e88401bac10e3130 to your computer and use it in GitHub Desktop.
Save paulc/a1484337201a2f34e88401bac10e3130 to your computer and use it in GitHub Desktop.
Python fzf wrapper
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