#!/usr/bin/python3 import subprocess import argparse import random from typing import List parser = argparse.ArgumentParser( prog="fzfsample", description="The dumbed down sample browser" ) parser.add_argument("-l", "--limit", action="store", help="limit sample count") parser.add_argument("-i", "--ignore", action="append", help="ignore string") parser.add_argument("-e", "--pattern", action="append", help="search for string") parser.add_argument("-r", "--random", action="store_true", help="random order for args") args = parser.parse_args() files = subprocess.getoutput(r"fd \(wav\|mp3\)$ .", errors="replace").splitlines() if args.random: random.shuffle(files) if args.ignore: files = [ file for file in files if not any(ignore.lower() in file.lower() for ignore in args.ignore) ] if args.pattern: files = [ file for file in files if all(pattern.lower() in file.lower() for pattern in args.pattern) ] if args.limit: files = files[: int(args.limit)] def fzf(items: List[str]) -> str: input = "\n".join(items) fzf_process = subprocess.Popen( ["fzf", "--preview", "mpv --no-video {}", "--preview-window=up:20%"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, ) for item in items: fzf_process.stdin.write(item + "\n") fzf_process.stdin.close() fzf_process.wait() selected_item = fzf_process.stdout.read().strip() return selected_item selection = fzf(files) if selection: subprocess.run(["dragon-drop", "--and-exit", selection])